001    /*
002     * Copyright (C) 2011 The Guava Authors
003     *
004     * Licensed under the Apache License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of the License at
007     *
008     * http://www.apache.org/licenses/LICENSE-2.0
009     *
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     * See the License for the specific language governing permissions and
014     * limitations under the License.
015     */
016    
017    package com.google.common.util.concurrent;
018    
019    import com.google.common.annotations.Beta;
020    import com.google.common.base.Preconditions;
021    import com.google.common.base.Throwables;
022    
023    import java.util.concurrent.Callable;
024    import java.util.concurrent.Executor;
025    import java.util.concurrent.Executors;
026    import java.util.concurrent.Future;
027    import java.util.concurrent.ScheduledExecutorService;
028    import java.util.concurrent.TimeUnit;
029    import java.util.concurrent.locks.ReentrantLock;
030    import java.util.logging.Level;
031    import java.util.logging.Logger;
032    
033    import javax.annotation.concurrent.GuardedBy;
034    
035    /**
036     * Base class for services that can implement {@link #startUp} and {@link #shutDown} but while in
037     * the "running" state need to perform a periodic task.  Subclasses can implement {@link #startUp},
038     * {@link #shutDown} and also a {@link #runOneIteration} method that will be executed periodically.
039     *
040     * <p>This class uses the {@link ScheduledExecutorService} returned from {@link #executor} to run
041     * the {@link #startUp} and {@link #shutDown} methods and also uses that service to schedule the
042     * {@link #runOneIteration} that will be executed periodically as specified by its
043     * {@link Scheduler}. When this service is asked to stop via {@link #stop} or {@link #stopAndWait},
044     * it will cancel the periodic task (but not interrupt it) and wait for it to stop before running
045     * the {@link #shutDown} method.
046     *
047     * <p>Subclasses are guaranteed that the life cycle methods ({@link #runOneIteration}, {@link
048     * #startUp} and {@link #shutDown}) will never run concurrently. Notably, if any execution of {@link
049     * #runOneIteration} takes longer than its schedule defines, then subsequent executions may start
050     * late.  Also, all life cycle methods are executed with a lock held, so subclasses can safely
051     * modify shared state without additional synchronization necessary for visibility to later
052     * executions of the life cycle methods.
053     *
054     * <h3>Usage Example</h3>
055     *
056     * Here is a sketch of a service which crawls a website and uses the scheduling capabilities to
057     * rate limit itself. <pre> {@code
058     * class CrawlingService extends AbstractScheduledService {
059     *   private Set<Uri> visited;
060     *   private Queue<Uri> toCrawl;
061     *   protected void startUp() throws Exception {
062     *     toCrawl = readStartingUris();
063     *   }
064     *
065     *   protected void runOneIteration() throws Exception {
066     *     Uri uri = toCrawl.remove();
067     *     Collection<Uri> newUris = crawl(uri);
068     *     visited.add(uri);
069     *     for (Uri newUri : newUris) {
070     *       if (!visited.contains(newUri)) { toCrawl.add(newUri); }
071     *     }
072     *   }
073     *
074     *   protected void shutDown() throws Exception {
075     *     saveUris(toCrawl);
076     *   }
077     *
078     *   protected Scheduler scheduler() {
079     *     return Scheduler.newFixedRateSchedule(0, 1, TimeUnit.SECONDS);
080     *   }
081     * }}</pre>
082     *
083     * This class uses the life cycle methods to read in a list of starting URIs and save the set of
084     * outstanding URIs when shutting down.  Also, it takes advantage of the scheduling functionality to
085     * rate limit the number of queries we perform.
086     *
087     * @author Luke Sandberg
088     * @since 11.0
089     */
090    @Beta
091    public abstract class AbstractScheduledService implements Service {
092      private static final Logger logger = Logger.getLogger(AbstractScheduledService.class.getName());
093    
094      /**
095       * A scheduler defines the policy for how the {@link AbstractScheduledService} should run its
096       * task.
097       *
098       * <p>Consider using the {@link #newFixedDelaySchedule} and {@link #newFixedRateSchedule} factory
099       * methods, these provide {@link Scheduler} instances for the common use case of running the
100       * service with a fixed schedule.  If more flexibility is needed then consider subclassing the
101       * {@link CustomScheduler} abstract class in preference to creating your own {@link Scheduler}
102       * implementation.
103       *
104       * @author Luke Sandberg
105       * @since 11.0
106       */
107      public abstract static class Scheduler {
108        /**
109         * Returns a {@link Scheduler} that schedules the task using the
110         * {@link ScheduledExecutorService#scheduleWithFixedDelay} method.
111         *
112         * @param initialDelay the time to delay first execution
113         * @param delay the delay between the termination of one execution and the commencement of the
114         *        next
115         * @param unit the time unit of the initialDelay and delay parameters
116         */
117        public static Scheduler newFixedDelaySchedule(final long initialDelay, final long delay,
118            final TimeUnit unit) {
119          return new Scheduler() {
120            
121            @Override
122            public Future<?> schedule(AbstractService service, ScheduledExecutorService executor,
123                Runnable task) {
124              return executor.scheduleWithFixedDelay(task, initialDelay, delay, unit);
125            }
126          };
127        }
128    
129        /**
130         * Returns a {@link Scheduler} that schedules the task using the
131         * {@link ScheduledExecutorService#scheduleAtFixedRate} method.
132         *
133         * @param initialDelay the time to delay first execution
134         * @param period the period between successive executions of the task
135         * @param unit the time unit of the initialDelay and period parameters
136         */
137        public static Scheduler newFixedRateSchedule(final long initialDelay, final long period,
138            final TimeUnit unit) {
139          return new Scheduler() {
140            
141            @Override
142            public Future<?> schedule(AbstractService service, ScheduledExecutorService executor,
143                Runnable task) {
144              return executor.scheduleAtFixedRate(task, initialDelay, period, unit);
145            }
146          };
147        }
148    
149        /** Schedules the task to run on the provided executor on behalf of the service.  */
150        abstract Future<?> schedule(AbstractService service, ScheduledExecutorService executor,
151            Runnable runnable);
152    
153        private Scheduler() {}
154      }
155    
156      /* use AbstractService for state management */
157      private final AbstractService delegate = new AbstractService() {
158    
159        // A handle to the running task so that we can stop it when a shutdown has been requested.
160        // These two fields are volatile because their values will be accessed from multiple threads.
161        private volatile Future<?> runningTask;
162        private volatile ScheduledExecutorService executorService;
163    
164        // This lock protects the task so we can ensure that none of the template methods (startUp,
165        // shutDown or runOneIteration) run concurrently with one another.
166        private final ReentrantLock lock = new ReentrantLock();
167    
168        private final Runnable task = new Runnable() {
169          public void run() {
170            lock.lock();
171            try {
172              AbstractScheduledService.this.runOneIteration();
173            } catch (Throwable t) {
174              try {
175                shutDown();
176              } catch (Exception ignored) {
177                logger.log(Level.WARNING,
178                    "Error while attempting to shut down the service after failure.", ignored);
179              }
180              notifyFailed(t);
181              throw Throwables.propagate(t);
182            } finally {
183              lock.unlock();
184            }
185          }
186        };
187    
188        @Override
189        protected final void doStart() {
190          executorService = executor();
191          executorService.execute(new Runnable() {
192            public void run() {
193              lock.lock();
194              try {
195                startUp();
196                runningTask = scheduler().schedule(delegate, executorService, task);
197                notifyStarted();
198              } catch (Throwable t) {
199                notifyFailed(t);
200                throw Throwables.propagate(t);
201              } finally {
202                lock.unlock();
203              }
204            }
205          });
206        }
207    
208        @Override
209        protected final void doStop() {
210          runningTask.cancel(false);
211          executorService.execute(new Runnable() {
212            public void run() {
213              try {
214                lock.lock();
215                try {
216                  if (state() != State.STOPPING) {
217                    // This means that the state has changed since we were scheduled.  This implies that
218                    // an execution of runOneIteration has thrown an exception and we have transitioned
219                    // to a failed state, also this means that shutDown has already been called, so we
220                    // do not want to call it again.
221                    return;
222                  }
223                  shutDown();
224                } finally {
225                  lock.unlock();
226                }
227                notifyStopped();
228              } catch (Throwable t) {
229                notifyFailed(t);
230                throw Throwables.propagate(t);
231              }
232            }
233          });
234        }
235      };
236    
237      /**
238       * Run one iteration of the scheduled task. If any invocation of this method throws an exception,
239       * the service will transition to the {@link Service.State#FAILED} state and this method will no
240       * longer be called.
241       */
242      protected abstract void runOneIteration() throws Exception;
243    
244      /**
245       * Start the service.
246       *
247       * <p>By default this method does nothing.
248       */
249      protected void startUp() throws Exception {}
250    
251      /**
252       * Stop the service. This is guaranteed not to run concurrently with {@link #runOneIteration}.
253       *
254       * <p>By default this method does nothing.
255       */
256      protected void shutDown() throws Exception {}
257    
258      /**
259       * Returns the {@link Scheduler} object used to configure this service.  This method will only be
260       * called once.
261       */
262      protected abstract Scheduler scheduler();
263    
264      /**
265       * Returns the {@link ScheduledExecutorService} that will be used to execute the {@link #startUp},
266       * {@link #runOneIteration} and {@link #shutDown} methods.  The executor will not be
267       * {@link ScheduledExecutorService#shutdown} when this service stops. Subclasses may override this
268       * method to use a custom {@link ScheduledExecutorService} instance.
269       *
270       * <p>By default this returns a new {@link ScheduledExecutorService} with a single thread thread
271       * pool.  This method will only be called once.
272       */
273      protected ScheduledExecutorService executor() {
274        return Executors.newSingleThreadScheduledExecutor();
275      }
276    
277      
278      @Override
279      public String toString() {
280        return getClass().getSimpleName() + " [" + state() + "]";
281      }
282    
283      // We override instead of using ForwardingService so that these can be final.
284    
285      public final ListenableFuture<State> start() {
286        return delegate.start();
287      }
288    
289      public final State startAndWait() {
290        return delegate.startAndWait();
291      }
292    
293      public final boolean isRunning() {
294        return delegate.isRunning();
295      }
296    
297      public final State state() {
298        return delegate.state();
299      }
300    
301      public final ListenableFuture<State> stop() {
302        return delegate.stop();
303      }
304    
305      public final State stopAndWait() {
306        return delegate.stopAndWait();
307      }
308    
309      public final void addListener(Listener listener, Executor executor) {
310        delegate.addListener(listener, executor);
311      }
312    
313      /**
314       * A {@link Scheduler} that provides a convenient way for the {@link AbstractScheduledService} to
315       * use a dynamically changing schedule.  After every execution of the task, assuming it hasn't
316       * been cancelled, the {@link #getNextSchedule} method will be called.
317       *
318       * @author Luke Sandberg
319       * @since 11.0
320       */
321      @Beta
322      public abstract static class CustomScheduler extends Scheduler {
323    
324        /**
325         * A callable class that can reschedule itself using a {@link CustomScheduler}.
326         */
327        private class ReschedulableCallable extends ForwardingFuture<Void> implements Callable<Void> {
328    
329          /** The underlying task. */
330          private final Runnable wrappedRunnable;
331    
332          /** The executor on which this Callable will be scheduled. */
333          private final ScheduledExecutorService executor;
334    
335          /**
336           * The service that is managing this callable.  This is used so that failure can be
337           * reported properly.
338           */
339          private final AbstractService service;
340    
341          /**
342           * This lock is used to ensure safe and correct cancellation, it ensures that a new task is
343           * not scheduled while a cancel is ongoing.  Also it protects the currentFuture variable to
344           * ensure that it is assigned atomically with being scheduled.
345           */
346          private final ReentrantLock lock = new ReentrantLock();
347    
348          /** The future that represents the next execution of this task.*/
349          @GuardedBy("lock")
350          private Future<Void> currentFuture;
351    
352          ReschedulableCallable(AbstractService service, ScheduledExecutorService executor,
353              Runnable runnable) {
354            this.wrappedRunnable = runnable;
355            this.executor = executor;
356            this.service = service;
357          }
358    
359          
360          public Void call() throws Exception {
361            wrappedRunnable.run();
362            reschedule();
363            return null;
364          }
365    
366          /**
367           * Atomically reschedules this task and assigns the new future to {@link #currentFuture}.
368           */
369          public void reschedule() {
370            // We reschedule ourselves with a lock held for two reasons. 1. we want to make sure that
371            // cancel calls cancel on the correct future. 2. we want to make sure that the assignment
372            // to currentFuture doesn't race with itself so that currentFuture is assigned in the
373            // correct order.
374            lock.lock();
375            try {
376              if (currentFuture == null || !currentFuture.isCancelled()) {
377                final Schedule schedule = CustomScheduler.this.getNextSchedule();
378                currentFuture = executor.schedule(this, schedule.delay, schedule.unit);
379              }
380            } catch (Throwable e) {
381              // If an exception is thrown by the subclass then we need to make sure that the service
382              // notices and transitions to the FAILED state.  We do it by calling notifyFailed directly
383              // because the service does not monitor the state of the future so if the exception is not
384              // caught and forwarded to the service the task would stop executing but the service would
385              // have no idea.
386              service.notifyFailed(e);
387            } finally {
388              lock.unlock();
389            }
390          }
391    
392          // N.B. Only protect cancel and isCancelled because those are the only methods that are
393          // invoked by the AbstractScheduledService.
394          
395          @Override
396          public boolean cancel(boolean mayInterruptIfRunning) {
397            // Ensure that a task cannot be rescheduled while a cancel is ongoing.
398            lock.lock();
399            try {
400              return currentFuture.cancel(mayInterruptIfRunning);
401            } finally {
402              lock.unlock();
403            }
404          }
405    
406          
407          @Override
408          protected Future<Void> delegate() {
409            throw new UnsupportedOperationException("Only cancel is supported by this future");
410          }
411        }
412    
413        
414        @Override
415        final Future<?> schedule(AbstractService service, ScheduledExecutorService executor,
416            Runnable runnable) {
417          ReschedulableCallable task = new ReschedulableCallable(service, executor, runnable);
418          task.reschedule();
419          return task;
420        }
421    
422        /**
423         * A value object that represents an absolute delay until a task should be invoked.
424         *
425         * @author Luke Sandberg
426         * @since 11.0
427         */
428        @Beta
429        protected static final class Schedule {
430    
431          private final long delay;
432          private final TimeUnit unit;
433    
434          /**
435           * @param delay the time from now to delay execution
436           * @param unit the time unit of the delay parameter
437           */
438          public Schedule(long delay, TimeUnit unit) {
439            this.delay = delay;
440            this.unit = Preconditions.checkNotNull(unit);
441          }
442        }
443    
444        /**
445         * Calculates the time at which to next invoke the task.
446         *
447         * <p>This is guaranteed to be called immediately after the task has completed an iteration and
448         * on the same thread as the previous execution of {@link
449         * AbstractScheduledService#runOneIteration}.
450         *
451         * @return a schedule that defines the delay before the next execution.
452         */
453        protected abstract Schedule getNextSchedule() throws Exception;
454      }
455    }