Search in sources :

Example 21 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)

Example 22 with Service

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

the class ReferenceCountedSupplierTests method testReleaseSupplier.

/**
   * Starts a few threads to perform release operation on the given {@link CacheSupplier}s. It verifies that the
   * services are still running until the last release operation is performed after which they are checked to see
   * if the services have stopped.
   *
   * @param supplierList list of {@link CacheSupplier}
   * @throws Exception if an error occurs during testing
   */
private void testReleaseSupplier(final List<CacheSupplier> supplierList) throws Exception {
    final AtomicInteger numOps = new AtomicInteger(NUM_OPS);
    final Random random = new Random(System.currentTimeMillis());
    final List<Service> serviceList = new ArrayList<>();
    for (CacheSupplier supplier : supplierList) {
        serviceList.add(supplier.get());
        supplier.release();
    }
    // Start threads that will 'release' DummyService
    ExecutorService executor = Executors.newFixedThreadPool(NUM_THREADS);
    List<Future> futureList = new ArrayList<>();
    numOps.set(NUM_OPS);
    for (int i = 0; i < NUM_THREADS; i++) {
        futureList.add(executor.submit(new Runnable() {

            @Override
            public void run() {
                // DummyService
                while (numOps.decrementAndGet() > 0) {
                    for (int i = 0; i < supplierList.size(); i++) {
                        CacheSupplier supplier = supplierList.get(i);
                        supplier.release();
                        Assert.assertTrue(serviceList.get(i).isRunning());
                    }
                    try {
                        TimeUnit.MICROSECONDS.sleep(random.nextInt(10));
                    } catch (InterruptedException e) {
                        LOG.warn("Received an exception.", e);
                    }
                }
            }
        }));
    }
    for (Future future : futureList) {
        future.get(1, TimeUnit.SECONDS);
    }
    executor.shutdown();
    executor.awaitTermination(2, TimeUnit.SECONDS);
    // Verify that the DummyService is still running.
    for (Service service : serviceList) {
        Assert.assertTrue(service.isRunning());
    }
    // Since we got one instance in the beginning, we need to release it
    for (CacheSupplier supplier : supplierList) {
        supplier.release();
    }
    // Verify that the DummyService is shutdown.
    for (Service service : serviceList) {
        Assert.assertFalse(service.isRunning());
    }
    // Release it again but it should not cause any problem and the service should still be stopped.
    for (CacheSupplier supplier : supplierList) {
        supplier.release();
    }
    // Verify that the DummyService is shutdown.
    for (Service service : serviceList) {
        Assert.assertFalse(service.isRunning());
    }
}
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)

Example 23 with Service

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

the class ApplicationMasterMain method main.

/**
   * Starts the application master.
   */
public static void main(String[] args) throws Exception {
    String zkConnect = System.getenv(EnvKeys.WEAVE_ZK_CONNECT);
    File weaveSpec = new File(Constants.Files.WEAVE_SPEC);
    RunId runId = RunIds.fromString(System.getenv(EnvKeys.WEAVE_RUN_ID));
    ZKClientService zkClientService = ZKClientServices.delegate(ZKClients.reWatchOnExpire(ZKClients.retryOnFailure(ZKClientService.Builder.of(zkConnect).build(), RetryStrategies.fixDelay(1, TimeUnit.SECONDS))));
    Configuration conf = new YarnConfiguration(new HdfsConfiguration(new Configuration()));
    Service service = new ApplicationMasterService(runId, zkClientService, weaveSpec, new VersionDetectYarnAMClientFactory(conf), createAppLocation(conf));
    new ApplicationMasterMain(String.format("%s/%s/kafka", zkConnect, runId.getId())).doMain(zkClientService, service);
}
Also used : ZKClientService(com.continuuity.weave.zookeeper.ZKClientService) YarnConfiguration(org.apache.hadoop.yarn.conf.YarnConfiguration) HdfsConfiguration(org.apache.hadoop.hdfs.HdfsConfiguration) Configuration(org.apache.hadoop.conf.Configuration) YarnConfiguration(org.apache.hadoop.yarn.conf.YarnConfiguration) ZKClientService(com.continuuity.weave.zookeeper.ZKClientService) Service(com.google.common.util.concurrent.Service) RunId(com.continuuity.weave.api.RunId) HdfsConfiguration(org.apache.hadoop.hdfs.HdfsConfiguration) File(java.io.File) VersionDetectYarnAMClientFactory(com.continuuity.weave.internal.yarn.VersionDetectYarnAMClientFactory)

Example 24 with Service

use of com.google.common.util.concurrent.Service in project helios by spotify.

the class SystemTestBase method baseTeardown.

@After
public void baseTeardown() throws Exception {
    for (final HeliosClient client : clients) {
        client.close();
    }
    clients.clear();
    for (final Service service : services) {
        try {
            service.stopAsync();
        } catch (Exception e) {
            log.error("Uncaught exception", e);
        }
    }
    for (final Service service : services) {
        try {
            service.awaitTerminated();
        } catch (Exception e) {
            log.error("Service failed", e);
        }
    }
    services.clear();
    // Clean up docker
    try (final DockerClient dockerClient = getNewDockerClient()) {
        final List<Container> containers = dockerClient.listContainers();
        for (final Container container : containers) {
            for (final String name : container.names()) {
                if (name.contains(testTag)) {
                    try {
                        dockerClient.killContainer(container.id());
                    } catch (DockerException e) {
                        e.printStackTrace();
                    }
                    break;
                }
            }
        }
    } catch (Exception e) {
        log.error("Docker client exception", e);
    }
    if (zk != null) {
        zk.close();
    }
    listThreads();
}
Also used : DockerException(com.spotify.docker.client.exceptions.DockerException) Container(com.spotify.docker.client.messages.Container) DockerClient(com.spotify.docker.client.DockerClient) DefaultDockerClient(com.spotify.docker.client.DefaultDockerClient) Service(com.google.common.util.concurrent.Service) Matchers.containsString(org.hamcrest.Matchers.containsString) Integer.toHexString(java.lang.Integer.toHexString) HeliosClient(com.spotify.helios.client.HeliosClient) DockerRequestException(com.spotify.docker.client.exceptions.DockerRequestException) DockerException(com.spotify.docker.client.exceptions.DockerException) ContainerNotFoundException(com.spotify.docker.client.exceptions.ContainerNotFoundException) ImageNotFoundException(com.spotify.docker.client.exceptions.ImageNotFoundException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) TimeoutException(java.util.concurrent.TimeoutException) ExpectedException(org.junit.rules.ExpectedException) After(org.junit.After)

Example 25 with Service

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

the class StampedeBootstrapTest method testCreateStampedeService_run.

@Test
@Ignore
public void testCreateStampedeService_run() {
    Service stampedeService = StampedeBootstrap.createStampedeService(new TestBootstrapModule(config, Clock.systemUTC()));
    stampedeService.startAsync();
    stampedeService.awaitRunning();
    stampedeService.stopAsync();
    stampedeService.awaitTerminated();
}
Also used : Service(com.google.common.util.concurrent.Service) Ignore(org.junit.Ignore) Test(org.junit.Test)

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