Skip to content

Commit

Permalink
Fix the problem that some plugins could not be used after upgrading d…
Browse files Browse the repository at this point in the history
…ependent plugin (#5855)

#### What type of PR is this?

/kind bug
/area plugin
/area core

#### What this PR does / why we need it:

This PR resolves the problem that some plugins could not be used after upgrading dependent plugin.

#### Which issue(s) this PR fixes:

Fixes #5615

#### Special notes for your reviewer:

1. Install plugin [app-store](https://www.halo.run/store/apps/app-VYJbF)
2. Install plugin [backup](https://www.halo.run/store/apps/app-dHakX) and activate it
3. Disable plugin app-store
4. Check the features of plugin backup
5. Enable plugin app-store
6. Check the features of plugin backup
7. Upgrade plugin app-store with the any versions
8. Check the features of plugin backup

#### Does this PR introduce a user-facing change?

```release-note
修复因升级应用市场插件导致部分插件意外停止的问题
```
  • Loading branch information
JohnNiang authored May 7, 2024
1 parent 9b3f00d commit fe809c1
Show file tree
Hide file tree
Showing 4 changed files with 251 additions and 91 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import static run.halo.app.plugin.PluginUtils.generateFileName;
import static run.halo.app.plugin.PluginUtils.isDevelopmentMode;

import com.fasterxml.jackson.core.type.TypeReference;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.MalformedURLException;
Expand All @@ -21,17 +22,24 @@
import java.nio.file.Path;
import java.nio.file.Paths;
import java.time.Clock;
import java.time.Duration;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Objects;
import java.util.Set;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.pf4j.PluginManager;
import org.pf4j.PluginRuntimeException;
import org.pf4j.PluginState;
import org.pf4j.PluginStateEvent;
import org.pf4j.PluginStateListener;
import org.pf4j.PluginWrapper;
import org.pf4j.RuntimeMode;
import org.springframework.core.io.DefaultResourceLoader;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ResourceUtils;
import org.springframework.web.util.UriComponentsBuilder;
import run.halo.app.core.extension.Plugin;
Expand All @@ -50,10 +58,13 @@
import run.halo.app.extension.controller.RequeueException;
import run.halo.app.infra.Condition;
import run.halo.app.infra.ConditionStatus;
import run.halo.app.infra.utils.JsonParseException;
import run.halo.app.infra.utils.JsonUtils;
import run.halo.app.infra.utils.PathUtils;
import run.halo.app.infra.utils.YamlUnstructuredLoader;
import run.halo.app.plugin.PluginConst;
import run.halo.app.plugin.PluginProperties;
import run.halo.app.plugin.SpringPluginManager;

/**
* Plugin reconciler.
Expand All @@ -66,19 +77,23 @@
@Component
public class PluginReconciler implements Reconciler<Request> {
private static final String FINALIZER_NAME = "plugin-protection";
private static final String DEPENDENTS_ANNO_KEY = "plugin.halo.run/dependents-snapshot";
private final ExtensionClient client;
private final PluginManager pluginManager;
private final SpringPluginManager pluginManager;

private final PluginProperties pluginProperties;

private Clock clock;

public PluginReconciler(ExtensionClient client, PluginManager pluginManager,
public PluginReconciler(ExtensionClient client, SpringPluginManager pluginManager,
PluginProperties pluginProperties) {
this.client = client;
this.pluginManager = pluginManager;
this.pluginProperties = pluginProperties;
this.clock = Clock.systemUTC();

this.pluginManager.addPluginStateListener(new PluginStartedListener());
this.pluginManager.addPluginStateListener(new PluginStoppedListener());
}

/**
Expand All @@ -95,6 +110,11 @@ public Result reconcile(Request request) {
return client.fetch(Plugin.class, request.name())
.map(plugin -> {
if (ExtensionUtil.isDeleted(plugin)) {
if (!checkDependents(plugin)) {
client.update(plugin);
// Check dependents every 10 seconds
return Result.requeue(Duration.ofSeconds(10));
}
// CleanUp resources and remove finalizer.
if (removeFinalizers(plugin.getMetadata(), Set.of(FINALIZER_NAME))) {
cleanupResources(plugin);
Expand All @@ -117,10 +137,10 @@ public Result reconcile(Request request) {

if (requestToEnable(plugin)) {
// Start
startPlugin(plugin);
enablePlugin(plugin);
} else {
// stop the plugin and disable it
stopAndDisablePlugin(plugin);
disablePlugin(plugin);
}
} catch (Throwable t) {
// populate condition
Expand All @@ -145,6 +165,28 @@ public Result reconcile(Request request) {
.orElseGet(Result::doNotRetry);
}

private boolean checkDependents(Plugin plugin) {
var pluginId = plugin.getMetadata().getName();
var dependents = pluginManager.getDependents(pluginId);
if (CollectionUtils.isEmpty(dependents)) {
return true;
}
var status = plugin.statusNonNull();
var condition = Condition.builder()
.type(PluginState.FAILED.toString())
.reason("DependentsExist")
.message(
"The plugin has dependents %s, please delete them first."
.formatted(dependents.stream().map(PluginWrapper::getPluginId).toList())
)
.status(ConditionStatus.FALSE)
.lastTransitionTime(clock.instant())
.build();
nullSafeConditions(status).addAndEvictFIFO(condition);
status.setPhase(Plugin.Phase.FAILED);
return false;
}

private void syncPluginState(Plugin plugin) {
var pluginName = plugin.getMetadata().getName();
var p = pluginManager.getPlugin(pluginName);
Expand Down Expand Up @@ -191,32 +233,87 @@ private void cleanupResources(Plugin plugin) {
}
}

private void startPlugin(Plugin plugin) {
private void enablePlugin(Plugin plugin) {
// start the plugin
var pluginName = plugin.getMetadata().getName();
var wrapper = pluginManager.getPlugin(pluginName);
log.info("Starting plugin {}", pluginName);
plugin.statusNonNull().setPhase(Plugin.Phase.STARTING);
if (!PluginState.STARTED.equals(wrapper.getPluginState())) {
var pluginState = pluginManager.startPlugin(pluginName);
if (!PluginState.STARTED.equals(pluginState)) {
throw new IllegalStateException("Failed to start plugin " + pluginName);
}
plugin.statusNonNull().setLastStartTime(clock.instant());
var condition = Condition.builder()
.type(PluginState.STARTED.toString())
.reason(PluginState.STARTED.toString())
.message("Started successfully")
.lastTransitionTime(clock.instant())
.status(ConditionStatus.TRUE)
.build();
nullSafeConditions(plugin.statusNonNull()).addAndEvictFIFO(condition);
var pluginState = pluginManager.startPlugin(pluginName);
if (!PluginState.STARTED.equals(pluginState)) {
throw new IllegalStateException("Failed to start plugin " + pluginName);
}

var dependents = getAndRemoveDependents(plugin);
log.info("Starting dependents {} for plugin {}", dependents, pluginName);
dependents.stream()
.sorted(Comparator.reverseOrder())
.forEach(dependent -> {
if (pluginManager.getPlugin(dependent) != null) {
pluginManager.startPlugin(dependent);
}
});
log.info("Started dependents {} for plugin {}", dependents, pluginName);

plugin.statusNonNull().setLastStartTime(clock.instant());
var condition = Condition.builder()
.type(PluginState.STARTED.toString())
.reason(PluginState.STARTED.toString())
.message("Started successfully")
.lastTransitionTime(clock.instant())
.status(ConditionStatus.TRUE)
.build();
nullSafeConditions(plugin.statusNonNull()).addAndEvictFIFO(condition);
plugin.statusNonNull().setPhase(Plugin.Phase.STARTED);

log.info("Started plugin {}", pluginName);
}

private void stopAndDisablePlugin(Plugin plugin) {
private List<String> getAndRemoveDependents(Plugin plugin) {
var pluginName = plugin.getMetadata().getName();
var annotations = plugin.getMetadata().getAnnotations();
if (annotations == null) {
return List.of();
}
var dependentsAnno = annotations.remove(DEPENDENTS_ANNO_KEY);
List<String> dependents = List.of();
if (StringUtils.isNotBlank(dependentsAnno)) {
try {
dependents = JsonUtils.jsonToObject(dependentsAnno, new TypeReference<>() {
});
} catch (JsonParseException ignored) {
log.error("Failed to parse dependents annotation {} for plugin {}",
dependentsAnno, pluginName);
// Keep going to start the plugin.
}
}
return dependents;
}

private void setDependents(Plugin plugin) {
var pluginName = plugin.getMetadata().getName();
var annotations = plugin.getMetadata().getAnnotations();
if (annotations == null) {
annotations = new HashMap<>();
plugin.getMetadata().setAnnotations(annotations);
}
if (!annotations.containsKey(DEPENDENTS_ANNO_KEY)) {
// get dependents
var dependents = pluginManager.getDependents(pluginName)
.stream()
.filter(pluginWrapper ->
Objects.equals(PluginState.STARTED, pluginWrapper.getPluginState())
)
.map(PluginWrapper::getPluginId)
.toList();

annotations.put(DEPENDENTS_ANNO_KEY, JsonUtils.objectToJson(dependents));
}
}

private void disablePlugin(Plugin plugin) {
var pluginName = plugin.getMetadata().getName();
if (pluginManager.getPlugin(pluginName) != null) {
setDependents(plugin);
pluginManager.disablePlugin(pluginName);
}
plugin.statusNonNull().setPhase(Plugin.Phase.DISABLED);
Expand Down Expand Up @@ -284,29 +381,29 @@ private void resolveStaticResources(Plugin plugin) {
}

private void loadOrReload(Plugin plugin) {
// TODO Try to check dependencies before.
var pluginName = plugin.getMetadata().getName();
try {
var p = pluginManager.getPlugin(pluginName);
var requestToReload = requestToReload(plugin);
if (requestToReload) {
log.info("Unloading plugin {}", pluginName);
if (p != null) {
pluginManager.unloadPlugin(pluginName);
}
}
if (p == null || requestToReload) {
log.info("Loading plugin {}", pluginName);
var p = pluginManager.getPlugin(pluginName);
var requestToReload = requestToReload(plugin);
if (requestToReload) {
if (p != null) {
var loadLocation = plugin.getStatus().getLoadLocation();
pluginManager.loadPlugin(Paths.get(loadLocation));
log.info("Loaded plugin {}", pluginName);
}
} catch (Throwable t) {
// unload the plugin
if (pluginManager.getPlugin(pluginName) != null) {
pluginManager.unloadPlugin(pluginName);
setDependents(plugin);
var unloaded = pluginManager.reloadPlugin(pluginName, Paths.get(loadLocation));
if (!unloaded) {
throw new PluginRuntimeException("Failed to reload plugin " + pluginName);
}
p = pluginManager.getPlugin(pluginName);
}
throw t;
}
if (p != null && pluginManager.getUnresolvedPlugins().contains(p)) {
pluginManager.unloadPlugin(pluginName);
p = null;
}
if (p == null) {
var loadLocation = plugin.getStatus().getLoadLocation();
log.info("Loading plugin {} from {}", pluginName, loadLocation);
pluginManager.loadPlugin(Paths.get(loadLocation));
log.info("Loaded plugin {} from {}", pluginName, loadLocation);
}
}

Expand Down Expand Up @@ -480,4 +577,38 @@ static String buildReverseProxyName(String pluginName) {
return pluginName + "-system-generated-reverse-proxy";
}

public class PluginStartedListener implements PluginStateListener {

@Override
public void pluginStateChanged(PluginStateEvent event) {
if (PluginState.STARTED.equals(event.getPluginState())) {
var pluginId = event.getPlugin().getPluginId();
client.fetch(Plugin.class, pluginId)
.ifPresent(plugin -> {
if (!Objects.equals(true, plugin.getSpec().getEnabled())) {
plugin.getSpec().setEnabled(true);
client.update(plugin);
}
});
}
}
}

public class PluginStoppedListener implements PluginStateListener {

@Override
public void pluginStateChanged(PluginStateEvent event) {
if (PluginState.STOPPED.equals(event.getPluginState())) {
var pluginId = event.getPlugin().getPluginId();
client.fetch(Plugin.class, pluginId)
.ifPresent(plugin -> {
if (!requestToReload(plugin)
&& Objects.equals(true, plugin.getSpec().getEnabled())) {
plugin.getSpec().setEnabled(false);
client.update(plugin);
}
});
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@

import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Objects;
import java.util.Stack;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.pf4j.CompoundPluginLoader;
Expand All @@ -18,6 +22,7 @@
import org.pf4j.PluginFactory;
import org.pf4j.PluginLoader;
import org.pf4j.PluginRepository;
import org.pf4j.PluginRuntimeException;
import org.pf4j.PluginStatusProvider;
import org.pf4j.PluginWrapper;
import org.springframework.context.ApplicationContext;
Expand Down Expand Up @@ -157,4 +162,50 @@ public ApplicationContext getSharedContext() {
return sharedContext.get();
}

public boolean reloadPlugin(String pluginId, Path loadLocation) {
log.info("Reloading plugin {} from {}", pluginId, loadLocation);

var dependents = getDependents(pluginId);
dependents.forEach(plugin -> unloadPlugin(plugin.getPluginId(), false));

var unloaded = unloadPlugin(pluginId, false);
if (!unloaded) {
return false;
}

// load the root plugin
var loadedPluginId = loadPlugin(loadLocation);

if (!Objects.equals(pluginId, loadedPluginId)) {
throw new PluginRuntimeException("""
The plugin {} is reloaded successfully, \
but the plugin id is different from the original one.
""");
}

// load all dependents with reverse order
dependents.stream()
.map(PluginWrapper::getPluginPath)
.sorted(Comparator.reverseOrder())
.forEach(this::loadPlugin);

log.info("Reloaded plugin {} from {}", loadedPluginId, loadLocation);
return true;
}

@Override
public List<PluginWrapper> getDependents(String pluginId) {
var dependents = new ArrayList<PluginWrapper>();
var stack = new Stack<String>();
dependencyResolver.getDependents(pluginId).forEach(stack::push);
while (!stack.isEmpty()) {
var dependent = stack.pop();
var pluginWrapper = getPlugin(dependent);
if (pluginWrapper != null) {
dependents.add(pluginWrapper);
dependencyResolver.getDependents(dependent).forEach(stack::push);
}
}
return dependents;
}
}
Loading

0 comments on commit fe809c1

Please sign in to comment.