From c4c608c5f57204586592d50fa4673e5207e8a897 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=A9=E7=A9=BA=E5=B0=8F=E5=B0=8F?= <751634819@qq.com> Date: Thu, 9 May 2024 06:16:53 +0000 Subject: [PATCH 1/7] =?UTF-8?q?!48=20update=20core/src/main/java/org/droma?= =?UTF-8?q?ra/dynamictp/core/monitor/collector/jmx/JMXCollector.java.=20*?= =?UTF-8?q?=20update=20core/src/main/java/org/dromara/dynamictp/core/monit?= =?UTF-8?q?or/collector=E2=80=A6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../monitor/collector/jmx/JMXCollector.java | 25 ++++++------------- 1 file changed, 7 insertions(+), 18 deletions(-) diff --git a/core/src/main/java/org/dromara/dynamictp/core/monitor/collector/jmx/JMXCollector.java b/core/src/main/java/org/dromara/dynamictp/core/monitor/collector/jmx/JMXCollector.java index a7643b2b7..1a9578130 100644 --- a/core/src/main/java/org/dromara/dynamictp/core/monitor/collector/jmx/JMXCollector.java +++ b/core/src/main/java/org/dromara/dynamictp/core/monitor/collector/jmx/JMXCollector.java @@ -40,26 +40,15 @@ public class JMXCollector extends AbstractCollector { public static final String DTP_METRIC_NAME_PREFIX = "dtp.thread.pool"; - /** - * thread pool stats map - */ - private static final Map GAUGE_CACHE = new ConcurrentHashMap<>(); - @Override public void collect(ThreadPoolStats threadPoolStats) { - if (GAUGE_CACHE.containsKey(threadPoolStats.getPoolName())) { - ThreadPoolStats poolStats = GAUGE_CACHE.get(threadPoolStats.getPoolName()); - BeanUtils.copyProperties(threadPoolStats, poolStats); - } else { - try { - MBeanServer server = ManagementFactory.getPlatformMBeanServer(); - ObjectName name = new ObjectName(DTP_METRIC_NAME_PREFIX + ":name=" + threadPoolStats.getPoolName()); - ThreadPoolStatsJMX stats = new ThreadPoolStatsJMX(threadPoolStats); - server.registerMBean(stats, name); - } catch (JMException e) { - log.error("collect thread pool stats error", e); - } - GAUGE_CACHE.put(threadPoolStats.getPoolName(), threadPoolStats); + try { + MBeanServer server = ManagementFactory.getPlatformMBeanServer(); + ObjectName name = new ObjectName(DTP_METRIC_NAME_PREFIX + ":name=" + threadPoolStats.getPoolName()); + ThreadPoolStatsJMX stats = new ThreadPoolStatsJMX(threadPoolStats); + server.registerMBean(stats, name); + } catch (JMException e) { + log.error("collect thread pool stats error", e); } } From 579a1140e477bfc543030ca9c726bef542b3fb3a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?tianxiang=20bao=20=EF=BC=88=E5=8C=85=E5=A4=A9=E7=A5=A5?= =?UTF-8?q?=EF=BC=89?= Date: Sat, 11 May 2024 15:09:02 +0800 Subject: [PATCH 2/7] =?UTF-8?q?=E8=A7=A3=E5=86=B3agent=E5=8C=85=E8=A3=85ru?= =?UTF-8?q?nnable=E4=BB=BB=E5=8A=A1=EF=BC=8C=E5=AF=BC=E8=87=B4=E4=B8=80?= =?UTF-8?q?=E4=BA=9Bmap=E5=86=85=E5=AD=98=E6=B3=84=E9=9C=B2=E9=97=AE?= =?UTF-8?q?=E9=A2=98=EF=BC=8C=E4=BB=A5=E5=8F=8A=E7=9B=B8=E5=BA=94=E7=9A=84?= =?UTF-8?q?=E5=91=8A=E8=AD=A6=E4=BF=A1=E6=81=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../dynamictp/core/aware/AwareManager.java | 8 +- .../dynamictp/core/aware/ExecutorAware.java | 23 +++ .../monitor/collector/jmx/JMXCollector.java | 3 - dependencies/pom.xml | 6 + extension/extension-agent/pom.xml | 14 ++ .../dynamictp/core/aware/AgentAware.java | 138 ++++++++++++++++++ ...dromara.dynamictp.core.aware.ExecutorAware | 1 + extension/pom.xml | 1 + 8 files changed, 187 insertions(+), 7 deletions(-) create mode 100644 extension/extension-agent/pom.xml create mode 100644 extension/extension-agent/src/main/java/org/dromara/dynamictp/core/aware/AgentAware.java create mode 100644 extension/extension-agent/src/main/resources/META-INF/services/org.dromara.dynamictp.core.aware.ExecutorAware diff --git a/core/src/main/java/org/dromara/dynamictp/core/aware/AwareManager.java b/core/src/main/java/org/dromara/dynamictp/core/aware/AwareManager.java index 28feee7dc..98cb34184 100644 --- a/core/src/main/java/org/dromara/dynamictp/core/aware/AwareManager.java +++ b/core/src/main/java/org/dromara/dynamictp/core/aware/AwareManager.java @@ -96,7 +96,7 @@ public static void execute(Executor executor, Runnable r) { public static void beforeExecute(Executor executor, Thread t, Runnable r) { for (ExecutorAware aware : EXECUTOR_AWARE_LIST) { try { - aware.beforeExecute(executor, t, r); + r = aware.beforeExecuteWrap(executor, t, r); } catch (Exception e) { log.error("DynamicTp aware [{}], enhance beforeExecute error.", aware.getName(), e); } @@ -106,7 +106,7 @@ public static void beforeExecute(Executor executor, Thread t, Runnable r) { public static void afterExecute(Executor executor, Runnable r, Throwable t) { for (ExecutorAware aware : EXECUTOR_AWARE_LIST) { try { - aware.afterExecute(executor, r, t); + r = aware.afterExecuteWrap(executor, r, t); } catch (Exception e) { log.error("DynamicTp aware [{}], enhance afterExecute error.", aware.getName(), e); } @@ -146,7 +146,7 @@ public static void terminated(Executor executor) { public static void beforeReject(Runnable r, Executor executor) { for (ExecutorAware aware : EXECUTOR_AWARE_LIST) { try { - aware.beforeReject(r, executor); + r = aware.beforeRejectWrap(r, executor); } catch (Exception e) { log.error("DynamicTp aware [{}], enhance beforeReject error.", aware.getName(), e); } @@ -156,7 +156,7 @@ public static void beforeReject(Runnable r, Executor executor) { public static void afterReject(Runnable r, Executor executor) { for (ExecutorAware aware : EXECUTOR_AWARE_LIST) { try { - aware.afterReject(r, executor); + r = aware.afterRejectWrap(r, executor); } catch (Exception e) { log.error("DynamicTp aware [{}], enhance afterReject error.", aware.getName(), e); } diff --git a/core/src/main/java/org/dromara/dynamictp/core/aware/ExecutorAware.java b/core/src/main/java/org/dromara/dynamictp/core/aware/ExecutorAware.java index 29af266b2..9fe023fc7 100644 --- a/core/src/main/java/org/dromara/dynamictp/core/aware/ExecutorAware.java +++ b/core/src/main/java/org/dromara/dynamictp/core/aware/ExecutorAware.java @@ -91,6 +91,11 @@ default void beforeExecute(Executor executor, Thread t, Runnable r) { // default no Operation } + default Runnable beforeExecuteWrap(Executor executor, Thread t, Runnable r) { + beforeExecute(executor, t, r); + return r; + } + /** * enhance afterExecute * @@ -102,6 +107,12 @@ default void afterExecute(Executor executor, Runnable r, Throwable t) { // default no Operation } + default Runnable afterExecuteWrap(Executor executor, Runnable r, Throwable t) { + // default no Operation + afterExecute(executor, r, t); + return r; + } + /** * enhance shutdown * @@ -139,6 +150,12 @@ default void beforeReject(Runnable r, Executor executor) { // default no Operation } + default Runnable beforeRejectWrap(Runnable r, Executor executor) { + // default no Operation + beforeReject(r, executor); + return r; + } + /** * enhance after reject * @param r runnable @@ -147,4 +164,10 @@ default void beforeReject(Runnable r, Executor executor) { default void afterReject(Runnable r, Executor executor) { // default no Operation } + + default Runnable afterRejectWrap(Runnable r, Executor executor) { + // default no Operation + afterReject(r, executor); + return r; + } } diff --git a/core/src/main/java/org/dromara/dynamictp/core/monitor/collector/jmx/JMXCollector.java b/core/src/main/java/org/dromara/dynamictp/core/monitor/collector/jmx/JMXCollector.java index 1a9578130..366aab465 100644 --- a/core/src/main/java/org/dromara/dynamictp/core/monitor/collector/jmx/JMXCollector.java +++ b/core/src/main/java/org/dromara/dynamictp/core/monitor/collector/jmx/JMXCollector.java @@ -21,14 +21,11 @@ import org.dromara.dynamictp.common.em.CollectorTypeEnum; import org.dromara.dynamictp.common.entity.ThreadPoolStats; import org.dromara.dynamictp.core.monitor.collector.AbstractCollector; -import org.springframework.beans.BeanUtils; import javax.management.JMException; import javax.management.MBeanServer; import javax.management.ObjectName; import java.lang.management.ManagementFactory; -import java.util.Map; -import java.util.concurrent.ConcurrentHashMap; /** * ThreadPoolStatsInfo related diff --git a/dependencies/pom.xml b/dependencies/pom.xml index 3cc5d53bf..83b9a2250 100644 --- a/dependencies/pom.xml +++ b/dependencies/pom.xml @@ -426,6 +426,12 @@ ${revision} + + org.dromara.dynamictp + dynamic-tp-extension-agent + ${revision} + + org.dromara.dynamictp dynamic-tp-extension-notify-email diff --git a/extension/extension-agent/pom.xml b/extension/extension-agent/pom.xml new file mode 100644 index 000000000..26585b894 --- /dev/null +++ b/extension/extension-agent/pom.xml @@ -0,0 +1,14 @@ + + 4.0.0 + + org.dromara.dynamictp + dynamic-tp-extension + ${revision} + ../pom.xml + + dynamic-tp-extension-agent + + + + diff --git a/extension/extension-agent/src/main/java/org/dromara/dynamictp/core/aware/AgentAware.java b/extension/extension-agent/src/main/java/org/dromara/dynamictp/core/aware/AgentAware.java new file mode 100644 index 000000000..8a6e3efb2 --- /dev/null +++ b/extension/extension-agent/src/main/java/org/dromara/dynamictp/core/aware/AgentAware.java @@ -0,0 +1,138 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 org.dromara.dynamictp.core.aware; + +import cn.hutool.core.lang.Pair; +import cn.hutool.core.util.ArrayUtil; +import lombok.extern.slf4j.Slf4j; +import org.dromara.dynamictp.core.support.task.runnable.DtpRunnable; + +import java.lang.ref.SoftReference; +import java.lang.reflect.Field; +import java.util.Arrays; +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.Executor; + +/** + * deal agent wrapper + * @author txbao + */ +@Slf4j +public class AgentAware extends TaskStatAware { + @Override + public int getOrder() { + return Integer.MIN_VALUE; + } + + @Override + public String getName() { + return "agent"; + } + + /** + * dtpRunnableCache key -> Runnable value -> DtpRunnable + */ + private final Map> dtpRunnableCache = new ConcurrentHashMap<>(); + + private Pair getDtpRunnable(Class rClass, Runnable r) throws IllegalAccessException { + while (Runnable.class.isAssignableFrom(rClass)) { + Field[] declaredFields = rClass.getDeclaredFields(); + if (ArrayUtil.isNotEmpty(declaredFields)) { + Field field = Arrays.stream(declaredFields) + .filter(ele -> Runnable.class == ele.getType()) + .findFirst() + .orElse(null); + if (field != null) { + field.setAccessible(true); + Runnable o = (Runnable) field.get(r); + if (o instanceof DtpRunnable) { + return new Pair<>(field, (DtpRunnable) o); + } + + // 纵向查找 + return getDtpRunnable(o.getClass(), o); + } + if (!Runnable.class.isAssignableFrom(rClass.getSuperclass())) { + break; + } + rClass = (Class) rClass.getSuperclass(); + } + } + return null; + } + + private Runnable getDtpRunnableInstance(Runnable r) { + if (r instanceof DtpRunnable) { + return r; + } + + Pair dtpRunnable = null; + Class rClass = r.getClass(); + try { + dtpRunnable = getDtpRunnable(rClass, r); + } catch (IllegalAccessException e) { + log.error("getDtpRunnable Error", e); + } + + if (dtpRunnable == null) { + if (log.isWarnEnabled()) { + log.warn("DynamicTp aware [{}], can not find DtpRunnable.", getName()); + } + return r; + } + + return dtpRunnable.getValue(); + } + + @Override + public Runnable beforeExecuteWrap(Executor executor, Thread t, Runnable r) { + Runnable runnableWrap = getDtpRunnableInstance(r); + if (runnableWrap instanceof DtpRunnable) { + dtpRunnableCache.put(r, new SoftReference<>((DtpRunnable) runnableWrap)); + } + return runnableWrap; + } + + @Override + public Runnable afterExecuteWrap(Executor executor, Runnable r, Throwable t) { + SoftReference remove = dtpRunnableCache.remove(r); + if (remove != null) { + return remove.get(); + } + return getDtpRunnableInstance(r); + } + + @Override + public Runnable beforeRejectWrap(Runnable r, Executor executor) { + SoftReference remove = dtpRunnableCache.remove(r); + if (remove != null) { + return remove.get(); + } + return getDtpRunnableInstance(r); + } + + @Override + public Runnable afterRejectWrap(Runnable r, Executor executor) { + SoftReference remove = dtpRunnableCache.remove(r); + if (remove != null) { + return remove.get(); + } + return getDtpRunnableInstance(r); + } +} diff --git a/extension/extension-agent/src/main/resources/META-INF/services/org.dromara.dynamictp.core.aware.ExecutorAware b/extension/extension-agent/src/main/resources/META-INF/services/org.dromara.dynamictp.core.aware.ExecutorAware new file mode 100644 index 000000000..064e5f138 --- /dev/null +++ b/extension/extension-agent/src/main/resources/META-INF/services/org.dromara.dynamictp.core.aware.ExecutorAware @@ -0,0 +1 @@ +org.dromara.dynamictp.core.aware.AgentAware \ No newline at end of file diff --git a/extension/pom.xml b/extension/pom.xml index 0941b815a..a2d20349d 100644 --- a/extension/pom.xml +++ b/extension/pom.xml @@ -17,6 +17,7 @@ extension-notify-email extension-opentelemetry extension-notify-yunzhijia + extension-agent From d21fa43a2d4b00d480016c2e3c876f9aa3296e6b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?tianxiang=20bao=20=EF=BC=88=E5=8C=85=E5=A4=A9=E7=A5=A5?= =?UTF-8?q?=EF=BC=89?= Date: Wed, 15 May 2024 14:57:34 +0800 Subject: [PATCH 3/7] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E7=9B=91=E6=8E=A7keepAli?= =?UTF-8?q?veTime=E5=AD=97=E6=AE=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../org/dromara/dynamictp/common/entity/ThreadPoolStats.java | 5 +++++ .../dromara/dynamictp/core/converter/ExecutorConverter.java | 1 + 2 files changed, 6 insertions(+) diff --git a/common/src/main/java/org/dromara/dynamictp/common/entity/ThreadPoolStats.java b/common/src/main/java/org/dromara/dynamictp/common/entity/ThreadPoolStats.java index c294765b5..06b4928b1 100644 --- a/common/src/main/java/org/dromara/dynamictp/common/entity/ThreadPoolStats.java +++ b/common/src/main/java/org/dromara/dynamictp/common/entity/ThreadPoolStats.java @@ -51,6 +51,11 @@ public class ThreadPoolStats extends Metrics { */ private int maximumPoolSize; + /** + * 空闲时间 (ms) + */ + private long keepAliveTime; + /** * 队列类型 */ diff --git a/core/src/main/java/org/dromara/dynamictp/core/converter/ExecutorConverter.java b/core/src/main/java/org/dromara/dynamictp/core/converter/ExecutorConverter.java index 70af16371..48d333321 100644 --- a/core/src/main/java/org/dromara/dynamictp/core/converter/ExecutorConverter.java +++ b/core/src/main/java/org/dromara/dynamictp/core/converter/ExecutorConverter.java @@ -97,6 +97,7 @@ private static ThreadPoolStats convertCommon(ExecutorAdapter executor) { poolStats.setCompletedTaskCount(executor.getCompletedTaskCount()); poolStats.setWaitTaskCount(executor.getQueueSize()); poolStats.setRejectHandlerName(executor.getRejectHandlerType()); + poolStats.setKeepAliveTime(executor.getKeepAliveTime(TimeUnit.MILLISECONDS)); return poolStats; } } From b53a72e8649751b46a8950b83dfb3f2c769de174 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?tianxiang=20bao=20=EF=BC=88=E5=8C=85=E5=A4=A9=E7=A5=A5?= =?UTF-8?q?=EF=BC=89?= Date: Tue, 21 May 2024 19:19:00 +0800 Subject: [PATCH 4/7] =?UTF-8?q?=E5=B0=86undertow=E7=9A=84=E4=BB=BB?= =?UTF-8?q?=E5=8A=A1=E5=B0=81=E8=A3=85=E6=88=90DtpRunnable=E5=AF=B9?= =?UTF-8?q?=E8=B1=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../undertow/EnhancedQueueExecutorProxy.java | 25 +++++++++++++++---- 1 file changed, 20 insertions(+), 5 deletions(-) diff --git a/starter/starter-adapter/starter-adapter-webserver/src/main/java/org/dromara/dynamictp/starter/adapter/webserver/undertow/EnhancedQueueExecutorProxy.java b/starter/starter-adapter/starter-adapter-webserver/src/main/java/org/dromara/dynamictp/starter/adapter/webserver/undertow/EnhancedQueueExecutorProxy.java index f34d27d70..b0dcf9faa 100644 --- a/starter/starter-adapter/starter-adapter-webserver/src/main/java/org/dromara/dynamictp/starter/adapter/webserver/undertow/EnhancedQueueExecutorProxy.java +++ b/starter/starter-adapter/starter-adapter-webserver/src/main/java/org/dromara/dynamictp/starter/adapter/webserver/undertow/EnhancedQueueExecutorProxy.java @@ -18,9 +18,12 @@ package org.dromara.dynamictp.starter.adapter.webserver.undertow; import org.dromara.dynamictp.core.aware.AwareManager; +import org.dromara.dynamictp.core.aware.TaskEnhanceAware; import org.dromara.dynamictp.core.support.task.runnable.EnhancedRunnable; +import org.dromara.dynamictp.core.support.task.wrapper.TaskWrapper; import org.jboss.threads.EnhancedQueueExecutor; +import java.util.List; import java.util.concurrent.RejectedExecutionException; /** @@ -30,7 +33,9 @@ * @since 1.1.4 **/ @SuppressWarnings("all") -public class EnhancedQueueExecutorProxy extends EnhancedQueueExecutor { +public class EnhancedQueueExecutorProxy extends EnhancedQueueExecutor implements TaskEnhanceAware { + + private List taskWrappers; public EnhancedQueueExecutorProxy(final Builder builder) { super(builder); @@ -49,19 +54,29 @@ public EnhancedQueueExecutorProxy(final EnhancedQueueExecutor executor) { @Override public void execute(Runnable runnable) { - EnhancedRunnable enhanceTask = EnhancedRunnable.of(runnable, this); - AwareManager.execute(this, enhanceTask); + Runnable enhancedTask = getEnhancedTask(EnhancedRunnable.of(runnable, this)); + AwareManager.execute(this, enhancedTask); try { - super.execute(enhanceTask); + super.execute(enhancedTask); } catch (Throwable e) { Throwable[] suppressedExceptions = e.getSuppressed(); for (Throwable t : suppressedExceptions) { if (t instanceof RejectedExecutionException) { - AwareManager.beforeReject(enhanceTask, this); + AwareManager.beforeReject(enhancedTask, this); return; } } throw e; } } + + @Override + public List getTaskWrappers() { + return this.taskWrappers; + } + + @Override + public void setTaskWrappers(List taskWrappers) { + this.taskWrappers = taskWrappers; + } } From ecc9b2b774c93bd3e3dccf41614cb51785c2c5b8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?tianxiang=20bao=20=EF=BC=88=E5=8C=85=E5=A4=A9=E7=A5=A5?= =?UTF-8?q?=EF=BC=89?= Date: Wed, 22 May 2024 14:10:12 +0800 Subject: [PATCH 5/7] =?UTF-8?q?AgentAware:=201=E3=80=81=E6=96=B0=E5=A2=9E?= =?UTF-8?q?=E5=A6=82=E6=9E=9C=E6=8E=A8=E6=96=AD=E4=B8=8D=E5=87=BADtpRunnab?= =?UTF-8?q?le=E5=AF=B9=E8=B1=A1=EF=BC=8C=E5=88=99=E8=AE=BE=E7=BD=AEdtp.exe?= =?UTF-8?q?cute.enhanced=E4=B8=BAfalse=EF=BC=9B=202=E3=80=81=E6=94=AF?= =?UTF-8?q?=E6=8C=81=E7=BB=9D=E5=A4=A7=E6=95=B0=E7=9A=84Wrapper=E8=A7=A3?= =?UTF-8?q?=E6=9E=90=EF=BC=88=E5=A4=9A=E4=B8=AADtpRunnable=E5=AF=B9?= =?UTF-8?q?=E8=B1=A1=E4=B8=8D=E6=94=AF=E6=8C=81=E6=8E=A8=E6=96=AD=EF=BC=89?= =?UTF-8?q?=203=E3=80=81=E6=96=B0=E5=A2=9EWrapper=E7=9A=84=E5=8D=95?= =?UTF-8?q?=E6=B5=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../dynamictp/core/aware/AgentAware.java | 65 +++++++---- test/test-extension/pom.xml | 37 ++++++ .../dynamictp/agent/AgentAwareTest.java | 110 ++++++++++++++++++ .../MyAgentTwoPathRunnableChildWrapper.java | 21 ++++ .../agent/MyAgentTwoPathRunnableWrapper.java | 24 ++++ .../dynamictp/agent/MyAgentWrapper.java | 23 ++++ .../dynamictp/agent/MyAgentWrapperChild.java | 12 ++ .../agent/MyAgentWrapperTwoRunnable.java | 27 +++++ 8 files changed, 299 insertions(+), 20 deletions(-) create mode 100644 test/test-extension/pom.xml create mode 100644 test/test-extension/src/test/java/org/dromara/dynamictp/agent/AgentAwareTest.java create mode 100644 test/test-extension/src/test/java/org/dromara/dynamictp/agent/MyAgentTwoPathRunnableChildWrapper.java create mode 100644 test/test-extension/src/test/java/org/dromara/dynamictp/agent/MyAgentTwoPathRunnableWrapper.java create mode 100644 test/test-extension/src/test/java/org/dromara/dynamictp/agent/MyAgentWrapper.java create mode 100644 test/test-extension/src/test/java/org/dromara/dynamictp/agent/MyAgentWrapperChild.java create mode 100644 test/test-extension/src/test/java/org/dromara/dynamictp/agent/MyAgentWrapperTwoRunnable.java diff --git a/extension/extension-agent/src/main/java/org/dromara/dynamictp/core/aware/AgentAware.java b/extension/extension-agent/src/main/java/org/dromara/dynamictp/core/aware/AgentAware.java index 8a6e3efb2..dc965e5e5 100644 --- a/extension/extension-agent/src/main/java/org/dromara/dynamictp/core/aware/AgentAware.java +++ b/extension/extension-agent/src/main/java/org/dromara/dynamictp/core/aware/AgentAware.java @@ -17,7 +17,7 @@ package org.dromara.dynamictp.core.aware; -import cn.hutool.core.lang.Pair; +import cn.hutool.core.collection.CollectionUtil; import cn.hutool.core.util.ArrayUtil; import lombok.extern.slf4j.Slf4j; import org.dromara.dynamictp.core.support.task.runnable.DtpRunnable; @@ -25,9 +25,15 @@ import java.lang.ref.SoftReference; import java.lang.reflect.Field; import java.util.Arrays; +import java.util.List; import java.util.Map; +import java.util.Objects; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.Executor; +import java.util.stream.Collectors; + +import static org.dromara.dynamictp.common.constant.DynamicTpConst.DTP_EXECUTE_ENHANCED; +import static org.dromara.dynamictp.common.constant.DynamicTpConst.FALSE_STR; /** * deal agent wrapper @@ -50,29 +56,45 @@ public String getName() { */ private final Map> dtpRunnableCache = new ConcurrentHashMap<>(); - private Pair getDtpRunnable(Class rClass, Runnable r) throws IllegalAccessException { + private DtpRunnable determineDtpRunnable(List conditionalFields, Runnable r) throws IllegalAccessException { + + for (Field field : conditionalFields) { + if (Objects.isNull(field)) { + continue; + } + + field.setAccessible(true); + Runnable o = (Runnable) field.get(r); + if (o instanceof DtpRunnable) { + return (DtpRunnable) o; + } + // 纵向查找 + DtpRunnable dtpRunnable = getDtpRunnable(o.getClass(), o); + if (dtpRunnable != null) { + return dtpRunnable; + } + } + return null; + } + + private DtpRunnable getDtpRunnable(Class rClass, Runnable r) throws IllegalAccessException { while (Runnable.class.isAssignableFrom(rClass)) { Field[] declaredFields = rClass.getDeclaredFields(); if (ArrayUtil.isNotEmpty(declaredFields)) { - Field field = Arrays.stream(declaredFields) - .filter(ele -> Runnable.class == ele.getType()) - .findFirst() - .orElse(null); - if (field != null) { - field.setAccessible(true); - Runnable o = (Runnable) field.get(r); - if (o instanceof DtpRunnable) { - return new Pair<>(field, (DtpRunnable) o); + List conditionFields = Arrays.stream(declaredFields) + .filter(ele -> Runnable.class.isAssignableFrom(ele.getType())) + .collect(Collectors.toList()); + if (CollectionUtil.isNotEmpty(conditionFields)) { + DtpRunnable dtpRunnable = determineDtpRunnable(conditionFields, r); + if (Objects.nonNull(dtpRunnable)) { + return dtpRunnable; } - - // 纵向查找 - return getDtpRunnable(o.getClass(), o); - } - if (!Runnable.class.isAssignableFrom(rClass.getSuperclass())) { - break; } - rClass = (Class) rClass.getSuperclass(); } + if (!Runnable.class.isAssignableFrom(rClass.getSuperclass())) { + break; + } + rClass = (Class) rClass.getSuperclass(); } return null; } @@ -82,7 +104,7 @@ private Runnable getDtpRunnableInstance(Runnable r) { return r; } - Pair dtpRunnable = null; + DtpRunnable dtpRunnable = null; Class rClass = r.getClass(); try { dtpRunnable = getDtpRunnable(rClass, r); @@ -97,7 +119,7 @@ private Runnable getDtpRunnableInstance(Runnable r) { return r; } - return dtpRunnable.getValue(); + return dtpRunnable; } @Override @@ -105,6 +127,9 @@ public Runnable beforeExecuteWrap(Executor executor, Thread t, Runnable r) { Runnable runnableWrap = getDtpRunnableInstance(r); if (runnableWrap instanceof DtpRunnable) { dtpRunnableCache.put(r, new SoftReference<>((DtpRunnable) runnableWrap)); + } else { + // 被封装的wrapper没有找到DtpRunnable对象,那么就关闭某些监控指标,防止内存溢出 + System.setProperty(DTP_EXECUTE_ENHANCED, FALSE_STR); } return runnableWrap; } diff --git a/test/test-extension/pom.xml b/test/test-extension/pom.xml new file mode 100644 index 000000000..8cfd8cd49 --- /dev/null +++ b/test/test-extension/pom.xml @@ -0,0 +1,37 @@ + + 4.0.0 + + org.dromara.dynamictp + dynamic-tp-all + 1.1.7 + ../../pom.xml + + + dynamic-tp-test-extension + jar + + test-extension + + + UTF-8 + + + + + junit + junit + test + + + + org.dromara.dynamictp + dynamic-tp-extension-agent + + + org.junit.jupiter + junit-jupiter-api + test + + + diff --git a/test/test-extension/src/test/java/org/dromara/dynamictp/agent/AgentAwareTest.java b/test/test-extension/src/test/java/org/dromara/dynamictp/agent/AgentAwareTest.java new file mode 100644 index 000000000..89688a549 --- /dev/null +++ b/test/test-extension/src/test/java/org/dromara/dynamictp/agent/AgentAwareTest.java @@ -0,0 +1,110 @@ +package org.dromara.dynamictp.agent; + +import org.dromara.dynamictp.core.aware.AgentAware; +import org.dromara.dynamictp.core.support.task.runnable.DtpRunnable; +import org.junit.Test; +import org.junit.jupiter.api.Assertions; +import org.springframework.util.ReflectionUtils; + +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; + +public class AgentAwareTest { + + @Test + public void testDirectOnlyOneDtpRunnable() throws InvocationTargetException, IllegalAccessException { + Method getDtpRunnableInstance = ReflectionUtils.findMethod(AgentAware.class, "getDtpRunnableInstance", Runnable.class); + Assertions.assertNotNull(getDtpRunnableInstance); + + getDtpRunnableInstance.setAccessible(true); + + Runnable runnable = () -> { + + }; + + + MyAgentWrapper myAgentWrapper = new MyAgentWrapper(runnable, new Object()); + Object result = getDtpRunnableInstance.invoke(new AgentAware(), myAgentWrapper); + Assertions.assertTrue(result == myAgentWrapper); + + DtpRunnable dtpRunnable = new DtpRunnable(runnable, runnable, "test"); + myAgentWrapper = new MyAgentWrapper(dtpRunnable, new Object()); + result = getDtpRunnableInstance.invoke(new AgentAware(), myAgentWrapper); + Assertions.assertNotNull(dtpRunnable == result); + } + + @Test + public void testDirectTwoRunnable() throws InvocationTargetException, IllegalAccessException { + Runnable runnable = () -> { + + }; + DtpRunnable dtpRunnable = new DtpRunnable(runnable, runnable, "test"); + MyAgentWrapperTwoRunnable myAgentWrapper = new MyAgentWrapperTwoRunnable(dtpRunnable, runnable, "test"); + + Method getDtpRunnableInstance = ReflectionUtils.findMethod(AgentAware.class, "getDtpRunnableInstance", Runnable.class); + Assertions.assertNotNull(getDtpRunnableInstance); + getDtpRunnableInstance.setAccessible(true); + + Object result = getDtpRunnableInstance.invoke(new AgentAware(), myAgentWrapper); + Assertions.assertTrue(result == dtpRunnable); + + myAgentWrapper = new MyAgentWrapperTwoRunnable(runnable, dtpRunnable, "test"); + result = getDtpRunnableInstance.invoke(new AgentAware(), myAgentWrapper); + Assertions.assertTrue(result == dtpRunnable); + } + + @Test + public void testNotDirectRunnable() throws InvocationTargetException, IllegalAccessException { + Runnable runnable = () -> { + + }; + DtpRunnable dtpRunnable = new DtpRunnable(runnable, runnable, "test"); + MyAgentWrapper myAgentWrapper = new MyAgentWrapper(dtpRunnable, new Object()); + + MyAgentTwoPathRunnableWrapper twoPathRunnableWrapper = new MyAgentTwoPathRunnableWrapper(myAgentWrapper, new Object()); + Method getDtpRunnableInstance = ReflectionUtils.findMethod(AgentAware.class, "getDtpRunnableInstance", Runnable.class); + Assertions.assertNotNull(getDtpRunnableInstance); + getDtpRunnableInstance.setAccessible(true); + + Object result = getDtpRunnableInstance.invoke(new AgentAware(), twoPathRunnableWrapper); + Assertions.assertTrue(result == dtpRunnable); + } + + @Test + public void testExtendRunnable() throws InvocationTargetException, IllegalAccessException { + Runnable runnable = () -> { + + }; + DtpRunnable dtpRunnable = new DtpRunnable(runnable, runnable, "test"); + MyAgentWrapper myAgentWrapper = new MyAgentWrapper(dtpRunnable, new Object()); + MyAgentWrapperChild myAgentWrapperChild = new MyAgentWrapperChild(myAgentWrapper, new Object()); + + Method getDtpRunnableInstance = ReflectionUtils.findMethod(AgentAware.class, "getDtpRunnableInstance", Runnable.class); + Assertions.assertNotNull(getDtpRunnableInstance); + getDtpRunnableInstance.setAccessible(true); + + Object result = getDtpRunnableInstance.invoke(new AgentAware(), myAgentWrapperChild); + Assertions.assertTrue(result == dtpRunnable); + } + + @Test + public void testDeepRunnable() throws InvocationTargetException, IllegalAccessException { + Runnable runnable = () -> { + + }; + DtpRunnable dtpRunnable = new DtpRunnable(runnable, runnable, "test"); + MyAgentWrapper myAgentWrapper = new MyAgentWrapper(runnable, new Object()); + MyAgentWrapper myAgentWrapperDtpRunnable = new MyAgentWrapper(dtpRunnable, new Object()); + MyAgentWrapperChild myAgentWrapperChild = new MyAgentWrapperChild(myAgentWrapperDtpRunnable, new Object()); + + MyAgentTwoPathRunnableChildWrapper myAgentTwoPathRunnableChildWrapper = new MyAgentTwoPathRunnableChildWrapper(myAgentWrapperChild, + myAgentWrapper, new Object()); + + Method getDtpRunnableInstance = ReflectionUtils.findMethod(AgentAware.class, "getDtpRunnableInstance", Runnable.class); + Assertions.assertNotNull(getDtpRunnableInstance); + getDtpRunnableInstance.setAccessible(true); + Object result = getDtpRunnableInstance.invoke(new AgentAware(), myAgentTwoPathRunnableChildWrapper); + Assertions.assertTrue(result == dtpRunnable); + } + +} diff --git a/test/test-extension/src/test/java/org/dromara/dynamictp/agent/MyAgentTwoPathRunnableChildWrapper.java b/test/test-extension/src/test/java/org/dromara/dynamictp/agent/MyAgentTwoPathRunnableChildWrapper.java new file mode 100644 index 000000000..8f7e783cb --- /dev/null +++ b/test/test-extension/src/test/java/org/dromara/dynamictp/agent/MyAgentTwoPathRunnableChildWrapper.java @@ -0,0 +1,21 @@ +package org.dromara.dynamictp.agent; + +public class MyAgentTwoPathRunnableChildWrapper implements Runnable { + + private MyAgentWrapperChild myAgentWrapperChild; + + private MyAgentWrapper myAgentWrapper; + + private Object busi; + + public MyAgentTwoPathRunnableChildWrapper(MyAgentWrapperChild myAgentWrapperChild, MyAgentWrapper myAgentWrapper, Object busi) { + this.myAgentWrapperChild = myAgentWrapperChild; + this.myAgentWrapper = myAgentWrapper; + this.busi = busi; + } + + @Override + public void run() { + System.out.println("MyAgentTwoPathRunnableChildWrapper"); + } +} diff --git a/test/test-extension/src/test/java/org/dromara/dynamictp/agent/MyAgentTwoPathRunnableWrapper.java b/test/test-extension/src/test/java/org/dromara/dynamictp/agent/MyAgentTwoPathRunnableWrapper.java new file mode 100644 index 000000000..3c7c622d5 --- /dev/null +++ b/test/test-extension/src/test/java/org/dromara/dynamictp/agent/MyAgentTwoPathRunnableWrapper.java @@ -0,0 +1,24 @@ +package org.dromara.dynamictp.agent; + +public class MyAgentTwoPathRunnableWrapper implements Runnable { + + private final MyAgentWrapper myAgentWrapper; + + private final Object busiObj; + + public MyAgentTwoPathRunnableWrapper(MyAgentWrapper myAgentWrapper, Object busiObj) { + this.myAgentWrapper = myAgentWrapper; + this.busiObj = busiObj; + } + + + @Override + public void run() { + System.out.println("before"); + try { + myAgentWrapper.run(); + } finally { + System.out.println("finally"); + } + } +} diff --git a/test/test-extension/src/test/java/org/dromara/dynamictp/agent/MyAgentWrapper.java b/test/test-extension/src/test/java/org/dromara/dynamictp/agent/MyAgentWrapper.java new file mode 100644 index 000000000..56edb3a8d --- /dev/null +++ b/test/test-extension/src/test/java/org/dromara/dynamictp/agent/MyAgentWrapper.java @@ -0,0 +1,23 @@ +package org.dromara.dynamictp.agent; + +public class MyAgentWrapper implements Runnable { + + private Runnable runnable; + + private Object busiObj; + + public MyAgentWrapper(Runnable runnable, Object busiObj) { + this.runnable = runnable; + this.busiObj = busiObj; + } + + @Override + public void run() { + System.out.println("before"); + try { + runnable.run(); + } finally { + System.out.println("finally"); + } + } +} diff --git a/test/test-extension/src/test/java/org/dromara/dynamictp/agent/MyAgentWrapperChild.java b/test/test-extension/src/test/java/org/dromara/dynamictp/agent/MyAgentWrapperChild.java new file mode 100644 index 000000000..44919ceae --- /dev/null +++ b/test/test-extension/src/test/java/org/dromara/dynamictp/agent/MyAgentWrapperChild.java @@ -0,0 +1,12 @@ +package org.dromara.dynamictp.agent; + +public class MyAgentWrapperChild extends MyAgentWrapper { + public MyAgentWrapperChild(MyAgentWrapper runnable, Object busiObj) { + super(runnable, busiObj); + } + + @Override + public void run() { + System.out.println("MyAgentWrapperChild"); + } +} diff --git a/test/test-extension/src/test/java/org/dromara/dynamictp/agent/MyAgentWrapperTwoRunnable.java b/test/test-extension/src/test/java/org/dromara/dynamictp/agent/MyAgentWrapperTwoRunnable.java new file mode 100644 index 000000000..b63d9f404 --- /dev/null +++ b/test/test-extension/src/test/java/org/dromara/dynamictp/agent/MyAgentWrapperTwoRunnable.java @@ -0,0 +1,27 @@ +package org.dromara.dynamictp.agent; + +public class MyAgentWrapperTwoRunnable implements Runnable { + + private Runnable r1; + + private Runnable r2; + + private Object busiObj; + + public MyAgentWrapperTwoRunnable(Runnable r1, Runnable r2, Object busiObj) { + this.r1 = r1; + this.r2 = r2; + this.busiObj = busiObj; + } + + @Override + public void run() { + System.out.println("before"); + try { + r1.run(); + r2.run(); + } finally { + System.out.println("after"); + } + } +} From dfb1d6b2645f6b550442b27d1a8878f17e9ba3b5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?tianxiang=20bao=20=EF=BC=88=E5=8C=85=E5=A4=A9=E7=A5=A5?= =?UTF-8?q?=EF=BC=89?= Date: Wed, 22 May 2024 14:17:59 +0800 Subject: [PATCH 6/7] =?UTF-8?q?=E4=BF=AE=E6=94=B9${revision}?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- test/test-extension/pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/test-extension/pom.xml b/test/test-extension/pom.xml index 8cfd8cd49..42d887aaf 100644 --- a/test/test-extension/pom.xml +++ b/test/test-extension/pom.xml @@ -4,7 +4,7 @@ org.dromara.dynamictp dynamic-tp-all - 1.1.7 + ${revision} ../../pom.xml From 4763828e0a1de151c76c8c50a940e2a4ecfc6750 Mon Sep 17 00:00:00 2001 From: yanhom Date: Thu, 13 Jun 2024 22:44:43 +0800 Subject: [PATCH 7/7] optimize AgentAware --- .../dynamictp/core/aware/ExecutorAware.java | 3 - dependencies/pom.xml | 2 +- .../dynamictp/core/aware/AgentAware.java | 30 +++---- extension/extension-skywalking/pom.xml | 5 -- .../skywalking/init/SwInitializer.java | 81 ------------------- ...dynamictp.core.support.init.DtpInitializer | 1 - pom.xml | 2 +- test/pom.xml | 1 + .../dynamictp/agent/AgentAwareTest.java | 17 ++++ .../MyAgentTwoPathRunnableChildWrapper.java | 17 ++++ .../agent/MyAgentTwoPathRunnableWrapper.java | 17 ++++ .../dynamictp/agent/MyAgentWrapper.java | 17 ++++ .../dynamictp/agent/MyAgentWrapperChild.java | 17 ++++ .../agent/MyAgentWrapperTwoRunnable.java | 17 ++++ 14 files changed, 114 insertions(+), 113 deletions(-) delete mode 100644 extension/extension-skywalking/src/main/java/org/dromara/dynamictp/extension/skywalking/init/SwInitializer.java delete mode 100644 extension/extension-skywalking/src/main/resources/META-INF/services/org.dromara.dynamictp.core.support.init.DtpInitializer diff --git a/core/src/main/java/org/dromara/dynamictp/core/aware/ExecutorAware.java b/core/src/main/java/org/dromara/dynamictp/core/aware/ExecutorAware.java index 9fe023fc7..56e492bf1 100644 --- a/core/src/main/java/org/dromara/dynamictp/core/aware/ExecutorAware.java +++ b/core/src/main/java/org/dromara/dynamictp/core/aware/ExecutorAware.java @@ -108,7 +108,6 @@ default void afterExecute(Executor executor, Runnable r, Throwable t) { } default Runnable afterExecuteWrap(Executor executor, Runnable r, Throwable t) { - // default no Operation afterExecute(executor, r, t); return r; } @@ -151,7 +150,6 @@ default void beforeReject(Runnable r, Executor executor) { } default Runnable beforeRejectWrap(Runnable r, Executor executor) { - // default no Operation beforeReject(r, executor); return r; } @@ -166,7 +164,6 @@ default void afterReject(Runnable r, Executor executor) { } default Runnable afterRejectWrap(Runnable r, Executor executor) { - // default no Operation afterReject(r, executor); return r; } diff --git a/dependencies/pom.xml b/dependencies/pom.xml index 83b9a2250..ba00b1c4e 100644 --- a/dependencies/pom.xml +++ b/dependencies/pom.xml @@ -12,7 +12,7 @@ https://github.com/yanhom1314/dynamic-tp - 1.1.7 + 1.1.8-beta UTF-8 1.18.24 diff --git a/extension/extension-agent/src/main/java/org/dromara/dynamictp/core/aware/AgentAware.java b/extension/extension-agent/src/main/java/org/dromara/dynamictp/core/aware/AgentAware.java index dc965e5e5..f456db816 100644 --- a/extension/extension-agent/src/main/java/org/dromara/dynamictp/core/aware/AgentAware.java +++ b/extension/extension-agent/src/main/java/org/dromara/dynamictp/core/aware/AgentAware.java @@ -17,9 +17,9 @@ package org.dromara.dynamictp.core.aware; -import cn.hutool.core.collection.CollectionUtil; import cn.hutool.core.util.ArrayUtil; import lombok.extern.slf4j.Slf4j; +import org.apache.commons.collections4.CollectionUtils; import org.dromara.dynamictp.core.support.task.runnable.DtpRunnable; import java.lang.ref.SoftReference; @@ -40,7 +40,14 @@ * @author txbao */ @Slf4j +@SuppressWarnings("all") public class AgentAware extends TaskStatAware { + + /** + * dtpRunnableCache key -> Runnable value -> DtpRunnable + */ + private final Map> dtpRunnableCache = new ConcurrentHashMap<>(); + @Override public int getOrder() { return Integer.MIN_VALUE; @@ -51,18 +58,11 @@ public String getName() { return "agent"; } - /** - * dtpRunnableCache key -> Runnable value -> DtpRunnable - */ - private final Map> dtpRunnableCache = new ConcurrentHashMap<>(); - private DtpRunnable determineDtpRunnable(List conditionalFields, Runnable r) throws IllegalAccessException { - for (Field field : conditionalFields) { if (Objects.isNull(field)) { continue; } - field.setAccessible(true); Runnable o = (Runnable) field.get(r); if (o instanceof DtpRunnable) { @@ -84,7 +84,7 @@ private DtpRunnable getDtpRunnable(Class rClass, Runnable r) List conditionFields = Arrays.stream(declaredFields) .filter(ele -> Runnable.class.isAssignableFrom(ele.getType())) .collect(Collectors.toList()); - if (CollectionUtil.isNotEmpty(conditionFields)) { + if (CollectionUtils.isNotEmpty(conditionFields)) { DtpRunnable dtpRunnable = determineDtpRunnable(conditionFields, r); if (Objects.nonNull(dtpRunnable)) { return dtpRunnable; @@ -103,7 +103,6 @@ private Runnable getDtpRunnableInstance(Runnable r) { if (r instanceof DtpRunnable) { return r; } - DtpRunnable dtpRunnable = null; Class rClass = r.getClass(); try { @@ -111,14 +110,12 @@ private Runnable getDtpRunnableInstance(Runnable r) { } catch (IllegalAccessException e) { log.error("getDtpRunnable Error", e); } - if (dtpRunnable == null) { if (log.isWarnEnabled()) { log.warn("DynamicTp aware [{}], can not find DtpRunnable.", getName()); } return r; } - return dtpRunnable; } @@ -151,13 +148,4 @@ public Runnable beforeRejectWrap(Runnable r, Executor executor) { } return getDtpRunnableInstance(r); } - - @Override - public Runnable afterRejectWrap(Runnable r, Executor executor) { - SoftReference remove = dtpRunnableCache.remove(r); - if (remove != null) { - return remove.get(); - } - return getDtpRunnableInstance(r); - } } diff --git a/extension/extension-skywalking/pom.xml b/extension/extension-skywalking/pom.xml index 70fb7469e..019485e91 100644 --- a/extension/extension-skywalking/pom.xml +++ b/extension/extension-skywalking/pom.xml @@ -21,11 +21,6 @@ org.slf4j slf4j-api - - - org.dromara.dynamictp - dynamic-tp-jvmti-runtime - diff --git a/extension/extension-skywalking/src/main/java/org/dromara/dynamictp/extension/skywalking/init/SwInitializer.java b/extension/extension-skywalking/src/main/java/org/dromara/dynamictp/extension/skywalking/init/SwInitializer.java deleted file mode 100644 index ff83ffd95..000000000 --- a/extension/extension-skywalking/src/main/java/org/dromara/dynamictp/extension/skywalking/init/SwInitializer.java +++ /dev/null @@ -1,81 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You 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 org.dromara.dynamictp.extension.skywalking.init; - -import lombok.extern.slf4j.Slf4j; -import org.apache.commons.collections4.CollectionUtils; -import org.dromara.dynamictp.core.support.init.DtpInitializer; -import org.dromara.dynamictp.jvmti.JVMTI; - -import java.util.List; - -import static org.dromara.dynamictp.common.constant.DynamicTpConst.DTP_EXECUTE_ENHANCED; -import static org.dromara.dynamictp.common.constant.DynamicTpConst.FALSE_STR; - -/** - * SwInitializer related - * - * @author yanhom - * @since 1.1.6 - */ -@Slf4j -public class SwInitializer implements DtpInitializer { - - private static final String SW_AGENT_CLASS_LOADER = "org.apache.skywalking.apm.agent.core.plugin.loader.AgentClassLoader"; - - private static final String SW_RUNNABLE_WRAPPER = "org.apache.skywalking.apm.plugin.wrapper.SwRunnableWrapper"; - - private static final String SW_CALLABLE_WRAPPER = "org.apache.skywalking.apm.plugin.wrapper.SwCallableWrapper"; - - @Override - public String getName() { - return "SwInitializer"; - } - - @Override - public void init() { - try { - List classLoaders = JVMTI.getInstances(ClassLoader.class); - if (CollectionUtils.isEmpty(classLoaders)) { - return; - } - for (ClassLoader cl : classLoaders) { - if (!SW_AGENT_CLASS_LOADER.equals(cl.getClass().getName())) { - continue; - } - if (conditionOnClass(SW_RUNNABLE_WRAPPER, cl) || conditionOnClass(SW_CALLABLE_WRAPPER, cl)) { - System.setProperty(DTP_EXECUTE_ENHANCED, FALSE_STR); - log.warn("DynamicTp init, disable enhancement for the execute method " + - "in the presence of the skywalking threadpool plugin."); - return; - } - } - } catch (Throwable e) { - log.error("DynamicTp {} init failed", getName(), e); - } - } - - private boolean conditionOnClass(String className, ClassLoader classLoader) { - try { - Class.forName(className, false, classLoader); - return true; - } catch (ClassNotFoundException e) { - return false; - } - } -} diff --git a/extension/extension-skywalking/src/main/resources/META-INF/services/org.dromara.dynamictp.core.support.init.DtpInitializer b/extension/extension-skywalking/src/main/resources/META-INF/services/org.dromara.dynamictp.core.support.init.DtpInitializer deleted file mode 100644 index aa2e33bdc..000000000 --- a/extension/extension-skywalking/src/main/resources/META-INF/services/org.dromara.dynamictp.core.support.init.DtpInitializer +++ /dev/null @@ -1 +0,0 @@ -org.dromara.dynamictp.extension.skywalking.init.SwInitializer \ No newline at end of file diff --git a/pom.xml b/pom.xml index 0e67e111e..10417deba 100644 --- a/pom.xml +++ b/pom.xml @@ -13,7 +13,7 @@ https://github.com/yanhom1314/dynamic-tp - 1.1.7 + 1.1.8-beta 8 8 diff --git a/test/pom.xml b/test/pom.xml index af7a147e9..0423f3252 100644 --- a/test/pom.xml +++ b/test/pom.xml @@ -17,6 +17,7 @@ test-core test-logging test-configcenter + test-extension diff --git a/test/test-extension/src/test/java/org/dromara/dynamictp/agent/AgentAwareTest.java b/test/test-extension/src/test/java/org/dromara/dynamictp/agent/AgentAwareTest.java index 89688a549..7525b188f 100644 --- a/test/test-extension/src/test/java/org/dromara/dynamictp/agent/AgentAwareTest.java +++ b/test/test-extension/src/test/java/org/dromara/dynamictp/agent/AgentAwareTest.java @@ -1,3 +1,20 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 org.dromara.dynamictp.agent; import org.dromara.dynamictp.core.aware.AgentAware; diff --git a/test/test-extension/src/test/java/org/dromara/dynamictp/agent/MyAgentTwoPathRunnableChildWrapper.java b/test/test-extension/src/test/java/org/dromara/dynamictp/agent/MyAgentTwoPathRunnableChildWrapper.java index 8f7e783cb..f76d06bf7 100644 --- a/test/test-extension/src/test/java/org/dromara/dynamictp/agent/MyAgentTwoPathRunnableChildWrapper.java +++ b/test/test-extension/src/test/java/org/dromara/dynamictp/agent/MyAgentTwoPathRunnableChildWrapper.java @@ -1,3 +1,20 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 org.dromara.dynamictp.agent; public class MyAgentTwoPathRunnableChildWrapper implements Runnable { diff --git a/test/test-extension/src/test/java/org/dromara/dynamictp/agent/MyAgentTwoPathRunnableWrapper.java b/test/test-extension/src/test/java/org/dromara/dynamictp/agent/MyAgentTwoPathRunnableWrapper.java index 3c7c622d5..0f3172661 100644 --- a/test/test-extension/src/test/java/org/dromara/dynamictp/agent/MyAgentTwoPathRunnableWrapper.java +++ b/test/test-extension/src/test/java/org/dromara/dynamictp/agent/MyAgentTwoPathRunnableWrapper.java @@ -1,3 +1,20 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 org.dromara.dynamictp.agent; public class MyAgentTwoPathRunnableWrapper implements Runnable { diff --git a/test/test-extension/src/test/java/org/dromara/dynamictp/agent/MyAgentWrapper.java b/test/test-extension/src/test/java/org/dromara/dynamictp/agent/MyAgentWrapper.java index 56edb3a8d..0217e03bd 100644 --- a/test/test-extension/src/test/java/org/dromara/dynamictp/agent/MyAgentWrapper.java +++ b/test/test-extension/src/test/java/org/dromara/dynamictp/agent/MyAgentWrapper.java @@ -1,3 +1,20 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 org.dromara.dynamictp.agent; public class MyAgentWrapper implements Runnable { diff --git a/test/test-extension/src/test/java/org/dromara/dynamictp/agent/MyAgentWrapperChild.java b/test/test-extension/src/test/java/org/dromara/dynamictp/agent/MyAgentWrapperChild.java index 44919ceae..91117d315 100644 --- a/test/test-extension/src/test/java/org/dromara/dynamictp/agent/MyAgentWrapperChild.java +++ b/test/test-extension/src/test/java/org/dromara/dynamictp/agent/MyAgentWrapperChild.java @@ -1,3 +1,20 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 org.dromara.dynamictp.agent; public class MyAgentWrapperChild extends MyAgentWrapper { diff --git a/test/test-extension/src/test/java/org/dromara/dynamictp/agent/MyAgentWrapperTwoRunnable.java b/test/test-extension/src/test/java/org/dromara/dynamictp/agent/MyAgentWrapperTwoRunnable.java index b63d9f404..c93c647dd 100644 --- a/test/test-extension/src/test/java/org/dromara/dynamictp/agent/MyAgentWrapperTwoRunnable.java +++ b/test/test-extension/src/test/java/org/dromara/dynamictp/agent/MyAgentWrapperTwoRunnable.java @@ -1,3 +1,20 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 org.dromara.dynamictp.agent; public class MyAgentWrapperTwoRunnable implements Runnable {