Search in sources :

Example 1 with ServiceListenerAdapter

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

the class MapReduceProgramRunner method createRuntimeServiceListener.

/**
   * Creates a service listener to reactor on state changes on {@link MapReduceRuntimeService}.
   */
private Service.Listener createRuntimeServiceListener(final ProgramId programId, final RunId runId, final Iterable<Closeable> closeables, final Arguments arguments, final Arguments userArgs) {
    final String twillRunId = arguments.getOption(ProgramOptionConstants.TWILL_RUN_ID);
    return new ServiceListenerAdapter() {

        @Override
        public void starting() {
            //Get start time from RunId
            long startTimeInSeconds = RunIds.getTime(runId, TimeUnit.SECONDS);
            if (startTimeInSeconds == -1) {
                // If RunId is not time-based, use current time as start time
                startTimeInSeconds = TimeUnit.MILLISECONDS.toSeconds(System.currentTimeMillis());
            }
            final long finalStartTimeInSeconds = startTimeInSeconds;
            Retries.supplyWithRetries(new Supplier<Void>() {

                @Override
                public Void get() {
                    runtimeStore.setStart(programId, runId.getId(), finalStartTimeInSeconds, twillRunId, userArgs.asMap(), arguments.asMap());
                    return null;
                }
            }, RetryStrategies.fixDelay(Constants.Retry.RUN_RECORD_UPDATE_RETRY_DELAY_SECS, TimeUnit.SECONDS));
        }

        @Override
        public void terminated(Service.State from) {
            closeAllQuietly(closeables);
            ProgramRunStatus runStatus = ProgramController.State.COMPLETED.getRunStatus();
            if (from == Service.State.STOPPING) {
                // Service was killed
                runStatus = ProgramController.State.KILLED.getRunStatus();
            }
            final ProgramRunStatus finalRunStatus = runStatus;
            Retries.supplyWithRetries(new Supplier<Void>() {

                @Override
                public Void get() {
                    runtimeStore.setStop(programId, runId.getId(), TimeUnit.MILLISECONDS.toSeconds(System.currentTimeMillis()), finalRunStatus);
                    return null;
                }
            }, RetryStrategies.fixDelay(Constants.Retry.RUN_RECORD_UPDATE_RETRY_DELAY_SECS, TimeUnit.SECONDS));
        }

        @Override
        public void failed(Service.State from, @Nullable final Throwable failure) {
            closeAllQuietly(closeables);
            Retries.supplyWithRetries(new Supplier<Void>() {

                @Override
                public Void get() {
                    runtimeStore.setStop(programId, runId.getId(), TimeUnit.MILLISECONDS.toSeconds(System.currentTimeMillis()), ProgramController.State.ERROR.getRunStatus(), new BasicThrowable(failure));
                    return null;
                }
            }, RetryStrategies.fixDelay(Constants.Retry.RUN_RECORD_UPDATE_RETRY_DELAY_SECS, TimeUnit.SECONDS));
        }
    };
}
Also used : ProgramRunStatus(co.cask.cdap.proto.ProgramRunStatus) BasicThrowable(co.cask.cdap.proto.BasicThrowable) ServiceListenerAdapter(org.apache.twill.internal.ServiceListenerAdapter) BasicThrowable(co.cask.cdap.proto.BasicThrowable) Nullable(javax.annotation.Nullable)

Example 2 with ServiceListenerAdapter

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

the class MetadataService method startUp.

@Override
protected void startUp() throws Exception {
    LOG.info("Starting Metadata Service");
    metadataUpgrader.createOrUpgradeIfNecessary();
    httpService = new CommonNettyHttpServiceBuilder(cConf, Constants.Service.METADATA_SERVICE).addHttpHandlers(handlers).setHandlerHooks(ImmutableList.of(new MetricsReporterHook(metricsCollectionService, Constants.Service.METADATA_SERVICE))).setHost(cConf.get(Constants.Metadata.SERVICE_BIND_ADDRESS)).setPort(cConf.getInt(Constants.Metadata.SERVICE_BIND_PORT)).setWorkerThreadPoolSize(cConf.getInt(Constants.Metadata.SERVICE_WORKER_THREADS)).setExecThreadPoolSize(cConf.getInt(Constants.Metadata.SERVICE_EXEC_THREADS)).setConnectionBacklog(20000).build();
    httpService.addListener(new ServiceListenerAdapter() {

        private Cancellable cancellable;

        @Override
        public void running() {
            final InetSocketAddress socketAddress = httpService.getBindAddress();
            LOG.info("Metadata service running at {}", socketAddress);
            cancellable = discoveryService.register(ResolvingDiscoverable.of(new Discoverable(Constants.Service.METADATA_SERVICE, socketAddress)));
        }

        @Override
        public void terminated(State from) {
            LOG.info("Metadata HTTP service stopped");
            cancellable.cancel();
        }

        @Override
        public void failed(State from, Throwable failure) {
            LOG.info("Metadata HTTP service stopped with failure.", failure);
            cancellable.cancel();
        }
    }, Threads.SAME_THREAD_EXECUTOR);
    httpService.startAndWait();
}
Also used : Discoverable(org.apache.twill.discovery.Discoverable) ResolvingDiscoverable(co.cask.cdap.common.discovery.ResolvingDiscoverable) MetricsReporterHook(co.cask.cdap.common.metrics.MetricsReporterHook) CommonNettyHttpServiceBuilder(co.cask.cdap.common.http.CommonNettyHttpServiceBuilder) Cancellable(org.apache.twill.common.Cancellable) InetSocketAddress(java.net.InetSocketAddress) ServiceListenerAdapter(org.apache.twill.internal.ServiceListenerAdapter)

