ml.shifu.guagua.coordinator.zk
类 GuaguaZooKeeper

java.lang.Object
  继承者 ml.shifu.guagua.coordinator.zk.GuaguaZooKeeper

public class GuaguaZooKeeper
extends Object

ZooKeeper provides only atomic operations. GuaguaZooKeeper provides additional non-atomic operations that are useful. It also provides wrappers to deal with ConnectionLossException. All methods of this class should be thread-safe.


嵌套类摘要
static class GuaguaZooKeeper.PathStat
          Data structure for handling the output of createOrSet()
 
构造方法摘要
GuaguaZooKeeper(String connectString, int sessionTimeout, int maxRetryAttempts, int retryWaitMsecs, org.apache.zookeeper.Watcher watcher)
          Constructor to connect to ZooKeeper, make progress
 
方法摘要
 void close()
          Close this client object.
 String createExt(String path, byte[] data, List<org.apache.zookeeper.data.ACL> acl, org.apache.zookeeper.CreateMode createMode, boolean recursive)
          Provides a possibility of a creating a path consisting of more than one znode (not atomic).
 GuaguaZooKeeper.PathStat createOnceExt(String path, byte[] data, List<org.apache.zookeeper.data.ACL> acl, org.apache.zookeeper.CreateMode createMode, boolean recursive)
          Create a znode if there is no other znode there
 GuaguaZooKeeper.PathStat createOrSetExt(String path, byte[] data, List<org.apache.zookeeper.data.ACL> acl, org.apache.zookeeper.CreateMode createMode, boolean recursive, int version)
          Create a znode.
 void deleteExt(String path, int version, boolean recursive)
          Delete a path recursively.
 org.apache.zookeeper.data.Stat exists(String path, boolean watch)
          Return the stat of the node of the given path.
 org.apache.zookeeper.data.Stat exists(String path, org.apache.zookeeper.Watcher watcher)
          Return the stat of the node of the given path.
 List<String> getChildrenExt(String path, boolean watch, boolean sequenceSorted, boolean fullPath)
          Get the children of the path with extensions.
 List<String> getChildrenExt(String path, boolean watch, boolean fullPath, Comparator<String> childComparator)
          Get the children of the path with extensions.
 byte[] getData(String path, boolean watch, org.apache.zookeeper.data.Stat stat)
          Return the data and the stat of the node of the given path.
 byte[] getData(String path, org.apache.zookeeper.Watcher watcher, org.apache.zookeeper.data.Stat stat)
          Return the data and the stat of the node of the given path.
 int getMaxRetryAttempts()
           
 long getRetryWaitMsecs()
           
 org.apache.zookeeper.ZooKeeper getZooKeeper()
           
protected  void retryDelay(int attemptCount)
          Performs a retry delay if this is not the first attempt
protected
<T> T
retryOperation(GuaguaZooKeeperOperation<T> operation)
          Perform the given operation, retrying if the connection fails
 
从类 java.lang.Object 继承的方法
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

构造方法详细信息

GuaguaZooKeeper

public GuaguaZooKeeper(String connectString,
                       int sessionTimeout,
                       int maxRetryAttempts,
                       int retryWaitMsecs,
                       org.apache.zookeeper.Watcher watcher)
                throws IOException
Constructor to connect to ZooKeeper, make progress

参数:
connectString - Comma separated host:port pairs, each corresponding to a zk server. e.g. "127.0.0.1:3000,127.0.0.1:3001,127.0.0.1:3002" If the optional chroot suffix is used the example would look like: "127.0.0.1:3000,127.0.0.1:3001,127.0.0.1:3002/app/a" where the client would be rooted at "/app/a" and all paths would be relative to this root - ie getting/setting/etc... "/foo/bar" would result in operations being run on "/app/a/foo/bar" (from the server perspective).
sessionTimeout - Session timeout in milliseconds
maxRetryAttempts - Max retry attempts during connection loss
retryWaitMsecs - Msecs to wait when retrying due to connection loss
watcher - A watcher object which will be notified of state changes, may also be notified for node events
抛出:
IOException - In case of any io exception to connect to zookeeper server.
方法详细信息

