From 70dfdc0bdcff5bfd6924c2832c5fdff69d1f201a Mon Sep 17 00:00:00 2001 From: Stephane Gamard Date: Mon, 25 Aug 2014 16:00:18 +0200 Subject: [PATCH] Disable process monitoring when in debug with agentlib:jdwt --- .../org/sonar/process/MonitoredProcess.java | 9 ++++++-- .../main/java/org/sonar/process/Props.java | 9 ++++++++ .../java/org/sonar/process/DummyProcess.java | 2 +- .../sonar/process/MonitoredProcessTest.java | 21 +++++++++++++++++++ .../java/org/sonar/search/SearchServer.java | 13 ++++++++++-- 5 files changed, 49 insertions(+), 5 deletions(-) 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 23cb5dfe3a0..6d75bece303 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,9 +32,10 @@ public abstract class MonitoredProcess implements ProcessMXBean { private final static Logger LOGGER = LoggerFactory.getLogger(MonitoredProcess.class); - public static final String NAME_PROPERTY = "pName"; + private 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"; public static final String MISSING_NAME_ARGUMENT = "Missing Name argument"; private Long lastPing; @@ -50,7 +51,7 @@ public abstract class MonitoredProcess implements ProcessMXBean { private final boolean isMonitored; protected MonitoredProcess(Props props) { - this(props, false); + this(props, !props.containsValue(DEBUG_AGENT)); } protected MonitoredProcess(Props props, boolean monitor) { @@ -154,6 +155,10 @@ public abstract class MonitoredProcess implements ProcessMXBean { return terminated && monitor == null; } + public boolean isMonitored() { + return this.isMonitored; + } + @Override public final boolean isReady() { try { diff --git a/server/process/sonar-process/src/main/java/org/sonar/process/Props.java b/server/process/sonar-process/src/main/java/org/sonar/process/Props.java index d6e2795a597..f8112ea6423 100644 --- a/server/process/sonar-process/src/main/java/org/sonar/process/Props.java +++ b/server/process/sonar-process/src/main/java/org/sonar/process/Props.java @@ -40,6 +40,15 @@ public class Props { return properties.containsKey(key); } + public boolean containsValue(String value) { + for (Object propertyValue : properties.values()) { + if (propertyValue.toString().contains(value)) { + return true; + } + } + return false; + } + @CheckForNull public String of(String key) { String value = properties.getProperty(key); diff --git a/server/process/sonar-process/src/test/java/org/sonar/process/DummyProcess.java b/server/process/sonar-process/src/test/java/org/sonar/process/DummyProcess.java index 15d3ec7af09..f16c61fd93c 100644 --- a/server/process/sonar-process/src/test/java/org/sonar/process/DummyProcess.java +++ b/server/process/sonar-process/src/test/java/org/sonar/process/DummyProcess.java @@ -43,7 +43,7 @@ public class DummyProcess extends MonitoredProcess { } protected DummyProcess(Props props) throws Exception { - this(props, false); + super(props); } @Override 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 515d3c1ea3d..f3e4d2a78db 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 @@ -44,6 +44,27 @@ public class MonitoredProcessTest { assertThat(dummyProcess).isNotNull(); } + @Test + public void should_not_monitor_debug() 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"); + DummyProcess dummyProcess = new DummyProcess(new Props(properties)); + + assertThat(dummyProcess.isMonitored()).isFalse(); + } + + @Test + public void should_monitor_by_default() throws Exception { + Properties properties = new Properties(); + properties.setProperty(MonitoredProcess.NAME_PROPERTY, DummyProcess.NAME); + properties.setProperty("sonar.search.javaOpts", "hello world"); + DummyProcess dummyProcess = new DummyProcess(new Props(properties)); + + assertThat(dummyProcess.isMonitored()).isTrue(); + } + + @Test(timeout = 3000L) public void monitor_dies_when_no_pings() throws Exception { Properties properties = new Properties(); 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 500665221fc..aecfb401eda 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 @@ -21,6 +21,7 @@ package org.sonar.search; import org.apache.commons.lang.StringUtils; import org.elasticsearch.action.admin.cluster.health.ClusterHealthStatus; +import org.elasticsearch.common.annotations.VisibleForTesting; import org.elasticsearch.common.settings.ImmutableSettings; import org.elasticsearch.common.unit.TimeValue; import org.elasticsearch.node.Node; @@ -58,7 +59,8 @@ public class SearchServer extends MonitoredProcess { private Node node; - public SearchServer(final Props props, boolean monitored, boolean blocking) { + @VisibleForTesting + SearchServer(final Props props, boolean monitored, boolean blocking) { super(props, monitored); this.isBlocking = blocking; @@ -71,7 +73,14 @@ public class SearchServer extends MonitoredProcess { } public SearchServer(Props props) { - this(props, true, true); + super(props); + this.isBlocking = false; + new MinimumViableSystem().check(); + + String esNodesInets = props.of(ES_CLUSTER_INET); + if (StringUtils.isNotEmpty(esNodesInets)) { + Collections.addAll(nodes, esNodesInets.split(",")); + } } @Override -- 2.39.5