001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.commons.lang3.concurrent;
018
019import java.util.concurrent.ScheduledExecutorService;
020import java.util.concurrent.ScheduledFuture;
021import java.util.concurrent.ScheduledThreadPoolExecutor;
022import java.util.concurrent.TimeUnit;
023
024import org.apache.commons.lang3.Validate;
025
026/**
027 * <p>
028 * A specialized <em>semaphore</em> implementation that provides a number of
029 * permits in a given time frame.
030 * </p>
031 * <p>
032 * This class is similar to the {@code java.util.concurrent.Semaphore} class
033 * provided by the JDK in that it manages a configurable number of permits.
034 * Using the {@link #acquire()} method a permit can be requested by a thread.
035 * However, there is an additional timing dimension: there is no {@code
036 * release()} method for freeing a permit, but all permits are automatically
037 * released at the end of a configurable time frame. If a thread calls
038 * {@link #acquire()} and the available permits are already exhausted for this
039 * time frame, the thread is blocked. When the time frame ends all permits
040 * requested so far are restored, and blocking threads are waked up again, so
041 * that they can try to acquire a new permit. This basically means that in the
042 * specified time frame only the given number of operations is possible.
043 * </p>
044 * <p>
045 * A use case for this class is to artificially limit the load produced by a
046 * process. As an example consider an application that issues database queries
047 * on a production system in a background process to gather statistical
048 * information. This background processing should not produce so much database
049 * load that the functionality and the performance of the production system are
050 * impacted. Here a {@code TimedSemaphore} could be installed to guarantee that
051 * only a given number of database queries are issued per second.
052 * </p>
053 * <p>
054 * A thread class for performing database queries could look as follows:
055 * </p>
056 *
057 * <pre>
058 * public class StatisticsThread extends Thread {
059 *     // The semaphore for limiting database load.
060 *     private final TimedSemaphore semaphore;
061 *     // Create an instance and set the semaphore
062 *     public StatisticsThread(TimedSemaphore timedSemaphore) {
063 *         semaphore = timedSemaphore;
064 *     }
065 *     // Gather statistics
066 *     public void run() {
067 *         try {
068 *             while(true) {
069 *                 semaphore.acquire();   // limit database load
070 *                 performQuery();        // issue a query
071 *             }
072 *         } catch(InterruptedException) {
073 *             // fall through
074 *         }
075 *     }
076 *     ...
077 * }
078 * </pre>
079 *
080 * <p>
081 * The following code fragment shows how a {@code TimedSemaphore} is created
082 * that allows only 10 operations per second and passed to the statistics
083 * thread:
084 * </p>
085 *
086 * <pre>
087 * TimedSemaphore sem = new TimedSemaphore(1, TimeUnit.SECOND, 10);
088 * StatisticsThread thread = new StatisticsThread(sem);
089 * thread.start();
090 * </pre>
091 *
092 * <p>
093 * When creating an instance the time period for the semaphore must be
094 * specified. {@code TimedSemaphore} uses an executor service with a
095 * corresponding period to monitor this interval. The {@code
096 * ScheduledExecutorService} to be used for this purpose can be provided at
097 * construction time. Alternatively the class creates an internal executor
098 * service.
099 * </p>
100 * <p>
101 * Client code that uses {@code TimedSemaphore} has to call the
102 * {@link #acquire()} method in aach processing step. {@code TimedSemaphore}
103 * keeps track of the number of invocations of the {@link #acquire()} method and
104 * blocks the calling thread if the counter exceeds the limit specified. When
105 * the timer signals the end of the time period the counter is reset and all
106 * waiting threads are released. Then another cycle can start.
107 * </p>
108 * <p>
109 * It is possible to modify the limit at any time using the
110 * {@link #setLimit(int)} method. This is useful if the load produced by an
111 * operation has to be adapted dynamically. In the example scenario with the
112 * thread collecting statistics it may make sense to specify a low limit during
113 * day time while allowing a higher load in the night time. Reducing the limit
114 * takes effect immediately by blocking incoming callers. If the limit is
115 * increased, waiting threads are not released immediately, but wake up when the
116 * timer runs out. Then, in the next period more processing steps can be
117 * performed without blocking. By setting the limit to 0 the semaphore can be
118 * switched off: in this mode the {@link #acquire()} method never blocks, but
119 * lets all callers pass directly.
120 * </p>
121 * <p>
122 * When the {@code TimedSemaphore} is no more needed its {@link #shutdown()}
123 * method should be called. This causes the periodic task that monitors the time
124 * interval to be canceled. If the {@code ScheduledExecutorService} has been
125 * created by the semaphore at construction time, it is also shut down.
126 * resources. After that {@link #acquire()} must not be called any more.
127 * </p>
128 *
129 * @since 3.0
130 * @version $Id$
131 */
132public class TimedSemaphore {
133    /**
134     * Constant for a value representing no limit. If the limit is set to a
135     * value less or equal this constant, the {@code TimedSemaphore} will be
136     * effectively switched off.
137     */
138    public static final int NO_LIMIT = 0;
139
140    /** Constant for the thread pool size for the executor. */
141    private static final int THREAD_POOL_SIZE = 1;
142
143    /** The executor service for managing the timer thread. */
144    private final ScheduledExecutorService executorService;
145
146    /** Stores the period for this timed semaphore. */
147    private final long period;
148
149    /** The time unit for the period. */
150    private final TimeUnit unit;
151
152    /** A flag whether the executor service was created by this object. */
153    private final boolean ownExecutor;
154
155    /** A future object representing the timer task. */
156    private ScheduledFuture<?> task; // @GuardedBy("this")
157
158    /** Stores the total number of invocations of the acquire() method. */
159    private long totalAcquireCount; // @GuardedBy("this")
160
161    /**
162     * The counter for the periods. This counter is increased every time a
163     * period ends.
164     */
165    private long periodCount; // @GuardedBy("this")
166
167    /** The limit. */
168    private int limit; // @GuardedBy("this")
169
170    /** The current counter. */
171    private int acquireCount;  // @GuardedBy("this")
172
173    /** The number of invocations of acquire() in the last period. */
174    private int lastCallsPerPeriod; // @GuardedBy("this")
175
176    /** A flag whether shutdown() was called. */
177    private boolean shutdown;  // @GuardedBy("this")
178
179    /**
180     * Creates a new instance of {@link TimedSemaphore} and initializes it with
181     * the given time period and the limit.
182     *
183     * @param timePeriod the time period
184     * @param timeUnit the unit for the period
185     * @param limit the limit for the semaphore
186     * @throws IllegalArgumentException if the period is less or equals 0
187     */
188    public TimedSemaphore(final long timePeriod, final TimeUnit timeUnit, final int limit) {
189        this(null, timePeriod, timeUnit, limit);
190    }
191
192    /**
193     * Creates a new instance of {@link TimedSemaphore} and initializes it with
194     * an executor service, the given time period, and the limit. The executor
195     * service will be used for creating a periodic task for monitoring the time
196     * period. It can be <b>null</b>, then a default service will be created.
197     *
198     * @param service the executor service
199     * @param timePeriod the time period
200     * @param timeUnit the unit for the period
201     * @param limit the limit for the semaphore
202     * @throws IllegalArgumentException if the period is less or equals 0
203     */
204    public TimedSemaphore(final ScheduledExecutorService service, final long timePeriod,
205            final TimeUnit timeUnit, final int limit) {
206        Validate.inclusiveBetween(1, Long.MAX_VALUE, timePeriod, "Time period must be greater than 0!");
207
208        period = timePeriod;
209        unit = timeUnit;
210
211        if (service != null) {
212            executorService = service;
213            ownExecutor = false;
214        } else {
215            final ScheduledThreadPoolExecutor s = new ScheduledThreadPoolExecutor(
216                    THREAD_POOL_SIZE);
217            s.setContinueExistingPeriodicTasksAfterShutdownPolicy(false);
218            s.setExecuteExistingDelayedTasksAfterShutdownPolicy(false);
219            executorService = s;
220            ownExecutor = true;
221        }
222
223        setLimit(limit);
224    }
225
226    /**
227     * Returns the limit enforced by this semaphore. The limit determines how
228     * many invocations of {@link #acquire()} are allowed within the monitored
229     * period.
230     *
231     * @return the limit
232     */
233    public final synchronized int getLimit() {
234        return limit;
235    }
236
237    /**
238     * Sets the limit. This is the number of times the {@link #acquire()} method
239     * can be called within the time period specified. If this limit is reached,
240     * further invocations of {@link #acquire()} will block. Setting the limit
241     * to a value &lt;= {@link #NO_LIMIT} will cause the limit to be disabled,
242     * i.e. an arbitrary number of{@link #acquire()} invocations is allowed in
243     * the time period.
244     *
245     * @param limit the limit
246     */
247    public final synchronized void setLimit(final int limit) {
248        this.limit = limit;
249    }
250
251    /**
252     * Initializes a shutdown. After that the object cannot be used any more.
253     * This method can be invoked an arbitrary number of times. All invocations
254     * after the first one do not have any effect.
255     */
256    public synchronized void shutdown() {
257        if (!shutdown) {
258
259            if (ownExecutor) {
260                // if the executor was created by this instance, it has
261                // to be shutdown
262                getExecutorService().shutdownNow();
263            }
264            if (task != null) {
265                task.cancel(false);
266            }
267
268            shutdown = true;
269        }
270    }
271
272    /**
273     * Tests whether the {@link #shutdown()} method has been called on this
274     * object. If this method returns <b>true</b>, this instance cannot be used
275     * any longer.
276     *
277     * @return a flag whether a shutdown has been performed
278     */
279    public synchronized boolean isShutdown() {
280        return shutdown;
281    }
282
283    /**
284     * Tries to acquire a permit from this semaphore. This method will block if
285     * the limit for the current period has already been reached. If
286     * {@link #shutdown()} has already been invoked, calling this method will
287     * cause an exception. The very first call of this method starts the timer
288     * task which monitors the time period set for this {@code TimedSemaphore}.
289     * From now on the semaphore is active.
290     *
291     * @throws InterruptedException if the thread gets interrupted
292     * @throws IllegalStateException if this semaphore is already shut down
293     */
294    public synchronized void acquire() throws InterruptedException {
295        if (isShutdown()) {
296            throw new IllegalStateException("TimedSemaphore is shut down!");
297        }
298
299        if (task == null) {
300            task = startTimer();
301        }
302
303        boolean canPass = false;
304        do {
305            canPass = getLimit() <= NO_LIMIT || acquireCount < getLimit();
306            if (!canPass) {
307                wait();
308            } else {
309                acquireCount++;
310            }
311        } while (!canPass);
312    }
313
314    /**
315     * Returns the number of (successful) acquire invocations during the last
316     * period. This is the number of times the {@link #acquire()} method was
317     * called without blocking. This can be useful for testing or debugging
318     * purposes or to determine a meaningful threshold value. If a limit is set,
319     * the value returned by this method won't be greater than this limit.
320     *
321     * @return the number of non-blocking invocations of the {@link #acquire()}
322     * method
323     */
324    public synchronized int getLastAcquiresPerPeriod() {
325        return lastCallsPerPeriod;
326    }
327
328    /**
329     * Returns the number of invocations of the {@link #acquire()} method for
330     * the current period. This may be useful for testing or debugging purposes.
331     *
332     * @return the current number of {@link #acquire()} invocations
333     */
334    public synchronized int getAcquireCount() {
335        return acquireCount;
336    }
337
338    /**
339     * Returns the number of calls to the {@link #acquire()} method that can
340     * still be performed in the current period without blocking. This method
341     * can give an indication whether it is safe to call the {@link #acquire()}
342     * method without risking to be suspended. However, there is no guarantee
343     * that a subsequent call to {@link #acquire()} actually is not-blocking
344     * because in the mean time other threads may have invoked the semaphore.
345     *
346     * @return the current number of available {@link #acquire()} calls in the
347     * current period
348     */
349    public synchronized int getAvailablePermits() {
350        return getLimit() - getAcquireCount();
351    }
352
353    /**
354     * Returns the average number of successful (i.e. non-blocking)
355     * {@link #acquire()} invocations for the entire life-time of this {@code
356     * TimedSemaphore}. This method can be used for instance for statistical
357     * calculations.
358     *
359     * @return the average number of {@link #acquire()} invocations per time
360     * unit
361     */
362    public synchronized double getAverageCallsPerPeriod() {
363        return periodCount == 0 ? 0 : (double) totalAcquireCount
364                / (double) periodCount;
365    }
366
367    /**
368     * Returns the time period. This is the time monitored by this semaphore.
369     * Only a given number of invocations of the {@link #acquire()} method is
370     * possible in this period.
371     *
372     * @return the time period
373     */
374    public long getPeriod() {
375        return period;
376    }
377
378    /**
379     * Returns the time unit. This is the unit used by {@link #getPeriod()}.
380     *
381     * @return the time unit
382     */
383    public TimeUnit getUnit() {
384        return unit;
385    }
386
387    /**
388     * Returns the executor service used by this instance.
389     *
390     * @return the executor service
391     */
392    protected ScheduledExecutorService getExecutorService() {
393        return executorService;
394    }
395
396    /**
397     * Starts the timer. This method is called when {@link #acquire()} is called
398     * for the first time. It schedules a task to be executed at fixed rate to
399     * monitor the time period specified.
400     *
401     * @return a future object representing the task scheduled
402     */
403    protected ScheduledFuture<?> startTimer() {
404        return getExecutorService().scheduleAtFixedRate(new Runnable() {
405            @Override
406            public void run() {
407                endOfPeriod();
408            }
409        }, getPeriod(), getPeriod(), getUnit());
410    }
411
412    /**
413     * The current time period is finished. This method is called by the timer
414     * used internally to monitor the time period. It resets the counter and
415     * releases the threads waiting for this barrier.
416     */
417    synchronized void endOfPeriod() {
418        lastCallsPerPeriod = acquireCount;
419        totalAcquireCount += acquireCount;
420        periodCount++;
421        acquireCount = 0;
422        notifyAll();
423    }
424}