Search in sources :

Example 1 with ZKClientService

use of com.continuuity.weave.zookeeper.ZKClientService 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);
}
Also used : YarnConfiguration(org.apache.hadoop.yarn.conf.YarnConfiguration) Configuration(org.apache.hadoop.conf.Configuration) HdfsConfiguration(org.apache.hadoop.hdfs.HdfsConfiguration) EnvContainerInfo(com.continuuity.weave.internal.EnvContainerInfo) Arguments(com.continuuity.weave.internal.Arguments) DiscoveryService(com.continuuity.weave.discovery.DiscoveryService) ZKDiscoveryService(com.continuuity.weave.discovery.ZKDiscoveryService) ZKClientService(com.continuuity.weave.zookeeper.ZKClientService) Service(com.google.common.util.concurrent.Service) WeaveSpecification(com.continuuity.weave.api.WeaveSpecification) HdfsConfiguration(org.apache.hadoop.hdfs.HdfsConfiguration) WeaveRunnableSpecification(com.continuuity.weave.api.WeaveRunnableSpecification) ZKDiscoveryService(com.continuuity.weave.discovery.ZKDiscoveryService) ZKClientService(com.continuuity.weave.zookeeper.ZKClientService) YarnConfiguration(org.apache.hadoop.yarn.conf.YarnConfiguration) EnvContainerInfo(com.continuuity.weave.internal.EnvContainerInfo) ContainerInfo(com.continuuity.weave.internal.ContainerInfo) BasicWeaveContext(com.continuuity.weave.internal.BasicWeaveContext) RunId(com.continuuity.weave.api.RunId) DiscoveryService(com.continuuity.weave.discovery.DiscoveryService) ZKDiscoveryService(com.continuuity.weave.discovery.ZKDiscoveryService) LocalFile(com.continuuity.weave.api.LocalFile) File(java.io.File)

Example 2 with ZKClientService

use of com.continuuity.weave.zookeeper.ZKClientService 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 3 with ZKClientService

use of com.continuuity.weave.zookeeper.ZKClientService 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();
    }
}
Also used : ZKClientService(com.continuuity.weave.zookeeper.ZKClientService) AbstractIdleService(com.google.common.util.concurrent.AbstractIdleService) ZKClientService(com.continuuity.weave.zookeeper.ZKClientService) Service(com.google.common.util.concurrent.Service) WeaveController(com.continuuity.weave.api.WeaveController) ServiceListenerAdapter(com.continuuity.weave.common.ServiceListenerAdapter) RunId(com.continuuity.weave.api.RunId) CountDownLatch(java.util.concurrent.CountDownLatch) InMemoryZKServer(com.continuuity.weave.internal.zookeeper.InMemoryZKServer) Test(org.junit.Test)

Example 4 with ZKClientService

use of com.continuuity.weave.zookeeper.ZKClientService 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();
    }
}
Also used : ZKClientService(com.continuuity.weave.zookeeper.ZKClientService) AbstractIdleService(com.google.common.util.concurrent.AbstractIdleService) ZKClientService(com.continuuity.weave.zookeeper.ZKClientService) Service(com.google.common.util.concurrent.Service) WeaveController(com.continuuity.weave.api.WeaveController) ServiceListenerAdapter(com.continuuity.weave.common.ServiceListenerAdapter) RunId(com.continuuity.weave.api.RunId) CountDownLatch(java.util.concurrent.CountDownLatch) InMemoryZKServer(com.continuuity.weave.internal.zookeeper.InMemoryZKServer) Test(org.junit.Test)

Example 5 with ZKClientService

use of com.continuuity.weave.zookeeper.ZKClientService 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();
    }
}
Also used : ZKClientService(com.continuuity.weave.zookeeper.ZKClientService) AbstractIdleService(com.google.common.util.concurrent.AbstractIdleService) ZKClientService(com.continuuity.weave.zookeeper.ZKClientService) Service(com.google.common.util.concurrent.Service) WeaveController(com.continuuity.weave.api.WeaveController) ServiceListenerAdapter(com.continuuity.weave.common.ServiceListenerAdapter) RunId(com.continuuity.weave.api.RunId) CountDownLatch(java.util.concurrent.CountDownLatch) InMemoryZKServer(com.continuuity.weave.internal.zookeeper.InMemoryZKServer) Test(org.junit.Test)

Aggregations

ZKClientService (com.continuuity.weave.zookeeper.ZKClientService)7 RunId (com.continuuity.weave.api.RunId)6 Service (com.google.common.util.concurrent.Service)5 Test (org.junit.Test)5 InMemoryZKServer (com.continuuity.weave.internal.zookeeper.InMemoryZKServer)4 AbstractIdleService (com.google.common.util.concurrent.AbstractIdleService)4 WeaveController (com.continuuity.weave.api.WeaveController)3 ServiceListenerAdapter (com.continuuity.weave.common.ServiceListenerAdapter)3 CountDownLatch (java.util.concurrent.CountDownLatch)3 File (java.io.File)2 Configuration (org.apache.hadoop.conf.Configuration)2 HdfsConfiguration (org.apache.hadoop.hdfs.HdfsConfiguration)2 YarnConfiguration (org.apache.hadoop.yarn.conf.YarnConfiguration)2 LocalFile (com.continuuity.weave.api.LocalFile)1 WeaveRunnableSpecification (com.continuuity.weave.api.WeaveRunnableSpecification)1 WeaveSpecification (com.continuuity.weave.api.WeaveSpecification)1 Cancellable (com.continuuity.weave.common.Cancellable)1 DiscoveryService (com.continuuity.weave.discovery.DiscoveryService)1 ZKDiscoveryService (com.continuuity.weave.discovery.ZKDiscoveryService)1 Arguments (com.continuuity.weave.internal.Arguments)1