Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The plugin loading mechanism may not be very reasonable #330

Open
masteryourself opened this issue Jan 23, 2024 · 0 comments
Open

The plugin loading mechanism may not be very reasonable #330

masteryourself opened this issue Jan 23, 2024 · 0 comments

Comments

@masteryourself
Copy link

masteryourself commented Jan 23, 2024

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)

  1. ./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());
    }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant