Search in sources :

Example 46 with Service

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

the class DefaultPreviewRunner method startUp.

@Override
protected void startUp() throws Exception {
    if (messagingService instanceof Service) {
        ((Service) messagingService).startAndWait();
    }
    datasetService.startAndWait();
    // It is recommended to initialize log appender after datasetService is started,
    // since log appender instantiates a dataset.
    logAppenderInitializer.initialize();
    LoggingContextAccessor.setLoggingContext(new ServiceLoggingContext(NamespaceId.SYSTEM.getNamespace(), Constants.Logging.COMPONENT_NAME, Constants.Service.PREVIEW_HTTP));
    Futures.allAsList(applicationLifecycleService.start(), systemArtifactLoader.start(), programRuntimeService.start(), programLifecycleService.start(), metricsCollectionService.start(), programNotificationSubscriberService.start()).get();
}
Also used : MessagingService(co.cask.cdap.messaging.MessagingService) MetricsCollectionService(co.cask.cdap.api.metrics.MetricsCollectionService) ProgramRuntimeService(co.cask.cdap.app.runtime.ProgramRuntimeService) ProgramNotificationSubscriberService(co.cask.cdap.internal.app.services.ProgramNotificationSubscriberService) ProgramLifecycleService(co.cask.cdap.internal.app.services.ProgramLifecycleService) AbstractIdleService(com.google.common.util.concurrent.AbstractIdleService) ApplicationLifecycleService(co.cask.cdap.internal.app.services.ApplicationLifecycleService) DatasetService(co.cask.cdap.data2.datafabric.dataset.service.DatasetService) Service(com.google.common.util.concurrent.Service) ServiceLoggingContext(co.cask.cdap.common.logging.ServiceLoggingContext)

Example 47 with Service

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

the class FlowletProgramRunner method run.

