public List<String> getChildren(CuratorFramework zk, String path, boolean watch) throws Exception { String npath = PathUtils.normalize_path(path); if (watch) { return zk.getChildren().watched().forPath(npath); } else { return zk.getChildren().forPath(npath); } }
public byte[] getData(CuratorFramework zk, String path, boolean watch) throws Exception { String npath = PathUtils.normalize_path(path); try { if (existsNode(zk, npath, watch)) { if (watch) { return zk.getData().watched().forPath(npath); } else { return zk.getData().forPath(npath); } } } catch (KeeperException e) { LOG.error("zookeeper getdata for path" + path, e); } return null; }
public boolean existsNode(CuratorFramework zk, String path, boolean watch) throws Exception { Stat stat = null; if (watch) { stat = zk.checkExists().watched() .forPath(PathUtils.normalize_path(path)); } else { stat = zk.checkExists().forPath(PathUtils.normalize_path(path)); } return stat != null; }
@Override public Void call() throws Exception { zk.checkExists().usingWatcher(callback).forPath(buildZookeeperPath(pathSuffix)); return null; } });
public byte[] getMemberData(String path){ try{ if(transport.getZKHandle().checkExists().usingWatcher(watcher).forPath(path) != null){ byte[] data = transport.getZKHandle().getData().usingWatcher(watcher).forPath(path); return data; } }catch(Exception e){ LOGGER.error( "Exception while fetching data for " + path, e); } return null; }
private void wathPath(String nodePath) { try { this.watchPathDataChange(nodePath); GetChildrenBuilder childrenBuilder = client.getChildren(); try { List<String> children = null; if (StringUtils.isEmpty(nodePath)) { children = childrenBuilder.watched().forPath(null); } else { children = childrenBuilder.watched().forPath(nodePath); } this.watchChildDataChange(children, nodePath); } catch (Exception e) { throw Throwables.propagate(e); } } catch (Exception e) { logger.error("load zk config error namespace={}", configOption.getNameSpace()); logger.error("load config error", e); throw new RuntimeException("load zk error"); } }
private void checkRevocableWatcher(String path) throws Exception { RevocationSpec entry = revocable.get(); if ( entry != null ) { try { byte[] bytes = client.getData().usingWatcher(revocableWatcher).forPath(path); if ( Arrays.equals(bytes, REVOKE_MESSAGE) ) { entry.getExecutor().execute(entry.getRunnable()); } } catch ( KeeperException.NoNodeException ignore ) { // ignore } } }
private void loadConfig(String nodePath) { try { if (StringUtils.isNotEmpty(nodePath)) { loadData(nodePath); } GetChildrenBuilder childrenBuilder = client.getChildren(); try { List<String> children = null; if (StringUtils.isEmpty(nodePath)) { children = childrenBuilder.watched().forPath(null); } else { children = childrenBuilder.watched().forPath(nodePath); } loadChildsConfig(children, nodePath); } catch (Exception e) { throw Throwables.propagate(e); } } catch (Exception e) { logger.error("load zk config error namespace={}", configOption.getNameSpace()); logger.error("load config error", e); this.client.close(); throw new RuntimeException("load zk error"); } }
private void watchPathDataChange(String path) throws Exception { this.client.getData().usingWatcher(getPathWatcher()).forPath(path); }
/** * Set watch on each child node to receive notifications on node data * change/creation/deletion notifications * * @param path */ @SuppressWarnings("restriction") public void getChildrenAndSetWatch(String path) { List<String> children = null; try { children = transport.getZKHandle().getChildren().usingWatcher(watcher) .forPath(path); for (String child : children) { String childnodePath = (new StringBuilder()).append(path) .append("/").append(child).toString(); transport.getZKHandle().checkExists().usingWatcher(watcher) .forPath(childnodePath); getChildrenAndSetWatch(childnodePath); } }catch(NoNodeException nee){ LOGGER.warn( " NoNodeException While setting children watch for " + path + nee.getMessage()); } catch (Exception e) { LOGGER.error( " Exception While setting watch on " + path, e); } }
@SuppressWarnings("static-access") public void createsubgroups(String path) throws Exception { if (transport.getZKHandle().checkExists().usingWatcher(watcher).forPath(groupname) != null) { final String subgrouppath; if (transport.getZKHandle().checkExists().usingWatcher(watcher).forPath(subgrouppath) == null) { transport.getZKHandle().getZookeeperClient() .newRetryLoop()
@SuppressWarnings("static-access") public void setMemberData(final String membername, final byte[] data) throws Exception { final String memberpath = new StringBuffer(groupname).append(FWD_SLASH) .append(membername).toString(); if (transport.getZKHandle().checkExists().usingWatcher(watcher).forPath(memberpath) == null) { join(groupname); } transport.getZKHandle().getZookeeperClient().newRetryLoop() .callWithRetry(transport.getZKHandle().getZookeeperClient(), new Callable<Stat>() { public Stat call() throws Exception { return transport.getZKHandle().setData().forPath(memberpath, data); } }); }
@SuppressWarnings("static-access") public void setGroupMemberData(final String groupname, final byte[] data) throws Exception { final String memberpath = new StringBuffer(groupname).append(FWD_SLASH) .append(membername).toString(); if (transport.getZKHandle().checkExists().usingWatcher(watcher).forPath(memberpath) == null) { join(groupname); } transport.getZKHandle().getZookeeperClient().newRetryLoop() .callWithRetry(transport.getZKHandle().getZookeeperClient(), new Callable<Stat>() { public Stat call() throws Exception { return transport.getZKHandle().setData().forPath(memberpath, data); } }); }
/** * Member can join the group as ephemeral node. * * @throws Exception */ @SuppressWarnings("static-access") public void join() throws Exception { final String memberpath = new StringBuffer(groupname).append(FWD_SLASH) .append(membername).toString(); if (transport.getZKHandle().checkExists().usingWatcher(watcher).forPath(memberpath) == null) { transport.getZKHandle().getZookeeperClient() .newRetryLoop() .callWithRetry(transport.getZKHandle().getZookeeperClient(), new Callable<String>() { public String call() throws Exception { return transport.getZKHandle().create() .creatingParentsIfNeeded() .withMode(CreateMode.EPHEMERAL) .forPath(memberpath); } }); } }
/** * Enter the barrier and block until all members have entered or the timeout has * elapsed * * @param maxWait max time to block * @param unit time unit * @return true if the entry was successful, false if the timeout elapsed first * @throws Exception interruptions, errors, etc. */ public boolean enter(long maxWait, TimeUnit unit) throws Exception { long startMs = System.currentTimeMillis(); boolean hasMaxWait = (unit != null); long maxWaitMs = hasMaxWait ? TimeUnit.MILLISECONDS.convert(maxWait, unit) : Long.MAX_VALUE; boolean readyPathExists = (client.checkExists().usingWatcher(watcher).forPath(readyPath) != null); client.create().creatingParentsIfNeeded().withMode(CreateMode.EPHEMERAL).forPath(ourPath); boolean result = (readyPathExists || internalEnter(startMs, hasMaxWait, maxWaitMs)); if ( connectionLost.get() ) { throw new KeeperException.ConnectionLossException(); } return result; }
@SuppressWarnings("static-access") public void join(final String groupname) throws Exception { create(groupname); final String memberpath = new StringBuffer(groupname).append(FWD_SLASH) .append(membername).toString(); if (transport.getZKHandle().checkExists().usingWatcher(watcher).forPath(memberpath) == null) { transport.getZKHandle().getZookeeperClient() .newRetryLoop() .callWithRetry(transport.getZKHandle().getZookeeperClient(), new Callable<String>() { public String call() throws Exception { return transport.getZKHandle().create() .creatingParentsIfNeeded() .withMode(CreateMode.EPHEMERAL) .forPath(memberpath); } }); } setChildrenWatch(groupname); }
try nodes = (watcher != null) ? client.getChildren().usingWatcher(watcher).forPath(path) : client.getChildren().forPath(path);
@SuppressWarnings("static-access") private void creategroup() throws Exception { if (transport.getZKHandle().checkExists().usingWatcher(watcher).forPath(groupname) == null) { transport.getZKHandle().getZookeeperClient() .newRetryLoop() .callWithRetry(transport.getZKHandle().getZookeeperClient(), new Callable<String>() { public String call() throws Exception { return transport.getZKHandle().create() .withMode(CreateMode.PERSISTENT) .forPath(groupname); } }); } }
/** * Member can join the subgroup. if subgroup not exists, it will be created * based * * @param subgroup */ @SuppressWarnings("static-access") private void create(final String subgroup) throws Exception { if (transport.getZKHandle().checkExists().usingWatcher(watcher).forPath(subgroup) == null) { transport.getZKHandle().getZookeeperClient() .newRetryLoop() .callWithRetry(transport.getZKHandle().getZookeeperClient(), new Callable<String>() { public String call() throws Exception { return transport.getZKHandle().create() .creatingParentsIfNeeded() .withMode(CreateMode.PERSISTENT) .forPath(subgroup); } }); } }