private JavaCommand createEsCommandForWindows() { EsInstallation esInstallation = createEsInstallation(); return new JavaCommand<EsJvmOptions>(ProcessId.ELASTICSEARCH, esInstallation.getHomeDirectory()) .setEsInstallation(esInstallation) .setReadsArgumentsFromFile(false) .setArgument("path.conf", esInstallation.getConfDirectory().getAbsolutePath()) .setJvmOptions(new EsJvmOptions() .addFromMandatoryProperty(props, SEARCH_JAVA_OPTS.getKey()) .addFromMandatoryProperty(props, SEARCH_JAVA_ADDITIONAL_OPTS.getKey()) .add("-Delasticsearch") .add("-Des.path.home=" + esInstallation.getHomeDirectory())) .setEnvVariable("ES_JVM_OPTIONS", esInstallation.getJvmOptions().getAbsolutePath()) .setEnvVariable("JAVA_HOME", System.getProperties().getProperty("java.home")) .setClassName("org.elasticsearch.bootstrap.Elasticsearch") .addClasspath("lib/*") .suppressEnvVariable(ENV_VAR_JAVA_TOOL_OPTIONS) .suppressEnvVariable(ENV_VAR_ES_JAVA_OPTS); }
@Test public void constructor_sets_mandatory_JVM_options() { EsJvmOptions underTest = new EsJvmOptions(); assertThat(underTest.getAll()).containsExactly( "-XX:+UseConcMarkSweepGC", "-XX:CMSInitiatingOccupancyFraction=75", "-XX:+UseCMSInitiatingOccupancyOnly", "-XX:+AlwaysPreTouch", "-server", "-Xss1m", "-Djava.awt.headless=true", "-Dfile.encoding=UTF-8", "-Djna.nosys=true", "-Djdk.io.permissionsUseCanonicalPath=true", "-Dio.netty.noUnsafe=true", "-Dio.netty.noKeySetOptimization=true", "-Dio.netty.recycler.maxCapacityPerThread=0", "-Dlog4j.shutdownHookEnabled=false", "-Dlog4j2.disable.jmx=true", "-Dlog4j.skipJansi=true"); }
private static void writeConfFiles(EsInstallation esInstallation) { File confDir = esInstallation.getConfDirectory(); if (!confDir.exists() && !confDir.mkdirs()) { String error = format("Failed to create temporary configuration directory [%s]", confDir.getAbsolutePath()); LOG.error(error); throw new IllegalStateException(error); } try { esInstallation.getEsYmlSettings().writeToYmlSettingsFile(esInstallation.getElasticsearchYml()); esInstallation.getEsJvmOptions().writeToJvmOptionFile(esInstallation.getJvmOptions()); esInstallation.getLog4j2Properties().store(new FileOutputStream(esInstallation.getLog4j2PropertiesLocation()), "log4j2 properties file for ES bundled in SonarQube"); } catch (IOException e) { throw new IllegalStateException("Failed to write ES configuration files", e); } }
private EsInstallation createEsInstallation() { EsInstallation esInstallation = new EsInstallation(props); if (!esInstallation.getExecutable().exists()) { throw new IllegalStateException("Cannot find elasticsearch binary"); } Map<String, String> settingsMap = new EsSettings(props, esInstallation, System2.INSTANCE).build(); esInstallation .setLog4j2Properties(new EsLogging().createProperties(props, esInstallation.getLogDirectory())) .setEsJvmOptions(new EsJvmOptions() .addFromMandatoryProperty(props, SEARCH_JAVA_OPTS.getKey()) .addFromMandatoryProperty(props, SEARCH_JAVA_ADDITIONAL_OPTS.getKey())) .setEsYmlSettings(new EsYmlSettings(settingsMap)) .setClusterName(settingsMap.get("cluster.name")) .setHost(settingsMap.get("network.host")) .setPort(Integer.valueOf(settingsMap.get("transport.tcp.port"))); return esInstallation; }
@Test public void writeToJvmOptionFile_writes_all_JVM_options_to_file_with_warning_header() throws IOException { File file = temporaryFolder.newFile(); EsJvmOptions underTest = new EsJvmOptions() .add("-foo") .add("-bar"); underTest.writeToJvmOptionFile(file);
@Test public void writeToJvmOptionFile_throws_ISE_in_case_of_IOException() throws IOException { File notAFile = temporaryFolder.newFolder(); EsJvmOptions underTest = new EsJvmOptions(); expectedException.expect(IllegalStateException.class); expectedException.expectMessage("Cannot write Elasticsearch jvm options file"); expectedException.expectCause(ExceptionCauseMatcher.hasType(IOException.class)); underTest.writeToJvmOptionFile(notAFile); } }
public void writeToJvmOptionFile(File file) { String jvmOptions = getAll().stream().collect(Collectors.joining("\n")); String jvmOptionsContent = ELASTICSEARCH_JVM_OPTIONS_HEADER + jvmOptions; try { Files.write(file.toPath(), jvmOptionsContent.getBytes(StandardCharsets.UTF_8)); } catch (IOException e) { throw new IllegalStateException("Cannot write Elasticsearch jvm options file", e); } } }
private EsInstallation createEsInstallation() throws IOException { return new EsInstallation(new Props(new Properties()) .set("sonar.path.home", temp.newFolder("home").getAbsolutePath()) .set("sonar.path.data", temp.newFolder("data").getAbsolutePath()) .set("sonar.path.temp", temp.newFolder("temp").getAbsolutePath()) .set("sonar.path.logs", temp.newFolder("logs").getAbsolutePath())) .setClusterName("cluster") .setPort(9001) .setHost("localhost") .setEsYmlSettings(new EsYmlSettings(new HashMap<>())) .setEsJvmOptions(new EsJvmOptions()) .setLog4j2Properties(new Properties()); }
public EsJvmOptions() { super(mandatoryOptions()); }
private EsInstallation createEsInstallation() { EsInstallation esInstallation = new EsInstallation(props); if (!esInstallation.getExecutable().exists()) { throw new IllegalStateException("Cannot find elasticsearch binary"); } Map<String, String> settingsMap = new EsSettings(props, esInstallation, System2.INSTANCE).build(); esInstallation .setLog4j2Properties(new EsLogging().createProperties(props, esInstallation.getLogDirectory())) .setEsJvmOptions(new EsJvmOptions() .addFromMandatoryProperty(props, ProcessProperties.SEARCH_JAVA_OPTS) .addFromMandatoryProperty(props, ProcessProperties.SEARCH_JAVA_ADDITIONAL_OPTS)) .setEsYmlSettings(new EsYmlSettings(settingsMap)) .setClusterName(settingsMap.get("cluster.name")) .setHost(settingsMap.get("network.host")) .setPort(Integer.valueOf(settingsMap.get("transport.tcp.port"))); return esInstallation; }
@Test public void createEsCommand_returns_command_for_overridden_settings() throws Exception { prepareEsFileSystem(); Properties props = new Properties(); props.setProperty("sonar.search.host", "localhost"); props.setProperty("sonar.cluster.name", "foo"); props.setProperty("sonar.search.port", "1234"); props.setProperty("sonar.search.javaOpts", "-Xms10G -Xmx10G"); AbstractCommand esCommand = newFactory(props).createEsCommand(); EsInstallation esConfig = esCommand.getEsInstallation(); assertThat(esConfig.getClusterName()).isEqualTo("foo"); assertThat(esConfig.getPort()).isEqualTo(1234); assertThat(esConfig.getEsJvmOptions().getAll()) // enforced values .contains("-XX:+UseConcMarkSweepGC", "-server", "-Dfile.encoding=UTF-8") // user settings .contains("-Xms10G", "-Xmx10G") // default values disabled .doesNotContain("-XX:+HeapDumpOnOutOfMemoryError"); }
public EsJvmOptions() { super(mandatoryOptions()); }
private JavaCommand createEsCommandForWindows() { EsInstallation esInstallation = createEsInstallation(); return new JavaCommand<EsJvmOptions>(ProcessId.ELASTICSEARCH, esInstallation.getHomeDirectory()) .setEsInstallation(esInstallation) .setReadsArgumentsFromFile(false) .setArgument("path.conf", esInstallation.getConfDirectory().getAbsolutePath()) .setJvmOptions(new EsJvmOptions() .addFromMandatoryProperty(props, ProcessProperties.SEARCH_JAVA_OPTS) .addFromMandatoryProperty(props, ProcessProperties.SEARCH_JAVA_ADDITIONAL_OPTS) .add("-Delasticsearch") .add("-Des.path.home=" + esInstallation.getHomeDirectory()) ) .setEnvVariable("ES_JVM_OPTIONS", esInstallation.getJvmOptions().getAbsolutePath()) .setEnvVariable("JAVA_HOME", System.getProperties().getProperty("java.home")) .setClassName("org.elasticsearch.bootstrap.Elasticsearch") .addClasspath("lib/*") .suppressEnvVariable(ENV_VAR_JAVA_TOOL_OPTIONS); }
@Test public void createEsCommand_for_unix_returns_command_for_default_settings() throws Exception { System2 system2 = Mockito.mock(System2.class); when(system2.isOsWindows()).thenReturn(false); prepareEsFileSystem(); Properties props = new Properties(); props.setProperty("sonar.search.host", "localhost"); AbstractCommand esCommand = newFactory(props, system2).createEsCommand(); EsInstallation esConfig = esCommand.getEsInstallation(); assertThat(esCommand).isInstanceOf(EsScriptCommand.class); assertThat(esConfig.getClusterName()).isEqualTo("sonarqube"); assertThat(esConfig.getHost()).isNotEmpty(); assertThat(esConfig.getPort()).isEqualTo(9001); assertThat(esConfig.getEsJvmOptions().getAll()) // enforced values .contains("-XX:+UseConcMarkSweepGC", "-server", "-Dfile.encoding=UTF-8") // default settings .contains("-Xms512m", "-Xmx512m", "-XX:+HeapDumpOnOutOfMemoryError"); File esConfDir = new File(tempDir, "conf/es"); assertThat(esCommand.getEnvVariables()) .contains(entry("ES_JVM_OPTIONS", new File(esConfDir, "jvm.options").getAbsolutePath())) .containsKey("JAVA_HOME"); assertThat(esConfig.getEsYmlSettings()).isNotNull(); assertThat(esConfig.getLog4j2Properties()) .contains(entry("appender.file_es.fileName", new File(logsDir, "es.log").getAbsolutePath())); assertThat(esCommand.getSuppressedEnvVariables()).containsOnly("JAVA_TOOL_OPTIONS", "ES_JAVA_OPTS"); }
private static void writeConfFiles(EsInstallation esInstallation) { File confDir = esInstallation.getConfDirectory(); if (!confDir.exists() && !confDir.mkdirs()) { String error = format("Failed to create temporary configuration directory [%s]", confDir.getAbsolutePath()); LOG.error(error); throw new IllegalStateException(error); } try { esInstallation.getEsYmlSettings().writeToYmlSettingsFile(esInstallation.getElasticsearchYml()); esInstallation.getEsJvmOptions().writeToJvmOptionFile(esInstallation.getJvmOptions()); esInstallation.getLog4j2Properties().store(new FileOutputStream(esInstallation.getLog4j2PropertiesLocation()), "log4j2 properties file for ES bundled in SonarQube"); } catch (IOException e) { throw new IllegalStateException("Failed to write ES configuration files", e); } }
@Test public void createEsCommand_for_windows_returns_command_for_default_settings() throws Exception { System2 system2 = Mockito.mock(System2.class); when(system2.isOsWindows()).thenReturn(true); prepareEsFileSystem(); Properties props = new Properties(); props.setProperty("sonar.search.host", "localhost"); AbstractCommand esCommand = newFactory(props, system2).createEsCommand(); EsInstallation esConfig = esCommand.getEsInstallation(); assertThat(esCommand).isInstanceOf(JavaCommand.class); assertThat(esConfig.getClusterName()).isEqualTo("sonarqube"); assertThat(esConfig.getHost()).isNotEmpty(); assertThat(esConfig.getPort()).isEqualTo(9001); assertThat(esConfig.getEsJvmOptions().getAll()) // enforced values .contains("-XX:+UseConcMarkSweepGC", "-server", "-Dfile.encoding=UTF-8") // default settings .contains("-Xms512m", "-Xmx512m", "-XX:+HeapDumpOnOutOfMemoryError"); File esConfDir = new File(tempDir, "conf/es"); assertThat(esCommand.getEnvVariables()) .contains(entry("ES_JVM_OPTIONS", new File(esConfDir, "jvm.options").getAbsolutePath())) .containsKey("JAVA_HOME"); assertThat(esConfig.getEsYmlSettings()).isNotNull(); assertThat(esConfig.getLog4j2Properties()) .contains(entry("appender.file_es.fileName", new File(logsDir, "es.log").getAbsolutePath())); assertThat(esCommand.getSuppressedEnvVariables()).containsOnly("JAVA_TOOL_OPTIONS", "ES_JAVA_OPTS"); }
public void writeToJvmOptionFile(File file) { String jvmOptions = getAll().stream().collect(Collectors.joining("\n")); String jvmOptionsContent = ELASTICSEARCH_JVM_OPTIONS_HEADER + jvmOptions; try { Files.write(file.toPath(), jvmOptionsContent.getBytes(Charset.forName("UTF-8"))); } catch (IOException e) { throw new IllegalStateException("Cannot write Elasticsearch jvm options file", e); } } }