Search in sources :

Example 6 with ServiceListenerAdapter

use of org.apache.twill.internal.ServiceListenerAdapter in project cdap by caskdata.

the class AppFabricServer method startUp.

/**
   * Configures the AppFabricService pre-start.
   */
@Override
protected void startUp() throws Exception {
    LoggingContextAccessor.setLoggingContext(new ServiceLoggingContext(NamespaceId.SYSTEM.getNamespace(), Constants.Logging.COMPONENT_NAME, Constants.Service.APP_FABRIC_HTTP));
    Futures.allAsList(ImmutableList.of(notificationService.start(), applicationLifecycleService.start(), systemArtifactLoader.start(), programRuntimeService.start(), streamCoordinatorClient.start(), programLifecycleService.start(), pluginService.start(), coreSchedulerService.start())).get();
    int serverPort;
    if (sslEnabled) {
        serverPort = cConf.getInt(Constants.AppFabric.SERVER_SSL_PORT);
        String password = generateRandomPassword();
        KeyStore ks = KeyStores.generatedCertKeyStore(sConf, password);
        this.sslHandlerFactory = new SSLHandlerFactory(ks, password);
    } else {
        serverPort = cConf.getInt(Constants.AppFabric.SERVER_PORT);
        this.sslHandlerFactory = null;
    }
    // Create handler hooks
    ImmutableList.Builder<HandlerHook> builder = ImmutableList.builder();
    for (String hook : handlerHookNames) {
        builder.add(new MetricsReporterHook(metricsCollectionService, hook));
    }
    // Run http service on random port
    NettyHttpService.Builder httpServiceBuilder = new CommonNettyHttpServiceBuilder(cConf, Constants.Service.APP_FABRIC_HTTP).setHost(hostname.getCanonicalHostName()).setPort(serverPort).setHandlerHooks(builder.build()).addHttpHandlers(handlers).setConnectionBacklog(cConf.getInt(Constants.AppFabric.BACKLOG_CONNECTIONS, Constants.AppFabric.DEFAULT_BACKLOG)).setExecThreadPoolSize(cConf.getInt(Constants.AppFabric.EXEC_THREADS, Constants.AppFabric.DEFAULT_EXEC_THREADS)).setBossThreadPoolSize(cConf.getInt(Constants.AppFabric.BOSS_THREADS, Constants.AppFabric.DEFAULT_BOSS_THREADS)).setWorkerThreadPoolSize(cConf.getInt(Constants.AppFabric.WORKER_THREADS, Constants.AppFabric.DEFAULT_WORKER_THREADS));
    if (sslEnabled) {
        httpServiceBuilder.modifyChannelPipeline(new Function<ChannelPipeline, ChannelPipeline>() {

            @Override
            public ChannelPipeline apply(ChannelPipeline input) {
                LOG.debug("Adding ssl handler to the pipeline.");
                SslHandler sslHandler = sslHandlerFactory.create();
                // SSL handler needs to be the first handler in the pipeline.
                input.addFirst("ssl", sslHandler);
                return input;
            }
        });
    }
    httpService = httpServiceBuilder.build();
    // Add a listener so that when the service started, register with service discovery.
    // Remove from service discovery when it is stopped.
    httpService.addListener(new ServiceListenerAdapter() {

        private List<Cancellable> cancellables = Lists.newArrayList();

        @Override
        public void running() {
            String announceAddress = cConf.get(Constants.Service.MASTER_SERVICES_ANNOUNCE_ADDRESS, httpService.getBindAddress().getHostName());
            int announcePort = cConf.getInt(Constants.AppFabric.SERVER_ANNOUNCE_PORT, httpService.getBindAddress().getPort());
            final InetSocketAddress socketAddress = new InetSocketAddress(announceAddress, announcePort);
            LOG.info("AppFabric HTTP Service announced at {}", socketAddress);
            // Tag the discoverable's payload to mark it as supporting ssl.
            byte[] sslPayload = sslEnabled ? Constants.Security.SSL_URI_SCHEME.getBytes() : Bytes.EMPTY_BYTE_ARRAY;
            // When it is running, register it with service discovery
            for (final String serviceName : servicesNames) {
                cancellables.add(discoveryService.register(ResolvingDiscoverable.of(new Discoverable(serviceName, socketAddress, sslPayload))));
            }
        }

        @Override
        public void terminated(State from) {
            LOG.info("AppFabric HTTP service stopped.");
            for (Cancellable cancellable : cancellables) {
                if (cancellable != null) {
                    cancellable.cancel();
                }
            }
        }

        @Override
        public void failed(State from, Throwable failure) {
            LOG.info("AppFabric HTTP service stopped with failure.", failure);
            for (Cancellable cancellable : cancellables) {
                if (cancellable != null) {
                    cancellable.cancel();
                }
            }
        }
    }, Threads.SAME_THREAD_EXECUTOR);
    httpService.startAndWait();
    defaultNamespaceEnsurer.startAndWait();
    if (appVersionUpgradeService != null) {
        appVersionUpgradeService.startAndWait();
    }
}
Also used : ResolvingDiscoverable(co.cask.cdap.common.discovery.ResolvingDiscoverable) Discoverable(org.apache.twill.discovery.Discoverable) MetricsReporterHook(co.cask.cdap.common.metrics.MetricsReporterHook) CommonNettyHttpServiceBuilder(co.cask.cdap.common.http.CommonNettyHttpServiceBuilder) ImmutableList(com.google.common.collect.ImmutableList) Cancellable(org.apache.twill.common.Cancellable) InetSocketAddress(java.net.InetSocketAddress) ServiceListenerAdapter(org.apache.twill.internal.ServiceListenerAdapter) HandlerHook(co.cask.http.HandlerHook) ServiceLoggingContext(co.cask.cdap.common.logging.ServiceLoggingContext) KeyStore(java.security.KeyStore) ChannelPipeline(org.jboss.netty.channel.ChannelPipeline) SslHandler(org.jboss.netty.handler.ssl.SslHandler) NettyHttpService(co.cask.http.NettyHttpService) SSLHandlerFactory(co.cask.cdap.security.tools.SSLHandlerFactory)

