/** * This test is for a case where two Service.Listener callbacks for the same service would call * transitionService in the wrong order due to a race. Due to the fact that it is a race this test * isn't guaranteed to expose the issue, but it is at least likely to become flaky if the race * sneaks back in, and in this case flaky means something is definitely wrong. * * <p>Before the bug was fixed this test would fail at least 30% of the time. */ public void testTransitionRace() throws TimeoutException { for (int k = 0; k < 1000; k++) { List<Service> services = Lists.newArrayList(); for (int i = 0; i < 5; i++) { services.add(new SnappyShutdownService(i)); } ServiceManager manager = new ServiceManager(services); manager.startAsync().awaitHealthy(); manager.stopAsync().awaitStopped(1, TimeUnit.SECONDS); } }
this.serviceManager = new ServiceManager(this.services); this.serviceManager.addListener(new ServiceManager.Listener() { @Override public void failure(Service service) { this.serviceManager.startAsync().awaitHealthy();
TestLogHandler logHandler = new TestLogHandler(); logger.addHandler(logHandler); ServiceManager manager = new ServiceManager(Arrays.<Service>asList()); RecordingListener listener = new RecordingListener(); manager.addListener(listener); manager.startAsync().awaitHealthy(); assertTrue(manager.isHealthy()); assertTrue(listener.healthyCalled); assertFalse(listener.stoppedCalled); assertTrue(listener.failedServices.isEmpty()); manager.stopAsync().awaitStopped(); assertFalse(manager.isHealthy()); assertTrue(listener.stoppedCalled); assertTrue(listener.failedServices.isEmpty()); assertEquals("ServiceManager{services=[]}", manager.toString()); assertTrue(manager.servicesByState().isEmpty()); assertTrue(manager.startupTimes().isEmpty()); Formatter logFormatter = new Formatter() {
public void run() { // Give the services 5 seconds to stop to ensure that we are responsive to shutdown // requests. try { manager.stopAsync().awaitStopped(5, TimeUnit.SECONDS); } catch (TimeoutException timeout) { // stopping timed out } } });
private void startServices() { logger.info("SingleTaskRunner start services."); getServices(); this.serviceManager.startAsync(); try { this.serviceManager.awaitHealthy(10, TimeUnit.SECONDS); } catch (final TimeoutException e) { throw new GobblinClusterException("Timeout waiting for services to start.", e); } }
public LocalJobLauncher(Properties jobProps, SharedResourcesBroker<GobblinScopeTypes> instanceBroker, List<? extends Tag<?>> metadataTags) throws Exception { super(jobProps, metadataTags, instanceBroker); log.debug("Local job launched with properties: {}", jobProps); TimingEvent jobLocalSetupTimer = this.eventSubmitter.getTimingEvent(TimingEvent.RunJobTimings.JOB_LOCAL_SETUP); this.taskExecutor = new TaskExecutor(jobProps); this.taskStateTracker = new LocalTaskStateTracker(jobProps, this.jobContext.getJobState(), this.taskExecutor, this.eventBus); this.serviceManager = new ServiceManager(Lists.newArrayList( // The order matters due to dependencies between services this.taskExecutor, this.taskStateTracker)); // Start all dependent services this.serviceManager.startAsync().awaitHealthy(5, TimeUnit.SECONDS); startCancellationExecutor(); jobLocalSetupTimer.stop(); }
/** * This covers a bug where if a listener was installed that would stop the manager if any service * fails and something failed during startup before service.start was called on all the services, * then awaitStopped would deadlock due to an IllegalStateException that was thrown when trying to * stop the timer(!). */ public void testFailStart_stopOthers() throws TimeoutException { Service a = new FailStartService(); Service b = new NoOpService(); final ServiceManager manager = new ServiceManager(asList(a, b)); manager.addListener( new Listener() { @Override public void failure(Service service) { manager.stopAsync(); } }); manager.startAsync(); manager.awaitStopped(10, TimeUnit.MILLISECONDS); }
public void testNotifyStoppedAfterFailure() throws TimeoutException { Service a = new AbstractService() { @Override protected void doStart() { notifyFailed(new IllegalStateException("start failure")); notifyStopped(); // This will be a no-op. } @Override protected void doStop() { notifyStopped(); } }; final ServiceManager manager = new ServiceManager(asList(a)); manager.startAsync(); manager.awaitStopped(10, TimeUnit.MILLISECONDS); assertThat(manager.servicesByState().keySet()).containsExactly(Service.State.FAILED); }
serviceManager.addListener(serviceManagerListener); try { serviceManager.startAsync().awaitHealthy(); } catch (Exception e) { try { serviceManager.stopAsync().awaitStopped(configuration.getShutdownTimeout(), TimeUnit.MILLISECONDS); } catch (TimeoutException timeoutException) { LOG.error("Unable to shutdown properly on time. {}", serviceManager.servicesByState()); System.exit(-1); LOG.info("Services started, startup times in ms: {}", serviceManager.startupTimes());
public void testServiceStartupTimes() { Service a = new NoOpDelayedService(150); Service b = new NoOpDelayedService(353); ServiceManager serviceManager = new ServiceManager(asList(a, b)); serviceManager.startAsync().awaitHealthy(); ImmutableMap<Service, Long> startupTimes = serviceManager.startupTimes(); assertEquals(2, startupTimes.size()); // TODO(kak): Use assertThat(startupTimes.get(a)).isAtLeast(150); assertTrue(startupTimes.get(a) >= 150); // TODO(kak): Use assertThat(startupTimes.get(b)).isAtLeast(353); assertTrue(startupTimes.get(b) >= 353); }
new ServiceManager(Arrays.asList(failRunService, new NoOpService())); manager.addListener( new ServiceManager.Listener() { @Override manager.startAsync(); afterStarted.countDown(); assertFalse("State should be updated before calling listeners", manager.isHealthy());
final ServiceManager manager = new ServiceManager(services); manager.addListener(new ServiceManager.Listener() { @Override public void healthy() { manager.startAsync();
public static void main(String... args) { Logging.dispatcher().register(new TracingLogEventListener()); Injector injector = Guice.createInjector(stage(), loadModule(args[0])); final ServiceManager manager = ServiceManagers.forServiceSet(injector); Runtime.getRuntime().addShutdownHook(new Thread("shutdown") { @Override public void run() { try { manager.stopAsync().awaitStopped(2, TimeUnit.SECONDS); } catch (TimeoutException timeout) { timeout.printStackTrace(); } } }); manager.addListener(new Listener() { @Override public void failure(Service service) { System.err.println(Joiner.on('\n').withKeyValueSeparator(":").join(manager.servicesByState().entries())); System.exit(1); } }, MoreExecutors.directExecutor()); manager.startAsync().awaitHealthy(); manager.awaitStopped(); }
@Override public ServiceManager get() { LOG.debug("Using services: {}", services); return new ServiceManager(services); } }
this.serviceManager = Optional.of(new ServiceManager(services)); this.serviceManager.get().startAsync();
/** * Start this {@link GobblinTaskRunner} instance. */ public void start() { logger.info(String.format("Starting %s in container %s", this.helixInstanceName, this.taskRunnerId)); // Add a shutdown hook so the task scheduler gets properly shutdown addShutdownHook(); connectHelixManager(); addInstanceTags(); // Start metric reporting if (this.containerMetrics.isPresent()) { this.containerMetrics.get() .startMetricReportingWithFileSuffix(ConfigUtils.configToState(this.config), this.taskRunnerId); } if (this.serviceManager != null) { this.serviceManager.startAsync(); this.serviceManager.awaitStopped(); } }
@Override protected void doStart() { printBanner(); this.serviceManager.addListener(new ServerStartListener(this)); this.serviceManager.startAsync().awaitHealthy(); if (stopwatch == null) { LOG.info("Started Styx server"); } else { LOG.info("Started Styx server in {} ms", stopwatch.elapsed(MILLISECONDS)); } }
/** * Create a service manager from the present data. This should be done only once. */ ServiceManager makeServiceManager() { assert !done; done = true; ServiceManager serviceManager = new ServiceManager(services); serviceManager.addListener(new ServiceManagerListener(), MoreExecutors.directExecutor()); for (ServiceManager.Listener listener : serviceManagerListeners) { serviceManager.addListener(listener, MoreExecutors.directExecutor()); } for (Service service : services) { service.addListener(new LoggingServiceListener(service), MoreExecutors.directExecutor()); for (Service.Listener listener : serviceListeners) { service.addListener(listener, MoreExecutors.directExecutor()); } } return serviceManager; } }
public void testToString() throws Exception { Service a = new NoOpService(); Service b = new FailStartService(); ServiceManager manager = new ServiceManager(asList(a, b)); String toString = manager.toString(); assertThat(toString).contains("NoOpService"); assertThat(toString).contains("FailStartService"); }
static void startServices(final ServiceManager manager) { manager.addListener(new ServiceManager.Listener() { public void stopped() {} public void healthy() {} public void failure(Service service) { logger.error("Failure in " + service.toString() + ". Shutting down.", service.failureCause()); System.exit(1); } }, MoreExecutors.sameThreadExecutor()); Runtime.getRuntime().addShutdownHook(new Thread() { public void run() { // Give the services 5 seconds to stop to ensure that we are responsive to shutdown // requests. try { manager.stopAsync().awaitStopped(5, TimeUnit.SECONDS); } catch (TimeoutException timeout) { // stopping timed out } } }); manager.startAsync(); // start all the services asynchronously } }