Class ZKUtils


  • public class ZKUtils
    extends java.lang.Object
    Class to take care of zookeeper handling utilities with "connection loss" and "session expiration" handling to do: getData check for znode existence getChildren
    • Constructor Summary

      Constructors 
      Constructor Description
      ZKUtils()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static org.apache.zookeeper.data.Stat checkZKNodeForExistence​(org.apache.zookeeper.ZooKeeper s_zk, java.lang.String nodeName, org.apache.zookeeper.Watcher watcher, int retrials)
      Helper method to check for node existence in ZK with retrial logic in case of connection loss
      static byte[] getData​(org.apache.zookeeper.ZooKeeper s_zk, java.lang.String nodeName, org.apache.zookeeper.Watcher watcher, org.apache.zookeeper.data.Stat stat, int retrials)
      Helper method to get data from ZK with retrial logic in case of connection loss
      static java.util.List<java.lang.String> getZkNodeChildren​(org.apache.zookeeper.ZooKeeper s_zk, java.lang.String nodeName, org.apache.zookeeper.Watcher watcher, int retrials)
      Helper method to get znode children from ZK with retrial logic in case of connection loss
      static void resetZookeeper​(java.lang.String zkAddresses, org.apache.zookeeper.ZooKeeper s_zk, org.apache.zookeeper.Watcher watcher)
      Method to reset ZK - in case of KeeperException.SessionExpiredException that exception should actually remove znode from ZK - at least for nodes, as they are going to be EPHEMERAL_SEQUENTIAL, so we need to make sure that even new "master" is in place because of this (worst case master got SessionExpiredException) we do not restart service on any other node if it is running on "this one" Using writelock to prevent "reading" threads to get data that is in inconsistent state - during reset
      • Methods inherited from class java.lang.Object

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

      • ZKUtils

        public ZKUtils()
    • Method Detail

      • getData

        public static byte[] getData​(org.apache.zookeeper.ZooKeeper s_zk,
                                     java.lang.String nodeName,
                                     org.apache.zookeeper.Watcher watcher,
                                     org.apache.zookeeper.data.Stat stat,
                                     int retrials)
                              throws org.apache.zookeeper.KeeperException,
                                     java.lang.InterruptedException,
                                     ZKClosedException
        Helper method to get data from ZK with retrial logic in case of connection loss
        Parameters:
        s_zk -
        nodeName -
        watcher -
        stat -
        retrials -
        Returns:
        Throws:
        org.apache.zookeeper.KeeperException
        java.lang.InterruptedException
        ZKClosedException
      • checkZKNodeForExistence

        public static org.apache.zookeeper.data.Stat checkZKNodeForExistence​(org.apache.zookeeper.ZooKeeper s_zk,
                                                                             java.lang.String nodeName,
                                                                             org.apache.zookeeper.Watcher watcher,
                                                                             int retrials)
                                                                      throws org.apache.zookeeper.KeeperException,
                                                                             java.lang.InterruptedException,
                                                                             ZKClosedException
        Helper method to check for node existence in ZK with retrial logic in case of connection loss
        Parameters:
        s_zk -
        nodeName -
        watcher -
        retrials -
        Returns:
        Throws:
        org.apache.zookeeper.KeeperException
        java.lang.InterruptedException
        ZKClosedException
      • getZkNodeChildren

        public static java.util.List<java.lang.String> getZkNodeChildren​(org.apache.zookeeper.ZooKeeper s_zk,
                                                                         java.lang.String nodeName,
                                                                         org.apache.zookeeper.Watcher watcher,
                                                                         int retrials)
                                                                  throws org.apache.zookeeper.KeeperException,
                                                                         java.lang.InterruptedException,
                                                                         ZKClosedException
        Helper method to get znode children from ZK with retrial logic in case of connection loss
        Parameters:
        s_zk -
        nodeName -
        watcher -
        retrials -
        Returns:
        Throws:
        org.apache.zookeeper.KeeperException
        java.lang.InterruptedException
        ZKClosedException
      • resetZookeeper

        public static void resetZookeeper​(java.lang.String zkAddresses,
                                          org.apache.zookeeper.ZooKeeper s_zk,
                                          org.apache.zookeeper.Watcher watcher)
                                   throws java.io.IOException
        Method to reset ZK - in case of KeeperException.SessionExpiredException that exception should actually remove znode from ZK - at least for nodes, as they are going to be EPHEMERAL_SEQUENTIAL, so we need to make sure that even new "master" is in place because of this (worst case master got SessionExpiredException) we do not restart service on any other node if it is running on "this one" Using writelock to prevent "reading" threads to get data that is in inconsistent state - during reset
        Parameters:
        s_zk -
        watcher -
        Throws:
        java.io.IOException