@Override public void run() { try { String relativePath = from.get().substring(namespace.length()); to.set(relativePath.isEmpty() ? "/" : relativePath); } catch (Exception e) { to.setException(e.getCause()); } } }, Threads.SAME_THREAD_EXECUTOR);
final Class<? extends KeeperException> exceptionType, final V errorResult) { final SettableOperationFuture<V> resultFuture = SettableOperationFuture.create(future.getRequestPath(), Threads.SAME_THREAD_EXECUTOR);
private OperationFuture<String> relayPath(final OperationFuture<String> from, final SettableOperationFuture<String> to) { from.addListener(new Runnable() { @Override public void run() { try { String relativePath = from.get().substring(namespace.length()); to.set(relativePath.isEmpty() ? "/" : relativePath); } catch (Exception e) { to.setException(e.getCause()); } } }, Threads.SAME_THREAD_EXECUTOR); return to; } }
@Override public void run() { try { String relativePath = from.get().substring(namespace.length()); to.set(relativePath.isEmpty() ? "/" : relativePath); } catch (Exception e) { to.setException(e.getCause()); } } }, Threads.SAME_THREAD_EXECUTOR);
final Class<? extends KeeperException> exceptionType, final V errorResult) { final SettableOperationFuture<V> resultFuture = SettableOperationFuture.create(future.getRequestPath(), Threads.SAME_THREAD_EXECUTOR);
private OperationFuture<String> relayPath(final OperationFuture<String> from, final SettableOperationFuture<String> to) { from.addListener(new Runnable() { @Override public void run() { try { String relativePath = from.get().substring(namespace.length()); to.set(relativePath.isEmpty() ? "/" : relativePath); } catch (Exception e) { to.setException(e.getCause()); } } }, Threads.SAME_THREAD_EXECUTOR); return to; } }
@Override public void delete(final ProgramId serviceId) throws NotFoundException { OperationFuture<String> future = zkClient.delete(getZKPath(serviceId)); SettableFuture<RouteConfig> oldConfigFuture = routeConfigMap.get(serviceId); try { future.get(ZK_TIMEOUT_SECS, TimeUnit.SECONDS); routeConfigMap.remove(serviceId, oldConfigFuture); } catch (ExecutionException | InterruptedException | TimeoutException ex) { if (ex.getCause() instanceof KeeperException.NoNodeException) { throw new NotFoundException(String.format("Route Config for Service %s was not found.", serviceId)); } throw Throwables.propagate(ex); } }
@Override public void updated(NodeChildren nodeChildren) { LOG.info("Listing existing children for node {}", parentZnode); List<String> children = nodeChildren.getChildren(); for (String child : children) { OperationFuture<NodeData> dataFuture = zookeeper.getData(joinZNode(parentZnode, child), watcher); final String nodeName = getZNode(dataFuture.getRequestPath()); Futures.addCallback(dataFuture, new FutureCallback<NodeData>() { @Override public void onSuccess(NodeData result) { LOG.debug("Got data for child {}", nodeName); try { final T resource = codec.decode(result.getData()); loaded.put(nodeName, resource); listeners.notifyResourceUpdate(nodeName, resource); } catch (IOException ioe) { throw Throwables.propagate(ioe); } } @Override public void onFailure(Throwable t) { LOG.error("Failed to get data for child node {}", nodeName, t); listeners.notifyError(nodeName, t); } }); LOG.debug("Added future for {}", child); } } });
@Override public void delete(final ProgramId serviceId) throws NotFoundException { OperationFuture<String> future = zkClient.delete(getZKPath(serviceId)); SettableFuture<RouteConfig> oldConfigFuture = routeConfigMap.get(serviceId); try { future.get(ZK_TIMEOUT_SECS, TimeUnit.SECONDS); routeConfigMap.remove(serviceId, oldConfigFuture); } catch (ExecutionException | InterruptedException | TimeoutException ex) { if (ex.getCause() instanceof KeeperException.NoNodeException) { throw new NotFoundException(String.format("Route Config for Service %s was not found.", serviceId)); } throw Throwables.propagate(ex); } }
@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 shutDown() throws Exception { LOG.info("Removing container ZK path: {}{}", zkClient.getConnectString(), path); ZKOperations.recursiveDelete(zkClient, path).get(TIMEOUT_SECONDS, TimeUnit.SECONDS); } }
/** * 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; } } }
/** * 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; } } }
/** * Deletes the given ZK path. * * @param path path to delete * @return true if the path was deleted, false if failed to delete due to {@link KeeperException.NotEmptyException}. * @throws Exception if failed to delete the path */ private boolean delete(String path) throws Exception { try { LOG.info("Removing ZK path: {}{}", zkClient.getConnectString(), path); zkClient.delete(path).get(TIMEOUT_SECONDS, TimeUnit.SECONDS); return true; } catch (ExecutionException e) { if (e.getCause() instanceof KeeperException.NotEmptyException) { return false; } if (e.getCause() instanceof KeeperException.NoNodeException) { // If the node to be deleted was not created or is already gone, it is the same as delete successfully. return true; } throw e; } }
/** * Deletes the given ZK path. * * @param path path to delete * @return true if the path was deleted, false if failed to delete due to {@link KeeperException.NotEmptyException}. * @throws Exception if failed to delete the path */ private boolean delete(String path) throws Exception { try { LOG.info("Removing ZK path: {}{}", zkClient.getConnectString(), path); zkClient.delete(path).get(TIMEOUT_SECONDS, TimeUnit.SECONDS); return true; } catch (ExecutionException e) { if (e.getCause() instanceof KeeperException.NotEmptyException) { return false; } if (e.getCause() instanceof KeeperException.NoNodeException) { // If the node to be deleted was not created or is already gone, it is the same as delete successfully. return true; } throw e; } }
public void init() throws InterruptedException { this.watcher = new ZKWatcher(); try { LOG.info("Initializing SharedResourceCache. Checking for parent znode {}", parentZnode); if (zookeeper.exists(parentZnode).get() == null) { // may be created in parallel by another instance // Also the child nodes are secure even without adding any ACLs to parent node. ZKOperations.ignoreError(zookeeper.create(parentZnode, null, CreateMode.PERSISTENT), KeeperException.NodeExistsException.class, null).get(); } } catch (ExecutionException ee) { // recheck if already created throw Throwables.propagate(ee.getCause()); } this.resources = reloadAll(); listeners.notifyUpdate(); }
@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(); }
@Override protected void startUp() throws Exception { LOG.info("Creating container ZK path: {}{}", zkClient.getConnectString(), path); ZKOperations.ignoreError(zkClient.create(path, null, CreateMode.PERSISTENT), KeeperException.NodeExistsException.class, null).get(TIMEOUT_SECONDS, TimeUnit.SECONDS); }
@Override protected void startUp() throws Exception { LOG.info("Creating container ZK path: {}{}", zkClient.getConnectString(), path); ZKOperations.ignoreError(zkClient.create(path, null, CreateMode.PERSISTENT), KeeperException.NodeExistsException.class, null).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(); }