Class ZooKeeperConnection


  • public class ZooKeeperConnection
    extends java.lang.Object
    An 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 class  ZooKeeperConnection.ExistsWatcher
      Watcher class for exists state changes, so we get notified about deletions of lock request nodes.
      protected static class  ZooKeeperConnection.ZooKeeperWatcher
      Watcher class for zookeeper, so we get notified about zookeeper events.
    • 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
      boolean checkGlobalFlag​(java.lang.String flagPath)  
      boolean checkNodeExists​(java.lang.String nodePath)
      Check whether a node exists.
      void clearGlobalFlag​(java.lang.String flagPath)  
      void close()
      Close this connection.
      void createChild​(java.lang.String nodePath, java.lang.String childName)
      Create a persistent child of a node.
      void createNode​(java.lang.String nodePath, byte[] nodeData)
      Create a transient node.
      protected void createPersistentPath​(java.lang.String path, byte[] data)  
      protected java.lang.String createSequentialChild​(java.lang.String mainNode, java.lang.String childPrefix)
      Create a node and a sequential child node.
      protected void createSession()  
      void deleteChild​(java.lang.String nodePath, java.lang.String childName)
      Delete the child of a node.
      void deleteNode()
      Delete a node.
      void deleteNodeChildren​(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 void handleEphemeralNodeKeeperException​(org.apache.zookeeper.KeeperException e, boolean recreate)
      Handle keeper exceptions that may involve ephemeral node creation.
      protected void handleKeeperException​(org.apache.zookeeper.KeeperException e, boolean recreate)
      Handle keeper exceptions that don't involve ephemeral node creation.
      void obtainNonExWriteLock​(java.lang.String lockPath)
      Obtain a non-ex-write lock, with wait.
      boolean obtainNonExWriteLockNoWait​(java.lang.String lockPath)
      Obtain a non-ex-write lock, with no wait.
      void obtainReadLock​(java.lang.String lockPath)
      Obtain a read lock, with wait.
      boolean obtainReadLockNoWait​(java.lang.String lockPath)
      Obtain a read lock, with no wait.
      void obtainWriteLock​(java.lang.String lockPath)
      Obtain a write lock, with wait.
      boolean obtainWriteLockNoWait​(java.lang.String lockPath)
      Obtain a write lock, with no wait.
      byte[] readData​(java.lang.String resourcePath)  
      void releaseLock()
      Release the (saved) lock.
      void setGlobalFlag​(java.lang.String flagPath)  
      void setNodeData​(byte[] data)
      Set node data.
      void writeData​(java.lang.String resourcePath, byte[] data)  
      static java.lang.String zooKeeperDecodeSafeName​(java.lang.String input)  
      static java.lang.String zooKeeperSafeName​(java.lang.String input)  
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • connectString

        protected final java.lang.String connectString
      • sessionTimeout

        protected final int sessionTimeout
      • zookeeper

        protected org.apache.zookeeper.ZooKeeper zookeeper
      • 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.InterruptedException
        Constructor.
        Throws:
        ManifoldCFException
        java.lang.InterruptedException
    • Method Detail

      • createNode

        public void createNode​(java.lang.String nodePath,
                               byte[] nodeData)
                        throws ManifoldCFException,
                               java.lang.InterruptedException
        Create a transient node.
        Throws:
        ManifoldCFException
        java.lang.InterruptedException
      • checkNodeExists

        public boolean checkNodeExists​(java.lang.String nodePath)
                                throws ManifoldCFException,
                                       java.lang.InterruptedException
        Check whether a node exists.
        Parameters:
        nodePath - is the path of the node.
        Returns:
        the data, if the node if exists, otherwise null.
        Throws:
        ManifoldCFException
        java.lang.InterruptedException
      • getNodeData

        public byte[] getNodeData​(java.lang.String nodePath)
                           throws ManifoldCFException,
                                  java.lang.InterruptedException
        Get node data.
        Parameters:
        nodePath - is the path of the node.
        Returns:
        the data, if the node if exists, otherwise null.
        Throws:
        ManifoldCFException
        java.lang.InterruptedException
      • deleteNodeChildren

        public void deleteNodeChildren​(java.lang.String nodePath)
                                throws ManifoldCFException,
                                       java.lang.InterruptedException
        Delete all a node's children.
        Throws:
        ManifoldCFException
        java.lang.InterruptedException
      • getChildren

        public java.util.List<java.lang.String> getChildren​(java.lang.String nodePath)
                                                     throws ManifoldCFException,
                                                            java.lang.InterruptedException
        Get the relative paths of all node's children. If the node does not exist, return an empty list.
        Throws:
        ManifoldCFException
        java.lang.InterruptedException
      • createChild

        public void createChild​(java.lang.String nodePath,
                                java.lang.String childName)
                         throws ManifoldCFException,
                                java.lang.InterruptedException
        Create a persistent child of a node.
        Throws:
        ManifoldCFException
        java.lang.InterruptedException
      • createPersistentPath

        protected void createPersistentPath​(java.lang.String path,
                                            byte[] data)
                                     throws org.apache.zookeeper.KeeperException,
                                            java.lang.InterruptedException
        Throws:
        org.apache.zookeeper.KeeperException
        java.lang.InterruptedException
      • deleteChild

        public void deleteChild​(java.lang.String nodePath,
                                java.lang.String childName)
                         throws ManifoldCFException,
                                java.lang.InterruptedException
        Delete the child of a node.
        Throws:
        ManifoldCFException
        java.lang.InterruptedException
      • obtainWriteLockNoWait

        public boolean obtainWriteLockNoWait​(java.lang.String lockPath)
                                      throws ManifoldCFException,
                                             java.lang.InterruptedException
        Obtain a write lock, with no wait.
        Parameters:
        lockPath - is the lock node path.
        Returns:
        true if the lock was obtained, false otherwise.
        Throws:
        ManifoldCFException
        java.lang.InterruptedException
      • obtainWriteLock

        public void obtainWriteLock​(java.lang.String lockPath)
                             throws ManifoldCFException,
                                    java.lang.InterruptedException
        Obtain a write lock, with wait.
        Parameters:
        lockPath - is the lock node path.
        Throws:
        ManifoldCFException
        java.lang.InterruptedException
      • obtainNonExWriteLockNoWait

        public boolean obtainNonExWriteLockNoWait​(java.lang.String lockPath)
                                           throws ManifoldCFException,
                                                  java.lang.InterruptedException
        Obtain 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:
        ManifoldCFException
        java.lang.InterruptedException
      • obtainNonExWriteLock

        public void obtainNonExWriteLock​(java.lang.String lockPath)
                                  throws ManifoldCFException,
                                         java.lang.InterruptedException
        Obtain a non-ex-write lock, with wait.
        Parameters:
        lockPath - is the lock node path.
        Throws:
        ManifoldCFException
        java.lang.InterruptedException
      • obtainReadLockNoWait

        public boolean obtainReadLockNoWait​(java.lang.String lockPath)
                                     throws ManifoldCFException,
                                            java.lang.InterruptedException
        Obtain a read lock, with no wait.
        Parameters:
        lockPath - is the lock node path.
        Returns:
        true if the lock was obtained, false otherwise.
        Throws:
        ManifoldCFException
        java.lang.InterruptedException
      • obtainReadLock

        public void obtainReadLock​(java.lang.String lockPath)
                            throws ManifoldCFException,
                                   java.lang.InterruptedException
        Obtain a read lock, with wait.
        Parameters:
        lockPath - is the lock node path.
        Throws:
        ManifoldCFException
        java.lang.InterruptedException
      • writeData

        public void writeData​(java.lang.String resourcePath,
                              byte[] data)
                       throws ManifoldCFException,
                              java.lang.InterruptedException
        Throws:
        ManifoldCFException
        java.lang.InterruptedException
      • setGlobalFlag

        public void setGlobalFlag​(java.lang.String flagPath)
                           throws ManifoldCFException,
                                  java.lang.InterruptedException
        Throws:
        ManifoldCFException
        java.lang.InterruptedException
      • clearGlobalFlag

        public void clearGlobalFlag​(java.lang.String flagPath)
                             throws ManifoldCFException,
                                    java.lang.InterruptedException
        Throws:
        ManifoldCFException
        java.lang.InterruptedException
      • checkGlobalFlag

        public boolean checkGlobalFlag​(java.lang.String flagPath)
                                throws ManifoldCFException,
                                       java.lang.InterruptedException
        Throws:
        ManifoldCFException
        java.lang.InterruptedException
      • close

        public void close()
                   throws java.lang.InterruptedException
        Close this connection.
        Throws:
        java.lang.InterruptedException
      • handleEphemeralNodeKeeperException

        protected void handleEphemeralNodeKeeperException​(org.apache.zookeeper.KeeperException e,
                                                          boolean recreate)
                                                   throws ManifoldCFException,
                                                          java.lang.InterruptedException
        Handle keeper exceptions that may involve ephemeral node creation.
        Throws:
        ManifoldCFException
        java.lang.InterruptedException
      • handleKeeperException

        protected void handleKeeperException​(org.apache.zookeeper.KeeperException e,
                                             boolean recreate)
                                      throws ManifoldCFException,
                                             java.lang.InterruptedException
        Handle keeper exceptions that don't involve ephemeral node creation.
        Throws:
        ManifoldCFException
        java.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.InterruptedException
        Create a node and a sequential child node. Neither node has any data.
        Throws:
        org.apache.zookeeper.KeeperException
        java.lang.InterruptedException