From b4764b7f21c1b9af307f7dadfa83da81dc3a9cbb Mon Sep 17 00:00:00 2001 From: TLPC <470193496@qq.com> Date: Tue, 31 Oct 2023 17:00:56 +0800 Subject: [PATCH] =?UTF-8?q?=E8=8E=B7=E5=8F=96sermant=E5=A2=9E=E5=BC=BA?= =?UTF-8?q?=E4=BF=A1=E6=81=AF=EF=BC=9A=E6=8F=92=E4=BB=B6=E3=80=81=E5=A2=9E?= =?UTF-8?q?=E5=BC=BA=E7=B1=BB=E3=80=81=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../sermant/core/AgentCoreEntrance.java | 4 + .../CheckEnhancementsCommandExecutor.java | 69 +++++++++++++ .../sermant/core/command/Command.java | 8 +- .../core/command/CommandProcessor.java | 1 + .../sermant/core/plugin/PluginManager.java | 8 ++ .../plugin/agent/info/EnhancementManager.java | 96 +++++++++++++++++++ .../transformer/ReentrantTransformer.java | 4 + 7 files changed, 189 insertions(+), 1 deletion(-) create mode 100644 sermant-agentcore/sermant-agentcore-core/src/main/java/com/huaweicloud/sermant/core/command/CheckEnhancementsCommandExecutor.java create mode 100644 sermant-agentcore/sermant-agentcore-core/src/main/java/com/huaweicloud/sermant/core/plugin/agent/info/EnhancementManager.java diff --git a/sermant-agentcore/sermant-agentcore-core/src/main/java/com/huaweicloud/sermant/core/AgentCoreEntrance.java b/sermant-agentcore/sermant-agentcore-core/src/main/java/com/huaweicloud/sermant/core/AgentCoreEntrance.java index feced95bcf..755151a693 100644 --- a/sermant-agentcore/sermant-agentcore-core/src/main/java/com/huaweicloud/sermant/core/AgentCoreEntrance.java +++ b/sermant-agentcore/sermant-agentcore-core/src/main/java/com/huaweicloud/sermant/core/AgentCoreEntrance.java @@ -32,6 +32,7 @@ import com.huaweicloud.sermant.core.plugin.agent.ByteEnhanceManager; import com.huaweicloud.sermant.core.plugin.agent.adviser.AdviserInterface; import com.huaweicloud.sermant.core.plugin.agent.adviser.AdviserScheduler; +import com.huaweicloud.sermant.core.plugin.agent.info.EnhancementManager; import com.huaweicloud.sermant.core.plugin.agent.template.DefaultAdviser; import com.huaweicloud.sermant.core.service.ServiceManager; import com.huaweicloud.sermant.god.common.SermantManager; @@ -157,6 +158,9 @@ public static void uninstall() { // 清理配置类 ConfigManager.shutdown(); + // 清理增强信息类 + EnhancementManager.shutdown(); + // 设置该artifact的Sermant状态为false,非运行状态 SermantManager.updateSermantStatus(artifactCache, false); } diff --git a/sermant-agentcore/sermant-agentcore-core/src/main/java/com/huaweicloud/sermant/core/command/CheckEnhancementsCommandExecutor.java b/sermant-agentcore/sermant-agentcore-core/src/main/java/com/huaweicloud/sermant/core/command/CheckEnhancementsCommandExecutor.java new file mode 100644 index 0000000000..48ba3a6054 --- /dev/null +++ b/sermant-agentcore/sermant-agentcore-core/src/main/java/com/huaweicloud/sermant/core/command/CheckEnhancementsCommandExecutor.java @@ -0,0 +1,69 @@ +/* + * Copyright (C) 2023-2023 Huawei Technologies Co., Ltd. All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.huaweicloud.sermant.core.command; + +import com.huaweicloud.sermant.core.common.LoggerFactory; +import com.huaweicloud.sermant.core.plugin.Plugin; +import com.huaweicloud.sermant.core.plugin.PluginManager; +import com.huaweicloud.sermant.core.plugin.agent.info.EnhancementManager; +import com.huaweicloud.sermant.core.utils.CollectionUtils; +import com.huaweicloud.sermant.core.utils.MapUtils; + +import java.util.Map; +import java.util.Set; +import java.util.logging.Level; +import java.util.logging.Logger; + +/** + * 增强信息查询命令执行器 + * + * @author tangle + * @since 2023-11-02 + */ +public class CheckEnhancementsCommandExecutor implements CommandExecutor { + private static final Logger LOGGER = LoggerFactory.getLogger(); + + @Override + public void execute(String args) { + LOGGER.log(Level.INFO, "---------- PLUGINS ----------"); + for (Map.Entry entry : PluginManager.getPluginMap().entrySet()) { + LOGGER.log(Level.INFO, entry.getKey() + ":" + entry.getValue().getVersion()); + } + LOGGER.log(Level.INFO, "---------- ENHANCEMENTS ----------"); + Map>> enhancement = EnhancementManager.getEnhancements(); + if (MapUtils.isEmpty(enhancement)) { + LOGGER.log(Level.INFO, "No enhancement currently."); + return; + } + for (Map.Entry>> enhancemantEntry : enhancement.entrySet()) { + Map> methodDescMap = enhancemantEntry.getValue(); + if (MapUtils.isEmpty(methodDescMap)) { + continue; + } + String pluginInfo = enhancemantEntry.getKey(); + LOGGER.log(Level.INFO, pluginInfo); + for (Map.Entry> methodDescEntry : methodDescMap.entrySet()) { + Set interceptorSet = methodDescEntry.getValue(); + if (CollectionUtils.isEmpty(interceptorSet)) { + continue; + } + String methodDesc = methodDescEntry.getKey(); + LOGGER.log(Level.INFO, methodDesc + " " + interceptorSet); + } + } + } +} diff --git a/sermant-agentcore/sermant-agentcore-core/src/main/java/com/huaweicloud/sermant/core/command/Command.java b/sermant-agentcore/sermant-agentcore-core/src/main/java/com/huaweicloud/sermant/core/command/Command.java index 0e6384a182..b6cdf1f511 100644 --- a/sermant-agentcore/sermant-agentcore-core/src/main/java/com/huaweicloud/sermant/core/command/Command.java +++ b/sermant-agentcore/sermant-agentcore-core/src/main/java/com/huaweicloud/sermant/core/command/Command.java @@ -34,7 +34,13 @@ public enum Command { /** * 卸载插件指令 */ - UNINSTALL_PLUGINS("UNINSTALL-PLUGINS"); + UNINSTALL_PLUGINS("UNINSTALL-PLUGINS"), + + /** + * 增强查询指令 + */ + CHECK_ENHANCEMENT("CHECK-ENHANCEMENT"); + private final String value; Command(String value) { diff --git a/sermant-agentcore/sermant-agentcore-core/src/main/java/com/huaweicloud/sermant/core/command/CommandProcessor.java b/sermant-agentcore/sermant-agentcore-core/src/main/java/com/huaweicloud/sermant/core/command/CommandProcessor.java index d685226a24..615ce5af6f 100644 --- a/sermant-agentcore/sermant-agentcore-core/src/main/java/com/huaweicloud/sermant/core/command/CommandProcessor.java +++ b/sermant-agentcore/sermant-agentcore-core/src/main/java/com/huaweicloud/sermant/core/command/CommandProcessor.java @@ -43,6 +43,7 @@ public class CommandProcessor { COMMAND_EXECUTOR_MAP.put(Command.INSTALL_PLUGINS.getValue(), new PluginsInstallCommandExecutor()); COMMAND_EXECUTOR_MAP.put(Command.UNINSTALL_AGENT.getValue(), new AgentUnInstallCommandExecutor()); COMMAND_EXECUTOR_MAP.put(Command.UNINSTALL_PLUGINS.getValue(), new PluginsUnInstallCommandExecutor()); + COMMAND_EXECUTOR_MAP.put(Command.CHECK_ENHANCEMENT.getValue(), new CheckEnhancementsCommandExecutor()); } /** diff --git a/sermant-agentcore/sermant-agentcore-core/src/main/java/com/huaweicloud/sermant/core/plugin/PluginManager.java b/sermant-agentcore/sermant-agentcore-core/src/main/java/com/huaweicloud/sermant/core/plugin/PluginManager.java index 209db2bc30..b0b728da1a 100644 --- a/sermant-agentcore/sermant-agentcore-core/src/main/java/com/huaweicloud/sermant/core/plugin/PluginManager.java +++ b/sermant-agentcore/sermant-agentcore-core/src/main/java/com/huaweicloud/sermant/core/plugin/PluginManager.java @@ -23,6 +23,7 @@ import com.huaweicloud.sermant.core.exception.SchemaException; import com.huaweicloud.sermant.core.plugin.agent.ByteEnhanceManager; import com.huaweicloud.sermant.core.plugin.agent.adviser.AdviserScheduler; +import com.huaweicloud.sermant.core.plugin.agent.info.EnhancementManager; import com.huaweicloud.sermant.core.plugin.agent.interceptor.Interceptor; import com.huaweicloud.sermant.core.plugin.agent.template.BaseAdviseHandler; import com.huaweicloud.sermant.core.plugin.classloader.PluginClassLoader; @@ -108,6 +109,9 @@ public static void uninstall(Set pluginNames) { // 取消字节码增强 ByteEnhanceManager.unEnhanceDynamicPlugin(plugin); + // 清除增强信息 + EnhancementManager.removePluginEnhancements(plugin); + // 关闭插件服务 PluginServiceManager.shutdownPluginServices(plugin); @@ -183,6 +187,10 @@ public static void initPlugins(Set pluginNames, boolean isDynamic) { } } + public static Map getPluginMap() { + return PLUGIN_MAP; + } + private static void doInitPlugin(Plugin plugin) { loadPluginLibs(plugin); loadServiceLibs(plugin); diff --git a/sermant-agentcore/sermant-agentcore-core/src/main/java/com/huaweicloud/sermant/core/plugin/agent/info/EnhancementManager.java b/sermant-agentcore/sermant-agentcore-core/src/main/java/com/huaweicloud/sermant/core/plugin/agent/info/EnhancementManager.java new file mode 100644 index 0000000000..db096502b6 --- /dev/null +++ b/sermant-agentcore/sermant-agentcore-core/src/main/java/com/huaweicloud/sermant/core/plugin/agent/info/EnhancementManager.java @@ -0,0 +1,96 @@ +/* + * Copyright (C) 2023-2023 Huawei Technologies Co., Ltd. All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.huaweicloud.sermant.core.plugin.agent.info; + +import com.huaweicloud.sermant.core.plugin.Plugin; +import com.huaweicloud.sermant.core.plugin.agent.interceptor.Interceptor; +import com.huaweicloud.sermant.core.utils.StringUtils; + +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Set; + +/** + * 存储增强信息的静态类 + * + * @author tangle + * @since 2023-11-02 + */ +public class EnhancementManager { + private static final Map>> ENHANCEMENTS = new HashMap<>(); + + private EnhancementManager() { + } + + public static Map>> getEnhancements() { + return ENHANCEMENTS; + } + + /** + * 添加拦截器信息 + * + * @param plugin 插件 + * @param interceptorList 拦截器列表 + * @param classLoader 类加载器 + * @param methodDesc 被增强方法的信息 + */ + public static void addEnhancements(Plugin plugin, List interceptorList, ClassLoader classLoader, + String methodDesc) { + String enhancementKey = combinePluginInfo(plugin); + if (!StringUtils.isEmpty(methodDesc)) { + Map> methodDescMap = ENHANCEMENTS.computeIfAbsent(enhancementKey, + key -> new HashMap<>()); + String methodDescKey = combineEnhanceInfo(methodDesc, classLoader); + Set interceptorSet = methodDescMap.computeIfAbsent(methodDescKey, key -> new HashSet<>()); + for (Interceptor interceptor : interceptorList) { + interceptorSet.add(interceptor.getClass().getCanonicalName()); + } + } + } + + /** + * 卸载插件时清除该插件的增强信息 + * + * @param plugin 插件 + */ + public static void removePluginEnhancements(Plugin plugin) { + ENHANCEMENTS.remove(combinePluginInfo(plugin)); + } + + /** + * 清理缓存的增强信息 + */ + public static void shutdown() { + ENHANCEMENTS.clear(); + } + + /** + * 拼接插件信息 + */ + private static String combinePluginInfo(Plugin plugin) { + return plugin.getName() + ":" + plugin.getVersion(); + } + + /** + * 拼接增强信息 + */ + private static String combineEnhanceInfo(String methodDesc, ClassLoader classLoader) { + return methodDesc + "@" + classLoader; + } +} diff --git a/sermant-agentcore/sermant-agentcore-core/src/main/java/com/huaweicloud/sermant/core/plugin/agent/transformer/ReentrantTransformer.java b/sermant-agentcore/sermant-agentcore-core/src/main/java/com/huaweicloud/sermant/core/plugin/agent/transformer/ReentrantTransformer.java index 67ae1013af..33d5a7175c 100644 --- a/sermant-agentcore/sermant-agentcore-core/src/main/java/com/huaweicloud/sermant/core/plugin/agent/transformer/ReentrantTransformer.java +++ b/sermant-agentcore/sermant-agentcore-core/src/main/java/com/huaweicloud/sermant/core/plugin/agent/transformer/ReentrantTransformer.java @@ -19,8 +19,10 @@ import com.huaweicloud.sermant.core.plugin.Plugin; import com.huaweicloud.sermant.core.plugin.agent.adviser.AdviserScheduler; import com.huaweicloud.sermant.core.plugin.agent.declarer.InterceptDeclarer; +import com.huaweicloud.sermant.core.plugin.agent.info.EnhancementManager; import com.huaweicloud.sermant.core.plugin.agent.interceptor.Interceptor; import com.huaweicloud.sermant.core.plugin.agent.template.BaseAdviseHandler; +import com.huaweicloud.sermant.core.plugin.agent.template.MethodKeyCreator; import net.bytebuddy.asm.Advice; import net.bytebuddy.description.method.MethodDescription.InDefinedShape; @@ -69,6 +71,8 @@ protected Builder resolve(Builder builder, InDefinedShape methodDesc, List createdInterceptorForAdviceKey.add(interceptor.getClass().getCanonicalName()); } } + EnhancementManager.addEnhancements(plugin, interceptors, classLoader, + MethodKeyCreator.getMethodDescKey(methodDesc)); if (checkAdviceLock(adviceKey)) { return builder.visit(Advice.to(templateCls).on(ElementMatchers.is(methodDesc))); }