diff --git a/distribution/tools/launchers/src/main/java/org/elasticsearch/tools/launchers/JvmErgonomics.java b/distribution/tools/launchers/src/main/java/org/elasticsearch/tools/launchers/JvmErgonomics.java index b94859fa250..54c432d28c7 100644 --- a/distribution/tools/launchers/src/main/java/org/elasticsearch/tools/launchers/JvmErgonomics.java +++ b/distribution/tools/launchers/src/main/java/org/elasticsearch/tools/launchers/JvmErgonomics.java @@ -59,12 +59,20 @@ final class JvmErgonomics { final Map> finalJvmOptions = finalJvmOptions(userDefinedJvmOptions); final long heapSize = extractHeapSize(finalJvmOptions); final long maxDirectMemorySize = extractMaxDirectMemorySize(finalJvmOptions); - if (maxDirectMemorySize == 0) { + + if (System.getProperty("os.name").startsWith("Windows") && JavaVersion.majorVersion(JavaVersion.CURRENT) == 8) { + Launchers.errPrintln("Warning: with JDK 8 on Windows, Elasticsearch may be unable to derive correct"); + Launchers.errPrintln(" ergonomic settings due to a JDK issue (JDK-8074459). Please use a newer"); + Launchers.errPrintln(" version of Java."); + } + + if (maxDirectMemorySize == 0 && userDefinedJvmOptions.stream().noneMatch(s -> s.startsWith("-XX:MaxDirectMemorySize"))) { + if (System.getProperty("os.name").startsWith("Windows") && JavaVersion.majorVersion(JavaVersion.CURRENT) == 8) { - Launchers.errPrintln("Warning: with JDK 8 on Windows, Elasticsearch may miscalculate MaxDirectMemorySize"); - Launchers.errPrintln(" due to a JDK issue (JDK-8074459)."); - Launchers.errPrintln(" Please use a newer version of Java or set MaxDirectMemorySize explicitly"); + Launchers.errPrintln("Warning: MaxDirectMemorySize may have been miscalculated due to JDK-8074459."); + Launchers.errPrintln(" Please use a newer version of Java or set MaxDirectMemorySize explicitly."); } + ergonomicChoices.add("-XX:MaxDirectMemorySize=" + heapSize / 2); } return ergonomicChoices; diff --git a/distribution/tools/launchers/src/test/java/org/elasticsearch/tools/launchers/JvmErgonomicsTests.java b/distribution/tools/launchers/src/test/java/org/elasticsearch/tools/launchers/JvmErgonomicsTests.java index 5fdd9923b80..69cf2b437c4 100644 --- a/distribution/tools/launchers/src/test/java/org/elasticsearch/tools/launchers/JvmErgonomicsTests.java +++ b/distribution/tools/launchers/src/test/java/org/elasticsearch/tools/launchers/JvmErgonomicsTests.java @@ -25,6 +25,7 @@ import java.io.IOException; import java.util.Arrays; import java.util.Collections; import java.util.HashMap; +import java.util.List; import java.util.Map; import static org.hamcrest.Matchers.anyOf; @@ -142,8 +143,10 @@ public class JvmErgonomicsTests extends LaunchersTestCase { } public void testMaxDirectMemorySizeChoiceWhenSet() throws InterruptedException, IOException { + List derivedSettingList = JvmErgonomics.choose(Arrays.asList("-Xms5g", "-Xmx5g", "-XX:MaxDirectMemorySize=4g")); assertThat( - JvmErgonomics.choose(Arrays.asList("-Xms1g", "-Xmx1g", "-XX:MaxDirectMemorySize=1g")), + derivedSettingList, + // if MaxDirectMemorySize is set, we shouldn't derive our own value for it everyItem(not(startsWith("-XX:MaxDirectMemorySize=")))); }