|
@@ -52,24 +52,13 @@ import org.apache.zookeeper.proto.SetDataRequest;
|
|
|
import org.apache.zookeeper.proto.SetDataResponse;
|
|
|
import org.apache.zookeeper.proto.SyncRequest;
|
|
|
import org.apache.zookeeper.proto.SyncResponse;
|
|
|
-import org.apache.zookeeper.proto.WatcherEvent;
|
|
|
import org.apache.zookeeper.server.DataTree;
|
|
|
-import org.apache.zookeeper.Watcher.Event.EventType;
|
|
|
-import org.apache.zookeeper.Watcher.Event.KeeperState;
|
|
|
|
|
|
/**
|
|
|
* This is the main class of ZooKeeper client library. To use a ZooKeeper
|
|
|
* service, an application must first instantiate an object of ZooKeeper class.
|
|
|
* All the iterations will be done by calling the methods of ZooKeeper class.
|
|
|
* <p>
|
|
|
- * To create a client(ZooKeeper) object, the application needs to pass a string
|
|
|
- * containing a list of host:port pairs, each corresponding to a ZooKeeper
|
|
|
- * server; a sessionTimeout; and an object of Watcher type.
|
|
|
- * <p>
|
|
|
- * The client object will pick an arbitrary server and try to connect to it. If
|
|
|
- * failed, it will try the next one in the list, until a connection is
|
|
|
- * established, or all the servers have been tried.
|
|
|
- * <p>
|
|
|
* Once a connection to a server is established, a session ID is assigned to the
|
|
|
* client. The client will send heart beats to the server periodically to keep
|
|
|
* the session valid.
|
|
@@ -109,10 +98,10 @@ public class ZooKeeper {
|
|
|
private static final Logger LOG = Logger.getLogger(ZooKeeper.class);
|
|
|
|
|
|
private final ZKWatchManager watchManager = new ZKWatchManager();
|
|
|
-
|
|
|
+
|
|
|
/**
|
|
|
* Manage watchers & handle events generated by the ClientCnxn object.
|
|
|
- *
|
|
|
+ *
|
|
|
* We are implementing this as a nested class of ZooKeeper so that
|
|
|
* the public methods will not be exposed as part of the ZooKeeper client
|
|
|
* API.
|
|
@@ -128,10 +117,10 @@ public class ZooKeeper {
|
|
|
/* (non-Javadoc)
|
|
|
* @see org.apache.zookeeper.ClientWatchManager#materialize(Event.KeeperState, Event.EventType, java.lang.String)
|
|
|
*/
|
|
|
- public Set<Watcher> materialize(Watcher.Event.KeeperState state,
|
|
|
+ public Set<Watcher> materialize(Watcher.Event.KeeperState state,
|
|
|
Watcher.Event.EventType type, String path) {
|
|
|
Set<Watcher> result = new HashSet<Watcher>();
|
|
|
-
|
|
|
+
|
|
|
// clear the watches if we are not connected
|
|
|
|
|
|
if (state != Watcher.Event.KeeperState.SyncConnected) {
|
|
@@ -152,9 +141,9 @@ public class ZooKeeper {
|
|
|
childWatches.clear();
|
|
|
}
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
Set<Watcher> watchers = null;
|
|
|
-
|
|
|
+
|
|
|
switch (type) {
|
|
|
case None:
|
|
|
result.add(defaultWatcher);
|
|
@@ -192,7 +181,7 @@ public class ZooKeeper {
|
|
|
LOG.error(msg);
|
|
|
throw new RuntimeException(msg);
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
result.addAll(watchers);
|
|
|
return result;
|
|
|
}
|
|
@@ -268,12 +257,46 @@ public class ZooKeeper {
|
|
|
|
|
|
protected ClientCnxn cnxn;
|
|
|
|
|
|
+ /**
|
|
|
+ * @see ZooKeeper(String, int, Watcher, long, byte[])
|
|
|
+ */
|
|
|
public ZooKeeper(String host, int sessionTimeout, Watcher watcher)
|
|
|
throws IOException {
|
|
|
watchManager.defaultWatcher = watcher;
|
|
|
cnxn = new ClientCnxn(host, sessionTimeout, this, watchManager);
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * To create a client(ZooKeeper) object, the application needs to pass a
|
|
|
+ * string containing a list of host:port pairs, each corresponding to a
|
|
|
+ * ZooKeeper server.
|
|
|
+ * <p>
|
|
|
+ * The client object will pick an arbitrary server and try to connect to it.
|
|
|
+ * If failed, it will try the next one in the list, until a connection is
|
|
|
+ * established, or all the servers have been tried.
|
|
|
+ * <p>
|
|
|
+ * Use {@link #getSessionId} and {@link #getSessionPasswd} on an established
|
|
|
+ * client connection, these values must be passed as sessionId and
|
|
|
+ * sessionPasswd respectively if reconnecting. Otherwise, if not
|
|
|
+ * reconnecting, use the other constructor which does not require these
|
|
|
+ * parameters.
|
|
|
+ *
|
|
|
+ * @param host
|
|
|
+ * comma separated host:port pairs, each corresponding to a zk
|
|
|
+ * server. eg. "127.0.0.1:3000,127.0.0.1:3001,127.0.0.1:3002"
|
|
|
+ * @param sessionTimeout
|
|
|
+ * session timeout in milliseconds
|
|
|
+ * @param watcher
|
|
|
+ * a watcher object which will be notified of state changes, may
|
|
|
+ * also be notified for node events
|
|
|
+ * @param sessionId
|
|
|
+ * specific session id to use if reconnecting
|
|
|
+ * @param sessionPasswd
|
|
|
+ * password for this session
|
|
|
+ *
|
|
|
+ * @throws IOException
|
|
|
+ * in cases of network failure
|
|
|
+ */
|
|
|
public ZooKeeper(String host, int sessionTimeout, Watcher watcher,
|
|
|
long sessionId, byte[] sessionPasswd) throws IOException {
|
|
|
watchManager.defaultWatcher = watcher;
|
|
@@ -282,16 +305,23 @@ public class ZooKeeper {
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * The session id for this ZooKeeper client instance. The value returned
|
|
|
- * is not valid until the client connects to a server and may change
|
|
|
- * after a re-connect.
|
|
|
- *
|
|
|
+ * The session id for this ZooKeeper client instance. The value returned is
|
|
|
+ * not valid until the client connects to a server and may change after a
|
|
|
+ * re-connect.
|
|
|
+ *
|
|
|
* @return current session id
|
|
|
*/
|
|
|
public long getSessionId() {
|
|
|
return cnxn.getSessionId();
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * The session password for this ZooKeeper client instance. The value
|
|
|
+ * returned is not valid until the client connects to a server and may
|
|
|
+ * change after a re-connect.
|
|
|
+ *
|
|
|
+ * @return current session password
|
|
|
+ */
|
|
|
public byte[] getSessionPasswd() {
|
|
|
return cnxn.getSessionPasswd();
|
|
|
}
|
|
@@ -363,7 +393,7 @@ public class ZooKeeper {
|
|
|
* watches on the path left by exists calls, and the watches on the parent
|
|
|
* of the node by getChildren calls.
|
|
|
* <p>
|
|
|
- * The maximum allowable size of the data array is 1 MB (1,048,576 bytes).
|
|
|
+ * The maximum allowable size of the data array is 1 MB (1,048,576 bytes).
|
|
|
* Arrays larger than this will cause a KeeperExecption to be thrown.
|
|
|
*
|
|
|
* @param path
|
|
@@ -402,7 +432,7 @@ public class ZooKeeper {
|
|
|
return response.getPath();
|
|
|
}
|
|
|
|
|
|
- /**
|
|
|
+ /**
|
|
|
* The Asynchronous version of create. The request doesn't actually until
|
|
|
* the asynchronous callback is called.
|
|
|
*
|
|
@@ -686,7 +716,7 @@ public class ZooKeeper {
|
|
|
* A KeeperException with error code KeeperException.BadVersion will be
|
|
|
* thrown if the given version does not match the node's version.
|
|
|
* <p>
|
|
|
- * The maximum allowable size of the data array is 1 MB (1,048,576 bytes).
|
|
|
+ * The maximum allowable size of the data array is 1 MB (1,048,576 bytes).
|
|
|
* Arrays larger than this will cause a KeeperExecption to be thrown.
|
|
|
*
|
|
|
* @param path
|