@SuppressWarnings("unchecked")
@Override
public ProgramController run(Program program, ProgramOptions options) {
    BasicFlowletContext flowletContext = null;
    try {
        // Extract and verify parameters
        String flowletName = options.getProgramId().getProgram();
        int instanceId = Integer.parseInt(options.getArguments().getOption(ProgramOptionConstants.INSTANCE_ID, "-1"));
        Preconditions.checkArgument(instanceId >= 0, "Missing instance Id");
        int instanceCount = Integer.parseInt(options.getArguments().getOption(ProgramOptionConstants.INSTANCES, "0"));
        Preconditions.checkArgument(instanceCount > 0, "Invalid or missing instance count");
        RunId runId = ProgramRunners.getRunId(options);
        ApplicationSpecification appSpec = program.getApplicationSpecification();
        Preconditions.checkNotNull(appSpec, "Missing application specification.");
        ProgramType processorType = program.getType();
        Preconditions.checkNotNull(processorType, "Missing processor type.");
        Preconditions.checkArgument(processorType == ProgramType.FLOW, "Only FLOW process type is supported.");
        String processorName = program.getName();
        Preconditions.checkNotNull(processorName, "Missing processor name.");
        FlowSpecification flowSpec = appSpec.getFlows().get(processorName);
        FlowletDefinition flowletDef = flowSpec.getFlowlets().get(flowletName);
        Preconditions.checkNotNull(flowletDef, "Definition missing for flowlet \"%s\"", flowletName);
        Class<?> clz = Class.forName(flowletDef.getFlowletSpec().getClassName(), true, program.getClassLoader());
        Preconditions.checkArgument(Flowlet.class.isAssignableFrom(clz), "%s is not a Flowlet.", clz);
        // Setup dataset framework context, if required
        ProgramId programId = program.getId();
        FlowletId flowletId = programId.flowlet(flowletName);
        ProgramRunId run = programId.run(runId);
        ProgramContext programContext = new BasicProgramContext(run, flowletId);
        if (dsFramework instanceof ProgramContextAware) {
            ((ProgramContextAware) dsFramework).setContext(programContext);
        }
        Class<? extends Flowlet> flowletClass = (Class<? extends Flowlet>) clz;
        // Creates flowlet context
        flowletContext = new BasicFlowletContext(program, options, flowletId, instanceId, instanceCount, flowletDef.getDatasets(), flowletDef.getFlowletSpec(), metricsCollectionService, discoveryServiceClient, txClient, dsFramework, secureStore, secureStoreManager, messageService, cConf);
        // Creates tx related objects
        DataFabricFacade dataFabricFacade = dataFabricFacadeFactory.create(program, flowletContext.getDatasetCache());
        if (dataFabricFacade instanceof ProgramContextAware) {
            ((ProgramContextAware) dataFabricFacade).setContext(programContext);
        }
        // Creates QueueSpecification
        Table<Node, String, Set<QueueSpecification>> queueSpecs = new SimpleQueueSpecificationGenerator(new ApplicationId(program.getNamespaceId(), program.getApplicationId())).create(flowSpec);
        Flowlet flowlet = new InstantiatorFactory(false).get(TypeToken.of(flowletClass)).create();
        TypeToken<? extends Flowlet> flowletType = TypeToken.of(flowletClass);
        // Set the context classloader to the cdap classloader. It is needed for the DatumWriterFactory be able
        // to load cdap classes
        Thread.currentThread().setContextClassLoader(FlowletProgramRunner.class.getClassLoader());
        // Inject DataSet, OutputEmitter, Metric fields
        ImmutableList.Builder<ProducerSupplier> queueProducerSupplierBuilder = ImmutableList.builder();
        Reflections.visit(flowlet, flowlet.getClass(), new PropertyFieldSetter(flowletDef.getFlowletSpec().getProperties()), new DataSetFieldSetter(flowletContext), new MetricsFieldSetter(flowletContext.getMetrics()), new OutputEmitterFieldSetter(outputEmitterFactory(flowletContext, flowletName, dataFabricFacade, queueProducerSupplierBuilder, queueSpecs)));
        ImmutableList.Builder<ConsumerSupplier<?>> queueConsumerSupplierBuilder = ImmutableList.builder();
        Collection<ProcessSpecification<?>> processSpecs = createProcessSpecification(flowletContext, flowletType, processMethodFactory(flowlet), processSpecificationFactory(flowletContext, dataFabricFacade, queueReaderFactory, flowletName, queueSpecs, queueConsumerSupplierBuilder, createSchemaCache(program)), Lists.<ProcessSpecification<?>>newLinkedList());
        List<ConsumerSupplier<?>> consumerSuppliers = queueConsumerSupplierBuilder.build();
        // Create the flowlet driver
        AtomicReference<FlowletProgramController> controllerRef = new AtomicReference<>();
        Service serviceHook = createServiceHook(flowletName, consumerSuppliers, controllerRef);
        FlowletRuntimeService driver = new FlowletRuntimeService(flowlet, flowletContext, processSpecs, createCallback(flowlet, flowletDef.getFlowletSpec()), dataFabricFacade, serviceHook);
        FlowletProgramController controller = new FlowletProgramController(flowletName, flowletContext, driver, queueProducerSupplierBuilder.build(), consumerSuppliers);
        controllerRef.set(controller);
        LOG.info("Starting flowlet: {}", flowletContext);
        driver.start();
        LOG.info("Flowlet started: {}", flowletContext);
        return controller;
    } catch (Exception e) {
        // of the flowlet context.
        if (flowletContext != null) {
            flowletContext.close();
        }
        throw Throwables.propagate(e);
    }
}
Also used : ApplicationSpecification(co.cask.cdap.api.app.ApplicationSpecification) Set(java.util.Set) ImmutableSet(com.google.common.collect.ImmutableSet) ImmutableList(com.google.common.collect.ImmutableList) Node(co.cask.cdap.app.queue.QueueSpecificationGenerator.Node) BasicProgramContext(co.cask.cdap.internal.app.runtime.BasicProgramContext) ProgramContext(co.cask.cdap.data.ProgramContext) InstantiatorFactory(co.cask.cdap.common.lang.InstantiatorFactory) FlowletDefinition(co.cask.cdap.api.flow.FlowletDefinition) FlowSpecification(co.cask.cdap.api.flow.FlowSpecification) MetricsFieldSetter(co.cask.cdap.internal.app.runtime.MetricsFieldSetter) ProgramType(co.cask.cdap.proto.ProgramType) RunId(org.apache.twill.api.RunId) ProgramRunId(co.cask.cdap.proto.id.ProgramRunId) Flowlet(co.cask.cdap.api.flow.flowlet.Flowlet) MetricsCollectionService(co.cask.cdap.api.metrics.MetricsCollectionService) Service(com.google.common.util.concurrent.Service) MessagingService(co.cask.cdap.messaging.MessagingService) AbstractService(com.google.common.util.concurrent.AbstractService) AtomicReference(java.util.concurrent.atomic.AtomicReference) ProgramId(co.cask.cdap.proto.id.ProgramId) BasicProgramContext(co.cask.cdap.internal.app.runtime.BasicProgramContext) DataSetFieldSetter(co.cask.cdap.internal.app.runtime.DataSetFieldSetter) UnsupportedTypeException(co.cask.cdap.api.data.schema.UnsupportedTypeException) IOException(java.io.IOException) SimpleQueueSpecificationGenerator(co.cask.cdap.internal.app.queue.SimpleQueueSpecificationGenerator) PropertyFieldSetter(co.cask.cdap.common.lang.PropertyFieldSetter) FlowletId(co.cask.cdap.proto.id.FlowletId) ProgramRunId(co.cask.cdap.proto.id.ProgramRunId) DataFabricFacade(co.cask.cdap.internal.app.runtime.DataFabricFacade) ApplicationId(co.cask.cdap.proto.id.ApplicationId) ProgramContextAware(co.cask.cdap.data.ProgramContextAware)

