Merge remote-tracking branch 'dakrone/hot-threads-fail-better'

This commit is contained in:
Lee Hinman 2016-04-04 16:41:03 -06:00
commit 177e3ae331

View File

@ -21,8 +21,12 @@ package org.elasticsearch.monitor.jvm;
import org.apache.lucene.util.CollectionUtil;
import org.apache.lucene.util.Constants;
import org.elasticsearch.ElasticsearchException;
import org.elasticsearch.ExceptionsHelper;
import org.elasticsearch.common.joda.FormatDateTimeFormatter;
import org.elasticsearch.common.joda.Joda;
import org.elasticsearch.common.logging.ESLogger;
import org.elasticsearch.common.logging.Loggers;
import org.elasticsearch.common.unit.TimeValue;
import java.lang.management.ManagementFactory;
@ -41,6 +45,7 @@ import java.util.concurrent.TimeUnit;
public class HotThreads {
private static final Object mutex = new Object();
private static final ESLogger logger = Loggers.getLogger(HotThreads.class);
private static final FormatDateTimeFormatter DATE_TIME_FORMATTER = Joda.forPattern("dateOptionalTime");
@ -130,13 +135,12 @@ public class HotThreads {
}
private String innerDetect() throws Exception {
StringBuilder sb = new StringBuilder();
if (Constants.FREE_BSD) {
sb.append("hot_threads is not supported on FreeBSD");
return sb.toString();
ThreadMXBean threadBean = ManagementFactory.getThreadMXBean();
if (threadBean.isThreadCpuTimeSupported() == false) {
throw new ElasticsearchException("thread CPU time is not supported on this JDK");
}
StringBuilder sb = new StringBuilder();
sb.append("Hot threads at ");
sb.append(DATE_TIME_FORMATTER.printer().print(System.currentTimeMillis()));
sb.append(", interval=");
@ -147,17 +151,6 @@ public class HotThreads {
sb.append(ignoreIdleThreads);
sb.append(":\n");
ThreadMXBean threadBean = ManagementFactory.getThreadMXBean();
boolean enabledCpu = false;
try {
if (threadBean.isThreadCpuTimeSupported()) {
if (!threadBean.isThreadCpuTimeEnabled()) {
enabledCpu = true;
threadBean.setThreadCpuTimeEnabled(true);
}
} else {
throw new IllegalStateException("MBean doesn't support thread CPU Time");
}
Map<Long, MyThreadInfo> threadInfos = new HashMap<>();
for (long threadId : threadBean.getAllThreadIds()) {
// ignore our own thread...
@ -211,7 +204,7 @@ public class HotThreads {
} else if ("block".equals(type)) {
return (int) (o2.blockedTime - o1.blockedTime);
}
throw new IllegalArgumentException();
throw new IllegalArgumentException("expected thread type to be either 'cpu', 'wait', or 'block', but was " + type);
}
});
// analyse N stack traces for M busiest threads
@ -294,11 +287,6 @@ public class HotThreads {
}
}
return sb.toString();
} finally {
if (enabledCpu) {
threadBean.setThreadCpuTimeEnabled(false);
}
}
}
private static final StackTraceElement[] EMPTY = new StackTraceElement[0];