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 78a42c90805a4..68a20f0d97607 100644 --- a/server/src/main/java/org/elasticsearch/monitor/os/OsStats.java +++ b/server/src/main/java/org/elasticsearch/monitor/os/OsStats.java @@ -10,6 +10,7 @@ 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; import org.elasticsearch.common.io.stream.Writeable; @@ -184,10 +185,18 @@ public Swap(long total, long 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; + if (in.getVersion().onOrAfter(Version.V_7_8_0)) { + this.total = in.readLong(); + assert this.total >= 0 : "expected total swap to be positive, got: " + total; + this.free = in.readLong(); + assert this.free >= 0 : "expected free swap to be positive, got: " + total; + } else { + // If we have a node in the cluster without the bug fix for + // negative memory values, we need to coerce negative values to 0 here. + // The relevant bug fix was added for 7.8.0 in https://github.com/elastic/elasticsearch/pull/57317 + this.total = Math.max(0, in.readLong()); + this.free = Math.max(0, in.readLong()); + } } @Override @@ -247,10 +256,18 @@ public Mem(long total, long free) { } public Mem(StreamInput in) throws IOException { - this.total = in.readLong(); - assert total >= 0 : "expected total memory to be positive, got: " + total; - this.free = in.readLong(); - assert free >= 0 : "expected free memory to be positive, got: " + total; + if (in.getVersion().onOrAfter(Version.V_7_2_0)) { + this.total = in.readLong(); + assert total >= 0 : "expected total memory to be positive, got: " + total; + this.free = in.readLong(); + assert free >= 0 : "expected free memory to be positive, got: " + total; + } else { + // If we have a node in the cluster without the bug fix for + // negative memory values, we need to coerce negative values to 0 here. + // The relevant bug fix was added for 7.2.0 in https://github.com/elastic/elasticsearch/pull/42725 + this.total = Math.max(0, in.readLong()); + this.free = Math.max(0, in.readLong()); + } } @Override