Skip to content

Commit 449cf2a

Browse files
author
David Roberts
committed
Using always present adjusted_total instead of optional total_override
1 parent 4e7b3c1 commit 449cf2a

File tree

12 files changed

+49
-135
lines changed

12 files changed

+49
-135
lines changed

docs/reference/cluster/nodes-stats.asciidoc

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1036,16 +1036,17 @@ Total amount of physical memory.
10361036
(integer)
10371037
Total amount of physical memory in bytes.
10381038

1039-
`total_override`::
1039+
`adjusted_total`::
10401040
(<<byte-units,byte value>>)
10411041
If the amount of physical memory has been overridden using the `es.total_memory_bytes`
1042-
system property then this reports the overridden value. Otherwise it is not present.
1042+
system property then this reports the overridden value. Otherwise it reports the same
1043+
value as `total`.
10431044

1044-
`total_override_in_bytes`::
1045+
`adjusted_total_in_bytes`::
10451046
(integer)
10461047
If the amount of physical memory has been overridden using the `es.total_memory_bytes`
1047-
system property then this reports the overridden value in bytes. Otherwise it is not
1048-
present.
1048+
system property then this reports the overridden value in bytes. Otherwise it reports
1049+
the same value as `total_in_bytes`.
10491050

10501051
`free`::
10511052
(<<byte-units,byte value>>)

docs/reference/cluster/stats.asciidoc

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -916,17 +916,17 @@ Total amount of physical memory across all selected nodes.
916916
(integer)
917917
Total amount, in bytes, of physical memory across all selected nodes.
918918
919-
`total_override`::
919+
`adjusted_total`::
920920
(<<byte-units,byte value>>)
921-
If the amount of physical memory has been overridden using the `es.total_memory_bytes`
922-
system property on all selected nodes then this reports the sum of the overridden
923-
values. Otherwise it is not present.
921+
Total amount of memory across all selected nodes, but using the value specified
922+
using the `es.total_memory_bytes` system property instead of measured total
923+
memory for those nodes where that system property was set.
924924
925-
`total_override_in_bytes`::
925+
`adjusted_total_in_bytes`::
926926
(integer)
927-
If the amount of physical memory has been overridden using the `es.total_memory_bytes`
928-
system property on all selected nodes then this reports the sum of the overridden
929-
values in bytes. Otherwise it is not present.
927+
Total amount, in bytes, of memory across all selected nodes, but using the
928+
value specified using the `es.total_memory_bytes` system property instead
929+
of measured total memory for those nodes where that system property was set.
930930
931931
`free`::
932932
(<<byte-units, byte units>>)

