Skip to content

Commit f98631a

Browse files
authored
Remove legacy versioned logic for DefaultSystemMemoryInfo (#85761)
The machine dependent heap logic uses SystemMemoryInfo to determine how much total memory is available on the system. The default implementation delegates to Java's MX bean to get OS stats. In the past this was only available in Java 14, but since the main branch is now on Java 17, we do not need this logic. This commit refactors the default implementation to no longer need the version check, and to also remove unnecessary forbidden api suppressions for the entire class, instead using a LongSupplier for the memory to isolate the platform specific bean references. relates #85758
1 parent 026fb09 commit f98631a

File tree

6 files changed

+22
-49
lines changed

6 files changed

+22
-49
lines changed

distribution/tools/launchers/src/main/java/org/elasticsearch/tools/launchers/DefaultSystemMemoryInfo.java

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,28 +11,26 @@
1111
import com.sun.management.OperatingSystemMXBean;
1212

1313
import java.lang.management.ManagementFactory;
14+
import java.util.function.LongSupplier;
1415

1516
/**
1617
* A {@link SystemMemoryInfo} which delegates to {@link OperatingSystemMXBean}.
17-
*
18-
* <p>Prior to JDK 14 {@link OperatingSystemMXBean} did not take into consideration container memory limits when reporting total system
19-
* memory. Therefore attempts to use this implementation on earlier JDKs will result in an {@link SystemMemoryInfoException}.
2018
*/
21-
@SuppressForbidden(reason = "Using com.sun internals is the only way to query total system memory")
2219
public final class DefaultSystemMemoryInfo implements SystemMemoryInfo {
23-
private final OperatingSystemMXBean operatingSystemMXBean;
20+
private final LongSupplier totalMemory;
2421

2522
public DefaultSystemMemoryInfo() {
26-
this.operatingSystemMXBean = (OperatingSystemMXBean) ManagementFactory.getOperatingSystemMXBean();
23+
this.totalMemory = getOSBeanMemoryGetter();
2724
}
2825

29-
@Override
30-
@SuppressWarnings("deprecation")
31-
public long availableSystemMemory() throws SystemMemoryInfoException {
32-
if (Runtime.version().feature() < 14) {
33-
throw new SystemMemoryInfoException("The minimum required Java version is 14 to use " + this.getClass().getName());
34-
}
26+
@SuppressForbidden(reason = "Using com.sun internals is the only way to query total system memory")
27+
private static LongSupplier getOSBeanMemoryGetter() {
28+
OperatingSystemMXBean bean = (OperatingSystemMXBean) ManagementFactory.getOperatingSystemMXBean();
29+
return bean::getTotalMemorySize;
30+
}
3531

36-
return operatingSystemMXBean.getTotalPhysicalMemorySize();
32+
@Override
33+
public long availableSystemMemory() {
34+
return totalMemory.getAsLong();
3735
}
3836
}

distribution/tools/launchers/src/main/java/org/elasticsearch/tools/launchers/MachineDependentHeap.java

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -71,14 +71,8 @@ public List<String> determineHeapSettings(Path configDir, List<String> userDefin
7171

7272
List<String> determineHeapSettings(InputStream config) {
7373
MachineNodeRole nodeRole = NodeRoleParser.parse(config);
74-
75-
try {
76-
long availableSystemMemory = systemMemoryInfo.availableSystemMemory();
77-
return options(nodeRole.heap(availableSystemMemory));
78-
} catch (SystemMemoryInfo.SystemMemoryInfoException e) {
79-
// If unable to determine system memory (ex: incompatible jdk version) fallback to defaults
80-
return options(DEFAULT_HEAP_SIZE_MB);
81-
}
74+
long availableSystemMemory = systemMemoryInfo.availableSystemMemory();
75+
return options(nodeRole.heap(availableSystemMemory));
8276
}
8377

8478
private static List<String> options(int heapSize) {

distribution/tools/launchers/src/main/java/org/elasticsearch/tools/launchers/OverridableSystemMemoryInfo.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public OverridableSystemMemoryInfo(final List<String> userDefinedJvmOptions, Sys
2727
}
2828

2929
@Override
30-
public long availableSystemMemory() throws SystemMemoryInfoException {
30+
public long availableSystemMemory() {
3131

3232
return userDefinedJvmOptions.stream()
3333
.filter(option -> option.startsWith("-Des.total_memory_bytes="))

distribution/tools/launchers/src/main/java/org/elasticsearch/tools/launchers/SystemMemoryInfo.java

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,6 @@ public interface SystemMemoryInfo {
1919
/**
2020
*
2121
* @return total system memory available to heap or native process allocation in bytes
22-
* @throws SystemMemoryInfoException if unable to determine available system memory
2322
*/
24-
long availableSystemMemory() throws SystemMemoryInfoException;
25-
26-
class SystemMemoryInfoException extends Exception {
27-
public SystemMemoryInfoException(String message) {
28-
super(message);
29-
}
30-
}
23+
long availableSystemMemory();
3124
}

distribution/tools/launchers/src/test/java/org/elasticsearch/tools/launchers/MachineDependentHeapTests.java

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -77,12 +77,6 @@ public void testDataNodeOptions() {
7777
assertThat(options, containsInAnyOrder("-Xmx128m", "-Xms128m"));
7878
}
7979

80-
public void testFallbackOptions() throws Exception {
81-
MachineDependentHeap machineDependentHeap = new MachineDependentHeap(errorThrowingMemoryInfo());
82-
List<String> options = machineDependentHeap.determineHeapSettings(configPath(), Collections.emptyList());
83-
assertThat(options, containsInAnyOrder("-Xmx1024m", "-Xms1024m"));
84-
}
85-
8680
private static List<String> calculateHeap(double memoryInGigabytes, String... roles) {
8781
MachineDependentHeap machineDependentHeap = new MachineDependentHeap(systemMemoryInGigabytes(memoryInGigabytes));
8882
String configYaml = "node.roles: [" + String.join(",", roles) + "]";
@@ -101,10 +95,6 @@ private static SystemMemoryInfo systemMemoryInGigabytes(double gigabytes) {
10195
return () -> (long) (gigabytes * 1024 * 1024 * 1024);
10296
}
10397

104-
private static SystemMemoryInfo errorThrowingMemoryInfo() {
105-
return () -> { throw new SystemMemoryInfo.SystemMemoryInfoException("something went wrong"); };
106-
}
107-
10898
private static Path configPath() {
10999
URL resource = MachineDependentHeapTests.class.getResource("/config/elasticsearch.yml");
110100
try {

distribution/tools/launchers/src/test/java/org/elasticsearch/tools/launchers/OverridableSystemMemoryInfoTests.java

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@
88

99
package org.elasticsearch.tools.launchers;
1010

11-
import org.elasticsearch.tools.launchers.SystemMemoryInfo.SystemMemoryInfoException;
12-
1311
import java.util.List;
1412

1513
import static org.hamcrest.Matchers.is;
@@ -20,41 +18,41 @@ public class OverridableSystemMemoryInfoTests extends LaunchersTestCase {
2018

2119
private static final long FALLBACK = -1L;
2220

23-
public void testNoOptions() throws SystemMemoryInfoException {
21+
public void testNoOptions() {
2422
final SystemMemoryInfo memoryInfo = new OverridableSystemMemoryInfo(List.of(), fallbackSystemMemoryInfo());
2523
assertThat(memoryInfo.availableSystemMemory(), is(FALLBACK));
2624
}
2725

28-
public void testNoOverrides() throws SystemMemoryInfoException {
26+
public void testNoOverrides() {
2927
final SystemMemoryInfo memoryInfo = new OverridableSystemMemoryInfo(List.of("-Da=b", "-Dx=y"), fallbackSystemMemoryInfo());
3028
assertThat(memoryInfo.availableSystemMemory(), is(FALLBACK));
3129
}
3230

33-
public void testValidSingleOverride() throws SystemMemoryInfoException {
31+
public void testValidSingleOverride() {
3432
final SystemMemoryInfo memoryInfo = new OverridableSystemMemoryInfo(
3533
List.of("-Des.total_memory_bytes=123456789"),
3634
fallbackSystemMemoryInfo()
3735
);
3836
assertThat(memoryInfo.availableSystemMemory(), is(123456789L));
3937
}
4038

41-
public void testValidOverrideInList() throws SystemMemoryInfoException {
39+
public void testValidOverrideInList() {
4240
final SystemMemoryInfo memoryInfo = new OverridableSystemMemoryInfo(
4341
List.of("-Da=b", "-Des.total_memory_bytes=987654321", "-Dx=y"),
4442
fallbackSystemMemoryInfo()
4543
);
4644
assertThat(memoryInfo.availableSystemMemory(), is(987654321L));
4745
}
4846

49-
public void testMultipleValidOverridesInList() throws SystemMemoryInfoException {
47+
public void testMultipleValidOverridesInList() {
5048
final SystemMemoryInfo memoryInfo = new OverridableSystemMemoryInfo(
5149
List.of("-Des.total_memory_bytes=123456789", "-Da=b", "-Des.total_memory_bytes=987654321", "-Dx=y"),
5250
fallbackSystemMemoryInfo()
5351
);
5452
assertThat(memoryInfo.availableSystemMemory(), is(987654321L));
5553
}
5654

57-
public void testNegativeOverride() throws SystemMemoryInfoException {
55+
public void testNegativeOverride() {
5856
final SystemMemoryInfo memoryInfo = new OverridableSystemMemoryInfo(
5957
List.of("-Da=b", "-Des.total_memory_bytes=-123", "-Dx=y"),
6058
fallbackSystemMemoryInfo()
@@ -67,7 +65,7 @@ public void testNegativeOverride() throws SystemMemoryInfoException {
6765
}
6866
}
6967

70-
public void testUnparsableOverride() throws SystemMemoryInfoException {
68+
public void testUnparsableOverride() {
7169
final SystemMemoryInfo memoryInfo = new OverridableSystemMemoryInfo(
7270
List.of("-Da=b", "-Des.total_memory_bytes=invalid", "-Dx=y"),
7371
fallbackSystemMemoryInfo()

0 commit comments

Comments
 (0)