001/* 002 * Copyright (C) 2012 The Guava Authors 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except 005 * in compliance with the License. You may obtain a copy of the License at 006 * 007 * http://www.apache.org/licenses/LICENSE-2.0 008 * 009 * Unless required by applicable law or agreed to in writing, software distributed under the License 010 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express 011 * or implied. See the License for the specific language governing permissions and limitations under 012 * the License. 013 */ 014 015package com.google.common.util.concurrent; 016 017import static com.google.common.base.Preconditions.checkArgument; 018import static com.google.common.base.Preconditions.checkNotNull; 019import static com.google.common.base.Preconditions.checkState; 020import static com.google.common.base.Predicates.equalTo; 021import static com.google.common.base.Predicates.in; 022import static com.google.common.base.Predicates.instanceOf; 023import static com.google.common.base.Predicates.not; 024import static com.google.common.util.concurrent.Internal.toNanosSaturated; 025import static com.google.common.util.concurrent.MoreExecutors.directExecutor; 026import static com.google.common.util.concurrent.Service.State.FAILED; 027import static com.google.common.util.concurrent.Service.State.NEW; 028import static com.google.common.util.concurrent.Service.State.RUNNING; 029import static com.google.common.util.concurrent.Service.State.STARTING; 030import static com.google.common.util.concurrent.Service.State.STOPPING; 031import static com.google.common.util.concurrent.Service.State.TERMINATED; 032import static java.util.Collections.sort; 033import static java.util.concurrent.TimeUnit.MILLISECONDS; 034 035import com.google.common.annotations.GwtIncompatible; 036import com.google.common.annotations.J2ktIncompatible; 037import com.google.common.base.MoreObjects; 038import com.google.common.base.Stopwatch; 039import com.google.common.collect.Collections2; 040import com.google.common.collect.ImmutableCollection; 041import com.google.common.collect.ImmutableList; 042import com.google.common.collect.ImmutableMap; 043import com.google.common.collect.ImmutableSet; 044import com.google.common.collect.ImmutableSetMultimap; 045import com.google.common.collect.Lists; 046import com.google.common.collect.Maps; 047import com.google.common.collect.MultimapBuilder; 048import com.google.common.collect.Multimaps; 049import com.google.common.collect.Multiset; 050import com.google.common.collect.Ordering; 051import com.google.common.collect.SetMultimap; 052import com.google.common.util.concurrent.Service.State; 053import com.google.errorprone.annotations.CanIgnoreReturnValue; 054import com.google.errorprone.annotations.concurrent.GuardedBy; 055import com.google.j2objc.annotations.J2ObjCIncompatible; 056import com.google.j2objc.annotations.WeakOuter; 057import java.lang.ref.WeakReference; 058import java.time.Duration; 059import java.util.EnumSet; 060import java.util.IdentityHashMap; 061import java.util.List; 062import java.util.Map.Entry; 063import java.util.concurrent.Executor; 064import java.util.concurrent.TimeUnit; 065import java.util.concurrent.TimeoutException; 066import java.util.logging.Level; 067 068/** 069 * A manager for monitoring and controlling a set of {@linkplain Service services}. This class 070 * provides methods for {@linkplain #startAsync() starting}, {@linkplain #stopAsync() stopping} and 071 * {@linkplain #servicesByState inspecting} a collection of {@linkplain Service services}. 072 * Additionally, users can monitor state transitions with the {@linkplain Listener listener} 073 * mechanism. 074 * 075 * <p>While it is recommended that service lifecycles be managed via this class, state transitions 076 * initiated via other mechanisms do not impact the correctness of its methods. For example, if the 077 * services are started by some mechanism besides {@link #startAsync}, the listeners will be invoked 078 * when appropriate and {@link #awaitHealthy} will still work as expected. 079 * 080 * <p>Here is a simple example of how to use a {@code ServiceManager} to start a server. 081 * 082 * <pre>{@code 083 * class Server { 084 * public static void main(String[] args) { 085 * Set<Service> services = ...; 086 * ServiceManager manager = new ServiceManager(services); 087 * manager.addListener(new Listener() { 088 * public void stopped() {} 089 * public void healthy() { 090 * // Services have been initialized and are healthy, start accepting requests... 091 * } 092 * public void failure(Service service) { 093 * // Something failed, at this point we could log it, notify a load balancer, or take 094 * // some other action. For now we will just exit. 095 * System.exit(1); 096 * } 097 * }, 098 * MoreExecutors.directExecutor()); 099 * 100 * Runtime.getRuntime().addShutdownHook(new Thread() { 101 * public void run() { 102 * // Give the services 5 seconds to stop to ensure that we are responsive to shutdown 103 * // requests. 104 * try { 105 * manager.stopAsync().awaitStopped(5, TimeUnit.SECONDS); 106 * } catch (TimeoutException timeout) { 107 * // stopping timed out 108 * } 109 * } 110 * }); 111 * manager.startAsync(); // start all the services asynchronously 112 * } 113 * } 114 * }</pre> 115 * 116 * <p>This class uses the ServiceManager's methods to start all of its services, to respond to 117 * service failure and to ensure that when the JVM is shutting down all the services are stopped. 118 * 119 * @author Luke Sandberg 120 * @since 14.0 121 */ 122@J2ktIncompatible 123@GwtIncompatible 124public final class ServiceManager implements ServiceManagerBridge { 125 private static final LazyLogger logger = new LazyLogger(ServiceManager.class); 126 private static final ListenerCallQueue.Event<Listener> HEALTHY_EVENT = 127 new ListenerCallQueue.Event<Listener>() { 128 @Override 129 public void call(Listener listener) { 130 listener.healthy(); 131 } 132 133 @Override 134 public String toString() { 135 return "healthy()"; 136 } 137 }; 138 private static final ListenerCallQueue.Event<Listener> STOPPED_EVENT = 139 new ListenerCallQueue.Event<Listener>() { 140 @Override 141 public void call(Listener listener) { 142 listener.stopped(); 143 } 144 145 @Override 146 public String toString() { 147 return "stopped()"; 148 } 149 }; 150 151 /** 152 * A listener for the aggregate state changes of the services that are under management. Users 153 * that need to listen to more fine-grained events (such as when each particular {@linkplain 154 * Service service} starts, or terminates), should attach {@linkplain Service.Listener service 155 * listeners} to each individual service. 156 * 157 * @author Luke Sandberg 158 * @since 15.0 (present as an interface in 14.0) 159 */ 160 public abstract static class Listener { 161 /** Constructor for use by subclasses. */ 162 public Listener() {} 163 164 /** 165 * Called when the service initially becomes healthy. 166 * 167 * <p>This will be called at most once after all the services have entered the {@linkplain 168 * State#RUNNING running} state. If any services fail during start up or {@linkplain 169 * State#FAILED fail}/{@linkplain State#TERMINATED terminate} before all other services have 170 * started {@linkplain State#RUNNING running} then this method will not be called. 171 */ 172 public void healthy() {} 173 174 /** 175 * Called when the all of the component services have reached a terminal state, either 176 * {@linkplain State#TERMINATED terminated} or {@linkplain State#FAILED failed}. 177 */ 178 public void stopped() {} 179 180 /** 181 * Called when a component service has {@linkplain State#FAILED failed}. 182 * 183 * @param service The service that failed. 184 */ 185 public void failure(Service service) {} 186 } 187 188 /** 189 * An encapsulation of all of the state that is accessed by the {@linkplain ServiceListener 190 * service listeners}. This is extracted into its own object so that {@link ServiceListener} could 191 * be made {@code static} and its instances can be safely constructed and added in the {@link 192 * ServiceManager} constructor without having to close over the partially constructed {@link 193 * ServiceManager} instance (i.e. avoid leaking a pointer to {@code this}). 194 */ 195 private final ServiceManagerState state; 196 197 private final ImmutableList<Service> services; 198 199 /** 200 * Constructs a new instance for managing the given services. 201 * 202 * @param services The services to manage 203 * @throws IllegalArgumentException if not all services are {@linkplain State#NEW new} or if there 204 * are any duplicate services. 205 */ 206 public ServiceManager(Iterable<? extends Service> services) { 207 ImmutableList<Service> copy = ImmutableList.copyOf(services); 208 if (copy.isEmpty()) { 209 // Having no services causes the manager to behave strangely. Notably, listeners are never 210 // fired. To avoid this we substitute a placeholder service. 211 logger 212 .get() 213 .log( 214 Level.WARNING, 215 "ServiceManager configured with no services. Is your application configured" 216 + " properly?", 217 new EmptyServiceManagerWarning()); 218 copy = ImmutableList.<Service>of(new NoOpService()); 219 } 220 this.state = new ServiceManagerState(copy); 221 this.services = copy; 222 WeakReference<ServiceManagerState> stateReference = new WeakReference<>(state); 223 for (Service service : copy) { 224 service.addListener(new ServiceListener(service, stateReference), directExecutor()); 225 // We check the state after adding the listener as a way to ensure that our listener was added 226 // to a NEW service. 227 checkArgument(service.state() == NEW, "Can only manage NEW services, %s", service); 228 } 229 // We have installed all of our listeners and after this point any state transition should be 230 // correct. 231 this.state.markReady(); 232 } 233 234 /** 235 * Registers a {@link Listener} to be {@linkplain Executor#execute executed} on the given 236 * executor. The listener will not have previous state changes replayed, so it is suggested that 237 * listeners are added before any of the managed services are {@linkplain Service#startAsync 238 * started}. 239 * 240 * <p>{@code addListener} guarantees execution ordering across calls to a given listener but not 241 * across calls to multiple listeners. Specifically, a given listener will have its callbacks 242 * invoked in the same order as the underlying service enters those states. Additionally, at most 243 * one of the listener's callbacks will execute at once. However, multiple listeners' callbacks 244 * may execute concurrently, and listeners may execute in an order different from the one in which 245 * they were registered. 246 * 247 * <p>RuntimeExceptions thrown by a listener will be caught and logged. Any exception thrown 248 * during {@code Executor.execute} (e.g., a {@code RejectedExecutionException}) will be caught and 249 * logged. 250 * 251 * <p>When selecting an executor, note that {@code directExecutor} is dangerous in some cases. See 252 * the discussion in the {@link ListenableFuture#addListener ListenableFuture.addListener} 253 * documentation. 254 * 255 * @param listener the listener to run when the manager changes state 256 * @param executor the executor in which the listeners callback methods will be run. 257 */ 258 public void addListener(Listener listener, Executor executor) { 259 state.addListener(listener, executor); 260 } 261 262 /** 263 * Initiates service {@linkplain Service#startAsync startup} on all the services being managed. It 264 * is only valid to call this method if all of the services are {@linkplain State#NEW new}. 265 * 266 * @return this 267 * @throws IllegalStateException if any of the Services are not {@link State#NEW new} when the 268 * method is called. 269 */ 270 @CanIgnoreReturnValue 271 public ServiceManager startAsync() { 272 for (Service service : services) { 273 checkState(service.state() == NEW, "Not all services are NEW, cannot start %s", this); 274 } 275 for (Service service : services) { 276 try { 277 state.tryStartTiming(service); 278 service.startAsync(); 279 } catch (IllegalStateException e) { 280 // This can happen if the service has already been started or stopped (e.g. by another 281 // service or listener). Our contract says it is safe to call this method if 282 // all services were NEW when it was called, and this has already been verified above, so we 283 // don't propagate the exception. 284 logger.get().log(Level.WARNING, "Unable to start Service " + service, e); 285 } 286 } 287 return this; 288 } 289 290 /** 291 * Waits for the {@link ServiceManager} to become {@linkplain #isHealthy() healthy}. The manager 292 * will become healthy after all the component services have reached the {@linkplain State#RUNNING 293 * running} state. 294 * 295 * @throws IllegalStateException if the service manager reaches a state from which it cannot 296 * become {@linkplain #isHealthy() healthy}. 297 */ 298 public void awaitHealthy() { 299 state.awaitHealthy(); 300 } 301 302 /** 303 * Waits for the {@link ServiceManager} to become {@linkplain #isHealthy() healthy} for no more 304 * than the given time. The manager will become healthy after all the component services have 305 * reached the {@linkplain State#RUNNING running} state. 306 * 307 * @param timeout the maximum time to wait 308 * @throws TimeoutException if not all of the services have finished starting within the deadline 309 * @throws IllegalStateException if the service manager reaches a state from which it cannot 310 * become {@linkplain #isHealthy() healthy}. 311 * @since 33.4.0 (but since 28.0 in the JRE flavor) 312 */ 313 @SuppressWarnings("Java7ApiChecker") 314 @IgnoreJRERequirement // Users will use this only if they're already using Duration. 315 public void awaitHealthy(Duration timeout) throws TimeoutException { 316 awaitHealthy(toNanosSaturated(timeout), TimeUnit.NANOSECONDS); 317 } 318 319 /** 320 * Waits for the {@link ServiceManager} to become {@linkplain #isHealthy() healthy} for no more 321 * than the given time. The manager will become healthy after all the component services have 322 * reached the {@linkplain State#RUNNING running} state. 323 * 324 * @param timeout the maximum time to wait 325 * @param unit the time unit of the timeout argument 326 * @throws TimeoutException if not all of the services have finished starting within the deadline 327 * @throws IllegalStateException if the service manager reaches a state from which it cannot 328 * become {@linkplain #isHealthy() healthy}. 329 */ 330 @SuppressWarnings("GoodTime") // should accept a java.time.Duration 331 public void awaitHealthy(long timeout, TimeUnit unit) throws TimeoutException { 332 state.awaitHealthy(timeout, unit); 333 } 334 335 /** 336 * Initiates service {@linkplain Service#stopAsync shutdown} if necessary on all the services 337 * being managed. 338 * 339 * @return this 340 */ 341 @CanIgnoreReturnValue 342 public ServiceManager stopAsync() { 343 for (Service service : services) { 344 service.stopAsync(); 345 } 346 return this; 347 } 348 349 /** 350 * Waits for the all the services to reach a terminal state. After this method returns all 351 * services will either be {@linkplain Service.State#TERMINATED terminated} or {@linkplain 352 * Service.State#FAILED failed}. 353 */ 354 public void awaitStopped() { 355 state.awaitStopped(); 356 } 357 358 /** 359 * Waits for the all the services to reach a terminal state for no more than the given time. After 360 * this method returns all services will either be {@linkplain Service.State#TERMINATED 361 * terminated} or {@linkplain Service.State#FAILED failed}. 362 * 363 * @param timeout the maximum time to wait 364 * @throws TimeoutException if not all of the services have stopped within the deadline 365 * @since 33.4.0 (but since 28.0 in the JRE flavor) 366 */ 367 @SuppressWarnings("Java7ApiChecker") 368 @IgnoreJRERequirement // Users will use this only if they're already using Duration. 369 public void awaitStopped(Duration timeout) throws TimeoutException { 370 awaitStopped(toNanosSaturated(timeout), TimeUnit.NANOSECONDS); 371 } 372 373 /** 374 * Waits for the all the services to reach a terminal state for no more than the given time. After 375 * this method returns all services will either be {@linkplain Service.State#TERMINATED 376 * terminated} or {@linkplain Service.State#FAILED failed}. 377 * 378 * @param timeout the maximum time to wait 379 * @param unit the time unit of the timeout argument 380 * @throws TimeoutException if not all of the services have stopped within the deadline 381 */ 382 @SuppressWarnings("GoodTime") // should accept a java.time.Duration 383 public void awaitStopped(long timeout, TimeUnit unit) throws TimeoutException { 384 state.awaitStopped(timeout, unit); 385 } 386 387 /** 388 * Returns true if all services are currently in the {@linkplain State#RUNNING running} state. 389 * 390 * <p>Users who want more detailed information should use the {@link #servicesByState} method to 391 * get detailed information about which services are not running. 392 */ 393 public boolean isHealthy() { 394 for (Service service : services) { 395 if (!service.isRunning()) { 396 return false; 397 } 398 } 399 return true; 400 } 401 402 /** 403 * Provides a snapshot of the current state of all the services under management. 404 * 405 * <p>N.B. This snapshot is guaranteed to be consistent, i.e. the set of states returned will 406 * correspond to a point in time view of the services. 407 * 408 * @since 29.0 (present with return type {@code ImmutableMultimap} since 14.0) 409 */ 410 @Override 411 public ImmutableSetMultimap<State, Service> servicesByState() { 412 return state.servicesByState(); 413 } 414 415 /** 416 * Returns the service load times. This value will only return startup times for services that 417 * have finished starting. 418 * 419 * @return Map of services and their corresponding startup time in millis, the map entries will be 420 * ordered by startup time. 421 */ 422 public ImmutableMap<Service, Long> startupTimes() { 423 return state.startupTimes(); 424 } 425 426 /** 427 * Returns the service load times. This value will only return startup times for services that 428 * have finished starting. 429 * 430 * @return Map of services and their corresponding startup time, the map entries will be ordered 431 * by startup time. 432 * @since 33.4.0 (but since 31.0 in the JRE flavor) 433 */ 434 @J2ObjCIncompatible 435 @SuppressWarnings("Java7ApiChecker") 436 // If users use this when they shouldn't, we hope that NewApi will catch subsequent Duration calls 437 @IgnoreJRERequirement 438 public ImmutableMap<Service, Duration> startupDurations() { 439 return ImmutableMap.copyOf( 440 Maps.<Service, Long, Duration>transformValues(startupTimes(), Duration::ofMillis)); 441 } 442 443 @Override 444 public String toString() { 445 return MoreObjects.toStringHelper(ServiceManager.class) 446 .add("services", Collections2.filter(services, not(instanceOf(NoOpService.class)))) 447 .toString(); 448 } 449 450 /** 451 * An encapsulation of all the mutable state of the {@link ServiceManager} that needs to be 452 * accessed by instances of {@link ServiceListener}. 453 */ 454 private static final class ServiceManagerState { 455 final Monitor monitor = new Monitor(); 456 457 @GuardedBy("monitor") 458 final SetMultimap<State, Service> servicesByState = 459 MultimapBuilder.enumKeys(State.class).linkedHashSetValues().build(); 460 461 @GuardedBy("monitor") 462 final Multiset<State> states = servicesByState.keys(); 463 464 @GuardedBy("monitor") 465 final IdentityHashMap<Service, Stopwatch> startupTimers = new IdentityHashMap<>(); 466 467 /** 468 * These two booleans are used to mark the state as ready to start. 469 * 470 * <p>{@link #ready}: is set by {@link #markReady} to indicate that all listeners have been 471 * correctly installed 472 * 473 * <p>{@link #transitioned}: is set by {@link #transitionService} to indicate that some 474 * transition has been performed. 475 * 476 * <p>Together, they allow us to enforce that all services have their listeners installed prior 477 * to any service performing a transition, then we can fail in the ServiceManager constructor 478 * rather than in a Service.Listener callback. 479 */ 480 @GuardedBy("monitor") 481 boolean ready; 482 483 @GuardedBy("monitor") 484 boolean transitioned; 485 486 final int numberOfServices; 487 488 /** 489 * Controls how long to wait for all the services to either become healthy or reach a state from 490 * which it is guaranteed that it can never become healthy. 491 */ 492 final Monitor.Guard awaitHealthGuard = new AwaitHealthGuard(); 493 494 @WeakOuter 495 final class AwaitHealthGuard extends Monitor.Guard { 496 AwaitHealthGuard() { 497 super(ServiceManagerState.this.monitor); 498 } 499 500 @Override 501 @GuardedBy("ServiceManagerState.this.monitor") 502 public boolean isSatisfied() { 503 // All services have started or some service has terminated/failed. 504 return states.count(RUNNING) == numberOfServices 505 || states.contains(STOPPING) 506 || states.contains(TERMINATED) 507 || states.contains(FAILED); 508 } 509 } 510 511 /** Controls how long to wait for all services to reach a terminal state. */ 512 final Monitor.Guard stoppedGuard = new StoppedGuard(); 513 514 @WeakOuter 515 final class StoppedGuard extends Monitor.Guard { 516 StoppedGuard() { 517 super(ServiceManagerState.this.monitor); 518 } 519 520 @Override 521 @GuardedBy("ServiceManagerState.this.monitor") 522 public boolean isSatisfied() { 523 return states.count(TERMINATED) + states.count(FAILED) == numberOfServices; 524 } 525 } 526 527 /** The listeners to notify during a state transition. */ 528 final ListenerCallQueue<Listener> listeners = new ListenerCallQueue<>(); 529 530 /** 531 * It is implicitly assumed that all the services are NEW and that they will all remain NEW 532 * until all the Listeners are installed and {@link #markReady()} is called. It is our caller's 533 * responsibility to only call {@link #markReady()} if all services were new at the time this 534 * method was called and when all the listeners were installed. 535 */ 536 ServiceManagerState(ImmutableCollection<Service> services) { 537 this.numberOfServices = services.size(); 538 servicesByState.putAll(NEW, services); 539 } 540 541 /** 542 * Attempts to start the timer immediately prior to the service being started via {@link 543 * Service#startAsync()}. 544 */ 545 void tryStartTiming(Service service) { 546 monitor.enter(); 547 try { 548 Stopwatch stopwatch = startupTimers.get(service); 549 if (stopwatch == null) { 550 startupTimers.put(service, Stopwatch.createStarted()); 551 } 552 } finally { 553 monitor.leave(); 554 } 555 } 556 557 /** 558 * Marks the {@link State} as ready to receive transitions. Returns true if no transitions have 559 * been observed yet. 560 */ 561 void markReady() { 562 monitor.enter(); 563 try { 564 if (!transitioned) { 565 // nothing has transitioned since construction, good. 566 ready = true; 567 } else { 568 // This should be an extremely rare race condition. 569 List<Service> servicesInBadStates = Lists.newArrayList(); 570 for (Service service : servicesByState().values()) { 571 if (service.state() != NEW) { 572 servicesInBadStates.add(service); 573 } 574 } 575 throw new IllegalArgumentException( 576 "Services started transitioning asynchronously before " 577 + "the ServiceManager was constructed: " 578 + servicesInBadStates); 579 } 580 } finally { 581 monitor.leave(); 582 } 583 } 584 585 void addListener(Listener listener, Executor executor) { 586 listeners.addListener(listener, executor); 587 } 588 589 void awaitHealthy() { 590 monitor.enterWhenUninterruptibly(awaitHealthGuard); 591 try { 592 checkHealthy(); 593 } finally { 594 monitor.leave(); 595 } 596 } 597 598 void awaitHealthy(long timeout, TimeUnit unit) throws TimeoutException { 599 monitor.enter(); 600 try { 601 if (!monitor.waitForUninterruptibly(awaitHealthGuard, timeout, unit)) { 602 throw new TimeoutException( 603 "Timeout waiting for the services to become healthy. The " 604 + "following services have not started: " 605 + Multimaps.filterKeys(servicesByState, in(ImmutableSet.of(NEW, STARTING)))); 606 } 607 checkHealthy(); 608 } finally { 609 monitor.leave(); 610 } 611 } 612 613 void awaitStopped() { 614 monitor.enterWhenUninterruptibly(stoppedGuard); 615 monitor.leave(); 616 } 617 618 void awaitStopped(long timeout, TimeUnit unit) throws TimeoutException { 619 monitor.enter(); 620 try { 621 if (!monitor.waitForUninterruptibly(stoppedGuard, timeout, unit)) { 622 throw new TimeoutException( 623 "Timeout waiting for the services to stop. The following " 624 + "services have not stopped: " 625 + Multimaps.filterKeys(servicesByState, not(in(EnumSet.of(TERMINATED, FAILED))))); 626 } 627 } finally { 628 monitor.leave(); 629 } 630 } 631 632 ImmutableSetMultimap<State, Service> servicesByState() { 633 ImmutableSetMultimap.Builder<State, Service> builder = ImmutableSetMultimap.builder(); 634 monitor.enter(); 635 try { 636 for (Entry<State, Service> entry : servicesByState.entries()) { 637 if (!(entry.getValue() instanceof NoOpService)) { 638 builder.put(entry); 639 } 640 } 641 } finally { 642 monitor.leave(); 643 } 644 return builder.build(); 645 } 646 647 ImmutableMap<Service, Long> startupTimes() { 648 List<Entry<Service, Long>> loadTimes; 649 monitor.enter(); 650 try { 651 loadTimes = Lists.newArrayListWithCapacity(startupTimers.size()); 652 // N.B. There will only be an entry in the map if the service has started 653 for (Entry<Service, Stopwatch> entry : startupTimers.entrySet()) { 654 Service service = entry.getKey(); 655 Stopwatch stopwatch = entry.getValue(); 656 if (!stopwatch.isRunning() && !(service instanceof NoOpService)) { 657 loadTimes.add(Maps.immutableEntry(service, stopwatch.elapsed(MILLISECONDS))); 658 } 659 } 660 } finally { 661 monitor.leave(); 662 } 663 sort(loadTimes, Ordering.natural().onResultOf(Entry::getValue)); 664 return ImmutableMap.copyOf(loadTimes); 665 } 666 667 /** 668 * Updates the state with the given service transition. 669 * 670 * <p>This method performs the main logic of ServiceManager in the following steps. 671 * 672 * <ol> 673 * <li>Update the {@link #servicesByState()} 674 * <li>Update the {@link #startupTimers} 675 * <li>Based on the new state queue listeners to run 676 * <li>Run the listeners (outside of the lock) 677 * </ol> 678 */ 679 void transitionService(Service service, State from, State to) { 680 checkNotNull(service); 681 checkArgument(from != to); 682 monitor.enter(); 683 try { 684 transitioned = true; 685 if (!ready) { 686 return; 687 } 688 // Update state. 689 checkState( 690 servicesByState.remove(from, service), 691 "Service %s not at the expected location in the state map %s", 692 service, 693 from); 694 checkState( 695 servicesByState.put(to, service), 696 "Service %s in the state map unexpectedly at %s", 697 service, 698 to); 699 // Update the timer 700 Stopwatch stopwatch = startupTimers.get(service); 701 if (stopwatch == null) { 702 // This means the service was started by some means other than ServiceManager.startAsync 703 stopwatch = Stopwatch.createStarted(); 704 startupTimers.put(service, stopwatch); 705 } 706 if (to.compareTo(RUNNING) >= 0 && stopwatch.isRunning()) { 707 // N.B. if we miss the STARTING event then we may never record a startup time. 708 stopwatch.stop(); 709 if (!(service instanceof NoOpService)) { 710 logger.get().log(Level.FINE, "Started {0} in {1}.", new Object[] {service, stopwatch}); 711 } 712 } 713 // Queue our listeners 714 715 // Did a service fail? 716 if (to == FAILED) { 717 enqueueFailedEvent(service); 718 } 719 720 if (states.count(RUNNING) == numberOfServices) { 721 // This means that the manager is currently healthy. N.B. If other threads call isHealthy 722 // they are not guaranteed to get 'true', because any service could fail right now. 723 enqueueHealthyEvent(); 724 } else if (states.count(TERMINATED) + states.count(FAILED) == numberOfServices) { 725 enqueueStoppedEvent(); 726 } 727 } finally { 728 monitor.leave(); 729 // Run our executors outside of the lock 730 dispatchListenerEvents(); 731 } 732 } 733 734 void enqueueStoppedEvent() { 735 listeners.enqueue(STOPPED_EVENT); 736 } 737 738 void enqueueHealthyEvent() { 739 listeners.enqueue(HEALTHY_EVENT); 740 } 741 742 void enqueueFailedEvent(Service service) { 743 listeners.enqueue( 744 new ListenerCallQueue.Event<Listener>() { 745 @Override 746 public void call(Listener listener) { 747 listener.failure(service); 748 } 749 750 @Override 751 public String toString() { 752 return "failed({service=" + service + "})"; 753 } 754 }); 755 } 756 757 /** Attempts to execute all the listeners in {@link #listeners}. */ 758 void dispatchListenerEvents() { 759 checkState( 760 !monitor.isOccupiedByCurrentThread(), 761 "It is incorrect to execute listeners with the monitor held."); 762 listeners.dispatch(); 763 } 764 765 @GuardedBy("monitor") 766 void checkHealthy() { 767 if (states.count(RUNNING) != numberOfServices) { 768 IllegalStateException exception = 769 new IllegalStateException( 770 "Expected to be healthy after starting. The following services are not running: " 771 + Multimaps.filterKeys(servicesByState, not(equalTo(RUNNING)))); 772 for (Service service : servicesByState.get(State.FAILED)) { 773 exception.addSuppressed(new FailedService(service)); 774 } 775 throw exception; 776 } 777 } 778 } 779 780 /** 781 * A {@link Service} that wraps another service and times how long it takes for it to start and 782 * also calls the {@link ServiceManagerState#transitionService(Service, State, State)}, to record 783 * the state transitions. 784 */ 785 private static final class ServiceListener extends Service.Listener { 786 final Service service; 787 // We store the state in a weak reference to ensure that if something went wrong while 788 // constructing the ServiceManager we don't pointlessly keep updating the state. 789 final WeakReference<ServiceManagerState> state; 790 791 ServiceListener(Service service, WeakReference<ServiceManagerState> state) { 792 this.service = service; 793 this.state = state; 794 } 795 796 @Override 797 public void starting() { 798 ServiceManagerState state = this.state.get(); 799 if (state != null) { 800 state.transitionService(service, NEW, STARTING); 801 if (!(service instanceof NoOpService)) { 802 logger.get().log(Level.FINE, "Starting {0}.", service); 803 } 804 } 805 } 806 807 @Override 808 public void running() { 809 ServiceManagerState state = this.state.get(); 810 if (state != null) { 811 state.transitionService(service, STARTING, RUNNING); 812 } 813 } 814 815 @Override 816 public void stopping(State from) { 817 ServiceManagerState state = this.state.get(); 818 if (state != null) { 819 state.transitionService(service, from, STOPPING); 820 } 821 } 822 823 @Override 824 public void terminated(State from) { 825 ServiceManagerState state = this.state.get(); 826 if (state != null) { 827 if (!(service instanceof NoOpService)) { 828 logger 829 .get() 830 .log( 831 Level.FINE, 832 "Service {0} has terminated. Previous state was: {1}", 833 new Object[] {service, from}); 834 } 835 state.transitionService(service, from, TERMINATED); 836 } 837 } 838 839 @Override 840 public void failed(State from, Throwable failure) { 841 ServiceManagerState state = this.state.get(); 842 if (state != null) { 843 // Log before the transition, so that if the process exits in response to server failure, 844 // there is a higher likelihood that the cause will be in the logs. 845 boolean log = !(service instanceof NoOpService); 846 /* 847 * We have already exposed startup exceptions to the user in the form of suppressed 848 * exceptions. We don't need to log those exceptions again. 849 */ 850 log &= from != State.STARTING; 851 if (log) { 852 logger 853 .get() 854 .log( 855 Level.SEVERE, 856 "Service " + service + " has failed in the " + from + " state.", 857 failure); 858 } 859 state.transitionService(service, from, FAILED); 860 } 861 } 862 } 863 864 /** 865 * A {@link Service} instance that does nothing. This is only useful as a placeholder to ensure 866 * that the {@link ServiceManager} functions properly even when it is managing no services. 867 * 868 * <p>The use of this class is considered an implementation detail of ServiceManager and as such 869 * it is excluded from {@link #servicesByState}, {@link #startupTimes}, {@link #toString} and all 870 * logging statements. 871 */ 872 private static final class NoOpService extends AbstractService { 873 @Override 874 protected void doStart() { 875 notifyStarted(); 876 } 877 878 @Override 879 protected void doStop() { 880 notifyStopped(); 881 } 882 } 883 884 /** This is never thrown but only used for logging. */ 885 private static final class EmptyServiceManagerWarning extends Throwable {} 886 887 private static final class FailedService extends Throwable { 888 FailedService(Service service) { 889 super( 890 service.toString(), 891 service.failureCause(), 892 false /* don't enable suppression */, 893 false /* don't calculate a stack trace. */); 894 } 895 } 896}