Search in sources :

Example 6 with Cancellable

use of com.continuuity.weave.common.Cancellable in project weave by continuuity.

the class DiscoveryServiceTestBase method multiServiceDiscoverable.

@Test
public void multiServiceDiscoverable() throws Exception {
    Map.Entry<DiscoveryService, DiscoveryServiceClient> entry = create();
    DiscoveryService discoveryService = entry.getKey();
    DiscoveryServiceClient discoveryServiceClient = entry.getValue();
    List<Cancellable> cancellables = Lists.newArrayList();
    cancellables.add(register(discoveryService, "service1", "localhost", 1));
    cancellables.add(register(discoveryService, "service1", "localhost", 2));
    cancellables.add(register(discoveryService, "service1", "localhost", 3));
    cancellables.add(register(discoveryService, "service1", "localhost", 4));
    cancellables.add(register(discoveryService, "service1", "localhost", 5));
    cancellables.add(register(discoveryService, "service2", "localhost", 1));
    cancellables.add(register(discoveryService, "service2", "localhost", 2));
    cancellables.add(register(discoveryService, "service2", "localhost", 3));
    cancellables.add(register(discoveryService, "service3", "localhost", 1));
    cancellables.add(register(discoveryService, "service3", "localhost", 2));
    ServiceDiscovered serviceDiscovered = discoveryServiceClient.discover("service1");
    Assert.assertTrue(waitTillExpected(5, serviceDiscovered));
    serviceDiscovered = discoveryServiceClient.discover("service2");
    Assert.assertTrue(waitTillExpected(3, serviceDiscovered));
    serviceDiscovered = discoveryServiceClient.discover("service3");
    Assert.assertTrue(waitTillExpected(2, serviceDiscovered));
    cancellables.add(register(discoveryService, "service3", "localhost", 3));
    // Shows live iterator.
    Assert.assertTrue(waitTillExpected(3, serviceDiscovered));
    for (Cancellable cancellable : cancellables) {
        cancellable.cancel();
    }
    Assert.assertTrue(waitTillExpected(0, discoveryServiceClient.discover("service1")));
    Assert.assertTrue(waitTillExpected(0, discoveryServiceClient.discover("service2")));
    Assert.assertTrue(waitTillExpected(0, discoveryServiceClient.discover("service3")));
}
Also used : Cancellable(com.continuuity.weave.common.Cancellable) Map(java.util.Map) Test(org.junit.Test)

Example 7 with Cancellable

use of com.continuuity.weave.common.Cancellable in project weave by continuuity.

the class ZKDiscoveryServiceTest method testDoubleRegister.

@Test(timeout = 5000)
public void testDoubleRegister() throws Exception {
    Map.Entry<DiscoveryService, DiscoveryServiceClient> entry = create();
    DiscoveryService discoveryService = entry.getKey();
    DiscoveryServiceClient discoveryServiceClient = entry.getValue();
    // Register on the same host port, it shouldn't fail.
    Cancellable cancellable = register(discoveryService, "test_double_reg", "localhost", 54321);
    Cancellable cancellable2 = register(discoveryService, "test_double_reg", "localhost", 54321);
    ServiceDiscovered discoverables = discoveryServiceClient.discover("test_double_reg");
    Assert.assertTrue(waitTillExpected(1, discoverables));
    cancellable.cancel();
    cancellable2.cancel();
    // Register again with two different clients, but killing session of the first one.
    final ZKClientService zkClient2 = ZKClientServices.delegate(ZKClients.retryOnFailure(ZKClients.reWatchOnExpire(ZKClientService.Builder.of(zkServer.getConnectionStr()).build()), RetryStrategies.fixDelay(1, TimeUnit.SECONDS)));
    zkClient2.startAndWait();
    try {
        DiscoveryService discoveryService2 = new ZKDiscoveryService(zkClient2);
        cancellable2 = register(discoveryService2, "test_multi_client", "localhost", 54321);
        // Schedule a thread to shutdown zkClient2.
        new Thread() {

            @Override
            public void run() {
                try {
                    TimeUnit.SECONDS.sleep(2);
                    zkClient2.stopAndWait();
                } catch (InterruptedException e) {
                    LOG.error(e.getMessage(), e);
                }
            }
        }.start();
        // This call would block until zkClient2 is shutdown.
        cancellable = register(discoveryService, "test_multi_client", "localhost", 54321);
        cancellable.cancel();
    } finally {
        zkClient2.stopAndWait();
    }
}
Also used : ZKClientService(com.continuuity.weave.zookeeper.ZKClientService) Cancellable(com.continuuity.weave.common.Cancellable) Map(java.util.Map) Test(org.junit.Test)

Example 8 with Cancellable

use of com.continuuity.weave.common.Cancellable in project weave by continuuity.

the class RunnableProcessLauncher method doLaunch.

@Override
protected <R> ProcessController<R> doLaunch(YarnLaunchContext launchContext) {
    Map<String, String> env = Maps.newHashMap(launchContext.getEnvironment());
    // Set extra environments
    env.put(EnvKeys.YARN_CONTAINER_ID, containerInfo.getId());
    env.put(EnvKeys.YARN_CONTAINER_HOST, containerInfo.getHost().getHostName());
    env.put(EnvKeys.YARN_CONTAINER_PORT, Integer.toString(containerInfo.getPort()));
    env.put(EnvKeys.YARN_CONTAINER_MEMORY_MB, Integer.toString(containerInfo.getMemoryMB()));
    env.put(EnvKeys.YARN_CONTAINER_VIRTUAL_CORES, Integer.toString(containerInfo.getVirtualCores()));
    launchContext.setEnvironment(env);
    LOG.info("Launching in container {}, {}", containerInfo.getId(), launchContext.getCommands());
    final Cancellable cancellable = nmClient.start(containerInfo, launchContext);
    launched = true;
    return new ProcessController<R>() {

        @Override
        public R getReport() {
            // No reporting support for runnable launch yet.
            return null;
        }

        @Override
        public void cancel() {
            cancellable.cancel();
        }
    };
}
Also used : Cancellable(com.continuuity.weave.common.Cancellable) ProcessController(com.continuuity.weave.internal.ProcessController)

