Class: org.apache.zookeeper.ZooKeeper
- public class ZooKeeper
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.
The application can call ZooKeeper APIs through a client as long as the session ID of the client remains valid.
If for some reason, the client fails to send heart beats to the server for a prolonged period of time (exceeding the sessionTimeout value, for instance), the server will expire the session, and the session ID will become invalid. The client object will no longer be usable. To make ZooKeeper API calls, the application must create a new client object.
If the ZooKeeper server the client currently connects to fails or otherwise does not respond, the client will automatically try to connect to another server before its session ID expires. If successful, the application can continue to use the client.
Some successful ZooKeeper API calls can leave watches on the "data nodes" in the ZooKeeper server. Other successful ZooKeeper API calls can trigger those watches. Once a watch is triggered, an event will be delivered to the client which left the watch at the first place. Each watch can be triggered only once. Thus, up to one event will be delivered to a client for every watch it leaves.
A client needs an object of a class implementing Watcher interface for processing the events delivered to the client. When a client drops current connection and re-connects to a server, all the existing watches are considered as being triggered but the undelivered events are lost. To emulate this, the client will generate a special event to tell the event handler a connection has been dropped. This special event has type EventNone and state sKeeperStateDisconnected.
Methods
-
ZooKeepertop
To create a ZooKeeper client object, the application needs to pass a connection string containing a comma separated list of host:port pairs, each corresponding to a ZooKeeper server.Session establishment is asynchronous. This constructor will initiate connection to the server and return immediately - potentially (usually) before the session is fully established. The watcher argument specifies the watcher that will be notified of any changes in state. This notification can come at any point before or after the constructor call has returned.
The instantiated ZooKeeper client object will pick an arbitrary server from the connectString and attempt to connect to it. If establishment of the connection fails, another server in the connect string will be tried (the order is non-deterministic, as we random shuffle the list), until a connection is established. The client will continue attempts until the session is explicitly closed.
Added in 3.2.0: An optional "chroot" suffix may also be appended to the connection string. This will run the client commands while interpreting all paths relative to this root (similar to the unix chroot command).
-
ZooKeepertop
public ZooKeeper(String connectString, int sessionTimeout, Watcher watcher, long sessionId, byte[] sessionPasswd) throws IOExceptionTo create a ZooKeeper client object, the application needs to pass a connection string containing a comma separated list of host:port pairs, each corresponding to a ZooKeeper server.Session establishment is asynchronous. This constructor will initiate connection to the server and return immediately - potentially (usually) before the session is fully established. The watcher argument specifies the watcher that will be notified of any changes in state. This notification can come at any point before or after the constructor call has returned.
The instantiated ZooKeeper client object will pick an arbitrary server from the connectString and attempt to connect to it. If establishment of the connection fails, another server in the connect string will be tried (the order is non-deterministic, as we random shuffle the list), until a connection is established. The client will continue attempts until the session is explicitly closed (or the session is expired by the server).
Added in 3.2.0: An optional "chroot" suffix may also be appended to the connection string. This will run the client commands while interpreting all paths relative to this root (similar to the unix chroot command).
Use org.apache.zookeeper.ZooKeeper.getSessionId() and org.apache.zookeeper.ZooKeeper.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.
-
addAuthInfotop
public void addAuthInfo(String scheme, byte[] auth)Add the specified scheme:auth information to this connection. This method is NOT thread safe -
closetop
public synchronized void close() throws InterruptedExceptionClose this client object. Once the client is closed, its session becomes invalid. All the ephemeral nodes in the ZooKeeper server associated with the session will be removed. The watches left on those nodes (and on their parents) will be triggered. -
createtop
public String create(String path, byte[] data, List<ACL> acl, CreateMode createMode) throws KeeperException, InterruptedExceptionCreate a node with the given path. The node data will be the given data, and node acl will be the given acl.The flags argument specifies whether the created node will be ephemeral or not.
An ephemeral node will be removed by the ZooKeeper automatically when the session associated with the creation of the node expires.
The flags argument can also specify to create a sequential node. The actual path name of a sequential node will be the given path plus a suffix "_i" where i is the current sequential number of the node. Once such a node is created, the sequential number will be incremented by one.
If a node with the same actual path already exists in the ZooKeeper, a KeeperException with error code KeeperException.NodeExists will be thrown. Note that since a different actual path is used for each invocation of creating sequential node with the same path argument, the call will never throw "file exists" KeeperException.
If the parent node does not exist in the ZooKeeper, a KeeperException with error code KeeperException.NoNode will be thrown.
An ephemeral node cannot have children. If the parent node of the given path is ephemeral, a KeeperException with error code KeeperException.NoChildrenForEphemerals will be thrown.
This operation, if successful, will trigger all the watches left on the node of the given path by exists and getData API calls, and the watches left on the parent node by getChildren API calls.
If a node is created successfully, the ZooKeeper server will trigger the watches on the path left by exists calls, and the watches on the parent of the node by getChildren calls.
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.
-
createtop
public void create(String path, byte[] data, List<ACL> acl, CreateMode createMode, AsyncCallback.StringCallback cb, Object ctx)The Asynchronous version of create. The request doesn't actually until the asynchronous callback is called. -
deletetop
public void delete(String path, int version) throws InterruptedException, KeeperExceptionDelete the node with the given path. The call will succeed if such a node exists, and the given version matches the node's version (if the given version is -1, it matches any node's versions).A KeeperException with error code KeeperException.NoNode will be thrown if the nodes does not exist.
A KeeperException with error code KeeperException.BadVersion will be thrown if the given version does not match the node's version.
A KeeperException with error code KeeperException.NotEmpty will be thrown if the node has children.
This operation, if successful, will trigger all the watches on the node of the given path left by exists API calls, and the watches on the parent node left by getChildren API calls.
-
deletetop
The Asynchronous version of delete. The request doesn't actually until the asynchronous callback is called. -
existstop
Return the stat of the node of the given path. Return null if no such a node exists.If the watch is non-null and the call is successful (no exception is thrown), a watch will be left on the node with the given path. The watch will be triggered by a successful operation that creates/delete the node or sets the data on the node.
-
existstop
The Asynchronous version of exists. The request doesn't actually until the asynchronous callback is called. -
existstop
Return the stat of the node of the given path. Return null if no such a node exists.If the watch is true and the call is successful (no exception is thrown), a watch will be left on the node with the given path. The watch will be triggered by a successful operation that creates/delete the node or sets the data on the node.
-
existstop
The Asynchronous version of exists. The request doesn't actually until the asynchronous callback is called. -
getACLtop
Return the ACL and stat of the node of the given path.A KeeperException with error code KeeperException.NoNode will be thrown if no node with the given path exists.
-
getACLtop
The Asynchronous version of getACL. The request doesn't actually until the asynchronous callback is called. -
getChildrentop
public List<String> getChildren(String path, Watcher watcher) throws KeeperException, InterruptedExceptionReturn the list of the children of the node of the given path.If the watch is non-null and the call is successful (no exception is thrown), a watch will be left on the node with the given path. The watch willbe triggered by a successful operation that deletes the node of the given path or creates/delete a child under the node.
The list of children returned is not sorted and no guarantee is provided as to its natural or lexical order.
A KeeperException with error code KeeperException.NoNode will be thrown if no node with the given path exists.
-
getChildrentop
public void getChildren(String path, Watcher watcher, AsyncCallback.ChildrenCallback cb, Object ctx)The Asynchronous version of getChildren. The request doesn't actually until the asynchronous callback is called. -
getChildrentop
public List<String> getChildren(String path, boolean watch) throws KeeperException, InterruptedExceptionReturn the list of the children of the node of the given path.If the watch is true and the call is successful (no exception is thrown), a watch will be left on the node with the given path. The watch willbe triggered by a successful operation that deletes the node of the given path or creates/delete a child under the node.
The list of children returned is not sorted and no guarantee is provided as to its natural or lexical order.
A KeeperException with error code KeeperException.NoNode will be thrown if no node with the given path exists.
-
getChildrentop
The Asynchronous version of getChildren. The request doesn't actually until the asynchronous callback is called. -
getDatatop
The Asynchronous version of getData. The request doesn't actually until the asynchronous callback is called. -
getDatatop
public byte[] getData(String path, Watcher watcher, Stat stat) throws KeeperException, InterruptedExceptionReturn the data and the stat of the node of the given path.If the watch is non-null and the call is successful (no exception is thrown), a watch will be left on the node with the given path. The watch will be triggered by a successful operation that sets data on the node, or deletes the node.
A KeeperException with error code KeeperException.NoNode will be thrown if no node with the given path exists.
-
getDatatop
The Asynchronous version of getData. The request doesn't actually until the asynchronous callback is called. -
getDatatop
public byte[] getData(String path, boolean watch, Stat stat) throws KeeperException, InterruptedExceptionReturn the data and the stat of the node of the given path.If the watch is true and the call is successful (no exception is thrown), a watch will be left on the node with the given path. The watch will be triggered by a successful operation that sets data on the node, or deletes the node.
A KeeperException with error code KeeperException.NoNode will be thrown if no node with the given path exists.
-
getSessionIdtop
public long getSessionId()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. This method is NOT thread safe -
getSessionPasswdtop
public byte[] getSessionPasswd()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. This method is NOT thread safe -
getStatetop
-
registertop
public synchronized void register(Watcher watcher)Specify the default watcher for the connection (overrides the one specified during construction). -
setACLtop
public Stat setACL(String path, List<ACL> acl, int version) throws KeeperException, InterruptedExceptionSet the ACL for the node of the given path if such a node exists and the given version matches the version of the node. Return the stat of the node.A KeeperException with error code KeeperException.NoNode will be thrown if no node with the given path exists.
A KeeperException with error code KeeperException.BadVersion will be thrown if the given version does not match the node's version.
-
setACLtop
public void setACL(String path, List<ACL> acl, int version, AsyncCallback.StatCallback cb, Object ctx)The Asynchronous version of setACL. The request doesn't actually until the asynchronous callback is called. -
setDatatop
public Stat setData(String path, byte[] data, int version) throws KeeperException, InterruptedExceptionSet the data for the node of the given path if such a node exists and the given version matches the version of the node (if the given version is -1, it matches any node's versions). Return the stat of the node.This operation, if successful, will trigger all the watches on the node of the given path left by getData calls.
A KeeperException with error code KeeperException.NoNode will be thrown if no node with the given path exists.
A KeeperException with error code KeeperException.BadVersion will be thrown if the given version does not match the node's version.
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.
-
setDatatop
public void setData(String path, byte[] data, int version, AsyncCallback.StatCallback cb, Object ctx)The Asynchronous version of setData. The request doesn't actually until the asynchronous callback is called. -
synctop
Asynchronous sync. Flushes channel between process and leader.
Fields
-
cnxn
final protected ClientCnxn cnxn
