You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by yx...@apache.org on 2022/12/15 12:24:02 UTC

[shardingsphere] branch master updated: Refactor AgentAdvisors (#22885)

This is an automated email from the ASF dual-hosted git repository.

yx9o pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/shardingsphere.git


The following commit(s) were added to refs/heads/master by this push:
     new fe00d7fa846 Refactor AgentAdvisors (#22885)
fe00d7fa846 is described below

commit fe00d7fa8460297efb57ca6f0a4503e8cf873e12
Author: Liang Zhang <zh...@apache.org>
AuthorDate: Thu Dec 15 20:23:53 2022 +0800

    Refactor AgentAdvisors (#22885)
---
 .../agent/bootstrap/ShardingSphereAgent.java          |  7 ++++---
 .../yaml/swapper/YamlAgentConfigurationSwapper.java   | 10 +++++-----
 .../agent/core/plugin/advisor/AgentAdvisors.java      | 19 +++----------------
 .../agent/core/plugin/advisor/AgentAdvisorsTest.java  |  2 +-
 .../agent/core/transformer/AgentTransformerTest.java  |  2 +-
 5 files changed, 14 insertions(+), 26 deletions(-)

diff --git a/agent/bootstrap/src/main/java/org/apache/shardingsphere/agent/bootstrap/ShardingSphereAgent.java b/agent/bootstrap/src/main/java/org/apache/shardingsphere/agent/bootstrap/ShardingSphereAgent.java
index 51f35d384e9..d2282416809 100644
--- a/agent/bootstrap/src/main/java/org/apache/shardingsphere/agent/bootstrap/ShardingSphereAgent.java
+++ b/agent/bootstrap/src/main/java/org/apache/shardingsphere/agent/bootstrap/ShardingSphereAgent.java
@@ -36,6 +36,7 @@ import org.apache.shardingsphere.agent.core.transformer.AgentTransformer;
 
 import java.io.IOException;
 import java.lang.instrument.Instrumentation;
+import java.util.Collection;
 import java.util.Map;
 
 /**
@@ -55,14 +56,14 @@ public final class ShardingSphereAgent {
         AgentConfiguration agentConfig = AgentConfigurationLoader.load();
         AgentConfigurationRegistry.INSTANCE.put(agentConfig);
         boolean isEnhancedForProxy = isEnhancedForProxy();
-        setUpAgentBuilder(instrumentation, loadAgentAdvisors(isEnhancedForProxy), isEnhancedForProxy);
+        setUpAgentBuilder(instrumentation, loadAgentAdvisors(agentConfig.getPlugins().keySet(), isEnhancedForProxy), isEnhancedForProxy);
         if (isEnhancedForProxy) {
             setupPluginBootService(agentConfig.getPlugins());
         }
     }
     
-    private static AgentAdvisors loadAgentAdvisors(final boolean isEnhancedForProxy) throws IOException {
-        AgentAdvisors result = new AgentAdvisors(new AgentPluginLoader().load());
+    private static AgentAdvisors loadAgentAdvisors(final Collection<String> pluginTypes, final boolean isEnhancedForProxy) throws IOException {
+        AgentAdvisors result = new AgentAdvisors(pluginTypes, new AgentPluginLoader().load());
         result.setEnhancedForProxy(isEnhancedForProxy);
         return result;
     }
diff --git a/agent/core/src/main/java/org/apache/shardingsphere/agent/core/config/yaml/swapper/YamlAgentConfigurationSwapper.java b/agent/core/src/main/java/org/apache/shardingsphere/agent/core/config/yaml/swapper/YamlAgentConfigurationSwapper.java
index 0c7d39ae1ca..e118a01c66b 100644
--- a/agent/core/src/main/java/org/apache/shardingsphere/agent/core/config/yaml/swapper/YamlAgentConfigurationSwapper.java
+++ b/agent/core/src/main/java/org/apache/shardingsphere/agent/core/config/yaml/swapper/YamlAgentConfigurationSwapper.java
@@ -42,14 +42,14 @@ public final class YamlAgentConfigurationSwapper {
      * @return agent configuration
      */
     public static AgentConfiguration swap(final YamlAgentConfiguration yamlConfig) {
-        Map<String, PluginConfiguration> configurationMap = new LinkedHashMap<>();
+        Map<String, PluginConfiguration> pluginConfigs = new LinkedHashMap<>();
         YamlPluginCategoryConfiguration plugins = yamlConfig.getPlugins();
         if (null != plugins) {
-            configurationMap.putAll(transformPluginConfigurationMap(plugins.getLogging()));
-            configurationMap.putAll(transformPluginConfigurationMap(plugins.getMetrics()));
-            configurationMap.putAll(transformPluginConfigurationMap(plugins.getTracing()));
+            pluginConfigs.putAll(transformPluginConfigurationMap(plugins.getLogging()));
+            pluginConfigs.putAll(transformPluginConfigurationMap(plugins.getMetrics()));
+            pluginConfigs.putAll(transformPluginConfigurationMap(plugins.getTracing()));
         }
-        return new AgentConfiguration(configurationMap);
+        return new AgentConfiguration(pluginConfigs);
     }
     
     private static Map<String, PluginConfiguration> transformPluginConfigurationMap(final Map<String, YamlPluginConfiguration> yamlConfigurationMap) {
diff --git a/agent/core/src/main/java/org/apache/shardingsphere/agent/core/plugin/advisor/AgentAdvisors.java b/agent/core/src/main/java/org/apache/shardingsphere/agent/core/plugin/advisor/AgentAdvisors.java
index b299194d23d..b91428a0297 100644
--- a/agent/core/src/main/java/org/apache/shardingsphere/agent/core/plugin/advisor/AgentAdvisors.java
+++ b/agent/core/src/main/java/org/apache/shardingsphere/agent/core/plugin/advisor/AgentAdvisors.java
@@ -23,16 +23,13 @@ import net.bytebuddy.description.type.TypeDescription;
 import net.bytebuddy.matcher.ElementMatcher;
 import net.bytebuddy.matcher.ElementMatcher.Junction;
 import org.apache.shardingsphere.agent.config.advisor.ClassAdvisorConfiguration;
-import org.apache.shardingsphere.agent.config.plugin.AgentConfiguration;
 import org.apache.shardingsphere.agent.core.classloader.AgentClassLoader;
-import org.apache.shardingsphere.agent.core.config.registry.AgentConfigurationRegistry;
 import org.apache.shardingsphere.agent.core.plugin.PluginJar;
 import org.apache.shardingsphere.agent.core.spi.PluginServiceLoader;
 import org.apache.shardingsphere.agent.spi.advisor.AdvisorDefinitionService;
 
 import java.util.Collection;
 import java.util.HashMap;
-import java.util.HashSet;
 import java.util.Map;
 import java.util.function.Function;
 import java.util.stream.Collectors;
@@ -47,14 +44,13 @@ public final class AgentAdvisors {
     @Setter
     private boolean isEnhancedForProxy = true;
     
-    public AgentAdvisors(final Collection<PluginJar> pluginJars) {
+    public AgentAdvisors(final Collection<String> pluginTypes, final Collection<PluginJar> pluginJars) {
         AgentClassLoader.init(pluginJars);
-        advisorConfigs = getAllAdvisorConfigurations(AgentClassLoader.getClassLoader());
+        advisorConfigs = getAllAdvisorConfigurations(pluginTypes, AgentClassLoader.getClassLoader());
     }
     
-    private Map<String, ClassAdvisorConfiguration> getAllAdvisorConfigurations(final ClassLoader classLoader) {
+    private Map<String, ClassAdvisorConfiguration> getAllAdvisorConfigurations(final Collection<String> pluginTypes, final ClassLoader classLoader) {
         Map<String, ClassAdvisorConfiguration> result = new HashMap<>();
-        Collection<String> pluginTypes = getPluginTypes();
         for (AdvisorDefinitionService each : PluginServiceLoader.newServiceInstances(AdvisorDefinitionService.class, classLoader)) {
             if (pluginTypes.contains(each.getType())) {
                 Collection<ClassAdvisorConfiguration> advisorConfigs = isEnhancedForProxy ? each.getProxyAdvisorConfigurations() : each.getJDBCAdvisorConfigurations();
@@ -64,15 +60,6 @@ public final class AgentAdvisors {
         return ImmutableMap.<String, ClassAdvisorConfiguration>builder().putAll(result).build();
     }
     
-    private Collection<String> getPluginTypes() {
-        AgentConfiguration agentConfig = AgentConfigurationRegistry.INSTANCE.get(AgentConfiguration.class);
-        Collection<String> result = new HashSet<>();
-        if (null != agentConfig && null != agentConfig.getPlugins()) {
-            result.addAll(agentConfig.getPlugins().keySet());
-        }
-        return result;
-    }
-    
     /**
      * To find all intercepting target classes then to build type matcher.
      *
diff --git a/agent/core/src/test/java/org/apache/shardingsphere/agent/core/plugin/advisor/AgentAdvisorsTest.java b/agent/core/src/test/java/org/apache/shardingsphere/agent/core/plugin/advisor/AgentAdvisorsTest.java
index 57eec1add30..b3ef435f572 100644
--- a/agent/core/src/test/java/org/apache/shardingsphere/agent/core/plugin/advisor/AgentAdvisorsTest.java
+++ b/agent/core/src/test/java/org/apache/shardingsphere/agent/core/plugin/advisor/AgentAdvisorsTest.java
@@ -43,7 +43,7 @@ import static org.junit.Assert.assertTrue;
 
 public final class AgentAdvisorsTest {
     
-    private static final AgentAdvisors AGENT_ADVISORS = new AgentAdvisors(Collections.emptyList());
+    private static final AgentAdvisors AGENT_ADVISORS = new AgentAdvisors(Collections.emptySet(), Collections.emptyList());
     
     private static final TypePool POOL = TypePool.Default.ofSystemLoader();
     
diff --git a/agent/core/src/test/java/org/apache/shardingsphere/agent/core/transformer/AgentTransformerTest.java b/agent/core/src/test/java/org/apache/shardingsphere/agent/core/transformer/AgentTransformerTest.java
index 0bf5d5c58d4..81dc6903d2b 100644
--- a/agent/core/src/test/java/org/apache/shardingsphere/agent/core/transformer/AgentTransformerTest.java
+++ b/agent/core/src/test/java/org/apache/shardingsphere/agent/core/transformer/AgentTransformerTest.java
@@ -57,7 +57,7 @@ import static org.junit.Assert.assertArrayEquals;
 
 public final class AgentTransformerTest {
     
-    private static final AgentAdvisors AGENT_ADVISORS = new AgentAdvisors(Collections.emptyList());
+    private static final AgentAdvisors AGENT_ADVISORS = new AgentAdvisors(Collections.emptySet(), Collections.emptyList());
     
     private static ResettableClassFileTransformer byteBuddyAgent;