Reimplement restarting of scm-manager

SCM-Manager tries now to figure out which is the best strategy for the restart.
It chooses from one of the following strategies:

* PosixRestartStrategy which uses native LibC
* ExitRestartStrategy uses System.exit and relies on external mechanism to start again
* InjectionContextRestartStrategy destroys and re initializes the injection context
This commit is contained in:
Sebastian Sdorra
2020-02-12 12:13:10 +01:00
parent e30796b995
commit 56b8dbdb22
11 changed files with 412 additions and 8 deletions

View File

@@ -295,6 +295,20 @@
<version>2.7.0</version>
</dependency>
<!-- unix restart -->
<dependency>
<groupId>org.kohsuke</groupId>
<artifactId>akuma</artifactId>
<version>1.10</version>
</dependency>
<dependency>
<groupId>net.java.dev.jna</groupId>
<artifactId>jna</artifactId>
<version>5.5.0</version>
</dependency>
<!-- test scope -->
<dependency>

View File

@@ -42,6 +42,7 @@ import sonia.scm.event.ScmEventBus;
import javax.servlet.FilterConfig;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletException;
import java.util.Optional;
//~--- JDK imports ------------------------------------------------------------
@@ -100,8 +101,12 @@ public class BootstrapContextFilter extends GuiceFilter {
if (filterConfig == null) {
LOG.error("filter config is null, scm-manager is not initialized");
} else {
RestartStrategy restartStrategy = RestartStrategy.get(webAppClassLoader);
restartStrategy.restart(new GuiceInjectionContext());
Optional<RestartStrategy> restartStrategy = RestartStrategy.get(webAppClassLoader);
if (restartStrategy.isPresent()) {
restartStrategy.get().restart(new GuiceInjectionContext());
} else {
LOG.warn("restarting is not supported by the underlying platform");
}
}
}

View File

@@ -0,0 +1,22 @@
package sonia.scm.lifecycle;
/**
* Interface for native c library.
*/
@SuppressWarnings({
"squid:S1214", // usage as constant is common practice for jna
"squid:S1191" // use of sun.* classes is required for jna
})
interface CLibrary extends com.sun.jna.Library {
CLibrary LIBC = com.sun.jna.Native.load("c", CLibrary.class);
int F_GETFD = 1;
int F_SETFD = 2;
int FD_CLOEXEC = 1;
int getdtablesize();
int fcntl(int fd, int command);
int fcntl(int fd, int command, int flags);
int execvp(String file, com.sun.jna.StringArray args);
String strerror(int errno);
}

View File

@@ -0,0 +1,52 @@
package sonia.scm.lifecycle;
import com.google.common.annotations.VisibleForTesting;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.function.IntConsumer;
/**
* {@link RestartStrategy} which tears down the scm-manager context and
* then exists the java process with {@link System#exit(int)}.
* <p>
* This is useful if an external mechanism is able to restart the process after it has exited.
*/
class ExitRestartStrategy implements RestartStrategy {
private static final Logger LOG = LoggerFactory.getLogger(ExitRestartStrategy.class);
static final String NAME = "exit";
static final String PROPERTY_EXIT_CODE = "sonia.scm.restart.exit-code";
private IntConsumer exiter = System::exit;
ExitRestartStrategy() {
}
@VisibleForTesting
void setExiter(IntConsumer exiter) {
this.exiter = exiter;
}
@Override
public void restart(InjectionContext context) {
int exitCode = determineExitCode();
LOG.warn("destroy injection context");
context.destroy();
LOG.warn("exit scm-manager with exit code {}", exitCode);
exiter.accept(exitCode);
}
private int determineExitCode() {
String exitCodeAsString = System.getProperty(PROPERTY_EXIT_CODE, "0");
try {
return Integer.parseInt(exitCodeAsString);
} catch (NumberFormatException ex) {
throw new RestartNotSupportedException("invalid exit code " + exitCodeAsString, ex);
}
}
}

View File

