diff options
author | Duarte Meneses <duarte.meneses@sonarsource.com> | 2019-05-14 11:23:16 -0500 |
---|---|---|
committer | SonarTech <sonartech@sonarsource.com> | 2019-06-03 20:21:23 +0200 |
commit | bbe0a0802a3ead415b99155aa82cf41c0694d33d (patch) | |
tree | e9d548dfe487e9e2d576941c61b19dfb5ff5f891 /server/sonar-process/src/main | |
parent | a8877e19050e05ff7b30b44ca4a0ccd50d33b6af (diff) | |
download | sonarqube-bbe0a0802a3ead415b99155aa82cf41c0694d33d.tar.gz sonarqube-bbe0a0802a3ead415b99155aa82cf41c0694d33d.zip |
SONAR-12043 Hard stop if stop takes too long
Diffstat (limited to 'server/sonar-process/src/main')
3 files changed, 18 insertions, 10 deletions
diff --git a/server/sonar-process/src/main/java/org/sonar/process/AbstractStopperThread.java b/server/sonar-process/src/main/java/org/sonar/process/AbstractStopperThread.java index 13332656ca7..5451f64fd66 100644 --- a/server/sonar-process/src/main/java/org/sonar/process/AbstractStopperThread.java +++ b/server/sonar-process/src/main/java/org/sonar/process/AbstractStopperThread.java @@ -19,10 +19,13 @@ */ package org.sonar.process; +import com.google.common.util.concurrent.ThreadFactoryBuilder; +import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.Future; import java.util.concurrent.TimeUnit; +import java.util.concurrent.TimeoutException; import org.slf4j.LoggerFactory; /** @@ -32,7 +35,6 @@ abstract class AbstractStopperThread extends Thread { private final Runnable stopCode; private final long terminationTimeoutMs; - private boolean stop = false; AbstractStopperThread(String threadName, Runnable stopCode, long terminationTimeoutMs) { super(threadName); @@ -43,20 +45,23 @@ abstract class AbstractStopperThread extends Thread { @Override public void run() { - ExecutorService executor = Executors.newSingleThreadExecutor(); + ExecutorService executor = Executors.newSingleThreadExecutor( + new ThreadFactoryBuilder() + .setDaemon(false) + .setNameFormat(getName() + "-%d") + .build()); try { Future future = executor.submit(stopCode); future.get(terminationTimeoutMs, TimeUnit.MILLISECONDS); - } catch (Exception e) { - if (!stop) { - LoggerFactory.getLogger(getClass()).error("Can not stop in {}ms", terminationTimeoutMs, e); - } + } catch (TimeoutException | InterruptedException e) { + LoggerFactory.getLogger(getClass()).warn("Can not stop in {}ms", terminationTimeoutMs); + } catch (ExecutionException e) { + LoggerFactory.getLogger(getClass()).error("Can not stop in {}ms", terminationTimeoutMs, e); } executor.shutdownNow(); } public void stopIt() { - this.stop = true; super.interrupt(); } } diff --git a/server/sonar-process/src/main/java/org/sonar/process/ProcessEntryPoint.java b/server/sonar-process/src/main/java/org/sonar/process/ProcessEntryPoint.java index 308dbeccb8a..ddb40c97ad1 100644 --- a/server/sonar-process/src/main/java/org/sonar/process/ProcessEntryPoint.java +++ b/server/sonar-process/src/main/java/org/sonar/process/ProcessEntryPoint.java @@ -20,7 +20,6 @@ package org.sonar.process; import java.io.File; -import java.util.concurrent.CountDownLatch; import java.util.function.Predicate; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -82,8 +81,6 @@ public class ProcessEntryPoint { launch(logger); } catch (Exception e) { logger.warn("Fail to start {}", processKey, e); - } finally { - logger.trace("Hard stopping to clean any resource..."); hardStop(); } } @@ -163,6 +160,8 @@ public class ProcessEntryPoint { private void terminate() { lifecycle.tryToMoveTo(STOPPED); + hardStopWatcher.stopWatching(); + stopWatcher.stopWatching(); commands.endWatch(); } diff --git a/server/sonar-process/src/main/java/org/sonar/process/StopWatcher.java b/server/sonar-process/src/main/java/org/sonar/process/StopWatcher.java index 3e1af3b7b25..bb5a9365706 100644 --- a/server/sonar-process/src/main/java/org/sonar/process/StopWatcher.java +++ b/server/sonar-process/src/main/java/org/sonar/process/StopWatcher.java @@ -21,8 +21,11 @@ package org.sonar.process; import com.google.common.annotations.VisibleForTesting; import java.util.function.BooleanSupplier; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; public class StopWatcher extends Thread { + private static final Logger LOG = LoggerFactory.getLogger(StopWatcher.class); private final Runnable stopCommand; private final BooleanSupplier shouldStopTest; private final long delayMs; @@ -44,6 +47,7 @@ public class StopWatcher extends Thread { public void run() { while (watching) { if (shouldStopTest.getAsBoolean()) { + LOG.trace("{} triggering stop command", this.getName()); stopCommand.run(); watching = false; } else { |