/** * Close resources like zookeeper, thread pool */ protected void close() throws InterruptedException { if(this.zkHeartBeatEnabled) { stopHeartBeat(); } if(getZooKeeper() != null) { getZooKeeper().close(); } // shut down thread pool this.threadPool.shutdownNow(); this.threadPool.awaitTermination(2, TimeUnit.SECONDS); }
Stat setStat = null; try { createdPath = createExt(path, data, acl, createMode, recursive); } catch (KeeperException.NodeExistsException e) { LOG.warn("createOrSet: Node exists on path {}", path); setStat = getZooKeeper().setData(path, data, version);
@Override public Void execute() throws KeeperException, InterruptedException { if(!recursive) { getZooKeeper().delete(path, version); return null; } try { getZooKeeper().delete(path, version); return null; } catch (KeeperException.NotEmptyException e) { LOG.warn("deleteExt: Cannot directly remove node {}", path); } List<String> childList = getZooKeeper().getChildren(path, false); for(String child: childList) { deleteExt(path + GuaguaConstants.ZOOKEEPER_SEPARATOR + child, -1, true); } getZooKeeper().delete(path, version); return null; } });
/** * Perform the given operation, retrying if the connection fails * * @return object. it needs to be cast to the callee's expected return type. */ protected <T> T retryOperation(GuaguaZooKeeperOperation<T> operation) throws KeeperException, InterruptedException { KeeperException exception = null; for(int i = 0; i < this.getMaxRetryAttempts(); i++) { try { return operation.execute(); } catch (KeeperException.SessionExpiredException e) { LOG.warn("Session expired so reconnecting due to:", e); throw e; } catch (KeeperException.ConnectionLossException e) { if(exception == null) { exception = e; } LOG.debug("Attempt {} failed with connection loss so attempting to reconnect. Exception is: {} ", i, e); retryDelay(i); } } throw exception; }
getZooKeeper().createExt(znode, null, Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT, true); } catch (KeeperException.NodeExistsException e) { LOG.warn("Node exists: {}", znode); if(getZooKeeper().exists(znode, false) == null) { String znodeValue = InetAddress.getLocalHost().getHostName() + ":" + NettyMasterCoordinator.this.messageServerPort + ":" + 1; getZooKeeper().createExt(znode, znodeValue.getBytes(Charset.forName("UTF-8")), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT, false); LOG.info("Master znode initialization with server info {}", znodeValue); } else { String existZnodeValue = new String(getZooKeeper().getData(znode, null, null), Charset.forName("UTF-8")); int version = NumberFormatUtils.getInt(existZnodeValue.split(":")[2], true); String znodeValue = InetAddress.getLocalHost().getHostName() + ":" + NettyMasterCoordinator.this.messageServerPort + ":" + (version + 1); getZooKeeper().createOrSetExt(znode, znodeValue.getBytes(Charset.forName("UTF-8")), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT, false, -1); LOG.info("Master znode re-initialization with server info {}", znodeValue);
try { znode = getRootNode().toString(); stat = getZooKeeper().exists(znode, false); if(stat == null) { getZooKeeper().createExt(znode, null, Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT, false); stat = getZooKeeper().exists(znode, false); if(stat == null) { getZooKeeper().createExt(znode, null, Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT, false); stat = getZooKeeper().exists(znode, false); if(stat == null) { getZooKeeper().createExt(znode, null, Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT, false); stat = getZooKeeper().exists(znode, false); if(stat == null) { getZooKeeper().createExt(znode, null, Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT, false); getZooKeeper().createExt(znode, null, Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT, false); } catch (KeeperException.NodeExistsException e) { LOG.warn("Has such node:{}", znode); stat = getZooKeeper().exists(appMasterNode, true); if(stat == null) { LOG.info("DEBUG: wait for {}.", appMasterNode);
byte[] data = getZooKeeper().getData(znode, null, null); if(data != null) { return data; final List<String> children = getZooKeeper().getChildrenExt(splitZnode, false, true, new ChildrenComparator()); if(children == null || children.size() == 0) { return null;
String znode = getWorkerNode(appId, containerId, currentIteration - 1).toString(); try { getZooKeeper().deleteExt(znode, -1, false); if(isSplit) { znode = getCurrentWorkerSplitNode(appId, containerId, currentIteration - 1).toString(); getZooKeeper().deleteExt(znode, -1, true); Stat stat = getZooKeeper().exists(appMasterNode, true); if(stat == null) { AsyncWorkerCoordinator.this.masterIterationLock.waitForever();
GuaguaConstants.DEFAULT_MASTER_NUMBER) > 1) { String masterElectionPath = getBaseMasterElectionNode(appId).toString(); List<String> masterElectionNodes = getZooKeeper().getChildrenExt(masterElectionPath, false, true, true); isLastMaster = isLastMaster(masterElectionNodes); getZooKeeper().deleteExt(appNode, -1, true); } catch (KeeperException.NoNodeException e) { if(System.nanoTime() % 20 == 0) {
/** * Create a znode if there is no other znode there * * @param path * path to create * @param data * data to set on the final znode * @param acl * acls on each znode created * @param createMode * only affects the final znode * @param recursive * if true, creates all ancestors * @return Path of created znode or Stat of set znode * @throws InterruptedException * @throws KeeperException * Both KeeperException InterruptedException are thrown from {@link ZooKeeper} methods. */ public PathStat createOnceExt(final String path, byte[] data, List<ACL> acl, CreateMode createMode, boolean recursive) throws KeeperException, InterruptedException { try { return new PathStat(createExt(path, data, acl, createMode, recursive), null); } catch (KeeperException.NodeExistsException e) { LOG.warn("createOnceExt: Node already exists on path {}", path); } return null; }
List<String> masterIterations = null; try { masterIterations = getZooKeeper().getChildrenExt(masterBaseNode, false, false, false, new Filter() { @Override public boolean filter(String path) {
/** * Performs a retry delay if this is not the first attempt * * @param attemptCount * the number of the attempts performed so far */ protected void retryDelay(int attemptCount) { if(attemptCount > 0) { try { Thread.sleep(attemptCount * getRetryWaitMsecs()); } catch (InterruptedException e) { Thread.currentThread().interrupt(); } } }
@Override public BytesPair call() throws Exception { byte[] data = zookeeper.getData(znode, null, null); return new BytesPair(index, data); }
props.getProperty(GuaguaConstants.GUAGUA_ZK_RETRY_WAIT_MILLS), GuaguaConstants.GUAGUA_ZK_DEFAULT_RETRY_WAIT_MILLS); setZooKeeper(new GuaguaZooKeeper(zkServers, sessionTimeout, maxRetryAttempts, retryWaitMsecs, this));
workerBaseNode = getWorkerBaseNode(context.getAppId(), context.getCurrentIteration() + 1) .toString(); getZooKeeper().createExt(workerBaseNode, null, Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT, false); } catch (KeeperException.NodeExistsException e) { LOG.warn("Node exists: {}", workerBaseNode); String znode = getMasterNode(context.getAppId(), context.getCurrentIteration() - 2).toString(); try { getZooKeeper().deleteExt(znode, -1, false); if(isSplit) { znode = getCurrentMasterSplitNode(context.getAppId(), context.getCurrentIteration() - 2) .toString(); getZooKeeper().deleteExt(znode, -1, true);
try { znode = getRootNode().toString(); stat = getZooKeeper().exists(znode, false); if(stat == null) { getZooKeeper().createExt(znode, null, Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT, false); stat = getZooKeeper().exists(znode, false); if(stat == null) { getZooKeeper().createExt(znode, null, Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT, false); stat = getZooKeeper().exists(znode, false); if(stat == null) { getZooKeeper().createExt(znode, null, Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT, false); stat = getZooKeeper().exists(znode, false); if(stat == null) { getZooKeeper().createExt(znode, null, Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT, false); getZooKeeper().createExt(znode, null, Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT, false); } catch (KeeperException.NodeExistsException e) { LOG.warn("Has such node:{}", znode);
if(bytes.length > zkDataLimit) { getZooKeeper().createExt(splitZnode, null, Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT, true); int childrenSize = (bytes.length % zkDataLimit == 0) ? (bytes.length / zkDataLimit) : (bytes.length / zkDataLimit) + 1; getZooKeeper().createExt(znode, null, Ids.OPEN_ACL_UNSAFE, createNode, false); return true; } else { getZooKeeper().createExt(znode, bytes, Ids.OPEN_ACL_UNSAFE, createNode, false); return false;
return; final List<String> workerChildern = getZooKeeper().getChildrenExt(appCurrentWorkersNode, false, false, false);