Class ZooKeeperRetry
- All Implemented Interfaces:
AutoCloseable
ZooKeeper
and will automatically retry calls to
zookeeper if a KeeperException.ConnectionLossException
occurs.-
Nested Class Summary
Nested classes/interfaces inherited from class org.apache.zookeeper.ZooKeeper
ZooKeeper.States, ZooKeeper.WatchRegistration
-
Field Summary
Fields inherited from class org.apache.zookeeper.ZooKeeper
cnxn, hostProvider, SECURE_CLIENT, ZOOKEEPER_CLIENT_CNXN_SOCKET
-
Constructor Summary
ConstructorsConstructorDescriptionZooKeeperRetry
(String connectString, int sessionTimeout, Watcher watcher) ZooKeeperRetry
(String connectString, int sessionTimeout, Watcher watcher, long sessionId, byte[] sessionPasswd) -
Method Summary
Modifier and TypeMethodDescriptionvoid
close()
Close this client object.create
(String path, byte[] data, List<ACL> acl, CreateMode createMode) Create a node with the given path.void
Delete the node with the given path.Return the stat of the node of the given path.Return the stat of the node of the given path.Return the ACL and stat of the node of the given path.getChildren
(String path, boolean watch) Return the list of the children of the node of the given path.getChildren
(String path, Watcher watcher) Return the list of the children of the node of the given path.byte[]
Return the data and the stat of the node of the given path.byte[]
Return the data and the stat of the node of the given path.Set the ACL for the node of the given path if such a node exists and the given aclVersion matches the acl version of the node.Set 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).void
setRetryLimit
(int limit) boolean
Methods inherited from class org.apache.zookeeper.ZooKeeper
addAuthInfo, addWatch, addWatch, addWatch, addWatch, close, create, create, create, create, create, delete, exists, exists, getACL, getAllChildrenNumber, getAllChildrenNumber, getChildren, getChildren, getChildren, getChildren, getChildren, getChildren, getChildWatches, getClientConfig, getConfig, getConfig, getConfig, getConfig, getData, getData, getDataWatches, getEphemerals, getEphemerals, getEphemerals, getEphemerals, getExistWatches, getPersistentRecursiveWatches, getPersistentWatches, getSaslClient, getSessionId, getSessionPasswd, getSessionTimeout, getState, getTestable, multi, multi, multiInternal, multiInternal, register, removeAllWatches, removeAllWatches, removeWatches, removeWatches, setACL, setData, sync, sync, testableLocalSocketAddress, testableRemoteSocketAddress, testableWaitForShutdown, toString, transaction, updateServerList, whoAmI
-
Constructor Details
-
ZooKeeperRetry
- Parameters:
connectString
-sessionTimeout
-watcher
-- Throws:
IOException
-
ZooKeeperRetry
public ZooKeeperRetry(String connectString, int sessionTimeout, Watcher watcher, long sessionId, byte[] sessionPasswd) throws IOException - Parameters:
connectString
-sessionTimeout
-watcher
-sessionId
-sessionPasswd
-- Throws:
IOException
-
-
Method Details
-
close
Description copied from class:ZooKeeper
Close 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.Added in 3.5.3: try-with-resources may be used instead of calling close directly.
This method does not wait for all internal threads to exit. Use the
ZooKeeper.close(int)
method to wait for all resources to be released- Specified by:
close
in interfaceAutoCloseable
- Overrides:
close
in classZooKeeper
- Throws:
InterruptedException
-
create
public String create(String path, byte[] data, List<ACL> acl, CreateMode createMode) throws KeeperException, InterruptedException Description copied from class:ZooKeeper
Create 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. The sequence number is always fixed length of 10 digits, 0 padded. 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.
- Overrides:
create
in classZooKeeper
- Parameters:
path
- the path for the nodedata
- the initial data for the nodeacl
- the acl for the nodecreateMode
- specifying whether the node to be created is ephemeral and/or sequential- Returns:
- the actual path of the created node
- Throws:
KeeperException
- if the server returns a non-zero error codeInterruptedException
- if the transaction is interrupted
-
delete
Description copied from class:ZooKeeper
Delete 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.
- Overrides:
delete
in classZooKeeper
- Parameters:
path
- the path of the node to be deleted.version
- the expected node version.- Throws:
InterruptedException
- IF the server transaction is interruptedKeeperException
- If the server signals an error with a non-zero return code.
-
exists
Description copied from class:ZooKeeper
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.
- Overrides:
exists
in classZooKeeper
- Parameters:
path
- the node pathwatch
- whether need to watch this node- Returns:
- the stat of the node of the given path; return null if no such a node exists.
- Throws:
KeeperException
- If the server signals an errorInterruptedException
- If the server transaction is interrupted.
-
exists
Description copied from class:ZooKeeper
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.
- Overrides:
exists
in classZooKeeper
- Parameters:
path
- the node pathwatcher
- explicit watcher- Returns:
- the stat of the node of the given path; return null if no such a node exists.
- Throws:
KeeperException
- If the server signals an errorInterruptedException
- If the server transaction is interrupted.
-
getACL
Description copied from class:ZooKeeper
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.
- Overrides:
getACL
in classZooKeeper
- Parameters:
path
- the given path for the nodestat
- the stat of the node will be copied to this parameter if not null.- Returns:
- the ACL array of the given node.
- Throws:
KeeperException
- If the server signals an error with a non-zero error code.InterruptedException
- If the server transaction is interrupted.
-
getChildren
public List<String> getChildren(String path, boolean watch) throws KeeperException, InterruptedException Description copied from class:ZooKeeper
Return 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 will be 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.
- Overrides:
getChildren
in classZooKeeper
- Parameters:
path
- the node pathwatch
- whether need to watch this node- Returns:
- an unordered array of children of the node with the given path
- Throws:
KeeperException
- If the server signals an error with a non-zero error code.InterruptedException
- If the server transaction is interrupted.
-
getChildren
public List<String> getChildren(String path, Watcher watcher) throws KeeperException, InterruptedException Description copied from class:ZooKeeper
Return 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 will be 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.
- Overrides:
getChildren
in classZooKeeper
- Parameters:
path
-watcher
- explicit watcher- Returns:
- an unordered array of children of the node with the given path
- Throws:
KeeperException
- If the server signals an error with a non-zero error code.InterruptedException
- If the server transaction is interrupted.
-
getData
public byte[] getData(String path, boolean watch, Stat stat) throws KeeperException, InterruptedException Description copied from class:ZooKeeper
Return 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.
- Overrides:
getData
in classZooKeeper
- Parameters:
path
- the given pathwatch
- whether need to watch this nodestat
- the stat of the node- Returns:
- the data of the node
- Throws:
KeeperException
- If the server signals an error with a non-zero error codeInterruptedException
- If the server transaction is interrupted.
-
getData
public byte[] getData(String path, Watcher watcher, Stat stat) throws KeeperException, InterruptedException Description copied from class:ZooKeeper
Return 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.
- Overrides:
getData
in classZooKeeper
- Parameters:
path
- the given pathwatcher
- explicit watcherstat
- the stat of the node- Returns:
- the data of the node
- Throws:
KeeperException
- If the server signals an error with a non-zero error codeInterruptedException
- If the server transaction is interrupted.
-
setACL
public Stat setACL(String path, List<ACL> acl, int aclVersion) throws KeeperException, InterruptedException Description copied from class:ZooKeeper
Set the ACL for the node of the given path if such a node exists and the given aclVersion matches the acl 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 aclVersion does not match the node's aclVersion.
- Overrides:
setACL
in classZooKeeper
- Parameters:
path
- the given path for the nodeacl
- the given acl for the nodeaclVersion
- the given acl version of the node- Returns:
- the stat of the node.
- Throws:
KeeperException
- If the server signals an error with a non-zero error code.InterruptedException
- If the server transaction is interrupted.
-
setData
public Stat setData(String path, byte[] data, int version) throws KeeperException, InterruptedException Description copied from class:ZooKeeper
Set 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 KeeperException to be thrown.
- Overrides:
setData
in classZooKeeper
- Parameters:
path
- the path of the nodedata
- the data to setversion
- the expected matching version- Returns:
- the state of the node
- Throws:
KeeperException
- If the server signals an error with a non-zero error code.InterruptedException
- If the server transaction is interrupted.
-
setRetryLimit
public void setRetryLimit(int limit) - Parameters:
limit
-
-
testConnection
public boolean testConnection()- Returns:
- true if successfully connected to zookeeper
-