@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);
@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);
@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 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; } } }
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(); }
@Override public Stat call() throws Exception { return zkClient.exists("/EchoServer").get(); } }, 10000, 100, TimeUnit.MILLISECONDS);
@Override public Stat call() throws Exception { return zkClient.exists("/EchoServer").get(); } }, 10000, 100, TimeUnit.MILLISECONDS);
@Override protected final void shutDown() throws Exception { if (watcherCancellable != null) { watcherCancellable.cancel(); } messageCallbackExecutor.shutdownNow(); try { doStop(); } finally { // Given at most 5 seconds to cleanup ZK nodes removeLiveNode().get(5, TimeUnit.SECONDS); LOG.info("Service {} with runId {} shutdown completed", getServiceName(), runId.getId()); } }
@Override protected final void shutDown() throws Exception { if (watcherCancellable != null) { watcherCancellable.cancel(); } messageCallbackExecutor.shutdownNow(); try { doStop(); } finally { // Given at most 5 seconds to cleanup ZK nodes removeLiveNode().get(5, TimeUnit.SECONDS); LOG.info("Service {} with runId {} shutdown completed", getServiceName(), runId.getId()); } }
@Override protected void doStart() throws Exception { LOG.info("Start application master with spec: {}", TwillRuntimeSpecificationAdapter.create().toJson(twillRuntimeSpec)); // initialize the event handler, if it fails, it will fail the application. eventHandler.initialize(new BasicEventHandlerContext(twillRuntimeSpec)); // call event handler started. eventHandler.started(); instanceChangeExecutor = Executors.newSingleThreadExecutor(Threads.createDaemonThreadFactory("instanceChanger")); // Creates ZK path for runnable. It's ok if the path already exists. // That's for the case when the AM get killed and restarted ZKOperations.ignoreError( zkClient.create("/" + runId.getId() + "/runnables", null, CreateMode.PERSISTENT), KeeperException.NodeExistsException.class, null) .get(); runningContainers.addWatcher(Constants.DISCOVERY_PATH_PREFIX); runnableContainerRequests = initContainerRequests(); }
@Override protected void doStart() throws Exception { LOG.info("Start application master with spec: {}", TwillRuntimeSpecificationAdapter.create().toJson(twillRuntimeSpec)); // initialize the event handler, if it fails, it will fail the application. eventHandler.initialize(new BasicEventHandlerContext(twillRuntimeSpec)); // call event handler started. eventHandler.started(); instanceChangeExecutor = Executors.newSingleThreadExecutor(Threads.createDaemonThreadFactory("instanceChanger")); // Creates ZK path for runnable. It's ok if the path already exists. // That's for the case when the AM get killed and restarted ZKOperations.ignoreError( zkClient.create("/" + runId.getId() + "/runnables", null, CreateMode.PERSISTENT), KeeperException.NodeExistsException.class, null) .get(); runningContainers.addWatcher(Constants.DISCOVERY_PATH_PREFIX); runnableContainerRequests = initContainerRequests(); }
@Test public void testCreateOrSet() throws Exception { String path = "/parent/testCreateOrSet"; ZKClientService zkClient = ZKClientService.Builder.of(zkServer.getConnectionStr()).build(); zkClient.startAndWait(); // Create with "1" Assert.assertEquals(1, ZKExtOperations.createOrSet(zkClient, path, Suppliers.ofInstance(1), INT_CODEC, 0).get().intValue()); // Should get "1" back Assert.assertEquals(1, INT_CODEC.decode(zkClient.getData(path).get().getData()).intValue()); // Set with "2" Assert.assertEquals(2, ZKExtOperations.createOrSet(zkClient, path, Suppliers.ofInstance(2), INT_CODEC, 0).get().intValue()); // Should get "2" back Assert.assertEquals(2, INT_CODEC.decode(zkClient.getData(path).get().getData()).intValue()); zkClient.stopAndWait(); }
@Test public void testSetOrCreate() throws Exception { String path = "/parent/testSetOrCreate"; ZKClientService zkClient = ZKClientService.Builder.of(zkServer.getConnectionStr()).build(); zkClient.startAndWait(); // Create with "1" Assert.assertEquals(1, ZKExtOperations.setOrCreate(zkClient, path, Suppliers.ofInstance(1), INT_CODEC, 0).get().intValue()); // Should get "1" back Assert.assertEquals(1, INT_CODEC.decode(zkClient.getData(path).get().getData()).intValue()); // Set with "2" Assert.assertEquals(2, ZKExtOperations.setOrCreate(zkClient, path, Suppliers.ofInstance(2), INT_CODEC, 0).get().intValue()); // Should get "2" back Assert.assertEquals(2, INT_CODEC.decode(zkClient.getData(path).get().getData()).intValue()); zkClient.stopAndWait(); }