@Override public void setWatch() throws KeeperException, InterruptedException { zk.getChildren(ZkPath.append(path, COMMAND_QUEUE_PATH), this); }
private void dumpZk(String parentPath, String nodeName, int depth) throws Exception { System.err.print(StringUtils.repeat(" ", depth)); System.err.print("/" + nodeName); String nodePath = ZkPath.append(parentPath, nodeName); nodePath = nodePath.replace("//", "/"); byte[] data = zk.getData(nodePath, false, null); if (data == null) { System.err.print(" -> null"); } else { System.err.print(" -> [bytes]"); } System.err.println(); List<String> children = zk.getChildren(nodePath, false); for (String child : children) { dumpZk(nodePath, child, depth + 1); } }
private int countDescendants(String node) throws InterruptedException, KeeperException { List<String> children = zk.getChildren(node, false); int count = 0; for (String child : children) { count += countDescendants(ZkPath.append(node, child)); } return 1 + count; }
@Override public List<HostCommand> getCommandQueue() throws IOException { try { List<String> children = zk.getChildren(ZkPath.append(path, COMMAND_QUEUE_PATH), false); Collections.sort(children); List<HostCommand> queue = new ArrayList<HostCommand>(); for (String child : children) { queue.add(HostCommand.valueOf(zk.getString(ZkPath.append(path, COMMAND_QUEUE_PATH, child)))); } return queue; } catch (Exception e) { throw new IOException(e); } }
private boolean syncMap() { try { final List<String> childrenRelPaths = zk.getChildren(path, watcher);
@Override public void clearCommandQueue() throws IOException { try { List<String> children = zk.getChildren(ZkPath.append(path, COMMAND_QUEUE_PATH), false); for (String child : children) { zk.delete(ZkPath.append(path, COMMAND_QUEUE_PATH, child), 0); } } catch (Exception e) { throw new IOException(e); } }
System.out.println("Result: " + count); } else if (command.equals("ls")) { List<String> children = zkCli.zk.getChildren(argument, false); for (String child : children) { System.out.println(ZkPath.append(argument, child));
@Override public HostCommand nextCommand() throws IOException { try { // get the queue and sort so we have correct ordering List<String> children = zk.getChildren(ZkPath.append(path, COMMAND_QUEUE_PATH), false); Collections.sort(children); // if there are no children, the queue is empty. if (children.size() == 0) { currentCommand.set(null); return null; } // parse out the actual command String headOfQueuePath = ZkPath.append(path, COMMAND_QUEUE_PATH, children.get(0)); HostCommand nextCommand = HostCommand.valueOf(zk.getString(headOfQueuePath)); // set the current command first (modifying the queue will call the queue listeners) currentCommand.set(nextCommand); // delete the head of the queue zk.delete(headOfQueuePath, -1); return nextCommand; } catch (Exception e) { throw new IOException(e); } }
public void deleteNodeRecursively(String path) throws InterruptedException, KeeperException { try { delete(path, -1); } catch (KeeperException.NotEmptyException e) { List<String> children = getChildren(path, null); for (String child : children) { deleteNodeRecursively(ZkPath.append(path, child)); } delete(path, -1); } catch (KeeperException.NoNodeException e) { // Silently return if the node has already been deleted. return; } }