public static RecoverableZooKeeper connect(Configuration conf, String ensemble, Watcher watcher, final String descriptor) throws IOException { if (ensemble == null) { throw new IOException("Unable to determine ZooKeeper ensemble"); } int timeout = conf.getInt(HConstants.ZK_SESSION_TIMEOUT, HConstants.DEFAULT_ZK_SESSION_TIMEOUT); LOG.debug(descriptor + " opening connection to ZooKeeper with ensemble (" + ensemble + ")"); int retry = conf.getInt("zookeeper.recovery.retry", 3); int retryIntervalMillis = conf.getInt( "zookeeper.recovery.retry.intervalmill", 1000); zkDumpConnectionTimeOut = conf.getInt("zookeeper.dump.connection.timeout", 1000); return new RecoverableZooKeeper(ensemble, timeout, watcher, retry, retryIntervalMillis); }
/** * <p> * NONSEQUENTIAL create is idempotent operation. Retry before throwing * exceptions. But this function will not throw the NodeExist exception back * to the application. * </p> * <p> * But SEQUENTIAL is NOT idempotent operation. It is necessary to add * identifier to the path to verify, whether the previous one is successful or * not. * </p> * * @return Path */ public String create(String path, byte[] data, List<ACL> acl, CreateMode createMode) throws KeeperException, InterruptedException { byte[] newData = appendMetaData(data); switch (createMode) { case EPHEMERAL: case PERSISTENT: return createNonSequential(path, newData, acl, createMode); case EPHEMERAL_SEQUENTIAL: case PERSISTENT_SEQUENTIAL: return createSequential(path, newData, acl, createMode); default: throw new IllegalArgumentException("Unrecognized CreateMode: " + createMode); } }
/** * Close the connection to ZooKeeper. * * @throws InterruptedException */ public void close() { try { if (recoverableZooKeeper != null) { recoverableZooKeeper.close(); } } catch (InterruptedException e) { Thread.currentThread().interrupt(); } }
RecoverableZooKeeper zk = zkw.getRecoverableZooKeeper(); waitForZKConnectionIfAuthenticating(zkw); if (zk.exists(znode, false) == null) { zk.create(znode, new byte[0], createACL(zkw, znode), CreateMode.PERSISTENT); } catch (KeeperException.NoAuthException nee) { try { if (null == zkw.getRecoverableZooKeeper().exists(znode, false)) {
throws KeeperException { try { Stat s = zkw.getRecoverableZooKeeper().exists(znode, zkw); boolean exists = s != null ? true : false; if (exists) {
if (!first) { String previousResult = findPreviousSequentialNode(newPath); if (previousResult != null) { return previousResult; case SESSIONEXPIRED: case OPERATIONTIMEOUT: retryOrThrow(retryCounter, e, "create"); break;
throws KeeperException, InterruptedException { RetryCounter retryCounter = retryCounterFactory.create(); byte[] newData = appendMetaData(data); boolean isRetry = false; while (true) { case SESSIONEXPIRED: case OPERATIONTIMEOUT: retryOrThrow(retryCounter, e, "setData"); break; case BADVERSION:
/** * Delete the specified node with the specified version. Sets no watches. * Throws all exceptions. */ public static boolean deleteNode(ZooKeeperWatcher zkw, String node, int version) throws KeeperException { try { zkw.getRecoverableZooKeeper().delete(node, version); return true; } catch (KeeperException.BadVersionException bve) { return false; } catch (InterruptedException ie) { zkw.interruptedException(ie); return false; } }
try { waitForZKConnectionIfAuthenticating(zkw); zkw.getRecoverableZooKeeper().create(znode, data, createACL(zkw, znode), CreateMode.EPHEMERAL); } catch (KeeperException.NodeExistsException nee) {
private String findPreviousSequentialNode(String path) throws KeeperException, InterruptedException { int lastSlashIdx = path.lastIndexOf('/'); assert (lastSlashIdx != -1); String parent = path.substring(0, lastSlashIdx); String nodePrefix = path.substring(lastSlashIdx + 1); List<String> nodes = zk.getChildren(parent, false); List<String> matching = filterByPrefix(nodes, nodePrefix); for (String node : matching) { String nodePath = parent + "/" + node; Stat stat = zk.exists(nodePath, false); if (stat != null) { return nodePath; } } return null; }
/** * Check if the specified node exists. Sets no watches. * * @param zkw * zk reference * @param znode * path of node to watch * @return version of the node if it exists, -1 if does not exist * @throws org.apache.zookeeper.KeeperException * if unexpected zookeeper exception */ public static int checkExists(ZooKeeperWatcher zkw, String znode) throws KeeperException { try { Stat s = zkw.getRecoverableZooKeeper().exists(znode, null); return s != null ? s.getVersion() : -1; } catch (KeeperException e) { LOG.warn(zkw.prefix("Unable to set watcher on znode (" + znode + ")"), e); zkw.keeperException(e); return -1; } catch (InterruptedException e) { LOG.warn(zkw.prefix("Unable to set watcher on znode (" + znode + ")"), e); zkw.interruptedException(e); return -1; } }
/** * Deletes the specified node. Fails silent if the node does not exist. * * @param zkw * @param node * @throws org.apache.zookeeper.KeeperException */ public static void deleteNodeFailSilent(ZooKeeperWatcher zkw, String node) throws KeeperException { try { zkw.getRecoverableZooKeeper().delete(node, -1); } catch (KeeperException.NoNodeException nne) { } catch (InterruptedException ie) { zkw.interruptedException(ie); } }
zkw.getRecoverableZooKeeper().create(znode, new byte[0], createACL(zkw, znode), CreateMode.PERSISTENT); } catch (KeeperException.NodeExistsException nee) {
try { waitForZKConnectionIfAuthenticating(zkw); zkw.getRecoverableZooKeeper().create(znode, data, createACL(zkw, znode), CreateMode.PERSISTENT); } catch (KeeperException.NodeExistsException nee) { try { zkw.getRecoverableZooKeeper().exists(znode, zkw); } catch (InterruptedException e) { zkw.interruptedException(e);
/** * Get the number of children of the specified node. * * If the node does not exist or has no children, returns 0. * * Sets no watches at all. * * @param zkw * zk reference * @param znode * path of node to count children of * @return number of children of specified node, 0 if none or parent does not * exist * @throws org.apache.zookeeper.KeeperException * if unexpected zookeeper exception */ public static int getNumberOfChildren(ZooKeeperWatcher zkw, String znode) throws KeeperException { try { Stat stat = zkw.getRecoverableZooKeeper().exists(znode, null); return stat == null ? 0 : stat.getNumChildren(); } catch (KeeperException e) { LOG.warn(zkw.prefix("Unable to get children of node " + znode)); zkw.keeperException(e); } catch (InterruptedException e) { zkw.interruptedException(e); } return 0; }
/** * Delete the specified node and all of it's children. * <p> * If the node does not exist, just returns. * <p> * Sets no watches. Throws all exceptions besides dealing with deletion of * children. */ public static void deleteNodeRecursively(ZooKeeperWatcher zkw, String node) throws KeeperException { try { List<String> children = ZKUtil.listChildrenNoWatch(zkw, node); // the node is already deleted, so we just finish if (children == null) return; if (!children.isEmpty()) { for (String child : children) { deleteNodeRecursively(zkw, joinZNode(node, child)); } } zkw.getRecoverableZooKeeper().delete(node, -1); } catch (InterruptedException ie) { zkw.interruptedException(ie); } }
Stat stats = zkw.getRecoverableZooKeeper().exists(path, false); LOG.info("EPHEMERAL NODE BEFORE SERVER ABORT, path=" + path + ", stats=" + stats);