Class ZooKeeperConnection
- java.lang.Object
-
- org.apache.manifoldcf.core.lockmanager.ZooKeeperConnection
-
public class ZooKeeperConnection extends java.lang.ObjectAn instance of this class is the Zookeeper analog to a database connection. Basically, it bundles up the Zookeeper functionality we need in a nice package, which we can share between users as needed. These connections will be pooled, and will be closed when the process they live in is shut down.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description protected static classZooKeeperConnection.ExistsWatcherWatcher class for exists state changes, so we get notified about deletions of lock request nodes.protected static classZooKeeperConnection.ZooKeeperWatcherWatcher class for zookeeper, so we get notified about zookeeper events.
-
Field Summary
Fields Modifier and Type Field Description static java.lang.String_rcsidprotected java.lang.StringconnectStringprotected java.lang.StringlockNodeprotected byte[]nodeDataprotected java.lang.StringnodePathprotected intsessionTimeoutprotected org.apache.zookeeper.ZooKeeperzookeeperprotected ZooKeeperConnection.ZooKeeperWatcherzookeeperWatcher
-
Constructor Summary
Constructors Constructor Description ZooKeeperConnection(java.lang.String connectString, int sessionTimeout)Constructor.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description booleancheckGlobalFlag(java.lang.String flagPath)booleancheckNodeExists(java.lang.String nodePath)Check whether a node exists.voidclearGlobalFlag(java.lang.String flagPath)voidclose()Close this connection.voidcreateChild(java.lang.String nodePath, java.lang.String childName)Create a persistent child of a node.voidcreateNode(java.lang.String nodePath, byte[] nodeData)Create a transient node.protected voidcreatePersistentPath(java.lang.String path, byte[] data)protected java.lang.StringcreateSequentialChild(java.lang.String mainNode, java.lang.String childPrefix)Create a node and a sequential child node.protected voidcreateSession()voiddeleteChild(java.lang.String nodePath, java.lang.String childName)Delete the child of a node.voiddeleteNode()Delete a node.voiddeleteNodeChildren(java.lang.String nodePath)Delete all a node's children.java.util.List<java.lang.String>getChildren(java.lang.String nodePath)Get the relative paths of all node's children.byte[]getNodeData(java.lang.String nodePath)Get node data.protected voidhandleEphemeralNodeKeeperException(org.apache.zookeeper.KeeperException e, boolean recreate)Handle keeper exceptions that may involve ephemeral node creation.protected voidhandleKeeperException(org.apache.zookeeper.KeeperException e, boolean recreate)Handle keeper exceptions that don't involve ephemeral node creation.voidobtainNonExWriteLock(java.lang.String lockPath)Obtain a non-ex-write lock, with wait.booleanobtainNonExWriteLockNoWait(java.lang.String lockPath)Obtain a non-ex-write lock, with no wait.voidobtainReadLock(java.lang.String lockPath)Obtain a read lock, with wait.booleanobtainReadLockNoWait(java.lang.String lockPath)Obtain a read lock, with no wait.voidobtainWriteLock(java.lang.String lockPath)Obtain a write lock, with wait.booleanobtainWriteLockNoWait(java.lang.String lockPath)Obtain a write lock, with no wait.byte[]readData(java.lang.String resourcePath)voidreleaseLock()Release the (saved) lock.voidsetGlobalFlag(java.lang.String flagPath)voidsetNodeData(byte[] data)Set node data.voidwriteData(java.lang.String resourcePath, byte[] data)static java.lang.StringzooKeeperDecodeSafeName(java.lang.String input)static java.lang.StringzooKeeperSafeName(java.lang.String input)
-
-
-
Field Detail
-
_rcsid
public static final java.lang.String _rcsid
- See Also:
- Constant Field Values
-
connectString
protected final java.lang.String connectString
-
sessionTimeout
protected final int sessionTimeout
-
zookeeper
protected org.apache.zookeeper.ZooKeeper zookeeper
-
zookeeperWatcher
protected ZooKeeperConnection.ZooKeeperWatcher zookeeperWatcher
-
lockNode
protected java.lang.String lockNode
-
nodePath
protected java.lang.String nodePath
-
nodeData
protected byte[] nodeData
-
-
Constructor Detail
-
ZooKeeperConnection
public ZooKeeperConnection(java.lang.String connectString, int sessionTimeout) throws ManifoldCFException, java.lang.InterruptedExceptionConstructor.- Throws:
ManifoldCFExceptionjava.lang.InterruptedException
-
-
Method Detail
-
createSession
protected void createSession() throws ManifoldCFException, java.lang.InterruptedException- Throws:
ManifoldCFExceptionjava.lang.InterruptedException
-
createNode
public void createNode(java.lang.String nodePath, byte[] nodeData) throws ManifoldCFException, java.lang.InterruptedExceptionCreate a transient node.- Throws:
ManifoldCFExceptionjava.lang.InterruptedException
-
checkNodeExists
public boolean checkNodeExists(java.lang.String nodePath) throws ManifoldCFException, java.lang.InterruptedExceptionCheck whether a node exists.- Parameters:
nodePath- is the path of the node.- Returns:
- the data, if the node if exists, otherwise null.
- Throws:
ManifoldCFExceptionjava.lang.InterruptedException
-
getNodeData
public byte[] getNodeData(java.lang.String nodePath) throws ManifoldCFException, java.lang.InterruptedExceptionGet node data.- Parameters:
nodePath- is the path of the node.- Returns:
- the data, if the node if exists, otherwise null.
- Throws:
ManifoldCFExceptionjava.lang.InterruptedException
-
setNodeData
public void setNodeData(byte[] data) throws ManifoldCFException, java.lang.InterruptedExceptionSet node data.- Throws:
ManifoldCFExceptionjava.lang.InterruptedException
-
deleteNode
public void deleteNode() throws ManifoldCFException, java.lang.InterruptedExceptionDelete a node.- Throws:
ManifoldCFExceptionjava.lang.InterruptedException
-
deleteNodeChildren
public void deleteNodeChildren(java.lang.String nodePath) throws ManifoldCFException, java.lang.InterruptedExceptionDelete all a node's children.- Throws:
ManifoldCFExceptionjava.lang.InterruptedException
-
getChildren
public java.util.List<java.lang.String> getChildren(java.lang.String nodePath) throws ManifoldCFException, java.lang.InterruptedExceptionGet the relative paths of all node's children. If the node does not exist, return an empty list.- Throws:
ManifoldCFExceptionjava.lang.InterruptedException
-
createChild
public void createChild(java.lang.String nodePath, java.lang.String childName) throws ManifoldCFException, java.lang.InterruptedExceptionCreate a persistent child of a node.- Throws:
ManifoldCFExceptionjava.lang.InterruptedException
-
createPersistentPath
protected void createPersistentPath(java.lang.String path, byte[] data) throws org.apache.zookeeper.KeeperException, java.lang.InterruptedException- Throws:
org.apache.zookeeper.KeeperExceptionjava.lang.InterruptedException
-
deleteChild
public void deleteChild(java.lang.String nodePath, java.lang.String childName) throws ManifoldCFException, java.lang.InterruptedExceptionDelete the child of a node.- Throws:
ManifoldCFExceptionjava.lang.InterruptedException
-
obtainWriteLockNoWait
public boolean obtainWriteLockNoWait(java.lang.String lockPath) throws ManifoldCFException, java.lang.InterruptedExceptionObtain a write lock, with no wait.- Parameters:
lockPath- is the lock node path.- Returns:
- true if the lock was obtained, false otherwise.
- Throws:
ManifoldCFExceptionjava.lang.InterruptedException
-
obtainWriteLock
public void obtainWriteLock(java.lang.String lockPath) throws ManifoldCFException, java.lang.InterruptedExceptionObtain a write lock, with wait.- Parameters:
lockPath- is the lock node path.- Throws:
ManifoldCFExceptionjava.lang.InterruptedException
-
obtainNonExWriteLockNoWait
public boolean obtainNonExWriteLockNoWait(java.lang.String lockPath) throws ManifoldCFException, java.lang.InterruptedExceptionObtain a non-ex-write lock, with no wait.- Parameters:
lockPath- is the lock node path.- Returns:
- true if the lock was obtained, false otherwise.
- Throws:
ManifoldCFExceptionjava.lang.InterruptedException
-
obtainNonExWriteLock
public void obtainNonExWriteLock(java.lang.String lockPath) throws ManifoldCFException, java.lang.InterruptedExceptionObtain a non-ex-write lock, with wait.- Parameters:
lockPath- is the lock node path.- Throws:
ManifoldCFExceptionjava.lang.InterruptedException
-
obtainReadLockNoWait
public boolean obtainReadLockNoWait(java.lang.String lockPath) throws ManifoldCFException, java.lang.InterruptedExceptionObtain a read lock, with no wait.- Parameters:
lockPath- is the lock node path.- Returns:
- true if the lock was obtained, false otherwise.
- Throws:
ManifoldCFExceptionjava.lang.InterruptedException
-
obtainReadLock
public void obtainReadLock(java.lang.String lockPath) throws ManifoldCFException, java.lang.InterruptedExceptionObtain a read lock, with wait.- Parameters:
lockPath- is the lock node path.- Throws:
ManifoldCFExceptionjava.lang.InterruptedException
-
releaseLock
public void releaseLock() throws ManifoldCFException, java.lang.InterruptedExceptionRelease the (saved) lock.- Throws:
ManifoldCFExceptionjava.lang.InterruptedException
-
readData
public byte[] readData(java.lang.String resourcePath) throws ManifoldCFException, java.lang.InterruptedException- Throws:
ManifoldCFExceptionjava.lang.InterruptedException
-
writeData
public void writeData(java.lang.String resourcePath, byte[] data) throws ManifoldCFException, java.lang.InterruptedException- Throws:
ManifoldCFExceptionjava.lang.InterruptedException
-
setGlobalFlag
public void setGlobalFlag(java.lang.String flagPath) throws ManifoldCFException, java.lang.InterruptedException- Throws:
ManifoldCFExceptionjava.lang.InterruptedException
-
clearGlobalFlag
public void clearGlobalFlag(java.lang.String flagPath) throws ManifoldCFException, java.lang.InterruptedException- Throws:
ManifoldCFExceptionjava.lang.InterruptedException
-
checkGlobalFlag
public boolean checkGlobalFlag(java.lang.String flagPath) throws ManifoldCFException, java.lang.InterruptedException- Throws:
ManifoldCFExceptionjava.lang.InterruptedException
-
close
public void close() throws java.lang.InterruptedExceptionClose this connection.- Throws:
java.lang.InterruptedException
-
handleEphemeralNodeKeeperException
protected void handleEphemeralNodeKeeperException(org.apache.zookeeper.KeeperException e, boolean recreate) throws ManifoldCFException, java.lang.InterruptedExceptionHandle keeper exceptions that may involve ephemeral node creation.- Throws:
ManifoldCFExceptionjava.lang.InterruptedException
-
handleKeeperException
protected void handleKeeperException(org.apache.zookeeper.KeeperException e, boolean recreate) throws ManifoldCFException, java.lang.InterruptedExceptionHandle keeper exceptions that don't involve ephemeral node creation.- Throws:
ManifoldCFExceptionjava.lang.InterruptedException
-
zooKeeperSafeName
public static java.lang.String zooKeeperSafeName(java.lang.String input)
-
zooKeeperDecodeSafeName
public static java.lang.String zooKeeperDecodeSafeName(java.lang.String input)
-
createSequentialChild
protected java.lang.String createSequentialChild(java.lang.String mainNode, java.lang.String childPrefix) throws org.apache.zookeeper.KeeperException, java.lang.InterruptedExceptionCreate a node and a sequential child node. Neither node has any data.- Throws:
org.apache.zookeeper.KeeperExceptionjava.lang.InterruptedException
-
-