Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[7.7] Report used memory as zero when total memory cannot be obtained #56905

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 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 @@ -225,6 +227,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 @@ -253,6 +257,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,9 @@ public void testSerialization() throws IOException {
}
}

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

}