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