Browse Source

SONAR-12043 Minor refactor

tags/7.8
Duarte Meneses 5 years ago
parent
commit
c3f0b65a4d

+ 1
- 1
server/sonar-main/src/main/java/org/sonar/application/AbstractStopRequestWatcher.java View File

@@ -55,7 +55,7 @@ public abstract class AbstractStopRequestWatcher extends Thread implements StopR
Thread.sleep(delayMs);
}
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
interrupt();
// stop watching the commands
}
}

+ 16
- 42
server/sonar-process/src/main/java/org/sonar/process/ProcessEntryPoint.java View File

@@ -48,32 +48,27 @@ public class ProcessEntryPoint {
private final ProcessCommands commands;
private final SystemExit exit;
private final StopWatcher stopWatcher;
private final HardStopWatcher hardStopWatcher;
private final StopWatcher hardStopWatcher;
// new Runnable() is important to avoid conflict of call to ProcessEntryPoint#stop() with Thread#stop()
private final Thread shutdownHook = new Thread(new Runnable() {
@Override
public void run() {
exit.setInShutdownHook();
stop();
}
});
private final Runtime runtime;
private volatile Monitored monitored;
private volatile StopperThread stopperThread;
private volatile HardStopperThread hardStopperThread;

ProcessEntryPoint(Props props, SystemExit exit, ProcessCommands commands) {
this(props, getProcessNumber(props), getSharedDir(props), exit, commands);
this(props, getProcessNumber(props), getSharedDir(props), exit, commands, Runtime.getRuntime());
}

private ProcessEntryPoint(Props props, int processNumber, File sharedDir, SystemExit exit, ProcessCommands commands) {
private ProcessEntryPoint(Props props, int processNumber, File sharedDir, SystemExit exit, ProcessCommands commands, Runtime runtime) {
this.props = props;
this.processKey = props.nonNullValue(PROPERTY_PROCESS_KEY);
this.processNumber = processNumber;
this.sharedDir = sharedDir;
this.exit = exit;
this.commands = commands;
this.stopWatcher = new StopWatcher(commands, this);
this.hardStopWatcher = new HardStopWatcher(commands, this);
this.stopWatcher = createStopWatcher(commands, this);
this.hardStopWatcher = createHardStopWatcher(commands, this);
this.runtime = runtime;
}

public ProcessCommands getCommands() {
@@ -118,7 +113,10 @@ public class ProcessEntryPoint {

private void launch(Logger logger) throws InterruptedException {
logger.info("Starting {}", getKey());
Runtime.getRuntime().addShutdownHook(shutdownHook);
runtime.addShutdownHook(new Thread(() -> {
exit.setInShutdownHook();
stop();
}));
stopWatcher.start();
hardStopWatcher.start();

@@ -225,16 +223,12 @@ public class ProcessEntryPoint {
return lifecycle.isCurrentState(candidateState);
}

Thread getShutdownHook() {
return shutdownHook;
}

public static ProcessEntryPoint createForArguments(String[] args) {
Props props = ConfigurationUtils.loadPropsFromCommandLineArgs(args);
File sharedDir = getSharedDir(props);
int processNumber = getProcessNumber(props);
ProcessCommands commands = DefaultProcessCommands.main(sharedDir, processNumber);
return new ProcessEntryPoint(props, processNumber, sharedDir, new SystemExit(), commands);
return new ProcessEntryPoint(props, processNumber, sharedDir, new SystemExit(), commands, Runtime.getRuntime());
}

private static int getProcessNumber(Props props) {
@@ -248,35 +242,15 @@ public class ProcessEntryPoint {
/**
* This watchdog is looking for hard stop to be requested via {@link ProcessCommands#askedForHardStop()}.
*/
private static class HardStopWatcher extends AbstractStopWatcher {

private HardStopWatcher(ProcessCommands commands, ProcessEntryPoint processEntryPoint) {
super(
"HardStop Watcher",
() -> {
LoggerFactory.getLogger(HardStopWatcher.class).info("Hard stopping process");
processEntryPoint.hardStopAsync();
},
commands::askedForHardStop);
}

private static StopWatcher createHardStopWatcher(ProcessCommands commands, ProcessEntryPoint processEntryPoint) {
return new StopWatcher("HardStop Watcher", processEntryPoint::hardStopAsync, commands::askedForHardStop);
}

/**
* This watchdog is looking for graceful stop to be requested via {@link ProcessCommands#askedForStop()} ()}.
*/
private static class StopWatcher extends AbstractStopWatcher {

private StopWatcher(ProcessCommands commands, ProcessEntryPoint processEntryPoint) {
super(
"Stop Watcher",
() -> {
LoggerFactory.getLogger(StopWatcher.class).info("Stopping process");
processEntryPoint.stopAsync();
},
commands::askedForStop);
}

private static StopWatcher createStopWatcher(ProcessCommands commands, ProcessEntryPoint processEntryPoint) {
return new StopWatcher("Stop Watcher", processEntryPoint::stopAsync, commands::askedForStop);
}

/**

server/sonar-process/src/main/java/org/sonar/process/AbstractStopWatcher.java → server/sonar-process/src/main/java/org/sonar/process/StopWatcher.java View File

@@ -22,18 +22,18 @@ package org.sonar.process;
import com.google.common.annotations.VisibleForTesting;
import java.util.function.BooleanSupplier;

abstract class AbstractStopWatcher extends Thread {
public class StopWatcher extends Thread {
private final Runnable stopCommand;
private final BooleanSupplier shouldStopTest;
private final long delayMs;
private volatile boolean watching = true;

public AbstractStopWatcher(String threadName, Runnable stopCommand, BooleanSupplier shouldStopTest) {
public StopWatcher(String threadName, Runnable stopCommand, BooleanSupplier shouldStopTest) {
this(threadName, stopCommand, shouldStopTest, 500L);
}

@VisibleForTesting
AbstractStopWatcher(String threadName, Runnable stopCommand, BooleanSupplier shouldStopTest, long delayMs) {
StopWatcher(String threadName, Runnable stopCommand, BooleanSupplier shouldStopTest, long delayMs) {
super(threadName);
this.stopCommand = stopCommand;
this.shouldStopTest = shouldStopTest;

server/sonar-process/src/test/java/org/sonar/process/AbstractStopWatcherTest.java → server/sonar-process/src/test/java/org/sonar/process/StopWatcherTest.java View File

@@ -32,7 +32,7 @@ import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

public class AbstractStopWatcherTest {
public class StopWatcherTest {

@Rule
public TestRule safeguardTimeout = new DisableOnDebug(Timeout.seconds(60));
@@ -44,8 +44,7 @@ public class AbstractStopWatcherTest {
when(commands.askedForHardStop()).thenReturn(false, true);
Stoppable stoppable = mock(Stoppable.class);

AbstractStopWatcher underTest = new AbstractStopWatcher("TheThreadName",
stoppable::hardStopAsync, commands::askedForHardStop, 1L) {};
StopWatcher underTest = new StopWatcher("TheThreadName", stoppable::hardStopAsync, commands::askedForHardStop, 1L);
underTest.start();

while (underTest.isAlive()) {
@@ -61,8 +60,7 @@ public class AbstractStopWatcherTest {
when(commands.askedForHardStop()).thenReturn(false);
Stoppable stoppable = mock(Stoppable.class);

AbstractStopWatcher underTest = new AbstractStopWatcher("TheThreadName",
stoppable::hardStopAsync, commands::askedForHardStop, 1L) {};
StopWatcher underTest = new StopWatcher("TheThreadName", stoppable::hardStopAsync, commands::askedForHardStop, 1L);
underTest.start();
underTest.interrupt();


+ 2
- 4
server/sonar-process/src/test/java/org/sonar/process/test/StandardProcess.java View File

@@ -36,11 +36,9 @@ public class StandardProcess implements Monitored {
@Override
public void run() {
try {
while (true) {
Thread.sleep(100L);
}
Thread.sleep(Long.MAX_VALUE);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
interrupt();
}
}
};

Loading…
Cancel
Save