Java Code Examples for org.apache.zookeeper.ZooKeeper#getChildren()
The following examples show how to use
org.apache.zookeeper.ZooKeeper#getChildren() .
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: ZKTools.java From stategen with GNU Affero General Public License v3.0 | 6 votes |
public static String[] getTree(ZooKeeper zk,String path) throws Exception{ if(zk.exists(path, false) == null){ return new String[0]; } List<String> dealList = new ArrayList<String>(); dealList.add(path); int index =0; while(index < dealList.size()){ String tempPath = dealList.get(index); List<String> children = zk.getChildren(tempPath, false); if(tempPath.equalsIgnoreCase("/") == false){ tempPath = tempPath +"/"; } Collections.sort(children); for(int i = children.size() -1;i>=0;i--){ dealList.add(index+1, tempPath + children.get(i)); } index++; } return (String[])dealList.toArray(new String[0]); }
Example 2
Source File: ServiceDiscovery.java From Tatala-RPC with Apache License 2.0 | 6 votes |
private static void watchNode(final ZooKeeper zk) { try { List<String> nodeList = zk.getChildren(ServiceRegistry.ZK_REGISTRY_PATH, new Watcher() { @Override public void process(WatchedEvent event) { if (event.getType() == Event.EventType.NodeChildrenChanged) { watchNode(zk); } } }); List<String> dataList = new ArrayList<>(); for (String node : nodeList) { byte[] bytes = zk.getData(ServiceRegistry.ZK_REGISTRY_PATH + "/" + node, false, null); dataList.add(new String(bytes)); } ServiceDiscovery.dataList = dataList; } catch (Exception e) { log.error("ServiceDiscovery.watchNode: ", e); } }
Example 3
Source File: ZKTool.java From Scribengin with GNU Affero General Public License v3.0 | 6 votes |
static void dump(ZooKeeper zkClient, String parentPath, String node, String indentation) throws KeeperException, InterruptedException { String path = parentPath + "/" + node; byte[] bytes = zkClient.getData(path, false, new Stat()); if(bytes != null) { String data = new String(bytes); if(data.length() > 120 ) data = data.substring(0, 120); System.out.println(indentation + node + " - " + data) ; } else { System.out.println(indentation + node) ; } List<String> children = zkClient.getChildren(path, false); String childIndentation = indentation + " " ; for(int i = 0; i < children.size(); i++) { dump(zkClient, path, children.get(i), childIndentation); } }
Example 4
Source File: ZKTools.java From tbschedule with Apache License 2.0 | 6 votes |
public static String[] getTree(ZooKeeper zk, String path) throws Exception { if (zk.exists(path, false) == null) { return new String[0]; } List<String> dealList = new ArrayList<>(); dealList.add(path); int index = 0; while (index < dealList.size()) { String tempPath = dealList.get(index); List<String> children = zk.getChildren(tempPath, false); if (tempPath.equalsIgnoreCase("/") == false) { tempPath = tempPath + "/"; } Collections.sort(children); for (int i = children.size() - 1; i >= 0; i--) { dealList.add(index + 1, tempPath + children.get(i)); } index++; } return dealList.toArray(new String[0]); }
Example 5
Source File: ZKTools.java From uncode-schedule with Apache License 2.0 | 6 votes |
public static String[] getSortedTree(ZooKeeper zk, String path) throws Exception { if (zk.exists(path, false) == null) { return new String[0]; } List<String> dealList = new ArrayList<String>(); dealList.add(path); int index = 0; while (index < dealList.size()) { String tempPath = dealList.get(index); List<String> children = zk.getChildren(tempPath, false); if (tempPath.equalsIgnoreCase("/") == false) { tempPath = tempPath + "/"; } Collections.sort(children); for (int i = children.size() - 1; i >= 0; i--) { dealList.add(index + 1, tempPath + children.get(i)); } index++; } return (String[]) dealList.toArray(new String[0]); }
Example 6
Source File: ZkCopyIT.java From zkcopy with Apache License 2.0 | 6 votes |
@Test public void testSimpleCopy() throws KeeperException, InterruptedException, IOException { ZooKeeper dest = connectTo(zkDest); ZooKeeper src = connectTo(zkSource); for (int i = 0; i < 10; i++) { src.create(String.format("/itest/ephemeral/node-%s", i), Integer.toString(i).getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL); } ZkCopy.main(new String[] {"-s", connectionString(zkSource, "itest"), "-t", connectionString(zkDest, "itest"), "--ignoreEphemeralNodes", "false"}); List<String> persistentChildren = dest.getChildren("/itest/persistent", false); List<String> ephemeralChildren = dest.getChildren("/itest/ephemeral", false); dest.close(); src.close(); assertEquals("There should be 200 persistent nodes under /itest/persistent", 200, persistentChildren.size()); assertEquals("There should be 10 ephemeral nodes under /itest/ephemeral", 10, ephemeralChildren.size()); }
Example 7
Source File: WriteLock.java From disconf with Apache License 2.0 | 6 votes |
/** * find if we have been created earler if not create our node * * @param prefix the prefix node * @param zookeeper teh zookeeper client * @param dir the dir paretn * * @throws KeeperException * @throws InterruptedException */ private void findPrefixInChildren(String prefix, ZooKeeper zookeeper, String dir) throws KeeperException, InterruptedException { List<String> names = zookeeper.getChildren(dir, false); for (String name : names) { if (name.startsWith(prefix)) { id = name; if (LOG.isDebugEnabled()) { LOG.debug("Found id created last time: " + id); } break; } } if (id == null) { id = zookeeper.create(dir + "/" + prefix, data, getAcl(), EPHEMERAL_SEQUENTIAL); if (LOG.isDebugEnabled()) { LOG.debug("Created id: " + id); } } }
Example 8
Source File: ZooKeeperMigrator.java From nifi with Apache License 2.0 | 5 votes |
private ZooKeeperNode getNode(ZooKeeper zooKeeper, String path) throws KeeperException, InterruptedException { LOGGER.debug("retrieving node and children at {}", path); final List<String> children = zooKeeper.getChildren(path, false); return new ZooKeeperNode(path, children.stream().map(s -> { final String childPath = Joiner.on('/').skipNulls().join(path.equals("/") ? "" : path, s); try { return getNode(zooKeeper, childPath); } catch (InterruptedException | KeeperException e) { if (e instanceof InterruptedException) { Thread.currentThread().interrupt(); } throw new RuntimeException(String.format("unable to discover sub-tree from %s", childPath), e); } }).collect(Collectors.toList())); }
Example 9
Source File: ZookeeperDriverImpl.java From disconf with Apache License 2.0 | 5 votes |
/** * 获取指定 配置数据 * * @return * * @throws InterruptedException * @throws KeeperException */ private ZkDisconfData getDisconfData(String path, String keyName, ZooKeeper zooKeeper) throws KeeperException, InterruptedException { String curPath = path + "/" + keyName; if (zooKeeper.exists(curPath, false) == null) { return null; } ZkDisconfData zkDisconfData = new ZkDisconfData(); zkDisconfData.setKey(keyName); List<String> secChiList = zooKeeper.getChildren(curPath, false); List<ZkDisconfData.ZkDisconfDataItem> zkDisconfDataItems = new ArrayList<ZkDisconfData.ZkDisconfDataItem>(); // list for (String secKey : secChiList) { // machine ZkDisconfData.ZkDisconfDataItem zkDisconfDataItem = new ZkDisconfData.ZkDisconfDataItem(); zkDisconfDataItem.setMachine(secKey); String thirdPath = curPath + "/" + secKey; // value byte[] data = zooKeeper.getData(thirdPath, null, null); if (data != null) { zkDisconfDataItem.setValue(new String(data, CHARSET)); } // add zkDisconfDataItems.add(zkDisconfDataItem); } zkDisconfData.setData(zkDisconfDataItems); return zkDisconfData; }
Example 10
Source File: LocalDLMEmulator.java From distributedlog with Apache License 2.0 | 5 votes |
/** * Check that a number of bookies are available. * * @param count number of bookies required * @param timeout number of seconds to wait for bookies to start * @throws java.io.IOException if bookies are not started by the time the timeout hits */ public int checkBookiesUp(int count, int timeout) throws Exception { ZooKeeper zkc = connectZooKeeper(zkHost, zkPort, zkTimeoutSec); try { int mostRecentSize = 0; for (int i = 0; i < timeout; i++) { try { List<String> children = zkc.getChildren("/ledgers/available", false); children.remove("readonly"); mostRecentSize = children.size(); if ((mostRecentSize > count) || LOG.isDebugEnabled()) { LOG.info("Found " + mostRecentSize + " bookies up, " + "waiting for " + count); if ((mostRecentSize > count) || LOG.isTraceEnabled()) { for (String child : children) { LOG.info(" server: " + child); } } } if (mostRecentSize == count) { break; } } catch (KeeperException e) { // ignore } Thread.sleep(1000); } return mostRecentSize; } finally { zkc.close(); } }
Example 11
Source File: IndexerIT.java From hbase-indexer with Apache License 2.0 | 5 votes |
private void collectChildren(String path, ZooKeeper zk, List<String> paths) throws InterruptedException, KeeperException { List<String> children = zk.getChildren(path, false); for (String child : children) { String childPath = path + "/" + child; collectChildren(childPath, zk, paths); paths.add(childPath); } }
Example 12
Source File: ZookeeperClusterStatusTest.java From incubator-retired-blur with Apache License 2.0 | 5 votes |
private static void rmr(ZooKeeper zooKeeper, String path) throws KeeperException, InterruptedException { List<String> children = zooKeeper.getChildren(path, false); for (String c : children) { rmr(zooKeeper, path + "/" + c); } zooKeeper.delete(path, -1); }
Example 13
Source File: KafakOffsetMonitor.java From cognition with Apache License 2.0 | 5 votes |
public void action() throws IOException, KeeperException, InterruptedException { Watcher watcher = event -> logger.debug(event.toString()); ZooKeeper zooKeeper = new ZooKeeper(zkHosts, 3000, watcher); Map<TopicAndPartition, Long> kafkaSpoutOffsets = new HashMap<>(); Map<TopicAndPartition, SimpleConsumer> kafkaConsumers = new HashMap<>(); List<String> children = zooKeeper.getChildren(zkRoot + "/" + spoutId, false); for (String child : children) { byte[] data = zooKeeper.getData(zkRoot + "/" + spoutId + "/" + child, false, null); String json = new String(data); Config kafkaSpoutOffset = ConfigFactory.parseString(json); String topic = kafkaSpoutOffset.getString("topic"); int partition = kafkaSpoutOffset.getInt("partition"); long offset = kafkaSpoutOffset.getLong("offset"); String brokerHost = kafkaSpoutOffset.getString("broker.host"); int brokerPort = kafkaSpoutOffset.getInt("broker.port"); TopicAndPartition topicAndPartition = TopicAndPartition.apply(topic, partition); kafkaSpoutOffsets.put(topicAndPartition, offset); kafkaConsumers.put(topicAndPartition, new SimpleConsumer(brokerHost, brokerPort, 3000, 1024, "kafkaOffsetMonitor")); } zooKeeper.close(); sendToLogstash(kafkaSpoutOffsets, kafkaConsumers); }
Example 14
Source File: NodeReader.java From zkcopy with Apache License 2.0 | 5 votes |
@Override public void run() { try { if (failed.get()) { return; } ReaderThread thread = (ReaderThread) Thread.currentThread(); ZooKeeper zk = thread.getZooKeeper(); Stat stat = new Stat(); String path = znode.getAbsolutePath(); LOGGER.debug("Reading node " + path); byte[] data = zk.getData(path, false, stat); if (stat.getEphemeralOwner() != 0) { znode.setEphemeral(true); } znode.setData(data); znode.setMtime(stat.getMtime()); List<String> children = zk.getChildren(path, false); for (String child : children) { if ("zookeeper".equals(child)) { // reserved continue; } Node zchild = new Node(znode, child); znode.appendChild(zchild); pool.execute(new NodeReader(pool, zchild, totalCounter, processedCounter, failed)); } } catch (KeeperException | InterruptedException e) { LOGGER.error("Could not read from remote server", e); failed.set(true); } finally { processedCounter.incrementAndGet(); } }
Example 15
Source File: SafeModeTest.java From incubator-retired-blur with Apache License 2.0 | 5 votes |
private static void rm(ZooKeeper zk, String path) throws KeeperException, InterruptedException { List<String> children = zk.getChildren(path, false); for (String c : children) { rm(zk, path + "/" + c); } zk.delete(path, -1); }
Example 16
Source File: ZooKeeperMigrator.java From localization_nifi with Apache License 2.0 | 5 votes |
private ZooKeeperNode getNode(ZooKeeper zooKeeper, String path) throws KeeperException, InterruptedException { LOGGER.debug("retrieving node and children at {}", path); final List<String> children = zooKeeper.getChildren(path, false); return new ZooKeeperNode(path, children.stream().map(s -> { final String childPath = Joiner.on('/').skipNulls().join(path.equals("/") ? "" : path, s); try { return getNode(zooKeeper, childPath); } catch (InterruptedException | KeeperException e) { if (e instanceof InterruptedException) { Thread.currentThread().interrupt(); } throw new RuntimeException(String.format("unable to discover sub-tree from %s", childPath), e); } }).collect(Collectors.toList())); }
Example 17
Source File: h2oworker.java From h2o-2 with Apache License 2.0 | 4 votes |
@Override public String fetchFlatfile() throws Exception { System.out.printf("EmbeddedH2OConfig: fetchFlatfile called\n"); StringBuffer flatfile = new StringBuffer(); long startMillis = System.currentTimeMillis(); while (true) { ZooKeeper z = ZooKeeperFactory.makeZk(_zk); List<String> list = z.getChildren(_zkroot + "/nodes", true); z.close(); if (list.size() == _numNodes) { for (String child : list) { byte[] payload; String zkpath = _zkroot + "/nodes/" + child; z = ZooKeeperFactory.makeZk(_zk); payload = z.getData(zkpath, null, null); WorkerPayload wp = WorkerPayload.fromPayload(payload, WorkerPayload.class); flatfile.append(wp.ip).append(":").append(wp.port).append("\n"); z.close(); } break; } else if (list.size() > _numNodes) { System.out.println("EmbeddedH2OConfig: fetchFlatfile sees too many nodes (" + list.size() + " > " + _numNodes + ")"); System.exit(1); } long now = System.currentTimeMillis(); if (Math.abs(now - startMillis) > (_cloudFormationTimeoutSeconds * 1000)) { System.out.printf("EmbeddedH2OConfig: fetchFlatfile timed out waiting for cloud to form\n"); System.exit(1); } Thread.sleep(1000); } System.out.printf("EmbeddedH2OConfig: fetchFlatfile returned\n"); System.out.println("------------------------------------------------------------"); System.out.println(flatfile); System.out.println("------------------------------------------------------------"); return flatfile.toString(); }
Example 18
Source File: ResourceClaim.java From azeroth with Apache License 2.0 | 4 votes |
static String waitInLine(ZooKeeper zookeeper, String lockNode, String placeInLine) throws KeeperException, InterruptedException { List<String> children = zookeeper.getChildren(lockNode, false); Collections.sort(children); if (children.size() == 0) { logger.warn("getChildren() returned empty list, but we created a ticket."); return acquireLock(zookeeper, lockNode); } boolean lockingTicketExists = children.get(0).equals(LOCKING_TICKET); if (lockingTicketExists) { children.remove(0); } int positionInQueue = -1; int i = 0; for (String child : children) { if (child.equals(placeInLine)) { positionInQueue = i; break; } i++; } if (positionInQueue < 0) { throw new RuntimeException( "Created node (" + placeInLine + ") not found in getChildren()."); } String placeBeforeUs; if (positionInQueue == 0) { if (grabTicket(zookeeper, lockNode, LOCKING_TICKET)) { releaseTicket(zookeeper, lockNode, placeInLine); return LOCKING_TICKET; } else { placeBeforeUs = LOCKING_TICKET; } } else { placeBeforeUs = children.get(positionInQueue - 1); } final CountDownLatch latch = new CountDownLatch(1); Stat stat = zookeeper.exists(lockNode + "/" + placeBeforeUs, event -> { latch.countDown(); }); if (stat != null) { logger.debug("Watching place in queue before us ({})", placeBeforeUs); latch.await(); } return waitInLine(zookeeper, lockNode, placeInLine); }
Example 19
Source File: ZKPaths.java From curator with Apache License 2.0 | 3 votes |
/** * Return the children of the given path sorted by sequence number * * @param zookeeper the client * @param path the path * @return sorted list of children * @throws InterruptedException thread interruption * @throws org.apache.zookeeper.KeeperException zookeeper errors */ public static List<String> getSortedChildren(ZooKeeper zookeeper, String path) throws InterruptedException, KeeperException { List<String> children = zookeeper.getChildren(path, false); List<String> sortedList = Lists.newArrayList(children); Collections.sort(sortedList); return sortedList; }
Example 20
Source File: PepperBoxKafkaSampler.java From pepper-box with Apache License 2.0 | 2 votes |
private String getBrokerServers(JavaSamplerContext context) { StringBuilder kafkaBrokers = new StringBuilder(); String zookeeperServers = context.getParameter(ProducerKeys.ZOOKEEPER_SERVERS); if (zookeeperServers != null && !zookeeperServers.equalsIgnoreCase(ProducerKeys.ZOOKEEPER_SERVERS_DEFAULT)) { try { ZooKeeper zk = new ZooKeeper(zookeeperServers, 10000, null); List<String> ids = zk.getChildren(PropsKeys.BROKER_IDS_ZK_PATH, false); for (String id : ids) { String brokerInfo = new String(zk.getData(PropsKeys.BROKER_IDS_ZK_PATH + "/" + id, false, null)); JsonObject jsonObject = Json.parse(brokerInfo).asObject(); String brokerHost = jsonObject.getString(PropsKeys.HOST, ""); int brokerPort = jsonObject.getInt(PropsKeys.PORT, -1); if (!brokerHost.isEmpty() && brokerPort != -1) { kafkaBrokers.append(brokerHost); kafkaBrokers.append(":"); kafkaBrokers.append(brokerPort); kafkaBrokers.append(","); } } } catch (IOException | KeeperException | InterruptedException e) { log.error("Failed to get broker information", e); } } if (kafkaBrokers.length() > 0) { kafkaBrokers.setLength(kafkaBrokers.length() - 1); return kafkaBrokers.toString(); } else { return context.getParameter(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG); } }