From: Sébastien Lesaint Date: Wed, 9 Mar 2016 15:56:51 +0000 (+0100) Subject: SONAR-7435 SQ processes should have constant process ids X-Git-Tag: 5.5-M11~86 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=8a9738d1bebdb4584b40370fd45e43e898e29a15;p=sonarqube.git SONAR-7435 SQ processes should have constant process ids remove process id generation from JavaCommand --- diff --git a/server/sonar-process-monitor/src/main/java/org/sonar/process/monitor/JavaCommand.java b/server/sonar-process-monitor/src/main/java/org/sonar/process/monitor/JavaCommand.java index ea7e0bee695..6a4f77607d9 100644 --- a/server/sonar-process-monitor/src/main/java/org/sonar/process/monitor/JavaCommand.java +++ b/server/sonar-process-monitor/src/main/java/org/sonar/process/monitor/JavaCommand.java @@ -52,11 +52,11 @@ public class JavaCommand { private final Map envVariables = new HashMap<>(System.getenv()); - private int processIndex = -1; + private final int processIndex; - public JavaCommand(String key) { + public JavaCommand(String key, int processIndex) { this.key = key; - processIndex = Monitor.getNextProcessId(); + this.processIndex = processIndex; } public String getKey() { diff --git a/server/sonar-process-monitor/src/main/java/org/sonar/process/monitor/Monitor.java b/server/sonar-process-monitor/src/main/java/org/sonar/process/monitor/Monitor.java index 6dc22a292f0..4e5683d2e1b 100644 --- a/server/sonar-process-monitor/src/main/java/org/sonar/process/monitor/Monitor.java +++ b/server/sonar-process-monitor/src/main/java/org/sonar/process/monitor/Monitor.java @@ -31,7 +31,6 @@ import org.slf4j.LoggerFactory; import org.sonar.process.DefaultProcessCommands; import org.sonar.process.Lifecycle; import org.sonar.process.Lifecycle.State; -import org.sonar.process.ProcessCommands; import org.sonar.process.ProcessUtils; import org.sonar.process.SystemExit; @@ -40,10 +39,10 @@ public class Monitor { private static final Logger LOG = LoggerFactory.getLogger(Monitor.class); private static final Timeouts TIMEOUTS = new Timeouts(); private static final long WATCH_DELAY_MS = 500L; - private static final int CURRENT_PROCESS_NUMBER = 0; private static int restartorInstanceCounter = 0; + private final int processNumber; private final FileSystem fileSystem; private final SystemExit systemExit; private final boolean watchForHardStop; @@ -62,16 +61,16 @@ public class Monitor { private RestartorThread restartor; @CheckForNull HardStopWatcherThread hardStopWatcher; - static int nextProcessId = 1; - Monitor(FileSystem fileSystem, SystemExit exit, boolean watchForHardStop) { + Monitor(int processNumber, FileSystem fileSystem, SystemExit exit, boolean watchForHardStop) { + this.processNumber = processNumber; this.fileSystem = fileSystem; this.systemExit = exit; this.watchForHardStop = watchForHardStop; } - public static Monitor create(FileSystem fileSystem, boolean watchForHardStop) { - return new Monitor(fileSystem, new SystemExit(), watchForHardStop); + public static Monitor create(int processNumber, FileSystem fileSystem, boolean watchForHardStop) { + return new Monitor(processNumber, fileSystem, new SystemExit(), watchForHardStop); } /** @@ -356,7 +355,7 @@ public class Monitor { private boolean askedForStop() { File tempDir = fileSystem.getTempDir(); - try (DefaultProcessCommands processCommands = DefaultProcessCommands.secondary(tempDir, CURRENT_PROCESS_NUMBER)) { + try (DefaultProcessCommands processCommands = DefaultProcessCommands.secondary(tempDir, processNumber)) { if (processCommands.askedForStop()) { return true; } @@ -452,10 +451,4 @@ public class Monitor { LOG.trace(s, args); } - public static int getNextProcessId() { - if (nextProcessId >= ProcessCommands.MAX_PROCESSES) { - throw new IllegalStateException("The maximum number of processes launched has been reached " + ProcessCommands.MAX_PROCESSES); - } - return nextProcessId++; - } } diff --git a/server/sonar-process-monitor/src/test/java/org/sonar/process/monitor/JavaCommandTest.java b/server/sonar-process-monitor/src/test/java/org/sonar/process/monitor/JavaCommandTest.java index 9f24c04decb..e53c564cdac 100644 --- a/server/sonar-process-monitor/src/test/java/org/sonar/process/monitor/JavaCommandTest.java +++ b/server/sonar-process-monitor/src/test/java/org/sonar/process/monitor/JavaCommandTest.java @@ -32,10 +32,11 @@ public class JavaCommandTest { @Rule public TemporaryFolder temp = new TemporaryFolder(); + private int processIndex = 0; @Test public void test_parameters() throws Exception { - JavaCommand command = new JavaCommand("es"); + JavaCommand command = new JavaCommand("es", processIndex++); command.setArgument("first_arg", "val1"); Properties args = new Properties(); @@ -63,7 +64,7 @@ public class JavaCommandTest { @Test public void add_java_options() { - JavaCommand command = new JavaCommand("foo"); + JavaCommand command = new JavaCommand("foo", processIndex++); assertThat(command.getJavaOptions()).isEmpty(); command.addJavaOptions(""); diff --git a/server/sonar-process-monitor/src/test/java/org/sonar/process/monitor/JavaProcessLauncherTest.java b/server/sonar-process-monitor/src/test/java/org/sonar/process/monitor/JavaProcessLauncherTest.java index af32f3f759b..499b22a04bd 100644 --- a/server/sonar-process-monitor/src/test/java/org/sonar/process/monitor/JavaProcessLauncherTest.java +++ b/server/sonar-process-monitor/src/test/java/org/sonar/process/monitor/JavaProcessLauncherTest.java @@ -35,7 +35,7 @@ public class JavaProcessLauncherTest { @Test public void fail_to_launch() throws Exception { File tempDir = temp.newFolder(); - JavaCommand command = new JavaCommand("test"); + JavaCommand command = new JavaCommand("test", 0); JavaProcessLauncher launcher = new JavaProcessLauncher(new Timeouts(), tempDir); try { // command is not correct (missing options), java.lang.ProcessBuilder#start() diff --git a/server/sonar-process-monitor/src/test/java/org/sonar/process/monitor/MonitorTest.java b/server/sonar-process-monitor/src/test/java/org/sonar/process/monitor/MonitorTest.java index 9b8a305a7f4..f278e4978bb 100644 --- a/server/sonar-process-monitor/src/test/java/org/sonar/process/monitor/MonitorTest.java +++ b/server/sonar-process-monitor/src/test/java/org/sonar/process/monitor/MonitorTest.java @@ -42,7 +42,6 @@ import org.junit.rules.TestRule; import org.junit.rules.Timeout; import org.sonar.process.Lifecycle.State; import org.sonar.process.NetworkUtils; -import org.sonar.process.ProcessCommands; import org.sonar.process.SystemExit; import static java.util.Collections.singletonList; @@ -58,6 +57,7 @@ public class MonitorTest { private static File testJar; + private int processIndex = 0; private FileSystem fileSystem = mock(FileSystem.class); private SystemExit exit = mock(SystemExit.class); @@ -283,23 +283,10 @@ public class MonitorTest { } } - @Test - public void test_too_many_processes() throws Exception { - while (Monitor.getNextProcessId() < ProcessCommands.MAX_PROCESSES - 1) { - } - try { - newDefaultMonitor(tempDir); - } catch (IllegalStateException e) { - assertThat(e).hasMessageStartingWith("The maximum number of processes launched has been reached "); - } finally { - Monitor.nextProcessId = 0; - } - } - @Test public void fail_to_start_if_bad_class_name() throws Exception { underTest = newDefaultMonitor(tempDir); - JavaCommand command = new JavaCommand("test") + JavaCommand command = new JavaCommand("test", processIndex++) .addClasspath(testJar.getAbsolutePath()) .setClassName("org.sonar.process.test.Unknown"); @@ -335,7 +322,7 @@ public class MonitorTest { private Monitor newDefaultMonitor(File tempDir, boolean watchForHardStop) throws IOException { when(fileSystem.getTempDir()).thenReturn(tempDir); - return new Monitor(fileSystem, exit, watchForHardStop); + return new Monitor(1, fileSystem, exit, watchForHardStop); } /** @@ -360,7 +347,7 @@ public class MonitorTest { } JavaCommand newCommand() { - return new JavaCommand(commandKey) + return new JavaCommand(commandKey, processIndex++) .addClasspath(testJar.getAbsolutePath()) .setClassName("org.sonar.process.test.HttpProcess") .setArgument("httpPort", String.valueOf(httpPort)); @@ -583,7 +570,7 @@ public class MonitorTest { } private JavaCommand newStandardProcessCommand() throws IOException { - return new JavaCommand("standard") + return new JavaCommand("standard", processIndex++) .addClasspath(testJar.getAbsolutePath()) .setClassName("org.sonar.process.test.StandardProcess"); } diff --git a/server/sonar-process/src/main/java/org/sonar/process/AllProcessesCommands.java b/server/sonar-process/src/main/java/org/sonar/process/AllProcessesCommands.java index fe11a0e9fa3..512cd788bab 100644 --- a/server/sonar-process/src/main/java/org/sonar/process/AllProcessesCommands.java +++ b/server/sonar-process/src/main/java/org/sonar/process/AllProcessesCommands.java @@ -168,8 +168,7 @@ public class AllProcessesCommands { } public void checkProcessNumber(int processNumber) { - boolean result = processNumber >= 0 && processNumber < MAX_PROCESSES; - if (!result) { + if (processNumber < 0 || processNumber >= MAX_PROCESSES) { throw new IllegalArgumentException(String.format("Process number %s is not valid", processNumber)); } } diff --git a/server/sonar-process/src/test/java/org/sonar/process/DefaultProcessCommandsTest.java b/server/sonar-process/src/test/java/org/sonar/process/DefaultProcessCommandsTest.java index 4bd68e86c7c..0b981e03ac0 100644 --- a/server/sonar-process/src/test/java/org/sonar/process/DefaultProcessCommandsTest.java +++ b/server/sonar-process/src/test/java/org/sonar/process/DefaultProcessCommandsTest.java @@ -127,6 +127,15 @@ public class DefaultProcessCommandsTest { DefaultProcessCommands.main(temp.newFolder(), processNumber); } + @Test + public void main_fails_if_processNumber_is_MAX_PROCESSES() throws Exception { + int processNumber = MAX_PROCESSES; + + expectProcessNumberNoValidIAE(processNumber); + + DefaultProcessCommands.main(temp.newFolder(), processNumber); + } + @Test public void secondary_fails_if_processNumber_is_less_than_0() throws Exception { int processNumber = -2; diff --git a/sonar-application/src/main/java/org/sonar/application/App.java b/sonar-application/src/main/java/org/sonar/application/App.java index 1537405fa04..c66b5fade38 100644 --- a/sonar-application/src/main/java/org/sonar/application/App.java +++ b/sonar-application/src/main/java/org/sonar/application/App.java @@ -33,14 +33,19 @@ import org.sonar.process.monitor.JavaCommand; import org.sonar.process.monitor.Monitor; /** - * Entry-point of process that starts and monitors elasticsearch and web servers + * Entry-point of process that starts and monitors ElasticSearch, the Web Server and the Compute Engine. */ public class App implements Stoppable { + public static final int APP_PROCESS_NUMBER = 0; + public static final int ES_PROCESS_INDEX = 1; + public static final int WEBSERVER_PROCESS_INDEX = 2; + public static final int CESERVER_PROCESS_INDEX = 3; + private final Monitor monitor; public App(AppFileSystem appFileSystem, boolean watchForHardStop) { - this(Monitor.create(appFileSystem, watchForHardStop)); + this(Monitor.create(APP_PROCESS_NUMBER, appFileSystem, watchForHardStop)); } App(Monitor monitor) { @@ -67,7 +72,7 @@ public class App implements Stoppable { } private static JavaCommand createESCommand(Props props, File homeDir) { - JavaCommand elasticsearch = new JavaCommand("search"); + JavaCommand elasticsearch = new JavaCommand("search", ES_PROCESS_INDEX); elasticsearch .setWorkDir(homeDir) .addJavaOptions("-Djava.awt.headless=true") @@ -81,7 +86,7 @@ public class App implements Stoppable { } private static JavaCommand createWebServerCommand(Props props, File homeDir) { - JavaCommand webServer = new JavaCommand("web") + JavaCommand webServer = new JavaCommand("web", WEBSERVER_PROCESS_INDEX) .setWorkDir(homeDir) .addJavaOptions(ProcessProperties.WEB_ENFORCED_JVM_ARGS) .addJavaOptions(props.nonNullValue(ProcessProperties.WEB_JAVA_OPTS)) @@ -100,7 +105,7 @@ public class App implements Stoppable { } private static JavaCommand createCeServerCommand(Props props, File homeDir) { - JavaCommand webServer = new JavaCommand("ce") + JavaCommand webServer = new JavaCommand("ce", CESERVER_PROCESS_INDEX) .setWorkDir(homeDir) .setClassName("org.sonar.ce.app.CeServer") .setArguments(props.rawProperties())