chaosblade-exec-jvm
chaosblade-exec-jvm copied to clipboard
The plugin loading mechanism may not be very reasonable
Issue Description
bug report
Describe what happened (or what feature you want)
In the HTTP scenario, I want to inject the rest plugin and the okhttp plugin at one time, but the break here causes only one plugin to take effect, and I don't want to load the httpclient4 plugin, but this makes it effective.
private void lazyLoadPlugin(ModelSpec modelSpec, Model model) throws ExperimentException {
PluginLifecycleListener listener = ManagerFactory.getListenerManager().getPluginLifecycleListener();
if (listener == null) {
throw new ExperimentException("can get plugin listener");
}
PluginBeans pluginBeans = ManagerFactory.getPluginManager().getPlugins(modelSpec.getTarget());
if (pluginBeans == null) {
throw new ExperimentException("can get plugin bean");
}
if (pluginBeans.isLoad()) {
return;
}
for (PluginBean pluginBean : pluginBeans.getPluginBeans()) {
String flag = model.getMatcher().get(pluginBean.getName());
if ("true".equalsIgnoreCase(flag)) {
listener.add(pluginBean);
break;
}
listener.add(pluginBean);
}
ManagerFactory.getPluginManager().setLoad(pluginBeans, modelSpec.getTarget());
}
Describe what you expected to happen
How to reproduce it (as minimally and precisely as possible)
- ./blade create http throwCustomException --exception java.lang.Exception --exception-message='模拟下游调用异常' --uri='https://www.baidu.com' --rest --httpclient4 --process=Demo
Tell us your environment
macos chaosblade-exec-jvm v1.7.2
Anything else we need to know?
I think this might be better
private void lazyLoadPlugin(ModelSpec modelSpec, Model model) throws ExperimentException {
PluginLifecycleListener listener = ManagerFactory.getListenerManager().getPluginLifecycleListener();
if (listener == null) {
throw new ExperimentException("can get plugin listener");
}
PluginBeans pluginBeans = ManagerFactory.getPluginManager().getPlugins(modelSpec.getTarget());
if (pluginBeans == null) {
throw new ExperimentException("can get plugin bean");
}
if (pluginBeans.isLoad()) {
return;
}
// 判断是否有加载条件
List<PluginBean> candidatePlugin = new ArrayList<PluginBean>();
for (PluginBean pluginBean : pluginBeans.getPluginBeans()) {
String flag = model.getMatcher().get(pluginBean.getName());
if ("true".equalsIgnoreCase(flag)) {
candidatePlugin.add(pluginBean);
}
}
// 如果没有加载条件, 则全部加载
if (candidatePlugin.isEmpty()) {
candidatePlugin.addAll(pluginBeans.getPluginBeans());
}
// 依次加载
for (PluginBean pluginBean : candidatePlugin) {
listener.add(pluginBean);
LOGGER.info("plugin [{}] load success", pluginBean.getName());
}
ManagerFactory.getPluginManager().setLoad(pluginBeans, modelSpec.getTarget());
}