Search in sources :

Example 1 with NodeData

use of org.apache.twill.zookeeper.NodeData in project cdap by caskdata.

the class ResourceCoordinatorClient method watchAssignment.

/**
   * Starts watching ZK for ResourceAssignment changes for the given service.
   */
private void watchAssignment(final String serviceName) {
    final String zkPath = CoordinationConstants.ASSIGNMENTS_PATH + "/" + serviceName;
    // Watch for both getData() and exists() call
    Watcher watcher = wrapWatcher(new AssignmentWatcher(serviceName, EnumSet.of(Watcher.Event.EventType.NodeDataChanged, Watcher.Event.EventType.NodeDeleted)));
    Futures.addCallback(zkClient.getData(zkPath, watcher), wrapCallback(new FutureCallback<NodeData>() {

        @Override
        public void onSuccess(NodeData result) {
            try {
                ResourceAssignment assignment = CoordinationConstants.RESOURCE_ASSIGNMENT_CODEC.decode(result.getData());
                LOG.debug("Received resource assignment for {}. {}", serviceName, assignment.getAssignments());
                handleAssignmentChange(serviceName, assignment);
            } catch (Exception e) {
                LOG.error("Failed to decode ResourceAssignment {}", Bytes.toStringBinary(result.getData()), e);
            }
        }

        @Override
        public void onFailure(Throwable t) {
            if (t instanceof KeeperException.NoNodeException) {
                // Treat it as assignment has been removed. If the node doesn't exists for the first time fetch data,
                // there will be no oldAssignment, hence the following call would be a no-op.
                handleAssignmentChange(serviceName, new ResourceAssignment(serviceName));
                // Watch for exists if it still interested
                synchronized (ResourceCoordinatorClient.this) {
                    if (changeListeners.containsKey(serviceName)) {
                        watchAssignmentOnExists(serviceName);
                    }
                }
            } else {
                LOG.error("Failed to getData on ZK {}{}", zkClient.getConnectString(), zkPath, t);
                doNotifyFailed(t);
            }
        }
    }), Threads.SAME_THREAD_EXECUTOR);
}
Also used : Watcher(org.apache.zookeeper.Watcher) FutureCallback(com.google.common.util.concurrent.FutureCallback) KeeperException(org.apache.zookeeper.KeeperException) NodeData(org.apache.twill.zookeeper.NodeData)

Example 2 with NodeData

use of org.apache.twill.zookeeper.NodeData 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

NodeData (org.apache.twill.zookeeper.NodeData)2 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 Discoverable (org.apache.twill.discovery.Discoverable)1 NodeChildren (org.apache.twill.zookeeper.NodeChildren)1 KeeperException (org.apache.zookeeper.KeeperException)1 Watcher (org.apache.zookeeper.Watcher)1