server/src/main/java/org/elasticsearch/action/admin/cluster/stats/ClusterStatsNodes.java

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,7 @@ private OsStats(List<NodeInfo> nodeInfos, List<NodeStats> nodeStatsList) {
266266
this.allocatedProcessors = allocatedProcessors;
267267

268268
long totalMemory = 0;
269-
Long totalMemoryOverride = 0L;
269+
long adjustedTotalMemory = 0;
270270
long freeMemory = 0;
271271
for (NodeStats nodeStats : nodeStatsList) {
272272
if (nodeStats.getOs() != null) {
@@ -275,24 +275,17 @@ private OsStats(List<NodeInfo> nodeInfos, List<NodeStats> nodeStatsList) {
275275
if (total > 0) {
276276
totalMemory += total;
277277
}
278-
// Only report a total memory override for the whole cluster if every node has overridden total memory
279-
if (totalMemoryOverride != null) {
280-
if (mem.getTotalOverride() != null) {
281-
long totalOverride = mem.getTotalOverride().getBytes();
282-
if (totalOverride > 0) {
283-
totalMemoryOverride += totalOverride;
284-
}
285-
} else {
286-
totalMemoryOverride = null;
287-
}
278+
long adjustedTotal = mem.getAdjustedTotal().getBytes();
279+
if (total > 0) {
280+
adjustedTotalMemory += adjustedTotal;
288281
}
289282
long free = nodeStats.getOs().getMem().getFree().getBytes();
290283
if (free > 0) {
291284
freeMemory += free;
292285
}
293286
}
294287
}
295-
this.mem = new org.elasticsearch.monitor.os.OsStats.Mem(totalMemory, totalMemoryOverride, freeMemory);
288+
this.mem = new org.elasticsearch.monitor.os.OsStats.Mem(totalMemory, adjustedTotalMemory, freeMemory);
296289
}
297290

298291
public int getAvailableProcessors() {

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

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -128,14 +128,15 @@ public long getTotalPhysicalMemorySize() {
128128
}
129129

130130
/**
131-
* Returns the overridden total amount of physical memory in bytes.
131+
* Returns the adjusted total amount of physical memory in bytes.
132132
* Total memory may be overridden when some other process is running
133-
* that is known to consume a non-negligible amount of memory. This
134-
* is read from the "es.total_memory_bytes" system property. Negative
135-
* values or not set at all mean no override.
133+
* that is known to consume a non-negligible amount of memory. This is
134+
* read from the "es.total_memory_bytes" system property. Negative values
135+
* or not set at all mean no override. When there is no override this
136+
* method returns the same value as {@link #getTotalPhysicalMemorySize}.
136137
*/
137-
public Long getTotalMemoryOverride() {
138-
return getTotalMemoryOverride(memoryOverrideProperty);
138+
public long getAdjustedTotalMemorySize() {
139+
return Optional.ofNullable(getTotalMemoryOverride(memoryOverrideProperty)).orElse(getTotalPhysicalMemorySize());
139140
}
140141

141142
static Long getTotalMemoryOverride(String memoryOverrideProperty) {
@@ -883,7 +884,7 @@ OsStats.Cgroup getCgroup(boolean isLinux) {
883884

884885
public OsStats osStats() {
885886
final OsStats.Cpu cpu = new OsStats.Cpu(getSystemCpuPercent(), getSystemLoadAverage());
886-
final OsStats.Mem mem = new OsStats.Mem(getTotalPhysicalMemorySize(), getTotalMemoryOverride(), getFreePhysicalMemorySize());
887+
final OsStats.Mem mem = new OsStats.Mem(getTotalPhysicalMemorySize(), getAdjustedTotalMemorySize(), getFreePhysicalMemorySize());
887888
final OsStats.Swap swap = new OsStats.Swap(getTotalSwapSpaceSize(), getFreeSwapSpaceSize());
888889
final OsStats.Cgroup cgroup = getCgroup(Constants.LINUX);
889890
return new OsStats(System.currentTimeMillis(), cpu, mem, swap, cgroup);

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

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,8 @@ static final class Fields {
9191
static final String USED_IN_BYTES = "used_in_bytes";
9292
static final String TOTAL = "total";
9393
static final String TOTAL_IN_BYTES = "total_in_bytes";
94-
static final String TOTAL_OVERRIDE = "total_override";
95-
static final String TOTAL_OVERRIDE_IN_BYTES = "total_override_in_bytes";
94+
static final String ADJUSTED_TOTAL = "adjusted_total";
95+
static final String ADJUSTED_TOTAL_IN_BYTES = "adjusted_total_in_bytes";
9696

9797
static final String FREE_PERCENT = "free_percent";
9898
static final String USED_PERCENT = "used_percent";
@@ -240,37 +240,36 @@ public static class Mem implements Writeable, ToXContentFragment {
240240
private static final Logger logger = LogManager.getLogger(Mem.class);
241241

242242
private final long total;
243-
private final Long totalOverride;
243+
private final long adjustedTotal;
244244
private final long free;
245245

246-
public Mem(long total, Long totalOverride, long free) {
246+
public Mem(long total, long adjustedTotal, long free) {
247247
assert total >= 0 : "expected total memory to be positive, got: " + total;
248-
assert totalOverride == null || totalOverride >= 0 : "expected total overridden memory to be positive, got: " + totalOverride;
248+
assert adjustedTotal >= 0 : "expected adjusted total memory to be positive, got: " + adjustedTotal;
249249
assert free >= 0 : "expected free memory to be positive, got: " + free;
250250
this.total = total;
251-
this.totalOverride = totalOverride;
251+
this.adjustedTotal = adjustedTotal;
252252
this.free = free;
253253
}
254254

255255
public Mem(StreamInput in) throws IOException {
256-
this.total = in.readLong();
256+
total = in.readLong();
257257
assert total >= 0 : "expected total memory to be positive, got: " + total;
258258
if (in.getVersion().onOrAfter(Version.V_8_0_0)) {
259-
this.totalOverride = in.readOptionalLong();
260-
assert totalOverride == null || totalOverride >= 0
261-
: "expected total overridden memory to be positive, got: " + totalOverride;
259+
adjustedTotal = in.readLong();
260+
assert adjustedTotal >= 0 : "expected adjusted total memory to be positive, got: " + adjustedTotal;
262261
} else {
263-
this.totalOverride = null;
262+
adjustedTotal = total;
264263
}
265-
this.free = in.readLong();
264+
free = in.readLong();
266265
assert free >= 0 : "expected free memory to be positive, got: " + free;
267266
}
268267

269268
@Override
270269
public void writeTo(StreamOutput out) throws IOException {
271270
out.writeLong(total);
272271
if (out.getVersion().onOrAfter(Version.V_8_0_0)) {
273-
out.writeOptionalLong(totalOverride);
272+
out.writeLong(adjustedTotal);
274273
}
275274
out.writeLong(free);
276275
}
@@ -279,8 +278,8 @@ public ByteSizeValue getTotal() {
279278
return new ByteSizeValue(total);
280279
}
281280

282-
public ByteSizeValue getTotalOverride() {
283-
return totalOverride == null ? null : new ByteSizeValue(totalOverride);
281+
public ByteSizeValue getAdjustedTotal() {
282+
return new ByteSizeValue(adjustedTotal);
284283
}
285284

286285
public ByteSizeValue getUsed() {
@@ -315,9 +314,7 @@ public short getFreePercent() {
315314
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
316315
builder.startObject(Fields.MEM);
317316
builder.humanReadableField(Fields.TOTAL_IN_BYTES, Fields.TOTAL, getTotal());
318-
if (getTotalOverride() != null) {
319-
builder.humanReadableField(Fields.TOTAL_OVERRIDE_IN_BYTES, Fields.TOTAL_OVERRIDE, getTotalOverride());
320-
}
317+
builder.humanReadableField(Fields.ADJUSTED_TOTAL_IN_BYTES, Fields.ADJUSTED_TOTAL, getAdjustedTotal());
321318
builder.humanReadableField(Fields.FREE_IN_BYTES, Fields.FREE, getFree());
322319
builder.humanReadableField(Fields.USED_IN_BYTES, Fields.USED, getUsed());
323320
builder.field(Fields.FREE_PERCENT, getFreePercent());

server/src/test/java/org/elasticsearch/action/admin/cluster/node/stats/NodeStatsTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -433,7 +433,7 @@ public static NodeStats createNodeStats() {
433433
long memTotal = randomNonNegativeLong();
434434
long swapTotal = randomNonNegativeLong();
435435
osStats = new OsStats(System.currentTimeMillis(), new OsStats.Cpu(randomShort(), loadAverages),
436-
new OsStats.Mem(memTotal, randomFrom(randomLongBetween(1, memTotal), null), randomLongBetween(0, memTotal)),
436+
new OsStats.Mem(memTotal, randomLongBetween(0, memTotal), randomLongBetween(0, memTotal)),
437437
new OsStats.Swap(swapTotal, randomLongBetween(0, swapTotal)),
438438
new OsStats.Cgroup(
439439
randomAlphaOfLength(8),

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public void testSerialization() throws IOException {
2626
}
2727
OsStats.Cpu cpu = new OsStats.Cpu(randomShort(), loadAverages);
2828
long memTotal = randomNonNegativeLong();
29-
OsStats.Mem mem = new OsStats.Mem(memTotal, randomFrom(randomLongBetween(1, memTotal), null), randomLongBetween(0, memTotal));
29+
OsStats.Mem mem = new OsStats.Mem(memTotal, randomLongBetween(0, memTotal), randomLongBetween(0, memTotal));
3030
long swapTotal = randomNonNegativeLong();
3131
OsStats.Swap swap = new OsStats.Swap(swapTotal, randomLongBetween(0, swapTotal));
3232
OsStats.Cgroup cgroup = new OsStats.Cgroup(
@@ -73,7 +73,7 @@ public void testSerialization() throws IOException {
7373
}
7474

7575
public void testGetUsedMemoryWithZeroTotal() {
76-
OsStats.Mem mem = new OsStats.Mem(0, null, randomNonNegativeLong());
76+
OsStats.Mem mem = new OsStats.Mem(0, 0, randomNonNegativeLong());
7777
assertThat(mem.getUsed().getBytes(), equalTo(0L));
7878
}
7979

x-pack/plugin/autoscaling/src/test/java/org/elasticsearch/xpack/autoscaling/capacity/memory/AutoscalingMemoryInfoServiceTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -347,7 +347,7 @@ private static NodeStats statsForNode(DiscoveryNode node, long memory) {
347347
OsStats osStats = new OsStats(
348348
randomNonNegativeLong(),
349349
new OsStats.Cpu(randomShort(), null),
350-
new OsStats.Mem(memory, null, randomLongBetween(0, memory)),
350+
new OsStats.Mem(memory, memory, randomLongBetween(0, memory)),
351351
new OsStats.Swap(randomNonNegativeLong(), randomNonNegativeLong()),
352352
null
353353
);

x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/MachineLearning.java

Lines changed: 1 addition & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,6 @@
5959
import org.elasticsearch.license.XPackLicenseState;
6060
import org.elasticsearch.monitor.jvm.JvmInfo;
6161
import org.elasticsearch.monitor.os.OsProbe;
62-
import org.elasticsearch.monitor.os.OsStats;
6362
import org.elasticsearch.persistent.PersistentTaskParams;
6463
import org.elasticsearch.persistent.PersistentTaskState;
6564
import org.elasticsearch.persistent.PersistentTasksExecutor;
@@ -415,7 +414,6 @@
415414
import org.elasticsearch.xpack.ml.utils.persistence.ResultsPersisterService;
416415

417416
import java.io.IOException;
418-
import java.math.BigInteger;
419417
import java.nio.file.Path;
420418
import java.time.Clock;
421419
import java.util.ArrayList;
@@ -674,7 +672,7 @@ public Settings additionalSettings() {
674672
addMlNodeAttribute(additionalSettings, maxOpenJobsPerNodeNodeAttrName,
675673
String.valueOf(MAX_OPEN_JOBS_PER_NODE.get(settings)));
676674
addMlNodeAttribute(additionalSettings, machineMemoryAttrName,
677-
Long.toString(machineMemoryFromStats(OsProbe.getInstance().osStats())));
675+
Long.toString(OsProbe.getInstance().osStats().getMem().getAdjustedTotal().getBytes()));
678676
addMlNodeAttribute(additionalSettings, jvmSizeAttrName, Long.toString(Runtime.getRuntime().maxMemory()));
679677
// This is not used in v7 and higher, but users are still prevented from setting it directly to avoid confusion
680678
disallowMlNodeAttributes(mlEnabledNodeAttrName);
@@ -1284,38 +1282,6 @@ public static boolean allTemplatesInstalled(ClusterState clusterState) {
12841282
return allPresent;
12851283
}
12861284

1287-
/**
1288-
* Find the memory size (in bytes) of the machine this node is running on.
1289-
* Takes container limits (as used by Docker for example) and forced memory
1290-
* size overrides into account.
1291-
*/
1292-
static long machineMemoryFromStats(OsStats stats) {
1293-
long mem = stats.getMem().getTotal().getBytes();
1294-
OsStats.Cgroup cgroup = stats.getCgroup();
1295-
if (cgroup != null) {
1296-
String containerLimitStr = cgroup.getMemoryLimitInBytes();
1297-
if (containerLimitStr != null && containerLimitStr.equals("max") == false) {
1298-
BigInteger containerLimit = new BigInteger(containerLimitStr);
1299-
if ((containerLimit.compareTo(BigInteger.valueOf(mem)) < 0 && containerLimit.compareTo(BigInteger.ZERO) > 0)
1300-
// mem <= 0 means the value couldn't be obtained for some reason
1301-
|| (mem <= 0 && containerLimit.compareTo(BigInteger.valueOf(Long.MAX_VALUE)) < 0)) {
1302-
mem = containerLimit.longValue();
1303-
}
1304-
}
1305-
}
1306-
ByteSizeValue memOverride = stats.getMem().getTotalOverride();
1307-
if (memOverride != null && memOverride.getBytes() != mem) {
1308-
long diff = memOverride.getBytes() - mem;
1309-
// Only log if the difference is more than 1%
1310-
if (Math.abs((double) diff) / (double) Math.max(memOverride.getBytes(), mem) > 0.01) {
1311-
logger.info("ML memory calculations will be based on total memory override [{}] rather than measured total memory [{}]",
1312-
memOverride, ByteSizeValue.ofBytes(mem));
1313-
}
1314-
mem = memOverride.getBytes();
1315-
}
1316-
return mem;
1317-
}
1318-
13191285
@Override
13201286
public List<NamedXContentRegistry.Entry> getNamedXContent() {
13211287
List<NamedXContentRegistry.Entry> namedXContent = new ArrayList<>();

x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/MachineLearningTests.java

Lines changed: 0 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,11 @@
88

99
import org.elasticsearch.common.settings.Settings;
1010
import org.elasticsearch.license.XPackLicenseState;
11-
import org.elasticsearch.monitor.os.OsStats;
1211
import org.elasticsearch.test.ESTestCase;
1312

1413
import static org.hamcrest.Matchers.containsString;
1514
import static org.hamcrest.Matchers.startsWith;
1615
import static org.mockito.Mockito.mock;
17-
import static org.mockito.Mockito.when;
1816

1917
public class MachineLearningTests extends ESTestCase {
2018

@@ -94,48 +92,6 @@ public void testNoAttributes_givenClash() {
9492
"it is reserved for machine learning. If your intention was to customize machine learning, set the [xpack.ml."));
9593
}
9694

97-
public void testMachineMemory_givenStatsFailure() {
98-
OsStats stats = mock(OsStats.class);
99-
when(stats.getMem()).thenReturn(new OsStats.Mem(0, null, 0));
100-
assertEquals(0L, MachineLearning.machineMemoryFromStats(stats));
101-
}
102-
103-
public void testMachineMemory_givenOverride() {
104-
OsStats stats = mock(OsStats.class);
105-
when(stats.getMem()).thenReturn(new OsStats.Mem(10_737_418_240L, 1_234_567_890L, 5_368_709_120L));
106-
assertEquals(1_234_567_890L, MachineLearning.machineMemoryFromStats(stats));
107-
}
108-
109-
public void testMachineMemory_givenNoCgroup() {
110-
OsStats stats = mock(OsStats.class);
111-
when(stats.getMem()).thenReturn(new OsStats.Mem(10_737_418_240L, null, 5_368_709_120L));
112-
assertEquals(10_737_418_240L, MachineLearning.machineMemoryFromStats(stats));
113-
}
114-
115-
public void testMachineMemory_givenCgroupNullLimit() {
116-
OsStats stats = mock(OsStats.class);
117-
when(stats.getMem()).thenReturn(new OsStats.Mem(10_737_418_240L, null, 5_368_709_120L));
118-
when(stats.getCgroup()).thenReturn(new OsStats.Cgroup("a", 1, "b", 2, 3,
119-
new OsStats.Cgroup.CpuStat(4, 5, 6), null, null, null));
120-
assertEquals(10_737_418_240L, MachineLearning.machineMemoryFromStats(stats));
121-
}
122-
123-
public void testMachineMemory_givenCgroupNoLimit() {
124-
OsStats stats = mock(OsStats.class);
125-
when(stats.getMem()).thenReturn(new OsStats.Mem(10_737_418_240L, null, 5_368_709_120L));
126-
when(stats.getCgroup()).thenReturn(new OsStats.Cgroup("a", 1, "b", 2, 3,
127-
new OsStats.Cgroup.CpuStat(4, 5, 6), "c", "18446744073709551615", "4796416"));
128-
assertEquals(10_737_418_240L, MachineLearning.machineMemoryFromStats(stats));
129-
}
130-
131-
public void testMachineMemory_givenCgroupLowLimit() {
132-
OsStats stats = mock(OsStats.class);
133-
when(stats.getMem()).thenReturn(new OsStats.Mem(10_737_418_240L, null, 5_368_709_120L));
134-
when(stats.getCgroup()).thenReturn(new OsStats.Cgroup("a", 1, "b", 2, 3,
135-
new OsStats.Cgroup.CpuStat(4, 5, 6), "c", "7516192768", "4796416"));
136-
assertEquals(7_516_192_768L, MachineLearning.machineMemoryFromStats(stats));
137-
}
138-
13995
private MachineLearning createMachineLearning(Settings settings) {
14096
XPackLicenseState licenseState = mock(XPackLicenseState.class);
14197

0 commit comments

Comments
 (0)