From 6c7f0f06a10a7141f7c617f506fada0235888648 Mon Sep 17 00:00:00 2001 From: Dan Hermann Date: Fri, 8 May 2020 05:49:02 -0500 Subject: [PATCH 1/2] Report used memory as zero when total memory cannot be obtained --- .../java/org/elasticsearch/monitor/os/OsStats.java | 12 ++++++++++++ .../org/elasticsearch/monitor/os/OsStatsTests.java | 7 +++++++ 2 files changed, 19 insertions(+) diff --git a/server/src/main/java/org/elasticsearch/monitor/os/OsStats.java b/server/src/main/java/org/elasticsearch/monitor/os/OsStats.java index f4ddd441c8e7b..b4904e44a4cf7 100644 --- a/server/src/main/java/org/elasticsearch/monitor/os/OsStats.java +++ b/server/src/main/java/org/elasticsearch/monitor/os/OsStats.java @@ -225,6 +225,8 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws public static class Mem implements Writeable, ToXContentFragment { + private static final Logger logger = LogManager.getLogger(Mem.class); + private final long total; private final long free; @@ -253,6 +255,16 @@ public ByteSizeValue getTotal() { } public ByteSizeValue getUsed() { + if (total == 0) { + // The work in https://github.com/elastic/elasticsearch/pull/42725 established that total memory + // can be reported as negative in some cases. In those cases, we force it to zero in which case + // we can no longer correctly report the used memory as (total-free) and should report it as zero. + // + // We intentionally check for (total == 0) rather than (total - free < 0) so as not to hide + // cases where (free > total) which would be a different bug. + logger.warn("cannot compute used memory when total memory is 0 and free memory is " + free); + return new ByteSizeValue(0); + } return new ByteSizeValue(total - free); } diff --git a/server/src/test/java/org/elasticsearch/monitor/os/OsStatsTests.java b/server/src/test/java/org/elasticsearch/monitor/os/OsStatsTests.java index 8c0820fc5b589..3efb0549fe10e 100644 --- a/server/src/test/java/org/elasticsearch/monitor/os/OsStatsTests.java +++ b/server/src/test/java/org/elasticsearch/monitor/os/OsStatsTests.java @@ -25,6 +25,8 @@ import java.io.IOException; +import static org.hamcrest.Matchers.equalTo; + public class OsStatsTests extends ESTestCase { public void testSerialization() throws IOException { @@ -81,4 +83,9 @@ public void testSerialization() throws IOException { } } + public void testGetUsedMemoryWithZeroTotal() { + OsStats.Mem mem = new OsStats.Mem(0, 1); + assertThat(mem.getUsed().getBytes(), equalTo(0L)); + } + } From 6ece2ce834eb7aa2ab29fc16df529615591b3d84 Mon Sep 17 00:00:00 2001 From: Dan Hermann Date: Fri, 8 May 2020 06:05:33 -0500 Subject: [PATCH 2/2] add missing import --- server/src/main/java/org/elasticsearch/monitor/os/OsStats.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/server/src/main/java/org/elasticsearch/monitor/os/OsStats.java b/server/src/main/java/org/elasticsearch/monitor/os/OsStats.java index b4904e44a4cf7..3b8809e50e14f 100644 --- a/server/src/main/java/org/elasticsearch/monitor/os/OsStats.java +++ b/server/src/main/java/org/elasticsearch/monitor/os/OsStats.java @@ -19,6 +19,8 @@ package org.elasticsearch.monitor.os; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; import org.elasticsearch.Version; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput;