From 191a19739e294985caf5c6dbce7766443533d86c Mon Sep 17 00:00:00 2001 From: =?utf8?q?S=C3=A9bastien=20Lesaint?= Date: Thu, 9 Jan 2020 09:57:58 +0100 Subject: [PATCH] SONAR-12686 update JVM options to match defaults in ES 7.X Elastic introduced "system" JVM settings in 7.X: * "system" settings are set from Java code (class SystemJvmOptions) * which means these JVM settings are not defined in "jvm.options" file anymore --- .../application/command/EsJvmOptions.java | 96 +++++++++++-------- .../application/command/EsJvmOptionsTest.java | 14 ++- 2 files changed, 63 insertions(+), 47 deletions(-) diff --git a/server/sonar-main/src/main/java/org/sonar/application/command/EsJvmOptions.java b/server/sonar-main/src/main/java/org/sonar/application/command/EsJvmOptions.java index 85a7cc05da2..4447f1b7293 100644 --- a/server/sonar-main/src/main/java/org/sonar/application/command/EsJvmOptions.java +++ b/server/sonar-main/src/main/java/org/sonar/application/command/EsJvmOptions.java @@ -43,48 +43,22 @@ public class EsJvmOptions extends JvmOptions { // with some changes to fit running bundled in SQ private static Map mandatoryOptions(File tmpDir, Props props) { Map res = new LinkedHashMap<>(30); + fromJvmDotOptionsFile(tmpDir, res); + fromSystemJvmOptionsClass(res); + + if (!props.value("sonar.jdbc.url", "").contains("jdbc:h2") && !props.valueAsBoolean("sonar.es.bootstrap.checks.disable")) { + res.put("-Des.enforce.bootstrap.checks=", "true"); + } + + return res; + } + + private static void fromJvmDotOptionsFile(File tmpDir, Map res) { // GC configuration res.put("-XX:+UseConcMarkSweepGC", ""); res.put("-XX:CMSInitiatingOccupancyFraction=", "75"); res.put("-XX:+UseCMSInitiatingOccupancyOnly", ""); - // DNS cache policy - // cache ttl in seconds for positive DNS lookups noting that this overrides the - // JDK security property networkaddress.cache.ttl; set to -1 to cache forever - res.put("-Des.networkaddress.cache.ttl=", "60"); - // cache ttl in seconds for negative DNS lookups noting that this overrides the - // JDK security property networkaddress.cache.negative ttl; set to -1 to cache - // forever - res.put("-Des.networkaddress.cache.negative.ttl=", "10"); - - // optimizations - - // pre-touch memory pages used by the JVM during initialization - res.put("-XX:+AlwaysPreTouch", ""); - - // basic - // explicitly set the stack size - res.put("-Xss1m", ""); - // set to headless, just in case - res.put("-Djava.awt.headless=", "true"); - // ensure UTF-8 encoding by default (e.g. filenames) - res.put("-Dfile.encoding=", "UTF-8"); - // use our provided JNA always versus the system one - res.put("-Djna.nosys=", "true"); - - // turn off a JDK optimization that throws away stack traces for common - // exceptions because stack traces are important for debugging - res.put("-XX:-OmitStackTraceInFastThrow", ""); - - // flags to configure Netty - res.put("-Dio.netty.noUnsafe=", "true"); - res.put("-Dio.netty.noKeySetOptimization=", "true"); - res.put("-Dio.netty.recycler.maxCapacityPerThread=", "0"); - - // log4j 2 - res.put("-Dlog4j.shutdownHookEnabled=", "false"); - res.put("-Dlog4j2.disable.jmx=", "true"); - // (by default ES 6.6.1 uses variable ${ES_TMPDIR} which is replaced by start scripts. Since we start JAR file // directly on windows, we specify absolute file as URL (to support space in path) instead res.put("-Djava.io.tmpdir=", tmpDir.getAbsolutePath()); @@ -110,12 +84,50 @@ public class EsJvmOptions extends JvmOptions { // res.put("8:-XX:GCLogFileSize", "64m"); // JDK 9+ GC logging // res.put("9-:-Xlog:gc*,gc+age=trace,safepoint:file=logs/gc.log:utctime,pid,tags:filecount=32,filesize=64m", ""); + } - if (!props.value("sonar.jdbc.url", "").contains("jdbc:h2") && !props.valueAsBoolean("sonar.es.bootstrap.checks.disable")) { - res.put("-Des.enforce.bootstrap.checks=", "true"); - } - - return res; + /** + * JVM options from class "org.elasticsearch.tools.launchers.SystemJvmOptions" + */ + private static void fromSystemJvmOptionsClass(Map res) { + /* + * Cache ttl in seconds for positive DNS lookups noting that this overrides the JDK security property networkaddress.cache.ttl; + * can be set to -1 to cache forever. + */ + res.put("-Des.networkaddress.cache.ttl=", "60"); + /* + * Cache ttl in seconds for negative DNS lookups noting that this overrides the JDK security property + * networkaddress.cache.negative ttl; set to -1 to cache forever. + */ + res.put("-Des.networkaddress.cache.negative.ttl=", "10"); + // pre-touch JVM emory pages during initialization + res.put("-XX:+AlwaysPreTouch", ""); + // explicitly set the stack size + res.put("-Xss1m", ""); + // set to headless, just in case, + res.put("-Djava.awt.headless=", "true"); + // ensure UTF-8 encoding by default (e.g., filenames) + res.put("-Dfile.encoding=", "UTF-8"); + // use our provided JNA always versus the system one + res.put("-Djna.nosys=", "true"); + /* + * Turn off a JDK optimization that throws away stack traces for common exceptions because stack traces are important for + * debugging. + */ + res.put("-XX:-OmitStackTraceInFastThrow", ""); + // flags to configure Netty + res.put("-Dio.netty.noUnsafe=", "true"); + res.put("-Dio.netty.noKeySetOptimization=", "true"); + res.put("-Dio.netty.recycler.maxCapacityPerThread=", "0"); + res.put("-Dio.netty.allocator.numDirectArenas=", "0"); + // log4j 2 + res.put("-Dlog4j.shutdownHookEnabled=", "false"); + res.put("-Dlog4j2.disable.jmx=", "true"); + /* + * Due to internationalization enhancements in JDK 9 Elasticsearch need to set the provider to COMPAT otherwise time/date + * parsing will break in an incompatible way for some date patterns and locales. + */ + res.put("-Djava.locale.providers=", "COMPAT"); } public void writeToJvmOptionFile(File file) { diff --git a/server/sonar-main/src/test/java/org/sonar/application/command/EsJvmOptionsTest.java b/server/sonar-main/src/test/java/org/sonar/application/command/EsJvmOptionsTest.java index 751f188d0cd..2d51a0f5403 100644 --- a/server/sonar-main/src/test/java/org/sonar/application/command/EsJvmOptionsTest.java +++ b/server/sonar-main/src/test/java/org/sonar/application/command/EsJvmOptionsTest.java @@ -44,7 +44,7 @@ public class EsJvmOptionsTest { private Properties properties = new Properties(); @Test - public void constructor_sets_mandatory_JVM_options_on_Java_11() throws IOException { + public void constructor_sets_mandatory_JVM_options() throws IOException { File tmpDir = temporaryFolder.newFolder(); EsJvmOptions underTest = new EsJvmOptions(new Props(properties), tmpDir); @@ -53,6 +53,8 @@ public class EsJvmOptionsTest { "-XX:+UseConcMarkSweepGC", "-XX:CMSInitiatingOccupancyFraction=75", "-XX:+UseCMSInitiatingOccupancyOnly", + "-Djava.io.tmpdir=" + tmpDir.getAbsolutePath(), + "-XX:ErrorFile=../logs/es_hs_err_pid%p.log", "-Des.networkaddress.cache.ttl=60", "-Des.networkaddress.cache.negative.ttl=10", "-XX:+AlwaysPreTouch", @@ -64,10 +66,10 @@ public class EsJvmOptionsTest { "-Dio.netty.noUnsafe=true", "-Dio.netty.noKeySetOptimization=true", "-Dio.netty.recycler.maxCapacityPerThread=0", + "-Dio.netty.allocator.numDirectArenas=0", "-Dlog4j.shutdownHookEnabled=false", "-Dlog4j2.disable.jmx=true", - "-Djava.io.tmpdir=" + tmpDir.getAbsolutePath(), - "-XX:ErrorFile=../logs/es_hs_err_pid%p.log", + "-Djava.locale.providers=COMPAT", "-Des.enforce.bootstrap.checks=true"); } @@ -132,6 +134,8 @@ public class EsJvmOptionsTest { "-XX:+UseConcMarkSweepGC\n" + "-XX:CMSInitiatingOccupancyFraction=75\n" + "-XX:+UseCMSInitiatingOccupancyOnly\n" + + "-Djava.io.tmpdir=" + tmpDir.getAbsolutePath() + "\n" + + "-XX:ErrorFile=../logs/es_hs_err_pid%p.log\n" + "-Des.networkaddress.cache.ttl=60\n" + "-Des.networkaddress.cache.negative.ttl=10\n" + "-XX:+AlwaysPreTouch\n" + @@ -143,10 +147,10 @@ public class EsJvmOptionsTest { "-Dio.netty.noUnsafe=true\n" + "-Dio.netty.noKeySetOptimization=true\n" + "-Dio.netty.recycler.maxCapacityPerThread=0\n" + + "-Dio.netty.allocator.numDirectArenas=0\n" + "-Dlog4j.shutdownHookEnabled=false\n" + "-Dlog4j2.disable.jmx=true\n" + - "-Djava.io.tmpdir=" + tmpDir.getAbsolutePath() + "\n" + - "-XX:ErrorFile=../logs/es_hs_err_pid%p.log\n" + + "-Djava.locale.providers=COMPAT\n" + "-Des.enforce.bootstrap.checks=true\n" + "-foo\n" + "-bar"); -- 2.39.5