List of usage examples for com.google.common.util.concurrent Service addListener
void addListener(Listener listener, Executor executor);
From source file:com.continuuity.weave.internal.WeaveContainerMain.java
/** * Main method for launching a {@link com.continuuity.weave.internal.WeaveContainerService} which runs * a {@link com.continuuity.weave.api.WeaveRunnable}. *//*from w w w. ja v a2 s . co m*/ public static void main(final String[] args) throws Exception { String zkConnectStr = System.getenv(EnvKeys.WEAVE_ZK_CONNECT); File weaveSpecFile = new File("weaveSpec.json"); RunId appRunId = RunIds.fromString(System.getenv(EnvKeys.WEAVE_APP_RUN_ID)); RunId runId = RunIds.fromString(System.getenv(EnvKeys.WEAVE_RUN_ID)); String runnableName = System.getenv(EnvKeys.WEAVE_RUNNABLE_NAME); int instanceId = Integer.parseInt(System.getenv(EnvKeys.WEAVE_INSTANCE_ID)); ZKClientService zkClientService = ZKClientServices.delegate( ZKClients.reWatchOnExpire(ZKClients.retryOnFailure(ZKClientService.Builder.of(zkConnectStr).build(), RetryStrategies.fixDelay(1, TimeUnit.SECONDS)))); DiscoveryService discoveryService = new ZKDiscoveryService(zkClientService); WeaveSpecification weaveSpec = loadWeaveSpec(weaveSpecFile); WeaveRunnableSpecification runnableSpec = weaveSpec.getRunnables().get(runnableName) .getRunnableSpecification(); ContainerInfo containerInfo = new ContainerInfo(); WeaveContext context = new BasicWeaveContext(containerInfo.getHost(), args, decodeArgs(System.getenv(EnvKeys.WEAVE_APPLICATION_ARGS)), runnableSpec, instanceId, discoveryService); Service service = new WeaveContainerService(context, containerInfo, getContainerZKClient(zkClientService, appRunId, runnableName), runId, runnableSpec, ClassLoader.getSystemClassLoader()); service.addListener(createServiceListener(zkClientService), Threads.SAME_THREAD_EXECUTOR); CountDownLatch stopLatch = new CountDownLatch(1); zkClientService.addListener(createZKClientServiceListener(stopLatch), Threads.SAME_THREAD_EXECUTOR); new WeaveContainerMain().doMain(wrapService(zkClientService, service)); stopLatch.await(); }
From source file:org.apache.twill.internal.Services.java
/** * Returns a {@link ListenableFuture} that will be completed when the given service is stopped. If the service * stopped due to error, the failure cause would be reflected in the future. * * @param service The {@link Service} to block on. * @return A {@link ListenableFuture} that will be completed when the service is stopped. *//*from ww w. ja v a 2s. c om*/ public static ListenableFuture<Service.State> getCompletionFuture(Service service) { final SettableFuture<Service.State> resultFuture = SettableFuture.create(); service.addListener(new ServiceListenerAdapter() { @Override public void terminated(Service.State from) { resultFuture.set(Service.State.TERMINATED); } @Override public void failed(Service.State from, Throwable failure) { resultFuture.setException(failure); } }, Threads.SAME_THREAD_EXECUTOR); Service.State state = service.state(); if (state == Service.State.TERMINATED) { return Futures.immediateFuture(state); } else if (state == Service.State.FAILED) { return Futures .immediateFailedFuture(new IllegalStateException("Service failed with unknown exception.")); } return resultFuture; }
From source file:io.pravega.common.concurrent.ServiceHelpers.java
/** * Attaches the given callbacks which will be invoked when the given Service enters a TERMINATED or FAILED state. * The callbacks are optional and may be invoked synchronously if the Service is already in one of these states. * * @param service The Service to attach to. * @param terminatedCallback (Optional) A Runnable that will be invoked if the Service enters a TERMINATED state. * @param failureCallback (Optional) A Runnable that will be invoked if the Service enters a FAILED state. * @param executor An Executor to use for callback invocations. *//*w w w .j a va 2 s . com*/ public static void onStop(Service service, Runnable terminatedCallback, Consumer<Throwable> failureCallback, Executor executor) { ShutdownListener listener = new ShutdownListener(terminatedCallback, failureCallback); service.addListener(listener, executor); // addListener() will not invoke the callbacks if the service is already in a terminal state. As such, we need to // manually check for these states after registering the listener and invoke the appropriate callback. The // ShutdownListener will make sure they are not invoked multiple times. Service.State state = service.state(); if (state == Service.State.FAILED) { // We don't care (or know) the state from which we came, so we just pass some random one. listener.failed(Service.State.FAILED, service.failureCause()); } else if (state == Service.State.TERMINATED) { listener.terminated(Service.State.TERMINATED); } }
From source file:io.pravega.common.concurrent.ServiceHelpers.java
/** * Asynchronously starts a Service and returns a CompletableFuture that will indicate when it is running. * * @param service The Service to start. * @param executor An Executor to use for callback invocations. * @return A CompletableFuture that will be completed when the service enters a RUNNING state, or completed * exceptionally if the service failed to start. *///from w ww . j a va2s .c o m public static CompletableFuture<Void> startAsync(Service service, Executor executor) { // Service.startAsync() will fail if the service is not in a NEW state. That is, if it is already RUNNING or // STARTED, then the method will fail synchronously, hence we are not in danger of not invoking our callbacks, // as long as we register the Listener before we attempt to start. // Nevertheless, do make a sanity check since once added, a Listener cannot be removed. Preconditions.checkState(service.state() == Service.State.NEW, "Service expected to be %s but was %s.", Service.State.NEW, service.state()); Preconditions.checkNotNull(executor, "executor"); CompletableFuture<Void> result = new CompletableFuture<>(); service.addListener(new StartupListener(result), executor); service.startAsync(); return result; }
From source file:co.cask.cdap.internal.app.runtime.ProgramControllerServiceAdapter.java
private void listenToRuntimeState(Service service) { service.addListener(new ServiceListenerAdapter() { @Override/*from ww w . ja v a 2 s . c o m*/ public void running() { started(); } @Override public void failed(Service.State from, Throwable failure) { LOG.error("Program terminated with exception", failure); error(failure); } @Override public void terminated(Service.State from) { if (from != Service.State.STOPPING) { // Service completed by itself. Simply signal the state change of this controller. complete(); } else { // Service was killed stop(); } } }, Threads.SAME_THREAD_EXECUTOR); }
From source file:co.cask.cdap.common.twill.AbstractMasterTwillRunnable.java
@Override public void run() { runThread = Thread.currentThread(); LOG.info("Starting runnable {}", name); SettableFuture<String> completionFuture = SettableFuture.create(); for (Service service : services) { service.addListener(createServiceListener(service.getClass().getName(), completionFuture), Threads.SAME_THREAD_EXECUTOR); }/*from w w w .jav a 2 s .c o m*/ Services.chainStart(services.get(0), services.subList(1, services.size()).toArray(new Service[0])); LOG.info("Runnable started {}", name); try { // exit as soon as any service completes completionFuture.get(); } catch (InterruptedException e) { LOG.debug("Waiting on latch interrupted {}", name); Thread.currentThread().interrupt(); } catch (ExecutionException e) { throw Throwables.propagate(e.getCause()); } List<Service> reverse = Lists.reverse(services); Services.chainStop(reverse.get(0), reverse.subList(1, reverse.size()).toArray(new Service[0])); LOG.info("Runnable stopped {}", name); }
From source file:co.cask.cdap.internal.app.runtime.workflow.WorkflowProgramController.java
private void startListen(Service service) { // Forward state changes from the given service to this controller. service.addListener(new ServiceListenerAdapter() { @Override//from w w w . jav a 2s . c om public void running() { InetSocketAddress endpoint = driver.getServiceEndpoint(); cancelAnnounce = serviceAnnouncer.announce(serviceName, endpoint.getPort()); LOG.info("Workflow service {} announced at {}", serviceName, endpoint); started(); } @Override public void terminated(Service.State from) { LOG.info("Workflow service terminated from {}. Un-registering service {}.", from, serviceName); cancelAnnounce.cancel(); LOG.info("Service {} unregistered.", serviceName); if (getState() != State.STOPPING) { // service completed itself. complete(); } else { // service was terminated stop(); } } @Override public void failed(Service.State from, Throwable failure) { LOG.info("Workflow service failed from {}. Un-registering service {}.", from, serviceName, failure); cancelAnnounce.cancel(); LOG.info("Service {} unregistered.", serviceName); error(failure); } }, Threads.SAME_THREAD_EXECUTOR); }
From source file:co.cask.tigon.internal.app.runtime.distributed.AbstractServiceTwillRunnable.java
@Override public void run() { runThread = Thread.currentThread(); LOG.info("Starting runnable {}", name); List<ListenableFuture<Service.State>> completions = Lists.newArrayList(); for (Service service : services) { SettableFuture<Service.State> completion = SettableFuture.create(); service.addListener(createServiceListener(completion), Threads.SAME_THREAD_EXECUTOR); completions.add(completion);// w ww . java2 s .c o m } Services.chainStart(services.get(0), services.subList(1, services.size()).toArray(new Service[0])); LOG.info("Runnable started {}", name); try { Futures.allAsList(completions).get(); } catch (InterruptedException e) { LOG.debug("Waiting on latch interrupted {}", name); Thread.currentThread().interrupt(); } catch (ExecutionException e) { LOG.error("Exception in service.", e); throw Throwables.propagate(e); } List<Service> reverse = Lists.reverse(services); Services.chainStop(reverse.get(0), reverse.subList(1, reverse.size()).toArray(new Service[0])); LOG.info("Runnable stopped {}", name); }
From source file:co.cask.cdap.internal.app.runtime.spark.SparkProgramRunner.java
@Override public ProgramController run(Program program, ProgramOptions options) { // Extract and verify parameters final ApplicationSpecification appSpec = program.getApplicationSpecification(); Preconditions.checkNotNull(appSpec, "Missing application specification."); ProgramType processorType = program.getType(); Preconditions.checkNotNull(processorType, "Missing processor type."); Preconditions.checkArgument(processorType == ProgramType.SPARK, "Only Spark process type is supported."); final SparkSpecification spec = appSpec.getSpark().get(program.getName()); Preconditions.checkNotNull(spec, "Missing SparkSpecification for %s", program.getName()); // Optionally get runId. If the spark started by other program (e.g. Workflow), it inherit the runId. Arguments arguments = options.getArguments(); RunId runId = RunIds.fromString(arguments.getOption(ProgramOptionConstants.RUN_ID)); long logicalStartTime = arguments.hasOption(ProgramOptionConstants.LOGICAL_START_TIME) ? Long.parseLong(arguments.getOption(ProgramOptionConstants.LOGICAL_START_TIME)) : System.currentTimeMillis(); WorkflowToken workflowToken = null;/* w w w . j ava 2 s . c om*/ if (arguments.hasOption(ProgramOptionConstants.WORKFLOW_TOKEN)) { workflowToken = GSON.fromJson(arguments.getOption(ProgramOptionConstants.WORKFLOW_TOKEN), BasicWorkflowToken.class); } ClientSparkContext context = new ClientSparkContext(program, runId, logicalStartTime, options.getUserArguments().asMap(), new TransactionContext(txSystemClient), datasetFramework, discoveryServiceClient, metricsCollectionService, workflowToken); Spark spark; try { spark = new InstantiatorFactory(false).get(TypeToken.of(program.<Spark>getMainClass())).create(); // Fields injection Reflections.visit(spark, TypeToken.of(spark.getClass()), new PropertyFieldSetter(spec.getProperties()), new DataSetFieldSetter(context), new MetricsFieldSetter(context.getMetrics())); } catch (Exception e) { LOG.error("Failed to instantiate Spark class for {}", spec.getClassName(), e); throw Throwables.propagate(e); } Service sparkRuntimeService = new SparkRuntimeService(cConf, hConf, spark, new SparkContextFactory(hConf, context, datasetFramework, streamAdmin), program.getJarLocation(), txSystemClient); sparkRuntimeService.addListener(createRuntimeServiceListener(program.getId(), runId, arguments), Threads.SAME_THREAD_EXECUTOR); ProgramController controller = new SparkProgramController(sparkRuntimeService, context); LOG.info("Starting Spark Job: {}", context.toString()); sparkRuntimeService.start(); return controller; }
From source file:co.cask.cdap.app.runtime.spark.SparkProgramRunner.java
@Override public ProgramController run(Program program, ProgramOptions options) { // Get the RunId first. It is used for the creation of the ClassLoader closing thread. Arguments arguments = options.getArguments(); RunId runId = RunIds.fromString(arguments.getOption(ProgramOptionConstants.RUN_ID)); Deque<Closeable> closeables = new LinkedList<>(); try {/*from w w w .jav a 2 s . co m*/ // Extract and verify parameters ApplicationSpecification appSpec = program.getApplicationSpecification(); Preconditions.checkNotNull(appSpec, "Missing application specification."); ProgramType processorType = program.getType(); Preconditions.checkNotNull(processorType, "Missing processor type."); Preconditions.checkArgument(processorType == ProgramType.SPARK, "Only Spark process type is supported."); SparkSpecification spec = appSpec.getSpark().get(program.getName()); Preconditions.checkNotNull(spec, "Missing SparkSpecification for %s", program.getName()); String host = options.getArguments().getOption(ProgramOptionConstants.HOST); Preconditions.checkArgument(host != null, "No hostname is provided"); // Get the WorkflowProgramInfo if it is started by Workflow WorkflowProgramInfo workflowInfo = WorkflowProgramInfo.create(arguments); DatasetFramework programDatasetFramework = workflowInfo == null ? datasetFramework : NameMappedDatasetFramework.createFromWorkflowProgramInfo(datasetFramework, workflowInfo, appSpec); // Setup dataset framework context, if required if (programDatasetFramework instanceof ProgramContextAware) { Id.Program programId = program.getId(); ((ProgramContextAware) programDatasetFramework).initContext(new Id.Run(programId, runId.getId())); } PluginInstantiator pluginInstantiator = createPluginInstantiator(options, program.getClassLoader()); if (pluginInstantiator != null) { closeables.addFirst(pluginInstantiator); } SparkRuntimeContext runtimeContext = new SparkRuntimeContext(new Configuration(hConf), program, runId, options.getUserArguments().asMap(), txClient, programDatasetFramework, discoveryServiceClient, metricsCollectionService, streamAdmin, workflowInfo, pluginInstantiator); closeables.addFirst(runtimeContext); Spark spark; try { spark = new InstantiatorFactory(false).get(TypeToken.of(program.<Spark>getMainClass())).create(); // Fields injection Reflections.visit(spark, spark.getClass(), new PropertyFieldSetter(spec.getProperties()), new DataSetFieldSetter(runtimeContext.getDatasetCache()), new MetricsFieldSetter(runtimeContext)); } catch (Exception e) { LOG.error("Failed to instantiate Spark class for {}", spec.getClassName(), e); throw Throwables.propagate(e); } SparkSubmitter submitter = SparkRuntimeContextConfig.isLocal(hConf) ? new LocalSparkSubmitter() : new DistributedSparkSubmitter(hConf, host, runtimeContext, options.getArguments().getOption(Constants.AppFabric.APP_SCHEDULER_QUEUE)); Service sparkRuntimeService = new SparkRuntimeService(cConf, spark, getPluginArchive(options), runtimeContext, submitter, host); sparkRuntimeService.addListener(createRuntimeServiceListener(program.getId(), runId, arguments, options.getUserArguments(), closeables, store), Threads.SAME_THREAD_EXECUTOR); ProgramController controller = new SparkProgramController(sparkRuntimeService, runtimeContext); LOG.info("Starting Spark Job: {}", runtimeContext); if (SparkRuntimeContextConfig.isLocal(hConf) || UserGroupInformation.isSecurityEnabled()) { sparkRuntimeService.start(); } else { ProgramRunners.startAsUser(cConf.get(Constants.CFG_HDFS_USER), sparkRuntimeService); } return controller; } catch (Throwable t) { closeAll(closeables); throw Throwables.propagate(t); } }