View Javadoc
1   /*
2    * Copyright 2012 The Netty Project
3    *
4    * The Netty Project licenses this file to you under the Apache License,
5    * version 2.0 (the "License"); you may not use this file except in compliance
6    * with the License. You may obtain a copy of the License at:
7    *
8    *   https://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13   * License for the specific language governing permissions and limitations
14   * under the License.
15   */
16  package io.netty.util.concurrent;
17  
18  import io.netty.util.internal.ObjectUtil;
19  import io.netty.util.internal.SystemPropertyUtil;
20  import io.netty.util.internal.ThreadExecutorMap;
21  import io.netty.util.internal.ThrowableUtil;
22  import io.netty.util.internal.logging.InternalLogger;
23  import io.netty.util.internal.logging.InternalLoggerFactory;
24  
25  import org.jetbrains.annotations.Async.Schedule;
26  
27  import java.security.AccessController;
28  import java.security.PrivilegedAction;
29  import java.util.Queue;
30  import java.util.concurrent.BlockingQueue;
31  import java.util.concurrent.Executors;
32  import java.util.concurrent.LinkedBlockingQueue;
33  import java.util.concurrent.RejectedExecutionException;
34  import java.util.concurrent.ThreadFactory;
35  import java.util.concurrent.TimeUnit;
36  import java.util.concurrent.atomic.AtomicBoolean;
37  
38  /**
39   * Single-thread singleton {@link EventExecutor}.  It starts the thread automatically and stops it when there is no
40   * task pending in the task queue for {@code io.netty.globalEventExecutor.quietPeriodSeconds} second
41   * (default is 1 second).  Please note it is not scalable to schedule large number of tasks to this executor;
42   * use a dedicated executor.
43   */
44  public final class GlobalEventExecutor extends AbstractScheduledEventExecutor implements OrderedEventExecutor {
45      private static final InternalLogger logger = InternalLoggerFactory.getInstance(GlobalEventExecutor.class);
46  
47      private static final long SCHEDULE_QUIET_PERIOD_INTERVAL;
48  
49      static {
50          int quietPeriod = SystemPropertyUtil.getInt("io.netty.globalEventExecutor.quietPeriodSeconds", 1);
51          if (quietPeriod <= 0) {
52              quietPeriod = 1;
53          }
54          logger.debug("-Dio.netty.globalEventExecutor.quietPeriodSeconds: {}", quietPeriod);
55  
56          SCHEDULE_QUIET_PERIOD_INTERVAL = TimeUnit.SECONDS.toNanos(quietPeriod);
57      }
58  
59      public static final GlobalEventExecutor INSTANCE = new GlobalEventExecutor();
60  
61      final BlockingQueue<Runnable> taskQueue = new LinkedBlockingQueue<Runnable>();
62      final ScheduledFutureTask<Void> quietPeriodTask = new ScheduledFutureTask<Void>(
63              this, Executors.<Void>callable(new Runnable() {
64          @Override
65          public void run() {
66              // NOOP
67          }
68      }, null),
69              // note: the getCurrentTimeNanos() call here only works because this is a final class, otherwise the method
70              // could be overridden leading to unsafe initialization here!
71              deadlineNanos(getCurrentTimeNanos(), SCHEDULE_QUIET_PERIOD_INTERVAL),
72              -SCHEDULE_QUIET_PERIOD_INTERVAL
73      );
74  
75      // because the GlobalEventExecutor is a singleton, tasks submitted to it can come from arbitrary threads and this
76      // can trigger the creation of a thread from arbitrary thread groups; for this reason, the thread factory must not
77      // be sticky about its thread group
78      // visible for testing
79      final ThreadFactory threadFactory;
80      private final TaskRunner taskRunner = new TaskRunner();
81      private final AtomicBoolean started = new AtomicBoolean();
82      volatile Thread thread;
83  
84      private final Future<?> terminationFuture;
85  
86      private GlobalEventExecutor() {
87          scheduleFromEventLoop(quietPeriodTask);
88          threadFactory = ThreadExecutorMap.apply(new DefaultThreadFactory(
89                  DefaultThreadFactory.toPoolName(getClass()), false, Thread.NORM_PRIORITY, null), this);
90  
91          terminationFuture = new FailedFuture<Object>(this,
92                  StacklessUnsupportedOperationException.newInstance(GlobalEventExecutor.class, "terminationFuture"));
93      }
94  
95      /**
96       * Take the next {@link Runnable} from the task queue and so will block if no task is currently present.
97       *
98       * @return {@code null} if the executor thread has been interrupted or waken up.
99       */
100     Runnable takeTask() {
101         BlockingQueue<Runnable> taskQueue = this.taskQueue;
102         for (;;) {
103             ScheduledFutureTask<?> scheduledTask = peekScheduledTask();
104             if (scheduledTask == null) {
105                 Runnable task = null;
106                 try {
107                     task = taskQueue.take();
108                 } catch (InterruptedException e) {
109                     // Ignore
110                 }
111                 return task;
112             } else {
113                 long delayNanos = scheduledTask.delayNanos();
114                 Runnable task = null;
115                 if (delayNanos > 0) {
116                     try {
117                         task = taskQueue.poll(delayNanos, TimeUnit.NANOSECONDS);
118                     } catch (InterruptedException e) {
119                         // Waken up.
120                         return null;
121                     }
122                 }
123                 if (task == null) {
124                     // We need to fetch the scheduled tasks now as otherwise there may be a chance that
125                     // scheduled tasks are never executed if there is always one task in the taskQueue.
126                     // This is for example true for the read task of OIO Transport
127                     // See https://github.com/netty/netty/issues/1614
128                     fetchFromScheduledTaskQueue();
129                     task = taskQueue.poll();
130                 }
131 
132                 if (task != null) {
133                     return task;
134                 }
135             }
136         }
137     }
138 
139     private void fetchFromScheduledTaskQueue() {
140         long nanoTime = getCurrentTimeNanos();
141         ScheduledFutureTask scheduledTask;
142         while ((scheduledTask = (ScheduledFutureTask) pollScheduledTask(nanoTime)) != null) {
143             if (scheduledTask.isCancelled()) {
144                 continue;
145             }
146             taskQueue.add(scheduledTask);
147         }
148     }
149 
150     /**
151      * Return the number of tasks that are pending for processing.
152      */
153     public int pendingTasks() {
154         return taskQueue.size();
155     }
156 
157     /**
158      * Add a task to the task queue, or throws a {@link RejectedExecutionException} if this instance was shutdown
159      * before.
160      */
161     private void addTask(Runnable task) {
162         taskQueue.add(ObjectUtil.checkNotNull(task, "task"));
163     }
164 
165     @Override
166     public boolean inEventLoop(Thread thread) {
167         return thread == this.thread;
168     }
169 
170     @Override
171     public Future<?> shutdownGracefully(long quietPeriod, long timeout, TimeUnit unit) {
172         return terminationFuture();
173     }
174 
175     @Override
176     public Future<?> terminationFuture() {
177         return terminationFuture;
178     }
179 
180     @Override
181     @Deprecated
182     public void shutdown() {
183         throw new UnsupportedOperationException();
184     }
185 
186     @Override
187     public boolean isShuttingDown() {
188         return false;
189     }
190 
191     @Override
192     public boolean isShutdown() {
193         return false;
194     }
195 
196     @Override
197     public boolean isTerminated() {
198         return false;
199     }
200 
201     @Override
202     public boolean awaitTermination(long timeout, TimeUnit unit) {
203         return false;
204     }
205 
206     /**
207      * Waits until the worker thread of this executor has no tasks left in its task queue and terminates itself.
208      * Because a new worker thread will be started again when a new task is submitted, this operation is only useful
209      * when you want to ensure that the worker thread is terminated <strong>after</strong> your application is shut
210      * down and there's no chance of submitting a new task afterwards.
211      *
212      * @return {@code true} if and only if the worker thread has been terminated
213      */
214     public boolean awaitInactivity(long timeout, TimeUnit unit) throws InterruptedException {
215         ObjectUtil.checkNotNull(unit, "unit");
216 
217         final Thread thread = this.thread;
218         if (thread == null) {
219             throw new IllegalStateException("thread was not started");
220         }
221         thread.join(unit.toMillis(timeout));
222         return !thread.isAlive();
223     }
224 
225     @Override
226     public void execute(Runnable task) {
227         execute0(task);
228     }
229 
230     private void execute0(@Schedule Runnable task) {
231         addTask(ObjectUtil.checkNotNull(task, "task"));
232         if (!inEventLoop()) {
233             startThread();
234         }
235     }
236 
237     private void startThread() {
238         if (started.compareAndSet(false, true)) {
239             final Thread callingThread = Thread.currentThread();
240             ClassLoader parentCCL = AccessController.doPrivileged(new PrivilegedAction<ClassLoader>() {
241                 @Override
242                 public ClassLoader run() {
243                     return callingThread.getContextClassLoader();
244                 }
245             });
246             // Avoid calling classloader leaking through Thread.inheritedAccessControlContext.
247             setContextClassLoader(callingThread, null);
248             try {
249                 final Thread t = threadFactory.newThread(taskRunner);
250                 // Set to null to ensure we not create classloader leaks by holds a strong reference to the inherited
251                 // classloader.
252                 // See:
253                 // - https://github.com/netty/netty/issues/7290
254                 // - https://bugs.openjdk.java.net/browse/JDK-7008595
255                 setContextClassLoader(t, null);
256 
257                 // Set the thread before starting it as otherwise inEventLoop() may return false and so produce
258                 // an assert error.
259                 // See https://github.com/netty/netty/issues/4357
260                 thread = t;
261                 t.start();
262             } finally {
263                 setContextClassLoader(callingThread, parentCCL);
264             }
265         }
266     }
267 
268     private static void setContextClassLoader(final Thread t, final ClassLoader cl) {
269         AccessController.doPrivileged(new PrivilegedAction<Void>() {
270             @Override
271             public Void run() {
272                 t.setContextClassLoader(cl);
273                 return null;
274             }
275         });
276     }
277 
278     final class TaskRunner implements Runnable {
279         @Override
280         public void run() {
281             for (;;) {
282                 Runnable task = takeTask();
283                 if (task != null) {
284                     try {
285                         runTask(task);
286                     } catch (Throwable t) {
287                         logger.warn("Unexpected exception from the global event executor: ", t);
288                     }
289 
290                     if (task != quietPeriodTask) {
291                         continue;
292                     }
293                 }
294 
295                 Queue<ScheduledFutureTask<?>> scheduledTaskQueue = GlobalEventExecutor.this.scheduledTaskQueue;
296                 // Terminate if there is no task in the queue (except the noop task).
297                 if (taskQueue.isEmpty() && (scheduledTaskQueue == null || scheduledTaskQueue.size() == 1)) {
298                     // Mark the current thread as stopped.
299                     // The following CAS must always success and must be uncontended,
300                     // because only one thread should be running at the same time.
301                     boolean stopped = started.compareAndSet(true, false);
302                     assert stopped;
303 
304                     // Check if there are pending entries added by execute() or schedule*() while we do CAS above.
305                     // Do not check scheduledTaskQueue because it is not thread-safe and can only be mutated from a
306                     // TaskRunner actively running tasks.
307                     if (taskQueue.isEmpty()) {
308                         // A) No new task was added and thus there's nothing to handle
309                         //    -> safe to terminate because there's nothing left to do
310                         // B) A new thread started and handled all the new tasks.
311                         //    -> safe to terminate the new thread will take care the rest
312                         break;
313                     }
314 
315                     // There are pending tasks added again.
316                     if (!started.compareAndSet(false, true)) {
317                         // startThread() started a new thread and set 'started' to true.
318                         // -> terminate this thread so that the new thread reads from taskQueue exclusively.
319                         break;
320                     }
321 
322                     // New tasks were added, but this worker was faster to set 'started' to true.
323                     // i.e. a new worker thread was not started by startThread().
324                     // -> keep this thread alive to handle the newly added entries.
325                 }
326             }
327         }
328     }
329 
330     private static final class StacklessUnsupportedOperationException extends UnsupportedOperationException {
331 
332         private static final long serialVersionUID = -8060232216137960173L;
333 
334         private StacklessUnsupportedOperationException() { }
335 
336         // Override fillInStackTrace() so we not populate the backtrace via a native call and so leak the
337         // Classloader. As the GlobalEventExecutor.INSTANCE is a singleton and holds on to this exception via its
338         // terminationFuture, a populated backtrace would pin the Classloader of whatever thread happened to trigger
339         // the lazy initialization of INSTANCE (see https://github.com/netty/netty/issues/17128).
340         @Override
341         public Throwable fillInStackTrace() {
342             return this;
343         }
344 
345         static StacklessUnsupportedOperationException newInstance(Class<?> clazz, String method) {
346             return ThrowableUtil.unknownStackTrace(new StacklessUnsupportedOperationException(), clazz, method);
347         }
348     }
349 }