Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HBASE-26041 Replace PrintThreadInfoHelper with HBase's own ReflectionUtils.printThreadInfo() #3442

Merged
merged 2 commits into from
Jul 2, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -30,7 +30,6 @@
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;
Expand Down Expand Up @@ -196,76 +195,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);
}
}