mirror of
https://github.com/scm-manager/scm-manager.git
synced 2025-11-12 16:35:45 +01:00
Merge pull request #1079 from scm-manager/feature/real_jvm_restart
Real jvm restart
This commit is contained in:
@@ -21,9 +21,10 @@
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
|
||||
package sonia.scm.api.v2.resources;
|
||||
|
||||
import com.google.common.annotations.VisibleForTesting;
|
||||
import io.swagger.v3.oas.annotations.OpenAPIDefinition;
|
||||
import io.swagger.v3.oas.annotations.Operation;
|
||||
import io.swagger.v3.oas.annotations.Parameter;
|
||||
@@ -45,6 +46,7 @@ import javax.ws.rs.PUT;
|
||||
import javax.ws.rs.Path;
|
||||
import javax.ws.rs.Produces;
|
||||
import javax.ws.rs.core.Response;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
/**
|
||||
* RESTful Web Service Resource to manage the configuration.
|
||||
@@ -61,6 +63,8 @@ public class ConfigResource {
|
||||
private final ScmConfiguration configuration;
|
||||
private final NamespaceStrategyValidator namespaceStrategyValidator;
|
||||
|
||||
private Consumer<ScmConfiguration> store = (config) -> ScmConfigurationUtil.getInstance().store(config);
|
||||
|
||||
@Inject
|
||||
public ConfigResource(ConfigDtoToScmConfigurationMapper dtoToConfigMapper,
|
||||
ScmConfigurationToConfigDtoMapper configToDtoMapper,
|
||||
@@ -71,6 +75,11 @@ public class ConfigResource {
|
||||
this.namespaceStrategyValidator = namespaceStrategyValidator;
|
||||
}
|
||||
|
||||
@VisibleForTesting
|
||||
void setStore(Consumer<ScmConfiguration> store) {
|
||||
this.store = store;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the global scm config.
|
||||
*/
|
||||
@@ -137,7 +146,7 @@ public class ConfigResource {
|
||||
ScmConfiguration config = dtoToConfigMapper.map(configDto);
|
||||
synchronized (ScmConfiguration.class) {
|
||||
configuration.load(config);
|
||||
ScmConfigurationUtil.getInstance().store(configuration);
|
||||
store.accept(configuration);
|
||||
}
|
||||
|
||||
return Response.noContent().build();
|
||||
|
||||
@@ -21,7 +21,7 @@
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
|
||||
package sonia.scm.api.v2.resources;
|
||||
|
||||
import de.otto.edison.hal.Embedded;
|
||||
@@ -31,6 +31,7 @@ import io.swagger.v3.oas.annotations.Operation;
|
||||
import io.swagger.v3.oas.annotations.media.Content;
|
||||
import io.swagger.v3.oas.annotations.media.Schema;
|
||||
import io.swagger.v3.oas.annotations.responses.ApiResponse;
|
||||
import sonia.scm.lifecycle.Restarter;
|
||||
import sonia.scm.plugin.AvailablePlugin;
|
||||
import sonia.scm.plugin.InstalledPlugin;
|
||||
import sonia.scm.plugin.PluginManager;
|
||||
@@ -56,12 +57,14 @@ public class PendingPluginResource {
|
||||
private final PluginManager pluginManager;
|
||||
private final ResourceLinks resourceLinks;
|
||||
private final PluginDtoMapper mapper;
|
||||
private final Restarter restarter;
|
||||
|
||||
@Inject
|
||||
public PendingPluginResource(PluginManager pluginManager, ResourceLinks resourceLinks, PluginDtoMapper mapper) {
|
||||
public PendingPluginResource(PluginManager pluginManager, ResourceLinks resourceLinks, PluginDtoMapper mapper, Restarter restarter) {
|
||||
this.pluginManager = pluginManager;
|
||||
this.resourceLinks = resourceLinks;
|
||||
this.mapper = mapper;
|
||||
this.restarter = restarter;
|
||||
}
|
||||
|
||||
@GET
|
||||
@@ -118,7 +121,9 @@ public class PendingPluginResource {
|
||||
PluginPermissions.manage().isPermitted() &&
|
||||
(!installDtos.isEmpty() || !updateDtos.isEmpty() || !uninstallDtos.isEmpty())
|
||||
) {
|
||||
linksBuilder.single(link("execute", resourceLinks.pendingPluginCollection().executePending()));
|
||||
if (restarter.isSupported()) {
|
||||
linksBuilder.single(link("execute", resourceLinks.pendingPluginCollection().executePending()));
|
||||
}
|
||||
linksBuilder.single(link("cancel", resourceLinks.pendingPluginCollection().cancelPending()));
|
||||
}
|
||||
|
||||
|
||||
@@ -21,12 +21,13 @@
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
|
||||
package sonia.scm.api.v2.resources;
|
||||
|
||||
import de.otto.edison.hal.Links;
|
||||
import org.mapstruct.Mapper;
|
||||
import org.mapstruct.MappingTarget;
|
||||
import sonia.scm.lifecycle.Restarter;
|
||||
import sonia.scm.plugin.AvailablePlugin;
|
||||
import sonia.scm.plugin.InstalledPlugin;
|
||||
import sonia.scm.plugin.Plugin;
|
||||
@@ -47,6 +48,9 @@ public abstract class PluginDtoMapper {
|
||||
@Inject
|
||||
private ResourceLinks resourceLinks;
|
||||
|
||||
@Inject
|
||||
private Restarter restarter;
|
||||
|
||||
public abstract void map(PluginInformation plugin, @MappingTarget PluginDto dto);
|
||||
|
||||
public PluginDto mapInstalled(InstalledPlugin plugin, List<AvailablePlugin> availablePlugins) {
|
||||
@@ -78,12 +82,20 @@ public abstract class PluginDtoMapper {
|
||||
.self(information.getName()));
|
||||
|
||||
if (!plugin.isPending() && PluginPermissions.manage().isPermitted()) {
|
||||
links.single(link("install", resourceLinks.availablePlugin().install(information.getName())));
|
||||
String href = resourceLinks.availablePlugin().install(information.getName());
|
||||
appendLink(links, "install", href);
|
||||
}
|
||||
|
||||
return new PluginDto(links.build());
|
||||
}
|
||||
|
||||
private void appendLink(Links.Builder links, String name, String href) {
|
||||
links.single(link(name, href));
|
||||
if (restarter.isSupported()) {
|
||||
links.single(link(name + "WithRestart", href + "?restart=true"));
|
||||
}
|
||||
}
|
||||
|
||||
private PluginDto createDtoForInstalled(InstalledPlugin plugin, List<AvailablePlugin> availablePlugins) {
|
||||
PluginInformation information = plugin.getDescriptor().getInformation();
|
||||
Optional<AvailablePlugin> availablePlugin = checkForUpdates(plugin, availablePlugins);
|
||||
@@ -96,13 +108,16 @@ public abstract class PluginDtoMapper {
|
||||
&& !availablePlugin.get().isPending()
|
||||
&& PluginPermissions.manage().isPermitted()
|
||||
) {
|
||||
links.single(link("update", resourceLinks.availablePlugin().install(information.getName())));
|
||||
String href = resourceLinks.availablePlugin().install(information.getName());
|
||||
appendLink(links, "update", href);
|
||||
}
|
||||
|
||||
if (plugin.isUninstallable()
|
||||
&& (!availablePlugin.isPresent() || !availablePlugin.get().isPending())
|
||||
&& PluginPermissions.manage().isPermitted()
|
||||
) {
|
||||
links.single(link("uninstall", resourceLinks.installedPlugin().uninstall(information.getName())));
|
||||
String href = resourceLinks.installedPlugin().uninstall(information.getName());
|
||||
appendLink(links, "uninstall", href);
|
||||
}
|
||||
|
||||
PluginDto dto = new PluginDto(links.build());
|
||||
|
||||
@@ -21,7 +21,7 @@
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
|
||||
package sonia.scm.lifecycle;
|
||||
|
||||
//~--- non-JDK imports --------------------------------------------------------
|
||||
@@ -35,6 +35,7 @@ import sonia.scm.event.ScmEventBus;
|
||||
import javax.servlet.FilterConfig;
|
||||
import javax.servlet.ServletContextEvent;
|
||||
import javax.servlet.ServletException;
|
||||
import java.util.Optional;
|
||||
|
||||
//~--- JDK imports ------------------------------------------------------------
|
||||
|
||||
@@ -93,12 +94,16 @@ 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");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private class GuiceInjectionContext implements RestartStrategy.InjectionContext {
|
||||
private class GuiceInjectionContext implements RestartStrategy.InternalInjectionContext {
|
||||
|
||||
@Override
|
||||
public void initialize() {
|
||||
|
||||
45
scm-webapp/src/main/java/sonia/scm/lifecycle/CLibrary.java
Normal file
45
scm-webapp/src/main/java/sonia/scm/lifecycle/CLibrary.java
Normal file
@@ -0,0 +1,45 @@
|
||||
/*
|
||||
* MIT License
|
||||
*
|
||||
* Copyright (c) 2020-present Cloudogu GmbH and Contributors
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
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);
|
||||
}
|
||||
@@ -21,48 +21,44 @@
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
package sonia.scm.lifecycle.classloading;
|
||||
package sonia.scm.lifecycle;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import se.jiderhamn.classloader.leak.prevention.ClassLoaderLeakPreventor;
|
||||
import com.google.common.annotations.VisibleForTesting;
|
||||
import sonia.scm.event.ScmEventBus;
|
||||
|
||||
/**
|
||||
* Logging adapter for {@link ClassLoaderLeakPreventor}.
|
||||
*/
|
||||
public class LoggingAdapter implements se.jiderhamn.classloader.leak.prevention.Logger {
|
||||
import javax.inject.Inject;
|
||||
import javax.inject.Singleton;
|
||||
|
||||
@SuppressWarnings("squid:S3416") // suppress "loggers should be named for their enclosing classes" rule
|
||||
private static final Logger LOG = LoggerFactory.getLogger(ClassLoaderLeakPreventor.class);
|
||||
@Singleton
|
||||
public class DefaultRestarter implements Restarter {
|
||||
|
||||
@Override
|
||||
public void debug(String msg) {
|
||||
LOG.debug(msg);
|
||||
private ScmEventBus eventBus;
|
||||
private RestartStrategy strategy;
|
||||
|
||||
@Inject
|
||||
public DefaultRestarter() {
|
||||
this(
|
||||
ScmEventBus.getInstance(),
|
||||
RestartStrategy.get(Thread.currentThread().getContextClassLoader()).orElse(null)
|
||||
);
|
||||
}
|
||||
|
||||
@VisibleForTesting
|
||||
DefaultRestarter(ScmEventBus eventBus, RestartStrategy strategy) {
|
||||
this.eventBus = eventBus;
|
||||
this.strategy = strategy;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void info(String msg) {
|
||||
LOG.info(msg);
|
||||
public boolean isSupported() {
|
||||
return strategy != null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void warn(String msg) {
|
||||
LOG.warn(msg);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void warn(Throwable t) {
|
||||
LOG.warn(t.getMessage(), t);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void error(String msg) {
|
||||
LOG.error(msg);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void error(Throwable t) {
|
||||
LOG.error(t.getMessage(), t);
|
||||
public void restart(Class<?> cause, String reason) {
|
||||
if (!isSupported()) {
|
||||
throw new RestartNotSupportedException("restarting is not supported");
|
||||
}
|
||||
eventBus.post(new RestartEvent(cause, reason));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,77 @@
|
||||
/*
|
||||
* MIT License
|
||||
*
|
||||
* Copyright (c) 2020-present Cloudogu GmbH and Contributors
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
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 extends 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;
|
||||
|
||||
private int exitCode;
|
||||
|
||||
ExitRestartStrategy() {
|
||||
}
|
||||
|
||||
@VisibleForTesting
|
||||
void setExiter(IntConsumer exiter) {
|
||||
this.exiter = exiter;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void prepareRestart(InjectionContext context) {
|
||||
exitCode = determineExitCode();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void executeRestart(InjectionContext context) {
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,117 +0,0 @@
|
||||
/*
|
||||
* MIT License
|
||||
*
|
||||
* Copyright (c) 2020-present Cloudogu GmbH and Contributors
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
package sonia.scm.lifecycle;
|
||||
|
||||
import com.google.common.annotations.VisibleForTesting;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import sonia.scm.event.RecreateEventBusEvent;
|
||||
import sonia.scm.event.ScmEventBus;
|
||||
import sonia.scm.event.ShutdownEventBusEvent;
|
||||
|
||||
import java.util.concurrent.atomic.AtomicLong;
|
||||
|
||||
/**
|
||||
* Restart strategy implementation which destroy the injection context and re initialize it.
|
||||
*/
|
||||
public class InjectionContextRestartStrategy implements RestartStrategy {
|
||||
|
||||
private static final String DISABLE_RESTART_PROPERTY = "sonia.scm.restart.disable";
|
||||
private static final String WAIT_PROPERTY = "sonia.scm.restart.wait";
|
||||
private static final String DISABLE_GC_PROPERTY = "sonia.scm.restart.disable-gc";
|
||||
|
||||
private static final AtomicLong INSTANCE_COUNTER = new AtomicLong();
|
||||
private static final Logger LOG = LoggerFactory.getLogger(InjectionContextRestartStrategy.class);
|
||||
|
||||
private boolean restartEnabled = !Boolean.getBoolean(DISABLE_RESTART_PROPERTY);
|
||||
private long waitInMs = Integer.getInteger(WAIT_PROPERTY, 250);
|
||||
private boolean gcEnabled = !Boolean.getBoolean(DISABLE_GC_PROPERTY);
|
||||
|
||||
private final ClassLoader webAppClassLoader;
|
||||
|
||||
InjectionContextRestartStrategy(ClassLoader webAppClassLoader) {
|
||||
this.webAppClassLoader = webAppClassLoader;
|
||||
}
|
||||
|
||||
@VisibleForTesting
|
||||
void setWaitInMs(long waitInMs) {
|
||||
this.waitInMs = waitInMs;
|
||||
}
|
||||
|
||||
@VisibleForTesting
|
||||
void setGcEnabled(boolean gcEnabled) {
|
||||
this.gcEnabled = gcEnabled;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void restart(InjectionContext context) {
|
||||
stop(context);
|
||||
if (restartEnabled) {
|
||||
start(context);
|
||||
} else {
|
||||
LOG.warn("restarting context is disabled");
|
||||
}
|
||||
}
|
||||
|
||||
@SuppressWarnings("squid:S1215") // suppress explicit gc call warning
|
||||
private void start(InjectionContext context) {
|
||||
LOG.debug("use WebAppClassLoader as ContextClassLoader, to avoid ClassLoader leaks");
|
||||
Thread.currentThread().setContextClassLoader(webAppClassLoader);
|
||||
|
||||
LOG.warn("send recreate eventbus event");
|
||||
ScmEventBus.getInstance().post(new RecreateEventBusEvent());
|
||||
|
||||
// restart context delayed, to avoid timing problems
|
||||
new Thread(() -> {
|
||||
try {
|
||||
if (gcEnabled){
|
||||
LOG.info("call gc to clean up memory from old instances");
|
||||
System.gc();
|
||||
}
|
||||
|
||||
LOG.info("wait {}ms before re starting the context", waitInMs);
|
||||
Thread.sleep(waitInMs);
|
||||
|
||||
LOG.warn("reinitialize injection context");
|
||||
context.initialize();
|
||||
|
||||
LOG.debug("register injection context on new eventbus");
|
||||
ScmEventBus.getInstance().register(context);
|
||||
} catch ( Exception ex) {
|
||||
LOG.error("failed to restart", ex);
|
||||
}
|
||||
}, "Delayed-Restart-" + INSTANCE_COUNTER.incrementAndGet()).start();
|
||||
}
|
||||
|
||||
private void stop(InjectionContext context) {
|
||||
LOG.warn("destroy injection context");
|
||||
context.destroy();
|
||||
|
||||
if (!restartEnabled) {
|
||||
// shutdown eventbus, but do this only if restart is disabled
|
||||
ScmEventBus.getInstance().post(new ShutdownEventBusEvent());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,72 @@
|
||||
/*
|
||||
* MIT License
|
||||
*
|
||||
* Copyright (c) 2020-present Cloudogu GmbH and Contributors
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
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 extends RestartStrategy {
|
||||
|
||||
private static final Logger LOG = LoggerFactory.getLogger(PosixRestartStrategy.class);
|
||||
|
||||
PosixRestartStrategy() {
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void executeRestart(InjectionContext context) {
|
||||
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()));
|
||||
}
|
||||
}
|
||||
@@ -21,22 +21,23 @@
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
|
||||
package sonia.scm.lifecycle;
|
||||
|
||||
/**
|
||||
* Strategy for restarting SCM-Manager.
|
||||
*/
|
||||
public interface RestartStrategy {
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
/**
|
||||
* Context for Injection in SCM-Manager.
|
||||
*/
|
||||
interface InjectionContext {
|
||||
/**
|
||||
* Initialize the injection context.
|
||||
*/
|
||||
void initialize();
|
||||
import java.util.Optional;
|
||||
|
||||
/**
|
||||
* Strategy for restarting SCM-Manager. Implementations must either have a default constructor or one taking the
|
||||
* class loader for the current context as a single argument.
|
||||
*/
|
||||
public abstract class RestartStrategy {
|
||||
|
||||
private static final Logger LOG = LoggerFactory.getLogger(RestartStrategy.class);
|
||||
|
||||
interface InternalInjectionContext extends InjectionContext {
|
||||
/**
|
||||
* Destroys the injection context.
|
||||
*/
|
||||
@@ -44,18 +45,52 @@ public interface RestartStrategy {
|
||||
}
|
||||
|
||||
/**
|
||||
* Restart SCM-Manager.
|
||||
* @param context injection context
|
||||
* Context for Injection in SCM-Manager.
|
||||
*/
|
||||
void restart(InjectionContext context);
|
||||
public interface InjectionContext {
|
||||
/**
|
||||
* Initialize the injection context.
|
||||
*/
|
||||
void initialize();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the configured strategy.
|
||||
* Restart SCM-Manager by first calling {@link #prepareRestart(InjectionContext)}, destroying the
|
||||
* current context, and finally calling {@link #executeRestart(InjectionContext)}.
|
||||
*
|
||||
* @return configured strategy
|
||||
* @param context injection context
|
||||
*/
|
||||
static RestartStrategy get(ClassLoader webAppClassLoader) {
|
||||
return new InjectionContextRestartStrategy(webAppClassLoader);
|
||||
public final void restart(InternalInjectionContext context) {
|
||||
prepareRestart(context);
|
||||
LOG.warn("destroy injection context");
|
||||
context.destroy();
|
||||
executeRestart(context);
|
||||
}
|
||||
|
||||
/**
|
||||
* Prepare the restart of SCM-Manager. Here you can check whether restart is possible and,
|
||||
* if necessary, throw a {@link RestartNotSupportedException} to abort the restart.
|
||||
*
|
||||
* @param context injection context
|
||||
*/
|
||||
protected void prepareRestart(InjectionContext context) {
|
||||
}
|
||||
|
||||
/**
|
||||
* Actually restart SCM-Manager.
|
||||
*
|
||||
* @param context injection context
|
||||
*/
|
||||
protected abstract void executeRestart(InjectionContext context);
|
||||
|
||||
/**
|
||||
* Returns the configured strategy or empty if restart is not supported by the underlying platform.
|
||||
*
|
||||
* @param webAppClassLoader root webapp classloader
|
||||
* @return configured strategy or empty optional
|
||||
*/
|
||||
static Optional<RestartStrategy> get(ClassLoader webAppClassLoader) {
|
||||
return Optional.ofNullable(RestartStrategyFactory.create(webAppClassLoader));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,98 @@
|
||||
/*
|
||||
* MIT License
|
||||
*
|
||||
* Copyright (c) 2020-present Cloudogu GmbH and Contributors
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
package sonia.scm.lifecycle;
|
||||
|
||||
import com.google.common.base.Strings;
|
||||
import sonia.scm.PlatformType;
|
||||
import sonia.scm.util.SystemUtil;
|
||||
|
||||
import java.lang.reflect.Constructor;
|
||||
|
||||
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 {
|
||||
return fromClassName(property, webAppClassLoader);
|
||||
}
|
||||
}
|
||||
|
||||
private static RestartStrategy fromClassName(String className, ClassLoader classLoader) {
|
||||
try {
|
||||
Class<? extends RestartStrategy> rsClass = Class.forName(className).asSubclass(RestartStrategy.class);
|
||||
return createInstance(rsClass, classLoader);
|
||||
} catch (Exception e) {
|
||||
throw new RestartNotSupportedException("failed to create restart strategy from property", e);
|
||||
}
|
||||
}
|
||||
|
||||
private static RestartStrategy createInstance(Class<? extends RestartStrategy> rsClass, ClassLoader classLoader) throws InstantiationException, IllegalAccessException, java.lang.reflect.InvocationTargetException, NoSuchMethodException {
|
||||
try {
|
||||
Constructor<? extends RestartStrategy> constructor = rsClass.getConstructor(ClassLoader.class);
|
||||
return constructor.newInstance(classLoader);
|
||||
} catch (NoSuchMethodException ex) {
|
||||
return rsClass.getConstructor().newInstance();
|
||||
}
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
@@ -21,10 +21,9 @@
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
|
||||
package sonia.scm.lifecycle.classloading;
|
||||
|
||||
import com.google.common.annotations.VisibleForTesting;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import sonia.scm.lifecycle.LifeCycle;
|
||||
@@ -42,18 +41,10 @@ public abstract class ClassLoaderLifeCycle implements LifeCycle {
|
||||
|
||||
private static final Logger LOG = LoggerFactory.getLogger(ClassLoaderLifeCycle.class);
|
||||
|
||||
@VisibleForTesting
|
||||
static final String PROPERTY = "sonia.scm.classloading.lifecycle";
|
||||
|
||||
public static ClassLoaderLifeCycle create() {
|
||||
ClassLoader webappClassLoader = Thread.currentThread().getContextClassLoader();
|
||||
String implementation = System.getProperty(PROPERTY);
|
||||
if (SimpleClassLoaderLifeCycle.NAME.equalsIgnoreCase(implementation)) {
|
||||
LOG.info("create new simple ClassLoaderLifeCycle");
|
||||
return new SimpleClassLoaderLifeCycle(webappClassLoader);
|
||||
}
|
||||
LOG.info("create new ClassLoaderLifeCycle with leak prevention");
|
||||
return new ClassLoaderLifeCycleWithLeakPrevention(webappClassLoader);
|
||||
LOG.info("create new simple ClassLoaderLifeCycle");
|
||||
return new SimpleClassLoaderLifeCycle(webappClassLoader);
|
||||
}
|
||||
|
||||
private final ClassLoader webappClassLoader;
|
||||
|
||||
@@ -1,187 +0,0 @@
|
||||
/*
|
||||
* MIT License
|
||||
*
|
||||
* Copyright (c) 2020-present Cloudogu GmbH and Contributors
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
package sonia.scm.lifecycle.classloading;
|
||||
|
||||
import com.google.common.annotations.VisibleForTesting;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import se.jiderhamn.classloader.leak.prevention.ClassLoaderLeakPreventor;
|
||||
import se.jiderhamn.classloader.leak.prevention.ClassLoaderLeakPreventorFactory;
|
||||
import se.jiderhamn.classloader.leak.prevention.cleanup.IIOServiceProviderCleanUp;
|
||||
import se.jiderhamn.classloader.leak.prevention.cleanup.MBeanCleanUp;
|
||||
import se.jiderhamn.classloader.leak.prevention.cleanup.ShutdownHookCleanUp;
|
||||
import se.jiderhamn.classloader.leak.prevention.cleanup.StopThreadsCleanUp;
|
||||
import se.jiderhamn.classloader.leak.prevention.preinit.AwtToolkitInitiator;
|
||||
import se.jiderhamn.classloader.leak.prevention.preinit.Java2dDisposerInitiator;
|
||||
import se.jiderhamn.classloader.leak.prevention.preinit.Java2dRenderQueueInitiator;
|
||||
import se.jiderhamn.classloader.leak.prevention.preinit.SunAwtAppContextInitiator;
|
||||
|
||||
import java.io.Closeable;
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayDeque;
|
||||
import java.util.Deque;
|
||||
|
||||
import static se.jiderhamn.classloader.leak.prevention.cleanup.ShutdownHookCleanUp.SHUTDOWN_HOOK_WAIT_MS_DEFAULT;
|
||||
|
||||
/**
|
||||
* Creates and shutdown SCM-Manager ClassLoaders with ClassLoader leak detection.
|
||||
*/
|
||||
final class ClassLoaderLifeCycleWithLeakPrevention extends ClassLoaderLifeCycle {
|
||||
|
||||
private static final Logger LOG = LoggerFactory.getLogger(ClassLoaderLifeCycleWithLeakPrevention.class);
|
||||
|
||||
private Deque<ClassLoaderAndPreventor> classLoaders = new ArrayDeque<>();
|
||||
|
||||
private final ClassLoaderLeakPreventorFactory classLoaderLeakPreventorFactory;
|
||||
|
||||
private ClassLoaderAppendListener classLoaderAppendListener = new ClassLoaderAppendListener() {
|
||||
@Override
|
||||
public <C extends ClassLoader> C apply(C classLoader) {
|
||||
return classLoader;
|
||||
}
|
||||
};
|
||||
|
||||
ClassLoaderLifeCycleWithLeakPrevention(ClassLoader webappClassLoader) {
|
||||
this(webappClassLoader, createClassLoaderLeakPreventorFactory(webappClassLoader));
|
||||
}
|
||||
|
||||
ClassLoaderLifeCycleWithLeakPrevention(ClassLoader webappClassLoader, ClassLoaderLeakPreventorFactory classLoaderLeakPreventorFactory) {
|
||||
super(webappClassLoader);
|
||||
this.classLoaderLeakPreventorFactory = classLoaderLeakPreventorFactory;
|
||||
}
|
||||
|
||||
private static ClassLoaderLeakPreventorFactory createClassLoaderLeakPreventorFactory(ClassLoader webappClassLoader) {
|
||||
// Should threads tied to the web app classloader be forced to stop at application shutdown?
|
||||
boolean stopThreads = Boolean.getBoolean("ClassLoaderLeakPreventor.stopThreads");
|
||||
|
||||
// Should Timer threads tied to the web app classloader be forced to stop at application shutdown?
|
||||
boolean stopTimerThreads = Boolean.getBoolean("ClassLoaderLeakPreventor.stopTimerThreads");
|
||||
|
||||
// Should shutdown hooks registered from the application be executed at application shutdown?
|
||||
boolean executeShutdownHooks = Boolean.getBoolean("ClassLoaderLeakPreventor.executeShutdownHooks");
|
||||
|
||||
// No of milliseconds to wait for threads to finish execution, before stopping them.
|
||||
int threadWaitMs = Integer.getInteger("ClassLoaderLeakPreventor.threadWaitMs", ClassLoaderLeakPreventor.THREAD_WAIT_MS_DEFAULT);
|
||||
|
||||
/*
|
||||
* No of milliseconds to wait for shutdown hooks to finish execution, before stopping them.
|
||||
* If set to -1 there will be no waiting at all, but Thread is allowed to run until finished.
|
||||
*/
|
||||
int shutdownHookWaitMs = Integer.getInteger("ClassLoaderLeakPreventor.shutdownHookWaitMs", SHUTDOWN_HOOK_WAIT_MS_DEFAULT);
|
||||
|
||||
LOG.info("Settings for {} (CL: 0x{}):", ClassLoaderLifeCycleWithLeakPrevention.class.getName(), Integer.toHexString(System.identityHashCode(webappClassLoader)) );
|
||||
LOG.info(" stopThreads = {}", stopThreads);
|
||||
LOG.info(" stopTimerThreads = {}", stopTimerThreads);
|
||||
LOG.info(" executeShutdownHooks = {}", executeShutdownHooks);
|
||||
LOG.info(" threadWaitMs = {} ms", threadWaitMs);
|
||||
LOG.info(" shutdownHookWaitMs = {} ms", shutdownHookWaitMs);
|
||||
|
||||
// use webapp classloader as safe base? or system?
|
||||
ClassLoaderLeakPreventorFactory classLoaderLeakPreventorFactory = new ClassLoaderLeakPreventorFactory(webappClassLoader);
|
||||
classLoaderLeakPreventorFactory.setLogger(new LoggingAdapter());
|
||||
|
||||
final ShutdownHookCleanUp shutdownHookCleanUp = classLoaderLeakPreventorFactory.getCleanUp(ShutdownHookCleanUp.class);
|
||||
shutdownHookCleanUp.setExecuteShutdownHooks(executeShutdownHooks);
|
||||
shutdownHookCleanUp.setShutdownHookWaitMs(shutdownHookWaitMs);
|
||||
|
||||
final StopThreadsCleanUp stopThreadsCleanUp = classLoaderLeakPreventorFactory.getCleanUp(StopThreadsCleanUp.class);
|
||||
stopThreadsCleanUp.setStopThreads(stopThreads);
|
||||
stopThreadsCleanUp.setStopTimerThreads(stopTimerThreads);
|
||||
stopThreadsCleanUp.setThreadWaitMs(threadWaitMs);
|
||||
|
||||
// remove awt and imageio cleanup
|
||||
classLoaderLeakPreventorFactory.removePreInitiator(AwtToolkitInitiator.class);
|
||||
classLoaderLeakPreventorFactory.removePreInitiator(SunAwtAppContextInitiator.class);
|
||||
classLoaderLeakPreventorFactory.removeCleanUp(IIOServiceProviderCleanUp.class);
|
||||
classLoaderLeakPreventorFactory.removePreInitiator(Java2dRenderQueueInitiator.class);
|
||||
classLoaderLeakPreventorFactory.removePreInitiator(Java2dDisposerInitiator.class);
|
||||
|
||||
// the MBeanCleanUp causes a Exception and we use no mbeans
|
||||
classLoaderLeakPreventorFactory.removeCleanUp(MBeanCleanUp.class);
|
||||
|
||||
return classLoaderLeakPreventorFactory;
|
||||
}
|
||||
|
||||
@VisibleForTesting
|
||||
void setClassLoaderAppendListener(ClassLoaderAppendListener classLoaderAppendListener) {
|
||||
this.classLoaderAppendListener = classLoaderAppendListener;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void shutdownClassLoaders() {
|
||||
ClassLoaderAndPreventor clap = classLoaders.poll();
|
||||
while (clap != null) {
|
||||
clap.shutdown();
|
||||
clap = classLoaders.poll();
|
||||
}
|
||||
// be sure it is realy empty
|
||||
classLoaders.clear();
|
||||
classLoaders = new ArrayDeque<>();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected <T extends ClassLoader> T initAndAppend(T originalClassLoader) {
|
||||
LOG.debug("init classloader {}", originalClassLoader);
|
||||
T classLoader = classLoaderAppendListener.apply(originalClassLoader);
|
||||
|
||||
ClassLoaderLeakPreventor preventor = classLoaderLeakPreventorFactory.newLeakPreventor(classLoader);
|
||||
preventor.runPreClassLoaderInitiators();
|
||||
classLoaders.push(new ClassLoaderAndPreventor(classLoader, preventor));
|
||||
|
||||
return classLoader;
|
||||
}
|
||||
|
||||
interface ClassLoaderAppendListener {
|
||||
<C extends ClassLoader> C apply(C classLoader);
|
||||
}
|
||||
|
||||
private static class ClassLoaderAndPreventor {
|
||||
|
||||
private final ClassLoader classLoader;
|
||||
private final ClassLoaderLeakPreventor preventor;
|
||||
|
||||
private ClassLoaderAndPreventor(ClassLoader classLoader, ClassLoaderLeakPreventor preventor) {
|
||||
this.classLoader = classLoader;
|
||||
this.preventor = preventor;
|
||||
}
|
||||
|
||||
void shutdown() {
|
||||
LOG.debug("shutdown classloader {}", classLoader);
|
||||
preventor.runCleanUps();
|
||||
close();
|
||||
}
|
||||
|
||||
private void close() {
|
||||
if (classLoader instanceof Closeable) {
|
||||
LOG.trace("close classloader {}", classLoader);
|
||||
try {
|
||||
((Closeable) classLoader).close();
|
||||
} catch (IOException e) {
|
||||
LOG.warn("failed to close classloader", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -33,6 +33,8 @@ import sonia.scm.SCMContext;
|
||||
import sonia.scm.SCMContextProvider;
|
||||
import sonia.scm.io.DefaultFileSystem;
|
||||
import sonia.scm.io.FileSystem;
|
||||
import sonia.scm.lifecycle.DefaultRestarter;
|
||||
import sonia.scm.lifecycle.Restarter;
|
||||
import sonia.scm.plugin.PluginLoader;
|
||||
import sonia.scm.repository.RepositoryLocationResolver;
|
||||
import sonia.scm.repository.xml.MetadataStore;
|
||||
@@ -85,6 +87,8 @@ public class BootstrapModule extends AbstractModule {
|
||||
|
||||
bind(FileSystem.class, DefaultFileSystem.class);
|
||||
|
||||
bind(Restarter.class, DefaultRestarter.class);
|
||||
|
||||
// note CipherUtil uses an other generator
|
||||
bind(CipherHandler.class).toInstance(CipherUtil.getInstance().getCipherHandler());
|
||||
|
||||
|
||||
@@ -32,6 +32,7 @@ import org.slf4j.LoggerFactory;
|
||||
import sonia.scm.NotFoundException;
|
||||
import sonia.scm.event.ScmEventBus;
|
||||
import sonia.scm.lifecycle.RestartEvent;
|
||||
import sonia.scm.lifecycle.Restarter;
|
||||
import sonia.scm.version.Version;
|
||||
|
||||
import javax.inject.Inject;
|
||||
@@ -59,20 +60,21 @@ public class DefaultPluginManager implements PluginManager {
|
||||
|
||||
private static final Logger LOG = LoggerFactory.getLogger(DefaultPluginManager.class);
|
||||
|
||||
private final ScmEventBus eventBus;
|
||||
private final PluginLoader loader;
|
||||
private final PluginCenter center;
|
||||
private final PluginInstaller installer;
|
||||
private final Restarter restarter;
|
||||
|
||||
private final Collection<PendingPluginInstallation> pendingInstallQueue = new ArrayList<>();
|
||||
private final Collection<PendingPluginUninstallation> pendingUninstallQueue = new ArrayList<>();
|
||||
private final PluginDependencyTracker dependencyTracker = new PluginDependencyTracker();
|
||||
|
||||
@Inject
|
||||
public DefaultPluginManager(ScmEventBus eventBus, PluginLoader loader, PluginCenter center, PluginInstaller installer) {
|
||||
this.eventBus = eventBus;
|
||||
public DefaultPluginManager(PluginLoader loader, PluginCenter center, PluginInstaller installer, Restarter restarter) {
|
||||
this.loader = loader;
|
||||
this.center = center;
|
||||
this.installer = installer;
|
||||
this.restarter = restarter;
|
||||
|
||||
this.computeInstallationDependencies();
|
||||
}
|
||||
@@ -233,16 +235,8 @@ public class DefaultPluginManager implements PluginManager {
|
||||
}
|
||||
}
|
||||
|
||||
@VisibleForTesting
|
||||
void triggerRestart(String cause) {
|
||||
new Thread(() -> {
|
||||
try {
|
||||
Thread.sleep(200);
|
||||
} catch (InterruptedException e) {
|
||||
Thread.currentThread().interrupt();
|
||||
}
|
||||
eventBus.post(new RestartEvent(PluginManager.class, cause));
|
||||
}).start();
|
||||
private void triggerRestart(String cause) {
|
||||
restarter.restart(PluginManager.class, cause);
|
||||
}
|
||||
|
||||
private void cancelPending(List<PendingPluginInstallation> pendingInstallations) {
|
||||
|
||||
@@ -21,11 +21,21 @@
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
|
||||
package sonia.scm.plugin;
|
||||
|
||||
import static sonia.scm.ContextEntry.ContextBuilder.entity;
|
||||
|
||||
public class PluginChecksumMismatchException extends PluginInstallException {
|
||||
public PluginChecksumMismatchException(String message) {
|
||||
super(message);
|
||||
public PluginChecksumMismatchException(AvailablePlugin plugin, String calculatedChecksum, String expectedChecksum) {
|
||||
super(
|
||||
entity("Plugin", plugin.getDescriptor().getInformation().getName()).build(),
|
||||
String.format("downloaded plugin checksum %s does not match expected %s", calculatedChecksum, expectedChecksum)
|
||||
);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getCode() {
|
||||
return "6mRuFxaWM1";
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,40 @@
|
||||
/*
|
||||
* MIT License
|
||||
*
|
||||
* Copyright (c) 2020-present Cloudogu GmbH and Contributors
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
package sonia.scm.plugin;
|
||||
|
||||
import java.nio.file.Path;
|
||||
|
||||
import static sonia.scm.ContextEntry.ContextBuilder.entity;
|
||||
|
||||
public class PluginCleanupException extends PluginInstallException {
|
||||
public PluginCleanupException(Path file) {
|
||||
super(entity("File", file.toString()).build(), "failed to cleanup, after broken installation");
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getCode() {
|
||||
return "8nRuFzjss1";
|
||||
}
|
||||
}
|
||||
@@ -21,11 +21,18 @@
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
|
||||
package sonia.scm.plugin;
|
||||
|
||||
import static sonia.scm.ContextEntry.ContextBuilder.entity;
|
||||
|
||||
public class PluginDownloadException extends PluginInstallException {
|
||||
public PluginDownloadException(String message, Throwable cause) {
|
||||
super(message, cause);
|
||||
public PluginDownloadException(AvailablePlugin plugin, Exception cause) {
|
||||
super(entity("Plugin", plugin.getDescriptor().getInformation().getName()).build(), "failed to download plugin", cause);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getCode() {
|
||||
return "9iRuFz1UB1";
|
||||
}
|
||||
}
|
||||
|
||||
@@ -21,16 +21,21 @@
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
|
||||
package sonia.scm.plugin;
|
||||
|
||||
public class PluginInstallException extends RuntimeException {
|
||||
import sonia.scm.ContextEntry;
|
||||
import sonia.scm.ExceptionWithContext;
|
||||
|
||||
public PluginInstallException(String message) {
|
||||
super(message);
|
||||
import java.util.List;
|
||||
|
||||
abstract class PluginInstallException extends ExceptionWithContext {
|
||||
|
||||
public PluginInstallException(List<ContextEntry> context, String message) {
|
||||
super(context, message);
|
||||
}
|
||||
|
||||
public PluginInstallException(String message, Throwable cause) {
|
||||
super(message, cause);
|
||||
public PluginInstallException(List<ContextEntry> context, String message, Exception cause) {
|
||||
super(context, message, cause);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -21,7 +21,7 @@
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
|
||||
package sonia.scm.plugin;
|
||||
|
||||
import com.google.common.hash.HashCode;
|
||||
@@ -64,7 +64,7 @@ class PluginInstaller {
|
||||
return new PendingPluginInstallation(plugin.install(), file);
|
||||
} catch (IOException ex) {
|
||||
cleanup(file);
|
||||
throw new PluginDownloadException("failed to download plugin", ex);
|
||||
throw new PluginDownloadException(plugin, ex);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -74,7 +74,7 @@ class PluginInstaller {
|
||||
Files.deleteIfExists(file);
|
||||
}
|
||||
} catch (IOException e) {
|
||||
throw new PluginInstallException("failed to cleanup, after broken installation");
|
||||
throw new PluginCleanupException(file);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -84,9 +84,7 @@ class PluginInstaller {
|
||||
String calculatedChecksum = hash.toString();
|
||||
if (!checksum.get().equalsIgnoreCase(calculatedChecksum)) {
|
||||
cleanup(file);
|
||||
throw new PluginChecksumMismatchException(
|
||||
String.format("downloaded plugin checksum %s does not match expected %s", calculatedChecksum, checksum.get())
|
||||
);
|
||||
throw new PluginChecksumMismatchException(plugin, calculatedChecksum, checksum.get());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -21,7 +21,7 @@
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
|
||||
package sonia.scm.update;
|
||||
|
||||
import com.github.mustachejava.DefaultMustacheFactory;
|
||||
@@ -36,6 +36,7 @@ import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import sonia.scm.event.ScmEventBus;
|
||||
import sonia.scm.lifecycle.RestartEvent;
|
||||
import sonia.scm.lifecycle.Restarter;
|
||||
import sonia.scm.update.repository.DefaultMigrationStrategyDAO;
|
||||
import sonia.scm.update.repository.MigrationStrategy;
|
||||
import sonia.scm.update.repository.V1Repository;
|
||||
@@ -65,11 +66,13 @@ class MigrationWizardServlet extends HttpServlet {
|
||||
|
||||
private final XmlRepositoryV1UpdateStep repositoryV1UpdateStep;
|
||||
private final DefaultMigrationStrategyDAO migrationStrategyDao;
|
||||
private final Restarter restarter;
|
||||
|
||||
@Inject
|
||||
MigrationWizardServlet(XmlRepositoryV1UpdateStep repositoryV1UpdateStep, DefaultMigrationStrategyDAO migrationStrategyDao) {
|
||||
MigrationWizardServlet(XmlRepositoryV1UpdateStep repositoryV1UpdateStep, DefaultMigrationStrategyDAO migrationStrategyDao, Restarter restarter) {
|
||||
this.repositoryV1UpdateStep = repositoryV1UpdateStep;
|
||||
this.migrationStrategyDao = migrationStrategyDao;
|
||||
this.restarter = restarter;
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -140,12 +143,16 @@ class MigrationWizardServlet extends HttpServlet {
|
||||
);
|
||||
|
||||
Map<String, Object> model = Collections.singletonMap("contextPath", req.getContextPath());
|
||||
|
||||
respondWithTemplate(resp, model, "templates/repository-migration-restart.mustache");
|
||||
|
||||
ThreadContext.bind(new Subject.Builder(new DefaultSecurityManager()).authenticated(false).buildSubject());
|
||||
|
||||
ScmEventBus.getInstance().post(new RestartEvent(MigrationWizardServlet.class, "wrote migration data"));
|
||||
if (restarter.isSupported()) {
|
||||
respondWithTemplate(resp, model, "templates/repository-migration-restart.mustache");
|
||||
restarter.restart(MigrationWizardServlet.class, "wrote migration data");
|
||||
} else {
|
||||
respondWithTemplate(resp, model, "templates/repository-migration-manual-restart.mustache");
|
||||
LOG.error("Restarting is not supported on this platform.");
|
||||
LOG.error("Please do a manual restart");
|
||||
}
|
||||
}
|
||||
|
||||
private List<RepositoryLineEntry> getRepositoryLineEntries() {
|
||||
|
||||
Reference in New Issue
Block a user