本文整理了Java中com.twitter.common.zookeeper.ZooKeeperUtils
类的一些代码示例,展示了ZooKeeperUtils
类的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。ZooKeeperUtils
类的具体详情如下:
包路径:com.twitter.common.zookeeper.ZooKeeperUtils
类名称:ZooKeeperUtils
[英]Utilities for dealing with zoo keeper.
[中]与动物园管理员打交道的实用程序。
代码示例来源:origin: com.twitter.common/zookeeper
private synchronized void prepare()
throws ZooKeeperClient.ZooKeeperConnectionException, InterruptedException, KeeperException {
ZooKeeperUtils.ensurePath(zkClient, acl, lockPath);
LOG.log(Level.FINE, "Working with locking path:" + lockPath);
// Create an EPHEMERAL_SEQUENTIAL node.
currentNode =
zkClient.get().create(lockPath + "/member_", null, acl, CreateMode.EPHEMERAL_SEQUENTIAL);
// We only care about our actual id since we want to compare ourselves to siblings.
if (currentNode.contains("/")) {
currentId = currentNode.substring(currentNode.lastIndexOf("/") + 1);
}
LOG.log(Level.FINE, "Received ID from zk:" + currentId);
this.watcher = new LockWatcher();
}
代码示例来源:origin: com.twitter.common/zookeeper
/**
* Ensures the given {@code path} exists in the ZK cluster accessed by {@code zkClient}. If the
* path already exists, nothing is done; however if any portion of the path is missing, it will be
* created with the given {@code acl} as a persistent zookeeper node. The given {@code path} must
* be a valid zookeeper absolute path.
*
* @param zkClient the client to use to access the ZK cluster
* @param acl the acl to use if creating path nodes
* @param path the path to ensure exists
* @throws ZooKeeperConnectionException if there was a problem accessing the ZK cluster
* @throws InterruptedException if we were interrupted attempting to connect to the ZK cluster
* @throws KeeperException if there was a problem in ZK
*/
public static void ensurePath(ZooKeeperClient zkClient, List<ACL> acl, String path)
throws ZooKeeperConnectionException, InterruptedException, KeeperException {
Preconditions.checkNotNull(zkClient);
Preconditions.checkNotNull(path);
Preconditions.checkArgument(path.startsWith("/"));
ensurePathInternal(zkClient, acl, path);
}
代码示例来源:origin: com.twitter.common/zookeeper
/**
* Checks to see if the client might reasonably re-try an operation given the exception thrown
* while attempting it. If the ZooKeeper session should be expired to enable the re-try to
* succeed this method will expire it as a side-effect.
*
* @param e the exception to test
* @return true if a retry can be attempted
*/
public boolean shouldRetry(KeeperException e) {
if (e instanceof SessionExpiredException) {
close();
}
return ZooKeeperUtils.isRetryable(e);
}
代码示例来源:origin: com.twitter.common.zookeeper/group
/**
* Creates a group rooted at the given {@code path}. Paths must be absolute and trailing or
* duplicate slashes will be normalized. For example, all the following paths would create a
* group at the normalized path /my/distributed/group:
* <ul>
* <li>/my/distributed/group
* <li>/my/distributed/group/
* <li>/my/distributed//group
* </ul>
*
* @param zkClient the client to use for interactions with ZooKeeper
* @param acl the ACL to use for creating the persistent group path if it does not already exist
* @param path the absolute persistent path that represents this group
* @param nodeScheme the scheme that defines how nodes are created
*/
public Group(ZooKeeperClient zkClient, Iterable<ACL> acl, String path, NodeScheme nodeScheme) {
this.zkClient = Preconditions.checkNotNull(zkClient);
this.acl = ImmutableList.copyOf(acl);
this.path = ZooKeeperUtils.normalizePath(Preconditions.checkNotNull(path));
this.nodeScheme = Preconditions.checkNotNull(nodeScheme);
nodeNameFilter = new Predicate<String>() {
@Override public boolean apply(String nodeName) {
return Group.this.nodeScheme.isMember(nodeName);
}
};
backoffHelper = new BackoffHelper();
}
代码示例来源:origin: com.twitter.common.zookeeper/lock
private synchronized void prepare()
throws ZooKeeperClient.ZooKeeperConnectionException, InterruptedException, KeeperException {
ZooKeeperUtils.ensurePath(zkClient, acl, lockPath);
LOG.log(Level.FINE, "Working with locking path:" + lockPath);
// Create an EPHEMERAL_SEQUENTIAL node.
currentNode =
zkClient.get().create(lockPath + "/member_", null, acl, CreateMode.EPHEMERAL_SEQUENTIAL);
// We only care about our actual id since we want to compare ourselves to siblings.
if (currentNode.contains("/")) {
currentId = currentNode.substring(currentNode.lastIndexOf("/") + 1);
}
LOG.log(Level.FINE, "Received ID from zk:" + currentId);
this.watcher = new LockWatcher();
}
代码示例来源:origin: com.twitter.common/zookeeper
private static void ensurePathInternal(ZooKeeperClient zkClient, List<ACL> acl, String path)
throws ZooKeeperConnectionException, InterruptedException, KeeperException {
if (zkClient.get().exists(path, false) == null) {
// The current path does not exist; so back up a level and ensure the parent path exists
// unless we're already a root-level path.
int lastPathIndex = path.lastIndexOf('/');
if (lastPathIndex > 0) {
ensurePathInternal(zkClient, acl, path.substring(0, lastPathIndex));
}
// We've ensured our parent path (if any) exists so we can proceed to create our path.
try {
zkClient.get().create(path, null, acl, CreateMode.PERSISTENT);
} catch (KeeperException.NodeExistsException e) {
// This ensures we don't die if a race condition was met between checking existence and
// trying to create the node.
LOG.info("Node existed when trying to ensure path " + path + ", somebody beat us to it?");
}
}
}
代码示例来源:origin: com.twitter.common.zookeeper/group
@Override public Boolean get() throws JoinException {
try {
ZooKeeperUtils.ensurePath(zkClient, acl, path);
return true;
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
throw new JoinException("Interrupted trying to ensure group at path: " + path, e);
} catch (ZooKeeperConnectionException e) {
LOG.log(Level.WARNING, "Problem connecting to ZooKeeper, retrying", e);
return false;
} catch (KeeperException e) {
if (zkClient.shouldRetry(e)) {
LOG.log(Level.WARNING, "Temporary error ensuring path: " + path, e);
return false;
} else {
throw new JoinException("Problem ensuring group at path: " + path, e);
}
}
}
});
代码示例来源:origin: com.twitter.common/zookeeper
@Override public Boolean get() throws JoinException {
try {
ZooKeeperUtils.ensurePath(zkClient, acl, path);
return true;
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
throw new JoinException("Interrupted trying to ensure group at path: " + path, e);
} catch (ZooKeeperConnectionException e) {
LOG.log(Level.WARNING, "Problem connecting to ZooKeeper, retrying", e);
return false;
} catch (KeeperException e) {
if (zkClient.shouldRetry(e)) {
LOG.log(Level.WARNING, "Temporary error ensuring path: " + path, e);
return false;
} else {
throw new JoinException("Problem ensuring group at path: " + path, e);
}
}
}
});
内容来源于网络,如有侵权,请联系作者删除!