@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 boolean equals(Object o) { if (this == o) { return true; } if (o == null || !(o instanceof NodeChildren)) { return false; } NodeChildren that = (NodeChildren) o; return stat.equals(that.getStat()) && children.equals(that.getChildren()); }
@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 boolean equals(Object o) { if (this == o) { return true; } if (o == null || !(o instanceof NodeChildren)) { return false; } NodeChildren that = (NodeChildren) o; return stat.equals(that.getStat()) && children.equals(that.getChildren()); }
@Override public void updated(NodeChildren nodeChildren) { resourceReport.setServices(nodeChildren.getChildren()); } });
@Override public boolean equals(Object o) { if (this == o) { return true; } if (o == null || !(o instanceof NodeChildren)) { return false; } NodeChildren that = (NodeChildren) o; return stat.equals(that.getStat()) && children.equals(that.getChildren()); }
@Override public void updated(NodeChildren nodeChildren) { resourceReport.setServices(nodeChildren.getChildren()); } });
@Override public boolean equals(Object o) { if (this == o) { return true; } if (o == null || !(o instanceof NodeChildren)) { return false; } NodeChildren that = (NodeChildren) o; return stat.equals(that.getStat()) && children.equals(that.getChildren()); }
private void refreshQueues(NodeChildren nodeChildren) { Set<String> queueNames = Sets.newHashSet(nodeChildren.getChildren()); Set<String> existingQueues = queueMap.asMap().keySet(); Set<String> toAdd = Sets.difference(queueNames, existingQueues); for (String queueName : toAdd) { queueMap.refresh(queueName); } Set<String> toRemove = Sets.difference(existingQueues, queueNames); for (String queueName : toRemove) { queueMap.invalidate(queueName); } }
@Override public boolean equals(Object o) { if (this == o) { return true; } if (o == null || !(o instanceof NodeChildren)) { return false; } NodeChildren that = (NodeChildren) o; return stat.equals(that.getStat()) && children.equals(that.getChildren()); }
/** * Find the maximum node that is smaller than node of the given lockPath. * * @return the node found or {@code null} if no such node exist */ private String findNodeToWatch(NodeChildren children, String lockPath, String guid) { // Lock path is "path/guid-id" int guidLen = guid.length(); int id = Integer.parseInt(lockPath.substring(path.length() + guidLen + 2)); String nodeToWatch = null; int maxOfMins = Integer.MIN_VALUE; for (String node : children.getChildren()) { int nodeId = Integer.parseInt(node.substring(guidLen + 1)); if (nodeId < id && nodeId > maxOfMins) { maxOfMins = nodeId; nodeToWatch = path + "/" + node; } } return nodeToWatch; }
private void reloadCacheIfNeeded() { NodeChildren nodeChildren = Futures.getUnchecked(ZKClientExt.getChildrenOrNull(zkClient, ENTRIES_PATH)); if (nodeChildren == null) { if (currentView.size() > 0) { currentView = Collections.emptyMap(); } return; } // we use children version to detect if we need to update local view int trueVersion = nodeChildren.getStat().getCversion(); if (currentViewVersion == trueVersion) { return; } List<String> nodes = nodeChildren.getChildren(); final Map<String, ListenableFuture<NodeData>> nodeAndDataFutures = Maps.newHashMap(); List<OperationFuture<NodeData>> dataFutures = Lists.newArrayList(); for (String node : nodes) { OperationFuture<NodeData> dataFuture = zkClient.getData(getNodePath(node)); dataFutures.add(dataFuture); nodeAndDataFutures.put(node, dataFuture); } Futures.getUnchecked(Futures.successfulAsList(dataFutures)); ImmutableMap.Builder<String, T> builder = ImmutableMap.builder(); for (Entry<String, ListenableFuture<NodeData>> nodeAndData : nodeAndDataFutures.entrySet()) { T value = serializer.deserialize(Futures.getUnchecked(nodeAndData.getValue()).getData()); builder.put(nodeAndData.getKey(), value); } currentView = builder.build(); currentViewVersion = trueVersion; }
/** * Find the maximum node that is smaller than node of the given lockPath. * * @return the node found or {@code null} if no such node exist */ private String findNodeToWatch(NodeChildren children, String lockPath, String guid) { // Lock path is "path/guid-id" int guidLen = guid.length(); int id = Integer.parseInt(lockPath.substring(path.length() + guidLen + 2)); String nodeToWatch = null; int maxOfMins = Integer.MIN_VALUE; for (String node : children.getChildren()) { int nodeId = Integer.parseInt(node.substring(guidLen + 1)); if (nodeId < id && nodeId > maxOfMins) { maxOfMins = nodeId; nodeToWatch = path + "/" + node; } } return nodeToWatch; }
@Override public void onSuccess(NodeChildren result) { Set<String> children = ImmutableSet.copyOf(result.getChildren()); // Handle new resources for (String child : children) { String path = CoordinationConstants.REQUIREMENTS_PATH + "/" + child; Watcher requirementWatcher = wrapWatcher(new ResourceRequirementWatcher(path)); fetchAndProcessRequirement(path, requirementWatcher); } // Handle removed resources for (String removed: ImmutableSet.copyOf(Sets.difference(requirements.keySet(), children))) { ResourceRequirement requirement = requirements.remove(removed); LOG.info("Requirement deleted {}", requirement); // Delete the assignment node. removeAssignment(removed); } }
@Override public void onSuccess(NodeChildren result) { Set<String> children = ImmutableSet.copyOf(result.getChildren()); // Handle new resources for (String child : children) { String path = CoordinationConstants.REQUIREMENTS_PATH + "/" + child; Watcher requirementWatcher = wrapWatcher(new ResourceRequirementWatcher(path)); fetchAndProcessRequirement(path, requirementWatcher); } // Handle removed resources for (String removed: ImmutableSet.copyOf(Sets.difference(requirements.keySet(), children))) { ResourceRequirement requirement = requirements.remove(removed); LOG.info("Requirement deleted {}", requirement); // Delete the assignment node. removeAssignment(removed); } }
/** * Find the maximum node that is smaller than node of the given lockPath. * * @return the node found or {@code null} if no such node exist */ private String findNodeToWatch(NodeChildren children, String lockPath, String guid) { // Lock path is "path/PREFIX-guid-id" int guidLen = guid.length(); int id = Integer.parseInt(lockPath.substring(path.length() + 1 + PREFIX.length() + 1 + guidLen + 1)); String nodeToWatch = null; int maxOfMins = Integer.MIN_VALUE; for (String node : children.getChildren()) { if (node.startsWith(PREFIX)) { int nodeId = Integer.parseInt(node.substring(PREFIX.length() + 1 + guidLen + 1)); if (nodeId < id && nodeId > maxOfMins) { maxOfMins = nodeId; nodeToWatch = path + "/" + node; } } } return nodeToWatch; }
@Override public void onSuccess(NodeChildren result) { Optional<String> nodeToWatch = findNodeToWatch(result.getChildren()); if (state == State.CANCELLED) { deleteNode(); return; } if (nodeToWatch == null) { // zkNodePath unknown, need to run register. register(); return; } if (nodeToWatch.isPresent()) { // Watch for deletion of largest node smaller than current node watchNode(zkFolderPath + "/" + nodeToWatch.get(), new LowerNodeWatcher()); } else { // This is leader becomeLeader(); } }
@Override public void onSuccess(NodeChildren result) { Optional<String> nodeToWatch = findNodeToWatch(result.getChildren()); if (state == State.CANCELLED) { deleteNode(); return; } if (nodeToWatch == null) { // zkNodePath unknown, need to run register. register(); return; } if (nodeToWatch.isPresent()) { // Watch for deletion of largest node smaller than current node watchNode(zkFolderPath + "/" + nodeToWatch.get(), new LowerNodeWatcher()); } else { // This is leader becomeLeader(); } }
@Override public void onSuccess(NodeChildren result) { Optional<String> nodeToWatch = findNodeToWatch(result.getChildren()); if (state == State.CANCELLED) { deleteNode(); return; } if (nodeToWatch == null) { // zkNodePath unknown, need to run register. register(); return; } if (nodeToWatch.isPresent()) { // Watch for deletion of largest node smaller than current node watchNode(zkFolderPath + "/" + nodeToWatch.get(), new LowerNodeWatcher()); } else { // This is leader becomeLeader(); } }
/** * 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; } } }