Package com.mapr.baseutils.zookeeper
Class ZKUtils
java.lang.Object
com.mapr.baseutils.zookeeper.ZKUtils
Class to take care of zookeeper handling utilities with "connection loss" and "session expiration" handling to do:
getData
check for znode existence
getChildren
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final Stringstatic final intstatic final intstatic final int -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic org.apache.zookeeper.data.StatcheckZKNodeForExistence(org.apache.zookeeper.ZooKeeper s_zk, 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 lossstatic byte[]getData(org.apache.zookeeper.ZooKeeper s_zk, 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 lossgetZkNodeChildren(org.apache.zookeeper.ZooKeeper s_zk, String nodeName, org.apache.zookeeper.Watcher watcher, int retrials) Helper method to get znode children from ZK with retrial logic in case of connection lossstatic voidresetZookeeper(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
-
Field Details
-
ZK_RETRIALS
public static final int ZK_RETRIALS- See Also:
-
TIMEOUT
public static final int TIMEOUT- See Also:
-
TIMEOUT_SERVER
public static final int TIMEOUT_SERVER- See Also:
-
SERVICE_MASTER_NODE
- See Also:
-
-
Constructor Details
-
ZKUtils
public ZKUtils()
-
-
Method Details
-
getData
public static byte[] getData(org.apache.zookeeper.ZooKeeper s_zk, String nodeName, org.apache.zookeeper.Watcher watcher, org.apache.zookeeper.data.Stat stat, int retrials) throws org.apache.zookeeper.KeeperException, 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.KeeperExceptionInterruptedExceptionZKClosedException
-
checkZKNodeForExistence
public static org.apache.zookeeper.data.Stat checkZKNodeForExistence(org.apache.zookeeper.ZooKeeper s_zk, String nodeName, org.apache.zookeeper.Watcher watcher, int retrials) throws org.apache.zookeeper.KeeperException, 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.KeeperExceptionInterruptedExceptionZKClosedException
-
getZkNodeChildren
public static List<String> getZkNodeChildren(org.apache.zookeeper.ZooKeeper s_zk, String nodeName, org.apache.zookeeper.Watcher watcher, int retrials) throws org.apache.zookeeper.KeeperException, 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.KeeperExceptionInterruptedExceptionZKClosedException
-
resetZookeeper
public static void resetZookeeper(String zkAddresses, org.apache.zookeeper.ZooKeeper s_zk, org.apache.zookeeper.Watcher watcher) throws 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:
IOException
-