Remove load average leniency

Today when acquiring load average stats, this method is rather lenient
when reading /proc/loadavg. This commit removes this leniency so that
any such issues are more likely to be exposed to the end-user.

Relates #21380
This commit is contained in:
Jason Tedor 2016-11-07 14:04:04 -05:00 committed by GitHub
parent 23a271f092
commit 8067412983
1 changed files with 26 additions and 33 deletions

View File

@ -29,6 +29,7 @@ import org.elasticsearch.monitor.Probes;
import java.io.IOException; import java.io.IOException;
import java.lang.management.ManagementFactory; import java.lang.management.ManagementFactory;
import java.lang.management.OperatingSystemMXBean; import java.lang.management.OperatingSystemMXBean;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.Path; import java.nio.file.Path;
@ -120,10 +121,8 @@ public class OsProbe {
* *
* On Windows, this method returns {@code null}. * On Windows, this method returns {@code null}.
* *
* On Linux, this method should return the 1, 5, and 15-minute load * On Linux, this method returns the 1, 5, and 15-minute load
* averages. If obtaining these values from {@code /proc/loadavg} * averages.
* fails, the method will fallback to obtaining the 1-minute load
* average.
* *
* On macOS, this method should return the 1-minute load average. * On macOS, this method should return the 1-minute load average.
* *
@ -133,32 +132,33 @@ public class OsProbe {
if (Constants.WINDOWS) { if (Constants.WINDOWS) {
return null; return null;
} else if (Constants.LINUX) { } else if (Constants.LINUX) {
try {
final String procLoadAvg = readProcLoadavg(); final String procLoadAvg = readProcLoadavg();
if (procLoadAvg != null) {
assert procLoadAvg.matches("(\\d+\\.\\d+\\s+){3}\\d+/\\d+\\s+\\d+"); assert procLoadAvg.matches("(\\d+\\.\\d+\\s+){3}\\d+/\\d+\\s+\\d+");
final String[] fields = procLoadAvg.split("\\s+"); final String[] fields = procLoadAvg.split("\\s+");
try {
return new double[]{Double.parseDouble(fields[0]), Double.parseDouble(fields[1]), Double.parseDouble(fields[2])}; return new double[]{Double.parseDouble(fields[0]), Double.parseDouble(fields[1]), Double.parseDouble(fields[2])};
} catch (final NumberFormatException e) { } catch (final IOException e) {
if (logger.isDebugEnabled()) { if (logger.isDebugEnabled()) {
logger.debug(String.format(Locale.ROOT, "error parsing /proc/loadavg [%s]", procLoadAvg), e); logger.debug("error reading /proc/loadavg", e);
} }
return null;
} }
} } else {
// fallback assert Constants.MAC_OS_X;
}
if (getSystemLoadAverage == null) { if (getSystemLoadAverage == null) {
return null; return null;
} }
try { try {
final double oneMinuteLoadAverage = (double) getSystemLoadAverage.invoke(osMxBean); final double oneMinuteLoadAverage = (double) getSystemLoadAverage.invoke(osMxBean);
return new double[] { oneMinuteLoadAverage >= 0 ? oneMinuteLoadAverage : -1, -1, -1 }; return new double[]{oneMinuteLoadAverage >= 0 ? oneMinuteLoadAverage : -1, -1, -1};
} catch (final Exception e) { } catch (IllegalAccessException | InvocationTargetException e) {
logger.debug("error obtaining system load average", e); if (logger.isDebugEnabled()) {
logger.debug("error reading one minute load average from operating system", e);
}
return null; return null;
} }
} }
}
/** /**
* The line from {@code /proc/loadavg}. The first three fields are * The line from {@code /proc/loadavg}. The first three fields are
@ -171,15 +171,8 @@ public class OsProbe {
* @return the line from {@code /proc/loadavg} or {@code null} * @return the line from {@code /proc/loadavg} or {@code null}
*/ */
@SuppressForbidden(reason = "access /proc/loadavg") @SuppressForbidden(reason = "access /proc/loadavg")
String readProcLoadavg() { String readProcLoadavg() throws IOException {
try { return readSingleLine(PathUtils.get("/proc/loadavg"));
final List<String> lines = Files.readAllLines(PathUtils.get("/proc/loadavg"));
assert lines != null && lines.size() == 1;
return lines.get(0);
} catch (final IOException e) {
logger.debug("error reading /proc/loadavg", e);
return null;
}
} }
public short getSystemCpuPercent() { public short getSystemCpuPercent() {