createExt

public String createExt(String path,
                        byte[] data,
                        List<org.apache.zookeeper.data.ACL> acl,
                        org.apache.zookeeper.CreateMode createMode,
                        boolean recursive)
                 throws org.apache.zookeeper.KeeperException,
                        InterruptedException
Provides a possibility of a creating a path consisting of more than one znode (not atomic). If recursive is false, operates exactly the same as create().

参数:
path - path to create
data - data to set on the final znode
acl - acls on each znode created
createMode - only affects the final znode
recursive - if true, creates all ancestors
返回:
Actual created path
抛出:
org.apache.zookeeper.KeeperException
InterruptedException - Both KeeperException InterruptedException are thrown from ZooKeeper methods.
NullPointerException - If path is null.

createOrSetExt

public GuaguaZooKeeper.PathStat createOrSetExt(String path,
                                               byte[] data,
                                               List<org.apache.zookeeper.data.ACL> acl,
                                               org.apache.zookeeper.CreateMode createMode,
                                               boolean recursive,
                                               int version)
                                        throws org.apache.zookeeper.KeeperException,
                                               InterruptedException
Create a znode. Set the znode if the created znode already exists.

参数:
path - path to create
data - data to set on the final znode
acl - acls on each znode created
createMode - only affects the final znode
recursive - if true, creates all ancestors
version - Version to set if setting
返回:
Path of created znode or Stat of set znode
抛出:
InterruptedException
org.apache.zookeeper.KeeperException - Both KeeperException InterruptedException are thrown from ZooKeeper methods.

createOnceExt

public GuaguaZooKeeper.PathStat createOnceExt(String path,
                                              byte[] data,
                                              List<org.apache.zookeeper.data.ACL> acl,
                                              org.apache.zookeeper.CreateMode createMode,
                                              boolean recursive)
                                       throws org.apache.zookeeper.KeeperException,
                                              InterruptedException
Create a znode if there is no other znode there

参数:
path - path to create
data - data to set on the final znode
acl - acls on each znode created
createMode - only affects the final znode
recursive - if true, creates all ancestors
返回:
Path of created znode or Stat of set znode
抛出:
InterruptedException
org.apache.zookeeper.KeeperException - Both KeeperException InterruptedException are thrown from ZooKeeper methods.

deleteExt

public void deleteExt(String path,
                      int version,
                      boolean recursive)
               throws InterruptedException,
                      org.apache.zookeeper.KeeperException
Delete a path recursively. When the deletion is recursive, it is a non-atomic operation, hence, not part of ZooKeeper.

参数:
path - path to remove (i.e. /tmp will remove /tmp/1 and /tmp/2)
version - expected version (-1 for all)
recursive - if true, remove all children, otherwise behave like remove()
抛出:
InterruptedException
org.apache.zookeeper.KeeperException - Both KeeperException InterruptedException are thrown from ZooKeeper methods.

exists

public org.apache.zookeeper.data.Stat exists(String path,
                                             boolean watch)
                                      throws org.apache.zookeeper.KeeperException,
                                             InterruptedException
Return the stat of the node of the given path. Return null if no such a node exists.

If the watch is true and the call is successful (no exception is thrown), a watch will be left on the node with the given path. The watch will be triggered by a successful operation that creates/delete the node or sets the data on the node.

参数:
path - the node path
watch - whether need to watch this node
返回:
the stat of the node of the given path; return null if no such a node exists.
抛出:
org.apache.zookeeper.KeeperException - If the server signals an error
InterruptedException - If the server transaction is interrupted.

exists

public org.apache.zookeeper.data.Stat exists(String path,
                                             org.apache.zookeeper.Watcher watcher)
                                      throws org.apache.zookeeper.KeeperException,
                                             InterruptedException
Return the stat of the node of the given path. Return null if no such a node exists.

If the watch is non-null and the call is successful (no exception is thrown), a watch will be left on the node with the given path. The watch will be triggered by a successful operation that creates/delete the node or sets the data on the node.

