Skip to content

Commit 6da9735

Browse files
Don't drop user's MaxDirectMemorySize flag on jdk8/windows (#48657)
* Always pass user-specified MaxDirectMemorySize We had been testing whether a user had passed a value for MaxDirectMemorySize by parsing the output of "java -XX:PrintFlagsFinal -version". If MaxDirectMemorySize equals zero, we set it to half of max heap. The problem is that on Windows with JDK 8, a JDK bug incorrectly truncates values over 4g and returns multiples of 4g as zero. In order to always respect the user-defined settings, we need to check our input to see if an "-XX:MaxDirectMemorySize" value has been passed. * Always warn for Windows/jdk8 ergo issue Even if a user has set MaxDirectMemorySize, they aren't future-proof for this JDK bug. With this change, we issue a general warning for the windows/JDK8 issue, and a specific warning if MaxDirectMemorySize is unset.
1 parent 34c2375 commit 6da9735

File tree

2 files changed

+16
-5
lines changed

2 files changed

+16
-5
lines changed

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

+12-4
Original file line numberDiff line numberDiff line change
@@ -59,12 +59,20 @@ static List<String> choose(final List<String> userDefinedJvmOptions) throws Inte
5959
final Map<String, Optional<String>> finalJvmOptions = finalJvmOptions(userDefinedJvmOptions);
6060
final long heapSize = extractHeapSize(finalJvmOptions);
6161
final long maxDirectMemorySize = extractMaxDirectMemorySize(finalJvmOptions);
62-
if (maxDirectMemorySize == 0) {
62+
63+
if (System.getProperty("os.name").startsWith("Windows") && JavaVersion.majorVersion(JavaVersion.CURRENT) == 8) {
64+
Launchers.errPrintln("Warning: with JDK 8 on Windows, Elasticsearch may be unable to derive correct");
65+
Launchers.errPrintln(" ergonomic settings due to a JDK issue (JDK-8074459). Please use a newer");
66+
Launchers.errPrintln(" version of Java.");
67+
}
68+
69+
if (maxDirectMemorySize == 0 && userDefinedJvmOptions.stream().noneMatch(s -> s.startsWith("-XX:MaxDirectMemorySize"))) {
70+
6371
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");
72+
Launchers.errPrintln("Warning: MaxDirectMemorySize may have been miscalculated due to JDK-8074459.");
73+
Launchers.errPrintln(" Please use a newer version of Java or set MaxDirectMemorySize explicitly.");
6774
}
75+
6876
ergonomicChoices.add("-XX:MaxDirectMemorySize=" + heapSize / 2);
6977
}
7078
return ergonomicChoices;

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

+4-1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import java.util.Arrays;
2626
import java.util.Collections;
2727
import java.util.HashMap;
28+
import java.util.List;
2829
import java.util.Map;
2930

3031
import static org.hamcrest.Matchers.anyOf;
@@ -142,8 +143,10 @@ public void testMaxDirectMemorySizeChoice() throws InterruptedException, IOExcep
142143
}
143144

144145
public void testMaxDirectMemorySizeChoiceWhenSet() throws InterruptedException, IOException {
146+
List<String> derivedSettingList = JvmErgonomics.choose(Arrays.asList("-Xms5g", "-Xmx5g", "-XX:MaxDirectMemorySize=4g"));
145147
assertThat(
146-
JvmErgonomics.choose(Arrays.asList("-Xms1g", "-Xmx1g", "-XX:MaxDirectMemorySize=1g")),
148+
derivedSettingList,
149+
// if MaxDirectMemorySize is set, we shouldn't derive our own value for it
147150
everyItem(not(startsWith("-XX:MaxDirectMemorySize="))));
148151
}
149152

0 commit comments

Comments
 (0)