aboutsummaryrefslogtreecommitdiffstats
path: root/server/sonar-process-monitor
diff options
context:
space:
mode:
authorEric Hartmann <hartmann.eric@gmail.com>2015-02-19 18:58:59 +0100
committerSimon Brandhof <simon.brandhof@sonarsource.com>2015-02-20 19:07:28 +0100
commit2b5e9191374d82eeed8f07fa4940aeee75ff4bdb (patch)
treeae313cfb234cda831e667d1ffe755c875e434b73 /server/sonar-process-monitor
parenta46fb632283502280fe79d7908068f579a5dbba2 (diff)
downloadsonarqube-2b5e9191374d82eeed8f07fa4940aeee75ff4bdb.tar.gz
sonarqube-2b5e9191374d82eeed8f07fa4940aeee75ff4bdb.zip
SONAR-6036 use IPC (Inter Process Communication) for monitoring
processes
Diffstat (limited to 'server/sonar-process-monitor')
-rw-r--r--server/sonar-process-monitor/src/main/java/org/sonar/process/monitor/JavaCommand.java10
-rw-r--r--server/sonar-process-monitor/src/main/java/org/sonar/process/monitor/JavaProcessLauncher.java4
-rw-r--r--server/sonar-process-monitor/src/main/java/org/sonar/process/monitor/KnownJavaCommand.java64
-rw-r--r--server/sonar-process-monitor/src/main/java/org/sonar/process/monitor/Monitor.java9
-rw-r--r--server/sonar-process-monitor/src/test/java/org/sonar/process/monitor/MonitorTest.java13
5 files changed, 98 insertions, 2 deletions
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 813ea1d6ea9..f4593308b9b 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
@@ -54,14 +54,24 @@ public class JavaCommand {
private File tempDir = null;
+ private int processIndex = -1;
+
public JavaCommand(String key) {
this.key = key;
+ processIndex = KnownJavaCommand.lookIndexFor(key);
+ if (processIndex == -1) {
+ processIndex = Monitor.getNextProcessId();
+ }
}
public String getKey() {
return key;
}
+ public int getProcessIndex() {
+ return processIndex;
+ }
+
public File getWorkDir() {
return workDir;
}
diff --git a/server/sonar-process-monitor/src/main/java/org/sonar/process/monitor/JavaProcessLauncher.java b/server/sonar-process-monitor/src/main/java/org/sonar/process/monitor/JavaProcessLauncher.java
index 3cedc1c9c84..dd67a10489d 100644
--- a/server/sonar-process-monitor/src/main/java/org/sonar/process/monitor/JavaProcessLauncher.java
+++ b/server/sonar-process-monitor/src/main/java/org/sonar/process/monitor/JavaProcessLauncher.java
@@ -45,8 +45,7 @@ public class JavaProcessLauncher {
Process process = null;
try {
// cleanup existing monitor files
- ProcessCommands commands = new ProcessCommands(command.getTempDir(), command.getKey());
- commands.prepare();
+ ProcessCommands commands = new ProcessCommands(command.getTempDir(), command.getProcessIndex());
ProcessBuilder processBuilder = create(command);
LoggerFactory.getLogger(getClass()).info("Launch process[{}]: {}",
@@ -99,6 +98,7 @@ public class JavaProcessLauncher {
Properties props = new Properties();
props.putAll(javaCommand.getArguments());
props.setProperty(ProcessEntryPoint.PROPERTY_PROCESS_KEY, javaCommand.getKey());
+ props.setProperty(ProcessEntryPoint.PROPERTY_PROCESS_INDEX, "" + javaCommand.getProcessIndex());
props.setProperty(ProcessEntryPoint.PROPERTY_TERMINATION_TIMEOUT, String.valueOf(timeouts.getTerminationTimeout()));
props.setProperty(ProcessEntryPoint.PROPERTY_SHARED_PATH, javaCommand.getTempDir().getAbsolutePath());
OutputStream out = new FileOutputStream(propertiesFile);
diff --git a/server/sonar-process-monitor/src/main/java/org/sonar/process/monitor/KnownJavaCommand.java b/server/sonar-process-monitor/src/main/java/org/sonar/process/monitor/KnownJavaCommand.java
new file mode 100644
index 00000000000..ce9a49aed1a
--- /dev/null
+++ b/server/sonar-process-monitor/src/main/java/org/sonar/process/monitor/KnownJavaCommand.java
@@ -0,0 +1,64 @@
+/*
+ * SonarQube, open source software quality management tool.
+ * Copyright (C) 2008-2014 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * SonarQube is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * SonarQube is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+package org.sonar.process.monitor;
+
+/**
+ * Created by eric on 20/02/15.
+ */
+public enum KnownJavaCommand {
+ APP("app", 0), WEB("web", 1), ELASTIC_SEARCH("search", 2), UNKNOWN("unknown", -1);
+
+ private String key;
+ private int index;
+
+ KnownJavaCommand(String key, int index) {
+ this.key = key;
+ this.index = index;
+ }
+
+ public String getKey() {
+ return key;
+ }
+
+ public int getIndex() {
+ return index;
+ }
+
+ public static KnownJavaCommand lookFor(String key) {
+ for (KnownJavaCommand knownJavaCommand : KnownJavaCommand.values()) {
+ if (knownJavaCommand.getKey().equals(key)) {
+ return knownJavaCommand;
+ }
+ }
+ return KnownJavaCommand.UNKNOWN;
+ }
+
+ public static int lookIndexFor(String key) {
+ return lookFor(key).getIndex();
+ }
+
+ public static int getFirstIndexAvailable() {
+ int result = 0;
+ for (KnownJavaCommand knownJavaCommand : KnownJavaCommand.values()) {
+ result = knownJavaCommand.getIndex() >= result ? knownJavaCommand.getIndex() + 1 : result;
+ }
+ return result;
+ }
+}
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 4af3a08a1c2..ccbeb558db0 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
@@ -22,6 +22,7 @@ package org.sonar.process.monitor;
import org.slf4j.LoggerFactory;
import org.sonar.process.Lifecycle;
import org.sonar.process.Lifecycle.State;
+import org.sonar.process.ProcessCommands;
import org.sonar.process.SystemExit;
import java.util.List;
@@ -39,6 +40,7 @@ public class Monitor {
// used by awaitStop() to block until all processes are shutdown
private final List<WatcherThread> watcherThreads = new CopyOnWriteArrayList<WatcherThread>();
+ static int nextProcessId = KnownJavaCommand.getFirstIndexAvailable();
Monitor(JavaProcessLauncher launcher, SystemExit exit, TerminatorThread terminator) {
this.launcher = launcher;
@@ -161,4 +163,11 @@ public class Monitor {
stop();
}
}
+
+ public static int getNextProcessId() {
+ if (nextProcessId >= ProcessCommands.getMaxProcesses()) {
+ throw new IllegalStateException("The maximum number of processes launched has been reached " + ProcessCommands.getMaxProcesses());
+ }
+ return nextProcessId++;
+ }
}
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 46200f45b7b..4a27d50ed62 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
@@ -29,6 +29,7 @@ import org.junit.rules.TemporaryFolder;
import org.junit.rules.Timeout;
import org.sonar.process.NetworkUtils;
import org.sonar.process.Lifecycle.State;
+import org.sonar.process.ProcessCommands;
import org.sonar.process.SystemExit;
import java.io.File;
@@ -209,6 +210,18 @@ public class MonitorTest {
}
@Test
+ public void test_too_many_processes() {
+ while (Monitor.getNextProcessId() < ProcessCommands.getMaxProcesses() - 1) {}
+ try {
+ newDefaultMonitor();
+ } catch (IllegalStateException e) {
+ assertThat(e).hasMessageStartingWith("The maximum number of processes launched has been reached ");
+ } finally {
+ Monitor.nextProcessId = KnownJavaCommand.getFirstIndexAvailable();
+ }
+ }
+
+ @Test
public void force_stop_if_too_long() throws Exception {
// TODO
}