Skip to content

Commit 1dd7ab8

Browse files
Warn when MaxDirectMemorySize may be incorrect (Windows/JDK8 only issue) (#48365)
Our JVM ergonomics extract max heap size from JDK PrintFlagsFinal output. On JDK 8, there is a system-dependent bug where memory sizes are cast to 32-bit integers. On affected systems (namely, Windows), when 1/4 of physical memory is more than the maximum integer value, the output of PrintFlagsFinal will be inaccurate. In the pathological case, where the max heap size would be a multiple of 4g, the test will fail. The practical effect of this bug, beyond test failures, is that we may set MaxDirectMemorySize to an incorrect value on Windows. This commit adds a warning about this situation during startup.
1 parent d27a307 commit 1dd7ab8

File tree

2 files changed

+11
-3
lines changed

2 files changed

+11
-3
lines changed

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

+7
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919

2020
package org.elasticsearch.tools.launchers;
2121

22+
import org.elasticsearch.tools.java_version_checker.JavaVersion;
23+
2224
import java.io.BufferedReader;
2325
import java.io.IOException;
2426
import java.io.InputStream;
@@ -58,6 +60,11 @@ static List<String> choose(final List<String> userDefinedJvmOptions) throws Inte
5860
final long heapSize = extractHeapSize(finalJvmOptions);
5961
final long maxDirectMemorySize = extractMaxDirectMemorySize(finalJvmOptions);
6062
if (maxDirectMemorySize == 0) {
63+
if (System.getProperty("os.name").startsWith("Windows") && JavaVersion.majorVersion(JavaVersion.CURRENT) == 8) {
64+
Launchers.errPrintln("Warning: with JDK 8 on Windows, Elasticsearch may miscalculate MaxDirectMemorySize");
65+
Launchers.errPrintln(" due to a JDK issue (JDK-8074459).");
66+
Launchers.errPrintln(" Please use a newer version of Java or set MaxDirectMemorySize explicitly");
67+
}
6168
ergonomicChoices.add("-XX:MaxDirectMemorySize=" + heapSize / 2);
6269
}
6370
return ergonomicChoices;

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

+4-3
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,8 @@ public void testExtractValidHeapSizeUsingMaxHeapSize() throws InterruptedExcepti
5656
}
5757

5858
public void testExtractValidHeapSizeNoOptionPresent() throws InterruptedException, IOException {
59-
// Muting on Windows, awaitsfix: https://github.com/elastic/elasticsearch/issues/47384
60-
assumeFalse(System.getProperty("os.name").startsWith("Windows"));
59+
// Muted for jdk8/Windows, see: https://github.com/elastic/elasticsearch/issues/47384
60+
assumeFalse(System.getProperty("os.name").startsWith("Windows") && JavaVersion.majorVersion(JavaVersion.CURRENT) == 8);
6161
assertThat(
6262
JvmErgonomics.extractHeapSize(JvmErgonomics.finalJvmOptions(Collections.emptyList())),
6363
greaterThan(0L));
@@ -123,8 +123,9 @@ public void testExtractNoSystemProperties() {
123123
Map<String, String> parsedSystemProperties = JvmErgonomics.extractSystemProperties(Arrays.asList("-Xms1024M", "-Xmx1024M"));
124124
assertTrue(parsedSystemProperties.isEmpty());
125125
}
126-
126+
127127
public void testMaxDirectMemorySizeChoice() throws InterruptedException, IOException {
128+
// Muted for jdk8/Windows, see: https://github.com/elastic/elasticsearch/issues/47384
128129
assumeFalse(System.getProperty("os.name").startsWith("Windows") && JavaVersion.majorVersion(JavaVersion.CURRENT) == 8);
129130
final Map<String, String> heapMaxDirectMemorySize = new HashMap<>();
130131
heapMaxDirectMemorySize.put("64M", Long.toString((64L << 20) / 2));

0 commit comments

Comments
 (0)