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")));
}
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();
}
}
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();
}
};
}
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));
}
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();
}
}
};
}
Aggregations