use of org.apache.twill.zookeeper.NodeChildren in project cdap by caskdata.
the class LeaderElectionInfoService method fetchCurrentParticipants.
/**
* 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();
}
}
use of org.apache.twill.zookeeper.NodeChildren in project cdap by caskdata.
the class ResourceCoordinator method fetchAndProcessAllResources.
/**
* Fetches all {@link ResourceRequirement} and perform assignment for the one that changed. Also, it will
* remove assignments for the resource requirements that are removed.
*/
private void fetchAndProcessAllResources(final Watcher watcher) {
Futures.addCallback(zkClient.getChildren(CoordinationConstants.REQUIREMENTS_PATH, watcher), wrapCallback(new FutureCallback<NodeChildren>() {
@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 onFailure(Throwable t) {
// If the resource path node doesn't exists, resort to watch for exists.
if (t instanceof KeeperException.NoNodeException) {
beginWatch(watcher);
}
// Otherwise, it's a unexpected failure.
LOG.error("Failed to getChildren on ZK node {}{}", zkClient.getConnectString(), CoordinationConstants.REQUIREMENTS_PATH, t);
doNotifyFailed(t);
}
}), executor);
}
use of org.apache.twill.zookeeper.NodeChildren in project cdap by caskdata.
the class UpgradeTool method ensureCDAPMasterStopped.
/**
* Checks for appfabric service path on zookeeper, if they exist, CDAP master is still running, so throw
* exception message with information on where its running.
* @throws Exception if at least one master is running
*/
private void ensureCDAPMasterStopped() throws Exception {
String appFabricPath = String.format("/discoverable/%s", Constants.Service.APP_FABRIC_HTTP);
NodeChildren nodeChildren = zkClientService.getChildren(appFabricPath).get();
List<String> runningNodes = new ArrayList<>();
// if no children nodes at appfabric path, all master nodes are stopped
if (!nodeChildren.getChildren().isEmpty()) {
for (String runId : nodeChildren.getChildren()) {
// only one children would be present, as only the active master will be registered at this path
NodeData nodeData = zkClientService.getData(String.format("%s/%s", appFabricPath, runId)).get();
Discoverable discoverable = GSON.fromJson(Bytes.toString(nodeData.getData()), Discoverable.class);
runningNodes.add(discoverable.getSocketAddress().getHostName());
}
String exceptionMessage = String.format("CDAP Master is still running on %s, please stop it before running upgrade.", com.google.common.base.Joiner.on(",").join(runningNodes));
throw new Exception(exceptionMessage);
}
// CDAP-11733 As a future improvement, the upgrade tool can register as a CDAP master to become the leader
// and prevent other masters from starting.
}
Aggregations