From 2aa94a5133a909c3072ec228b37034124fb6eceb Mon Sep 17 00:00:00 2001 From: Jason Tedor Date: Mon, 18 Jan 2016 12:00:33 -0500 Subject: [PATCH 1/5] Normalize unavailable load average This commit normalizes the one-minute load average obtained from OperatingSystemMXBean#getSystemLoadAverage to -1 when it is not available. This is to reflect the Javadocs for this method saying "If the load average is not available, a negative value is returned." --- core/src/main/java/org/elasticsearch/monitor/os/OsProbe.java | 2 +- .../test/java/org/elasticsearch/monitor/os/OsProbeTests.java | 3 +-- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/core/src/main/java/org/elasticsearch/monitor/os/OsProbe.java b/core/src/main/java/org/elasticsearch/monitor/os/OsProbe.java index 077b4218fa9..3322c05fc97 100644 --- a/core/src/main/java/org/elasticsearch/monitor/os/OsProbe.java +++ b/core/src/main/java/org/elasticsearch/monitor/os/OsProbe.java @@ -124,7 +124,7 @@ public class OsProbe { } try { double oneMinuteLoadAverage = (double) getSystemLoadAverage.invoke(osMxBean); - return new double[] { oneMinuteLoadAverage, -1, -1 }; + return new double[] { oneMinuteLoadAverage >= 0 ? oneMinuteLoadAverage : -1, -1, -1 }; } catch (Throwable t) { return null; } diff --git a/core/src/test/java/org/elasticsearch/monitor/os/OsProbeTests.java b/core/src/test/java/org/elasticsearch/monitor/os/OsProbeTests.java index 4f4319e212c..83007c5a86c 100644 --- a/core/src/test/java/org/elasticsearch/monitor/os/OsProbeTests.java +++ b/core/src/test/java/org/elasticsearch/monitor/os/OsProbeTests.java @@ -29,7 +29,6 @@ import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.greaterThan; import static org.hamcrest.Matchers.greaterThanOrEqualTo; import static org.hamcrest.Matchers.is; -import static org.hamcrest.Matchers.lessThan; import static org.hamcrest.Matchers.lessThanOrEqualTo; public class OsProbeTests extends ESTestCase { @@ -71,7 +70,7 @@ public class OsProbeTests extends ESTestCase { // one minute load average is available, but 10-minute and 15-minute load averages are not // load average can be negative if not available or not computed yet, otherwise it should be >= 0 if (loadAverage != null) { - assertThat(loadAverage[0], anyOf(lessThan((double) 0), greaterThanOrEqualTo((double) 0))); + assertThat(loadAverage[0], anyOf(equalTo((double) -1), greaterThanOrEqualTo((double) 0))); assertThat(loadAverage[1], equalTo((double) -1)); assertThat(loadAverage[2], equalTo((double) -1)); } From e5013d16f00bae7c76585a597e5d5b70bbfed890 Mon Sep 17 00:00:00 2001 From: Jason Tedor Date: Mon, 18 Jan 2016 13:33:48 -0500 Subject: [PATCH 2/5] Fix test for load average on FreeBSD This commit fixes the test for load averages on FreeBSD. On FreeBSD, it is either the case that linprocfs is mounted at /compat/linux/proc in which case the load averages are available, or this is not the case and no load average are available. Previously, the test on FreeBSD was falling back to the catch all case which asserts that the five-minute and fifteen-minute load averages are not available. --- .../java/org/elasticsearch/monitor/os/OsProbeTests.java | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/core/src/test/java/org/elasticsearch/monitor/os/OsProbeTests.java b/core/src/test/java/org/elasticsearch/monitor/os/OsProbeTests.java index 83007c5a86c..8dd80231623 100644 --- a/core/src/test/java/org/elasticsearch/monitor/os/OsProbeTests.java +++ b/core/src/test/java/org/elasticsearch/monitor/os/OsProbeTests.java @@ -66,6 +66,12 @@ public class OsProbeTests extends ESTestCase { assertThat(loadAverage[0], greaterThanOrEqualTo((double) 0)); assertThat(loadAverage[1], greaterThanOrEqualTo((double) 0)); assertThat(loadAverage[2], greaterThanOrEqualTo((double) 0)); + } else if (Constants.FREE_BSD) { + // five- and fifteen-minute load averages not available if linprocfs is not mounted at /compat/linux/proc + assertNotNull(loadAverage); + assertThat(loadAverage[0], greaterThanOrEqualTo((double) 0)); + assertThat(loadAverage[1], anyOf(equalTo((double) -1), greaterThanOrEqualTo((double) 0))); + assertThat(loadAverage[2], anyOf(equalTo((double) -1), greaterThanOrEqualTo((double) 0))); } else { // one minute load average is available, but 10-minute and 15-minute load averages are not // load average can be negative if not available or not computed yet, otherwise it should be >= 0 From d55952a90d1c71a5db0fb89e1ab860b6c73de140 Mon Sep 17 00:00:00 2001 From: Jason Tedor Date: Mon, 18 Jan 2016 13:45:00 -0500 Subject: [PATCH 3/5] Tigthen load average test assertions This commit tightens the load average test assertions by separating out OS X as a system where we can make tighter assertions about the load average values. --- .../java/org/elasticsearch/monitor/os/OsProbeTests.java | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/core/src/test/java/org/elasticsearch/monitor/os/OsProbeTests.java b/core/src/test/java/org/elasticsearch/monitor/os/OsProbeTests.java index 8dd80231623..5aa1899c61d 100644 --- a/core/src/test/java/org/elasticsearch/monitor/os/OsProbeTests.java +++ b/core/src/test/java/org/elasticsearch/monitor/os/OsProbeTests.java @@ -72,9 +72,14 @@ public class OsProbeTests extends ESTestCase { assertThat(loadAverage[0], greaterThanOrEqualTo((double) 0)); assertThat(loadAverage[1], anyOf(equalTo((double) -1), greaterThanOrEqualTo((double) 0))); assertThat(loadAverage[2], anyOf(equalTo((double) -1), greaterThanOrEqualTo((double) 0))); - } else { + } else if (Constants.MAC_OS_X) { // one minute load average is available, but 10-minute and 15-minute load averages are not - // load average can be negative if not available or not computed yet, otherwise it should be >= 0 + assertNotNull(loadAverage); + assertThat(loadAverage[0], greaterThanOrEqualTo((double) 0)); + assertThat(loadAverage[1], equalTo((double) -1)); + assertThat(loadAverage[2], equalTo((double) -1)); + } else { + // unknown system, but the best case is that we have the one-minute load average if (loadAverage != null) { assertThat(loadAverage[0], anyOf(equalTo((double) -1), greaterThanOrEqualTo((double) 0))); assertThat(loadAverage[1], equalTo((double) -1)); From f5b72b0714095cc6b4fe9d302e1ce858b570ba4e Mon Sep 17 00:00:00 2001 From: Jason Tedor Date: Mon, 18 Jan 2016 14:00:15 -0500 Subject: [PATCH 4/5] Load average on Windows is never available Since load average is never available on Windows, this commit modifies the handling of load average there to just always return null. --- .../src/main/java/org/elasticsearch/monitor/os/OsProbe.java | 3 +++ .../java/org/elasticsearch/monitor/os/OsProbeTests.java | 6 +----- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/core/src/main/java/org/elasticsearch/monitor/os/OsProbe.java b/core/src/main/java/org/elasticsearch/monitor/os/OsProbe.java index 3322c05fc97..5ee2232068f 100644 --- a/core/src/main/java/org/elasticsearch/monitor/os/OsProbe.java +++ b/core/src/main/java/org/elasticsearch/monitor/os/OsProbe.java @@ -119,6 +119,9 @@ public class OsProbe { } // fallback } + if (Constants.WINDOWS) { + return null; + } if (getSystemLoadAverage == null) { return null; } diff --git a/core/src/test/java/org/elasticsearch/monitor/os/OsProbeTests.java b/core/src/test/java/org/elasticsearch/monitor/os/OsProbeTests.java index 5aa1899c61d..2edaad5c4ba 100644 --- a/core/src/test/java/org/elasticsearch/monitor/os/OsProbeTests.java +++ b/core/src/test/java/org/elasticsearch/monitor/os/OsProbeTests.java @@ -55,11 +55,7 @@ public class OsProbeTests extends ESTestCase { } if (Constants.WINDOWS) { // load average is unavailable on Windows - if (loadAverage != null) { - assertThat(loadAverage[0], equalTo((double) -1)); - assertThat(loadAverage[1], equalTo((double) -1)); - assertThat(loadAverage[2], equalTo((double) -1)); - } + assertNull(loadAverage); } else if (Constants.LINUX) { // we should be able to get the load average assertNotNull(loadAverage); From e62221168ea5f2a259ecf321f0dff1f40ffe12c3 Mon Sep 17 00:00:00 2001 From: Jason Tedor Date: Mon, 18 Jan 2016 14:01:48 -0500 Subject: [PATCH 5/5] No load average if the values are meaningless This commit modifies the presentation of load average to not be present in the response if all of the values are meaningless. --- core/src/main/java/org/elasticsearch/monitor/os/OsStats.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/core/src/main/java/org/elasticsearch/monitor/os/OsStats.java b/core/src/main/java/org/elasticsearch/monitor/os/OsStats.java index c419c4f2608..569f8825aa9 100644 --- a/core/src/main/java/org/elasticsearch/monitor/os/OsStats.java +++ b/core/src/main/java/org/elasticsearch/monitor/os/OsStats.java @@ -28,6 +28,7 @@ import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentBuilderString; import java.io.IOException; +import java.util.Arrays; /** * @@ -89,7 +90,7 @@ public class OsStats implements Streamable, ToXContent { if (cpu != null) { builder.startObject(Fields.CPU); builder.field(Fields.PERCENT, cpu.getPercent()); - if (cpu.getLoadAverage() != null) { + if (cpu.getLoadAverage() != null && Arrays.stream(cpu.getLoadAverage()).anyMatch(load -> load != -1)) { builder.startObject(Fields.LOAD_AVERAGE); if (cpu.getLoadAverage()[0] != -1) { builder.field(Fields.LOAD_AVERAGE_1M, cpu.getLoadAverage()[0]);