View Javadoc

1   /**
2    *
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *     http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   */
19  package org.apache.hadoop.hbase.util;
20  
21  import java.io.PrintStream;
22  import java.io.PrintWriter;
23  import java.lang.Thread.UncaughtExceptionHandler;
24  import java.lang.reflect.InvocationTargetException;
25  import java.lang.reflect.Method;
26  import java.util.concurrent.LinkedBlockingQueue;
27  import java.util.concurrent.ThreadFactory;
28  import java.util.concurrent.ThreadPoolExecutor;
29  import java.util.concurrent.TimeUnit;
30  import java.util.concurrent.atomic.AtomicInteger;
31  
32  import org.apache.commons.logging.Log;
33  import org.apache.commons.logging.LogFactory;
34  import org.apache.hadoop.hbase.classification.InterfaceAudience;
35  import org.apache.hadoop.util.ReflectionUtils;
36  import org.apache.hadoop.util.StringUtils;
37  
38  /**
39   * Thread Utility
40   */
41  @InterfaceAudience.Private
42  public class Threads {
43    private static final Log LOG = LogFactory.getLog(Threads.class);
44    private static final AtomicInteger poolNumber = new AtomicInteger(1);
45  
46    private static UncaughtExceptionHandler LOGGING_EXCEPTION_HANDLER =
47      new UncaughtExceptionHandler() {
48      @Override
49      public void uncaughtException(Thread t, Throwable e) {
50        LOG.warn("Thread:" + t + " exited with Exception:"
51            + StringUtils.stringifyException(e));
52      }
53    };
54  
55    /**
56     * Utility method that sets name, daemon status and starts passed thread.
57     * @param t thread to run
58     * @return Returns the passed Thread <code>t</code>.
59     */
60    public static Thread setDaemonThreadRunning(final Thread t) {
61      return setDaemonThreadRunning(t, t.getName());
62    }
63  
64    /**
65     * Utility method that sets name, daemon status and starts passed thread.
66     * @param t thread to frob
67     * @param name new name
68     * @return Returns the passed Thread <code>t</code>.
69     */
70    public static Thread setDaemonThreadRunning(final Thread t,
71      final String name) {
72      return setDaemonThreadRunning(t, name, null);
73    }
74  
75    /**
76     * Utility method that sets name, daemon status and starts passed thread.
77     * @param t thread to frob
78     * @param name new name
79     * @param handler A handler to set on the thread.  Pass null if want to
80     * use default handler.
81     * @return Returns the passed Thread <code>t</code>.
82     */
83    public static Thread setDaemonThreadRunning(final Thread t,
84      final String name, final UncaughtExceptionHandler handler) {
85      t.setName(name);
86      if (handler != null) {
87        t.setUncaughtExceptionHandler(handler);
88      }
89      t.setDaemon(true);
90      t.start();
91      return t;
92    }
93  
94    /**
95     * Shutdown passed thread using isAlive and join.
96     * @param t Thread to shutdown
97     */
98    public static void shutdown(final Thread t) {
99      shutdown(t, 0);
100   }
101 
102   /**
103    * Shutdown passed thread using isAlive and join.
104    * @param joinwait Pass 0 if we're to wait forever.
105    * @param t Thread to shutdown
106    */
107   public static void shutdown(final Thread t, final long joinwait) {
108     if (t == null) return;
109     while (t.isAlive()) {
110       try {
111         t.join(joinwait);
112       } catch (InterruptedException e) {
113         LOG.warn(t.getName() + "; joinwait=" + joinwait, e);
114       }
115     }
116   }
117 
118 
119   /**
120    * @param t Waits on the passed thread to die dumping a threaddump every
121    * minute while its up.
122    * @throws InterruptedException
123    */
124   public static void threadDumpingIsAlive(final Thread t)
125   throws InterruptedException {
126     if (t == null) {
127       return;
128     }
129 
130     while (t.isAlive()) {
131       t.join(60 * 1000);
132       if (t.isAlive()) {
133         printThreadInfo(System.out,
134             "Automatic Stack Trace every 60 seconds waiting on " +
135             t.getName());
136       }
137     }
138   }
139 
140   /**
141    * If interrupted, just prints out the interrupt on STDOUT, resets interrupt and returns
142    * @param millis How long to sleep for in milliseconds.
143    */
144   public static void sleep(long millis) {
145     try {
146       Thread.sleep(millis);
147     } catch (InterruptedException e) {
148       e.printStackTrace();
149       Thread.currentThread().interrupt();
150     }
151   }
152 
153   /**
154    * Sleeps for the given amount of time even if interrupted. Preserves
155    * the interrupt status.
156    * @param msToWait the amount of time to sleep in milliseconds
157    */
158   public static void sleepWithoutInterrupt(final long msToWait) {
159     long timeMillis = System.currentTimeMillis();
160     long endTime = timeMillis + msToWait;
161     boolean interrupted = false;
162     while (timeMillis < endTime) {
163       try {
164         Thread.sleep(endTime - timeMillis);
165       } catch (InterruptedException ex) {
166         interrupted = true;
167       }
168       timeMillis = System.currentTimeMillis();
169     }
170 
171     if (interrupted) {
172       Thread.currentThread().interrupt();
173     }
174   }
175 
176   /**
177    * Create a new CachedThreadPool with a bounded number as the maximum
178    * thread size in the pool.
179    *
180    * @param maxCachedThread the maximum thread could be created in the pool
181    * @param timeout the maximum time to wait
182    * @param unit the time unit of the timeout argument
183    * @param threadFactory the factory to use when creating new threads
184    * @return threadPoolExecutor the cachedThreadPool with a bounded number
185    * as the maximum thread size in the pool.
186    */
187   public static ThreadPoolExecutor getBoundedCachedThreadPool(
188       int maxCachedThread, long timeout, TimeUnit unit,
189       ThreadFactory threadFactory) {
190     ThreadPoolExecutor boundedCachedThreadPool =
191       new ThreadPoolExecutor(maxCachedThread, maxCachedThread, timeout,
192         unit, new LinkedBlockingQueue<Runnable>(), threadFactory);
193     // allow the core pool threads timeout and terminate
194     boundedCachedThreadPool.allowCoreThreadTimeOut(true);
195     return boundedCachedThreadPool;
196   }
197 
198 
199   /**
200    * Returns a {@link java.util.concurrent.ThreadFactory} that names each created thread uniquely,
201    * with a common prefix.
202    * @param prefix The prefix of every created Thread's name
203    * @return a {@link java.util.concurrent.ThreadFactory} that names threads
204    */
205   public static ThreadFactory getNamedThreadFactory(final String prefix) {
206     SecurityManager s = System.getSecurityManager();
207     final ThreadGroup threadGroup = (s != null) ? s.getThreadGroup() : Thread.currentThread()
208         .getThreadGroup();
209 
210     return new ThreadFactory() {
211       final AtomicInteger threadNumber = new AtomicInteger(1);
212       private final int poolNumber = Threads.poolNumber.getAndIncrement();
213       final ThreadGroup group = threadGroup;
214 
215       @Override
216       public Thread newThread(Runnable r) {
217         final String name = prefix + "-pool" + poolNumber + "-t" + threadNumber.getAndIncrement();
218         return new Thread(group, r, name);
219       }
220     };
221   }
222 
223   /**
224    * Same as {#newDaemonThreadFactory(String, UncaughtExceptionHandler)},
225    * without setting the exception handler.
226    */
227   public static ThreadFactory newDaemonThreadFactory(final String prefix) {
228     return newDaemonThreadFactory(prefix, null);
229   }
230 
231   /**
232    * Get a named {@link ThreadFactory} that just builds daemon threads.
233    * @param prefix name prefix for all threads created from the factory
234    * @param handler unhandles exception handler to set for all threads
235    * @return a thread factory that creates named, daemon threads with
236    *         the supplied exception handler and normal priority
237    */
238   public static ThreadFactory newDaemonThreadFactory(final String prefix,
239       final UncaughtExceptionHandler handler) {
240     final ThreadFactory namedFactory = getNamedThreadFactory(prefix);
241     return new ThreadFactory() {
242       @Override
243       public Thread newThread(Runnable r) {
244         Thread t = namedFactory.newThread(r);
245         if (handler != null) {
246           t.setUncaughtExceptionHandler(handler);
247         } else {
248           t.setUncaughtExceptionHandler(LOGGING_EXCEPTION_HANDLER);
249         }
250         if (!t.isDaemon()) {
251           t.setDaemon(true);
252         }
253         if (t.getPriority() != Thread.NORM_PRIORITY) {
254           t.setPriority(Thread.NORM_PRIORITY);
255         }
256         return t;
257       }
258 
259     };
260   }
261 
262   /** Sets an UncaughtExceptionHandler for the thread which logs the
263    * Exception stack if the thread dies.
264    */
265   public static void setLoggingUncaughtExceptionHandler(Thread t) {
266     t.setUncaughtExceptionHandler(LOGGING_EXCEPTION_HANDLER);
267   }
268 
269   private static Method PRINT_THREAD_INFO_METHOD = null;
270   private static boolean PRINT_THREAD_INFO_METHOD_WITH_PRINTSTREAM = true;
271 
272   /**
273    * Print all of the thread's information and stack traces. Wrapper around Hadoop's method.
274    *
275    * @param stream the stream to
276    * @param title a string title for the stack trace
277    */
278   public static synchronized void printThreadInfo(PrintStream stream, String title) {
279     if (PRINT_THREAD_INFO_METHOD == null) {
280       try {
281         // Hadoop 2.7+ declares printThreadInfo(PrintStream, String)
282         PRINT_THREAD_INFO_METHOD = ReflectionUtils.class.getMethod("printThreadInfo",
283           PrintStream.class, String.class);
284       } catch (NoSuchMethodException e) {
285         // Hadoop 2.6 and earlier declares printThreadInfo(PrintWriter, String)
286         PRINT_THREAD_INFO_METHOD_WITH_PRINTSTREAM = false;
287         try {
288           PRINT_THREAD_INFO_METHOD = ReflectionUtils.class.getMethod("printThreadInfo",
289             PrintWriter.class, String.class);
290         } catch (NoSuchMethodException e1) {
291           throw new RuntimeException("Cannot find method. Check hadoop jars linked", e1);
292         }
293       }
294       PRINT_THREAD_INFO_METHOD.setAccessible(true);
295     }
296 
297     try {
298       if (PRINT_THREAD_INFO_METHOD_WITH_PRINTSTREAM) {
299         PRINT_THREAD_INFO_METHOD.invoke(null, stream, title);
300       } else {
301         PRINT_THREAD_INFO_METHOD.invoke(null, new PrintWriter(stream), title);
302       }
303     } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
304       throw new RuntimeException(e.getCause());
305     }
306   }
307 }