You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

EsJvmOptions.java 6.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2023 SonarSource SA
  4. * mailto:info AT sonarsource DOT com
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 3 of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with this program; if not, write to the Free Software Foundation,
  18. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  19. */
  20. package org.sonar.application.command;
  21. import java.io.File;
  22. import java.io.IOException;
  23. import java.nio.charset.StandardCharsets;
  24. import java.nio.file.Files;
  25. import java.nio.file.Paths;
  26. import java.util.LinkedHashMap;
  27. import java.util.Map;
  28. import java.util.stream.Collectors;
  29. import org.sonar.process.Props;
  30. public class EsJvmOptions extends JvmOptions<EsJvmOptions> {
  31. private static final String ELASTICSEARCH_JVM_OPTIONS_HEADER = """
  32. # This file has been automatically generated by SonarQube during startup.
  33. # Please use sonar.search.javaOpts and/or sonar.search.javaAdditionalOpts in sonar.properties to specify jvm options for Elasticsearch
  34. # DO NOT EDIT THIS FILE
  35. """;
  36. public EsJvmOptions(Props props, File tmpDir) {
  37. super(mandatoryOptions(tmpDir, props));
  38. }
  39. // this basically writes down the content of jvm.options file distributed in vanilla Elasticsearch package
  40. // with some changes to fit running bundled in SQ
  41. private static Map<String, String> mandatoryOptions(File tmpDir, Props props) {
  42. Map<String, String> res = new LinkedHashMap<>(30);
  43. fromJvmDotOptionsFile(tmpDir, res, props);
  44. fromSystemJvmOptionsClass(tmpDir, res);
  45. boolean defaultDisableBootstrapChecks = props.value("sonar.jdbc.url", "").contains("jdbc:h2");
  46. if (!props.valueAsBoolean("sonar.es.bootstrap.checks.disable", defaultDisableBootstrapChecks)) {
  47. res.put("-Des.enforce.bootstrap.checks=", "true");
  48. }
  49. return res;
  50. }
  51. private static void fromJvmDotOptionsFile(File tmpDir, Map<String, String> res, Props props) {
  52. // GC configuration
  53. res.put("-XX:+UseG1GC", "");
  54. // (by default ES 6.6.1 uses variable ${ES_TMPDIR} which is replaced by start scripts. Since we start JAR file
  55. // directly on windows, we specify absolute file as URL (to support space in path) instead
  56. res.put("-Djava.io.tmpdir=", tmpDir.getAbsolutePath());
  57. // heap dumps (enable by default in ES 6.6.1, we don't enable them, no one will analyze them anyway)
  58. // generate a heap dump when an allocation from the Java heap fails
  59. // heap dumps are created in the working directory of the JVM
  60. // res.put("-XX:+HeapDumpOnOutOfMemoryError", "");
  61. // specify an alternative path for heap dumps; ensure the directory exists and
  62. // has sufficient space
  63. // res.put("-XX:HeapDumpPath", "data");
  64. // specify an alternative path for JVM fatal error logs (ES 6.6.1 default is "logs/hs_err_pid%p.log")
  65. var path = Paths.get(props.value("sonar.path.logs", "logs"), "es_hs_err_pid%p.log");
  66. res.put("-XX:ErrorFile=", path.toAbsolutePath().toString());
  67. res.put("-Xlog:disable", "");
  68. }
  69. /**
  70. * JVM options from class "org.elasticsearch.tools.launchers.SystemJvmOptions"
  71. */
  72. private static void fromSystemJvmOptionsClass(File tmpDir, Map<String, String> res) {
  73. /*
  74. * Cache ttl in seconds for positive DNS lookups noting that this overrides the JDK security property networkaddress.cache.ttl;
  75. * can be set to -1 to cache forever.
  76. */
  77. res.put("-Des.networkaddress.cache.ttl=", "60");
  78. /*
  79. * Cache ttl in seconds for negative DNS lookups noting that this overrides the JDK security property
  80. * networkaddress.cache.negative ttl; set to -1 to cache forever.
  81. */
  82. res.put("-Des.networkaddress.cache.negative.ttl=", "10");
  83. // pre-touch JVM emory pages during initialization
  84. res.put("-XX:+AlwaysPreTouch", "");
  85. // explicitly set the stack size
  86. res.put("-Xss1m", "");
  87. // set to headless, just in case,
  88. res.put("-Djava.awt.headless=", "true");
  89. // ensure UTF-8 encoding by default (e.g., filenames)
  90. res.put("-Dfile.encoding=", "UTF-8");
  91. // use our provided JNA always versus the system one
  92. res.put("-Djna.nosys=", "true");
  93. res.put("-Djna.tmpdir=", tmpDir.getAbsolutePath());
  94. /*
  95. * Turn off a JDK optimization that throws away stack traces for common exceptions because stack traces are important for
  96. * debugging.
  97. */
  98. res.put("-XX:-OmitStackTraceInFastThrow", "");
  99. // flags to configure Netty
  100. res.put("-Dio.netty.noUnsafe=", "true");
  101. res.put("-Dio.netty.noKeySetOptimization=", "true");
  102. res.put("-Dio.netty.recycler.maxCapacityPerThread=", "0");
  103. res.put("-Dio.netty.allocator.numDirectArenas=", "0");
  104. // log4j 2
  105. res.put("-Dlog4j.shutdownHookEnabled=", "false");
  106. res.put("-Dlog4j2.disable.jmx=", "true");
  107. res.put("-Dlog4j2.formatMsgNoLookups=", "true");
  108. /*
  109. * Due to internationalization enhancements in JDK 9 org.sonarqube.ws.tester.Elasticsearch need to set the provider to COMPAT otherwise
  110. * time/date
  111. * parsing will break in an incompatible way for some date patterns and locales.
  112. */
  113. res.put("-Djava.locale.providers=", "COMPAT");
  114. // disable FIPS mode for the JVM so SonarQube can use certain algorithms
  115. res.put("-Dcom.redhat.fips=", "false");
  116. }
  117. public void writeToJvmOptionFile(File file) {
  118. String jvmOptions = getAll().stream().collect(Collectors.joining("\n"));
  119. String jvmOptionsContent = ELASTICSEARCH_JVM_OPTIONS_HEADER + jvmOptions;
  120. try {
  121. Files.write(file.toPath(), jvmOptionsContent.getBytes(StandardCharsets.UTF_8));
  122. } catch (IOException e) {
  123. throw new IllegalStateException("Cannot write Elasticsearch jvm options file", e);
  124. }
  125. }
  126. }