/** * Fetches the {@link ResourceRequirement} for the given resource. * * @param resourceName Name of the resource. * @return A {@link ListenableFuture} that will be completed when the requirement is fetch. A {@code null} result * will be set into the future if no such requirement exists. The future will fail if failed to fetch * the requirement due to error other than requirement not exists. * Calling {@link ListenableFuture#cancel(boolean)} has no effect. */ public ListenableFuture<ResourceRequirement> fetchRequirement(String resourceName) { String zkPath = CoordinationConstants.REQUIREMENTS_PATH + "/" + resourceName; return Futures.transform( ZKOperations.ignoreError(zkClient.getData(zkPath), KeeperException.NoNodeException.class, null), NODE_DATA_TO_REQUIREMENT ); }
@Override protected void startUp() throws Exception { cancellable = ZKOperations.watchChildren(zkClient, leaderElectionPath, new ZKOperations.ChildrenCallback() { @Override public void updated(NodeChildren nodeChildren) { childrenUpdated(nodeChildren, participants, readyFuture); } }); }
public static ListenableFuture<String> watchDeleted(final ZKClient zkClient, final String path) { SettableFuture<String> completion = SettableFuture.create(); watchDeleted(zkClient, path, completion); return completion; }
@Nullable Location logLevelLocation) { Futures.getUnchecked(ZKOperations.ignoreError( ZKOperations.recursiveDelete(zkClient, "/" + runId), KeeperException.NoNodeException.class, null));
@Override protected void shutDown() throws Exception { LOG.info("Removing container ZK path: {}{}", zkClient.getConnectString(), path); ZKOperations.recursiveDelete(zkClient, path).get(TIMEOUT_SECONDS, TimeUnit.SECONDS); } }
/** * Creates the live node for the service. If the node already exists, it will be deleted before creation. * * @return A {@link OperationFuture} that will be completed when the creation is done. */ private OperationFuture<String> createLiveNode() { final String liveNodePath = getLiveNodePath(); LOG.info("Creating live node {}{}", zkClient.getConnectString(), liveNodePath); return ZKOperations.createDeleteIfExists(zkClient, liveNodePath, serializeLiveNode(), CreateMode.EPHEMERAL, true); }
@Nullable Location logLevelLocation) { Futures.getUnchecked(ZKOperations.ignoreError( ZKOperations.recursiveDelete(zkClient, "/" + runId), KeeperException.NoNodeException.class, null));
@Override protected void shutDown() throws Exception { LOG.info("Removing container ZK path: {}{}", zkClient.getConnectString(), path); ZKOperations.recursiveDelete(zkClient, path).get(TIMEOUT_SECONDS, TimeUnit.SECONDS); } }
@Override protected void startUp() throws Exception { // Create the ZK node for Kafka to use. If the node already exists, delete it to make sure there is // no left over content from previous AM attempt. LOG.info("Preparing Kafka ZK path {}{}", zkClient.getConnectString(), kafkaZKPath); ZKOperations.createDeleteIfExists(zkClient, kafkaZKPath, null, CreateMode.PERSISTENT, true).get(); kafkaServer.startAndWait(); }
/** * Fetches the {@link ResourceRequirement} for the given resource. * * @param resourceName Name of the resource. * @return A {@link ListenableFuture} that will be completed when the requirement is fetch. A {@code null} result * will be set into the future if no such requirement exists. The future will fail if failed to fetch * the requirement due to error other than requirement not exists. * Calling {@link ListenableFuture#cancel(boolean)} has no effect. */ public ListenableFuture<ResourceRequirement> fetchRequirement(String resourceName) { String zkPath = CoordinationConstants.REQUIREMENTS_PATH + "/" + resourceName; return Futures.transform( ZKOperations.ignoreError(zkClient.getData(zkPath), KeeperException.NoNodeException.class, null), NODE_DATA_TO_REQUIREMENT ); }
@Override protected void startUp() throws Exception { cancellable = ZKOperations.watchChildren(zkClient, leaderElectionPath, new ZKOperations.ChildrenCallback() { @Override public void updated(NodeChildren nodeChildren) { childrenUpdated(nodeChildren, participants, readyFuture); } }); }
@Override public void onFailure(final Throwable createFailure) { // If create failed not because of the NodeExistsException, just set the exception to the result future if (!(createFailure instanceof KeeperException.NodeExistsException)) { resultFuture.setException(createFailure); return; } // Try to delete the path LOG.info("Node {}{} already exists. Deleting it and retry creation", zkClient.getConnectString(), path); Futures.addCallback(recursiveDelete(zkClient, path), new FutureCallback<String>() { @Override public void onSuccess(String result) { // If delete succeeded, perform the creation again. createNode(zkClient, path, data, createMode, createParent, createACLs, createCallback); } @Override public void onFailure(Throwable t) { // If deletion failed because of NoNodeException, fail the result operation future if (!(t instanceof KeeperException.NoNodeException)) { createFailure.addSuppressed(t); resultFuture.setException(createFailure); return; } // If can't delete because the node no longer exists, just go ahead and recreate the node createNode(zkClient, path, data, createMode, createParent, createACLs, createCallback); } }, Threads.SAME_THREAD_EXECUTOR); } });
public static ListenableFuture<String> watchDeleted(final ZKClient zkClient, final String path) { SettableFuture<String> completion = SettableFuture.create(); watchDeleted(zkClient, path, completion); return completion; }
@Override protected void startUp() throws Exception { // Create the ZK node for Kafka to use. If the node already exists, delete it to make sure there is // no left over content from previous AM attempt. LOG.info("Preparing Kafka ZK path {}{}", zkClient.getConnectString(), kafkaZKPath); ZKOperations.createDeleteIfExists(zkClient, kafkaZKPath, null, CreateMode.PERSISTENT, true).get(); kafkaServer.startAndWait(); }
/** * Deletes the {@link ResourceRequirement} for the given resource. * * @param resourceName Name of the resource. * @return A {@link ListenableFuture} that will be completed when the requirement is successfully removed. * If the requirement doesn't exists, the deletion would still be treated as successful. */ public ListenableFuture<String> deleteRequirement(String resourceName) { String zkPath = CoordinationConstants.REQUIREMENTS_PATH + "/" + resourceName; return Futures.transform( ZKOperations.ignoreError(zkClient.delete(zkPath), KeeperException.NoNodeException.class, resourceName), Functions.constant(resourceName) ); }
/** * Watch for changes to services under given path. * @param path to check for changes. */ void addWatcher(String path) { ZKOperations.watchChildren(zkClient, path, new ZKOperations.ChildrenCallback() { @Override public void updated(NodeChildren nodeChildren) { resourceReport.setServices(nodeChildren.getChildren()); } }); }
@Override public void onFailure(final Throwable createFailure) { // If create failed not because of the NodeExistsException, just set the exception to the result future if (!(createFailure instanceof KeeperException.NodeExistsException)) { resultFuture.setException(createFailure); return; } // Try to delete the path LOG.info("Node {}{} already exists. Deleting it and retry creation", zkClient.getConnectString(), path); Futures.addCallback(recursiveDelete(zkClient, path), new FutureCallback<String>() { @Override public void onSuccess(String result) { // If delete succeeded, perform the creation again. createNode(zkClient, path, data, createMode, createParent, createACLs, createCallback); } @Override public void onFailure(Throwable t) { // If deletion failed because of NoNodeException, fail the result operation future if (!(t instanceof KeeperException.NoNodeException)) { createFailure.addSuppressed(t); resultFuture.setException(createFailure); return; } // If can't delete because the node no longer exists, just go ahead and recreate the node createNode(zkClient, path, data, createMode, createParent, createACLs, createCallback); } }, Threads.SAME_THREAD_EXECUTOR); } });
@Override public void onSuccess(String path) { Futures.addCallback(ZKOperations.watchDeleted(zkClient, path), new FutureCallback<String>() { @Override public void onSuccess(String result) { completion.set(completionResult); } @Override public void onFailure(Throwable t) { completion.setException(t); } }); }
/** * Creates the live node for the service. If the node already exists, it will be deleted before creation. * * @return A {@link OperationFuture} that will be completed when the creation is done. */ private OperationFuture<String> createLiveNode() { final String liveNodePath = getLiveNodePath(); LOG.info("Creating live node {}{}", zkClient.getConnectString(), liveNodePath); return ZKOperations.createDeleteIfExists(zkClient, liveNodePath, serializeLiveNode(), CreateMode.EPHEMERAL, true); }
/** * Deletes the {@link ResourceRequirement} for the given resource. * * @param resourceName Name of the resource. * @return A {@link ListenableFuture} that will be completed when the requirement is successfully removed. * If the requirement doesn't exists, the deletion would still be treated as successful. */ public ListenableFuture<String> deleteRequirement(String resourceName) { String zkPath = CoordinationConstants.REQUIREMENTS_PATH + "/" + resourceName; return Futures.transform( ZKOperations.ignoreError(zkClient.delete(zkPath), KeeperException.NoNodeException.class, resourceName), Functions.constant(resourceName) ); }