Example 9 with Cancellable

use of com.continuuity.weave.common.Cancellable in project weave by continuuity.

the class ZKDiscoveryServiceTest method testSessionExpires.

@Test
public void testSessionExpires() throws Exception {
    Map.Entry<DiscoveryService, DiscoveryServiceClient> entry = create();
    DiscoveryService discoveryService = entry.getKey();
    DiscoveryServiceClient discoveryServiceClient = entry.getValue();
    Cancellable cancellable = register(discoveryService, "test_expires", "localhost", 54321);
    ServiceDiscovered discoverables = discoveryServiceClient.discover("test_expires");
    // Discover that registered host:port.
    Assert.assertTrue(waitTillExpected(1, discoverables));
    KillZKSession.kill(zkClient.getZooKeeperSupplier().get(), zkServer.getConnectionStr(), 5000);
    // Register one more endpoint to make sure state has been reflected after reconnection
    Cancellable cancellable2 = register(discoveryService, "test_expires", "localhost", 54322);
    // Reconnection would trigger re-registration.
    Assert.assertTrue(waitTillExpected(2, discoverables));
    cancellable.cancel();
    cancellable2.cancel();
    // Verify that both are now gone.
    Assert.assertTrue(waitTillExpected(0, discoverables));
}
Also used : Cancellable(com.continuuity.weave.common.Cancellable) Map(java.util.Map) Test(org.junit.Test)

Example 10 with Cancellable

use of com.continuuity.weave.common.Cancellable in project weave by continuuity.

the class YarnWeaveRunnerService method watchLiveApps.

private Cancellable watchLiveApps() {
    final Map<String, Cancellable> watched = Maps.newConcurrentMap();
    final AtomicBoolean cancelled = new AtomicBoolean(false);
    // Watch child changes in the root, which gives all application names.
    final Cancellable cancellable = ZKOperations.watchChildren(zkClientService, "/", new ZKOperations.ChildrenCallback() {

        @Override
        public void updated(NodeChildren nodeChildren) {
            if (cancelled.get()) {
                return;
            }
            Set<String> apps = ImmutableSet.copyOf(nodeChildren.getChildren());
            // For each for the application name, watch for ephemeral nodes under /instances.
            for (final String appName : apps) {
                if (watched.containsKey(appName)) {
                    continue;
                }
                final String instancePath = String.format("/%s/instances", appName);
                watched.put(appName, ZKOperations.watchChildren(zkClientService, instancePath, new ZKOperations.ChildrenCallback() {

                    @Override
                    public void updated(NodeChildren nodeChildren) {
                        if (cancelled.get()) {
                            return;
                        }
                        if (nodeChildren.getChildren().isEmpty()) {
                            // No more child, means no live instances
                            Cancellable removed = watched.remove(appName);
                            if (removed != null) {
                                removed.cancel();
                            }
                            return;
                        }
                        synchronized (YarnWeaveRunnerService.this) {
                            // fetch the application Id and construct WeaveController.
                            for (final RunId runId : Iterables.transform(nodeChildren.getChildren(), STRING_TO_RUN_ID)) {
                                if (controllers.contains(appName, runId)) {
                                    continue;
                                }
                                updateController(appName, runId, cancelled);
                            }
                        }
                    }
                }));
            }
            // in the state listener attached to the weave controller.
            for (String removeApp : Sets.difference(watched.keySet(), apps)) {
                watched.remove(removeApp).cancel();
            }
        }
    });
    return new Cancellable() {

        @Override
        public void cancel() {
            cancelled.set(true);
            cancellable.cancel();
            for (Cancellable c : watched.values()) {
                c.cancel();
            }
        }
    };
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ImmutableSet(com.google.common.collect.ImmutableSet) Set(java.util.Set) Cancellable(com.continuuity.weave.common.Cancellable) ZKOperations(com.continuuity.weave.zookeeper.ZKOperations) RunId(com.continuuity.weave.api.RunId) NodeChildren(com.continuuity.weave.zookeeper.NodeChildren)

Aggregations

Cancellable (com.continuuity.weave.common.Cancellable)11 Map (java.util.Map)6 Test (org.junit.Test)6 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)3 SettableOperationFuture (com.continuuity.weave.internal.zookeeper.SettableOperationFuture)2 ImmutableList (com.google.common.collect.ImmutableList)2 List (java.util.List)2 ArrayBlockingQueue (java.util.concurrent.ArrayBlockingQueue)2 Watcher (org.apache.zookeeper.Watcher)2 RunId (com.continuuity.weave.api.RunId)1 ProcessController (com.continuuity.weave.internal.ProcessController)1 NodeChildren (com.continuuity.weave.zookeeper.NodeChildren)1 ZKClientService (com.continuuity.weave.zookeeper.ZKClientService)1 ZKOperations (com.continuuity.weave.zookeeper.ZKOperations)1 ImmutableSet (com.google.common.collect.ImmutableSet)1 IOException (java.io.IOException)1 InetSocketAddress (java.net.InetSocketAddress)1 ServerSocket (java.net.ServerSocket)1 Set (java.util.Set)1 Executor (java.util.concurrent.Executor)1