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;
}
}
}
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);
+ }
}
}
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();
}
private void stopAsyncForRestart() {
if (restartStopperThread != null) {
- LOG.debug("Restart stopper thread was not null", new Exception());
+ logThreadRecreated("Restart stopper", restartStopperThread);
restartStopperThread.interrupt();
}
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());
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;
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);
}
}
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();
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()) {
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;
}
}