Example 3 with ServiceListenerAdapter

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

the class ServiceProgramRunner method run.

@Override
public ProgramController run(Program program, ProgramOptions options) {
    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 programType = program.getType();
    Preconditions.checkNotNull(programType, "Missing processor type.");
    Preconditions.checkArgument(programType == ProgramType.SERVICE, "Only Service process type is supported.");
    ServiceSpecification spec = appSpec.getServices().get(program.getName());
    String host = options.getArguments().getOption(ProgramOptionConstants.HOST);
    Preconditions.checkArgument(host != null, "No hostname is provided");
    // 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 {
        ServiceHttpServer component = new ServiceHttpServer(host, program, options, cConf, spec, instanceId, instanceCount, serviceAnnouncer, metricsCollectionService, datasetFramework, txClient, discoveryServiceClient, pluginInstantiator, secureStore, secureStoreManager, messagingService, defaultArtifactManager);
        // Add a service listener to make sure the plugin instantiator is closed when the http server is finished.
        component.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 ServiceProgramControllerAdapter(component, program.getId(), runId, spec.getName() + "-" + instanceId);
        component.start();
        return controller;
    } catch (Throwable t) {
        Closeables.closeQuietly(pluginInstantiator);
        throw t;
    }
}
Also used : ApplicationSpecification(co.cask.cdap.api.app.ApplicationSpecification) ServiceSpecification(co.cask.cdap.api.service.ServiceSpecification) ProgramController(co.cask.cdap.app.runtime.ProgramController) 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) ServiceHttpServer(co.cask.cdap.internal.app.services.ServiceHttpServer) 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)

Example 4 with ServiceListenerAdapter

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

the class SparkProgramRunner method createRuntimeServiceListener.

/**
   * Creates a service listener to reactor on state changes on {@link SparkRuntimeService}.
   */
private Service.Listener createRuntimeServiceListener(final ProgramId programId, final RunId runId, final Arguments arguments, final Arguments userArgs, final Iterable<Closeable> closeables, final RuntimeStore runtimeStore) {
    final String twillRunId = arguments.getOption(ProgramOptionConstants.TWILL_RUN_ID);
    return new ServiceListenerAdapter() {

        @Override
        public void starting() {
            //Get start time from RunId
            long startTimeInSeconds = RunIds.getTime(runId, TimeUnit.SECONDS);
            if (startTimeInSeconds == -1) {
                // If RunId is not time-based, use current time as start time
                startTimeInSeconds = TimeUnit.MILLISECONDS.toSeconds(System.currentTimeMillis());
            }
            final long finalStartTimeInSeconds = startTimeInSeconds;
            Retries.supplyWithRetries(new Supplier<Void>() {

                @Override
                public Void get() {
                    runtimeStore.setStart(programId, runId.getId(), finalStartTimeInSeconds, twillRunId, userArgs.asMap(), arguments.asMap());
                    return null;
                }
            }, RetryStrategies.fixDelay(Constants.Retry.RUN_RECORD_UPDATE_RETRY_DELAY_SECS, TimeUnit.SECONDS));
        }

        @Override
        public void terminated(Service.State from) {
            closeAll(closeables);
            ProgramRunStatus runStatus = ProgramController.State.COMPLETED.getRunStatus();
            if (from == Service.State.STOPPING) {
                // Service was killed
                runStatus = ProgramController.State.KILLED.getRunStatus();
            }
            final ProgramRunStatus finalRunStatus = runStatus;
            Retries.supplyWithRetries(new Supplier<Void>() {

                @Override
                public Void get() {
                    runtimeStore.setStop(programId, runId.getId(), TimeUnit.MILLISECONDS.toSeconds(System.currentTimeMillis()), finalRunStatus);
                    return null;
                }
            }, RetryStrategies.fixDelay(Constants.Retry.RUN_RECORD_UPDATE_RETRY_DELAY_SECS, TimeUnit.SECONDS));
        }

        @Override
        public void failed(Service.State from, @Nullable final Throwable failure) {
            closeAll(closeables);
            Retries.supplyWithRetries(new Supplier<Void>() {

                @Override
                public Void get() {
                    runtimeStore.setStop(programId, runId.getId(), TimeUnit.MILLISECONDS.toSeconds(System.currentTimeMillis()), ProgramController.State.ERROR.getRunStatus(), new BasicThrowable(failure));
                    return null;
                }
            }, RetryStrategies.fixDelay(Constants.Retry.RUN_RECORD_UPDATE_RETRY_DELAY_SECS, TimeUnit.SECONDS));
        }
    };
}
Also used : ProgramRunStatus(co.cask.cdap.proto.ProgramRunStatus) BasicThrowable(co.cask.cdap.proto.BasicThrowable) ServiceListenerAdapter(org.apache.twill.internal.ServiceListenerAdapter) BasicThrowable(co.cask.cdap.proto.BasicThrowable) Nullable(javax.annotation.Nullable)

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

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