Search in sources :

Example 1 with ProgramControllerServiceAdapter

use of co.cask.cdap.internal.app.runtime.ProgramControllerServiceAdapter 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 2 with ProgramControllerServiceAdapter

use of co.cask.cdap.internal.app.runtime.ProgramControllerServiceAdapter 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

ProgramControllerServiceAdapter (co.cask.cdap.internal.app.runtime.ProgramControllerServiceAdapter)2 AbstractIdleService (com.google.common.util.concurrent.AbstractIdleService)2 Service (com.google.common.util.concurrent.Service)2 Program (co.cask.cdap.app.program.Program)1 AbstractListener (co.cask.cdap.internal.app.runtime.AbstractListener)1 SimpleProgramOptions (co.cask.cdap.internal.app.runtime.SimpleProgramOptions)1 ProgramType (co.cask.cdap.proto.ProgramType)1 ApplicationId (co.cask.cdap.proto.id.ApplicationId)1 ProgramId (co.cask.cdap.proto.id.ProgramId)1 AbstractExecutionThreadService (com.google.common.util.concurrent.AbstractExecutionThreadService)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 ExecutorService (java.util.concurrent.ExecutorService)1 Test (org.junit.Test)1