Skip to content

Commit

Permalink
SONAR-11792 define ES's JVM tmp directory to SQ's tmp directory
Browse files Browse the repository at this point in the history
  • Loading branch information
sns-seb authored and SonarTech committed Mar 19, 2019
1 parent 4112b9f commit 9fa0a0f
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ private JavaCommand createEsCommandForWindows() {
return new JavaCommand<EsJvmOptions>(ProcessId.ELASTICSEARCH, esInstallation.getHomeDirectory())
.setEsInstallation(esInstallation)
.setReadsArgumentsFromFile(false)
.setJvmOptions(new EsJvmOptions()
.setJvmOptions(new EsJvmOptions(tempDir)
.addFromMandatoryProperty(props, SEARCH_JAVA_OPTS.getKey())
.addFromMandatoryProperty(props, SEARCH_JAVA_ADDITIONAL_OPTS.getKey())
.add("-Delasticsearch")
Expand All @@ -135,7 +135,7 @@ private EsInstallation createEsInstallation() {

esInstallation
.setLog4j2Properties(new EsLogging().createProperties(props, esInstallation.getLogDirectory()))
.setEsJvmOptions(new EsJvmOptions()
.setEsJvmOptions(new EsJvmOptions(tempDir)
.addFromMandatoryProperty(props, SEARCH_JAVA_OPTS.getKey())
.addFromMandatoryProperty(props, SEARCH_JAVA_ADDITIONAL_OPTS.getKey()))
.setEsYmlSettings(new EsYmlSettings(settingsMap))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,18 +34,19 @@ public class EsJvmOptions extends JvmOptions<EsJvmOptions> {
"# DO NOT EDIT THIS FILE\n" +
"\n";

public EsJvmOptions() {
super(mandatoryOptions());
public EsJvmOptions(File tmpDir) {
super(mandatoryOptions(tmpDir));
}

private static Map<String, String> mandatoryOptions() {
private static Map<String, String> mandatoryOptions(File tmpDir) {
Map<String, String> res = new LinkedHashMap<>(16);
res.put("-XX:+UseConcMarkSweepGC", "");
res.put("-XX:CMSInitiatingOccupancyFraction=", "75");
res.put("-XX:+UseCMSInitiatingOccupancyOnly", "");
res.put("-XX:+AlwaysPreTouch", "");
res.put("-Xss", "1m");
res.put("-Djava.awt.headless=", "true");
res.put("-Djava.io.tmpdir=", tmpDir.getAbsolutePath());
res.put("-Dfile.encoding=", "UTF-8");
res.put("-Djna.nosys=", "true");
res.put("-Djdk.io.permissionsUseCanonicalPath=", "true");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public ProcessMonitor launch(AbstractCommand command) {
if (command instanceof EsScriptCommand) {
process = launchExternal((EsScriptCommand) command);
} else if (command instanceof JavaCommand) {
process = launchJava((JavaCommand) command);
process = launchJava((JavaCommand<?>) command);
} else {
throw new IllegalStateException("Unexpected type of command: " + command.getClass());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,9 @@ public void createEsCommand_for_unix_returns_command_for_default_settings() thro
.contains(entry("ES_JVM_OPTIONS", new File(esConfDir, "jvm.options").getAbsolutePath()))
.containsKey("JAVA_HOME");
assertThat(esCommand.getSuppressedEnvVariables()).containsOnly("JAVA_TOOL_OPTIONS", "ES_JAVA_OPTS");

assertThat(esConfig.getEsJvmOptions().getAll())
.contains("-Djava.io.tmpdir=" + tempDir.getAbsolutePath());
}

@Test
Expand Down Expand Up @@ -186,6 +189,8 @@ public void createEsCommand_for_windows_returns_command_for_default_settings() t
.containsKey("JAVA_HOME");
assertThat(esCommand.getSuppressedEnvVariables()).containsOnly("JAVA_TOOL_OPTIONS", "ES_JAVA_OPTS");

assertThat(esConfig.getEsJvmOptions().getAll())
.contains("-Djava.io.tmpdir=" + tempDir.getAbsolutePath());
assertThat(esCommand.getJvmOptions().getAll())
.containsAll(esConfig.getEsJvmOptions().getAll())
.contains("-Delasticsearch")
Expand All @@ -210,7 +215,8 @@ public void createEsCommand_returns_command_for_overridden_settings() throws Exc
assertThat(esConfig.getPort()).isEqualTo(1234);
assertThat(esConfig.getEsJvmOptions().getAll())
// enforced values
.contains("-XX:+UseConcMarkSweepGC", "-Dfile.encoding=UTF-8")
.contains("-XX:+UseConcMarkSweepGC", "-server", "-Dfile.encoding=UTF-8")
.contains("-Djava.io.tmpdir=" + tempDir.getAbsolutePath())
// user settings
.contains("-Xms10G", "-Xmx10G")
// default values disabled
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,9 @@ public class EsJvmOptionsTest {
public ExpectedException expectedException = ExpectedException.none();

@Test
public void constructor_sets_mandatory_JVM_options() {
EsJvmOptions underTest = new EsJvmOptions();
public void constructor_sets_mandatory_JVM_options() throws IOException {
File tmpDir = temporaryFolder.newFolder();
EsJvmOptions underTest = new EsJvmOptions(tmpDir);

assertThat(underTest.getAll()).containsExactly(
"-XX:+UseConcMarkSweepGC",
Expand All @@ -46,6 +47,7 @@ public void constructor_sets_mandatory_JVM_options() {
"-XX:+AlwaysPreTouch",
"-Xss1m",
"-Djava.awt.headless=true",
"-Djava.io.tmpdir="+ tmpDir.getAbsolutePath(),
"-Dfile.encoding=UTF-8",
"-Djna.nosys=true",
"-Djdk.io.permissionsUseCanonicalPath=true",
Expand All @@ -59,8 +61,9 @@ public void constructor_sets_mandatory_JVM_options() {

@Test
public void writeToJvmOptionFile_writes_all_JVM_options_to_file_with_warning_header() throws IOException {
File tmpDir = temporaryFolder.newFolder("with space");
File file = temporaryFolder.newFile();
EsJvmOptions underTest = new EsJvmOptions()
EsJvmOptions underTest = new EsJvmOptions(tmpDir)
.add("-foo")
.add("-bar");

Expand All @@ -78,6 +81,7 @@ public void writeToJvmOptionFile_writes_all_JVM_options_to_file_with_warning_hea
"-XX:+AlwaysPreTouch\n" +
"-Xss1m\n" +
"-Djava.awt.headless=true\n" +
"-Djava.io.tmpdir=" + tmpDir.getAbsolutePath() + "\n" +
"-Dfile.encoding=UTF-8\n" +
"-Djna.nosys=true\n" +
"-Djdk.io.permissionsUseCanonicalPath=true\n" +
Expand All @@ -95,7 +99,7 @@ public void writeToJvmOptionFile_writes_all_JVM_options_to_file_with_warning_hea
@Test
public void writeToJvmOptionFile_throws_ISE_in_case_of_IOException() throws IOException {
File notAFile = temporaryFolder.newFolder();
EsJvmOptions underTest = new EsJvmOptions();
EsJvmOptions underTest = new EsJvmOptions(temporaryFolder.newFolder());

expectedException.expect(IllegalStateException.class);
expectedException.expectMessage("Cannot write Elasticsearch jvm options file");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -202,16 +202,17 @@ private EsScriptCommand createEsScriptCommand(File tempDir, File homeDir, File d
}

private EsInstallation createEsInstallation() throws IOException {
File tempFolder = this.temp.newFolder("temp");
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()))
.set("sonar.path.home", this.temp.newFolder("home").getAbsolutePath())
.set("sonar.path.data", this.temp.newFolder("data").getAbsolutePath())
.set("sonar.path.temp", tempFolder.getAbsolutePath())
.set("sonar.path.logs", this.temp.newFolder("logs").getAbsolutePath()))
.setClusterName("cluster")
.setPort(9001)
.setHost("localhost")
.setEsYmlSettings(new EsYmlSettings(new HashMap<>()))
.setEsJvmOptions(new EsJvmOptions())
.setEsJvmOptions(new EsJvmOptions(tempFolder))
.setLog4j2Properties(new Properties());
}

Expand Down

0 comments on commit 9fa0a0f

Please sign in to comment.