From 242b94658b9764556bf7227de40f3e34058422e9 Mon Sep 17 00:00:00 2001 From: Stephane Gamard Date: Mon, 25 Aug 2014 17:38:56 +0200 Subject: [PATCH] Disable process monitoring when in debug with agentlib:jdwt --- debug.sh | 10 +++ .../org/sonar/process/MonitoredProcess.java | 2 +- .../sonar/process/MonitoredProcessTest.java | 81 +++++++++++++------ .../java/org/sonar/search/SearchServer.java | 2 +- .../main/java/org/sonar/application/App.java | 7 ++ 5 files changed, 76 insertions(+), 26 deletions(-) create mode 100755 debug.sh diff --git a/debug.sh b/debug.sh new file mode 100755 index 00000000000..5dadb3becab --- /dev/null +++ b/debug.sh @@ -0,0 +1,10 @@ +#!/bin/sh + + +cd sonar-application/target/ +if ! ls sonarqube-*/bin/sonar-application*.jar &> /dev/null; then + unzip sonarqube-*.zip +fi + +cd sonarqube-* +java -jar ./lib/sonar-application*.jar -Dsonar.web.javaOpts=-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=5005 \ No newline at end of file diff --git a/server/process/sonar-process/src/main/java/org/sonar/process/MonitoredProcess.java b/server/process/sonar-process/src/main/java/org/sonar/process/MonitoredProcess.java index 6d75bece303..4a6c3ec5e51 100644 --- a/server/process/sonar-process/src/main/java/org/sonar/process/MonitoredProcess.java +++ b/server/process/sonar-process/src/main/java/org/sonar/process/MonitoredProcess.java @@ -32,7 +32,7 @@ public abstract class MonitoredProcess implements ProcessMXBean { private final static Logger LOGGER = LoggerFactory.getLogger(MonitoredProcess.class); - private static final String DEBUG_AGENT = "-agentlib:jdwp"; + public static final String DEBUG_AGENT = "-agentlib:jdwp"; private static final long AUTOKILL_TIMEOUT_MS = 30000L; private static final long AUTOKILL_CHECK_DELAY_MS = 2000L; public static final String NAME_PROPERTY = "pName"; diff --git a/server/process/sonar-process/src/test/java/org/sonar/process/MonitoredProcessTest.java b/server/process/sonar-process/src/test/java/org/sonar/process/MonitoredProcessTest.java index f3e4d2a78db..e8f87147654 100644 --- a/server/process/sonar-process/src/test/java/org/sonar/process/MonitoredProcessTest.java +++ b/server/process/sonar-process/src/test/java/org/sonar/process/MonitoredProcessTest.java @@ -78,18 +78,12 @@ public class MonitoredProcessTest { dummyProcess.start(); } }); - assertThat(dummyProcess.isReady()).isFalse(); - assertThat(dummyProcess.isTerminated()).isFalse(); + assertProcessNotYetRunning(dummyProcess); process.start(); Thread.sleep(100); - assertThat(dummyProcess.isReady()).isTrue(); - assertThat(dummyProcess.isTerminated()).isFalse(); - process.join(); - assertThat(dummyProcess.isReady()).isTrue(); - assertThat(dummyProcess.isTerminated()).isTrue(); - assertThat(dummyProcess.getCheckFile()).isNotNull(); - assertThat(dummyProcess.getCheckFile().getName()).isEqualTo(DummyProcess.CHECKFILE_NAME); + assertProcessRunning(dummyProcess); + assertJoinAndTerminate(dummyProcess, process); } @Test(timeout = 3000L) @@ -105,30 +99,23 @@ public class MonitoredProcessTest { dummyProcess.start(); } }); - assertThat(dummyProcess.isReady()).isFalse(); - assertThat(dummyProcess.isTerminated()).isFalse(); + assertProcessNotYetRunning(dummyProcess); process.start(); Thread.sleep(100); int count = 0; for (int i = 0; i < 3; i++) { dummyProcess.ping(); - assertThat(dummyProcess.isReady()).isTrue(); - assertThat(dummyProcess.isTerminated()).isFalse(); + assertProcessRunning(dummyProcess); Thread.sleep(300); count++; } assertThat(count).isEqualTo(3); - process.join(); - assertThat(dummyProcess.isReady()).isTrue(); - assertThat(dummyProcess.isTerminated()).isTrue(); - - assertThat(dummyProcess.getCheckFile()).isNotNull(); - assertThat(dummyProcess.getCheckFile().getName()).isEqualTo(DummyProcess.CHECKFILE_NAME); + assertJoinAndTerminate(dummyProcess, process); } @Test(timeout = 3000L) - public void monitor_explicitely_shutdown() throws Exception { + public void monitor_explicitly_shutdown() throws Exception { Properties properties = new Properties(); properties.setProperty(MonitoredProcess.NAME_PROPERTY, DummyProcess.NAME); final DummyProcess dummyProcess = new DummyProcess(new Props(properties), true); @@ -139,17 +126,63 @@ public class MonitoredProcessTest { dummyProcess.start(); } }); - assertThat(dummyProcess.isReady()).isFalse(); - assertThat(dummyProcess.isTerminated()).isFalse(); + assertProcessNotYetRunning(dummyProcess); process.start(); Thread.sleep(100); - assertThat(dummyProcess.isReady()).isTrue(); - assertThat(dummyProcess.isTerminated()).isFalse(); + assertProcessRunning(dummyProcess); dummyProcess.terminate(); Thread.sleep(100); + assertProcessTerminated(dummyProcess); + } + + @Test(timeout = 1000L) + public void process_does_not_die_when_debugged() throws Exception { + Properties properties = new Properties(); + properties.setProperty(MonitoredProcess.NAME_PROPERTY, DummyProcess.NAME); + properties.setProperty("sonar.search.javaOpts", "-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=5005"); + + final DummyProcess dummyProcess = new DummyProcess(new Props(properties)); + assertThat(dummyProcess.isMonitored()).isFalse(); + + dummyProcess.setTimeout(100L).setCheckDelay(100L); + Thread process = new Thread(new Runnable() { + @Override + public void run() { + dummyProcess.start(); + } + }); + process.start(); + Thread.sleep(600); + + assertProcessRunning(dummyProcess); + dummyProcess.terminate(); + assertProcessTerminated(dummyProcess); + } + + + private void assertJoinAndTerminate(DummyProcess dummyProcess, Thread process) throws InterruptedException { + process.join(); + assertProcessTerminated(dummyProcess); + } + + private void assertProcessTerminated(DummyProcess dummyProcess) { assertThat(dummyProcess.isReady()).isTrue(); assertThat(dummyProcess.isTerminated()).isTrue(); + assertProcessCreatedFile(dummyProcess); + } + + private void assertProcessNotYetRunning(DummyProcess dummyProcess) { + assertThat(dummyProcess.isReady()).isFalse(); + assertThat(dummyProcess.isTerminated()).isFalse(); + } + + private void assertProcessRunning(DummyProcess dummyProcess) throws InterruptedException { + assertThat(dummyProcess.isReady()).isTrue(); + assertThat(dummyProcess.isTerminated()).isFalse(); + } + + private void assertProcessCreatedFile(DummyProcess dummyProcess) { assertThat(dummyProcess.getCheckFile()).isNotNull(); assertThat(dummyProcess.getCheckFile().getName()).isEqualTo(DummyProcess.CHECKFILE_NAME); } diff --git a/server/sonar-search/src/main/java/org/sonar/search/SearchServer.java b/server/sonar-search/src/main/java/org/sonar/search/SearchServer.java index 11196971a74..873e8b95ab0 100644 --- a/server/sonar-search/src/main/java/org/sonar/search/SearchServer.java +++ b/server/sonar-search/src/main/java/org/sonar/search/SearchServer.java @@ -74,7 +74,7 @@ public class SearchServer extends MonitoredProcess { public SearchServer(Props props) { super(props); - this.isBlocking = false; + this.isBlocking = true; new MinimumViableSystem().check(); String esNodesInets = props.of(ES_CLUSTER_INET); 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 72e7e229e7a..67efece6734 100644 --- a/sonar-application/src/main/java/org/sonar/application/App.java +++ b/sonar-application/src/main/java/org/sonar/application/App.java @@ -26,6 +26,7 @@ import org.slf4j.LoggerFactory; import org.sonar.process.JmxUtils; import org.sonar.process.MinimumViableSystem; import org.sonar.process.Monitor; +import org.sonar.process.MonitoredProcess; import org.sonar.process.ProcessLogging; import org.sonar.process.ProcessMXBean; import org.sonar.process.ProcessUtils; @@ -56,6 +57,12 @@ public class App implements ProcessMXBean { public void start(Props props) throws InterruptedException { try { Logger logger = LoggerFactory.getLogger(getClass()); + + if (props.containsValue(MonitoredProcess.DEBUG_AGENT)) { + logger.info("**********************************************************"); + logger.info("* sonarQube is running in debug mode. No monitoring *"); + logger.info("**********************************************************"); + } monitor.start(); File homeDir = props.fileOf("sonar.path.home"); -- 2.39.5