Skip to content

Commit

Permalink
[6.8] Backport fixes for memory reporting issues (#68554)
Browse files Browse the repository at this point in the history
* Report used memory as zero when total memory cannot be obtained
* Do not report negative values for swap sizes (#57324)

Co-authored-by: Dan Hermann <danhermann@users.noreply.github.com>
  • Loading branch information
williamrandolph and danhermann committed Feb 4, 2021
1 parent dab5822 commit b865f64
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 9 deletions.
31 changes: 23 additions & 8 deletions server/src/main/java/org/elasticsearch/monitor/os/OsProbe.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,9 @@
* The {@link OsProbe} class retrieves information about the physical and swap size of the machine
* memory, as well as the system load average and cpu load.
*
* In some exceptional cases, it's possible the underlying native method used by
* {@link #getFreePhysicalMemorySize()} and {@link #getTotalPhysicalMemorySize()} can return a
* In some exceptional cases, it's possible the underlying native methods used by
* {@link #getFreePhysicalMemorySize()}, {@link #getTotalPhysicalMemorySize()},
* {@link #getFreeSwapSpaceSize()}, and {@link #getTotalSwapSpaceSize()} can return a
* negative value. Because of this, we prevent those methods from returning negative values,
* returning 0 instead.
*
Expand Down Expand Up @@ -127,12 +128,19 @@ public long getTotalPhysicalMemorySize() {
*/
public long getFreeSwapSpaceSize() {
if (getFreeSwapSpaceSize == null) {
return -1;
logger.warn("getFreeSwapSpaceSize is not available");
return 0;
}
try {
return (long) getFreeSwapSpaceSize.invoke(osMxBean);
final long mem = (long) getFreeSwapSpaceSize.invoke(osMxBean);
if (mem < 0) {
logger.warn("OS reported a negative free swap space size [{}]", mem);
return 0;
}
return mem;
} catch (Exception e) {
return -1;
logger.warn("exception retrieving free swap space size", e);
return 0;
}
}

Expand All @@ -141,12 +149,19 @@ public long getFreeSwapSpaceSize() {
*/
public long getTotalSwapSpaceSize() {
if (getTotalSwapSpaceSize == null) {
return -1;
logger.warn("getTotalSwapSpaceSize is not available");
return 0;
}
try {
return (long) getTotalSwapSpaceSize.invoke(osMxBean);
final long mem = (long) getTotalSwapSpaceSize.invoke(osMxBean);
if (mem < 0) {
logger.warn("OS reported a negative total swap space size [{}]", mem);
return 0;
}
return mem;
} catch (Exception e) {
return -1;
logger.warn("exception retrieving total swap space size", e);
return 0;
}
}

Expand Down
31 changes: 31 additions & 0 deletions server/src/main/java/org/elasticsearch/monitor/os/OsStats.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -187,17 +189,23 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws

public static class Swap implements Writeable, ToXContentFragment {

private static final Logger logger = LogManager.getLogger(Swap.class);

private final long total;
private final long free;

public Swap(long total, long free) {
assert total >= 0 : "expected total swap to be positive, got: " + total;
assert free >= 0 : "expected free swap to be positive, got: " + total;
this.total = total;
this.free = free;
}

public Swap(StreamInput in) throws IOException {
this.total = in.readLong();
assert total >= 0 : "expected total swap to be positive, got: " + total;
this.free = in.readLong();
assert free >= 0 : "expected free swap to be positive, got: " + total;
}

@Override
Expand All @@ -211,6 +219,17 @@ public ByteSizeValue getFree() {
}

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. Swap can similarly be reported as negative and in
// those cases, we force it to zero in which case we can no longer correctly report the used swap
// 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 swap when total swap is 0 and free swap is " + free);
return new ByteSizeValue(0);
}
return new ByteSizeValue(total - free);
}

Expand All @@ -231,6 +250,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;

Expand Down Expand Up @@ -259,6 +280,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);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@

import java.io.IOException;

import static org.hamcrest.Matchers.equalTo;

public class OsStatsTests extends ESTestCase {

public void testSerialization() throws IOException {
Expand Down Expand Up @@ -81,4 +83,13 @@ public void testSerialization() throws IOException {
}
}

public void testGetUsedMemoryWithZeroTotal() {
OsStats.Mem mem = new OsStats.Mem(0, randomNonNegativeLong());
assertThat(mem.getUsed().getBytes(), equalTo(0L));
}

public void testGetUsedSwapWithZeroTotal() {
OsStats.Swap swap = new OsStats.Swap(0, randomNonNegativeLong());
assertThat(swap.getUsed().getBytes(), equalTo(0L));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,7 @@ private static NodeStats mockNodeStats() {
"_memory_ctrl_group", "2000000000", "1000000000");

final OsStats.Mem osMem = new OsStats.Mem(0, 0);
final OsStats.Swap osSwap = new OsStats.Swap(no, no);
final OsStats.Swap osSwap = new OsStats.Swap(0, 0);
final OsStats os = new OsStats(no, osCpu, osMem, osSwap, osCgroup);

// Process
Expand Down

0 comments on commit b865f64

Please sign in to comment.