Search in sources :

Example 56 with Service

use of com.google.common.util.concurrent.Service in project incubator-gobblin by apache.

the class ServiceBasedAppLauncher method start.

/**
 * Starts the {@link ApplicationLauncher} by starting all associated services. This method also adds a shutdown hook
 * that invokes {@link #stop()} and the {@link #close()} methods. So {@link #stop()} and {@link #close()} need not be
 * called explicitly; they can be triggered during the JVM shutdown.
 */
@Override
public synchronized void start() {
    if (this.hasStarted) {
        LOG.warn("ApplicationLauncher has already started");
        return;
    }
    this.hasStarted = true;
    this.serviceManager = new ServiceManager(this.services);
    // A listener that shutdowns the application if any service fails.
    this.serviceManager.addListener(new ServiceManager.Listener() {

        @Override
        public void failure(Service service) {
            super.failure(service);
            LOG.error(String.format("Service %s has failed.", service.getClass().getSimpleName()), service.failureCause());
            try {
                service.stopAsync();
                ServiceBasedAppLauncher.this.stop();
            } catch (ApplicationException ae) {
                LOG.error("Could not shutdown services gracefully. This may cause the application to hang.");
            }
        }
    });
    Runtime.getRuntime().addShutdownHook(new Thread() {

        @Override
        public void run() {
            try {
                ServiceBasedAppLauncher.this.stop();
            } catch (ApplicationException e) {
                LOG.error("Failed to shutdown application", e);
            } finally {
                try {
                    ServiceBasedAppLauncher.this.close();
                } catch (IOException e) {
                    LOG.error("Failed to close application", e);
                }
            }
        }
    });
    LOG.info("Starting the Gobblin application and all its associated Services");
    // Start the application
    this.serviceManager.startAsync().awaitHealthy();
}
Also used : ServiceManager(com.google.common.util.concurrent.ServiceManager) JMXReportingService(org.apache.gobblin.runtime.services.JMXReportingService) MetricsReportingService(org.apache.gobblin.runtime.services.MetricsReportingService) Service(com.google.common.util.concurrent.Service) IOException(java.io.IOException)

Example 57 with Service

use of com.google.common.util.concurrent.Service in project incubator-gobblin by apache.

the class GobblinYarnAppLauncher method launch.

/**
 * Launch a new Gobblin instance on Yarn.
 *
 * @throws IOException if there's something wrong launching the application
 * @throws YarnException if there's something wrong launching the application
 */
public void launch() throws IOException, YarnException {
    this.eventBus.register(this);
    String clusterName = this.config.getString(GobblinClusterConfigurationKeys.HELIX_CLUSTER_NAME_KEY);
    HelixUtils.createGobblinHelixCluster(this.config.getString(GobblinClusterConfigurationKeys.ZK_CONNECTION_STRING_KEY), clusterName);
    LOGGER.info("Created Helix cluster " + clusterName);
    connectHelixManager();
    startYarnClient();
    this.applicationId = getApplicationId();
    this.applicationStatusMonitor.scheduleAtFixedRate(new Runnable() {

        @Override
        public void run() {
            try {
                eventBus.post(new ApplicationReportArrivalEvent(yarnClient.getApplicationReport(applicationId.get())));
            } catch (YarnException | IOException e) {
                LOGGER.error("Failed to get application report for Gobblin Yarn application " + applicationId.get(), e);
                eventBus.post(new GetApplicationReportFailureEvent(e));
            }
        }
    }, 0, this.appReportIntervalMinutes, TimeUnit.MINUTES);
    List<Service> services = Lists.newArrayList();
    if (this.config.hasPath(GobblinYarnConfigurationKeys.KEYTAB_FILE_PATH)) {
        LOGGER.info("Adding YarnAppSecurityManager since login is keytab based");
        services.add(buildYarnAppSecurityManager());
    }
    if (!this.config.hasPath(GobblinYarnConfigurationKeys.LOG_COPIER_DISABLE_DRIVER_COPY) || !this.config.getBoolean(GobblinYarnConfigurationKeys.LOG_COPIER_DISABLE_DRIVER_COPY)) {
        services.add(buildLogCopier(this.config, new Path(this.sinkLogRootDir, this.applicationName + Path.SEPARATOR + this.applicationId.get().toString()), GobblinClusterUtils.getAppWorkDirPath(this.fs, this.applicationName, this.applicationId.get().toString())));
    }
    if (config.getBoolean(ConfigurationKeys.JOB_EXECINFO_SERVER_ENABLED_KEY)) {
        LOGGER.info("Starting the job execution info server since it is enabled");
        Properties properties = ConfigUtils.configToProperties(config);
        JobExecutionInfoServer executionInfoServer = new JobExecutionInfoServer(properties);
        services.add(executionInfoServer);
        if (config.getBoolean(ConfigurationKeys.ADMIN_SERVER_ENABLED_KEY)) {
            LOGGER.info("Starting the admin UI server since it is enabled");
            services.add(ServiceBasedAppLauncher.createAdminServer(properties, executionInfoServer.getAdvertisedServerUri()));
        }
    } else if (config.getBoolean(ConfigurationKeys.ADMIN_SERVER_ENABLED_KEY)) {
        LOGGER.warn("NOT starting the admin UI because the job execution info server is NOT enabled");
    }
    this.serviceManager = Optional.of(new ServiceManager(services));
    // Start all the services running in the ApplicationMaster
    this.serviceManager.get().startAsync();
}
Also used : Path(org.apache.hadoop.fs.Path) ApplicationReportArrivalEvent(org.apache.gobblin.yarn.event.ApplicationReportArrivalEvent) ServiceManager(com.google.common.util.concurrent.ServiceManager) ListeningExecutorService(com.google.common.util.concurrent.ListeningExecutorService) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) Service(com.google.common.util.concurrent.Service) JobExecutionInfoServer(org.apache.gobblin.rest.JobExecutionInfoServer) Properties(java.util.Properties) GetApplicationReportFailureEvent(org.apache.gobblin.yarn.event.GetApplicationReportFailureEvent)

Example 58 with Service

use of com.google.common.util.concurrent.Service in project helios by spotify.

the class SystemTestBase method baseTeardown.

@After
public void baseTeardown() throws Exception {
    for (final HeliosClient client : clients) {
        client.close();
    }
    clients.clear();
    for (final Service service : services) {
        try {
            service.stopAsync();
        } catch (Exception e) {
            log.error("Uncaught exception", e);
        }
    }
    for (final Service service : services) {
        try {
            service.awaitTerminated();
        } catch (Exception e) {
            log.error("Service failed", e);
        }
    }
    services.clear();
    // Clean up docker
    try (final DockerClient dockerClient = getNewDockerClient()) {
        final List<Container> containers = dockerClient.listContainers();
        for (final Container container : containers) {
            for (final String name : container.names()) {
                if (name.contains(testTag)) {
                    try {
                        dockerClient.killContainer(container.id());
                    } catch (DockerException e) {
                        e.printStackTrace();
                    }
                    break;
                }
            }
        }
    } catch (Exception e) {
        log.error("Docker client exception", e);
    }
    if (zk != null) {
        zk.close();
    }
    listThreads();
}
Also used : DockerException(com.spotify.docker.client.exceptions.DockerException) Container(com.spotify.docker.client.messages.Container) DockerClient(com.spotify.docker.client.DockerClient) DefaultDockerClient(com.spotify.docker.client.DefaultDockerClient) Service(com.google.common.util.concurrent.Service) Matchers.containsString(org.hamcrest.Matchers.containsString) Integer.toHexString(java.lang.Integer.toHexString) HeliosClient(com.spotify.helios.client.HeliosClient) DockerRequestException(com.spotify.docker.client.exceptions.DockerRequestException) DockerException(com.spotify.docker.client.exceptions.DockerException) ContainerNotFoundException(com.spotify.docker.client.exceptions.ContainerNotFoundException) ImageNotFoundException(com.spotify.docker.client.exceptions.ImageNotFoundException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) TimeoutException(java.util.concurrent.TimeoutException) ExpectedException(org.junit.rules.ExpectedException) After(org.junit.After)

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