Java Code Examples for org.apache.zookeeper.data.Stat#getNumChildren()
The following examples show how to use
org.apache.zookeeper.data.Stat#getNumChildren() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: UsageListing.java From exhibitor with Apache License 2.0 | 6 votes |
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); } } }
Example 2
Source File: ChildReaper.java From big-c with Apache License 2.0 | 6 votes |
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); } } }
Example 3
Source File: ChildReaper.java From hadoop with Apache License 2.0 | 6 votes |
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); } } }
Example 4
Source File: ZookeeperRegistryCenter.java From idworker with Apache License 2.0 | 5 votes |
@Override public int getNumChildren(final String key) { try { Stat stat = client.checkExists().forPath(key); if (null != stat) { return stat.getNumChildren(); } } catch (final Exception ex) { RegExceptionHandler.handleException(ex); } return 0; }
Example 5
Source File: CuratorManager.java From Singularity with Apache License 2.0 | 5 votes |
protected int getNumChildren(String path) { try { Stat s = curator.checkExists().forPath(path); if (s != null) { return s.getNumChildren(); } } catch (NoNodeException nne) {} catch (Throwable t) { throw new RuntimeException(t); } return 0; }
Example 6
Source File: HierarchicalDataHolder.java From helix with Apache License 2.0 | 5 votes |
private boolean refreshRecursively(Node<T> oldRoot, Node<T> newRoot, String path) { boolean dataChanged = false; Stat newStat = _zkClient.getStat(path); Stat oldStat = (oldRoot != null) ? oldRoot.stat : null; newRoot.name = path; if (newStat != null) { if (oldStat == null) { newRoot.stat = newStat; newRoot.data = _zkClient.<T> readData(path, true); dataChanged = true; } else if (newStat.equals(oldStat)) { newRoot.stat = oldStat; newRoot.data = oldRoot.data; } else { dataChanged = true; newRoot.stat = newStat; newRoot.data = _zkClient.<T> readData(path, true); } if (newStat.getNumChildren() > 0) { List<String> children = _zkClient.getChildren(path); for (String child : children) { String newPath = path + "/" + child; Node<T> oldChild = (oldRoot != null && oldRoot.children != null) ? oldRoot.children.get(child) : null; if (newRoot.children == null) { newRoot.children = new ConcurrentHashMap<>(); } if (!newRoot.children.contains(child)) { newRoot.children.put(child, new Node<T>()); } Node<T> newChild = newRoot.children.get(child); boolean childChanged = refreshRecursively(oldChild, newChild, newPath); dataChanged = dataChanged || childChanged; } } } else { logger.info(path + " does not exist"); } return dataChanged; }
Example 7
Source File: ScheduleDataManager4ZK.java From uncode-schedule with Apache License 2.0 | 5 votes |
@Override public boolean isOwner(ScheduleTask scheduleTask) throws Exception { Stat tempStat = null; //查看集群中是否注册当前任务,如果没有就自动注册 String zkPath = this.pathTask + "/" + scheduleTask.getName(); if (this.zkManager.isAutoRegisterTask()) { tempStat = this.getZooKeeper().exists(zkPath, false); if (tempStat == null) { this.getZooKeeper().create(zkPath, null, this.zkManager.getAcl(), CreateMode.PERSISTENT); tempStat = this.getZooKeeper().exists(zkPath, false); if (LOG.isDebugEnabled()) { LOG.debug(scheduleTask.getUuid() + ":自动向集群注册任务[" + scheduleTask.getName() + "]"); } } } //@wjw_note: 当task下的子节点为空时,进行等待,防止错过执行! if (tempStat.getNumChildren() == 0) { int sleepCount = 0; Thread.sleep(1000); tempStat = this.zkManager.getZooKeeper().exists(zkPath, false); while (tempStat.getNumChildren() == 0 && sleepCount < 100) { sleepCount++; Thread.sleep(1000); tempStat = this.zkManager.getZooKeeper().exists(zkPath, false); } } //判断是否分配给当前节点 zkPath = zkPath + "/" + scheduleTask.getUuid(); if (this.getZooKeeper().exists(zkPath, false) != null) { //@wjw_note: 写一些数据 this.refreshScheduleTask(scheduleTask); return true; } return false; }
Example 8
Source File: ZookeeperRegistryCenter.java From shardingsphere-elasticjob-lite with Apache License 2.0 | 5 votes |
@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; }
Example 9
Source File: ZkClient.java From TakinRPC with Apache License 2.0 | 5 votes |
public int countChildren(String path) { try { Stat stat = new Stat(); this.readData(path, stat); return stat.getNumChildren(); //return getChildren(path).size(); } catch (ZkNoNodeException e) { return -1; } }
Example 10
Source File: ZookeeperRegistryCenter.java From eagle with Apache License 2.0 | 5 votes |
@Override public int getNumChildren(final String key) { try { Stat stat = client.getZookeeperClient().getZooKeeper().exists(getNameSpace() + key, false); if (null != stat) { return stat.getNumChildren(); } //CHECKSTYLE:OFF } catch (final Exception ex) { //CHECKSTYLE:ON throw new EagleFrameException(ex); } return 0; }
Example 11
Source File: ZookeeperRegistryCenter.java From shardingsphere-elasticjob-cloud with Apache License 2.0 | 5 votes |
@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; }
Example 12
Source File: ZooKeeperStringCodecImpl.java From EasyTransaction with Apache License 2.0 | 5 votes |
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); } }
Example 13
Source File: MqProducerChangeListener.java From paascloud-master with Apache License 2.0 | 5 votes |
private int getNumChildren(CuratorFramework client, final String key) { log.info("获取生产者节点个数 path={}", key); Stat stat = null; try { stat = client.checkExists().forPath(key); } catch (final Exception ex) { log.error("获取目录 key={}的子节点个数出现异常={}", key, ex.getMessage(), ex); } return stat == null ? 0 : stat.getNumChildren(); }
Example 14
Source File: MqConsumerChangeListener.java From paascloud-master with Apache License 2.0 | 5 votes |
private int getNumChildrenCount(CuratorFramework client, final String key) { log.info("获取消费者节点个数 path={}", key); Stat stat = null; try { stat = client.checkExists().forPath(key); } catch (final Exception ex) { log.error("获取目录 key={}的子节点个数出现异常={}", key, ex.getMessage(), ex); } return stat == null ? 0 : stat.getNumChildren(); }
Example 15
Source File: ZookeeperRegistryCenter.java From paascloud-master with Apache License 2.0 | 5 votes |
/** * Gets num children. * * @param key the key * * @return the num children */ @Override public int getNumChildren(final String key) { Stat stat = null; try { stat = client.checkExists().forPath(key); } catch (final Exception ex) { RegExceptionHandler.handleException(ex); } return stat == null ? 0 : stat.getNumChildren(); }
Example 16
Source File: ChildReaper.java From xian with Apache License 2.0 | 5 votes |
private void addPathToReaperIfEmpty(String path) throws Exception { Stat stat = client.checkExists().forPath(path); if ( (stat != null) && (stat.getNumChildren() == 0) ) { log.info("Adding " + path); reaper.addPath(path, mode); } }
Example 17
Source File: ChildReaper.java From xian with Apache License 2.0 | 4 votes |
private void doWork() { if ( shouldDoWork() ) { if ( (pathIterator == null) || !pathIterator.hasNext() ) { pathIterator = paths.iterator(); } while ( pathIterator.hasNext() ) { String path = pathIterator.next(); try { int maxChildren = this.maxChildren.get(); if ( maxChildren > 0 ) { Stat stat = client.checkExists().forPath(path); if ( (stat != null) && (stat.getNumChildren() > maxChildren) ) { warnMaxChildren(path, stat); continue; } } List<String> children = client.getChildren().forPath(path); log.info(String.format("Found %d children for %s", children.size(), path)); for ( String name : children ) { String childPath = ZKPaths.makePath(path, name); addPathToReaperIfEmpty(childPath); for ( String subNode : lockSchema ) { addPathToReaperIfEmpty(ZKPaths.makePath(childPath, subNode)); } } } catch ( Exception e ) { ThreadUtils.checkInterrupted(e); log.error("Could not get children for path: " + path, e); } } } }
Example 18
Source File: ZkDistributedQueue.java From lucene-solr with Apache License 2.0 | 4 votes |
/** * Inserts data into queue. If there are no other queue consumers, the offered element * will be immediately visible when this method returns. */ @Override public void offer(byte[] data) throws KeeperException, InterruptedException { Timer.Context time = stats.time(dir + "_offer"); try { while (true) { try { if (maxQueueSize > 0) { if (offerPermits.get() <= 0 || offerPermits.getAndDecrement() <= 0) { // If a max queue size is set, check it before creating a new queue item. Stat stat = zookeeper.exists(dir, null, true); if (stat == null) { // jump to the code below, which tries to create dir if it doesn't exist throw new KeeperException.NoNodeException(); } int remainingCapacity = maxQueueSize - stat.getNumChildren(); if (remainingCapacity <= 0) { throw new IllegalStateException("queue is full"); } // Allow this client to push up to 1% of the remaining queue capacity without rechecking. offerPermits.set(remainingCapacity / 100); } } // Explicitly set isDirty here so that synchronous same-thread calls behave as expected. // This will get set again when the watcher actually fires, but that's ok. zookeeper.create(dir + "/" + PREFIX, data, CreateMode.PERSISTENT_SEQUENTIAL, true); isDirty = true; return; } catch (KeeperException.NoNodeException e) { try { zookeeper.create(dir, new byte[0], CreateMode.PERSISTENT, true); } catch (KeeperException.NodeExistsException ne) { // someone created it } } } } finally { time.stop(); } }
Example 19
Source File: DistributedMap.java From lucene-solr with Apache License 2.0 | 4 votes |
public int size() throws KeeperException, InterruptedException { Stat stat = new Stat(); zookeeper.getData(dir, null, stat, true); return stat.getNumChildren(); }