List of usage examples for org.apache.zookeeper ZooKeeper exists
public Stat exists(String path, boolean watch) throws KeeperException, InterruptedException
From source file:alluxio.master.ZkMasterInquireClient.java
License:Apache License
@Override public synchronized InetSocketAddress getPrimaryRpcAddress() throws UnavailableException { ensureStarted();/*from w w w. j a v a2 s. c om*/ long startTime = System.currentTimeMillis(); int tried = 0; try { CuratorZookeeperClient curatorClient = mClient.getZookeeperClient(); // #blockUntilConnectedOrTimedOut() will block for at least 1 second, even if the client is // connected within a few milliseconds. We improve the latency here by first waiting on the // connection status explicitly. for (int i = 0; i < 50; i++) { if (curatorClient.isConnected()) { break; } CommonUtils.sleepMs(20); } curatorClient.blockUntilConnectedOrTimedOut(); String leaderPath = mConnectDetails.getLeaderPath(); while (tried++ < mInquireRetryCount) { ZooKeeper zookeeper = curatorClient.getZooKeeper(); if (zookeeper.exists(leaderPath, false) != null) { List<String> masters = zookeeper.getChildren(leaderPath, null); LOG.debug("Master addresses: {}", masters); if (masters.size() >= 1) { if (masters.size() == 1) { return NetworkAddressUtils.parseInetSocketAddress(masters.get(0)); } long maxTime = 0; String leader = ""; for (String master : masters) { Stat stat = zookeeper.exists(PathUtils.concatPath(leaderPath, master), null); if (stat != null && stat.getCtime() > maxTime) { maxTime = stat.getCtime(); leader = master; } } LOG.debug("The leader master: {}", leader); return NetworkAddressUtils.parseInetSocketAddress(leader); } } else { LOG.info("{} does not exist ({})", leaderPath, tried); } CommonUtils.sleepMs(LOG, Constants.SECOND_MS); } } catch (InterruptedException e) { Thread.currentThread().interrupt(); } catch (Exception e) { LOG.error("Error getting the leader master address from zookeeper. Zookeeper: {}", mConnectDetails, e); } finally { LOG.debug("Finished getPrimaryRpcAddress() in {}ms", System.currentTimeMillis() - startTime); } throw new UnavailableException("Failed to determine primary master rpc address"); }
From source file:alluxio.MasterInquireClient.java
License:Apache License
/** * @return the address of the current leader master *///from w ww.j a v a 2 s .c o m public synchronized String getLeaderAddress() { long startTime = System.currentTimeMillis(); int tried = 0; try { CuratorZookeeperClient curatorClient = mClient.getZookeeperClient(); // #blockUntilConnectedOrTimedOut() will block for at least 1 second, even if the client is // connected within a few milliseconds. We improve the latency here by first waiting on the // connection status explicitly. for (int i = 0; i < 50; i++) { if (curatorClient.isConnected()) { break; } CommonUtils.sleepMs(20); } curatorClient.blockUntilConnectedOrTimedOut(); while (tried < mMaxTry) { ZooKeeper zookeeper = curatorClient.getZooKeeper(); if (zookeeper.exists(mLeaderPath, false) != null) { List<String> masters = zookeeper.getChildren(mLeaderPath, null); LOG.debug("Master addresses: {}", masters); if (masters.size() >= 1) { if (masters.size() == 1) { return masters.get(0); } long maxTime = 0; String leader = ""; for (String master : masters) { Stat stat = zookeeper.exists(PathUtils.concatPath(mLeaderPath, master), null); if (stat != null && stat.getCtime() > maxTime) { maxTime = stat.getCtime(); leader = master; } } LOG.debug("The leader master: {}", leader); return leader; } } else { LOG.info("{} does not exist ({})", mLeaderPath, ++tried); } CommonUtils.sleepMs(LOG, Constants.SECOND_MS); } } catch (Exception e) { LOG.error("Error getting the leader master address from zookeeper. Zookeeper address: {}", mZookeeperAddress, e); } finally { LOG.debug("Finished getLeaderAddress() in {}ms", System.currentTimeMillis() - startTime); } return null; }
From source file:cn.dataprocess.cfzk.ZkWatherUtil.java
/** * ?//w w w. ja va 2 s. c o m * * @param zooKeeper * @param watcher * @param pPath * @throws KeeperException * @throws InterruptedException */ public static void watch(ZooKeeper zooKeeper, Watcher watcher, String pPath) throws KeeperException, InterruptedException { zooKeeper.exists(pPath, watcher); zooKeeper.getData(pPath, watcher, null); zooKeeper.getChildren(pPath, watcher); }
From source file:com.alibaba.otter.shared.arbitrate.impl.zookeeper.lock.DistributedLock.java
License:Apache License
/** * lock??watch????lock?// w w w.j a v a 2 s.c o m */ private Boolean acquireLock(final BooleanMutex mutex) { try { do { if (id == null) {// ?lock long sessionId = getSessionId(); String prefix = "x-" + sessionId + "-"; // String path = zookeeper.create(root + "/" + prefix, data, CreateMode.EPHEMERAL_SEQUENTIAL); int index = path.lastIndexOf("/"); id = StringUtils.substring(path, index + 1); idName = new LockNode(id); } if (id != null) { List<String> names = zookeeper.getChildren(root); if (names.isEmpty()) { logger.warn("lock lost with scene:empty list, id[] and node[]", id, idName); unlock();// ?? } else { // ? SortedSet<LockNode> sortedNames = new TreeSet<LockNode>(); for (String name : names) { sortedNames.add(new LockNode(name)); } if (sortedNames.contains(idName) == false) { logger.warn("lock lost with scene:not contains ,id[] and node[]", id, idName); unlock();// ?? continue; } // ?ownerId ownerId = sortedNames.first().getName(); if (mutex != null && isOwner()) { mutex.set(true);// ? return true; } else if (mutex == null) { return isOwner(); } SortedSet<LockNode> lessThanMe = sortedNames.headSet(idName); if (!lessThanMe.isEmpty()) { // ? LockNode lastChildName = lessThanMe.last(); lastChildId = lastChildName.getName(); // watcher? IZkConnection connection = zookeeper.getConnection(); // zkclient?zk?lock??watcher?zk? ZooKeeper orginZk = ((ZooKeeperx) connection).getZookeeper(); Stat stat = orginZk.exists(root + "/" + lastChildId, new AsyncWatcher() { public void asyncProcess(WatchedEvent event) { if (!mutex.state()) { // ?????lock acquireLock(mutex); } else { logger.warn("locked successful."); } } }); if (stat == null) { acquireLock(mutex);// ????watcher? } } else { if (isOwner()) { mutex.set(true); } else { logger.warn("lock lost with scene:no less ,id[] and node[]", id, idName); unlock();// ?idownerId?? } } } } } while (id == null); } catch (KeeperException e) { exception = e; if (mutex != null) { mutex.set(true); } } catch (InterruptedException e) { interrupt = e; if (mutex != null) { mutex.set(true); } } catch (Throwable e) { other = e; if (mutex != null) { mutex.set(true); } } if (isOwner() && mutex != null) { mutex.set(true); } return Boolean.FALSE; }
From source file:com.alibaba.wasp.zookeeper.ZKUtil.java
License:Apache License
/** * Waits for HBase installation's base (parent) znode to become available. * * @throws java.io.IOException// www. j a v a 2 s. c o m * on ZK errors */ public static void waitForBaseZNode(Configuration conf) throws IOException { LOG.info("Waiting until the base znode is available"); String parentZNode = conf.get(HConstants.ZOOKEEPER_ZNODE_PARENT, HConstants.DEFAULT_ZOOKEEPER_ZNODE_PARENT); ZooKeeper zk = new ZooKeeper(ZKConfig.getZKQuorumServersString(conf), conf.getInt(HConstants.ZK_SESSION_TIMEOUT, HConstants.DEFAULT_ZK_SESSION_TIMEOUT), EmptyWatcher.instance); final int maxTimeMs = 10000; final int maxNumAttempts = maxTimeMs / HConstants.SOCKET_RETRY_WAIT_MS; KeeperException keeperEx = null; try { try { for (int attempt = 0; attempt < maxNumAttempts; ++attempt) { try { if (zk.exists(parentZNode, false) != null) { LOG.info("Parent znode exists: " + parentZNode); keeperEx = null; break; } } catch (KeeperException e) { keeperEx = e; } Threads.sleepWithoutInterrupt(HConstants.SOCKET_RETRY_WAIT_MS); } } finally { zk.close(); } } catch (InterruptedException ex) { Thread.currentThread().interrupt(); } if (keeperEx != null) { throw new IOException(keeperEx); } }
From source file:com.andyadc.menagerie.latches.ZkCountDownLatch.java
License:Apache License
/** * Closes the latch, signalling that this Latch has been expended. * <p>// ww w . j av a 2 s. c om * When this method is finished, all latch nodes relating to this latch will be removed, and the * state of the latchNode will be ready to accept new latches. * <p> * This method is here to allow callers to ensure the clean destruction of latches in the case where a latch * uses permanent nodes. * * @throws RuntimeException wrapping: * <ul> * <li> {@link KeeperException} if the ZooKeeper Server has trouble with the requests * <li> {@link InterruptedException} if the ZooKeeper client has trouble communicating with the ZooKeeper service * </ul> */ public void closeLatch() { ensureNodeExists(); ZooKeeper zooKeeper = zkSessionManager.getZooKeeper(); //TODO -sf- is there a non-blocking way to do this? Lock lock = new ReentrantZkLock(baseNode, zkSessionManager, privileges); try { lock.lock(); clearState(zooKeeper, latchPrefix); String readyNode = baseNode + "/countDown-latchReady"; if (zooKeeper.exists(readyNode, false) != null) { ZkUtils.safeDelete(zooKeeper, readyNode, -1); } } catch (KeeperException e) { throw new RuntimeException(e); } catch (InterruptedException e) { throw new RuntimeException(e); } finally { lock.unlock(); } }
From source file:com.andyadc.menagerie.latches.ZkCountDownLatch.java
License:Apache License
private void ensureState() { ensureNodeExists();/*from www . j a v a 2 s . co m*/ ZooKeeper zooKeeper = zkSessionManager.getZooKeeper(); //TODO -sf- is there a non-blocking way to do this? Lock lock = new ReentrantZkLock(baseNode, zkSessionManager, privileges); try { lock.lock(); if (zooKeeper.exists(baseNode + "/countDown-latchReady", false) == null) { clearState(zooKeeper, latchPrefix); zooKeeper.create(baseNode + "/countDown-latchReady", emptyNode, privileges, CreateMode.PERSISTENT); } } catch (KeeperException e) { throw new RuntimeException(e); } catch (InterruptedException e) { throw new RuntimeException(e); } finally { lock.unlock(); } }
From source file:com.andyadc.menagerie.latches.ZkCyclicBarrier.java
License:Apache License
private void checkReset() throws KeeperException, InterruptedException { //determine if we need to reset the state of the barrier ZooKeeper zooKeeper = zkSessionManager.getZooKeeper(); //TODO -sf- is there a non-blocking way to do this? Lock lock = new ReentrantZkLock(baseNode, zkSessionManager, privileges); try {/*ww w . java 2 s .c om*/ lock.lock(); if (!isCleared() && (isBroken() || isReset(zooKeeper) || zooKeeper.exists(getReadyPath(), false) == null)) { //need to reset doReset(zooKeeper); } else { //need to set the initial state of the localCounter localCount.set(ZkUtils .filterByPrefix(zkSessionManager.getZooKeeper().getChildren(baseNode, false), barrierPrefix) .size()); } } catch (KeeperException e) { throw new RuntimeException(e); } catch (InterruptedException e) { throw new RuntimeException(e); } finally { lock.unlock(); } }
From source file:com.andyadc.menagerie.latches.ZkCyclicBarrier.java
License:Apache License
private void doReset(ZooKeeper zooKeeper) throws KeeperException, InterruptedException { //put a reset node on the path to let people know it's been reset if (zooKeeper.exists(getResetPath(), false) == null) { ZkUtils.safeCreate(zooKeeper, getResetPath(), emptyNode, privileges, CreateMode.EPHEMERAL); }// w w w . j av a 2 s . co m //remove the latchReady state if (zooKeeper.exists(getReadyPath(), false) != null) { ZkUtils.safeDelete(zooKeeper, getReadyPath(), -1); } clearState(zooKeeper, barrierPrefix); //remove any broken state setters if (isBroken()) { ZkUtils.safeDelete(zooKeeper, getBrokenPath(), -1); } //remove the reset node, since we're back in a good state ZkUtils.safeDelete(zooKeeper, getBrokenPath(), -1); //delete the reset node ZkUtils.safeDelete(zooKeeper, getResetPath(), -1); //put the good state node in place ZkUtils.safeCreate(zooKeeper, getReadyPath(), emptyNode, privileges, CreateMode.PERSISTENT); }
From source file:com.andyadc.menagerie.latches.ZkCyclicBarrier.java
License:Apache License
private boolean isReset(ZooKeeper zk) throws InterruptedException, KeeperException { return zk.exists(getResetPath(), false) != null; }