Skip to content

Commit b001bab

Browse files
authored
Do not report negative values for swap sizes (#57317)
1 parent 29e9e79 commit b001bab

File tree

4 files changed

+46
-10
lines changed

4 files changed

+46
-10
lines changed

server/src/main/java/org/elasticsearch/monitor/os/OsProbe.java

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,9 @@
4646
* The {@link OsProbe} class retrieves information about the physical and swap size of the machine
4747
* memory, as well as the system load average and cpu load.
4848
*
49-
* In some exceptional cases, it's possible the underlying native method used by
50-
* {@link #getFreePhysicalMemorySize()} and {@link #getTotalPhysicalMemorySize()} can return a
49+
* In some exceptional cases, it's possible the underlying native methods used by
50+
* {@link #getFreePhysicalMemorySize()}, {@link #getTotalPhysicalMemorySize()},
51+
* {@link #getFreeSwapSpaceSize()}, and {@link #getTotalSwapSpaceSize()} can return a
5152
* negative value. Because of this, we prevent those methods from returning negative values,
5253
* returning 0 instead.
5354
*
@@ -127,12 +128,19 @@ public long getTotalPhysicalMemorySize() {
127128
*/
128129
public long getFreeSwapSpaceSize() {
129130
if (getFreeSwapSpaceSize == null) {
130-
return -1;
131+
logger.warn("getFreeSwapSpaceSize is not available");
132+
return 0;
131133
}
132134
try {
133-
return (long) getFreeSwapSpaceSize.invoke(osMxBean);
135+
final long mem = (long) getFreeSwapSpaceSize.invoke(osMxBean);
136+
if (mem < 0) {
137+
logger.warn("OS reported a negative free swap space size [{}]", mem);
138+
return 0;
139+
}
140+
return mem;
134141
} catch (Exception e) {
135-
return -1;
142+
logger.warn("exception retrieving free swap space size", e);
143+
return 0;
136144
}
137145
}
138146

@@ -141,12 +149,19 @@ public long getFreeSwapSpaceSize() {
141149
*/
142150
public long getTotalSwapSpaceSize() {
143151
if (getTotalSwapSpaceSize == null) {
144-
return -1;
152+
logger.warn("getTotalSwapSpaceSize is not available");
153+
return 0;
145154
}
146155
try {
147-
return (long) getTotalSwapSpaceSize.invoke(osMxBean);
156+
final long mem = (long) getTotalSwapSpaceSize.invoke(osMxBean);
157+
if (mem < 0) {
158+
logger.warn("OS reported a negative total swap space size [{}]", mem);
159+
return 0;
160+
}
161+
return mem;
148162
} catch (Exception e) {
149-
return -1;
163+
logger.warn("exception retrieving total swap space size", e);
164+
return 0;
150165
}
151166
}
152167

server/src/main/java/org/elasticsearch/monitor/os/OsStats.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,17 +182,23 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws
182182

183183
public static class Swap implements Writeable, ToXContentFragment {
184184

185+
private static final Logger logger = LogManager.getLogger(Swap.class);
186+
185187
private final long total;
186188
private final long free;
187189

188190
public Swap(long total, long free) {
191+
assert total >= 0 : "expected total swap to be positive, got: " + total;
192+
assert free >= 0 : "expected free swap to be positive, got: " + total;
189193
this.total = total;
190194
this.free = free;
191195
}
192196

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

198204
@Override
@@ -206,6 +212,17 @@ public ByteSizeValue getFree() {
206212
}
207213

208214
public ByteSizeValue getUsed() {
215+
if (total == 0) {
216+
// The work in https://github.com/elastic/elasticsearch/pull/42725 established that total memory
217+
// can be reported as negative in some cases. Swap can similarly be reported as negative and in
218+
// those cases, we force it to zero in which case we can no longer correctly report the used swap
219+
// as (total-free) and should report it as zero.
220+
//
221+
// We intentionally check for (total == 0) rather than (total - free < 0) so as not to hide
222+
// cases where (free > total) which would be a different bug.
223+
logger.warn("cannot compute used swap when total swap is 0 and free swap is " + free);
224+
return new ByteSizeValue(0);
225+
}
209226
return new ByteSizeValue(total - free);
210227
}
211228

server/src/test/java/org/elasticsearch/monitor/os/OsStatsTests.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,12 @@ public void testSerialization() throws IOException {
8484
}
8585

8686
public void testGetUsedMemoryWithZeroTotal() {
87-
OsStats.Mem mem = new OsStats.Mem(0, 1);
87+
OsStats.Mem mem = new OsStats.Mem(0, randomLong());
8888
assertThat(mem.getUsed().getBytes(), equalTo(0L));
8989
}
9090

91+
public void testGetUsedSwapWithZeroTotal() {
92+
OsStats.Swap swap = new OsStats.Swap(0, randomLong());
93+
assertThat(swap.getUsed().getBytes(), equalTo(0L));
94+
}
9195
}

x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/collector/node/NodeStatsMonitoringDocTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -340,7 +340,7 @@ private static NodeStats mockNodeStats() {
340340
"_memory_ctrl_group", "2000000000", "1000000000");
341341

342342
final OsStats.Mem osMem = new OsStats.Mem(0, 0);
343-
final OsStats.Swap osSwap = new OsStats.Swap(no, no);
343+
final OsStats.Swap osSwap = new OsStats.Swap(0, 0);
344344
final OsStats os = new OsStats(no, osCpu, osMem, osSwap, osCgroup);
345345

346346
// Process

0 commit comments

Comments
 (0)