use of com.google.common.util.concurrent.Service in project weave by continuuity.
the class WeaveContainerMain method main.
/**
* Main method for launching a {@link WeaveContainerService} which runs
* a {@link com.continuuity.weave.api.WeaveRunnable}.
*/
public static void main(final String[] args) throws Exception {
// Try to load the secure store from localized file, which AM requested RM to localize it for this container.
loadSecureStore();
String zkConnectStr = System.getenv(EnvKeys.WEAVE_ZK_CONNECT);
File weaveSpecFile = new File(Constants.Files.WEAVE_SPEC);
RunId appRunId = RunIds.fromString(System.getenv(EnvKeys.WEAVE_APP_RUN_ID));
RunId runId = RunIds.fromString(System.getenv(EnvKeys.WEAVE_RUN_ID));
String runnableName = System.getenv(EnvKeys.WEAVE_RUNNABLE_NAME);
int instanceId = Integer.parseInt(System.getenv(EnvKeys.WEAVE_INSTANCE_ID));
int instanceCount = Integer.parseInt(System.getenv(EnvKeys.WEAVE_INSTANCE_COUNT));
ZKClientService zkClientService = ZKClientServices.delegate(ZKClients.reWatchOnExpire(ZKClients.retryOnFailure(ZKClientService.Builder.of(zkConnectStr).build(), RetryStrategies.fixDelay(1, TimeUnit.SECONDS))));
DiscoveryService discoveryService = new ZKDiscoveryService(zkClientService);
WeaveSpecification weaveSpec = loadWeaveSpec(weaveSpecFile);
renameLocalFiles(weaveSpec.getRunnables().get(runnableName));
WeaveRunnableSpecification runnableSpec = weaveSpec.getRunnables().get(runnableName).getRunnableSpecification();
ContainerInfo containerInfo = new EnvContainerInfo();
Arguments arguments = decodeArgs();
BasicWeaveContext context = new BasicWeaveContext(runId, appRunId, containerInfo.getHost(), arguments.getRunnableArguments().get(runnableName).toArray(new String[0]), arguments.getArguments().toArray(new String[0]), runnableSpec, instanceId, discoveryService, instanceCount, containerInfo.getMemoryMB(), containerInfo.getVirtualCores());
Configuration conf = new YarnConfiguration(new HdfsConfiguration(new Configuration()));
Service service = new WeaveContainerService(context, containerInfo, getContainerZKClient(zkClientService, appRunId, runnableName), runId, runnableSpec, getClassLoader(), createAppLocation(conf));
new WeaveContainerMain().doMain(zkClientService, service);
}
use of com.google.common.util.concurrent.Service in project weave by continuuity.
the class ControllerTest method testController.
@Test
public void testController() throws ExecutionException, InterruptedException, TimeoutException {
InMemoryZKServer zkServer = InMemoryZKServer.builder().build();
zkServer.startAndWait();
LOG.info("ZKServer: " + zkServer.getConnectionStr());
try {
RunId runId = RunIds.generate();
ZKClientService zkClientService = ZKClientService.Builder.of(zkServer.getConnectionStr()).build();
zkClientService.startAndWait();
Service service = createService(zkClientService, runId);
service.startAndWait();
WeaveController controller = getController(zkClientService, runId);
controller.sendCommand(Command.Builder.of("test").build()).get(2, TimeUnit.SECONDS);
controller.stop().get(2, TimeUnit.SECONDS);
Assert.assertEquals(ServiceController.State.TERMINATED, controller.state());
final CountDownLatch terminateLatch = new CountDownLatch(1);
service.addListener(new ServiceListenerAdapter() {
@Override
public void terminated(Service.State from) {
terminateLatch.countDown();
}
}, Threads.SAME_THREAD_EXECUTOR);
Assert.assertTrue(service.state() == Service.State.TERMINATED || terminateLatch.await(2, TimeUnit.SECONDS));
zkClientService.stopAndWait();
} finally {
zkServer.stopAndWait();
}
}
use of com.google.common.util.concurrent.Service in project weave by continuuity.
the class ControllerTest method testControllerBefore.
// Test controller created before service starts.
@Test
public void testControllerBefore() throws InterruptedException {
InMemoryZKServer zkServer = InMemoryZKServer.builder().build();
zkServer.startAndWait();
LOG.info("ZKServer: " + zkServer.getConnectionStr());
try {
RunId runId = RunIds.generate();
ZKClientService zkClientService = ZKClientService.Builder.of(zkServer.getConnectionStr()).build();
zkClientService.startAndWait();
final CountDownLatch runLatch = new CountDownLatch(1);
final CountDownLatch stopLatch = new CountDownLatch(1);
WeaveController controller = getController(zkClientService, runId);
controller.addListener(new ServiceListenerAdapter() {
@Override
public void running() {
runLatch.countDown();
}
@Override
public void terminated(Service.State from) {
stopLatch.countDown();
}
}, Threads.SAME_THREAD_EXECUTOR);
Service service = createService(zkClientService, runId);
service.start();
Assert.assertTrue(runLatch.await(2, TimeUnit.SECONDS));
Assert.assertFalse(stopLatch.await(2, TimeUnit.SECONDS));
service.stop();
Assert.assertTrue(stopLatch.await(2, TimeUnit.SECONDS));
} finally {
zkServer.stopAndWait();
}
}
use of com.google.common.util.concurrent.Service in project weave by continuuity.
the class ControllerTest method testControllerListener.
// Test controller listener receive first state change without state transition from service
@Test
public void testControllerListener() throws InterruptedException {
InMemoryZKServer zkServer = InMemoryZKServer.builder().build();
zkServer.startAndWait();
LOG.info("ZKServer: " + zkServer.getConnectionStr());
try {
RunId runId = RunIds.generate();
ZKClientService zkClientService = ZKClientService.Builder.of(zkServer.getConnectionStr()).build();
zkClientService.startAndWait();
Service service = createService(zkClientService, runId);
service.startAndWait();
final CountDownLatch runLatch = new CountDownLatch(1);
WeaveController controller = getController(zkClientService, runId);
controller.addListener(new ServiceListenerAdapter() {
@Override
public void running() {
runLatch.countDown();
}
}, Threads.SAME_THREAD_EXECUTOR);
Assert.assertTrue(runLatch.await(2, TimeUnit.SECONDS));
service.stopAndWait();
zkClientService.stopAndWait();
} finally {
zkServer.stopAndWait();
}
}
use of com.google.common.util.concurrent.Service in project weave by continuuity.
the class ServicesTest method testChain.
@Test
public void testChain() throws ExecutionException, InterruptedException {
AtomicBoolean transiting = new AtomicBoolean(false);
Service s1 = new DummyService("s1", transiting);
Service s2 = new DummyService("s2", transiting);
Service s3 = new DummyService("s3", transiting);
Futures.allAsList(Services.chainStart(s1, s2, s3).get()).get();
Futures.allAsList(Services.chainStop(s3, s2, s1).get()).get();
}
Aggregations