|
10 | 10 |
|
11 | 11 | import org.apache.logging.log4j.LogManager; |
12 | 12 | import org.apache.logging.log4j.Logger; |
| 13 | +import org.elasticsearch.Version; |
13 | 14 | import org.elasticsearch.common.io.stream.StreamInput; |
14 | 15 | import org.elasticsearch.common.io.stream.StreamOutput; |
15 | 16 | import org.elasticsearch.common.io.stream.Writeable; |
@@ -184,10 +185,18 @@ public Swap(long total, long free) { |
184 | 185 | } |
185 | 186 |
|
186 | 187 | public Swap(StreamInput in) throws IOException { |
187 | | - this.total = in.readLong(); |
188 | | - assert total >= 0 : "expected total swap to be positive, got: " + total; |
189 | | - this.free = in.readLong(); |
190 | | - assert free >= 0 : "expected free swap to be positive, got: " + total; |
| 188 | + if (in.getVersion().onOrAfter(Version.V_7_8_0)) { |
| 189 | + this.total = in.readLong(); |
| 190 | + assert this.total >= 0 : "expected total swap to be positive, got: " + total; |
| 191 | + this.free = in.readLong(); |
| 192 | + assert this.free >= 0 : "expected free swap to be positive, got: " + total; |
| 193 | + } else { |
| 194 | + // If we have a node in the cluster without the bug fix for |
| 195 | + // negative memory values, we need to coerce negative values to 0 here. |
| 196 | + // The relevant bug fix was added for 7.8.0 in https://github.com/elastic/elasticsearch/pull/57317 |
| 197 | + this.total = Math.max(0, in.readLong()); |
| 198 | + this.free = Math.max(0, in.readLong()); |
| 199 | + } |
191 | 200 | } |
192 | 201 |
|
193 | 202 | @Override |
@@ -247,10 +256,18 @@ public Mem(long total, long free) { |
247 | 256 | } |
248 | 257 |
|
249 | 258 | public Mem(StreamInput in) throws IOException { |
250 | | - this.total = in.readLong(); |
251 | | - assert total >= 0 : "expected total memory to be positive, got: " + total; |
252 | | - this.free = in.readLong(); |
253 | | - assert free >= 0 : "expected free memory to be positive, got: " + total; |
| 259 | + if (in.getVersion().onOrAfter(Version.V_7_2_0)) { |
| 260 | + this.total = in.readLong(); |
| 261 | + assert total >= 0 : "expected total memory to be positive, got: " + total; |
| 262 | + this.free = in.readLong(); |
| 263 | + assert free >= 0 : "expected free memory to be positive, got: " + total; |
| 264 | + } else { |
| 265 | + // If we have a node in the cluster without the bug fix for |
| 266 | + // negative memory values, we need to coerce negative values to 0 here. |
| 267 | + // The relevant bug fix was added for 7.2.0 in https://github.com/elastic/elasticsearch/pull/42725 |
| 268 | + this.total = Math.max(0, in.readLong()); |
| 269 | + this.free = Math.max(0, in.readLong()); |
| 270 | + } |
254 | 271 | } |
255 | 272 |
|
256 | 273 | @Override |
|
0 commit comments