Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@

package org.apache.storm.cluster;

/**
* Listener for connection state changes of the client backing an {@link IStateStorage}.
*/
@FunctionalInterface
public interface ClusterStateListener {
void stateChanged(ConnectionState newState);
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,18 @@

package org.apache.storm.cluster;

/**
* Connection state of a ZooKeeper client, as delivered to a {@link ClusterStateListener}.
*/
public enum ConnectionState {
/** The first successful connection. */
CONNECTED,
/** The connection was re-established after being suspended or lost. */
RECONNECTED,
LOST
/** The session is considered lost (expired or the connection timed out). */
LOST,
/** The connection was interrupted (ZooKeeper Disconnected); the session may still be alive. */
SUSPENDED,
/** Connected in read-only mode (ZooKeeper ConnectedReadOnly). */
READ_ONLY
}
11 changes: 7 additions & 4 deletions storm-client/src/jvm/org/apache/storm/cluster/IStateStorage.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
import java.io.Closeable;
import java.util.List;
import org.apache.storm.callback.ZKStateChangedCallback;
import org.apache.storm.shade.org.apache.curator.framework.state.ConnectionStateListener;
import org.apache.storm.shade.org.apache.zookeeper.data.ACL;

/**
Expand Down Expand Up @@ -206,11 +205,15 @@ public interface IStateStorage extends Closeable {
void delete_worker_hb(String path);

/**
* Add a StateStorageListener to the connection.
* Add a listener for connection state changes of the underlying store.
*
* @param listener A StateStorageListener to handle changing cluster state events.
* <p>This takes Storm's own {@link ClusterStateListener}. Earlier releases took Curator's
* {@code ConnectionStateListener}; implementations of {@code storm.cluster.state.store} must
* be updated accordingly.
*
* @param listener A ClusterStateListener to handle changing cluster state events.
*/
void add_listener(ConnectionStateListener listener);
void add_listener(ClusterStateListener listener);

/**
* Force consistency on a path. Any writes committed on the path before this call will be
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
import org.apache.storm.generated.HBServerMessageType;
import org.apache.storm.pacemaker.PacemakerClientPool;
import org.apache.storm.pacemaker.PacemakerConnectionException;
import org.apache.storm.shade.org.apache.curator.framework.state.ConnectionStateListener;
import org.apache.storm.shade.org.apache.zookeeper.data.ACL;
import org.apache.storm.utils.Utils;
import org.apache.storm.utils.WrappedHBExecutionException;
Expand Down Expand Up @@ -286,7 +285,7 @@ public void delete_worker_hb(String path) {
}

@Override
public void add_listener(ConnectionStateListener listener) {
public void add_listener(ClusterStateListener listener) {
stateStorage.add_listener(listener);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@
import org.apache.storm.generated.WorkerTokenServiceType;
import org.apache.storm.nimbus.NimbusInfo;
import org.apache.storm.shade.org.apache.commons.lang3.StringUtils;
import org.apache.storm.shade.org.apache.curator.framework.state.ConnectionState;
import org.apache.storm.shade.org.apache.zookeeper.KeeperException;
import org.apache.storm.shade.org.apache.zookeeper.Watcher;
import org.apache.storm.shade.org.apache.zookeeper.data.ACL;
Expand Down Expand Up @@ -296,10 +295,10 @@ public List<NimbusSummary> nimbuses() {
public void addNimbusHost(final String nimbusId, final NimbusSummary nimbusSummary) {
// explicit delete for ephmeral node to ensure this session creates the entry.
stateStorage.delete_node(ClusterUtils.nimbusPath(nimbusId));
stateStorage.add_listener((curatorFramework, connectionState) -> {
stateStorage.add_listener(connectionState -> {
LOG.info("Connection state listener invoked, zookeeper connection state has changed "
+ "to {}", connectionState);
if (connectionState.equals(ConnectionState.RECONNECTED)) {
if (connectionState == ConnectionState.RECONNECTED) {
LOG.info("Connection state has changed to reconnected so setting nimbuses entry "
+ "one more time");
// explicit delete for ephemeral node to ensure this session creates the entry.
Expand Down
19 changes: 8 additions & 11 deletions storm-client/src/jvm/org/apache/storm/cluster/ZKStateStorage.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,13 @@
import org.apache.storm.callback.DefaultWatcherCallBack;
import org.apache.storm.callback.WatcherCallBack;
import org.apache.storm.callback.ZKStateChangedCallback;
import org.apache.storm.shade.org.apache.curator.framework.CuratorFramework;
import org.apache.storm.shade.org.apache.curator.framework.state.ConnectionStateListener;
import org.apache.storm.shade.org.apache.zookeeper.CreateMode;
import org.apache.storm.shade.org.apache.zookeeper.KeeperException;
import org.apache.storm.shade.org.apache.zookeeper.Watcher;
import org.apache.storm.shade.org.apache.zookeeper.data.ACL;
import org.apache.storm.utils.Utils;
import org.apache.storm.zookeeper.ClientZookeeper;
import org.apache.storm.zookeeper.ZkClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -45,8 +44,8 @@ public class ZKStateStorage implements IStateStorage {

private ConcurrentHashMap<String, ZKStateChangedCallback> callbacks =
new ConcurrentHashMap<String, ZKStateChangedCallback>();
private CuratorFramework zkWriter;
private CuratorFramework zkReader;
private ZkClient zkWriter;
private ZkClient zkReader;
private AtomicBoolean active;

private boolean isNimbus;
Expand All @@ -62,7 +61,7 @@ public ZKStateStorage(Map<String, Object> conf, Map<String, Object> authConf,
}

// just mkdir STORM_ZOOKEEPER_ROOT dir
CuratorFramework zkTemp = mkZk(context.getDaemonType());
ZkClient zkTemp = mkZk(context.getDaemonType());
String rootPath = String.valueOf(conf.get(Config.STORM_ZOOKEEPER_ROOT));
ClientZookeeper.mkdirs(zkTemp, rootPath, context.getDefaultZkAcls());
zkTemp.close();
Expand All @@ -78,14 +77,14 @@ public ZKStateStorage(Map<String, Object> conf, Map<String, Object> authConf,
}

@SuppressWarnings("unchecked")
private CuratorFramework mkZk(DaemonType type) {
private ZkClient mkZk(DaemonType type) {
return ClientZookeeper.mkClient(conf, (List<String>) conf
.get(Config.STORM_ZOOKEEPER_SERVERS),
conf.get(Config.STORM_ZOOKEEPER_PORT), "", new DefaultWatcherCallBack(), authConf, type);
}

@SuppressWarnings("unchecked")
private CuratorFramework mkZk(WatcherCallBack watcher,
private ZkClient mkZk(WatcherCallBack watcher,
DaemonType type) throws NumberFormatException {
return ClientZookeeper.mkClient(conf, (List<String>) conf
.get(Config.STORM_ZOOKEEPER_SERVERS),
Expand Down Expand Up @@ -225,10 +224,8 @@ public void delete_worker_hb(String path) {
}

@Override
public void add_listener(final ConnectionStateListener listener) {
ClientZookeeper.addListener(zkReader,
(curatorFramework, connectionState) -> listener.stateChanged(curatorFramework,
connectionState));
public void add_listener(ClusterStateListener listener) {
ClientZookeeper.addListener(zkReader, listener);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@

import java.util.List;
import java.util.Map;
import org.apache.storm.shade.org.apache.curator.framework.CuratorFramework;
import org.apache.storm.shade.org.apache.zookeeper.CreateMode;
import org.apache.storm.shade.org.apache.zookeeper.data.ACL;
import org.apache.storm.zookeeper.ZkClient;

/**
* Facilitates testing of non-public methods in the parent class.
Expand All @@ -30,9 +30,9 @@ protected TestTransactionalState(Map<String, Object> conf, String id, String sub
super(conf, id, subroot);
}

public static void createNode(CuratorFramework curator,
public static void createNode(ZkClient zk,
String rootDir, byte[] data, List<ACL> acls, CreateMode mode)
throws Exception {
TransactionalState.createNode(curator, rootDir, data, acls, mode);
TransactionalState.createNode(zk, rootDir, data, acls, mode);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,27 +21,25 @@
import org.apache.storm.cluster.DaemonType;
import org.apache.storm.shade.net.minidev.json.JSONValue;
import org.apache.storm.shade.net.minidev.json.parser.ParseException;
import org.apache.storm.shade.org.apache.curator.framework.CuratorFramework;
import org.apache.storm.shade.org.apache.curator.framework.api.PathAndBytesable;
import org.apache.storm.shade.org.apache.curator.framework.api.ProtectACLCreateModePathAndBytesable;
import org.apache.storm.shade.org.apache.zookeeper.CreateMode;
import org.apache.storm.shade.org.apache.zookeeper.KeeperException;
import org.apache.storm.shade.org.apache.zookeeper.data.ACL;
import org.apache.storm.utils.CuratorUtils;
import org.apache.storm.utils.Utils;
import org.apache.storm.utils.ZookeeperAuthInfo;
import org.apache.storm.zookeeper.ClientZookeeper;
import org.apache.storm.zookeeper.ZkClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* Class that contains the logic to extract the transactional state info from zookeeper. All
* transactional state is kept in zookeeper. This
* class only contains references to Curator, which is used to get all info from zookeeper.
* class only contains references to a {@link ZkClient}, which is used to get all info from
* zookeeper.
*/
public class TransactionalState {
private static final Logger LOG = LoggerFactory.getLogger(TransactionalState.class);

CuratorFramework curator;
ZkClient zkClient;
List<ACL> zkAcls = null;

protected TransactionalState(Map<String, Object> conf, String id, String subroot) {
Expand All @@ -54,9 +52,8 @@ protected TransactionalState(Map<String, Object> conf, String id, String subroot
Config.STORM_ZOOKEEPER_SERVERS);
Object port = getWithBackup(conf, Config.TRANSACTIONAL_ZOOKEEPER_PORT,
Config.STORM_ZOOKEEPER_PORT);
ZookeeperAuthInfo auth = new ZookeeperAuthInfo(conf);
CuratorFramework initter = CuratorUtils.newCuratorStarted(conf, servers, port, auth,
DaemonType.WORKER.getDefaultZkAcls(conf));
ZkClient initter = ClientZookeeper.mkClient(conf, servers, port, "", null, conf,
DaemonType.WORKER);
zkAcls = Utils.getWorkerACL(conf);
try {
TransactionalState.createNode(initter, transactionalRoot, null, null, null);
Expand All @@ -70,8 +67,8 @@ protected TransactionalState(Map<String, Object> conf, String id, String subroot
}
initter.close();

curator = CuratorUtils.newCuratorStarted(conf, servers, port, rootDir, auth,
DaemonType.WORKER.getDefaultZkAcls(conf));
zkClient = ClientZookeeper.mkClient(conf, servers, port, rootDir, null, conf,
DaemonType.WORKER);
} catch (Exception e) {
throw new RuntimeException(e);
}
Expand All @@ -85,30 +82,31 @@ public static TransactionalState newCoordinatorState(Map<String, Object> conf, S
return new TransactionalState(conf, id, "coordinator");
}

protected static String forPath(PathAndBytesable<String> builder,
String path, byte[] data) throws Exception {
return (data == null)
? builder.forPath(path)
: builder.forPath(path, data);
}

protected static void createNode(CuratorFramework curator, String path,
/**
* Create a node, and any missing parents.
*
* <p>{@code mode} is only honoured when {@code acls} is {@code null}; when {@code acls} is
* given, or {@code mode} is {@code null}, the node is created {@link CreateMode#PERSISTENT}.
* When {@code data} is {@code null} the client's default payload is stored.
*
* @param zk the client to use
* @param path the path of the node
* @param data the data to store, or {@code null} for the client's default payload
* @param acls the ACL of the node, or {@code null} for the client's default ACL
* @param mode the create mode, see above
* @throws Exception on failure, ZooKeeper exceptions unwrapped
*/
protected static void createNode(ZkClient zk, String path,
byte[] data, List<ACL> acls, CreateMode mode) throws Exception {
ProtectACLCreateModePathAndBytesable<String> builder =
curator.create().creatingParentsIfNeeded();
LOG.debug("Creating node [path = {}], [data = {}], [acls = {}], [mode = {}]", path,
asString(data), acls, mode);

if (acls == null) {
if (mode == null) {
TransactionalState.forPath(builder, path, data);
} else {
TransactionalState.forPath(builder.withMode(mode), path, data);
}
return;
CreateMode effectiveMode = (acls == null && mode != null) ? mode : CreateMode.PERSISTENT;
if (data == null) {
zk.createWithDefaultData(path, effectiveMode, acls, true);
} else {
zk.create(path, data, effectiveMode, acls, true);
}

TransactionalState.forPath(builder.withACL(acls), path, data);
}

private static String asString(byte[] data) {
Expand All @@ -124,10 +122,10 @@ public void setData(String path, Object obj) {
throw new RuntimeException(e);
}
try {
if (curator.checkExists().forPath(path) != null) {
curator.setData().forPath(path, ser);
if (zkClient.exists(path, false) != null) {
zkClient.setData(path, ser);
} else {
TransactionalState.createNode(curator, path, ser, zkAcls,
TransactionalState.createNode(zkClient, path, ser, zkAcls,
CreateMode.PERSISTENT);
}
} catch (KeeperException.NodeExistsException nne) {
Expand All @@ -140,7 +138,7 @@ public void setData(String path, Object obj) {
public void delete(String path) {
path = "/" + path;
try {
curator.delete().forPath(path);
zkClient.delete(path, false);
} catch (KeeperException.NoNodeException nne) {
LOG.warn("Path {} already deleted.", path);
} catch (Exception e) {
Expand All @@ -153,10 +151,10 @@ public List<String> list(String path) {
path = "/" + path;
try {
List<String> children;
if (curator.checkExists().forPath(path) == null) {
if (zkClient.exists(path, false) == null) {
children = new ArrayList<>();
} else {
children = curator.getChildren().forPath(path);
children = zkClient.getChildren(path, false);
}
LOG.debug("List [path = {}], [children = {}]", path, children);
return children;
Expand All @@ -173,12 +171,12 @@ public Object getData(String path) {
path = "/" + path;
try {
Object data;
if (curator.checkExists().forPath(path) != null) {
if (zkClient.exists(path, false) != null) {
// Use parseWithException instead of parse so we can capture deserialization errors
// in the log.
// They are likely to be bugs in the spout code.
try {
data = JSONValue.parseWithException(new String(curator.getData().forPath(path),
data = JSONValue.parseWithException(new String(zkClient.getData(path, false, null),
"UTF-8"));
} catch (ParseException e) {
LOG.warn("Failed to deserialize zookeeper data for path {}", path, e);
Expand All @@ -195,7 +193,7 @@ public Object getData(String path) {
}

public void close() {
curator.close();
zkClient.close();
}

private Object getWithBackup(Map<String, Object> amap, String primary, String backup) {
Expand Down
Loading
Loading