@Override public int getNumChildren(final String key) { try { Stat stat = client.checkExists().forPath(key); if (null != stat) { return stat.getNumChildren(); } //CHECKSTYLE:OFF } catch (final Exception ex) { //CHECKSTYLE:ON RegExceptionHandler.handleException(ex); } return 0; }
@Override public CompletableFuture<Integer> getCurrentNrHRS() { return zk.exists(znodePaths.rsZNode).thenApply(s -> s != null ? s.getNumChildren() : 0); }
public void print(Stat stat) { out.println("cZxid = 0x" + Long.toHexString(stat.getCzxid())); out.println("ctime = " + new Date(stat.getCtime()).toString()); out.println("mZxid = 0x" + Long.toHexString(stat.getMzxid())); out.println("mtime = " + new Date(stat.getMtime()).toString()); out.println("pZxid = 0x" + Long.toHexString(stat.getPzxid())); out.println("cversion = " + stat.getCversion()); out.println("dataVersion = " + stat.getVersion()); out.println("aclVersion = " + stat.getAversion()); out.println("ephemeralOwner = 0x" + Long.toHexString(stat.getEphemeralOwner())); out.println("dataLength = " + stat.getDataLength()); out.println("numChildren = " + stat.getNumChildren()); } }
private void doWork() { for ( String path : paths ) { try { List<String> children = client.getChildren().forPath(path); for ( String name : children ) { String thisPath = ZKPaths.makePath(path, name); Stat stat = client.checkExists().forPath(thisPath); if ( (stat != null) && (stat.getNumChildren() == 0) ) { reaper.addPath(thisPath, mode); } } } catch ( Exception e ) { log.error("Could not get children for path: " + path, e); } } } }
private void processNode(String path, NodeEntry parent) throws Exception { Stat stat = exhibitor.getLocalConnection().checkExists().forPath(path); if ( stat == null ) { return; // probably got deleted } NodeEntry entry = new NodeEntry(parent, stat.getNumChildren(), stat.getCtime()); details.put(path, entry); entry.addToDeepCount(stat.getNumChildren()); if ( stat.getNumChildren() <= maxChildren ) { List<String> children = exhibitor.getLocalConnection().getChildren().forPath(path); for ( String child : children ) { String thisPath = ZKPaths.makePath(path, child); processNode(thisPath, entry); } } } }
private static void printStat(Stat stat) { System.err.println("cZxid = 0x" + Long.toHexString(stat.getCzxid())); System.err.println("ctime = " + new Date(stat.getCtime()).toString()); System.err.println("mZxid = 0x" + Long.toHexString(stat.getMzxid())); System.err.println("mtime = " + new Date(stat.getMtime()).toString()); System.err.println("pZxid = 0x" + Long.toHexString(stat.getPzxid())); System.err.println("cversion = " + stat.getCversion()); System.err.println("dataVersion = " + stat.getVersion()); System.err.println("aclVersion = " + stat.getAversion()); System.err.println("ephemeralOwner = 0x" + Long.toHexString(stat.getEphemeralOwner())); System.err.println("dataLength = " + stat.getDataLength()); System.err.println("numChildren = " + stat.getNumChildren()); }
/** * 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 KeeperException if unexpected zookeeper exception */ public static int getNumberOfChildren(ZKWatcher 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; }
private void register(String stringType, String value) { try { //can not switch the order of sysStat and appStat.because of the zk sequential consistency Stat sysStat = client.checkExists().creatingParentContainersIfNeeded().forPath(getSystemTypePath(stringType)); Stat appStringStat = client.checkExists().forPath(getAppBaseItemPath(stringType, value)); if(appStringStat != null) { return; } if(sysStat == null) { createNodeIfNotExists(getSystemTypePath(stringType)); } createNodeIfNotExists(getAppTypePath(stringType)); int numChildren = 0; if(sysStat != null) { numChildren = sysStat.getNumChildren(); } client.inTransaction() .create().forPath(getSystemBaseItemPath(stringType, numChildren + 1),value.getBytes()).and() .create().forPath(getAppBaseItemPath(stringType, value),ByteBuffer.allocate(4).putInt(numChildren + 1).array()).and() .commit(); } catch (Exception e) { LOG.warn("register string in zk failed", e); } }
@Test public void testGetAndExists() throws Exception { assertArrayEquals(DATA, RO_ZK.get(PATH).get()); assertEquals(CHILDREN, RO_ZK.exists(PATH).get().getNumChildren()); assertNotNull(RO_ZK.zookeeper); waitForIdleConnectionClosed(); }
Assert.assertEquals(0, stat.getEphemeralOwner()); Assert.assertEquals(name.length(), stat.getDataLength()); Assert.assertEquals(i + 1, stat.getNumChildren()); Assert.assertEquals(s.size(), stat.getNumChildren());
Assert.assertEquals(0, stat.getEphemeralOwner()); Assert.assertEquals(name.length(), stat.getDataLength()); Assert.assertEquals(1, stat.getNumChildren()); Assert.assertEquals(s.size(), stat.getNumChildren()); Assert.assertEquals(zk.getSessionId(), stat.getEphemeralOwner()); Assert.assertEquals(childname.length(), stat.getDataLength()); Assert.assertEquals(0, stat.getNumChildren()); Assert.assertEquals(s.size(), stat.getNumChildren());
@Test public void testChildren() throws IOException, KeeperException, InterruptedException { String name = "/foo"; zk.create(name, name.getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); for(int i = 0; i < 10; i++) { String childname = name + "/bar" + i; zk.create(childname, childname.getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL); Stat stat; stat = newStat(); zk.getData(name, false, stat); Assert.assertEquals(stat.getCzxid(), stat.getMzxid()); Assert.assertEquals(stat.getCzxid() + i + 1, stat.getPzxid()); Assert.assertEquals(stat.getCtime(), stat.getMtime()); Assert.assertEquals(i + 1, stat.getCversion()); Assert.assertEquals(0, stat.getVersion()); Assert.assertEquals(0, stat.getAversion()); Assert.assertEquals(0, stat.getEphemeralOwner()); Assert.assertEquals(name.length(), stat.getDataLength()); Assert.assertEquals(i + 1, stat.getNumChildren()); } }
@Test public void testBasic() throws IOException, KeeperException, InterruptedException { String name = "/foo"; zk.create(name, name.getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); Stat stat; stat = newStat(); zk.getData(name, false, stat); Assert.assertEquals(stat.getCzxid(), stat.getMzxid()); Assert.assertEquals(stat.getCzxid(), stat.getPzxid()); Assert.assertEquals(stat.getCtime(), stat.getMtime()); Assert.assertEquals(0, stat.getCversion()); Assert.assertEquals(0, stat.getVersion()); Assert.assertEquals(0, stat.getAversion()); Assert.assertEquals(0, stat.getEphemeralOwner()); Assert.assertEquals(name.length(), stat.getDataLength()); Assert.assertEquals(0, stat.getNumChildren()); }
static public void copyStat(Stat from, Stat to) { to.setAversion(from.getAversion()); to.setCtime(from.getCtime()); to.setCversion(from.getCversion()); to.setCzxid(from.getCzxid()); to.setMtime(from.getMtime()); to.setMzxid(from.getMzxid()); to.setPzxid(from.getPzxid()); to.setVersion(from.getVersion()); to.setEphemeralOwner(from.getEphemeralOwner()); to.setDataLength(from.getDataLength()); to.setNumChildren(from.getNumChildren()); }
static public void copyStat(Stat from, Stat to) { to.setAversion(from.getAversion()); to.setCtime(from.getCtime()); to.setCversion(from.getCversion()); to.setCzxid(from.getCzxid()); to.setMtime(from.getMtime()); to.setMzxid(from.getMzxid()); to.setVersion(from.getVersion()); to.setEphemeralOwner(from.getEphemeralOwner()); to.setDataLength(from.getDataLength()); to.setNumChildren(from.getNumChildren()); }
Assert.assertEquals(0, stat.getEphemeralOwner()); Assert.assertEquals(name.length(), stat.getDataLength()); Assert.assertEquals(1, stat.getNumChildren()); Assert.assertEquals(zk.getSessionId(), stat.getEphemeralOwner()); Assert.assertEquals(childname.length(), stat.getDataLength()); Assert.assertEquals(0, stat.getNumChildren());
Assert.assertEquals(0, stat.getEphemeralOwner()); Assert.assertEquals(name.length(), stat.getDataLength()); Assert.assertEquals(0, stat.getNumChildren()); Assert.assertEquals(0, stat.getEphemeralOwner()); Assert.assertEquals(name.length() * 2, stat.getDataLength()); Assert.assertEquals(0, stat.getNumChildren());
private void validateCreateStat(Stat stat, String name) { Assert.assertEquals(stat.getCzxid(), stat.getMzxid()); Assert.assertEquals(stat.getCzxid(), stat.getPzxid()); Assert.assertEquals(stat.getCtime(), stat.getMtime()); Assert.assertEquals(0, stat.getCversion()); Assert.assertEquals(0, stat.getVersion()); Assert.assertEquals(0, stat.getAversion()); Assert.assertEquals(0, stat.getEphemeralOwner()); Assert.assertEquals(name.length(), stat.getDataLength()); Assert.assertEquals(0, stat.getNumChildren()); } }
static public void copyStat(Stat from, Stat to) { to.setAversion(from.getAversion()); to.setCtime(from.getCtime()); to.setCversion(from.getCversion()); to.setCzxid(from.getCzxid()); to.setMtime(from.getMtime()); to.setMzxid(from.getMzxid()); to.setPzxid(from.getPzxid()); to.setVersion(from.getVersion()); to.setEphemeralOwner(from.getEphemeralOwner()); to.setDataLength(from.getDataLength()); to.setNumChildren(from.getNumChildren()); }
private void validateCreateStat(Stat stat, String name) { Assert.assertEquals(stat.getCzxid(), stat.getMzxid()); Assert.assertEquals(stat.getCzxid(), stat.getPzxid()); Assert.assertEquals(stat.getCtime(), stat.getMtime()); Assert.assertEquals(0, stat.getCversion()); Assert.assertEquals(0, stat.getVersion()); Assert.assertEquals(0, stat.getAversion()); Assert.assertEquals(0, stat.getEphemeralOwner()); Assert.assertEquals(name.length(), stat.getDataLength()); Assert.assertEquals(0, stat.getNumChildren()); } }