@@ -10,9 +10,11 @@ import sonia.scm.event.ShutdownEventBusEvent;
import java.util.concurrent.atomic.AtomicLong;
/**
* Restart strategy implementation which destroy the injection context and re initialize it.
* Restart strategy which tries to free, every resource used by the context, starts gc and re initializes the context.
*/
public class InjectionContextRestartStrategy implements RestartStrategy {
class InjectionContextRestartStrategy implements RestartStrategy {
static final String NAME = "context";
private static final String DISABLE_RESTART_PROPERTY = "sonia.scm.restart.disable";
private static final String WAIT_PROPERTY = "sonia.scm.restart.wait";

View File

@@ -0,0 +1,52 @@
package sonia.scm.lifecycle;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
import static sonia.scm.lifecycle.CLibrary.*;
/**
* Restart strategy which uses execvp from libc. This strategy is only supported on posix base operating systems.
*/
class PosixRestartStrategy implements RestartStrategy {
private static final Logger LOG = LoggerFactory.getLogger(PosixRestartStrategy.class);
PosixRestartStrategy() {
}
@Override
public void restart(InjectionContext context) {
LOG.warn("destroy injection context");
context.destroy();
LOG.warn("restart scm-manager jvm process");
try {
restart();
} catch (IOException e) {
LOG.error("failed to collect java vm arguments", e);
LOG.error("we will now exit the java process");
System.exit(1);
}
}
@SuppressWarnings("squid:S1191") // use of sun.* classes is required for jna)
private static void restart() throws IOException {
com.sun.akuma.JavaVMArguments args = com.sun.akuma.JavaVMArguments.current();
args.remove("--daemon");
int sz = LIBC.getdtablesize();
for(int i=3; i<sz; i++) {
int flags = LIBC.fcntl(i, F_GETFD);
if(flags<0) continue;
LIBC.fcntl(i, F_SETFD,flags| FD_CLOEXEC);
}
// exec to self
String exe = args.get(0);
LIBC.execvp(exe, new com.sun.jna.StringArray(args.toArray(new String[0])));
throw new IOException("Failed to exec '"+exe+"' "+LIBC.strerror(com.sun.jna.Native.getLastError()));
}
}

View File

@@ -0,0 +1,10 @@
package sonia.scm.lifecycle;
/**
* Exception is thrown if a restart is not supported or a restart strategy is misconfigured.
*/
public class RestartNotSupportedException extends RuntimeException {
RestartNotSupportedException(String message, Throwable cause) {
super(message, cause);
}
}

View File

@@ -1,5 +1,7 @@
package sonia.scm.lifecycle;
import java.util.Optional;
/**
* Strategy for restarting SCM-Manager.
*/
@@ -13,6 +15,7 @@ public interface RestartStrategy {
* Initialize the injection context.
*/
void initialize();
/**
* Destroys the injection context.
*/
@@ -21,17 +24,19 @@ public interface RestartStrategy {
/**
* Restart SCM-Manager.
*
* @param context injection context
*/
void restart(InjectionContext context);
/**
* Returns the configured strategy.
* Returns the configured strategy or empty if restart is not supported by the underlying platform.
*
* @return configured strategy
* @param webAppClassLoader root webapp classloader
* @return configured strategy or empty optional
*/
static RestartStrategy get(ClassLoader webAppClassLoader) {
return new InjectionContextRestartStrategy(webAppClassLoader);
static Optional<RestartStrategy> get(ClassLoader webAppClassLoader) {
return Optional.ofNullable(RestartStrategyFactory.create(webAppClassLoader));
}
}

View File

@@ -0,0 +1,66 @@
package sonia.scm.lifecycle;
import com.google.common.base.Strings;
import sonia.scm.PlatformType;
import sonia.scm.util.SystemUtil;
final class RestartStrategyFactory {
/**
* System property to load a specific restart strategy.
*/
static final String PROPERTY_STRATEGY = "sonia.scm.lifecycle.restart-strategy";
/**
* No restart supported.
*/
static final String STRATEGY_NONE = "none";
private RestartStrategyFactory() {
}
/**
* Returns the configured strategy or {@code null} if restart is not supported.
*
* @param webAppClassLoader root webapp classloader
* @return configured strategy or {@code null}
*/
static RestartStrategy create(ClassLoader webAppClassLoader) {
String property = System.getProperty(PROPERTY_STRATEGY);
if (Strings.isNullOrEmpty(property)) {
return forPlatform();
}
return fromProperty(webAppClassLoader, property);
}
private static RestartStrategy fromProperty(ClassLoader webAppClassLoader, String property) {
if (STRATEGY_NONE.equalsIgnoreCase(property)) {
return null;
} else if (ExitRestartStrategy.NAME.equalsIgnoreCase(property)) {
return new ExitRestartStrategy();
} else if (InjectionContextRestartStrategy.NAME.equalsIgnoreCase(property)) {
return new InjectionContextRestartStrategy(webAppClassLoader);
} else {
return fromClassName(property);
}
}
private static RestartStrategy fromClassName(String property) {
try {
Class<? extends RestartStrategy> rsClass = Class.forName(property).asSubclass(RestartStrategy.class);
return rsClass.getConstructor().newInstance();
} catch (Exception e) {
throw new RestartNotSupportedException("failed to create restart strategy from property", e);
}
}
private static RestartStrategy forPlatform() {
// we do not use SystemUtil here, to allow testing
String osName = System.getProperty(SystemUtil.PROPERTY_OSNAME);
PlatformType platform = PlatformType.createPlatformType(osName);
if (platform.isPosix()) {
return new PosixRestartStrategy();
}
return null;
}
}

View File

@@ -0,0 +1,75 @@
package sonia.scm.lifecycle;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import java.util.function.IntConsumer;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.Mockito.verify;
@ExtendWith(MockitoExtension.class)
class ExitRestartStrategyTest {
@Mock
private RestartStrategy.InjectionContext context;
private ExitRestartStrategy strategy;
private CapturingExiter exiter;
@BeforeEach
void setUpStrategy() {
strategy = new ExitRestartStrategy();
exiter = new CapturingExiter();
strategy.setExiter(exiter);
}
@Test
void shouldTearDownContextAndThenExit() {
strategy.restart(context);
verify(context).destroy();
assertThat(exiter.getExitCode()).isEqualTo(0);
}
@Test
void shouldUseExitCodeFromSystemProperty() {
System.setProperty(ExitRestartStrategy.PROPERTY_EXIT_CODE, "42");
try {
strategy.restart(context);
verify(context).destroy();
assertThat(exiter.getExitCode()).isEqualTo(42);
} finally {
System.clearProperty(ExitRestartStrategy.PROPERTY_EXIT_CODE);
}
}
@Test
void shouldThrowExceptionForNonNumericExitCode() {
System.setProperty(ExitRestartStrategy.PROPERTY_EXIT_CODE, "xyz");
try {
assertThrows(RestartNotSupportedException.class, () -> strategy.restart(context));
} finally {
System.clearProperty(ExitRestartStrategy.PROPERTY_EXIT_CODE);
}
}
private static class CapturingExiter implements IntConsumer {
private int exitCode = -1;
public int getExitCode() {
return exitCode;
}
@Override
public void accept(int exitCode) {
this.exitCode = exitCode;
}
}
}

View File

@@ -0,0 +1,101 @@
package sonia.scm.lifecycle;
import com.google.common.base.Strings;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
import sonia.scm.util.SystemUtil;
import java.util.Optional;
import java.util.function.Consumer;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertThrows;
class RestartStrategyTest {
private final ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
@Test
void shouldReturnRestartStrategyFromSystemProperty() {
withStrategy(TestingRestartStrategy.class.getName(), (rs) -> {
assertThat(rs).containsInstanceOf(TestingRestartStrategy.class);
});
}
@Test
void shouldThrowExceptionForNonStrategyClass() {
withStrategy(RestartStrategyTest.class.getName(), () -> {
assertThrows(RestartNotSupportedException.class, () -> RestartStrategy.get(classLoader));
});
}
@Test
void shouldReturnEmpty() {
withStrategy(RestartStrategyFactory.STRATEGY_NONE, (rs) -> {
assertThat(rs).isEmpty();
});
}
@Test
void shouldReturnEmptyForUnknownOs() {
withSystemProperty(SystemUtil.PROPERTY_OSNAME, "hitchhiker-os", () -> {
Optional<RestartStrategy> restartStrategy = RestartStrategy.get(classLoader);
assertThat(restartStrategy).isEmpty();
});
}
@Test
void shouldReturnExitRestartStrategy() {
withStrategy(ExitRestartStrategy.NAME, (rs) -> {
assertThat(rs).containsInstanceOf(ExitRestartStrategy.class);
});
}
@Test
void shouldReturnInjectionContextRestartStrategy() {
withStrategy(InjectionContextRestartStrategy.NAME, (rs) -> {
assertThat(rs).containsInstanceOf(InjectionContextRestartStrategy.class);
});
}
@ParameterizedTest
@ValueSource(strings = { "linux", "darwin", "solaris", "freebsd", "openbsd" })
void shouldReturnPosixRestartStrategyForPosixBased(String os) {
withSystemProperty(SystemUtil.PROPERTY_OSNAME, os, () -> {
Optional<RestartStrategy> restartStrategy = RestartStrategy.get(classLoader);
assertThat(restartStrategy).containsInstanceOf(PosixRestartStrategy.class);
});
}
private void withStrategy(String strategy, Consumer<Optional<RestartStrategy>> consumer) {
withStrategy(strategy, () -> {
consumer.accept(RestartStrategy.get(classLoader));
});
}
private void withStrategy(String strategy, Runnable runnable) {
withSystemProperty(RestartStrategyFactory.PROPERTY_STRATEGY, strategy, runnable);
}
private void withSystemProperty(String key, String value, Runnable runnable) {
String oldValue = System.getProperty(key);
System.setProperty(key, value);
try {
runnable.run();
} finally {
if (Strings.isNullOrEmpty(oldValue)) {
System.clearProperty(key);
} else {
System.setProperty(key, oldValue);
}
}
}
public static class TestingRestartStrategy implements RestartStrategy {
@Override
public void restart(InjectionContext context) {
}
}
}