Package com.mapr.baseutils.zookeeper
Class ZKUtils
- java.lang.Object
-
- com.mapr.baseutils.zookeeper.ZKUtils
-
public class ZKUtils extends java.lang.ObjectClass to take care of zookeeper handling utilities with "connection loss" and "session expiration" handling to do: getData check for znode existence getChildren
-
-
Field Summary
Fields Modifier and Type Field Description static java.lang.StringSERVICE_MASTER_NODEstatic intTIMEOUTstatic intTIMEOUT_SERVERstatic intZK_RETRIALS
-
Constructor Summary
Constructors Constructor Description ZKUtils()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static org.apache.zookeeper.data.StatcheckZKNodeForExistence(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 lossstatic 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 lossstatic 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 lossstatic voidresetZookeeper(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
-
-
-
Field Detail
-
ZK_RETRIALS
public static final int ZK_RETRIALS
- See Also:
- Constant Field Values
-
TIMEOUT
public static final int TIMEOUT
- See Also:
- Constant Field Values
-
TIMEOUT_SERVER
public static final int TIMEOUT_SERVER
- See Also:
- Constant Field Values
-
SERVICE_MASTER_NODE
public static final java.lang.String SERVICE_MASTER_NODE
- See Also:
- Constant Field Values
-
-
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, ZKClosedExceptionHelper 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.KeeperExceptionjava.lang.InterruptedExceptionZKClosedException
-
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, ZKClosedExceptionHelper 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.KeeperExceptionjava.lang.InterruptedExceptionZKClosedException
-
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, ZKClosedExceptionHelper 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.KeeperExceptionjava.lang.InterruptedExceptionZKClosedException
-
resetZookeeper
public static void resetZookeeper(java.lang.String zkAddresses, org.apache.zookeeper.ZooKeeper s_zk, org.apache.zookeeper.Watcher watcher) throws java.io.IOExceptionMethod 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
-
-