diff options
author | Sébastien Lesaint <sebastien.lesaint@sonarsource.com> | 2017-08-18 16:11:54 +0200 |
---|---|---|
committer | Simon Brandhof <simon.brandhof@sonarsource.com> | 2017-09-05 14:24:12 +0200 |
commit | ae5637262461062c7e5fac17c1d6ac5792b5284d (patch) | |
tree | a819ae65cbabe3d25fbc8897e9337ad8b66f56a5 /server/sonar-process-monitor | |
parent | 4b8987715d5a476d3293a4a3e9d8e3f8d7a32e33 (diff) | |
download | sonarqube-ae5637262461062c7e5fac17c1d6ac5792b5284d.tar.gz sonarqube-ae5637262461062c7e5fac17c1d6ac5792b5284d.zip |
SONAR-9590 only heap and heap dump options are now in search.javaOpts
Diffstat (limited to 'server/sonar-process-monitor')
3 files changed, 40 insertions, 47 deletions
diff --git a/server/sonar-process-monitor/src/main/java/org/sonar/application/process/ProcessLauncherImpl.java b/server/sonar-process-monitor/src/main/java/org/sonar/application/process/ProcessLauncherImpl.java index bc19b2a2a69..549e4ddfe4e 100644 --- a/server/sonar-process-monitor/src/main/java/org/sonar/application/process/ProcessLauncherImpl.java +++ b/server/sonar-process-monitor/src/main/java/org/sonar/application/process/ProcessLauncherImpl.java @@ -23,15 +23,12 @@ import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStream; -import java.nio.charset.Charset; -import java.nio.file.Files; import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.Map; import java.util.Properties; import java.util.function.Supplier; -import java.util.stream.Collectors; import org.apache.commons.io.IOUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -39,6 +36,7 @@ import org.sonar.process.ProcessId; import org.sonar.process.command.AbstractCommand; import org.sonar.process.command.EsCommand; import org.sonar.process.command.JavaCommand; +import org.sonar.process.jmvoptions.JvmOptions; import org.sonar.process.sharedmemoryfile.AllProcessesCommands; import org.sonar.process.sharedmemoryfile.ProcessCommands; @@ -50,11 +48,6 @@ import static org.sonar.process.ProcessEntryPoint.PROPERTY_TERMINATION_TIMEOUT; public class ProcessLauncherImpl implements ProcessLauncher { private static final Logger LOG = LoggerFactory.getLogger(ProcessLauncherImpl.class); - private static final String ELASTICSEARCH_JVM_OPTIONS_HEADER = "# This file has been automatically generated by SonarQube during startup.\n" + - "# Please use the sonar.search.javaOpts in sonar.properties to specify jvm options for Elasticsearch\n" + - "\n" + - "# DO NOT EDIT THIS FILE\n" + - "\n"; private final File tempDir; private final AllProcessesCommands allProcessesCommands; @@ -106,7 +99,7 @@ public class ProcessLauncherImpl implements ProcessLauncher { try { IOUtils.copy(getClass().getResourceAsStream("elasticsearch.yml"), new FileOutputStream(new File(confDir, "elasticsearch.yml"))); - writeJvmOptions(esCommand, new File(confDir, "jvm.options")); + esCommand.getEsJvmOptions().writeToJvmOptionFile(new File(confDir, "jvm.options")); esCommand.getLog4j2Properties().store(new FileOutputStream(new File(confDir, "log4j2.properties")), "log42 properties file for ES bundled in SonarQube"); } catch (IOException e) { throw new IllegalStateException("Failed to write ES configuration files", e); @@ -150,29 +143,10 @@ public class ProcessLauncherImpl implements ProcessLauncher { return create(esCommand, commands); } - private static void writeJvmOptions(EsCommand esCommand, File jvmOptionsFile) { - String jvmOptions = esCommand.getJvmOptions() - .stream() - - // we do not expect the user to use parameters containing " -" - .map(s -> s.split(" (?=-)")) - .flatMap(Arrays::stream) - .collect(Collectors.joining("\n")); - String jvmOptionsContent = ELASTICSEARCH_JVM_OPTIONS_HEADER + jvmOptions; - try { - Files.write(jvmOptionsFile.toPath(), jvmOptionsContent.getBytes(Charset.forName("UTF-8"))); - } catch (IOException e) { - throw new IllegalStateException("Cannot write Elasticsearch jvm options file", e); - } - } - - private ProcessBuilder create(JavaCommand javaCommand) { + private <T extends JvmOptions> ProcessBuilder create(JavaCommand<T> javaCommand) { List<String> commands = new ArrayList<>(); commands.add(buildJavaPath()); - commands.addAll(javaCommand.getJavaOptions()); - // TODO warning - does it work if temp dir contains a whitespace ? - // TODO move to CommandFactory ? - commands.add(format("-Djava.io.tmpdir=%s", tempDir.getAbsolutePath())); + commands.addAll(javaCommand.getJvmOptions().getAll()); commands.addAll(buildClasspath(javaCommand)); commands.add(javaCommand.getClassName()); commands.add(buildPropertiesFile(javaCommand).getAbsolutePath()); diff --git a/server/sonar-process-monitor/src/test/java/org/sonar/application/SchedulerImplTest.java b/server/sonar-process-monitor/src/test/java/org/sonar/application/SchedulerImplTest.java index 231c2627450..f7336820816 100644 --- a/server/sonar-process-monitor/src/test/java/org/sonar/application/SchedulerImplTest.java +++ b/server/sonar-process-monitor/src/test/java/org/sonar/application/SchedulerImplTest.java @@ -19,6 +19,7 @@ */ package org.sonar.application; +import java.io.File; import java.io.InputStream; import java.util.ArrayList; import java.util.EnumMap; @@ -26,11 +27,13 @@ import java.util.List; import java.util.concurrent.CountDownLatch; import java.util.concurrent.TimeUnit; import org.junit.After; +import org.junit.Before; import org.junit.Ignore; import org.junit.Rule; import org.junit.Test; import org.junit.rules.DisableOnDebug; import org.junit.rules.ExpectedException; +import org.junit.rules.TemporaryFolder; import org.junit.rules.TestRule; import org.junit.rules.Timeout; import org.mockito.Mockito; @@ -56,15 +59,17 @@ import static org.sonar.process.ProcessId.WEB_SERVER; public class SchedulerImplTest { - private static final EsCommand ES_COMMAND = new EsCommand(ELASTICSEARCH); - private static final JavaCommand WEB_LEADER_COMMAND = new JavaCommand(WEB_SERVER); - private static final JavaCommand WEB_FOLLOWER_COMMAND = new JavaCommand(WEB_SERVER); - private static final JavaCommand CE_COMMAND = new JavaCommand(COMPUTE_ENGINE); - @Rule public TestRule safeguardTimeout = new DisableOnDebug(Timeout.seconds(60)); @Rule public ExpectedException expectedException = ExpectedException.none(); + @Rule + public TemporaryFolder temporaryFolder = new TemporaryFolder(); + + private EsCommand esCommand; + private JavaCommand webLeaderCommand; + private JavaCommand webFollowerCommand; + private JavaCommand ceCommand; private AppReloader appReloader = mock(AppReloader.class); private TestAppSettings settings = new TestAppSettings(); @@ -73,6 +78,15 @@ public class SchedulerImplTest { private TestAppState appState = new TestAppState(); private List<ProcessId> orderedStops = synchronizedList(new ArrayList<>()); + @Before + public void setUp() throws Exception { + File tempDir = temporaryFolder.newFolder(); + esCommand = new EsCommand(ELASTICSEARCH, tempDir); + webLeaderCommand = new JavaCommand(WEB_SERVER, tempDir); + webFollowerCommand = new JavaCommand(WEB_SERVER, tempDir); + ceCommand = new JavaCommand(COMPUTE_ENGINE, tempDir); + } + @After public void tearDown() throws Exception { processLauncher.close(); @@ -95,7 +109,7 @@ public class SchedulerImplTest { TestProcess web = processLauncher.waitForProcess(WEB_SERVER); assertThat(web.isAlive()).isTrue(); assertThat(processLauncher.processes).hasSize(2); - assertThat(processLauncher.commands).containsExactly(ES_COMMAND, WEB_LEADER_COMMAND); + assertThat(processLauncher.commands).containsExactly(esCommand, webLeaderCommand); // web becomes operational -> CE is starting web.operational = true; @@ -103,7 +117,7 @@ public class SchedulerImplTest { TestProcess ce = processLauncher.waitForProcess(COMPUTE_ENGINE); assertThat(ce.isAlive()).isTrue(); assertThat(processLauncher.processes).hasSize(3); - assertThat(processLauncher.commands).containsExactly(ES_COMMAND, WEB_LEADER_COMMAND, CE_COMMAND); + assertThat(processLauncher.commands).containsExactly(esCommand, webLeaderCommand, ceCommand); // all processes are up processLauncher.processes.values().forEach(p -> assertThat(p.isAlive()).isTrue()); @@ -307,20 +321,20 @@ public class SchedulerImplTest { } } - private static class TestCommandFactory implements CommandFactory { + private class TestCommandFactory implements CommandFactory { @Override public EsCommand createEsCommand() { - return ES_COMMAND; + return esCommand; } @Override public JavaCommand createWebCommand(boolean leader) { - return leader ? WEB_LEADER_COMMAND : WEB_FOLLOWER_COMMAND; + return leader ? webLeaderCommand : webFollowerCommand; } @Override public JavaCommand createCeCommand() { - return CE_COMMAND; + return ceCommand; } } diff --git a/server/sonar-process-monitor/src/test/java/org/sonar/application/process/ProcessLauncherImplTest.java b/server/sonar-process-monitor/src/test/java/org/sonar/application/process/ProcessLauncherImplTest.java index 044aa7b4310..a0a4bba391d 100644 --- a/server/sonar-process-monitor/src/test/java/org/sonar/application/process/ProcessLauncherImplTest.java +++ b/server/sonar-process-monitor/src/test/java/org/sonar/application/process/ProcessLauncherImplTest.java @@ -30,9 +30,10 @@ import org.junit.Rule; import org.junit.Test; import org.junit.rules.ExpectedException; import org.junit.rules.TemporaryFolder; +import org.sonar.process.ProcessId; import org.sonar.process.command.JavaCommand; +import org.sonar.process.jmvoptions.JvmOptions; import org.sonar.process.sharedmemoryfile.AllProcessesCommands; -import org.sonar.process.ProcessId; import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.data.MapEntry.entry; @@ -54,13 +55,15 @@ public class ProcessLauncherImplTest { File tempDir = temp.newFolder(); TestProcessBuilder processBuilder = new TestProcessBuilder(); ProcessLauncher underTest = new ProcessLauncherImpl(tempDir, commands, () -> processBuilder); - JavaCommand command = new JavaCommand(ProcessId.ELASTICSEARCH); + JavaCommand<JvmOptions> command = new JavaCommand<>(ProcessId.ELASTICSEARCH, temp.newFolder()); command.addClasspath("lib/*.class"); command.addClasspath("lib/*.jar"); command.setArgument("foo", "bar"); command.setClassName("org.sonarqube.Main"); command.setEnvVariable("VAR1", "valueOfVar1"); - command.setWorkDir(temp.newFolder()); + command.setJvmOptions(new JvmOptions<>() + .add("-Dfoo=bar") + .add("-Dfoo2=bar2")); ProcessMonitor monitor = underTest.launch(command); @@ -68,7 +71,8 @@ public class ProcessLauncherImplTest { assertThat(processBuilder.started).isTrue(); assertThat(processBuilder.commands.get(0)).endsWith("java"); assertThat(processBuilder.commands).containsSequence( - "-Djava.io.tmpdir=" + tempDir.getAbsolutePath(), + "-Dfoo=bar", + "-Dfoo2=bar2", "-cp", "lib/*.class" + System.getProperty("path.separator") + "lib/*.jar", "org.sonarqube.Main"); @@ -84,9 +88,10 @@ public class ProcessLauncherImplTest { File tempDir = temp.newFolder(); TestProcessBuilder processBuilder = new TestProcessBuilder(); ProcessLauncher underTest = new ProcessLauncherImpl(tempDir, commands, () -> processBuilder); - JavaCommand command = new JavaCommand(ProcessId.ELASTICSEARCH); + JavaCommand<JvmOptions> command = new JavaCommand<>(ProcessId.ELASTICSEARCH, temp.newFolder()); command.setArgument("foo", "bar"); command.setArgument("baz", "woo"); + command.setJvmOptions(new JvmOptions<>()); underTest.launch(command); @@ -116,7 +121,7 @@ public class ProcessLauncherImplTest { expectedException.expect(IllegalStateException.class); expectedException.expectMessage("Fail to launch process [es]"); - underTest.launch(new JavaCommand(ProcessId.ELASTICSEARCH)); + underTest.launch(new JavaCommand(ProcessId.ELASTICSEARCH, temp.newFolder())); } private static class TestProcessBuilder implements ProcessLauncherImpl.ProcessBuilder { |