参数:
path - the node path
watcher - explicit watcher
返回:
the stat of the node of the given path; return null if no such a node exists.
抛出:
org.apache.zookeeper.KeeperException - If the server signals an error
InterruptedException - If the server transaction is interrupted.
IllegalArgumentException - if an invalid path is specified

getData

public byte[] getData(String path,
                      org.apache.zookeeper.Watcher watcher,
                      org.apache.zookeeper.data.Stat stat)
               throws org.apache.zookeeper.KeeperException,
                      InterruptedException
Return the data and the stat of the node of the given path.

If the watch is non-null and the call is successful (no exception is thrown), a watch will be left on the node with the given path. The watch will be triggered by a successful operation that sets data on the node, or deletes the node.

A KeeperException with error code KeeperException.NoNode will be thrown if no node with the given path exists.

参数:
path - the given path
watcher - explicit watcher
stat - the stat of the node
返回:
the data of the node
抛出:
org.apache.zookeeper.KeeperException - If the server signals an error with a non-zero error code
InterruptedException - If the server transaction is interrupted.
IllegalArgumentException - if an invalid path is specified

getData

public byte[] getData(String path,
                      boolean watch,
                      org.apache.zookeeper.data.Stat stat)
               throws org.apache.zookeeper.KeeperException,
                      InterruptedException
Return the data and the stat of the node of the given path.

If the watch is true and the call is successful (no exception is thrown), a watch will be left on the node with the given path. The watch will be triggered by a successful operation that sets data on the node, or deletes the node.

A KeeperException with error code KeeperException.NoNode will be thrown if no node with the given path exists.

参数:
path - the given path
watch - whether need to watch this node
stat - the stat of the node
返回:
the data of the node
抛出:
org.apache.zookeeper.KeeperException - If the server signals an error with a non-zero error code
InterruptedException - If the server transaction is interrupted.

getChildrenExt

public List<String> getChildrenExt(String path,
                                   boolean watch,
                                   boolean fullPath,
                                   Comparator<String> childComparator)
                            throws org.apache.zookeeper.KeeperException,
                                   InterruptedException
Get the children of the path with extensions. Extension 1: Sort the children based on childComparator number Extension 2: Get the full path instead of relative path

参数:
path - path to znode
watch - set the watch?
fullPath - if true, get the fully znode path back
childComparator - comparator to sort children.
返回:
list of children
抛出:
InterruptedException
org.apache.zookeeper.KeeperException - Both KeeperException InterruptedException are thrown from ZooKeeper methods.

getChildrenExt

public List<String> getChildrenExt(String path,
                                   boolean watch,
                                   boolean sequenceSorted,
                                   boolean fullPath)
                            throws org.apache.zookeeper.KeeperException,
                                   InterruptedException
Get the children of the path with extensions. Extension 1: Sort the children based on sequence number Extension 2: Get the full path instead of relative path

参数:
path - path to znode
watch - set the watch?
sequenceSorted - sort by the sequence number
fullPath - if true, get the fully znode path back
返回:
list of children
抛出:
InterruptedException
org.apache.zookeeper.KeeperException - Both KeeperException InterruptedException are thrown from ZooKeeper methods.

close

public void close()
           throws InterruptedException
Close this client object. Once the client is closed, its session becomes invalid. All the ephemeral nodes in the ZooKeeper server associated with the session will be removed. The watches left on those nodes (and on their parents) will be triggered.

抛出:
InterruptedException - in case of InterruptedException from ZooKeeper#close();

retryOperation

protected <T> T retryOperation(GuaguaZooKeeperOperation<T> operation)
                    throws org.apache.zookeeper.KeeperException,
                           InterruptedException
Perform the given operation, retrying if the connection fails

返回:
object. it needs to be cast to the callee's expected return type.
抛出:
org.apache.zookeeper.KeeperException
InterruptedException

retryDelay

protected void retryDelay(int attemptCount)
Performs a retry delay if this is not the first attempt

参数:
attemptCount - the number of the attempts performed so far

getZooKeeper

public org.apache.zookeeper.ZooKeeper getZooKeeper()

getMaxRetryAttempts

public int getMaxRetryAttempts()

getRetryWaitMsecs

public long getRetryWaitMsecs()


Copyright © 2014. All Rights Reserved.