Search in sources :

Example 6 with InMemoryZKServer

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

use of com.continuuity.weave.internal.zookeeper.InMemoryZKServer 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)

Example 8 with InMemoryZKServer

use of com.continuuity.weave.internal.zookeeper.InMemoryZKServer in project weave by continuuity.

the class ZKServiceDecoratorTest method testStateTransition.

@Test
public void testStateTransition() throws InterruptedException, ExecutionException, TimeoutException {
    InMemoryZKServer zkServer = InMemoryZKServer.builder().build();
    zkServer.startAndWait();
    try {
        final String namespace = Joiner.on('/').join("/weave", RunIds.generate(), "runnables", "Runner1");
        final ZKClientService zkClient = ZKClientService.Builder.of(zkServer.getConnectionStr()).build();
        zkClient.startAndWait();
        zkClient.create(namespace, null, CreateMode.PERSISTENT).get();
        try {
            JsonObject content = new JsonObject();
            content.addProperty("containerId", "container-123");
            content.addProperty("host", "localhost");
            RunId runId = RunIds.generate();
            final Semaphore semaphore = new Semaphore(0);
            ZKServiceDecorator service = new ZKServiceDecorator(ZKClients.namespace(zkClient, namespace), runId, Suppliers.ofInstance(content), new AbstractIdleService() {

                @Override
                protected void startUp() throws Exception {
                    Preconditions.checkArgument(semaphore.tryAcquire(5, TimeUnit.SECONDS), "Fail to start");
                }

                @Override
                protected void shutDown() throws Exception {
                    Preconditions.checkArgument(semaphore.tryAcquire(5, TimeUnit.SECONDS), "Fail to stop");
                }
            });
            final String runnablePath = namespace + "/" + runId.getId();
            final AtomicReference<String> stateMatch = new AtomicReference<String>("STARTING");
            watchDataChange(zkClient, runnablePath + "/state", semaphore, stateMatch);
            Assert.assertEquals(Service.State.RUNNING, service.start().get(5, TimeUnit.SECONDS));
            stateMatch.set("STOPPING");
            Assert.assertEquals(Service.State.TERMINATED, service.stop().get(5, TimeUnit.SECONDS));
        } finally {
            zkClient.stopAndWait();
        }
    } finally {
        zkServer.stopAndWait();
    }
}
Also used : ZKServiceDecorator(com.continuuity.weave.internal.ZKServiceDecorator) ZKClientService(com.continuuity.weave.zookeeper.ZKClientService) JsonObject(com.google.gson.JsonObject) AtomicReference(java.util.concurrent.atomic.AtomicReference) Semaphore(java.util.concurrent.Semaphore) AbstractIdleService(com.google.common.util.concurrent.AbstractIdleService) RunId(com.continuuity.weave.api.RunId) TimeoutException(java.util.concurrent.TimeoutException) ExecutionException(java.util.concurrent.ExecutionException) InMemoryZKServer(com.continuuity.weave.internal.zookeeper.InMemoryZKServer) Test(org.junit.Test)

Example 9 with InMemoryZKServer

use of com.continuuity.weave.internal.zookeeper.InMemoryZKServer in project weave by continuuity.

the class ZKClientTest method testChroot.

@Test
public void testChroot() throws Exception {
    InMemoryZKServer zkServer = InMemoryZKServer.builder().setTickTime(1000).build();
    zkServer.startAndWait();
    try {
        ZKClientService client = ZKClientService.Builder.of(zkServer.getConnectionStr() + "/chroot").build();
        client.startAndWait();
        try {
            List<OperationFuture<String>> futures = Lists.newArrayList();
            futures.add(client.create("/test1/test2", null, CreateMode.PERSISTENT));
            futures.add(client.create("/test1/test3", null, CreateMode.PERSISTENT));
            Futures.successfulAsList(futures).get();
            Assert.assertNotNull(client.exists("/test1/test2").get());
            Assert.assertNotNull(client.exists("/test1/test3").get());
        } finally {
            client.stopAndWait();
        }
    } finally {
        zkServer.stopAndWait();
    }
}
Also used : InMemoryZKServer(com.continuuity.weave.internal.zookeeper.InMemoryZKServer) Test(org.junit.Test)

