Search in sources :

Example 6 with Service

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

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

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();
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) AbstractIdleService(com.google.common.util.concurrent.AbstractIdleService) Service(com.google.common.util.concurrent.Service) Test(org.junit.Test)

Example 9 with Service

use of com.google.common.util.concurrent.Service in project weave by continuuity.

the class ServicesTest method testCompletion.

@Test
public void testCompletion() throws ExecutionException, InterruptedException {
    Service service = new DummyService("s1", new AtomicBoolean());
    ListenableFuture<Service.State> completion = Services.getCompletionFuture(service);
    service.start();
    service.stop();
    completion.get();
    AtomicBoolean transiting = new AtomicBoolean();
    service = new DummyService("s2", transiting);
    completion = Services.getCompletionFuture(service);
    service.startAndWait();
    transiting.set(true);
    service.stop();
    try {
        completion.get();
        Assert.assertTrue(false);
    } catch (ExecutionException e) {
    // Expected
    }
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) AbstractIdleService(com.google.common.util.concurrent.AbstractIdleService) Service(com.google.common.util.concurrent.Service) ExecutionException(java.util.concurrent.ExecutionException) Test(org.junit.Test)

Example 10 with Service

use of com.google.common.util.concurrent.Service in project cdap by caskdata.

the class ReferenceCountedSupplierTests method testGetSupplier.

/**
   * Starts a few threads to perform get operation on the given {@link CacheSupplier}s. It verifies that subsequent
   * 'get' operation returns the same instance as it did during the first invocation. And it also verifies that the
   * service is running.
   *
   * @param supplierList list of {@link CacheSupplier}
   * @throws Exception if an error occurs during testing
   */
private void testGetSupplier(final List<CacheSupplier> supplierList) throws Exception {
    // Get one instance now, for later comparisons
    final List<Service> serviceList = new ArrayList<>();
    for (CacheSupplier supplier : supplierList) {
        serviceList.add(supplier.get());
    }
    final AtomicInteger numOps = new AtomicInteger(NUM_OPS);
    final Random random = new Random(System.currentTimeMillis());
    // Start threads that will 'get' DummyService
    ExecutorService executor = Executors.newFixedThreadPool(NUM_THREADS);
    List<Future> futureList = new ArrayList<>();
    for (int i = 0; i < NUM_THREADS; i++) {
        futureList.add(executor.submit(new Runnable() {

            @Override
            public void run() {
                // Perform NUM_OPS 'gets' of DummyService
                while (numOps.decrementAndGet() > 0) {
                    for (int i = 0; i < supplierList.size(); i++) {
                        CacheSupplier supplier = supplierList.get(i);
                        Service newService = supplier.get();
                        Assert.assertTrue(newService == serviceList.get(i));
                        Assert.assertTrue(newService.isRunning());
                    }
                    int waitTime = random.nextInt(10);
                    try {
                        TimeUnit.MICROSECONDS.sleep(waitTime);
                    } catch (InterruptedException e) {
                        LOG.warn("Received an exception.", e);
                    }
                }
            }
        }));
    }
    for (Future future : futureList) {
        future.get(5, TimeUnit.SECONDS);
    }
    executor.shutdown();
    executor.awaitTermination(2, TimeUnit.SECONDS);
}
Also used : Random(java.util.Random) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ArrayList(java.util.ArrayList) ExecutorService(java.util.concurrent.ExecutorService) Service(com.google.common.util.concurrent.Service) ExecutorService(java.util.concurrent.ExecutorService) CacheSupplier(co.cask.cdap.data2.transaction.coprocessor.CacheSupplier) TopicMetadataCacheSupplier(co.cask.cdap.messaging.TopicMetadataCacheSupplier) CConfigurationCacheSupplier(co.cask.cdap.data2.transaction.coprocessor.CConfigurationCacheSupplier) Future(java.util.concurrent.Future)

Aggregations

Service (com.google.common.util.concurrent.Service)43 MessagingService (co.cask.cdap.messaging.MessagingService)15 AbstractIdleService (com.google.common.util.concurrent.AbstractIdleService)15 MetricsCollectionService (co.cask.cdap.api.metrics.MetricsCollectionService)14 Test (org.junit.Test)14 IOException (java.io.IOException)8 CountDownLatch (java.util.concurrent.CountDownLatch)8 ProgramId (co.cask.cdap.proto.id.ProgramId)7 ProgramContextAware (co.cask.cdap.data.ProgramContextAware)6 DatasetService (co.cask.cdap.data2.datafabric.dataset.service.DatasetService)6 BasicProgramContext (co.cask.cdap.internal.app.runtime.BasicProgramContext)6 ProgramType (co.cask.cdap.proto.ProgramType)6 RunId (org.apache.twill.api.RunId)6 ApplicationSpecification (co.cask.cdap.api.app.ApplicationSpecification)5 CConfiguration (co.cask.cdap.common.conf.CConfiguration)5 Injector (com.google.inject.Injector)5 PluginInstantiator (co.cask.cdap.internal.app.runtime.plugin.PluginInstantiator)4 RunId (com.continuuity.weave.api.RunId)4 ZKClientService (com.continuuity.weave.zookeeper.ZKClientService)4 Configuration (org.apache.hadoop.conf.Configuration)4