@Override public OperationFuture<NodeChildren> exec(String path, Watcher watcher) { return zkClient.getChildren(path, watcher); } }, path, callback, cancelled);
@Override public OperationFuture<NodeChildren> exec(String path, Watcher watcher) { return zkClient.getChildren(path, watcher); } }, path, callback, cancelled);
@Override public OperationFuture<NodeChildren> getChildren(String path, @Nullable Watcher watcher) { return delegate.getChildren(path, watcher); }
@Override public OperationFuture<NodeChildren> getChildren(String path, @Nullable Watcher watcher) { return delegate.getChildren(path, watcher); }
@Override public OperationFuture<NodeChildren> getChildren(String path, @Nullable Watcher watcher) { return client.getChildren(path, watcher); }
@Override public OperationFuture<NodeChildren> getChildren(String path, @Nullable Watcher watcher) { return client.getChildren(path, watcher); }
/** * Acts as {@link ZKClient#getChildren(String, Watcher)} if node exists. * Otherwise sets {@code null} in returned future. */ public static ListenableFuture<NodeChildren> getChildrenOrNull(final ZKClient zkClient, final String path, @Nullable Watcher watcher) { return ignoreError(zkClient.getChildren(path, watcher), KeeperException.NoNodeException.class); }
/** * Watches for messages that are sent through ZK messages node. */ private void watchMessages() { final String messagesPath = getZKPath("messages"); Futures.addCallback(zkClient.getChildren(messagesPath, new Watcher() { @Override public void process(WatchedEvent event) { if (event.getType() == Event.EventType.NodeChildrenChanged && isRunning()) { watchMessages(); } } }), new FutureCallback<NodeChildren>() { @Override public void onSuccess(NodeChildren result) { // Sort by the name, which is the messageId. Assumption is that message ids is ordered by time. List<String> messages = Lists.newArrayList(result.getChildren()); Collections.sort(messages); for (String messageId : messages) { processMessage(messagesPath + "/" + messageId, messageId); } } @Override public void onFailure(Throwable t) { // TODO: what could be done besides just logging? LOG.error("Failed to watch messages.", t); } }, Threads.SAME_THREAD_EXECUTOR); }
/** * Watches for messages that are sent through ZK messages node. */ private void watchMessages() { final String messagesPath = getZKPath("messages"); Futures.addCallback(zkClient.getChildren(messagesPath, new Watcher() { @Override public void process(WatchedEvent event) { if (event.getType() == Event.EventType.NodeChildrenChanged && isRunning()) { watchMessages(); } } }), new FutureCallback<NodeChildren>() { @Override public void onSuccess(NodeChildren result) { // Sort by the name, which is the messageId. Assumption is that message ids is ordered by time. List<String> messages = Lists.newArrayList(result.getChildren()); Collections.sort(messages); for (String messageId : messages) { processMessage(messagesPath + "/" + messageId, messageId); } } @Override public void onFailure(Throwable t) { // TODO: what could be done besides just logging? LOG.error("Failed to watch messages.", t); } }, Threads.SAME_THREAD_EXECUTOR); }
zkClient.getChildren(CoordinationConstants.REQUIREMENTS_PATH, watcher), wrapCallback(new FutureCallback<NodeChildren>() { @Override
/** * Returns the list of children node under the given path. * * @param path path to get children * @return the list of children or empty list if the path doesn't exist. * @throws Exception if failed to get children */ private List<String> getChildren(String path) throws Exception { try { return zkClient.getChildren(path).get(TIMEOUT_SECONDS, TimeUnit.SECONDS).getChildren(); } catch (ExecutionException e) { if (e.getCause() instanceof KeeperException.NoNodeException) { // If the node doesn't exists, return an empty list return Collections.emptyList(); } throw e; } } }
private void clearInternal() { if (currentView.size() > 0) { currentView = Collections.emptyMap(); NodeChildren nodeChildren = Futures.getUnchecked(zkClient.getChildren(ENTRIES_PATH)); List<ListenableFuture<String>> deleteFutures = Lists.newArrayList(); for (String node : nodeChildren.getChildren()) { deleteFutures.add(ZKClientExt.delete(zkClient, getNodePath(node), true)); } Futures.getUnchecked(Futures.allAsList(deleteFutures)); } }
/** * Returns the list of children node under the given path. * * @param path path to get children * @return the list of children or empty list if the path doesn't exist. * @throws Exception if failed to get children */ private List<String> getChildren(String path) throws Exception { try { return zkClient.getChildren(path).get(TIMEOUT_SECONDS, TimeUnit.SECONDS).getChildren(); } catch (ExecutionException e) { if (e.getCause() instanceof KeeperException.NoNodeException) { // If the node doesn't exists, return an empty list return Collections.emptyList(); } throw e; } } }
public void clear() { currentView.set(Collections.<String, T>emptyMap()); // Hint: again, we can try to make removal more efficient by cleaning only when in-mem collection cleaned smth, // but then we may face races... NodeChildren nodeChildren = Futures.getUnchecked(zkClient.getChildren("")); List<ListenableFuture<String>> deleteFutures = Lists.newArrayList(); for (String node : nodeChildren.getChildren()) { deleteFutures.add(ZKClientExt.delete(zkClient, getNodePath(node), true)); } Futures.getUnchecked(Futures.allAsList(deleteFutures)); }
public void clear() { currentView.set(Collections.<T>emptyList()); // Hint: again, we can try to make removal more efficient by cleaning only when in-mem collection cleaned smth, // but then we may face races... NodeChildren nodeChildren = Futures.getUnchecked(zkClient.getChildren("")); List<ListenableFuture<String>> deleteFutures = Lists.newArrayList(); for (String node : nodeChildren.getChildren()) { deleteFutures.add(ZKClientExt.delete(zkClient, getNodePath(node), true)); } Futures.getUnchecked(Futures.allAsList(deleteFutures)); }
@Override public OperationFuture<NodeChildren> getChildren(String path, @Nullable Watcher watcher) { return relayFuture(delegate.getChildren(getNamespacedPath(path), watcher), this.<NodeChildren>createFuture(path)); }
@Override public OperationFuture<NodeChildren> getChildren(String path, @Nullable Watcher watcher) { return relayFuture(delegate.getChildren(getNamespacedPath(path), watcher), this.<NodeChildren>createFuture(path)); }
/** * Fetches the latest participants from ZK. This method will block until it fetched all participants information. * Note that the map returned is only a snapshot of the leader election information in ZK, which only reflects * the states in ZK at the time when the snapshot was taken. * * @return An immutable {@link SortedMap} ordered by the participant ID with the smallest key in the map * as the current leader * @throws InterruptedException if the caller thread is interrupted while waiting for the participants information * to be available * @throws Exception if failed to fetch information from ZK */ public SortedMap<Integer, Participant> fetchCurrentParticipants() throws Exception { try { NodeChildren nodeChildren = zkClient.getChildren(leaderElectionPath).get(); ConcurrentNavigableMap<Integer, Participant> result = new ConcurrentSkipListMap<>(); SettableFuture<CountDownLatch> completion = SettableFuture.create(); childrenUpdated(nodeChildren, result, completion); completion.get().await(); return Collections.unmodifiableSortedMap(result); } catch (ExecutionException e) { // If the election path doesn't exists, that means there is no participant Throwable cause = e.getCause(); if (cause instanceof KeeperException.NoNodeException) { return ImmutableSortedMap.of(); } Throwables.propagateIfPossible(cause, Exception.class); // Shouldn't reach here as we propagate any Exception/RuntimeException/Error already return ImmutableSortedMap.of(); } }
/** * Fetches the latest participants from ZK. This method will block until it fetched all participants information. * Note that the map returned is only a snapshot of the leader election information in ZK, which only reflects * the states in ZK at the time when the snapshot was taken. * * @return An immutable {@link SortedMap} ordered by the participant ID with the smallest key in the map * as the current leader * @throws InterruptedException if the caller thread is interrupted while waiting for the participants information * to be available * @throws Exception if failed to fetch information from ZK */ public SortedMap<Integer, Participant> fetchCurrentParticipants() throws Exception { try { NodeChildren nodeChildren = zkClient.getChildren(leaderElectionPath).get(); ConcurrentNavigableMap<Integer, Participant> result = new ConcurrentSkipListMap<>(); SettableFuture<CountDownLatch> completion = SettableFuture.create(); childrenUpdated(nodeChildren, result, completion); completion.get().await(); return Collections.unmodifiableSortedMap(result); } catch (ExecutionException e) { // If the election path doesn't exists, that means there is no participant Throwable cause = e.getCause(); if (cause instanceof KeeperException.NoNodeException) { return ImmutableSortedMap.of(); } Throwables.propagateIfPossible(cause, Exception.class); // Shouldn't reach here as we propagate any Exception/RuntimeException/Error already return ImmutableSortedMap.of(); } }
@Override protected void startUp() throws Exception { Futures.getUnchecked(ZKClientExt.ensureExists(zkClient, queueType.getPath())); refreshQueues(Futures.getUnchecked(zkClient.getChildren(queueType.getPath()))); ZKOperations.watchChildren(zkClient, queueType.getPath(), new ZKOperations.ChildrenCallback() { @Override public void updated(NodeChildren nodeChildren) { refreshQueues(nodeChildren); } }); }