]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-9715 improve documentation of process management
authorSimon Brandhof <simon.brandhof@sonarsource.com>
Wed, 30 Aug 2017 14:30:36 +0000 (16:30 +0200)
committerSimon Brandhof <simon.brandhof@sonarsource.com>
Tue, 5 Sep 2017 12:24:13 +0000 (14:24 +0200)
server/sonar-ce/src/main/java/org/sonar/ce/app/CeServer.java
server/sonar-ce/src/main/java/org/sonar/ce/container/ComputeEngineStatus.java
server/sonar-main/src/main/java/org/sonar/application/process/ProcessLauncherImpl.java
server/sonar-process/src/main/java/org/sonar/process/Monitored.java
server/sonar-process/src/main/java/org/sonar/process/ProcessEntryPoint.java
server/sonar-process/src/main/java/org/sonar/process/StopperThread.java
server/sonar-process/src/test/java/org/sonar/process/ProcessEntryPointTest.java

index 82b9f1ecc44b7b68b5b6aba407cceb6cdcedde2b..129c65ef502bf8a191a00a9d35d52fe212b96590 100644 (file)
@@ -22,7 +22,7 @@ package org.sonar.ce.app;
 import com.google.common.annotations.VisibleForTesting;
 import com.google.common.collect.ImmutableMap;
 import java.util.concurrent.atomic.AtomicReference;
-import javax.annotation.CheckForNull;
+import javax.annotation.Nullable;
 import org.sonar.api.utils.log.Logger;
 import org.sonar.api.utils.log.Loggers;
 import org.sonar.ce.ComputeEngine;
@@ -56,7 +56,7 @@ public class CeServer implements Monitored {
   private volatile boolean stopAwait = false;
 
   private final ComputeEngine computeEngine;
-  @CheckForNull
+  @Nullable
   private CeMainThread ceMainThread = null;
 
   @VisibleForTesting
@@ -172,6 +172,7 @@ public class CeServer implements Monitored {
           Thread.sleep(CHECK_FOR_STOP_DELAY);
         } catch (InterruptedException e) {
           // ignore the interruption itself, check the flag
+          Thread.currentThread().interrupt();
         }
       }
       attemptShutdown();