Example 48 with Service

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

the class RuntimeMonitorTest method init.

@Before
public void init() throws IOException, UnsupportedTypeException {
    cConf = CConfiguration.create();
    cConf.set(Constants.AppFabric.OUTPUT_DIR, System.getProperty("java.io.tmpdir"));
    cConf.set(Constants.AppFabric.TEMP_DIR, System.getProperty("java.io.tmpdir"));
    cConf.set(Constants.AppFabric.PROGRAM_STATUS_EVENT_TOPIC, "cdap-status");
    cConf.set(Constants.RuntimeMonitor.BATCH_LIMIT, "2");
    cConf.set(Constants.RuntimeMonitor.POLL_TIME_MS, "20");
    cConf.set(Constants.RuntimeMonitor.TOPICS, Constants.AppFabric.PROGRAM_STATUS_RECORD_EVENT_TOPIC);
    injector = Guice.createInjector(new AppFabricTestModule(cConf));
    messagingService = injector.getInstance(MessagingService.class);
    if (messagingService instanceof Service) {
        ((Service) messagingService).startAndWait();
    }
    address = new InetSocketAddress(InetAddress.getLoopbackAddress(), 1234);
    httpServer = HttpServer.create(address, 0);
    httpServer.start();
}
Also used : InetSocketAddress(java.net.InetSocketAddress) MessagingService(co.cask.cdap.messaging.MessagingService) Service(com.google.common.util.concurrent.Service) AppFabricTestModule(co.cask.cdap.internal.guice.AppFabricTestModule) MessagingService(co.cask.cdap.messaging.MessagingService) Before(org.junit.Before)

Example 49 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) {
                    ProgramId programId = program.getId();
                    argumentsMap.put(programId, options.getUserArguments());
                    Service service = new FastService();
                    ProgramController controller = new ProgramControllerServiceAdapter(service, programId.run(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) ProgramId(co.cask.cdap.proto.id.ProgramId) SimpleProgramOptions(co.cask.cdap.internal.app.runtime.SimpleProgramOptions)

Example 50 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.run(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)58 MessagingService (co.cask.cdap.messaging.MessagingService)19 MetricsCollectionService (co.cask.cdap.api.metrics.MetricsCollectionService)16 Test (org.junit.Test)16 AbstractIdleService (com.google.common.util.concurrent.AbstractIdleService)15 IOException (java.io.IOException)13 CountDownLatch (java.util.concurrent.CountDownLatch)9 DatasetService (co.cask.cdap.data2.datafabric.dataset.service.DatasetService)8 Injector (com.google.inject.Injector)7 CConfiguration (co.cask.cdap.common.conf.CConfiguration)6 MetricsQueryService (co.cask.cdap.metrics.query.MetricsQueryService)6 ProgramId (co.cask.cdap.proto.id.ProgramId)6 ArrayList (java.util.ArrayList)6 TimeoutException (java.util.concurrent.TimeoutException)5 RunId (com.continuuity.weave.api.RunId)4 ZKClientService (com.continuuity.weave.zookeeper.ZKClientService)4 ExecutionException (java.util.concurrent.ExecutionException)4 Configuration (org.apache.hadoop.conf.Configuration)4 ProgramContextAware (co.cask.cdap.data.ProgramContextAware)3 StreamService (co.cask.cdap.data.stream.service.StreamService)3