Search in sources :

Example 1 with NodeChildren

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();
    }
}
Also used : ConcurrentSkipListMap(java.util.concurrent.ConcurrentSkipListMap) CountDownLatch(java.util.concurrent.CountDownLatch) ExecutionException(java.util.concurrent.ExecutionException) KeeperException(org.apache.zookeeper.KeeperException) TimeoutException(java.util.concurrent.TimeoutException) KeeperException(org.apache.zookeeper.KeeperException) ExecutionException(java.util.concurrent.ExecutionException) NodeChildren(org.apache.twill.zookeeper.NodeChildren)

Example 2 with NodeChildren

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);
}
Also used : Watcher(org.apache.zookeeper.Watcher) FutureCallback(com.google.common.util.concurrent.FutureCallback) NodeChildren(org.apache.twill.zookeeper.NodeChildren)

Example 3 with NodeChildren

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.
}
Also used : Discoverable(org.apache.twill.discovery.Discoverable) ArrayList(java.util.ArrayList) DatasetManagementException(co.cask.cdap.api.dataset.DatasetManagementException) IOException(java.io.IOException) NodeChildren(org.apache.twill.zookeeper.NodeChildren) NodeData(org.apache.twill.zookeeper.NodeData)

Aggregations

NodeChildren (org.apache.twill.zookeeper.NodeChildren)3 DatasetManagementException (co.cask.cdap.api.dataset.DatasetManagementException)1 FutureCallback (com.google.common.util.concurrent.FutureCallback)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 ConcurrentSkipListMap (java.util.concurrent.ConcurrentSkipListMap)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 ExecutionException (java.util.concurrent.ExecutionException)1 TimeoutException (java.util.concurrent.TimeoutException)1 Discoverable (org.apache.twill.discovery.Discoverable)1 NodeData (org.apache.twill.zookeeper.NodeData)1 KeeperException (org.apache.zookeeper.KeeperException)1 Watcher (org.apache.zookeeper.Watcher)1