@@ -179,7 +180,8 @@ public class CeServer implements Monitored {
 
     private void attemptShutdown() {
       try {
-        shutdown();
+        LOG.info("Compute Engine shutting down...");
+        computeEngine.shutdown();
       } catch (Throwable e) {
         LOG.error("Compute Engine shutdown failed", e);
       } finally {
@@ -188,11 +190,6 @@ public class CeServer implements Monitored {
       }
     }
 
-    private void shutdown() {
-      LOG.info("Compute Engine shutting down...");
-      computeEngine.shutdown();
-    }
-
     public boolean isStarted() {
       return started;
     }
index 600128e9bf673aa6995f506c515a075084d57dfd..b19b2fad147c63258213d5abf25328d907efbc5f 100644 (file)
@@ -27,6 +27,6 @@ public interface ComputeEngineStatus {
   Status getStatus();
 
   enum Status {
-    INIT, STARTING, STARTED, STOPPING, STOPPED;
+    INIT, STARTING, STARTED, STOPPING, STOPPED
   }
 }
index 3c2816474a72727d70720bc40c0c62126b7091da..af17e5ff80b2d71352d940ba5bb72f5ea4ae8c90 100644 (file)
@@ -44,7 +44,7 @@ import static java.lang.String.format;
 import static org.sonar.process.ProcessEntryPoint.PROPERTY_PROCESS_INDEX;
 import static org.sonar.process.ProcessEntryPoint.PROPERTY_PROCESS_KEY;
 import static org.sonar.process.ProcessEntryPoint.PROPERTY_SHARED_PATH;
-import static org.sonar.process.ProcessEntryPoint.PROPERTY_TERMINATION_TIMEOUT;
+import static org.sonar.process.ProcessEntryPoint.PROPERTY_TERMINATION_TIMEOUT_MS;
 
 public class ProcessLauncherImpl implements ProcessLauncher {
   private static final Logger LOG = LoggerFactory.getLogger(ProcessLauncherImpl.class);
@@ -182,7 +182,7 @@ public class ProcessLauncherImpl implements ProcessLauncher {
       props.putAll(javaCommand.getArguments());
       props.setProperty(PROPERTY_PROCESS_KEY, javaCommand.getProcessId().getKey());
       props.setProperty(PROPERTY_PROCESS_INDEX, Integer.toString(javaCommand.getProcessId().getIpcIndex()));
-      props.setProperty(PROPERTY_TERMINATION_TIMEOUT, "60000");
+      props.setProperty(PROPERTY_TERMINATION_TIMEOUT_MS, "60000");
       props.setProperty(PROPERTY_SHARED_PATH, tempDir.getAbsolutePath());
       try (OutputStream out = new FileOutputStream(propertiesFile)) {
         props.store(out, format("Temporary properties file for command [%s]", javaCommand.getProcessId().getKey()));
index b37d929e54dc6d6010fa6922556dd57f3067793a..2fa65b1c5713c5e29f62ea6a0e9ffd2ef4f3ddd3 100644 (file)
  */
 package org.sonar.process;
 
+/**
+ * Base interface for the processes started by the bootstrap process.
+ * It provides the information and operations required by {@link ProcessEntryPoint}
+ * to handle the lifecycle of the process.
+ */
 public interface Monitored {
 
   /**
@@ -37,9 +42,12 @@ public interface Monitored {
   }
 
   /**
-   * Blocks until the process is terminated
+   * Blocks until the process is stopped
    */
   void awaitStop();
 
+  /**
+   * Stop process and wait until it's stopped.
+   */
   void stop();
 }
index b1476fba53e68f2ce58916fbbf27b93ea00c0f6d..76b89a007f9df6b3b1017c233c5e0f1e9a86444b 100644 (file)
@@ -29,7 +29,7 @@ public class ProcessEntryPoint implements Stoppable {
 
   public static final String PROPERTY_PROCESS_KEY = "process.key";
   public static final String PROPERTY_PROCESS_INDEX = "process.index";
-  public static final String PROPERTY_TERMINATION_TIMEOUT = "process.terminationTimeout";
+  public static final String PROPERTY_TERMINATION_TIMEOUT_MS = "process.terminationTimeout";
   public static final String PROPERTY_SHARED_PATH = "process.sharedDir";
 
   private final Props props;
@@ -171,7 +171,7 @@ public class ProcessEntryPoint implements Stoppable {
   @Override
   public void stopAsync() {
     if (lifecycle.tryToMoveTo(Lifecycle.State.STOPPING)) {
-      stopperThread = new StopperThread(monitored, commands, Long.parseLong(props.nonNullValue(PROPERTY_TERMINATION_TIMEOUT)));
+      stopperThread = new StopperThread(monitored, commands, Long.parseLong(props.nonNullValue(PROPERTY_TERMINATION_TIMEOUT_MS)));
       stopperThread.start();
       stopWatcher.stopWatching();
     }
index 746cd31dc1e85c9c7b6c47bd3da5c149c4be345d..14fe192cd9cbe15d2a14bee3db0c463f5075b925 100644 (file)
@@ -27,35 +27,32 @@ import java.util.concurrent.Future;
 import java.util.concurrent.TimeUnit;
 import org.sonar.process.sharedmemoryfile.ProcessCommands;
 
+import static java.lang.String.format;
+
 /**
  * Gracefully stops process in a timely fashion
  */
 class StopperThread extends Thread {
 
   private final Monitored monitored;
-  private final long terminationTimeout;
+  private final long terminationTimeoutMs;
   private final ProcessCommands commands;
 
-  StopperThread(Monitored monitored, ProcessCommands commands, long terminationTimeout) {
+  StopperThread(Monitored monitored, ProcessCommands commands, long terminationTimeoutMs) {
     super("Stopper");
     this.monitored = monitored;
-    this.terminationTimeout = terminationTimeout;
+    this.terminationTimeoutMs = terminationTimeoutMs;
     this.commands = commands;
   }
 
   @Override
   public void run() {
     ExecutorService executor = Executors.newSingleThreadExecutor();
-    Future future = executor.submit(new Runnable() {
-      @Override
-      public void run() {
-        monitored.stop();
-      }
-    });
     try {
-      future.get(terminationTimeout, TimeUnit.MILLISECONDS);
+      Future future = executor.submit(monitored::stop);
+      future.get(terminationTimeoutMs, TimeUnit.MILLISECONDS);
     } catch (Exception e) {
-      LoggerFactory.getLogger(getClass()).error(String.format("Can not stop in %dms", terminationTimeout), e);
+      LoggerFactory.getLogger(getClass()).error(format("Can not stop in %dms", terminationTimeoutMs), e);
     }
     executor.shutdownNow();
     commands.endWatch();
index 3b7a4659d3148215b5f3e2339a130e3a3899cca7..ccffe816e587489ed93afe3e330a53f5c9cb4254 100644 (file)
@@ -40,7 +40,7 @@ import static org.mockito.Mockito.mock;
 import static org.sonar.process.ProcessEntryPoint.PROPERTY_PROCESS_INDEX;
 import static org.sonar.process.ProcessEntryPoint.PROPERTY_PROCESS_KEY;
 import static org.sonar.process.ProcessEntryPoint.PROPERTY_SHARED_PATH;
-import static org.sonar.process.ProcessEntryPoint.PROPERTY_TERMINATION_TIMEOUT;
+import static org.sonar.process.ProcessEntryPoint.PROPERTY_TERMINATION_TIMEOUT_MS;
 
 public class ProcessEntryPointTest {
 
@@ -159,7 +159,7 @@ public class ProcessEntryPointTest {
     props.set(PROPERTY_SHARED_PATH, temp.newFolder().getAbsolutePath());
     props.set(PROPERTY_PROCESS_INDEX, "1");
     props.set(PROPERTY_PROCESS_KEY, "test");
-    props.set(PROPERTY_TERMINATION_TIMEOUT, "30000");
+    props.set(PROPERTY_TERMINATION_TIMEOUT_MS, "30000");
     return props;
   }