Search in sources :

Example 36 with Service

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

the class RetryOnStartFailureServiceTest method testRetrySucceed.

@Test
public void testRetrySucceed() throws InterruptedException {
    CountDownLatch startLatch = new CountDownLatch(1);
    Service service = new RetryOnStartFailureService(createServiceSupplier(3, startLatch, new CountDownLatch(1), false), RetryStrategies.fixDelay(10, TimeUnit.MILLISECONDS));
    service.startAndWait();
    Assert.assertTrue(startLatch.await(1, TimeUnit.SECONDS));
}
Also used : Service(com.google.common.util.concurrent.Service) AbstractIdleService(com.google.common.util.concurrent.AbstractIdleService) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 37 with Service

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

the class RetryOnStartFailureServiceTest method testStopWhileRetrying.

@Test
public void testStopWhileRetrying() throws InterruptedException {
    // This test the service can be stopped during failure retry
    CountDownLatch failureLatch = new CountDownLatch(1);
    Service service = new RetryOnStartFailureService(createServiceSupplier(1000, new CountDownLatch(1), failureLatch, false), RetryStrategies.fixDelay(10, TimeUnit.MILLISECONDS));
    service.startAndWait();
    Assert.assertTrue(failureLatch.await(1, TimeUnit.SECONDS));
    service.stopAndWait();
}
Also used : Service(com.google.common.util.concurrent.Service) AbstractIdleService(com.google.common.util.concurrent.AbstractIdleService) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 38 with Service

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

the class RetryOnStartFailureServiceTest method testLoggingContext.

@Test
public void testLoggingContext() {
    // This test logging context set before the service started is propagated into the service
    final Map<String, String> context = Collections.singletonMap("key", "value");
    // Create the service before setting the context.
    Service service = new RetryOnStartFailureService(new Supplier<Service>() {

        @Override
        public Service get() {
            return new AbstractIdleService() {

                @Override
                protected void startUp() throws Exception {
                    Assert.assertEquals(context, MDC.getCopyOfContextMap());
                }

                @Override
                protected void shutDown() throws Exception {
                    Assert.assertEquals(context, MDC.getCopyOfContextMap());
                }
            };
        }
    }, RetryStrategies.noRetry());
    // Set the MDC context
    MDC.setContextMap(context);
    // Start and stop shouldn't throw
    service.startAndWait();
    service.stopAndWait();
}
Also used : Service(com.google.common.util.concurrent.Service) AbstractIdleService(com.google.common.util.concurrent.AbstractIdleService) AbstractIdleService(com.google.common.util.concurrent.AbstractIdleService) TimeoutException(java.util.concurrent.TimeoutException) ExecutionException(java.util.concurrent.ExecutionException) Test(org.junit.Test)

Example 39 with Service

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

the class AbstractProgramRuntimeServiceTest method createProgramRunnerFactory.

/**
   * Creates a {@link ProgramRunnerFactory} for creating {@link ProgramRunner}
   * that always run with a {@link FastService}.
   *
   * @param argumentsMap the map to be populated with the user arguments for each run.
   */
private ProgramRunnerFactory createProgramRunnerFactory(final Map<ProgramId, Arguments> argumentsMap) {
    return new ProgramRunnerFactory() {

        @Override
        public ProgramRunner create(ProgramType programType) {
            return new ProgramRunner() {

                @Override
                public ProgramController run(Program program, ProgramOptions options) {
                    argumentsMap.put(program.getId(), options.getUserArguments());
                    Service service = new FastService();
                    ProgramController controller = new ProgramControllerServiceAdapter(service, program.getId(), RunIds.generate());
                    service.start();
                    return controller;
                }
            };
        }
    };
}
Also used : Program(co.cask.cdap.app.program.Program) ProgramControllerServiceAdapter(co.cask.cdap.internal.app.runtime.ProgramControllerServiceAdapter) AbstractExecutionThreadService(com.google.common.util.concurrent.AbstractExecutionThreadService) AbstractIdleService(com.google.common.util.concurrent.AbstractIdleService) Service(com.google.common.util.concurrent.Service) ProgramType(co.cask.cdap.proto.ProgramType) SimpleProgramOptions(co.cask.cdap.internal.app.runtime.SimpleProgramOptions)

Example 40 with Service

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

the class ProgramControllerTest method testInitState.

@Test
public void testInitState() throws ExecutionException, InterruptedException {
    // To test against race-condition, we start/stop service multiple times.
    // If there is race, there is a chance that this test will fail in some env.
    // Otherwise it should always pass
    ExecutorService executor = Executors.newCachedThreadPool();
    int serviceCount = 1000;
    final CountDownLatch latch = new CountDownLatch(serviceCount);
    ProgramId programId = new ApplicationId(NamespaceId.DEFAULT.getNamespace(), "test").service("test");
    for (int i = 0; i < serviceCount; i++) {
        // Creates a controller for a guava service do nothing in start/stop.
        // The short time in start creates a chance to have out-of-order init() and alive() call if there is a race.
        Service service = new TestService(0, 0);
        ProgramController controller = new ProgramControllerServiceAdapter(service, programId, RunIds.generate());
        ListenableFuture<Service.State> startCompletion = service.start();
        controller.addListener(new AbstractListener() {

            private volatile boolean initCalled;

            @Override
            public void init(ProgramController.State currentState, @Nullable Throwable cause) {
                initCalled = true;
                if (currentState == ProgramController.State.ALIVE) {
                    latch.countDown();
                }
            }

            @Override
            public void alive() {
                if (initCalled) {
                    latch.countDown();
                } else {
                    LOG.error("init() not called before alive()");
                }
            }
        }, executor);
        startCompletion.get();
        service.stopAndWait();
    }
    Assert.assertTrue(latch.await(5, TimeUnit.SECONDS));
}
Also used : Service(com.google.common.util.concurrent.Service) AbstractIdleService(com.google.common.util.concurrent.AbstractIdleService) ExecutorService(java.util.concurrent.ExecutorService) CountDownLatch(java.util.concurrent.CountDownLatch) ProgramId(co.cask.cdap.proto.id.ProgramId) ProgramControllerServiceAdapter(co.cask.cdap.internal.app.runtime.ProgramControllerServiceAdapter) ExecutorService(java.util.concurrent.ExecutorService) AbstractListener(co.cask.cdap.internal.app.runtime.AbstractListener) ApplicationId(co.cask.cdap.proto.id.ApplicationId) 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