/** * The asynchronous version of getChildren. * * @since 3.3.0 * * @see #getChildren(String, Watcher, Stat) */ public void getChildren(final String path, Watcher watcher, Children2Callback cb, Object ctx) { final String clientPath = path; PathUtils.validatePath(clientPath); // the watch contains the un-chroot path WatchRegistration wcb = null; if (watcher != null) { wcb = new ChildWatchRegistration(watcher, clientPath); } final String serverPath = prependChroot(clientPath); RequestHeader h = new RequestHeader(); h.setType(ZooDefs.OpCode.getChildren2); GetChildren2Request request = new GetChildren2Request(); request.setPath(serverPath); request.setWatch(watcher != null); GetChildren2Response response = new GetChildren2Response(); cnxn.queuePacket(h, new ReplyHeader(), request, response, cb, clientPath, serverPath, ctx, wcb); }
/** * The Asynchronous version of getChildren. The request doesn't actually * until the asynchronous callback is called. * * @since 3.3.0 * * @see #getChildren(String, Watcher, Stat) */ public void getChildren(final String path, Watcher watcher, Children2Callback cb, Object ctx) { final String clientPath = path; PathUtils.validatePath(clientPath); // the watch contains the un-chroot path WatchRegistration wcb = null; if (watcher != null) { wcb = new ChildWatchRegistration(watcher, clientPath); } final String serverPath = prependChroot(clientPath); RequestHeader h = new RequestHeader(); h.setType(ZooDefs.OpCode.getChildren2); GetChildren2Request request = new GetChildren2Request(); request.setPath(serverPath); request.setWatch(watcher != null); GetChildren2Response response = new GetChildren2Response(); cnxn.queuePacket(h, new ReplyHeader(), request, response, cb, clientPath, serverPath, ctx, wcb); }
@Test public void testSync() throws Exception { try { LOG.info("Starting ZK:" + (new Date()).toString()); opsCount = new CountDownLatch(limit); ZooKeeper zk = createClient(); LOG.info("Beginning test:" + (new Date()).toString()); for(int i = 0; i < 100; i++) zk.create("/test" + i, new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT, this, results); zk.sync("/test", this, results); for(int i = 0; i < 100; i++) zk.delete("/test" + i, 0, this, results); for(int i = 0; i < 100; i++) zk.getChildren("/", new NullWatcher(), (ChildrenCallback)this, results); for(int i = 0; i < 100; i++) zk.getChildren("/", new NullWatcher(), (Children2Callback)this, results); LOG.info("Submitted all operations:" + (new Date()).toString()); if(!opsCount.await(10000, TimeUnit.MILLISECONDS)) Assert.fail("Haven't received all confirmations" + opsCount.getCount()); for(int i = 0; i < limit ; i++){ Assert.assertEquals(0, (int) results.get(i)); } } catch (IOException e) { System.out.println(e.toString()); } }
@Test public void testSync() throws Exception { try { LOG.info("Starting ZK:" + (new Date()).toString()); opsCount = new CountDownLatch(limit); ZooKeeper zk = createClient(); LOG.info("Beginning test:" + (new Date()).toString()); for(int i = 0; i < 50; i++) zk.create("/test" + i, new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT, (StringCallback)this, results); for(int i = 50; i < 100; i++) { zk.create("/test" + i, new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT, (Create2Callback)this, results); } zk.sync("/test", this, results); for(int i = 0; i < 100; i++) zk.delete("/test" + i, 0, this, results); for(int i = 0; i < 100; i++) zk.getChildren("/", new NullWatcher(), (ChildrenCallback)this, results); for(int i = 0; i < 100; i++) zk.getChildren("/", new NullWatcher(), (Children2Callback)this, results); LOG.info("Submitted all operations:" + (new Date()).toString()); if(!opsCount.await(10000, TimeUnit.MILLISECONDS)) Assert.fail("Haven't received all confirmations" + opsCount.getCount()); for(int i = 0; i < limit ; i++){ Assert.assertEquals(0, (int) results.get(i)); } } catch (IOException e) { System.out.println(e.toString()); } }
@Override protected void installWatch() { connection.getChildren(getNode(), this, new Children2Callback() { public void processResult(int rc, String path, Object ctx, List<String> children, Stat stat) { } }, null); }
@Test public void testSync() throws Exception { try { LOG.info("Starting ZK:" + (new Date()).toString()); opsCount = new CountDownLatch(limit); ZooKeeper zk = createClient(); LOG.info("Beginning test:" + (new Date()).toString()); for(int i = 0; i < 100; i++) zk.create("/test" + i, new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT, this, results); zk.sync("/test", this, results); for(int i = 0; i < 100; i++) zk.delete("/test" + i, 0, this, results); for(int i = 0; i < 100; i++) zk.getChildren("/", new NullWatcher(), (ChildrenCallback)this, results); for(int i = 0; i < 100; i++) zk.getChildren("/", new NullWatcher(), (Children2Callback)this, results); LOG.info("Submitted all operations:" + (new Date()).toString()); if(!opsCount.await(10000, TimeUnit.MILLISECONDS)) fail("Haven't received all confirmations" + opsCount.getCount()); for(int i = 0; i < limit ; i++){ assertEquals(0, (int) results.get(i)); } } catch (IOException e) { System.out.println(e.toString()); } }
@Override public void getChildren(final String path, boolean watcher, final Children2Callback cb, final Object ctx) { executor.execute(() -> { mutex.lock(); if (getProgrammedFailStatus()) { mutex.unlock(); cb.processResult(failReturnCode.intValue(), path, ctx, null, null); return; } else if (stopped) { mutex.unlock(); cb.processResult(KeeperException.Code.ConnectionLoss, path, ctx, null, null); return; } else if (!tree.containsKey(path)) { mutex.unlock(); cb.processResult(KeeperException.Code.NoNode, path, ctx, null, null); return; } log.debug("getChildren path={}", path); List<String> children = Lists.newArrayList(); for (String item : tree.tailMap(path).keySet()) { log.debug("Checking path {}", item); if (!item.startsWith(path)) { break; } else if (item.equals(path)) { continue; } else { String child = item.substring(path.length() + 1); log.debug("child: '{}'", child); if (!child.contains("/")) { children.add(child); } } } log.debug("getChildren done path={} result={}", path, children); mutex.unlock(); cb.processResult(0, path, ctx, children, new Stat()); }); }
/** * The asynchronous version of getChildren. * * @since 3.3.0 * * @see #getChildren(String, boolean, Stat) */ public void getChildren(String path, boolean watch, Children2Callback cb, Object ctx) { getChildren(path, watch ? watchManager.defaultWatcher : null, cb, ctx); }
/** * The Asynchronous version of getChildren. The request doesn't actually * until the asynchronous callback is called. * * @since 3.3.0 * * @see #getChildren(String, boolean, Stat) */ public void getChildren(String path, boolean watch, Children2Callback cb, Object ctx) { getChildren(path, watch ? watchManager.defaultWatcher : null, cb, ctx); }