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