Example 7 with ServiceListenerAdapter

use of org.apache.twill.internal.ServiceListenerAdapter in project cdap by caskdata.

the class WorkerProgramRunner method run.

@Override
public ProgramController run(Program program, ProgramOptions options) {
    ApplicationSpecification appSpec = program.getApplicationSpecification();
    Preconditions.checkNotNull(appSpec, "Missing application specification.");
    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);
    ProgramType programType = program.getType();
    Preconditions.checkNotNull(programType, "Missing processor type.");
    Preconditions.checkArgument(programType == ProgramType.WORKER, "Only Worker process type is supported.");
    WorkerSpecification workerSpec = appSpec.getWorkers().get(program.getName());
    Preconditions.checkArgument(workerSpec != null, "Missing Worker specification for %s", program.getId());
    String instances = options.getArguments().getOption(ProgramOptionConstants.INSTANCES, String.valueOf(workerSpec.getInstances()));
    WorkerSpecification newWorkerSpec = new WorkerSpecification(workerSpec.getClassName(), workerSpec.getName(), workerSpec.getDescription(), workerSpec.getProperties(), workerSpec.getDatasets(), workerSpec.getResources(), Integer.valueOf(instances));
    // Setup dataset framework context, if required
    if (datasetFramework instanceof ProgramContextAware) {
        ProgramId programId = program.getId();
        ((ProgramContextAware) datasetFramework).setContext(new BasicProgramContext(programId.run(runId)));
    }
    final PluginInstantiator pluginInstantiator = createPluginInstantiator(options, program.getClassLoader());
    try {
        BasicWorkerContext context = new BasicWorkerContext(newWorkerSpec, program, options, cConf, instanceId, instanceCount, metricsCollectionService, datasetFramework, txClient, discoveryServiceClient, streamWriterFactory, pluginInstantiator, secureStore, secureStoreManager, messagingService);
        WorkerDriver worker = new WorkerDriver(program, newWorkerSpec, context);
        // Add a service listener to make sure the plugin instantiator is closed when the worker driver finished.
        worker.addListener(new ServiceListenerAdapter() {

            @Override
            public void terminated(Service.State from) {
                Closeables.closeQuietly(pluginInstantiator);
            }

            @Override
            public void failed(Service.State from, Throwable failure) {
                Closeables.closeQuietly(pluginInstantiator);
            }
        }, Threads.SAME_THREAD_EXECUTOR);
        ProgramController controller = new WorkerControllerServiceAdapter(worker, program.getId(), runId, workerSpec.getName() + "-" + instanceId);
        worker.start();
        return controller;
    } catch (Throwable t) {
        Closeables.closeQuietly(pluginInstantiator);
        throw t;
    }
}
Also used : ApplicationSpecification(co.cask.cdap.api.app.ApplicationSpecification) ProgramController(co.cask.cdap.app.runtime.ProgramController) WorkerSpecification(co.cask.cdap.api.worker.WorkerSpecification) MessagingService(co.cask.cdap.messaging.MessagingService) MetricsCollectionService(co.cask.cdap.api.metrics.MetricsCollectionService) Service(com.google.common.util.concurrent.Service) ServiceListenerAdapter(org.apache.twill.internal.ServiceListenerAdapter) ProgramId(co.cask.cdap.proto.id.ProgramId) BasicProgramContext(co.cask.cdap.internal.app.runtime.BasicProgramContext) PluginInstantiator(co.cask.cdap.internal.app.runtime.plugin.PluginInstantiator) ProgramType(co.cask.cdap.proto.ProgramType) RunId(org.apache.twill.api.RunId) ProgramContextAware(co.cask.cdap.data.ProgramContextAware)

Aggregations

ServiceListenerAdapter (org.apache.twill.internal.ServiceListenerAdapter)7 Service (com.google.common.util.concurrent.Service)3 ApplicationSpecification (co.cask.cdap.api.app.ApplicationSpecification)2 MetricsCollectionService (co.cask.cdap.api.metrics.MetricsCollectionService)2 ProgramController (co.cask.cdap.app.runtime.ProgramController)2 ResolvingDiscoverable (co.cask.cdap.common.discovery.ResolvingDiscoverable)2 CommonNettyHttpServiceBuilder (co.cask.cdap.common.http.CommonNettyHttpServiceBuilder)2 MetricsReporterHook (co.cask.cdap.common.metrics.MetricsReporterHook)2 ProgramContextAware (co.cask.cdap.data.ProgramContextAware)2 BasicProgramContext (co.cask.cdap.internal.app.runtime.BasicProgramContext)2 PluginInstantiator (co.cask.cdap.internal.app.runtime.plugin.PluginInstantiator)2 MessagingService (co.cask.cdap.messaging.MessagingService)2 BasicThrowable (co.cask.cdap.proto.BasicThrowable)2 ProgramRunStatus (co.cask.cdap.proto.ProgramRunStatus)2 ProgramType (co.cask.cdap.proto.ProgramType)2 ProgramId (co.cask.cdap.proto.id.ProgramId)2 InetSocketAddress (java.net.InetSocketAddress)2 Nullable (javax.annotation.Nullable)2 RunId (org.apache.twill.api.RunId)2 Cancellable (org.apache.twill.common.Cancellable)2