Skip to content

Commit 4a5f80a

Browse files
committed
Do not report negative values for swap sizes (elastic#57317)
1 parent d204a97 commit 4a5f80a

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
@@ -183,17 +183,23 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws
183183

184184
public static class Swap implements Writeable, ToXContentFragment {
185185

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

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

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

199205
@Override
@@ -207,6 +213,17 @@ public ByteSizeValue getFree() {
207213
}
208214

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

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
@@ -330,7 +330,7 @@ private static NodeStats mockNodeStats() {
330330
"_memory_ctrl_group", "2000000000", "1000000000");
331331

332332
final OsStats.Mem osMem = new OsStats.Mem(0, 0);
333-
final OsStats.Swap osSwap = new OsStats.Swap(no, no);
333+
final OsStats.Swap osSwap = new OsStats.Swap(0, 0);
334334
final OsStats os = new OsStats(no, osCpu, osMem, osSwap, osCgroup);
335335

336336
// Process

0 commit comments

Comments
 (0)