Skip to content

Commit

Permalink
HBASE-26041 Replace PrintThreadInfoHelper with HBase's own Reflection…
Browse files Browse the repository at this point in the history
…Utils.printThreadInfo() (#3442)

Signed-off-by: Michael Stack <[email protected]>
Signed-off-by: Duo Zhang <[email protected]>
  • Loading branch information
jojochuang authored Jul 2, 2021
1 parent 5118321 commit ef639ff
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 73 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ public static void logThreadInfo(Logger log,
* @param stream the stream to
* @param title a string title for the stack trace
*/
private static void printThreadInfo(PrintStream stream,
static void printThreadInfo(PrintStream stream,
String title) {
final int STACK_DEPTH = 20;
boolean contention = threadBean.isThreadContentionMonitoringEnabled();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,26 +18,18 @@
*/
package org.apache.hadoop.hbase.util;

import java.io.OutputStreamWriter;
import java.io.PrintStream;
import java.io.PrintWriter;
import java.lang.Thread.UncaughtExceptionHandler;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.nio.charset.StandardCharsets;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

import org.apache.hadoop.util.ReflectionUtils;
import org.apache.hadoop.util.StringUtils;
import org.apache.yetus.audience.InterfaceAudience;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import org.apache.hbase.thirdparty.com.google.common.base.Preconditions;

/**
* Thread Utility
*/
Expand Down Expand Up @@ -196,76 +188,13 @@ public static void setLoggingUncaughtExceptionHandler(Thread t) {
t.setUncaughtExceptionHandler(LOGGING_EXCEPTION_HANDLER);
}

private interface PrintThreadInfoHelper {

void printThreadInfo(PrintStream stream, String title);

}

private static class PrintThreadInfoLazyHolder {

public static final PrintThreadInfoHelper HELPER = initHelper();

private static PrintThreadInfoHelper initHelper() {
Method method = null;
try {
// Hadoop 2.7+ declares printThreadInfo(PrintStream, String)
method = ReflectionUtils.class.getMethod("printThreadInfo", PrintStream.class,
String.class);
method.setAccessible(true);
final Method hadoop27Method = method;
return new PrintThreadInfoHelper() {

@Override
public void printThreadInfo(PrintStream stream, String title) {
try {
hadoop27Method.invoke(null, stream, title);
} catch (IllegalAccessException | IllegalArgumentException e) {
throw new RuntimeException(e);
} catch (InvocationTargetException e) {
throw new RuntimeException(e.getCause());
}
}
};
} catch (NoSuchMethodException e) {
LOG.info(
"Can not find hadoop 2.7+ printThreadInfo method, try hadoop hadoop 2.6 and earlier", e);
}
try {
// Hadoop 2.6 and earlier declares printThreadInfo(PrintWriter, String)
method = ReflectionUtils.class.getMethod("printThreadInfo", PrintWriter.class,
String.class);
method.setAccessible(true);
final Method hadoop26Method = method;
return new PrintThreadInfoHelper() {

@Override
public void printThreadInfo(PrintStream stream, String title) {
try {
hadoop26Method.invoke(null, new PrintWriter(
new OutputStreamWriter(stream, StandardCharsets.UTF_8)), title);
} catch (IllegalAccessException | IllegalArgumentException e) {
throw new RuntimeException(e);
} catch (InvocationTargetException e) {
throw new RuntimeException(e.getCause());
}
}
};
} catch (NoSuchMethodException e) {
LOG.warn("Cannot find printThreadInfo method. Check hadoop jars linked", e);
}
return null;
}
}

/**
* Print all of the thread's information and stack traces. Wrapper around Hadoop's method.
*
* @param stream the stream to
* @param title a string title for the stack trace
*/
public static void printThreadInfo(PrintStream stream, String title) {
Preconditions.checkNotNull(PrintThreadInfoLazyHolder.HELPER,
"Cannot find method. Check hadoop jars linked").printThreadInfo(stream, title);
ReflectionUtils.printThreadInfo(stream, title);
}
}

0 comments on commit ef639ff

Please sign in to comment.