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