You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by xi...@apache.org on 2020/12/05 06:51:26 UTC

[shardingsphere] branch master updated: Enable to activate specify plugin by config file (#8511)

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

xiaoyu 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 7148120  Enable to activate specify plugin by config file (#8511)
7148120 is described below

commit 71481206dd42d1796fdb5313dcf9fca453491e88
Author: Daming <zt...@foxmail.com>
AuthorDate: Sat Dec 5 14:50:59 2020 +0800

    Enable to activate specify plugin by config file (#8511)
    
    * Enable to activate specify plugin by config file
    
    * fix checkstyle
---
 .../agent/core/config/AgentConfiguration.java       |  4 ++++
 .../agent/core/plugin/PluginDefinition.java         | 21 ++++++++++++++++++---
 .../agent/core/plugin/PluginLoader.java             | 16 +++++++++++++++-
 .../metrics/bootstrap/MetricsPluginDefinition.java  |  4 ++++
 .../agent/plugin/trace/SamplePluginDefinition.java  |  4 ++++
 5 files changed, 45 insertions(+), 4 deletions(-)

diff --git a/shardingsphere-agent/shardingsphere-agent-core/src/main/java/org/apache/shardingsphere/agent/core/config/AgentConfiguration.java b/shardingsphere-agent/shardingsphere-agent-core/src/main/java/org/apache/shardingsphere/agent/core/config/AgentConfiguration.java
index a689d62..8b5091a 100644
--- a/shardingsphere-agent/shardingsphere-agent-core/src/main/java/org/apache/shardingsphere/agent/core/config/AgentConfiguration.java
+++ b/shardingsphere-agent/shardingsphere-agent-core/src/main/java/org/apache/shardingsphere/agent/core/config/AgentConfiguration.java
@@ -19,6 +19,8 @@ package org.apache.shardingsphere.agent.core.config;
 
 import lombok.Data;
 
+import java.util.List;
+
 /**
  * Agent configuration.
  */
@@ -29,6 +31,8 @@ public class AgentConfiguration {
     
     private MetricsConfiguration metrics;
     
+    private List<String> activatedPlugins;
+    
     @Data
     public static class MetricsConfiguration {
         
diff --git a/shardingsphere-agent/shardingsphere-agent-core/src/main/java/org/apache/shardingsphere/agent/core/plugin/PluginDefinition.java b/shardingsphere-agent/shardingsphere-agent-core/src/main/java/org/apache/shardingsphere/agent/core/plugin/PluginDefinition.java
index f926296..d6efcb3 100644
--- a/shardingsphere-agent/shardingsphere-agent-core/src/main/java/org/apache/shardingsphere/agent/core/plugin/PluginDefinition.java
+++ b/shardingsphere-agent/shardingsphere-agent-core/src/main/java/org/apache/shardingsphere/agent/core/plugin/PluginDefinition.java
@@ -33,6 +33,12 @@ public abstract class PluginDefinition {
     private final Map<String, PluginAdviceDefinition.Builder> defineMap = Maps.newHashMap();
     
     private final List<Class<? extends Service>> services = Lists.newArrayList();
+
+    private final String pluginName;
+
+    public PluginDefinition(final String pluginName) {
+        this.pluginName = pluginName;
+    }
     
     /**
      * Define the plugin here.
@@ -51,7 +57,7 @@ public abstract class PluginDefinition {
     /**
      * Register service to agent.
      *
-     * @param service the class of Service.
+     * @param service the class of Service
      */
     protected void registerService(final Class<? extends Service> service) {
         services.add(service);
@@ -60,7 +66,7 @@ public abstract class PluginDefinition {
     /**
      * To build Plugin definition.
      *
-     * @return configurations.
+     * @return configurations
      */
     public final List<PluginAdviceDefinition> build() {
         define();
@@ -70,9 +76,18 @@ public abstract class PluginDefinition {
     /**
      * To get all services.
      *
-     * @return all services.
+     * @return all services
      */
     public List<Class<? extends Service>> getAllServices() {
         return services;
     }
+
+    /**
+     * To get plugin name.
+     *
+     * @return plugin name
+     */
+    public String getPluginName() {
+        return pluginName;
+    }
 }
diff --git a/shardingsphere-agent/shardingsphere-agent-core/src/main/java/org/apache/shardingsphere/agent/core/plugin/PluginLoader.java b/shardingsphere-agent/shardingsphere-agent-core/src/main/java/org/apache/shardingsphere/agent/core/plugin/PluginLoader.java
index b1fbd2e..1088c30 100644
--- a/shardingsphere-agent/shardingsphere-agent-core/src/main/java/org/apache/shardingsphere/agent/core/plugin/PluginLoader.java
+++ b/shardingsphere-agent/shardingsphere-agent-core/src/main/java/org/apache/shardingsphere/agent/core/plugin/PluginLoader.java
@@ -21,6 +21,7 @@ package org.apache.shardingsphere.agent.core.plugin;
 import com.google.common.base.Strings;
 import com.google.common.collect.ImmutableMap;
 import com.google.common.collect.Lists;
+import com.google.common.collect.Sets;
 import com.google.common.io.ByteStreams;
 import lombok.SneakyThrows;
 import lombok.extern.slf4j.Slf4j;
@@ -29,10 +30,14 @@ import net.bytebuddy.matcher.ElementMatcher;
 import net.bytebuddy.matcher.ElementMatchers;
 
 import org.apache.shardingsphere.agent.core.common.AgentPathLocator;
+import org.apache.shardingsphere.agent.core.config.AgentConfiguration;
+import org.apache.shardingsphere.agent.core.utils.SingletonHolder;
+
 import java.io.ByteArrayOutputStream;
 import java.io.Closeable;
 import java.io.File;
 import java.io.IOException;
+import java.util.Set;
 import java.util.List;
 import java.util.Map;
 import java.util.Objects;
@@ -111,11 +116,16 @@ public final class PluginLoader extends ClassLoader implements Closeable {
         if (jarFiles == null) {
             return pluginDefineMap.build();
         }
+        AgentConfiguration configuration = SingletonHolder.INSTANCE.get(AgentConfiguration.class);
+        List<String> activatedLists = configuration.getActivatedPlugins();
+        if (activatedLists == null) {
+            activatedLists = Lists.newArrayList();
+        }
+        Set<String> activatedPlugins = Sets.newHashSet(activatedLists);
         ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
         for (File jarFile : jarFiles) {
             outputStream.reset();
             JarFile jar = new JarFile(jarFile, true);
-            jars.add(jar);
             Attributes attributes = jar.getManifest().getMainAttributes();
             String entrypoint = attributes.getValue("Entrypoint");
             if (Strings.isNullOrEmpty(entrypoint)) {
@@ -125,6 +135,9 @@ public final class PluginLoader extends ClassLoader implements Closeable {
             ByteStreams.copy(jar.getInputStream(jar.getEntry(classNameToPath(entrypoint))), outputStream);
             try {
                 PluginDefinition pluginDefinition = (PluginDefinition) defineClass(entrypoint, outputStream.toByteArray(), 0, outputStream.size()).newInstance();
+                if (!activatedPlugins.isEmpty() && !activatedPlugins.contains(pluginDefinition.getPluginName())) {
+                    continue;
+                }
                 pluginDefinition.getAllServices().forEach(klass -> {
                     try {
                         services.add(klass.newInstance());
@@ -136,6 +149,7 @@ public final class PluginLoader extends ClassLoader implements Closeable {
             } catch (InstantiationException | IllegalAccessException e) {
                 log.error("Failed to load plugin definition, {}.", entrypoint, e);
             }
+            jars.add(jar);
         }
         return pluginDefineMap.build();
     }
diff --git a/shardingsphere-agent/shardingsphere-agent-plugins/shardingsphere-agent-plugin-metrics/shardingsphere-agent-metrics-bootstrap/src/main/java/org/apache/shardingsphere/agent/metrics/bootstrap/MetricsPluginDefinition.java b/shardingsphere-agent/shardingsphere-agent-plugins/shardingsphere-agent-plugin-metrics/shardingsphere-agent-metrics-bootstrap/src/main/java/org/apache/shardingsphere/agent/metrics/bootstrap/MetricsPluginDefinition.java
index 55c942f..8de1da6 100644
--- a/shardingsphere-agent/shardingsphere-agent-plugins/shardingsphere-agent-plugin-metrics/shardingsphere-agent-metrics-bootstrap/src/main/java/org/apache/shardingsphere/agent/metrics/bootstrap/MetricsPluginDefinition.java
+++ b/shardingsphere-agent/shardingsphere-agent-plugins/shardingsphere-agent-plugin-metrics/shardingsphere-agent-metrics-bootstrap/src/main/java/org/apache/shardingsphere/agent/metrics/bootstrap/MetricsPluginDefinition.java
@@ -26,6 +26,10 @@ import org.apache.shardingsphere.agent.core.plugin.PluginDefinition;
  */
 public class MetricsPluginDefinition extends PluginDefinition {
 
+    public MetricsPluginDefinition() {
+        super("sample-metrics");
+    }
+
     @Override
     protected void define() {
         intercept("org.apache.shardingsphere.proxy.frontend.command.CommandExecutorTask")
diff --git a/shardingsphere-agent/shardingsphere-agent-plugins/shardingsphere-agent-plugin-tracers/shardingsphere-agent-tracer-sample/src/main/java/org/apache/shardingsphere/agent/plugin/trace/SamplePluginDefinition.java b/shardingsphere-agent/shardingsphere-agent-plugins/shardingsphere-agent-plugin-tracers/shardingsphere-agent-tracer-sample/src/main/java/org/apache/shardingsphere/agent/plugin/trace/SamplePluginDefinition.java
index 280344f..0695381 100644
--- a/shardingsphere-agent/shardingsphere-agent-plugins/shardingsphere-agent-plugin-tracers/shardingsphere-agent-tracer-sample/src/main/java/org/apache/shardingsphere/agent/plugin/trace/SamplePluginDefinition.java
+++ b/shardingsphere-agent/shardingsphere-agent-plugins/shardingsphere-agent-plugin-tracers/shardingsphere-agent-tracer-sample/src/main/java/org/apache/shardingsphere/agent/plugin/trace/SamplePluginDefinition.java
@@ -26,6 +26,10 @@ import org.apache.shardingsphere.agent.core.plugin.PluginDefinition;
  */
 public class SamplePluginDefinition extends PluginDefinition {
 
+    public SamplePluginDefinition() {
+        super("");
+    }
+
     @Override
     protected void define() {
         intercept("org.apache.shardingsphere.proxy.frontend.command.CommandExecutorTask")