Example 10 with InMemoryZKServer

use of com.continuuity.weave.internal.zookeeper.InMemoryZKServer in project weave by continuuity.

the class ZKClientTest method testRetry.

@Test
public void testRetry() throws ExecutionException, InterruptedException, TimeoutException {
    File dataDir = Files.createTempDir();
    InMemoryZKServer zkServer = InMemoryZKServer.builder().setDataDir(dataDir).setTickTime(1000).build();
    zkServer.startAndWait();
    int port = zkServer.getLocalAddress().getPort();
    final CountDownLatch disconnectLatch = new CountDownLatch(1);
    ZKClientService client = ZKClientServices.delegate(ZKClients.retryOnFailure(ZKClientService.Builder.of(zkServer.getConnectionStr()).setConnectionWatcher(new Watcher() {

        @Override
        public void process(WatchedEvent event) {
            if (event.getState() == Event.KeeperState.Disconnected) {
                disconnectLatch.countDown();
            }
        }
    }).build(), RetryStrategies.fixDelay(0, TimeUnit.SECONDS)));
    client.startAndWait();
    zkServer.stopAndWait();
    Assert.assertTrue(disconnectLatch.await(1, TimeUnit.SECONDS));
    final CountDownLatch createLatch = new CountDownLatch(1);
    Futures.addCallback(client.create("/testretry/test", null, CreateMode.PERSISTENT), new FutureCallback<String>() {

        @Override
        public void onSuccess(String result) {
            createLatch.countDown();
        }

        @Override
        public void onFailure(Throwable t) {
            t.printStackTrace(System.out);
        }
    });
    TimeUnit.SECONDS.sleep(2);
    zkServer = InMemoryZKServer.builder().setDataDir(dataDir).setAutoCleanDataDir(true).setPort(port).setTickTime(1000).build();
    zkServer.startAndWait();
    try {
        Assert.assertTrue(createLatch.await(5, TimeUnit.SECONDS));
    } finally {
        zkServer.stopAndWait();
    }
}
Also used : WatchedEvent(org.apache.zookeeper.WatchedEvent) Watcher(org.apache.zookeeper.Watcher) CountDownLatch(java.util.concurrent.CountDownLatch) File(java.io.File) InMemoryZKServer(com.continuuity.weave.internal.zookeeper.InMemoryZKServer) Test(org.junit.Test)

Aggregations

InMemoryZKServer (com.continuuity.weave.internal.zookeeper.InMemoryZKServer)11 Test (org.junit.Test)11 CountDownLatch (java.util.concurrent.CountDownLatch)5 RunId (com.continuuity.weave.api.RunId)4 ZKClientService (com.continuuity.weave.zookeeper.ZKClientService)4 AbstractIdleService (com.google.common.util.concurrent.AbstractIdleService)4 WeaveController (com.continuuity.weave.api.WeaveController)3 ServiceListenerAdapter (com.continuuity.weave.common.ServiceListenerAdapter)3 Service (com.google.common.util.concurrent.Service)3 WatchedEvent (org.apache.zookeeper.WatchedEvent)2 Watcher (org.apache.zookeeper.Watcher)2 ZKServiceDecorator (com.continuuity.weave.internal.ZKServiceDecorator)1 JsonObject (com.google.gson.JsonObject)1 File (java.io.File)1 ExecutionException (java.util.concurrent.ExecutionException)1 LinkedBlockingQueue (java.util.concurrent.LinkedBlockingQueue)1 Semaphore (java.util.concurrent.Semaphore)1 TimeoutException (java.util.concurrent.TimeoutException)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1