From 9dda0bbeab053cc4a09f7d54176d074179fdf9e4 Mon Sep 17 00:00:00 2001 From: Simon Brandhof Date: Thu, 28 Aug 2014 16:02:35 +0200 Subject: [PATCH] SONAR-4898 try to fix ping monitoring Sometimes child autokills because it did not receive pings in the last 30 seconds. I hope it comes from the parent process that is slow to start monitoring (send pings). --- .../main/java/org/sonar/process/Monitor.java | 35 ++++++++----------- .../org/sonar/process/MonitoredProcess.java | 2 +- .../org/sonar/process/MonitorTestBase.java | 2 +- .../main/java/org/sonar/application/App.java | 4 +-- 4 files changed, 19 insertions(+), 24 deletions(-) diff --git a/server/process/sonar-process/src/main/java/org/sonar/process/Monitor.java b/server/process/sonar-process/src/main/java/org/sonar/process/Monitor.java index 1fe64d65ba0..0068992b3f0 100644 --- a/server/process/sonar-process/src/main/java/org/sonar/process/Monitor.java +++ b/server/process/sonar-process/src/main/java/org/sonar/process/Monitor.java @@ -22,8 +22,8 @@ package org.sonar.process; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import java.util.ArrayList; import java.util.List; +import java.util.concurrent.CopyOnWriteArrayList; import java.util.concurrent.Executors; import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.ScheduledFuture; @@ -36,7 +36,7 @@ public class Monitor extends Thread implements Terminable { private static final long PING_DELAY_MS = 3000L; private long pingDelayMs = PING_DELAY_MS; - private volatile List processes; + private final List processes = new CopyOnWriteArrayList(); private final ScheduledFuture watch; private final ScheduledExecutorService monitorExecutionService; @@ -45,7 +45,6 @@ public class Monitor extends Thread implements Terminable { */ public Monitor() { super("Process Monitor"); - processes = new ArrayList(); monitorExecutionService = Executors.newScheduledThreadPool(1); watch = monitorExecutionService.scheduleAtFixedRate(new ProcessWatch(), 0L, getPingDelayMs(), TimeUnit.MILLISECONDS); } @@ -84,13 +83,11 @@ public class Monitor extends Thread implements Terminable { /** * Registers and monitors process. Note that process is probably not ready yet. */ - public void registerProcess(ProcessWrapper process) throws InterruptedException { - LOGGER.info("Registering process[{}] for monitoring.", process.getName()); - synchronized (processes) { - processes.add(process); - } + public void monitor(ProcessWrapper process) throws InterruptedException { + LOGGER.info("Monitoring process[{}]", process.getName()); // starts a monitoring thread process.start(); + processes.add(process); } /** @@ -102,19 +99,17 @@ public class Monitor extends Thread implements Terminable { try { boolean ok = true; while (isRunning && ok) { - synchronized (processes) { - LOGGER.debug("Monitoring {} processes.", processes.size()); - for (ProcessWrapper process : processes) { - if (!ProcessUtils.isAlive(process.process())) { - LOGGER.info("{} is down, stopping all other processes", process.getName()); - ok = false; - interrupt(); - } + LOGGER.debug("Monitoring {} processes.", processes.size()); + for (ProcessWrapper process : processes) { + if (!ProcessUtils.isAlive(process.process())) { + LOGGER.info("{} is down, stopping all other processes", process.getName()); + ok = false; + interrupt(); } } - if (ok) { - Thread.sleep(PING_DELAY_MS); - } + } + if (ok) { + Thread.sleep(PING_DELAY_MS); } } catch (InterruptedException e) { LOGGER.debug("Monitoring thread is interrupted"); @@ -150,7 +145,7 @@ public class Monitor extends Thread implements Terminable { this.join(); } } catch (InterruptedException e) { - //Expected to be interrupted :) + // Expected to be interrupted :) } } } 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 c36df449b9e..f6a69834710 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 @@ -31,7 +31,7 @@ public abstract class MonitoredProcess implements ProcessMXBean { private static final Logger LOGGER = LoggerFactory.getLogger(MonitoredProcess.class); - private static final long AUTOKILL_TIMEOUT_MS = 30000L; + private static final long AUTOKILL_TIMEOUT_MS = 60000L; 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/MonitorTestBase.java b/server/process/sonar-process/src/test/java/org/sonar/process/MonitorTestBase.java index 0980251cdc7..fb9c818d017 100644 --- a/server/process/sonar-process/src/test/java/org/sonar/process/MonitorTestBase.java +++ b/server/process/sonar-process/src/test/java/org/sonar/process/MonitorTestBase.java @@ -68,7 +68,7 @@ public class MonitorTestBase extends BaseProcessTest { // 1 start my monitor & register process monitor.start(); - monitor.registerProcess(process); + monitor.monitor(process); // 2 terminate monitor, assert process is terminated monitor.terminate(); 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 784d75ef299..23293a2efed 100644 --- a/sonar-application/src/main/java/org/sonar/application/App.java +++ b/sonar-application/src/main/java/org/sonar/application/App.java @@ -70,7 +70,7 @@ public class App implements ProcessMXBean { .addClasspath("./lib/common/*") .addClasspath("./lib/search/*"); if (elasticsearch.execute()) { - monitor.registerProcess(elasticsearch); + monitor.monitor(elasticsearch); if (elasticsearch.waitForReady()) { logger.info("search server is up"); @@ -92,7 +92,7 @@ public class App implements ProcessMXBean { server.addClasspath(driverPath); } if (server.execute()) { - monitor.registerProcess(server); + monitor.monitor(server); if (server.waitForReady()) { success = true; logger.info("web server is up"); -- 2.39.5