]> source.dussan.org Git - sonarqube.git/commitdiff
improve logs to help debug SchedulerImpl and ManagedProcessHandler
authorSébastien Lesaint <sebastien.lesaint@sonarsource.com>
Thu, 5 Sep 2019 07:37:37 +0000 (09:37 +0200)
committerSonarTech <sonartech@sonarsource.com>
Fri, 6 Sep 2019 18:21:05 +0000 (20:21 +0200)
display Thread name more often
display call stack at trace level

server/sonar-main/src/main/java/org/sonar/application/NodeLifecycle.java
server/sonar-main/src/main/java/org/sonar/application/SchedulerImpl.java
server/sonar-main/src/main/java/org/sonar/application/process/ManagedProcessHandler.java
server/sonar-main/src/main/java/org/sonar/application/process/ManagedProcessLifecycle.java

index fe9830a8dd6b71f638327a1cd64464a71659a5de..20aa56b378282bcf7a20484d5d7055d32f64859b 100644 (file)
@@ -103,7 +103,7 @@ class NodeLifecycle {
       this.state = to;
       res = true;
     }
-    LOG.trace("tryToMoveTo from {} to {} => {}", currentState, to, res);
+    LOG.debug("{} tryToMoveTo from {} to {} => {}", Thread.currentThread().getName(), currentState, to, res);
     return res;
   }
 }
index 4e1b9377bc500b6df988a16e5ef047646c0ca078..93a258a0a9abce2d6995a992d8d9bfa7075864ee 100644 (file)
@@ -284,10 +284,14 @@ public class SchedulerImpl implements Scheduler, ManagedProcessEventListener, Pr
   }
 
   private static void interrupt(@Nullable Thread thread) {
-    if (thread != null
-      // do not interrupt oneself
-      && Thread.currentThread() != thread) {
+    Thread currentThread = Thread.currentThread();
+    // prevent current thread from interrupting itself
+    if (thread != null && currentThread != thread) {
       thread.interrupt();
+      if (LOG.isTraceEnabled()) {
+        Exception e = new Exception("(capturing stacktrace for debugging purpose)");
+        LOG.trace("{} interrupted {}", currentThread.getName(), thread.getName(), e);
+      }
     }
   }
 
@@ -385,7 +389,7 @@ public class SchedulerImpl implements Scheduler, ManagedProcessEventListener, Pr
 
   private void hardStopAsync() {
     if (hardStopperThread != null) {
-      LOG.debug("Hard stopper thread was not null (name is \"{}\")", hardStopperThread.getName(), new Exception());
+      logThreadRecreated("Hard stopper", hardStopperThread);
       hardStopperThread.interrupt();
     }
 
@@ -395,7 +399,7 @@ public class SchedulerImpl implements Scheduler, ManagedProcessEventListener, Pr
 
   private void stopAsyncForRestart() {
     if (restartStopperThread != null) {
-      LOG.debug("Restart stopper thread was not null", new Exception());
+      logThreadRecreated("Restart stopper", restartStopperThread);
       restartStopperThread.interrupt();
     }
 
@@ -403,6 +407,14 @@ public class SchedulerImpl implements Scheduler, ManagedProcessEventListener, Pr
     restartStopperThread.start();
   }
 
+  private static void logThreadRecreated(String threadType, Thread existingThread) {
+    if (LOG.isDebugEnabled()) {
+      Exception e = new Exception("(capturing stack trace for debugging purpose)");
+      LOG.debug("{} thread was not null (currentThread={},existingThread={})",
+        threadType, Thread.currentThread().getName(), existingThread.getName(), e);
+    }
+  }
+
   private void restartAsync() {
     if (restarterThread != null) {
       LOG.debug("Restarter thread was not null (name is \"{}\")", restarterThread.getName(), new Exception());
index d9b6835418d235299d2d946d08dbf48340993fc8..234239e5bec55ee56fad75e46ec40b8c99e7d422 100644 (file)
@@ -24,6 +24,7 @@ import java.util.List;
 import java.util.Objects;
 import java.util.concurrent.TimeUnit;
 import java.util.function.Supplier;
+import javax.annotation.Nullable;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.sonar.process.ProcessId;
@@ -164,7 +165,8 @@ public class ManagedProcessHandler {
       process.waitFor(hardStopTimeout.getDuration(), hardStopTimeout.getUnit());
     } catch (InterruptedException e) {
       // can't wait for the termination of process. Let's assume it's down.
-      throw rethrowWithWarn(e, format("Interrupted while hard stopping process %s", processId));
+      throw rethrowWithWarn(e,
+        format("Interrupted while hard stopping process %s (currentThread=%s)", processId, Thread.currentThread().getName()));
     } catch (Throwable e) {
       LOG.error("Failed while asking for hard stop of process {}", processId, e);
     }
@@ -177,13 +179,8 @@ public class ManagedProcessHandler {
   }
 
   public void stopForcibly() {
-    Thread currentThread = Thread.currentThread();
-    if (currentThread != eventWatcher) {
-      eventWatcher.interrupt();
-    }
-    if (currentThread != stopWatcher) {
-      stopWatcher.interrupt();
-    }
+    interrupt(eventWatcher);
+    interrupt(stopWatcher);
     if (process != null) {
       process.destroyForcibly();
       waitForDown();
@@ -201,6 +198,18 @@ public class ManagedProcessHandler {
     lifecycle.tryToMoveTo(ManagedProcessLifecycle.State.STOPPED);
   }
 
+  private static void interrupt(@Nullable Thread thread) {
+    Thread currentThread = Thread.currentThread();
+    // prevent current thread from interrupting itself
+    if (thread != null && currentThread != thread) {
+      thread.interrupt();
+      if (LOG.isTraceEnabled()) {
+        Exception e = new Exception("(capturing stack trace for debugging purpose)");
+        LOG.trace("{} interrupted {}", currentThread.getName(), thread.getName(), e);
+      }
+    }
+  }
+
   void refreshState() {
     if (process.isAlive()) {
       if (!operational && process.isOperational()) {
index 9c4ea29a9a0278cb38aaf0af2dc31db23fc875dd..ef4f6d07d0cf6af3f80bf9fc4ad094c0b92aaf4c 100644 (file)
@@ -93,7 +93,7 @@ public class ManagedProcessLifecycle {
       res = true;
       listeners.forEach(listener -> listener.onProcessState(processId, to));
     }
-    LOG.trace("tryToMoveTo {} from {} to {} => {}", processId.getKey(), currentState, to, res);
+    LOG.debug("{} tryToMoveTo {} from {} to {} => {}", Thread.currentThread().getName(), processId.getKey(), currentState, to, res);
     return res;
   }
 }