From f5f25f5832b6c0582c8ecbe94ed38786baec50d8 Mon Sep 17 00:00:00 2001 From: TheFatRatre <1981590178@qq.com> Date: Mon, 14 Oct 2024 18:33:13 +0800 Subject: [PATCH 01/26] =?UTF-8?q?virtual=20thread=E5=85=BC=E5=AE=B9?= =?UTF-8?q?=EF=BC=8C=E9=80=9A=E8=BF=87VirtualThreadExecutorAdapter?= =?UTF-8?q?=E8=BF=9B=E8=A1=8C=E5=A3=B0=E6=98=8E?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../core/spring/DtpPostProcessor.java | 18 +++++- .../core/support/ExecutorWrapper.java | 10 +++ .../support/VirtualThreadExecutorAdapter.java | 56 +++++++++++++++++ .../support/VirtualThreadExecutorProxy.java | 61 +++++++++++++++++++ .../config/ThreadPoolConfiguration.java | 12 ++-- pom.xml | 4 +- .../dynamictp/test/core/spring/Config.java | 6 ++ .../core/spring/DtpPostProcessorTest.java | 7 +++ 8 files changed, 165 insertions(+), 9 deletions(-) create mode 100644 core/src/main/java/org/dromara/dynamictp/core/support/VirtualThreadExecutorAdapter.java create mode 100644 core/src/main/java/org/dromara/dynamictp/core/support/VirtualThreadExecutorProxy.java diff --git a/core/src/main/java/org/dromara/dynamictp/core/spring/DtpPostProcessor.java b/core/src/main/java/org/dromara/dynamictp/core/spring/DtpPostProcessor.java index 78f768df3..597445dfd 100644 --- a/core/src/main/java/org/dromara/dynamictp/core/spring/DtpPostProcessor.java +++ b/core/src/main/java/org/dromara/dynamictp/core/spring/DtpPostProcessor.java @@ -32,6 +32,8 @@ import org.dromara.dynamictp.core.support.ExecutorWrapper; import org.dromara.dynamictp.core.support.ScheduledThreadPoolExecutorProxy; import org.dromara.dynamictp.core.support.ThreadPoolExecutorProxy; +import org.dromara.dynamictp.core.support.VirtualThreadExecutorAdapter; +import org.dromara.dynamictp.core.support.VirtualThreadExecutorProxy; import org.dromara.dynamictp.core.support.task.wrapper.TaskWrapper; import org.dromara.dynamictp.core.support.task.wrapper.TaskWrappers; import org.springframework.beans.BeansException; @@ -54,6 +56,7 @@ import java.util.Optional; import java.util.Set; import java.util.concurrent.Executor; +import java.util.concurrent.ExecutorService; import java.util.concurrent.ScheduledThreadPoolExecutor; import java.util.concurrent.ThreadPoolExecutor; @@ -76,7 +79,7 @@ public class DtpPostProcessor implements BeanPostProcessor, BeanFactoryAware, Pr /** * Compatible with lower versions of Spring. * - * @param bean the new bean instance + * @param bean the new bean instance * @param beanName the name of the bean * @return the bean instance to use * @throws BeansException in case of errors @@ -88,13 +91,14 @@ public Object postProcessBeforeInitialization(Object bean, String beanName) thro @Override public Object postProcessAfterInitialization(@NonNull Object bean, @NonNull String beanName) throws BeansException { - if (!(bean instanceof ThreadPoolExecutor) && !(bean instanceof ThreadPoolTaskExecutor)) { + if (!(bean instanceof ThreadPoolExecutor) && !(bean instanceof ThreadPoolTaskExecutor) && + !(bean instanceof VirtualThreadExecutorAdapter)) { return bean; } if (bean instanceof DtpExecutor) { return registerAndReturnDtp(bean); } - // register juc ThreadPoolExecutor or ThreadPoolTaskExecutor + // register juc ThreadPoolExecutor or ThreadPoolTaskExecutor or VirtualThreadExecutor return registerAndReturnCommon(bean, beanName); } @@ -155,6 +159,8 @@ private Object doRegisterAndReturnCommon(Object bean, String poolName) { Executor proxy; if (bean instanceof ScheduledThreadPoolExecutor) { proxy = newScheduledTpProxy(poolName, (ScheduledThreadPoolExecutor) bean); + } else if (bean instanceof VirtualThreadExecutorAdapter) { + proxy = newVirtualThreadProxy(poolName, ((VirtualThreadExecutorAdapter) bean).getOriginal()); } else { proxy = newProxy(poolName, (ThreadPoolExecutor) bean); } @@ -184,6 +190,12 @@ private ScheduledThreadPoolExecutorProxy newScheduledTpProxy(String name, Schedu return proxy; } + private VirtualThreadExecutorProxy newVirtualThreadProxy(String name, ExecutorService originExecutor) { + val proxy = new VirtualThreadExecutorProxy(); + shutdownGracefulAsync( originExecutor, name, 0); + return proxy; + } + private void tryWrapTaskDecorator(String poolName, ThreadPoolTaskExecutor poolTaskExecutor, ThreadPoolExecutorProxy proxy) throws IllegalAccessException { Object taskDecorator = ReflectionUtil.getFieldValue("taskDecorator", poolTaskExecutor); if (Objects.isNull(taskDecorator)) { diff --git a/core/src/main/java/org/dromara/dynamictp/core/support/ExecutorWrapper.java b/core/src/main/java/org/dromara/dynamictp/core/support/ExecutorWrapper.java index 4b02f4e2d..311abe489 100644 --- a/core/src/main/java/org/dromara/dynamictp/core/support/ExecutorWrapper.java +++ b/core/src/main/java/org/dromara/dynamictp/core/support/ExecutorWrapper.java @@ -178,6 +178,8 @@ public void initialize() { AwareManager.register(this); } else if (isThreadPoolExecutor()) { AwareManager.register(this); + } else if (isVirtualThreadExecutor()) { + AwareManager.register(this); } } @@ -203,6 +205,14 @@ public boolean isThreadPoolExecutor() { return this.executor instanceof ThreadPoolExecutorAdapter; } + /** + * whether is VirtualThreadExecutor + * @return boolean + */ + private boolean isVirtualThreadExecutor() { + return this.executor instanceof VirtualThreadExecutorAdapter; + } + /** * set taskWrappers * diff --git a/core/src/main/java/org/dromara/dynamictp/core/support/VirtualThreadExecutorAdapter.java b/core/src/main/java/org/dromara/dynamictp/core/support/VirtualThreadExecutorAdapter.java new file mode 100644 index 000000000..7fb68ee4e --- /dev/null +++ b/core/src/main/java/org/dromara/dynamictp/core/support/VirtualThreadExecutorAdapter.java @@ -0,0 +1,56 @@ +package org.dromara.dynamictp.core.support; + +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.ThreadPoolExecutor; + +/** + * ClassName: VirtualThreadExecutorAdapter + * Package: org.dromara.dynamictp.core.support + * Description: + * Adapter for virtual thread executor + * @Author CYC + * @Create 2024/10/14 15:33 + * @Version 1.0 + */ +public class VirtualThreadExecutorAdapter implements ExecutorAdapter { + + private final ExecutorService executor = Executors.newVirtualThreadPerTaskExecutor(); + + @Override + public ExecutorService getOriginal() { + return this.executor; + } + + @Override + public int getCorePoolSize() { + return 0; + } + + @Override + public void setCorePoolSize(int corePoolSize) { + + } + + @Override + public int getMaximumPoolSize() { + return 0; + } + + @Override + public void setMaximumPoolSize(int maximumPoolSize) { + + } + + @Override + public int getPoolSize() { + return 0; + } + + @Override + public int getActiveCount() { + return 0; + } + + +} diff --git a/core/src/main/java/org/dromara/dynamictp/core/support/VirtualThreadExecutorProxy.java b/core/src/main/java/org/dromara/dynamictp/core/support/VirtualThreadExecutorProxy.java new file mode 100644 index 000000000..a03bb6b10 --- /dev/null +++ b/core/src/main/java/org/dromara/dynamictp/core/support/VirtualThreadExecutorProxy.java @@ -0,0 +1,61 @@ +package org.dromara.dynamictp.core.support; + +import org.dromara.dynamictp.core.aware.AwareManager; +import org.dromara.dynamictp.core.aware.RejectHandlerAware; +import org.dromara.dynamictp.core.aware.TaskEnhanceAware; +import org.dromara.dynamictp.core.support.task.wrapper.TaskWrapper; + +import java.util.List; + +/** + * ClassName: VirtualThreadExecutorProxy + * Package: org.dromara.dynamictp.core.support + * Description: + * + * @Author CYC + * @Create 2024/10/14 15:59 + * @Version 1.0 + */ +public class VirtualThreadExecutorProxy extends VirtualThreadExecutorAdapter implements TaskEnhanceAware, RejectHandlerAware { + + /** + * Task wrappers, do sth enhanced. + */ + private List taskWrappers; + + /** + * Reject handler type. + */ + private String rejectHandlerType; + public VirtualThreadExecutorProxy() { + super(); + } + + @Override + public void execute(Runnable command) { + command = getEnhancedTask(command); + AwareManager.execute(this, command); + super.execute(command); + } + + + @Override + public List getTaskWrappers() { + return taskWrappers; + } + + @Override + public void setTaskWrappers(List taskWrappers) { + this.taskWrappers = taskWrappers; + } + + @Override + public String getRejectHandlerType() { + return rejectHandlerType; + } + + @Override + public void setRejectHandlerType(String rejectHandlerType) { + this.rejectHandlerType = rejectHandlerType; + } +} diff --git a/example/example-nacos-cloud/src/main/java/org/dromara/dynamictp/example/config/ThreadPoolConfiguration.java b/example/example-nacos-cloud/src/main/java/org/dromara/dynamictp/example/config/ThreadPoolConfiguration.java index ff51f8d24..0bae57ec4 100644 --- a/example/example-nacos-cloud/src/main/java/org/dromara/dynamictp/example/config/ThreadPoolConfiguration.java +++ b/example/example-nacos-cloud/src/main/java/org/dromara/dynamictp/example/config/ThreadPoolConfiguration.java @@ -22,14 +22,12 @@ import org.dromara.dynamictp.core.support.DynamicTp; import org.dromara.dynamictp.core.support.ThreadPoolBuilder; import org.dromara.dynamictp.core.support.ThreadPoolCreator; +import org.dromara.dynamictp.core.support.VirtualThreadExecutorAdapter; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor; -import java.util.concurrent.Executors; -import java.util.concurrent.ScheduledExecutorService; -import java.util.concurrent.ThreadPoolExecutor; -import java.util.concurrent.TimeUnit; +import java.util.concurrent.*; import static org.dromara.dynamictp.common.em.QueueTypeEnum.MEMORY_SAFE_LINKED_BLOCKING_QUEUE; import static org.dromara.dynamictp.common.em.RejectedTypeEnum.CALLER_RUNS_POLICY; @@ -62,6 +60,12 @@ public ThreadPoolTaskExecutor threadPoolTaskExecutor() { return new ThreadPoolTaskExecutor(); } + @DynamicTp("VirtualThreadExecutor") + @Bean + public VirtualThreadExecutorAdapter virtualThreadExecutor() { + return new VirtualThreadExecutorAdapter(); + } + /** * 通过{@link ThreadPoolCreator} 快速创建一些简单配置的线程池,使用默认参数 * tips: 建议直接在配置中心配置就行,不用@Bean声明 diff --git a/pom.xml b/pom.xml index 65836eed0..fe51f4e10 100644 --- a/pom.xml +++ b/pom.xml @@ -14,8 +14,8 @@ 1.1.9-3.x - 17 - 17 + 21 + 21 3.1.4 2022.0.3 diff --git a/test/test-core/src/test/java/org/dromara/dynamictp/test/core/spring/Config.java b/test/test-core/src/test/java/org/dromara/dynamictp/test/core/spring/Config.java index 1e2be72d3..ae8fd8ba9 100644 --- a/test/test-core/src/test/java/org/dromara/dynamictp/test/core/spring/Config.java +++ b/test/test-core/src/test/java/org/dromara/dynamictp/test/core/spring/Config.java @@ -19,6 +19,7 @@ import org.dromara.dynamictp.core.spring.EnableDynamicTp; import org.dromara.dynamictp.core.support.DynamicTp; +import org.dromara.dynamictp.core.support.VirtualThreadExecutorAdapter; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @@ -64,4 +65,9 @@ public ThreadPoolExecutor commonExecutor() { public ThreadPoolTaskExecutor taskExecutor() { return new ThreadPoolTaskExecutor(); } + @DynamicTp("VirtualThreadExecutor") + @Bean + public VirtualThreadExecutorAdapter virtualThreadExecutor() { + return new VirtualThreadExecutorAdapter(); + } } diff --git a/test/test-core/src/test/java/org/dromara/dynamictp/test/core/spring/DtpPostProcessorTest.java b/test/test-core/src/test/java/org/dromara/dynamictp/test/core/spring/DtpPostProcessorTest.java index eebf2c16d..eff42b08f 100644 --- a/test/test-core/src/test/java/org/dromara/dynamictp/test/core/spring/DtpPostProcessorTest.java +++ b/test/test-core/src/test/java/org/dromara/dynamictp/test/core/spring/DtpPostProcessorTest.java @@ -21,6 +21,7 @@ import org.dromara.dynamictp.core.spring.EnableDynamicTp; import org.dromara.dynamictp.core.spring.YamlPropertySourceFactory; import org.dromara.dynamictp.core.support.ThreadPoolExecutorProxy; +import org.dromara.dynamictp.core.support.VirtualThreadExecutorAdapter; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; @@ -64,4 +65,10 @@ void test() { taskExecutor.execute(() -> System.out.println("enhance taskExecutor success!")); } + @Test + void test2() { + VirtualThreadExecutorAdapter virtualThreadExecutorAdapter = (VirtualThreadExecutorAdapter) DtpRegistry.getExecutor("VirtualThreadExecutor"); + virtualThreadExecutorAdapter.getOriginal().execute(() -> System.out.println("VirtualThreadExecutorAdapter1 registered!")); + Assertions.assertNotNull(virtualThreadExecutorAdapter); + } } From cc83c79b4d8b00d10894751e6291636f3a94bd1d Mon Sep 17 00:00:00 2001 From: TheFatRatre <1981590178@qq.com> Date: Mon, 14 Oct 2024 19:48:55 +0800 Subject: [PATCH 02/26] =?UTF-8?q?bean=20instanceof=20ThreadContainer=20&&?= =?UTF-8?q?=20bean=20instanceof=20=20ExecutorService=20=E7=9A=84error?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../org/dromara/dynamictp/core/spring/DtpPostProcessor.java | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/core/src/main/java/org/dromara/dynamictp/core/spring/DtpPostProcessor.java b/core/src/main/java/org/dromara/dynamictp/core/spring/DtpPostProcessor.java index 597445dfd..ba9ac9509 100644 --- a/core/src/main/java/org/dromara/dynamictp/core/spring/DtpPostProcessor.java +++ b/core/src/main/java/org/dromara/dynamictp/core/spring/DtpPostProcessor.java @@ -18,6 +18,7 @@ package org.dromara.dynamictp.core.spring; import com.google.common.collect.Lists; +import jdk.internal.vm.ThreadContainer; import lombok.extern.slf4j.Slf4j; import lombok.val; import org.apache.commons.lang3.StringUtils; @@ -92,7 +93,10 @@ public Object postProcessBeforeInitialization(Object bean, String beanName) thro @Override public Object postProcessAfterInitialization(@NonNull Object bean, @NonNull String beanName) throws BeansException { if (!(bean instanceof ThreadPoolExecutor) && !(bean instanceof ThreadPoolTaskExecutor) && - !(bean instanceof VirtualThreadExecutorAdapter)) { + !(bean instanceof VirtualThreadExecutorAdapter) && !(bean instanceof ThreadContainer && bean instanceof ExecutorService)) +// if (!(bean instanceof ThreadPoolExecutor) && !(bean instanceof ThreadPoolTaskExecutor) && +// !(bean instanceof VirtualThreadExecutorAdapter)) + { return bean; } if (bean instanceof DtpExecutor) { From 0d0c16028b16a9d851594a3a16542ab701d7a4b1 Mon Sep 17 00:00:00 2001 From: TheFatRatre <1981590178@qq.com> Date: Tue, 15 Oct 2024 15:59:05 +0800 Subject: [PATCH 03/26] =?UTF-8?q?=E4=BD=BF=E7=94=A8Executors.newVirtualThr?= =?UTF-8?q?eadPerTaskExecutor()=E8=BF=9B=E8=A1=8C=E5=A3=B0=E6=98=8E?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../core/spring/DtpPostProcessor.java | 24 ++---- .../core/support/ExecutorWrapper.java | 2 + .../support/VirtualThreadExecutorAdapter.java | 17 +++- .../support/VirtualThreadExecutorProxy.java | 85 ++++++++++++++++--- .../dynamictp/test/core/spring/Config.java | 5 +- .../core/spring/DtpPostProcessorTest.java | 7 +- 6 files changed, 106 insertions(+), 34 deletions(-) diff --git a/core/src/main/java/org/dromara/dynamictp/core/spring/DtpPostProcessor.java b/core/src/main/java/org/dromara/dynamictp/core/spring/DtpPostProcessor.java index ba9ac9509..c64c7dfb0 100644 --- a/core/src/main/java/org/dromara/dynamictp/core/spring/DtpPostProcessor.java +++ b/core/src/main/java/org/dromara/dynamictp/core/spring/DtpPostProcessor.java @@ -18,7 +18,6 @@ package org.dromara.dynamictp.core.spring; import com.google.common.collect.Lists; -import jdk.internal.vm.ThreadContainer; import lombok.extern.slf4j.Slf4j; import lombok.val; import org.apache.commons.lang3.StringUtils; @@ -29,12 +28,7 @@ import org.dromara.dynamictp.core.executor.DtpExecutor; import org.dromara.dynamictp.core.executor.eager.EagerDtpExecutor; import org.dromara.dynamictp.core.executor.eager.TaskQueue; -import org.dromara.dynamictp.core.support.DynamicTp; -import org.dromara.dynamictp.core.support.ExecutorWrapper; -import org.dromara.dynamictp.core.support.ScheduledThreadPoolExecutorProxy; -import org.dromara.dynamictp.core.support.ThreadPoolExecutorProxy; -import org.dromara.dynamictp.core.support.VirtualThreadExecutorAdapter; -import org.dromara.dynamictp.core.support.VirtualThreadExecutorProxy; +import org.dromara.dynamictp.core.support.*; import org.dromara.dynamictp.core.support.task.wrapper.TaskWrapper; import org.dromara.dynamictp.core.support.task.wrapper.TaskWrappers; import org.springframework.beans.BeansException; @@ -93,10 +87,7 @@ public Object postProcessBeforeInitialization(Object bean, String beanName) thro @Override public Object postProcessAfterInitialization(@NonNull Object bean, @NonNull String beanName) throws BeansException { if (!(bean instanceof ThreadPoolExecutor) && !(bean instanceof ThreadPoolTaskExecutor) && - !(bean instanceof VirtualThreadExecutorAdapter) && !(bean instanceof ThreadContainer && bean instanceof ExecutorService)) -// if (!(bean instanceof ThreadPoolExecutor) && !(bean instanceof ThreadPoolTaskExecutor) && -// !(bean instanceof VirtualThreadExecutorAdapter)) - { + !(bean.getClass().getName().equals("java.util.concurrent.ThreadPerTaskExecutor"))) { return bean; } if (bean instanceof DtpExecutor) { @@ -156,15 +147,16 @@ private Object doRegisterAndReturnCommon(Object bean, String poolName) { try { ReflectionUtil.setFieldValue("threadPoolExecutor", bean, proxy); tryWrapTaskDecorator(poolName, poolTaskExecutor, proxy); - } catch (IllegalAccessException ignored) { } + } catch (IllegalAccessException ignored) { + } DtpRegistry.registerExecutor(new ExecutorWrapper(poolName, proxy), REGISTER_SOURCE); return bean; } Executor proxy; if (bean instanceof ScheduledThreadPoolExecutor) { proxy = newScheduledTpProxy(poolName, (ScheduledThreadPoolExecutor) bean); - } else if (bean instanceof VirtualThreadExecutorAdapter) { - proxy = newVirtualThreadProxy(poolName, ((VirtualThreadExecutorAdapter) bean).getOriginal()); + } else if (bean.getClass().getName().equals("java.util.concurrent.ThreadPerTaskExecutor")) { + proxy = newVirtualThreadProxy(poolName, (ExecutorService) bean); } else { proxy = newProxy(poolName, (ThreadPoolExecutor) bean); } @@ -195,8 +187,8 @@ private ScheduledThreadPoolExecutorProxy newScheduledTpProxy(String name, Schedu } private VirtualThreadExecutorProxy newVirtualThreadProxy(String name, ExecutorService originExecutor) { - val proxy = new VirtualThreadExecutorProxy(); - shutdownGracefulAsync( originExecutor, name, 0); + val proxy = new VirtualThreadExecutorProxy(originExecutor); + shutdownGracefulAsync(originExecutor, name, 0); return proxy; } diff --git a/core/src/main/java/org/dromara/dynamictp/core/support/ExecutorWrapper.java b/core/src/main/java/org/dromara/dynamictp/core/support/ExecutorWrapper.java index 311abe489..d1d96f7c7 100644 --- a/core/src/main/java/org/dromara/dynamictp/core/support/ExecutorWrapper.java +++ b/core/src/main/java/org/dromara/dynamictp/core/support/ExecutorWrapper.java @@ -139,6 +139,8 @@ public ExecutorWrapper(String threadPoolName, Executor executor) { this.executor = new ThreadPoolExecutorAdapter((ThreadPoolExecutor) executor); } else if (executor instanceof ExecutorAdapter) { this.executor = (ExecutorAdapter) executor; + } else if (executor instanceof VirtualThreadExecutorProxy) { + this.executor = new VirtualThreadExecutorAdapter(); } else { throw new IllegalArgumentException("unsupported Executor type !"); } diff --git a/core/src/main/java/org/dromara/dynamictp/core/support/VirtualThreadExecutorAdapter.java b/core/src/main/java/org/dromara/dynamictp/core/support/VirtualThreadExecutorAdapter.java index 7fb68ee4e..2e871877d 100644 --- a/core/src/main/java/org/dromara/dynamictp/core/support/VirtualThreadExecutorAdapter.java +++ b/core/src/main/java/org/dromara/dynamictp/core/support/VirtualThreadExecutorAdapter.java @@ -13,7 +13,7 @@ * @Create 2024/10/14 15:33 * @Version 1.0 */ -public class VirtualThreadExecutorAdapter implements ExecutorAdapter { +public class VirtualThreadExecutorAdapter implements ExecutorAdapter{ private final ExecutorService executor = Executors.newVirtualThreadPerTaskExecutor(); @@ -22,6 +22,11 @@ public ExecutorService getOriginal() { return this.executor; } + @Override + public void execute(Runnable command) { + this.executor.execute(command); + } + @Override public int getCorePoolSize() { return 0; @@ -52,5 +57,15 @@ public int getActiveCount() { return 0; } + @Override + public boolean isShutdown() { + return this.executor.isShutdown(); + } + + @Override + public boolean isTerminated() { + return this.executor.isTerminated(); + } + } diff --git a/core/src/main/java/org/dromara/dynamictp/core/support/VirtualThreadExecutorProxy.java b/core/src/main/java/org/dromara/dynamictp/core/support/VirtualThreadExecutorProxy.java index a03bb6b10..a54179c15 100644 --- a/core/src/main/java/org/dromara/dynamictp/core/support/VirtualThreadExecutorProxy.java +++ b/core/src/main/java/org/dromara/dynamictp/core/support/VirtualThreadExecutorProxy.java @@ -1,41 +1,43 @@ package org.dromara.dynamictp.core.support; import org.dromara.dynamictp.core.aware.AwareManager; -import org.dromara.dynamictp.core.aware.RejectHandlerAware; import org.dromara.dynamictp.core.aware.TaskEnhanceAware; import org.dromara.dynamictp.core.support.task.wrapper.TaskWrapper; +import java.util.Collection; import java.util.List; +import java.util.concurrent.*; /** * ClassName: VirtualThreadExecutorProxy * Package: org.dromara.dynamictp.core.support * Description: + * VirtualThreadExecutor Proxy * * @Author CYC * @Create 2024/10/14 15:59 * @Version 1.0 */ -public class VirtualThreadExecutorProxy extends VirtualThreadExecutorAdapter implements TaskEnhanceAware, RejectHandlerAware { +public class VirtualThreadExecutorProxy implements TaskEnhanceAware, ExecutorService { + + private final ExecutorService THREAD_PER_TASK_EXECUTOR; /** * Task wrappers, do sth enhanced. */ private List taskWrappers; - /** - * Reject handler type. - */ - private String rejectHandlerType; - public VirtualThreadExecutorProxy() { + + public VirtualThreadExecutorProxy(ExecutorService executor) { super(); + THREAD_PER_TASK_EXECUTOR = executor; } @Override public void execute(Runnable command) { command = getEnhancedTask(command); AwareManager.execute(this, command); - super.execute(command); + THREAD_PER_TASK_EXECUTOR.execute(command); } @@ -49,13 +51,72 @@ public void setTaskWrappers(List taskWrappers) { this.taskWrappers = taskWrappers; } + + @Override + public void shutdown() { + THREAD_PER_TASK_EXECUTOR.shutdown(); + } + + @Override + public List shutdownNow() { + return THREAD_PER_TASK_EXECUTOR.shutdownNow(); + } + + @Override + public boolean isShutdown() { + return THREAD_PER_TASK_EXECUTOR.isShutdown(); + } + + @Override + public boolean isTerminated() { + return THREAD_PER_TASK_EXECUTOR.isTerminated(); + } + + @Override + public boolean awaitTermination(long l, TimeUnit timeUnit) throws InterruptedException { + return THREAD_PER_TASK_EXECUTOR.awaitTermination(l, timeUnit); + } + + @Override + public Future submit(Callable callable) { + FutureTask futureTask = new FutureTask(callable); + futureTask = (FutureTask) getEnhancedTask(futureTask); + AwareManager.execute(this, futureTask); + return THREAD_PER_TASK_EXECUTOR.submit(callable); + } + + + @Override + public Future submit(Runnable runnable, T t) { + runnable = getEnhancedTask(runnable); + AwareManager.execute(this, runnable); + return THREAD_PER_TASK_EXECUTOR.submit(runnable, t); + } + + @Override + public Future submit(Runnable runnable) { + runnable = getEnhancedTask(runnable); + AwareManager.execute(this, runnable); + return THREAD_PER_TASK_EXECUTOR.submit(runnable); + } + + @Override + public List> invokeAll(Collection> collection) throws InterruptedException { + return THREAD_PER_TASK_EXECUTOR.invokeAll(collection); + } + + @Override + public List> invokeAll(Collection> collection, long l, TimeUnit timeUnit) throws InterruptedException { + return THREAD_PER_TASK_EXECUTOR.invokeAll(collection, l, timeUnit); + } + @Override - public String getRejectHandlerType() { - return rejectHandlerType; + public T invokeAny(Collection> collection) throws InterruptedException, ExecutionException { + return THREAD_PER_TASK_EXECUTOR.invokeAny(collection); } @Override - public void setRejectHandlerType(String rejectHandlerType) { - this.rejectHandlerType = rejectHandlerType; + public T invokeAny(Collection> collection, long l, TimeUnit timeUnit) throws InterruptedException, ExecutionException, TimeoutException { + return THREAD_PER_TASK_EXECUTOR.invokeAny(collection, l, timeUnit); } } diff --git a/test/test-core/src/test/java/org/dromara/dynamictp/test/core/spring/Config.java b/test/test-core/src/test/java/org/dromara/dynamictp/test/core/spring/Config.java index ae8fd8ba9..583df1fb9 100644 --- a/test/test-core/src/test/java/org/dromara/dynamictp/test/core/spring/Config.java +++ b/test/test-core/src/test/java/org/dromara/dynamictp/test/core/spring/Config.java @@ -26,6 +26,7 @@ import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor; import java.util.concurrent.Executor; +import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.ThreadPoolExecutor; @@ -67,7 +68,7 @@ public ThreadPoolTaskExecutor taskExecutor() { } @DynamicTp("VirtualThreadExecutor") @Bean - public VirtualThreadExecutorAdapter virtualThreadExecutor() { - return new VirtualThreadExecutorAdapter(); + public ExecutorService virtualThreadExecutor() { + return Executors.newVirtualThreadPerTaskExecutor(); } } diff --git a/test/test-core/src/test/java/org/dromara/dynamictp/test/core/spring/DtpPostProcessorTest.java b/test/test-core/src/test/java/org/dromara/dynamictp/test/core/spring/DtpPostProcessorTest.java index eff42b08f..e53da3d62 100644 --- a/test/test-core/src/test/java/org/dromara/dynamictp/test/core/spring/DtpPostProcessorTest.java +++ b/test/test-core/src/test/java/org/dromara/dynamictp/test/core/spring/DtpPostProcessorTest.java @@ -33,6 +33,7 @@ import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor; import java.util.concurrent.Executor; +import java.util.concurrent.ExecutorService; import java.util.concurrent.ThreadPoolExecutor; /** @@ -67,8 +68,8 @@ void test() { @Test void test2() { - VirtualThreadExecutorAdapter virtualThreadExecutorAdapter = (VirtualThreadExecutorAdapter) DtpRegistry.getExecutor("VirtualThreadExecutor"); - virtualThreadExecutorAdapter.getOriginal().execute(() -> System.out.println("VirtualThreadExecutorAdapter1 registered!")); - Assertions.assertNotNull(virtualThreadExecutorAdapter); + Executor virtualThreadExecutor = DtpRegistry.getExecutor("VirtualThreadExecutor"); + virtualThreadExecutor.execute(() -> System.out.println("VirtualThreadExecutor registered!")); + Assertions.assertNotNull(virtualThreadExecutor); } } From 74f64c66f84df39b0edb7aad0dcc28d9c6d94a36 Mon Sep 17 00:00:00 2001 From: TheFatRatre <1981590178@qq.com> Date: Tue, 15 Oct 2024 16:10:46 +0800 Subject: [PATCH 04/26] format --- .../core/support/VirtualThreadExecutorAdapter.java | 1 - .../core/support/VirtualThreadExecutorProxy.java | 8 +++++++- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/core/src/main/java/org/dromara/dynamictp/core/support/VirtualThreadExecutorAdapter.java b/core/src/main/java/org/dromara/dynamictp/core/support/VirtualThreadExecutorAdapter.java index 2e871877d..8b6cc0727 100644 --- a/core/src/main/java/org/dromara/dynamictp/core/support/VirtualThreadExecutorAdapter.java +++ b/core/src/main/java/org/dromara/dynamictp/core/support/VirtualThreadExecutorAdapter.java @@ -2,7 +2,6 @@ import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; -import java.util.concurrent.ThreadPoolExecutor; /** * ClassName: VirtualThreadExecutorAdapter diff --git a/core/src/main/java/org/dromara/dynamictp/core/support/VirtualThreadExecutorProxy.java b/core/src/main/java/org/dromara/dynamictp/core/support/VirtualThreadExecutorProxy.java index a54179c15..900563a5d 100644 --- a/core/src/main/java/org/dromara/dynamictp/core/support/VirtualThreadExecutorProxy.java +++ b/core/src/main/java/org/dromara/dynamictp/core/support/VirtualThreadExecutorProxy.java @@ -6,7 +6,13 @@ import java.util.Collection; import java.util.List; -import java.util.concurrent.*; +import java.util.concurrent.Callable; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Future; +import java.util.concurrent.FutureTask; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.TimeoutException; /** * ClassName: VirtualThreadExecutorProxy From 17a913b53bcd48b208050998e0bb978002015686 Mon Sep 17 00:00:00 2001 From: TheFatRatre <1981590178@qq.com> Date: Wed, 16 Oct 2024 15:45:21 +0800 Subject: [PATCH 05/26] =?UTF-8?q?=E4=BB=A5=E6=B3=A8=E9=87=8A=E7=9A=84?= =?UTF-8?q?=E6=96=B9=E5=BC=8F=E6=9D=A5=E5=A3=B0=E6=98=8Eexecutor=E7=9A=84?= =?UTF-8?q?=E5=AE=9E=E7=8E=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../dromara/dynamictp/core/DtpRegistry.java | 23 +++++++++++-------- .../core/spring/DtpPostProcessor.java | 6 ++++- .../core/support/ExecutorWrapper.java | 2 +- .../support/VirtualThreadExecutorProxy.java | 10 ++++++++ .../core/spring/DtpPostProcessorTest.java | 4 ++++ 5 files changed, 33 insertions(+), 12 deletions(-) diff --git a/core/src/main/java/org/dromara/dynamictp/core/DtpRegistry.java b/core/src/main/java/org/dromara/dynamictp/core/DtpRegistry.java index 0bfba6557..76bc98006 100644 --- a/core/src/main/java/org/dromara/dynamictp/core/DtpRegistry.java +++ b/core/src/main/java/org/dromara/dynamictp/core/DtpRegistry.java @@ -194,7 +194,7 @@ private static void refresh(ExecutorWrapper executorWrapper, DtpExecutorProps pr TpMainFields oldFields = ExecutorConverter.toMainFields(executorWrapper); doRefresh(executorWrapper, props); TpMainFields newFields = ExecutorConverter.toMainFields(executorWrapper); - if (oldFields.equals(newFields)) { + if (oldFields.equals(newFields) && !executorWrapper.isVirtualThreadExecutor()) { log.debug("DynamicTp refresh, main properties of [{}] have not changed.", executorWrapper.getThreadPoolName()); return; @@ -225,8 +225,10 @@ private static void doRefresh(ExecutorWrapper executorWrapper, DtpExecutorProps if (!Objects.equals(executor.allowsCoreThreadTimeOut(), props.isAllowCoreThreadTimeOut())) { executor.allowCoreThreadTimeOut(props.isAllowCoreThreadTimeOut()); } - // update queue - updateQueueProps(executor, props); + if(!executorWrapper.isVirtualThreadExecutor()) { + // update queue + updateQueueProps(executor, props); + } if (executorWrapper.isDtpExecutor()) { doRefreshDtp(executorWrapper, props); @@ -240,13 +242,14 @@ private static void doRefreshCommon(ExecutorWrapper executorWrapper, DtpExecutor if (StringUtils.isNotBlank(props.getThreadPoolAliasName())) { executorWrapper.setThreadPoolAliasName(props.getThreadPoolAliasName()); } - - ExecutorAdapter executor = executorWrapper.getExecutor(); - // update reject handler - String currentRejectHandlerType = executor.getRejectHandlerType(); - if (!Objects.equals(currentRejectHandlerType, props.getRejectedHandlerType())) { - val rejectHandler = RejectHandlerGetter.buildRejectedHandler(props.getRejectedHandlerType()); - executorWrapper.setRejectHandler(rejectHandler); + if(!executorWrapper.isVirtualThreadExecutor()) { + ExecutorAdapter executor = executorWrapper.getExecutor(); + // update reject handler + String currentRejectHandlerType = executor.getRejectHandlerType(); + if (!Objects.equals(currentRejectHandlerType, props.getRejectedHandlerType())) { + val rejectHandler = RejectHandlerGetter.buildRejectedHandler(props.getRejectedHandlerType()); + executorWrapper.setRejectHandler(rejectHandler); + } } List taskWrappers = TaskWrappers.getInstance().getByNames(props.getTaskWrapperNames()); diff --git a/core/src/main/java/org/dromara/dynamictp/core/spring/DtpPostProcessor.java b/core/src/main/java/org/dromara/dynamictp/core/spring/DtpPostProcessor.java index c64c7dfb0..f90ee68d9 100644 --- a/core/src/main/java/org/dromara/dynamictp/core/spring/DtpPostProcessor.java +++ b/core/src/main/java/org/dromara/dynamictp/core/spring/DtpPostProcessor.java @@ -28,7 +28,11 @@ import org.dromara.dynamictp.core.executor.DtpExecutor; import org.dromara.dynamictp.core.executor.eager.EagerDtpExecutor; import org.dromara.dynamictp.core.executor.eager.TaskQueue; -import org.dromara.dynamictp.core.support.*; +import org.dromara.dynamictp.core.support.DynamicTp; +import org.dromara.dynamictp.core.support.ExecutorWrapper; +import org.dromara.dynamictp.core.support.ScheduledThreadPoolExecutorProxy; +import org.dromara.dynamictp.core.support.ThreadPoolExecutorProxy; +import org.dromara.dynamictp.core.support.VirtualThreadExecutorProxy; import org.dromara.dynamictp.core.support.task.wrapper.TaskWrapper; import org.dromara.dynamictp.core.support.task.wrapper.TaskWrappers; import org.springframework.beans.BeansException; diff --git a/core/src/main/java/org/dromara/dynamictp/core/support/ExecutorWrapper.java b/core/src/main/java/org/dromara/dynamictp/core/support/ExecutorWrapper.java index d1d96f7c7..7dca15fa0 100644 --- a/core/src/main/java/org/dromara/dynamictp/core/support/ExecutorWrapper.java +++ b/core/src/main/java/org/dromara/dynamictp/core/support/ExecutorWrapper.java @@ -211,7 +211,7 @@ public boolean isThreadPoolExecutor() { * whether is VirtualThreadExecutor * @return boolean */ - private boolean isVirtualThreadExecutor() { + public boolean isVirtualThreadExecutor() { return this.executor instanceof VirtualThreadExecutorAdapter; } diff --git a/core/src/main/java/org/dromara/dynamictp/core/support/VirtualThreadExecutorProxy.java b/core/src/main/java/org/dromara/dynamictp/core/support/VirtualThreadExecutorProxy.java index 900563a5d..be82a84a5 100644 --- a/core/src/main/java/org/dromara/dynamictp/core/support/VirtualThreadExecutorProxy.java +++ b/core/src/main/java/org/dromara/dynamictp/core/support/VirtualThreadExecutorProxy.java @@ -1,11 +1,16 @@ package org.dromara.dynamictp.core.support; +import com.google.common.collect.Sets; +import org.dromara.dynamictp.common.em.NotifyItemEnum; +import org.dromara.dynamictp.common.entity.NotifyItem; 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 java.util.Collection; import java.util.List; +import java.util.Set; import java.util.concurrent.Callable; import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutorService; @@ -42,6 +47,7 @@ public VirtualThreadExecutorProxy(ExecutorService executor) { @Override public void execute(Runnable command) { command = getEnhancedTask(command); + EnhancedRunnable.of(command, this); AwareManager.execute(this, command); THREAD_PER_TASK_EXECUTOR.execute(command); } @@ -87,6 +93,7 @@ public boolean awaitTermination(long l, TimeUnit timeUnit) throws InterruptedExc public Future submit(Callable callable) { FutureTask futureTask = new FutureTask(callable); futureTask = (FutureTask) getEnhancedTask(futureTask); + EnhancedRunnable.of(futureTask, this); AwareManager.execute(this, futureTask); return THREAD_PER_TASK_EXECUTOR.submit(callable); } @@ -95,6 +102,7 @@ public Future submit(Callable callable) { @Override public Future submit(Runnable runnable, T t) { runnable = getEnhancedTask(runnable); + EnhancedRunnable.of(runnable, this); AwareManager.execute(this, runnable); return THREAD_PER_TASK_EXECUTOR.submit(runnable, t); } @@ -102,6 +110,7 @@ public Future submit(Runnable runnable, T t) { @Override public Future submit(Runnable runnable) { runnable = getEnhancedTask(runnable); + EnhancedRunnable.of(runnable, this); AwareManager.execute(this, runnable); return THREAD_PER_TASK_EXECUTOR.submit(runnable); } @@ -125,4 +134,5 @@ public T invokeAny(Collection> collection) throws Inte public T invokeAny(Collection> collection, long l, TimeUnit timeUnit) throws InterruptedException, ExecutionException, TimeoutException { return THREAD_PER_TASK_EXECUTOR.invokeAny(collection, l, timeUnit); } + } diff --git a/test/test-core/src/test/java/org/dromara/dynamictp/test/core/spring/DtpPostProcessorTest.java b/test/test-core/src/test/java/org/dromara/dynamictp/test/core/spring/DtpPostProcessorTest.java index e53da3d62..4925d167c 100644 --- a/test/test-core/src/test/java/org/dromara/dynamictp/test/core/spring/DtpPostProcessorTest.java +++ b/test/test-core/src/test/java/org/dromara/dynamictp/test/core/spring/DtpPostProcessorTest.java @@ -71,5 +71,9 @@ void test2() { Executor virtualThreadExecutor = DtpRegistry.getExecutor("VirtualThreadExecutor"); virtualThreadExecutor.execute(() -> System.out.println("VirtualThreadExecutor registered!")); Assertions.assertNotNull(virtualThreadExecutor); + +// Executor virtualThreadExecutor2 = DtpRegistry.getExecutor("VirtualThreadExecutor2"); +// virtualThreadExecutor.execute(() -> System.out.println("VirtualThreadExecutor2 registered!")); +// Assertions.assertNotNull(virtualThreadExecutor2); } } From 706d88766e8b8c72ad7ec9da91dc139fe648a69a Mon Sep 17 00:00:00 2001 From: TheFatRatre <1981590178@qq.com> Date: Wed, 16 Oct 2024 15:45:41 +0800 Subject: [PATCH 06/26] =?UTF-8?q?=E5=8A=A8=E6=80=81=E9=85=8D=E7=BD=AE?= =?UTF-8?q?=E7=9A=84=E5=AE=9E=E7=8E=B0=E6=80=9D=E8=B7=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../dynamictp/core/executor/ExecutorType.java | 9 +++++- .../spring/DtpBeanDefinitionRegistrar.java | 30 ++++++++++++++++++- .../dynamic-tp-nacos-cloud-demo-dtp-dev.yml | 2 ++ .../test/resources/postprocessor-dtp-dev.yml | 4 ++- 4 files changed, 42 insertions(+), 3 deletions(-) diff --git a/core/src/main/java/org/dromara/dynamictp/core/executor/ExecutorType.java b/core/src/main/java/org/dromara/dynamictp/core/executor/ExecutorType.java index 309866bda..b9d571066 100644 --- a/core/src/main/java/org/dromara/dynamictp/core/executor/ExecutorType.java +++ b/core/src/main/java/org/dromara/dynamictp/core/executor/ExecutorType.java @@ -21,6 +21,8 @@ import org.dromara.dynamictp.core.executor.eager.EagerDtpExecutor; import lombok.Getter; import org.dromara.dynamictp.core.executor.priority.PriorityDtpExecutor; +import org.dromara.dynamictp.core.support.VirtualThreadExecutorAdapter; +import org.dromara.dynamictp.core.support.VirtualThreadExecutorProxy; /** * ExecutorType related @@ -55,7 +57,12 @@ public enum ExecutorType { /** * Priority executor type. */ - PRIORITY("priority", PriorityDtpExecutor.class); + PRIORITY("priority", PriorityDtpExecutor.class), + + /** + * Virtual thread executor adapter type. + */ + VIRTUAL("virtual", VirtualThreadExecutorProxy.class); private final String name; diff --git a/core/src/main/java/org/dromara/dynamictp/core/spring/DtpBeanDefinitionRegistrar.java b/core/src/main/java/org/dromara/dynamictp/core/spring/DtpBeanDefinitionRegistrar.java index e149d2277..c7895cf89 100644 --- a/core/src/main/java/org/dromara/dynamictp/core/spring/DtpBeanDefinitionRegistrar.java +++ b/core/src/main/java/org/dromara/dynamictp/core/spring/DtpBeanDefinitionRegistrar.java @@ -31,6 +31,7 @@ import org.dromara.dynamictp.core.executor.priority.PriorityDtpExecutor; import org.dromara.dynamictp.core.reject.RejectHandlerGetter; import org.dromara.dynamictp.core.support.BinderHelper; +import org.dromara.dynamictp.core.support.VirtualThreadExecutorProxy; import org.dromara.dynamictp.core.support.task.wrapper.TaskWrappers; import org.springframework.beans.factory.support.BeanDefinitionRegistry; import org.springframework.context.EnvironmentAware; @@ -40,6 +41,7 @@ import java.util.Map; import java.util.concurrent.BlockingQueue; +import java.util.concurrent.Executors; import java.util.concurrent.PriorityBlockingQueue; import static org.dromara.dynamictp.common.constant.DynamicTpConst.ALLOW_CORE_THREAD_TIMEOUT; @@ -93,12 +95,34 @@ public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, B return; } Class executorTypeClass = ExecutorType.getClass(e.getExecutorType()); - Map propertyValues = buildPropertyValues(e); + Map propertyValues; + if(executorTypeClass.equals(VirtualThreadExecutorProxy.class)) { + propertyValues = buildVTPropertyValues(e); + } + else { + propertyValues = buildPropertyValues(e); + } Object[] args = buildConstructorArgs(executorTypeClass, e); SpringBeanHelper.register(registry, e.getThreadPoolName(), executorTypeClass, propertyValues, args); }); } + private Map buildVTPropertyValues(DtpExecutorProps props) { + Map propertyValues = Maps.newHashMap(); + propertyValues.put(THREAD_POOL_NAME, props.getThreadPoolName()); + propertyValues.put(THREAD_POOL_ALIAS_NAME, props.getThreadPoolAliasName()); + val notifyItems = mergeAllNotifyItems(props.getNotifyItems()); + propertyValues.put(NOTIFY_ITEMS, notifyItems); + propertyValues.put(PLATFORM_IDS, props.getPlatformIds()); + propertyValues.put(NOTIFY_ENABLED, props.isNotifyEnabled()); + + val taskWrappers = TaskWrappers.getInstance().getByNames(props.getTaskWrapperNames()); + propertyValues.put(TASK_WRAPPERS, taskWrappers); + propertyValues.put(PLUGIN_NAMES, props.getPluginNames()); + propertyValues.put(AWARE_NAMES, props.getAwareNames()); + return propertyValues; + } + private Map buildPropertyValues(DtpExecutorProps props) { Map propertyValues = Maps.newHashMap(); propertyValues.put(THREAD_POOL_NAME, props.getThreadPoolName()); @@ -130,6 +154,10 @@ private Object[] buildConstructorArgs(Class clazz, DtpExecutorProps props) { taskQueue = new TaskQueue(props.getQueueCapacity()); } else if (clazz.equals(PriorityDtpExecutor.class)) { taskQueue = new PriorityBlockingQueue<>(props.getQueueCapacity(), PriorityDtpExecutor.getRunnableComparator()); + } else if (clazz.equals(VirtualThreadExecutorProxy.class)) { + return new Object[] { + Executors.newVirtualThreadPerTaskExecutor() + }; } else { taskQueue = buildLbq(props.getQueueType(), props.getQueueCapacity(), diff --git a/example/example-nacos-cloud/src/main/resources/dynamic-tp-nacos-cloud-demo-dtp-dev.yml b/example/example-nacos-cloud/src/main/resources/dynamic-tp-nacos-cloud-demo-dtp-dev.yml index f685ff628..d55ed51c0 100644 --- a/example/example-nacos-cloud/src/main/resources/dynamic-tp-nacos-cloud-demo-dtp-dev.yml +++ b/example/example-nacos-cloud/src/main/resources/dynamic-tp-nacos-cloud-demo-dtp-dev.yml @@ -92,3 +92,5 @@ spring: keepAliveTime: 50 allowCoreThreadTimeOut: false threadNamePrefix: test + - threadPoolName: VirtualThreadExecutor1 + executorType: virtual diff --git a/test/test-core/src/test/resources/postprocessor-dtp-dev.yml b/test/test-core/src/test/resources/postprocessor-dtp-dev.yml index c4fc28dc0..334f615a6 100644 --- a/test/test-core/src/test/resources/postprocessor-dtp-dev.yml +++ b/test/test-core/src/test/resources/postprocessor-dtp-dev.yml @@ -45,4 +45,6 @@ spring: runTimeout: 200 # 任务执行超时阈值,目前只做告警用,单位(ms) queueTimeout: 100 # 任务在队列等待超时阈值,目前只做告警用,单位(ms) taskWrapperNames: [ "ttl", "mdc","cta" ] # 任务包装器名称,继承TaskWrapper接口 - notifyEnabled: false # 是否开启报警,默认true \ No newline at end of file + notifyEnabled: false # 是否开启报警,默认true +# - threadPoolName: VirtualThreadExecutor2 +# executorType: virtual \ No newline at end of file From f05dd265aec4bccac66b0581d61b7ddd1999676a Mon Sep 17 00:00:00 2001 From: TheFatRatre <1981590178@qq.com> Date: Wed, 16 Oct 2024 16:02:48 +0800 Subject: [PATCH 07/26] =?UTF-8?q?proxy=E7=9A=84=E9=87=8D=E6=9E=84=EF=BC=8C?= =?UTF-8?q?=E8=BF=99=E6=A0=B7=E5=86=99Bean=E6=97=A0=E6=B3=95=E6=B3=A8?= =?UTF-8?q?=E5=86=8C=EF=BC=8C=E8=80=A6=E5=90=88=E6=80=A7=E4=B9=9F=E9=AB=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../support/VirtualThreadExecutorProxy.java | 89 +++++++++++++++++++ 1 file changed, 89 insertions(+) diff --git a/core/src/main/java/org/dromara/dynamictp/core/support/VirtualThreadExecutorProxy.java b/core/src/main/java/org/dromara/dynamictp/core/support/VirtualThreadExecutorProxy.java index be82a84a5..6942f86b5 100644 --- a/core/src/main/java/org/dromara/dynamictp/core/support/VirtualThreadExecutorProxy.java +++ b/core/src/main/java/org/dromara/dynamictp/core/support/VirtualThreadExecutorProxy.java @@ -33,11 +33,46 @@ public class VirtualThreadExecutorProxy implements TaskEnhanceAware, ExecutorSer private final ExecutorService THREAD_PER_TASK_EXECUTOR; + /** + * Notify platform ids. + */ + private List platformIds; + /** * Task wrappers, do sth enhanced. */ private List taskWrappers; + /** + * The name of the thread pool. + */ + protected String threadPoolName; + + /** + * Simple Business alias Name of Dynamic ThreadPool. Use for notify. + */ + private String threadPoolAliasName; + + /** + * If enable notify. + */ + private boolean notifyEnabled = true; + + /** + * Notify items, see {@link NotifyItemEnum}. + */ + private List notifyItems; + + /** + * Plugin names. + */ + private Set pluginNames = Sets.newHashSet(); + + /** + * Aware names. + */ + private Set awareNames = Sets.newHashSet(); + public VirtualThreadExecutorProxy(ExecutorService executor) { super(); @@ -135,4 +170,58 @@ public T invokeAny(Collection> collection, long l, Tim return THREAD_PER_TASK_EXECUTOR.invokeAny(collection, l, timeUnit); } + public String getThreadPoolName() { + return threadPoolName; + } + + public void setThreadPoolName(String threadPoolName) { + this.threadPoolName = threadPoolName; + } + + public String getThreadPoolAliasName() { + return threadPoolAliasName; + } + + public void setThreadPoolAliasName(String threadPoolAliasName) { + this.threadPoolAliasName = threadPoolAliasName; + } + + public boolean isNotifyEnabled() { + return notifyEnabled; + } + + public void setNotifyEnabled(boolean notifyEnabled) { + this.notifyEnabled = notifyEnabled; + } + public Set getPluginNames() { + return pluginNames; + } + + public void setPluginNames(Set pluginNames) { + this.pluginNames = pluginNames; + } + + public Set getAwareNames() { + return awareNames; + } + + public void setAwareNames(Set awareNames) { + this.awareNames = awareNames; + } + + public List getPlatformIds() { + return platformIds; + } + + public void setPlatformIds(List platformIds) { + this.platformIds = platformIds; + } + + public List getNotifyItems() { + return notifyItems; + } + + public void setNotifyItems(List notifyItems) { + this.notifyItems = notifyItems; + } } From a85826ccfed6415300c460bdd17b12be744ea8d4 Mon Sep 17 00:00:00 2001 From: TheFatRatre <1981590178@qq.com> Date: Mon, 21 Oct 2024 11:20:05 +0800 Subject: [PATCH 08/26] =?UTF-8?q?=E7=A7=BB=E9=99=A4jdk21=E7=89=B9=E6=80=A7?= =?UTF-8?q?=E4=BD=BF=E4=B9=8B=E5=85=BC=E5=AE=B9=E8=80=81=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../dynamictp/core/spring/DtpPostProcessor.java | 1 - .../dynamictp/core/support/ExecutorWrapper.java | 3 ++- .../core/support/VirtualThreadExecutorAdapter.java | 13 +++++++++---- .../core/support/VirtualThreadExecutorProxy.java | 6 +++--- .../example/config/ThreadPoolConfiguration.java | 11 +++++++---- .../dromara/dynamictp/test/core/spring/Config.java | 1 - .../test/core/spring/DtpPostProcessorTest.java | 6 +++--- 7 files changed, 24 insertions(+), 17 deletions(-) diff --git a/core/src/main/java/org/dromara/dynamictp/core/spring/DtpPostProcessor.java b/core/src/main/java/org/dromara/dynamictp/core/spring/DtpPostProcessor.java index f90ee68d9..03fd1acaf 100644 --- a/core/src/main/java/org/dromara/dynamictp/core/spring/DtpPostProcessor.java +++ b/core/src/main/java/org/dromara/dynamictp/core/spring/DtpPostProcessor.java @@ -192,7 +192,6 @@ private ScheduledThreadPoolExecutorProxy newScheduledTpProxy(String name, Schedu private VirtualThreadExecutorProxy newVirtualThreadProxy(String name, ExecutorService originExecutor) { val proxy = new VirtualThreadExecutorProxy(originExecutor); - shutdownGracefulAsync(originExecutor, name, 0); return proxy; } diff --git a/core/src/main/java/org/dromara/dynamictp/core/support/ExecutorWrapper.java b/core/src/main/java/org/dromara/dynamictp/core/support/ExecutorWrapper.java index 7dca15fa0..210939bce 100644 --- a/core/src/main/java/org/dromara/dynamictp/core/support/ExecutorWrapper.java +++ b/core/src/main/java/org/dromara/dynamictp/core/support/ExecutorWrapper.java @@ -140,7 +140,7 @@ public ExecutorWrapper(String threadPoolName, Executor executor) { } else if (executor instanceof ExecutorAdapter) { this.executor = (ExecutorAdapter) executor; } else if (executor instanceof VirtualThreadExecutorProxy) { - this.executor = new VirtualThreadExecutorAdapter(); + this.executor = new VirtualThreadExecutorAdapter(executor); } else { throw new IllegalArgumentException("unsupported Executor type !"); } @@ -209,6 +209,7 @@ public boolean isThreadPoolExecutor() { /** * whether is VirtualThreadExecutor + * * @return boolean */ public boolean isVirtualThreadExecutor() { diff --git a/core/src/main/java/org/dromara/dynamictp/core/support/VirtualThreadExecutorAdapter.java b/core/src/main/java/org/dromara/dynamictp/core/support/VirtualThreadExecutorAdapter.java index 8b6cc0727..804a5f3be 100644 --- a/core/src/main/java/org/dromara/dynamictp/core/support/VirtualThreadExecutorAdapter.java +++ b/core/src/main/java/org/dromara/dynamictp/core/support/VirtualThreadExecutorAdapter.java @@ -1,20 +1,25 @@ package org.dromara.dynamictp.core.support; +import java.util.concurrent.Executor; import java.util.concurrent.ExecutorService; -import java.util.concurrent.Executors; /** * ClassName: VirtualThreadExecutorAdapter * Package: org.dromara.dynamictp.core.support * Description: - * Adapter for virtual thread executor + * Adapter for virtual thread executor + * * @Author CYC * @Create 2024/10/14 15:33 * @Version 1.0 */ -public class VirtualThreadExecutorAdapter implements ExecutorAdapter{ +public class VirtualThreadExecutorAdapter implements ExecutorAdapter { - private final ExecutorService executor = Executors.newVirtualThreadPerTaskExecutor(); + private final ExecutorService executor; + + public VirtualThreadExecutorAdapter(Executor executor) { + this.executor = ((VirtualThreadExecutorProxy) executor).getThreadPerTaskExecutor(); + } @Override public ExecutorService getOriginal() { diff --git a/core/src/main/java/org/dromara/dynamictp/core/support/VirtualThreadExecutorProxy.java b/core/src/main/java/org/dromara/dynamictp/core/support/VirtualThreadExecutorProxy.java index be82a84a5..ad879d3c5 100644 --- a/core/src/main/java/org/dromara/dynamictp/core/support/VirtualThreadExecutorProxy.java +++ b/core/src/main/java/org/dromara/dynamictp/core/support/VirtualThreadExecutorProxy.java @@ -1,8 +1,5 @@ package org.dromara.dynamictp.core.support; -import com.google.common.collect.Sets; -import org.dromara.dynamictp.common.em.NotifyItemEnum; -import org.dromara.dynamictp.common.entity.NotifyItem; import org.dromara.dynamictp.core.aware.AwareManager; import org.dromara.dynamictp.core.aware.TaskEnhanceAware; import org.dromara.dynamictp.core.support.task.runnable.EnhancedRunnable; @@ -52,6 +49,9 @@ public void execute(Runnable command) { THREAD_PER_TASK_EXECUTOR.execute(command); } + public ExecutorService getThreadPerTaskExecutor() { + return THREAD_PER_TASK_EXECUTOR; + } @Override public List getTaskWrappers() { diff --git a/example/example-nacos-cloud/src/main/java/org/dromara/dynamictp/example/config/ThreadPoolConfiguration.java b/example/example-nacos-cloud/src/main/java/org/dromara/dynamictp/example/config/ThreadPoolConfiguration.java index 0bae57ec4..0f3c7f215 100644 --- a/example/example-nacos-cloud/src/main/java/org/dromara/dynamictp/example/config/ThreadPoolConfiguration.java +++ b/example/example-nacos-cloud/src/main/java/org/dromara/dynamictp/example/config/ThreadPoolConfiguration.java @@ -22,12 +22,15 @@ import org.dromara.dynamictp.core.support.DynamicTp; import org.dromara.dynamictp.core.support.ThreadPoolBuilder; import org.dromara.dynamictp.core.support.ThreadPoolCreator; -import org.dromara.dynamictp.core.support.VirtualThreadExecutorAdapter; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor; -import java.util.concurrent.*; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.ScheduledExecutorService; +import java.util.concurrent.ThreadPoolExecutor; +import java.util.concurrent.TimeUnit; import static org.dromara.dynamictp.common.em.QueueTypeEnum.MEMORY_SAFE_LINKED_BLOCKING_QUEUE; import static org.dromara.dynamictp.common.em.RejectedTypeEnum.CALLER_RUNS_POLICY; @@ -62,8 +65,8 @@ public ThreadPoolTaskExecutor threadPoolTaskExecutor() { @DynamicTp("VirtualThreadExecutor") @Bean - public VirtualThreadExecutorAdapter virtualThreadExecutor() { - return new VirtualThreadExecutorAdapter(); + public ExecutorService virtualThreadExecutor() { + return Executors.newVirtualThreadPerTaskExecutor(); } /** diff --git a/test/test-core/src/test/java/org/dromara/dynamictp/test/core/spring/Config.java b/test/test-core/src/test/java/org/dromara/dynamictp/test/core/spring/Config.java index 583df1fb9..08bc8fa98 100644 --- a/test/test-core/src/test/java/org/dromara/dynamictp/test/core/spring/Config.java +++ b/test/test-core/src/test/java/org/dromara/dynamictp/test/core/spring/Config.java @@ -19,7 +19,6 @@ import org.dromara.dynamictp.core.spring.EnableDynamicTp; import org.dromara.dynamictp.core.support.DynamicTp; -import org.dromara.dynamictp.core.support.VirtualThreadExecutorAdapter; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; diff --git a/test/test-core/src/test/java/org/dromara/dynamictp/test/core/spring/DtpPostProcessorTest.java b/test/test-core/src/test/java/org/dromara/dynamictp/test/core/spring/DtpPostProcessorTest.java index 4925d167c..ea3d901d3 100644 --- a/test/test-core/src/test/java/org/dromara/dynamictp/test/core/spring/DtpPostProcessorTest.java +++ b/test/test-core/src/test/java/org/dromara/dynamictp/test/core/spring/DtpPostProcessorTest.java @@ -21,7 +21,6 @@ import org.dromara.dynamictp.core.spring.EnableDynamicTp; import org.dromara.dynamictp.core.spring.YamlPropertySourceFactory; import org.dromara.dynamictp.core.support.ThreadPoolExecutorProxy; -import org.dromara.dynamictp.core.support.VirtualThreadExecutorAdapter; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; @@ -33,7 +32,6 @@ import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor; import java.util.concurrent.Executor; -import java.util.concurrent.ExecutorService; import java.util.concurrent.ThreadPoolExecutor; /** @@ -69,7 +67,9 @@ void test() { @Test void test2() { Executor virtualThreadExecutor = DtpRegistry.getExecutor("VirtualThreadExecutor"); - virtualThreadExecutor.execute(() -> System.out.println("VirtualThreadExecutor registered!")); + virtualThreadExecutor.execute(() -> { + System.out.println("VirtualThreadExecutor registered!"); + }); Assertions.assertNotNull(virtualThreadExecutor); // Executor virtualThreadExecutor2 = DtpRegistry.getExecutor("VirtualThreadExecutor2"); From 4495be0e03fb71181767b58295c0a188132fbcd6 Mon Sep 17 00:00:00 2001 From: TheFatRatre <1981590178@qq.com> Date: Mon, 21 Oct 2024 11:23:29 +0800 Subject: [PATCH 09/26] fix --- .../dynamictp/core/support/VirtualThreadExecutorProxy.java | 3 +++ 1 file changed, 3 insertions(+) diff --git a/core/src/main/java/org/dromara/dynamictp/core/support/VirtualThreadExecutorProxy.java b/core/src/main/java/org/dromara/dynamictp/core/support/VirtualThreadExecutorProxy.java index 6d90e937e..7a77e5e94 100644 --- a/core/src/main/java/org/dromara/dynamictp/core/support/VirtualThreadExecutorProxy.java +++ b/core/src/main/java/org/dromara/dynamictp/core/support/VirtualThreadExecutorProxy.java @@ -1,5 +1,8 @@ package org.dromara.dynamictp.core.support; +import com.google.common.collect.Sets; +import org.dromara.dynamictp.common.em.NotifyItemEnum; +import org.dromara.dynamictp.common.entity.NotifyItem; import org.dromara.dynamictp.core.aware.AwareManager; import org.dromara.dynamictp.core.aware.TaskEnhanceAware; import org.dromara.dynamictp.core.support.task.runnable.EnhancedRunnable; From a729e11e44ca78c78b5d2cd19f8976a1a146030e Mon Sep 17 00:00:00 2001 From: TheFatRatre <1981590178@qq.com> Date: Mon, 21 Oct 2024 16:06:05 +0800 Subject: [PATCH 10/26] =?UTF-8?q?=E9=85=8D=E7=BD=AE=E4=B8=AD=E5=BF=83?= =?UTF-8?q?=E8=BF=9B=E8=A1=8C=E9=85=8D=E7=BD=AE=EF=BC=8C=E5=8F=AF=E4=BB=A5?= =?UTF-8?q?=E8=8E=B7=E5=8F=96=E5=88=B0executor=E5=B9=B6=E6=89=A7=E8=A1=8C?= =?UTF-8?q?=E4=BD=86spring=E6=B2=A1=E6=9C=89=E6=84=9F=E7=9F=A5=E5=88=B0?= =?UTF-8?q?=E6=AD=A4bean=EF=BC=8C=E5=8D=B3postProcessAfterInitialization?= =?UTF-8?q?=E6=9C=AA=E8=8E=B7=E5=8F=96=E5=88=B0=E6=AD=A4bean=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../core/support/ExecutorWrapper.java | 2 +- .../support/VirtualThreadExecutorAdapter.java | 2 +- .../config/ThreadPoolConfiguration.java | 11 ++++---- .../example/controller/TestController.java | 5 ++++ .../example/service/TestService.java | 5 ++++ .../example/service/impl/TestServiceImpl.java | 26 +++++++++++++++---- 6 files changed, 38 insertions(+), 13 deletions(-) diff --git a/core/src/main/java/org/dromara/dynamictp/core/support/ExecutorWrapper.java b/core/src/main/java/org/dromara/dynamictp/core/support/ExecutorWrapper.java index 210939bce..2aae4cdf3 100644 --- a/core/src/main/java/org/dromara/dynamictp/core/support/ExecutorWrapper.java +++ b/core/src/main/java/org/dromara/dynamictp/core/support/ExecutorWrapper.java @@ -140,7 +140,7 @@ public ExecutorWrapper(String threadPoolName, Executor executor) { } else if (executor instanceof ExecutorAdapter) { this.executor = (ExecutorAdapter) executor; } else if (executor instanceof VirtualThreadExecutorProxy) { - this.executor = new VirtualThreadExecutorAdapter(executor); + this.executor = new VirtualThreadExecutorAdapter(((VirtualThreadExecutorProxy) executor).getThreadPerTaskExecutor()); } else { throw new IllegalArgumentException("unsupported Executor type !"); } diff --git a/core/src/main/java/org/dromara/dynamictp/core/support/VirtualThreadExecutorAdapter.java b/core/src/main/java/org/dromara/dynamictp/core/support/VirtualThreadExecutorAdapter.java index 804a5f3be..33ede79b1 100644 --- a/core/src/main/java/org/dromara/dynamictp/core/support/VirtualThreadExecutorAdapter.java +++ b/core/src/main/java/org/dromara/dynamictp/core/support/VirtualThreadExecutorAdapter.java @@ -18,7 +18,7 @@ public class VirtualThreadExecutorAdapter implements ExecutorAdapter{ + log.info("i am a VTExecutor's {} task", finalI); + }); + } + } + public static class TestOrderedRunnable implements OrderedRunnable { private final UserInfo userInfo; From 6a1e1a93b4620277aa7ad475a0419abf8a38410c Mon Sep 17 00:00:00 2001 From: TheFatRatre <1981590178@qq.com> Date: Mon, 4 Nov 2024 15:54:20 +0800 Subject: [PATCH 11/26] =?UTF-8?q?=E8=A7=A3=E5=86=B3=E9=85=8D=E7=BD=AE?= =?UTF-8?q?=E4=B8=AD=E5=BF=83=E9=85=8D=E7=BD=AEVT=E6=9C=AA=E8=A2=AB?= =?UTF-8?q?=E7=99=BB=E8=AE=B0=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../dynamictp/core/executor/ExecutorType.java | 1 - .../dynamictp/core/spring/DtpPostProcessor.java | 9 +++++++-- .../example/service/impl/TestServiceImpl.java | 14 ++++++++++---- 3 files changed, 17 insertions(+), 7 deletions(-) diff --git a/core/src/main/java/org/dromara/dynamictp/core/executor/ExecutorType.java b/core/src/main/java/org/dromara/dynamictp/core/executor/ExecutorType.java index b9d571066..b59a04518 100644 --- a/core/src/main/java/org/dromara/dynamictp/core/executor/ExecutorType.java +++ b/core/src/main/java/org/dromara/dynamictp/core/executor/ExecutorType.java @@ -21,7 +21,6 @@ import org.dromara.dynamictp.core.executor.eager.EagerDtpExecutor; import lombok.Getter; import org.dromara.dynamictp.core.executor.priority.PriorityDtpExecutor; -import org.dromara.dynamictp.core.support.VirtualThreadExecutorAdapter; import org.dromara.dynamictp.core.support.VirtualThreadExecutorProxy; /** diff --git a/core/src/main/java/org/dromara/dynamictp/core/spring/DtpPostProcessor.java b/core/src/main/java/org/dromara/dynamictp/core/spring/DtpPostProcessor.java index 03fd1acaf..5d7c9ca9d 100644 --- a/core/src/main/java/org/dromara/dynamictp/core/spring/DtpPostProcessor.java +++ b/core/src/main/java/org/dromara/dynamictp/core/spring/DtpPostProcessor.java @@ -91,7 +91,8 @@ public Object postProcessBeforeInitialization(Object bean, String beanName) thro @Override public Object postProcessAfterInitialization(@NonNull Object bean, @NonNull String beanName) throws BeansException { if (!(bean instanceof ThreadPoolExecutor) && !(bean instanceof ThreadPoolTaskExecutor) && - !(bean.getClass().getName().equals("java.util.concurrent.ThreadPerTaskExecutor"))) { + !(bean.getClass().getName().equals("java.util.concurrent.ThreadPerTaskExecutor")) && + !(bean instanceof VirtualThreadExecutorProxy)) { return bean; } if (bean instanceof DtpExecutor) { @@ -124,6 +125,9 @@ private Object registerAndReturnCommon(Object bean, String beanName) { } else { BeanDefinition beanDefinition = beanFactory.getBeanDefinition(beanName); if (!(beanDefinition instanceof AnnotatedBeanDefinition)) { + if(beanDefinition.getBeanClassName().equals("org.dromara.dynamictp.core.support.VirtualThreadExecutorProxy")) { + return doRegisterAndReturnCommon(bean, beanName); + } return bean; } AnnotatedBeanDefinition annotatedBeanDefinition = (AnnotatedBeanDefinition) beanDefinition; @@ -159,7 +163,8 @@ private Object doRegisterAndReturnCommon(Object bean, String poolName) { Executor proxy; if (bean instanceof ScheduledThreadPoolExecutor) { proxy = newScheduledTpProxy(poolName, (ScheduledThreadPoolExecutor) bean); - } else if (bean.getClass().getName().equals("java.util.concurrent.ThreadPerTaskExecutor")) { + } else if (bean.getClass().getName().equals("java.util.concurrent.ThreadPerTaskExecutor") + || bean instanceof VirtualThreadExecutorProxy) { proxy = newVirtualThreadProxy(poolName, (ExecutorService) bean); } else { proxy = newProxy(poolName, (ThreadPoolExecutor) bean); diff --git a/example/example-nacos-cloud/src/main/java/org/dromara/dynamictp/example/service/impl/TestServiceImpl.java b/example/example-nacos-cloud/src/main/java/org/dromara/dynamictp/example/service/impl/TestServiceImpl.java index a3e5fd578..c53689098 100644 --- a/example/example-nacos-cloud/src/main/java/org/dromara/dynamictp/example/service/impl/TestServiceImpl.java +++ b/example/example-nacos-cloud/src/main/java/org/dromara/dynamictp/example/service/impl/TestServiceImpl.java @@ -52,7 +52,7 @@ public class TestServiceImpl implements TestService { private final OrderedDtpExecutor orderedDtpExecutor; - private final ExecutorService VirtualThreadExecutor; + private final ExecutorService virtualThreadExecutor; public TestServiceImpl(ThreadPoolExecutor jucThreadPoolExecutor, ThreadPoolTaskExecutor threadPoolTaskExecutor, @@ -65,7 +65,7 @@ public TestServiceImpl(ThreadPoolExecutor jucThreadPoolExecutor, this.eagerDtpExecutor = eagerDtpExecutor; this.scheduledDtpExecutor = scheduledDtpExecutor; this.orderedDtpExecutor = orderedDtpExecutor; - this.VirtualThreadExecutor = virtualThreadExecutor; + this.virtualThreadExecutor = virtualThreadExecutor; } // public TestServiceImpl(@Qualifier("VirtualThreadExecutor1") ExecutorService virtualThreadExecutor) { @@ -152,8 +152,15 @@ public void testOrderedDtp() { public void testVTExecutor() { for (int i = 0; i < 10; i++) { int finalI = i; - VirtualThreadExecutor.execute(()->{ + virtualThreadExecutor.execute(() -> { log.info("i am a VTExecutor's {} task", finalI); + try { + Thread.sleep(30000); + } catch (InterruptedException e) { + throw new RuntimeException(e); + } + System.out.println("i am a VTExecutor's task"); + }); } } @@ -161,7 +168,6 @@ public void testVTExecutor() { public static class TestOrderedRunnable implements OrderedRunnable { private final UserInfo userInfo; - public TestOrderedRunnable(UserInfo userInfo) { this.userInfo = userInfo; } From 3755c8b6c03ca898cfddc325fb37831a6e1a6c73 Mon Sep 17 00:00:00 2001 From: TheFatRatre <1981590178@qq.com> Date: Mon, 4 Nov 2024 16:09:47 +0800 Subject: [PATCH 12/26] doc --- .../config/ThreadPoolConfiguration.java | 20 ++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/example/example-nacos-cloud/src/main/java/org/dromara/dynamictp/example/config/ThreadPoolConfiguration.java b/example/example-nacos-cloud/src/main/java/org/dromara/dynamictp/example/config/ThreadPoolConfiguration.java index 72fd68a99..0888af6b9 100644 --- a/example/example-nacos-cloud/src/main/java/org/dromara/dynamictp/example/config/ThreadPoolConfiguration.java +++ b/example/example-nacos-cloud/src/main/java/org/dromara/dynamictp/example/config/ThreadPoolConfiguration.java @@ -26,10 +26,7 @@ import org.springframework.context.annotation.Configuration; import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor; -import java.util.concurrent.Executors; -import java.util.concurrent.ScheduledExecutorService; -import java.util.concurrent.ThreadPoolExecutor; -import java.util.concurrent.TimeUnit; +import java.util.concurrent.*; import static org.dromara.dynamictp.common.em.QueueTypeEnum.MEMORY_SAFE_LINKED_BLOCKING_QUEUE; import static org.dromara.dynamictp.common.em.RejectedTypeEnum.CALLER_RUNS_POLICY; @@ -62,11 +59,16 @@ public ThreadPoolTaskExecutor threadPoolTaskExecutor() { return new ThreadPoolTaskExecutor(); } -// @DynamicTp("VirtualThreadExecutor") -// @Bean -// public ExecutorService virtualThreadExecutor() { -// return Executors.newVirtualThreadPerTaskExecutor(); -// } + /** + * 通过{@link DynamicTp} 注解定义虚拟线程执行器 + * + * @return 虚拟线程执行器实例 + */ + @DynamicTp("VirtualThreadExecutor") + @Bean + public ExecutorService virtualThreadExecutor() { + return Executors.newVirtualThreadPerTaskExecutor(); + } /** * 通过{@link ThreadPoolCreator} 快速创建一些简单配置的线程池,使用默认参数 From 0a38447dab5837acab7475e4922112ec58ca3d7c Mon Sep 17 00:00:00 2001 From: TheFatRatre <1981590178@qq.com> Date: Mon, 4 Nov 2024 18:19:44 +0800 Subject: [PATCH 13/26] =?UTF-8?q?=E7=9B=91=E6=8E=A7=E5=AE=9E=E7=8E=B0?= =?UTF-8?q?=E6=80=9D=E8=B7=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../dynamictp/common/entity/VTTaskStats.java | 97 +++++++++++++++++++ .../core/converter/ExecutorConverter.java | 5 + .../core/handler/CollectorHandler.java | 13 +++ .../dynamictp/core/monitor/DtpMonitor.java | 15 ++- .../monitor/collector/MetricsCollector.java | 7 ++ .../support/VirtualThreadExecutorAdapter.java | 4 +- 6 files changed, 137 insertions(+), 4 deletions(-) create mode 100644 common/src/main/java/org/dromara/dynamictp/common/entity/VTTaskStats.java diff --git a/common/src/main/java/org/dromara/dynamictp/common/entity/VTTaskStats.java b/common/src/main/java/org/dromara/dynamictp/common/entity/VTTaskStats.java new file mode 100644 index 000000000..5b7c6f1d6 --- /dev/null +++ b/common/src/main/java/org/dromara/dynamictp/common/entity/VTTaskStats.java @@ -0,0 +1,97 @@ +package org.dromara.dynamictp.common.entity; + +import lombok.Data; +import lombok.EqualsAndHashCode; + +import java.util.List; + +/** + * ClassName: VTTaskStats + * Package: org.dromara.dynamictp.common.entity + * Description: + * Wanted performance is like: + * "virtual_threads": [ + * { + * "id": 1, + * "name": "VirtualThread-1", + * "state": "RUNNABLE", + * "stack_trace": [ + * { + * "class": "java.base/java.lang.Thread", + * "method": "lambda$main$0", + * "file": "Main.java", + * "line": 10 + * } + * ] + * }, + * { + * "id": 2, + * "name": "VirtualThread-2", + * "state": "BLOCKED", + * "stack_trace": [ + * { + * "class": "java.base/java.net.SocketInputStream", + * "method": "socketRead0", + * "file": "SocketInputStream.java", + * "line": 61 + * } + * ] + * } + * @author CYC + * @create 2024/11/4 16:52 + */ +@Data +@EqualsAndHashCode(callSuper = true) +public class VTTaskStats extends Metrics{ + + /** + * 虚拟线程的id + */ + private int id; + + /** + * 虚拟线程的名称 + */ + private String name; + + /** + * 虚拟线程的状态 + */ + private String state; + + /** + * 虚拟线程所承载任务的所有堆栈信息 + */ + private List stack; + + /** + * 虚拟线程所承载任务的理想的堆栈信息 + */ + private StackForGood stackForGood; + + /** + * 虚拟线程的任务数 + */ + private int taskCount; + + private class StackForGood { + /** + * Wanted stack trace is like: + * "stack_trace": [ + * { + * "class": "java.base/java.net.SocketInputStream", + * "method": "socketRead0", + * "file": "SocketInputStream.java", + * "line": 61 + * } + * ] + */ + private String clazz; + + private String method; + + private String file; + + private String line; + } +} 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 48d333321..9e3171322 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 @@ -20,6 +20,7 @@ import lombok.val; import org.dromara.dynamictp.common.entity.ThreadPoolStats; import org.dromara.dynamictp.common.entity.TpMainFields; +import org.dromara.dynamictp.common.entity.VTTaskStats; import org.dromara.dynamictp.core.executor.DtpExecutor; import org.dromara.dynamictp.core.monitor.PerformanceProvider; import org.dromara.dynamictp.core.support.ExecutorAdapter; @@ -82,6 +83,10 @@ public static ThreadPoolStats toMetrics(ExecutorWrapper wrapper) { return poolStats; } + public static VTTaskStats toVTTaskMetrics(ExecutorWrapper wrapper) { + return null; + } + private static ThreadPoolStats convertCommon(ExecutorAdapter executor) { ThreadPoolStats poolStats = new ThreadPoolStats(); poolStats.setCorePoolSize(executor.getCorePoolSize()); diff --git a/core/src/main/java/org/dromara/dynamictp/core/handler/CollectorHandler.java b/core/src/main/java/org/dromara/dynamictp/core/handler/CollectorHandler.java index 8275a3bc0..710c06049 100644 --- a/core/src/main/java/org/dromara/dynamictp/core/handler/CollectorHandler.java +++ b/core/src/main/java/org/dromara/dynamictp/core/handler/CollectorHandler.java @@ -20,6 +20,7 @@ import com.google.common.collect.Maps; import lombok.extern.slf4j.Slf4j; import org.dromara.dynamictp.common.entity.ThreadPoolStats; +import org.dromara.dynamictp.common.entity.VTTaskStats; import org.dromara.dynamictp.common.util.ExtensionServiceLoader; import org.dromara.dynamictp.core.monitor.collector.InternalLogCollector; import org.dromara.dynamictp.core.monitor.collector.LogCollector; @@ -68,6 +69,18 @@ public void collect(ThreadPoolStats poolStats, List types) { } } + public void collectVTtask(VTTaskStats vtTaskStats, List types) { + if (vtTaskStats == null || CollectionUtils.isEmpty(types)) { + return; + } + for (String collectorType : types) { + MetricsCollector collector = COLLECTORS.get(collectorType.toLowerCase()); + if (collector != null) { + collector.collect(vtTaskStats); + } + } + } + public static CollectorHandler getInstance() { return CollectorHandlerHolder.INSTANCE; } diff --git a/core/src/main/java/org/dromara/dynamictp/core/monitor/DtpMonitor.java b/core/src/main/java/org/dromara/dynamictp/core/monitor/DtpMonitor.java index 1a07c0af7..6729f1f09 100644 --- a/core/src/main/java/org/dromara/dynamictp/core/monitor/DtpMonitor.java +++ b/core/src/main/java/org/dromara/dynamictp/core/monitor/DtpMonitor.java @@ -19,6 +19,7 @@ import lombok.extern.slf4j.Slf4j; import org.dromara.dynamictp.common.entity.ThreadPoolStats; +import org.dromara.dynamictp.common.entity.VTTaskStats; import org.dromara.dynamictp.common.event.AlarmCheckEvent; import org.dromara.dynamictp.common.event.CollectEvent; import org.dromara.dynamictp.common.properties.DtpProperties; @@ -95,11 +96,23 @@ private void collectMetrics(Set executorNames) { } executorNames.forEach(x -> { ExecutorWrapper wrapper = DtpRegistry.getExecutorWrapper(x); - doCollect(ExecutorConverter.toMetrics(wrapper)); + if(wrapper.isVirtualThreadExecutor()) { + doCollectVTTask(ExecutorConverter.toVTTaskMetrics(wrapper)); + } else { + doCollect(ExecutorConverter.toMetrics(wrapper)); + } }); publishCollectEvent(); } + private void doCollectVTTask(VTTaskStats vtTaskStats) { + try { + CollectorHandler.getInstance().collectVTtask(vtTaskStats, dtpProperties.getCollectorTypes()); + } catch (Exception e) { + log.error("DynamicTp monitor, VTTask collect error.", e); + } + } + private void doCollect(ThreadPoolStats threadPoolStats) { try { CollectorHandler.getInstance().collect(threadPoolStats, dtpProperties.getCollectorTypes()); diff --git a/core/src/main/java/org/dromara/dynamictp/core/monitor/collector/MetricsCollector.java b/core/src/main/java/org/dromara/dynamictp/core/monitor/collector/MetricsCollector.java index b4728db4f..2f5388dce 100644 --- a/core/src/main/java/org/dromara/dynamictp/core/monitor/collector/MetricsCollector.java +++ b/core/src/main/java/org/dromara/dynamictp/core/monitor/collector/MetricsCollector.java @@ -18,6 +18,7 @@ package org.dromara.dynamictp.core.monitor.collector; import org.dromara.dynamictp.common.entity.ThreadPoolStats; +import org.dromara.dynamictp.common.entity.VTTaskStats; /** * MetricsCollector related @@ -33,6 +34,12 @@ public interface MetricsCollector { */ void collect(ThreadPoolStats poolStats); + /** + * + * @param vtTaskStats + */ + void collect(VTTaskStats vtTaskStats); + /** * Collector type. * @return collector type diff --git a/core/src/main/java/org/dromara/dynamictp/core/support/VirtualThreadExecutorAdapter.java b/core/src/main/java/org/dromara/dynamictp/core/support/VirtualThreadExecutorAdapter.java index 33ede79b1..b5e484201 100644 --- a/core/src/main/java/org/dromara/dynamictp/core/support/VirtualThreadExecutorAdapter.java +++ b/core/src/main/java/org/dromara/dynamictp/core/support/VirtualThreadExecutorAdapter.java @@ -9,9 +9,7 @@ * Description: * Adapter for virtual thread executor * - * @Author CYC - * @Create 2024/10/14 15:33 - * @Version 1.0 + * @author CYC */ public class VirtualThreadExecutorAdapter implements ExecutorAdapter { From c791fd219cc1a7b3c09b3b49d3af68faaa23d272 Mon Sep 17 00:00:00 2001 From: TheFatRatre <1981590178@qq.com> Date: Tue, 5 Nov 2024 18:05:05 +0800 Subject: [PATCH 14/26] =?UTF-8?q?=E7=9B=91=E6=8E=A7=E6=A8=A1=E5=9D=97?= =?UTF-8?q?=E9=83=A8=E5=88=86=E5=AE=9E=E7=8E=B0,VTExecutorStats=E4=B8=BA?= =?UTF-8?q?=E6=9C=9F=E6=9C=9B=E8=83=BD=E8=8E=B7=E5=8F=96=E5=88=B0=E7=9A=84?= =?UTF-8?q?=E6=95=B0=E6=8D=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../common/entity/VTExecutorStats.java | 127 ++++++++++++++++++ .../dynamictp/common/entity/VTTaskStats.java | 97 ------------- .../core/converter/ExecutorConverter.java | 8 +- .../core/handler/CollectorHandler.java | 6 +- .../dynamictp/core/monitor/DtpMonitor.java | 4 +- .../monitor/collector/MetricsCollector.java | 6 +- .../collector/MicroMeterCollector.java | 45 ++++++- .../starter/common/monitor/DtpEndpoint.java | 6 +- 8 files changed, 187 insertions(+), 112 deletions(-) create mode 100644 common/src/main/java/org/dromara/dynamictp/common/entity/VTExecutorStats.java delete mode 100644 common/src/main/java/org/dromara/dynamictp/common/entity/VTTaskStats.java diff --git a/common/src/main/java/org/dromara/dynamictp/common/entity/VTExecutorStats.java b/common/src/main/java/org/dromara/dynamictp/common/entity/VTExecutorStats.java new file mode 100644 index 000000000..3ff0a25f0 --- /dev/null +++ b/common/src/main/java/org/dromara/dynamictp/common/entity/VTExecutorStats.java @@ -0,0 +1,127 @@ +package org.dromara.dynamictp.common.entity; + +import lombok.Data; +import lombok.EqualsAndHashCode; + +import java.util.List; + +/** + * ClassName: VTTaskStats + * Package: org.dromara.dynamictp.common.entity + * Description: + * Wanted performance is like: + * "virtual_threads": [ + * { + * "id": 1, + * "name": "VirtualThread-1", + * "state": "RUNNABLE", + * "stack_trace": [ + * { + * "class": "java.base/java.lang.Thread", + * "method": "lambda$main$0", + * "file": "Main.java", + * "line": 10 + * } + * ] + * }, + * { + * "id": 2, + * "name": "VirtualThread-2", + * "state": "BLOCKED", + * "stack_trace": [ + * { + * "class": "java.base/java.net.SocketInputStream", + * "method": "socketRead0", + * "file": "SocketInputStream.java", + * "line": 61 + * } + * ] + * } + * + * @author CYC + * @create 2024/11/4 16:52 + */ +@Data +@EqualsAndHashCode(callSuper = true) +public class VTExecutorStats extends Metrics { + + /** + * 虚拟线程执行器的id + */ + private int id; + + /** + * 虚拟线程执行器的名称 + */ + private String name; + + /** + * 虚拟线程执行器的别名 + */ + private String aliasName; + + /** + * 虚拟线程执行器所承载的任务 + */ + private List tasks; + + public int getTasksCount() { + return tasks.size(); + } + + @Data + public class VTTaskStats { + + /** + * 虚拟线程的id + */ + private int id; + + /** + * 虚拟线程的名称 + */ + private String name; + + /** + * 虚拟线程的状态 + */ + private String state; + + /** + * 虚拟线程所承载任务的所有堆栈信息 + */ + private List stack; + + /** + * 虚拟线程所承载任务的理想的堆栈信息 + */ + private StackForGood stackForGood; + + /** + * 虚拟线程的任务数 + */ + private int taskCount; + + @Data + private class StackForGood { + /** + * Wanted stack trace is like: + * "stack_trace": [ + * { + * "class": "java.base/java.net.SocketInputStream", + * "method": "socketRead0", + * "file": "SocketInputStream.java", + * "line": 61 + * } + * ] + */ + private String clazz; + + private String method; + + private String file; + + private String line; + } + } +} diff --git a/common/src/main/java/org/dromara/dynamictp/common/entity/VTTaskStats.java b/common/src/main/java/org/dromara/dynamictp/common/entity/VTTaskStats.java deleted file mode 100644 index 5b7c6f1d6..000000000 --- a/common/src/main/java/org/dromara/dynamictp/common/entity/VTTaskStats.java +++ /dev/null @@ -1,97 +0,0 @@ -package org.dromara.dynamictp.common.entity; - -import lombok.Data; -import lombok.EqualsAndHashCode; - -import java.util.List; - -/** - * ClassName: VTTaskStats - * Package: org.dromara.dynamictp.common.entity - * Description: - * Wanted performance is like: - * "virtual_threads": [ - * { - * "id": 1, - * "name": "VirtualThread-1", - * "state": "RUNNABLE", - * "stack_trace": [ - * { - * "class": "java.base/java.lang.Thread", - * "method": "lambda$main$0", - * "file": "Main.java", - * "line": 10 - * } - * ] - * }, - * { - * "id": 2, - * "name": "VirtualThread-2", - * "state": "BLOCKED", - * "stack_trace": [ - * { - * "class": "java.base/java.net.SocketInputStream", - * "method": "socketRead0", - * "file": "SocketInputStream.java", - * "line": 61 - * } - * ] - * } - * @author CYC - * @create 2024/11/4 16:52 - */ -@Data -@EqualsAndHashCode(callSuper = true) -public class VTTaskStats extends Metrics{ - - /** - * 虚拟线程的id - */ - private int id; - - /** - * 虚拟线程的名称 - */ - private String name; - - /** - * 虚拟线程的状态 - */ - private String state; - - /** - * 虚拟线程所承载任务的所有堆栈信息 - */ - private List stack; - - /** - * 虚拟线程所承载任务的理想的堆栈信息 - */ - private StackForGood stackForGood; - - /** - * 虚拟线程的任务数 - */ - private int taskCount; - - private class StackForGood { - /** - * Wanted stack trace is like: - * "stack_trace": [ - * { - * "class": "java.base/java.net.SocketInputStream", - * "method": "socketRead0", - * "file": "SocketInputStream.java", - * "line": 61 - * } - * ] - */ - private String clazz; - - private String method; - - private String file; - - private String line; - } -} 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 9e3171322..690be2155 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 @@ -20,7 +20,7 @@ import lombok.val; import org.dromara.dynamictp.common.entity.ThreadPoolStats; import org.dromara.dynamictp.common.entity.TpMainFields; -import org.dromara.dynamictp.common.entity.VTTaskStats; +import org.dromara.dynamictp.common.entity.VTExecutorStats; import org.dromara.dynamictp.core.executor.DtpExecutor; import org.dromara.dynamictp.core.monitor.PerformanceProvider; import org.dromara.dynamictp.core.support.ExecutorAdapter; @@ -83,8 +83,10 @@ public static ThreadPoolStats toMetrics(ExecutorWrapper wrapper) { return poolStats; } - public static VTTaskStats toVTTaskMetrics(ExecutorWrapper wrapper) { - return null; + public static VTExecutorStats toVTTaskMetrics(ExecutorWrapper wrapper) { + VTExecutorStats vtTaskStats = new VTExecutorStats(); + + return vtTaskStats; } private static ThreadPoolStats convertCommon(ExecutorAdapter executor) { diff --git a/core/src/main/java/org/dromara/dynamictp/core/handler/CollectorHandler.java b/core/src/main/java/org/dromara/dynamictp/core/handler/CollectorHandler.java index fb485bc6c..10cb96e40 100644 --- a/core/src/main/java/org/dromara/dynamictp/core/handler/CollectorHandler.java +++ b/core/src/main/java/org/dromara/dynamictp/core/handler/CollectorHandler.java @@ -20,7 +20,7 @@ import com.google.common.collect.Maps; import lombok.extern.slf4j.Slf4j; import org.dromara.dynamictp.common.entity.ThreadPoolStats; -import org.dromara.dynamictp.common.entity.VTTaskStats; +import org.dromara.dynamictp.common.entity.VTExecutorStats; import org.dromara.dynamictp.common.util.ExtensionServiceLoader; import org.dromara.dynamictp.core.monitor.collector.InternalLogCollector; import org.dromara.dynamictp.core.monitor.collector.LogCollector; @@ -45,7 +45,7 @@ public final class CollectorHandler { private CollectorHandler() { List loadedCollectors = ExtensionServiceLoader.get(MetricsCollector.class); - loadedCollectors.forEach(collector -> COLLECTORS.put(collector.type().toLowerCase(), collector)); + loadedCollectors.forEach(collector -> COLLECTORS.put(collector.type(), collector)); MetricsCollector microMeterCollector = new MicroMeterCollector(); LogCollector logCollector = new LogCollector(); @@ -69,7 +69,7 @@ public void collect(ThreadPoolStats poolStats, List types) { } } - public void collectVTtask(VTTaskStats vtTaskStats, List types) { + public void collectVTtask(VTExecutorStats vtTaskStats, List types) { if (vtTaskStats == null || CollectionUtils.isEmpty(types)) { return; } diff --git a/core/src/main/java/org/dromara/dynamictp/core/monitor/DtpMonitor.java b/core/src/main/java/org/dromara/dynamictp/core/monitor/DtpMonitor.java index 6729f1f09..0ab386440 100644 --- a/core/src/main/java/org/dromara/dynamictp/core/monitor/DtpMonitor.java +++ b/core/src/main/java/org/dromara/dynamictp/core/monitor/DtpMonitor.java @@ -19,7 +19,7 @@ import lombok.extern.slf4j.Slf4j; import org.dromara.dynamictp.common.entity.ThreadPoolStats; -import org.dromara.dynamictp.common.entity.VTTaskStats; +import org.dromara.dynamictp.common.entity.VTExecutorStats; import org.dromara.dynamictp.common.event.AlarmCheckEvent; import org.dromara.dynamictp.common.event.CollectEvent; import org.dromara.dynamictp.common.properties.DtpProperties; @@ -105,7 +105,7 @@ private void collectMetrics(Set executorNames) { publishCollectEvent(); } - private void doCollectVTTask(VTTaskStats vtTaskStats) { + private void doCollectVTTask(VTExecutorStats vtTaskStats) { try { CollectorHandler.getInstance().collectVTtask(vtTaskStats, dtpProperties.getCollectorTypes()); } catch (Exception e) { diff --git a/core/src/main/java/org/dromara/dynamictp/core/monitor/collector/MetricsCollector.java b/core/src/main/java/org/dromara/dynamictp/core/monitor/collector/MetricsCollector.java index 2f5388dce..0d2aaa7ca 100644 --- a/core/src/main/java/org/dromara/dynamictp/core/monitor/collector/MetricsCollector.java +++ b/core/src/main/java/org/dromara/dynamictp/core/monitor/collector/MetricsCollector.java @@ -18,7 +18,7 @@ package org.dromara.dynamictp.core.monitor.collector; import org.dromara.dynamictp.common.entity.ThreadPoolStats; -import org.dromara.dynamictp.common.entity.VTTaskStats; +import org.dromara.dynamictp.common.entity.VTExecutorStats; /** * MetricsCollector related @@ -35,10 +35,10 @@ public interface MetricsCollector { void collect(ThreadPoolStats poolStats); /** - * + * Collect virtual thread key metrics. * @param vtTaskStats */ - void collect(VTTaskStats vtTaskStats); + void collect(VTExecutorStats vtTaskStats); /** * Collector type. diff --git a/core/src/main/java/org/dromara/dynamictp/core/monitor/collector/MicroMeterCollector.java b/core/src/main/java/org/dromara/dynamictp/core/monitor/collector/MicroMeterCollector.java index 37b7f1d48..8180109af 100644 --- a/core/src/main/java/org/dromara/dynamictp/core/monitor/collector/MicroMeterCollector.java +++ b/core/src/main/java/org/dromara/dynamictp/core/monitor/collector/MicroMeterCollector.java @@ -22,6 +22,7 @@ import lombok.extern.slf4j.Slf4j; import org.dromara.dynamictp.common.em.CollectorTypeEnum; import org.dromara.dynamictp.common.entity.ThreadPoolStats; +import org.dromara.dynamictp.common.entity.VTExecutorStats; import org.dromara.dynamictp.common.util.CommonUtil; import org.springframework.beans.BeanUtils; @@ -50,20 +51,37 @@ public class MicroMeterCollector extends AbstractCollector { public static final String POOL_ALIAS_TAG = DTP_METRIC_NAME_PREFIX + ".alias"; + public static final String VTE_METRIC_NAME_PREFIX = "virtual.thread.executor"; + + public static final String VTE_NAME_TAG = VTE_METRIC_NAME_PREFIX + ".name"; + + public static final String VTE_ALIAS_TAG = VTE_METRIC_NAME_PREFIX + ".alias"; + public static final String APP_NAME_TAG = "app.name"; - private static final Map GAUGE_CACHE = new ConcurrentHashMap<>(); + private static final Map GAUGE_CACHE = new ConcurrentHashMap<>(); @Override public void collect(ThreadPoolStats threadPoolStats) { // metrics must be held with a strong reference, even though it is never referenced within this class - ThreadPoolStats oldStats = GAUGE_CACHE.get(threadPoolStats.getPoolName()); + ThreadPoolStats oldStats = (ThreadPoolStats) GAUGE_CACHE.get(threadPoolStats.getPoolName()); if (Objects.isNull(oldStats)) { GAUGE_CACHE.put(threadPoolStats.getPoolName(), threadPoolStats); } else { BeanUtils.copyProperties(threadPoolStats, oldStats); } - gauge(GAUGE_CACHE.get(threadPoolStats.getPoolName())); + gauge((ThreadPoolStats) GAUGE_CACHE.get(threadPoolStats.getPoolName())); + } + + @Override + public void collect(VTExecutorStats vtTaskStats) { + VTExecutorStats oldStats = (VTExecutorStats) GAUGE_CACHE.get(vtTaskStats.getName()); + if (Objects.isNull(oldStats)) { + GAUGE_CACHE.put(vtTaskStats.getName(), vtTaskStats); + } else { + BeanUtils.copyProperties(vtTaskStats, oldStats); + } + gauge((VTExecutorStats) GAUGE_CACHE.get(vtTaskStats.getName())); } @Override @@ -105,6 +123,19 @@ public void gauge(ThreadPoolStats poolStats) { Metrics.gauge(metricName("completed.task.time.tp999"), tags, poolStats, ThreadPoolStats::getTp999); } + private void gauge(VTExecutorStats vtExecutorStats) { + Iterable tags = getTags(vtExecutorStats); + + Metrics.gauge(metricName("virtual.thread.executor.id"), tags, vtExecutorStats, VTExecutorStats::getId); + Metrics.gauge(metricName("virtual.thread.executor.task.count"), tags, vtExecutorStats, VTExecutorStats::getTasksCount); + + vtExecutorStats.getTasks().forEach(vtTaskStats -> { + Metrics.gauge(metricName("task.id"), tags, vtTaskStats, VTExecutorStats.VTTaskStats::getId); + + }); + + } + private static String metricName(String name) { return String.join(".", DTP_METRIC_NAME_PREFIX, name); } @@ -117,5 +148,13 @@ private Iterable getTags(ThreadPoolStats poolStats) { tags.add(Tag.of(POOL_ALIAS_TAG, Optional.ofNullable(poolStats.getPoolAliasName()).orElse(poolStats.getPoolName()))); return tags; } + + private Iterable getTags(VTExecutorStats vtExecutorStats) { + ArrayList tags = new ArrayList<>(3); + tags.add(Tag.of(VTE_NAME_TAG, vtExecutorStats.getName())); + tags.add(Tag.of(APP_NAME_TAG, CommonUtil.getInstance().getServiceName())); + tags.add(Tag.of(VTE_ALIAS_TAG, Optional.ofNullable(vtExecutorStats.getAliasName()).orElse(vtExecutorStats.getName()))); + return tags; + } } diff --git a/starter/starter-common/src/main/java/org/dromara/dynamictp/starter/common/monitor/DtpEndpoint.java b/starter/starter-common/src/main/java/org/dromara/dynamictp/starter/common/monitor/DtpEndpoint.java index abfca8ce4..3eca94ade 100644 --- a/starter/starter-common/src/main/java/org/dromara/dynamictp/starter/common/monitor/DtpEndpoint.java +++ b/starter/starter-common/src/main/java/org/dromara/dynamictp/starter/common/monitor/DtpEndpoint.java @@ -48,7 +48,11 @@ public List invoke() { List metricsList = Lists.newArrayList(); DtpRegistry.getAllExecutorNames().forEach(x -> { ExecutorWrapper wrapper = DtpRegistry.getExecutorWrapper(x); - metricsList.add(ExecutorConverter.toMetrics(wrapper)); + if(wrapper.isVirtualThreadExecutor()) { + metricsList.add(ExecutorConverter.toVTTaskMetrics(wrapper)); + } else { + metricsList.add(ExecutorConverter.toMetrics(wrapper)); + } }); val handlerMap = ApplicationContextHolder.getBeansOfType(MetricsAware.class); From ebc5e0ee7af7cad1ae44995b28c1d79ef2bf3b02 Mon Sep 17 00:00:00 2001 From: TheFatRatre <1981590178@qq.com> Date: Thu, 7 Nov 2024 14:55:30 +0800 Subject: [PATCH 15/26] =?UTF-8?q?=E7=9B=91=E6=8E=A7=E5=9F=BA=E6=9C=AC?= =?UTF-8?q?=E5=AE=9E=E7=8E=B0=EF=BC=8C=E7=95=99=E4=BA=86=E4=B8=80=E4=BA=9B?= =?UTF-8?q?=E4=BF=9D=E7=95=99=E5=AD=97=E6=AE=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../common/entity/VTExecutorStats.java | 145 +++++++----------- .../core/converter/ExecutorConverter.java | 33 +++- .../dynamictp/core/monitor/DtpMonitor.java | 2 +- .../collector/InternalLogCollector.java | 6 + .../core/monitor/collector/LogCollector.java | 11 ++ .../collector/MicroMeterCollector.java | 43 +++--- .../monitor/collector/jmx/JMXCollector.java | 24 ++- .../collector/jmx/VTExecutorStatsJMX.java | 29 ++++ .../collector/jmx/VTExecutorStatsMXBean.java | 21 +++ .../example/collector/EsCollector.java | 6 + .../example/collector/EsCollector.java | 6 + .../starter/common/monitor/DtpEndpoint.java | 2 +- 12 files changed, 214 insertions(+), 114 deletions(-) create mode 100644 core/src/main/java/org/dromara/dynamictp/core/monitor/collector/jmx/VTExecutorStatsJMX.java create mode 100644 core/src/main/java/org/dromara/dynamictp/core/monitor/collector/jmx/VTExecutorStatsMXBean.java diff --git a/common/src/main/java/org/dromara/dynamictp/common/entity/VTExecutorStats.java b/common/src/main/java/org/dromara/dynamictp/common/entity/VTExecutorStats.java index 3ff0a25f0..f67fde4df 100644 --- a/common/src/main/java/org/dromara/dynamictp/common/entity/VTExecutorStats.java +++ b/common/src/main/java/org/dromara/dynamictp/common/entity/VTExecutorStats.java @@ -3,40 +3,10 @@ import lombok.Data; import lombok.EqualsAndHashCode; -import java.util.List; - /** - * ClassName: VTTaskStats + * ClassName: VTExecutorStats * Package: org.dromara.dynamictp.common.entity * Description: - * Wanted performance is like: - * "virtual_threads": [ - * { - * "id": 1, - * "name": "VirtualThread-1", - * "state": "RUNNABLE", - * "stack_trace": [ - * { - * "class": "java.base/java.lang.Thread", - * "method": "lambda$main$0", - * "file": "Main.java", - * "line": 10 - * } - * ] - * }, - * { - * "id": 2, - * "name": "VirtualThread-2", - * "state": "BLOCKED", - * "stack_trace": [ - * { - * "class": "java.base/java.net.SocketInputStream", - * "method": "socketRead0", - * "file": "SocketInputStream.java", - * "line": 61 - * } - * ] - * } * * @author CYC * @create 2024/11/4 16:52 @@ -46,82 +16,83 @@ public class VTExecutorStats extends Metrics { /** - * 虚拟线程执行器的id + * 虚拟线程执行器名字 */ - private int id; + private String executorName; /** - * 虚拟线程执行器的名称 + * 虚拟线程执行器别名 */ - private String name; + private String executorAliasName; /** - * 虚拟线程执行器的别名 + * 正在执行任务的活跃线程大致总数 */ - private String aliasName; + private int activeCount; /** - * 虚拟线程执行器所承载的任务 + * 大致任务总数 */ - private List tasks; + private long taskCount; - public int getTasksCount() { - return tasks.size(); - } + /** + * 执行超时任务数量 + */ + private long runTimeoutCount; - @Data - public class VTTaskStats { + /** + * 是否为DtpExecutor + */ + private boolean dynamic; - /** - * 虚拟线程的id - */ - private int id; + /** + * tps + */ + private double tps; - /** - * 虚拟线程的名称 - */ - private String name; + /** + * 最大任务耗时 + */ + private long maxRt; + + /** + * 最小任务耗时 + */ + private long minRt; - /** - * 虚拟线程的状态 - */ - private String state; + /** + * 任务平均耗时(单位:ms) + */ + private double avg; - /** - * 虚拟线程所承载任务的所有堆栈信息 - */ - private List stack; + /** + * 满足50%的任务执行所需的最低耗时 + */ + private double tp50; - /** - * 虚拟线程所承载任务的理想的堆栈信息 - */ - private StackForGood stackForGood; + /** + * 满足75%的任务执行所需的最低耗时 + */ + private double tp75; - /** - * 虚拟线程的任务数 - */ - private int taskCount; + /** + * 满足90%的任务执行所需的最低耗时 + */ + private double tp90; - @Data - private class StackForGood { - /** - * Wanted stack trace is like: - * "stack_trace": [ - * { - * "class": "java.base/java.net.SocketInputStream", - * "method": "socketRead0", - * "file": "SocketInputStream.java", - * "line": 61 - * } - * ] - */ - private String clazz; + /** + * 满足95%的任务执行所需的最低耗时 + */ + private double tp95; - private String method; + /** + * 满足99%的任务执行所需的最低耗时 + */ + private double tp99; - private String file; + /** + * 满足99.9%的任务执行所需的最低耗时 + */ + private double tp999; - private String line; - } - } } 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 690be2155..529fc8c01 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 @@ -83,10 +83,30 @@ public static ThreadPoolStats toMetrics(ExecutorWrapper wrapper) { return poolStats; } - public static VTExecutorStats toVTTaskMetrics(ExecutorWrapper wrapper) { - VTExecutorStats vtTaskStats = new VTExecutorStats(); + public static VTExecutorStats toVTExecutorMetrics(ExecutorWrapper wrapper) { + ExecutorAdapter executor = wrapper.getExecutor(); + if (executor == null) { + return null; + } + ThreadPoolStatProvider provider = wrapper.getThreadPoolStatProvider(); + PerformanceProvider performanceProvider = provider.getPerformanceProvider(); + val performanceSnapshot = performanceProvider.getSnapshotAndReset(); + VTExecutorStats executorStats = convertCommonVT(executor); + executorStats.setExecutorName(wrapper.getThreadPoolName()); + executorStats.setExecutorAliasName(wrapper.getThreadPoolAliasName()); + executorStats.setDynamic(executor instanceof DtpExecutor); - return vtTaskStats; + executorStats.setTps(performanceSnapshot.getTps()); + executorStats.setAvg(performanceSnapshot.getAvg()); + executorStats.setMaxRt(performanceSnapshot.getMaxRt()); + executorStats.setMinRt(performanceSnapshot.getMinRt()); + executorStats.setTp50(performanceSnapshot.getTp50()); + executorStats.setTp75(performanceSnapshot.getTp75()); + executorStats.setTp90(performanceSnapshot.getTp90()); + executorStats.setTp95(performanceSnapshot.getTp95()); + executorStats.setTp99(performanceSnapshot.getTp99()); + executorStats.setTp999(performanceSnapshot.getTp999()); + return executorStats; } private static ThreadPoolStats convertCommon(ExecutorAdapter executor) { @@ -107,4 +127,11 @@ private static ThreadPoolStats convertCommon(ExecutorAdapter executor) { poolStats.setKeepAliveTime(executor.getKeepAliveTime(TimeUnit.MILLISECONDS)); return poolStats; } + + private static VTExecutorStats convertCommonVT(ExecutorAdapter executor) { + VTExecutorStats executorStats = new VTExecutorStats(); + executorStats.setActiveCount(executor.getActiveCount()); + executorStats.setTaskCount(executor.getTaskCount()); + return executorStats; + } } diff --git a/core/src/main/java/org/dromara/dynamictp/core/monitor/DtpMonitor.java b/core/src/main/java/org/dromara/dynamictp/core/monitor/DtpMonitor.java index 0ab386440..2ea4b1586 100644 --- a/core/src/main/java/org/dromara/dynamictp/core/monitor/DtpMonitor.java +++ b/core/src/main/java/org/dromara/dynamictp/core/monitor/DtpMonitor.java @@ -97,7 +97,7 @@ private void collectMetrics(Set executorNames) { executorNames.forEach(x -> { ExecutorWrapper wrapper = DtpRegistry.getExecutorWrapper(x); if(wrapper.isVirtualThreadExecutor()) { - doCollectVTTask(ExecutorConverter.toVTTaskMetrics(wrapper)); + doCollectVTTask(ExecutorConverter.toVTExecutorMetrics(wrapper)); } else { doCollect(ExecutorConverter.toMetrics(wrapper)); } diff --git a/core/src/main/java/org/dromara/dynamictp/core/monitor/collector/InternalLogCollector.java b/core/src/main/java/org/dromara/dynamictp/core/monitor/collector/InternalLogCollector.java index aa8d8a242..c43ea2d17 100644 --- a/core/src/main/java/org/dromara/dynamictp/core/monitor/collector/InternalLogCollector.java +++ b/core/src/main/java/org/dromara/dynamictp/core/monitor/collector/InternalLogCollector.java @@ -19,6 +19,7 @@ import org.dromara.dynamictp.common.entity.ThreadPoolStats; import org.dromara.dynamictp.common.em.CollectorTypeEnum; +import org.dromara.dynamictp.common.entity.VTExecutorStats; import org.dromara.dynamictp.common.util.JsonUtil; import lombok.extern.slf4j.Slf4j; @@ -33,6 +34,11 @@ public void collect(ThreadPoolStats poolStats) { log.info("dynamic.tp metrics: {}", JsonUtil.toJson(poolStats)); } + @Override + public void collect(VTExecutorStats vtTaskStats) { + log.info("dynamic.vt metrics: {}", JsonUtil.toJson(vtTaskStats)); + } + @Override public String type() { return CollectorTypeEnum.INTERNAL_LOGGING.name().toLowerCase(); diff --git a/core/src/main/java/org/dromara/dynamictp/core/monitor/collector/LogCollector.java b/core/src/main/java/org/dromara/dynamictp/core/monitor/collector/LogCollector.java index d5c212f34..48b468c97 100644 --- a/core/src/main/java/org/dromara/dynamictp/core/monitor/collector/LogCollector.java +++ b/core/src/main/java/org/dromara/dynamictp/core/monitor/collector/LogCollector.java @@ -19,6 +19,7 @@ import org.dromara.dynamictp.common.em.CollectorTypeEnum; import org.dromara.dynamictp.common.entity.ThreadPoolStats; +import org.dromara.dynamictp.common.entity.VTExecutorStats; import org.dromara.dynamictp.common.util.JsonUtil; import org.dromara.dynamictp.logging.LogHelper; import lombok.extern.slf4j.Slf4j; @@ -42,6 +43,16 @@ public void collect(ThreadPoolStats threadPoolStats) { LogHelper.getMonitorLogger().info("{}", metrics); } + @Override + public void collect(VTExecutorStats vtTaskStats) { + String metrics = JsonUtil.toJson(vtTaskStats); + if (LogHelper.getMonitorLogger() == null) { + log.error("Cannot find monitor logger..."); + return; + } + LogHelper.getMonitorLogger().info("{}", metrics); + } + @Override public String type() { return CollectorTypeEnum.LOGGING.name().toLowerCase(); diff --git a/core/src/main/java/org/dromara/dynamictp/core/monitor/collector/MicroMeterCollector.java b/core/src/main/java/org/dromara/dynamictp/core/monitor/collector/MicroMeterCollector.java index 8180109af..dc91ea6d1 100644 --- a/core/src/main/java/org/dromara/dynamictp/core/monitor/collector/MicroMeterCollector.java +++ b/core/src/main/java/org/dromara/dynamictp/core/monitor/collector/MicroMeterCollector.java @@ -26,11 +26,7 @@ import org.dromara.dynamictp.common.util.CommonUtil; import org.springframework.beans.BeanUtils; -import java.util.ArrayList; -import java.util.List; -import java.util.Map; -import java.util.Objects; -import java.util.Optional; +import java.util.*; import java.util.concurrent.ConcurrentHashMap; /** @@ -74,14 +70,14 @@ public void collect(ThreadPoolStats threadPoolStats) { } @Override - public void collect(VTExecutorStats vtTaskStats) { - VTExecutorStats oldStats = (VTExecutorStats) GAUGE_CACHE.get(vtTaskStats.getName()); + public void collect(VTExecutorStats vtExecutorStats) { + VTExecutorStats oldStats = (VTExecutorStats) GAUGE_CACHE.get(vtExecutorStats.getExecutorName()); if (Objects.isNull(oldStats)) { - GAUGE_CACHE.put(vtTaskStats.getName(), vtTaskStats); + GAUGE_CACHE.put(vtExecutorStats.getExecutorName(), vtExecutorStats); } else { - BeanUtils.copyProperties(vtTaskStats, oldStats); + BeanUtils.copyProperties(vtExecutorStats, oldStats); } - gauge((VTExecutorStats) GAUGE_CACHE.get(vtTaskStats.getName())); + gauge((VTExecutorStats) GAUGE_CACHE.get(vtExecutorStats.getExecutorName())); } @Override @@ -124,16 +120,23 @@ public void gauge(ThreadPoolStats poolStats) { } private void gauge(VTExecutorStats vtExecutorStats) { - Iterable tags = getTags(vtExecutorStats); - - Metrics.gauge(metricName("virtual.thread.executor.id"), tags, vtExecutorStats, VTExecutorStats::getId); - Metrics.gauge(metricName("virtual.thread.executor.task.count"), tags, vtExecutorStats, VTExecutorStats::getTasksCount); - vtExecutorStats.getTasks().forEach(vtTaskStats -> { - Metrics.gauge(metricName("task.id"), tags, vtTaskStats, VTExecutorStats.VTTaskStats::getId); - - }); + Iterable tags = getTags(vtExecutorStats); + Metrics.gauge(metricName("active.count"), tags, vtExecutorStats, VTExecutorStats::getActiveCount); + Metrics.gauge(metricName("task.count"), tags, vtExecutorStats, VTExecutorStats::getTaskCount); + Metrics.gauge(metricName("run.timeout.count"), tags, vtExecutorStats, VTExecutorStats::getRunTimeoutCount); + + Metrics.gauge(metricName("tps"), tags, vtExecutorStats, VTExecutorStats::getTps); + Metrics.gauge(metricName("completed.task.time.avg"), tags, vtExecutorStats, VTExecutorStats::getAvg); + Metrics.gauge(metricName("completed.task.time.max"), tags, vtExecutorStats, VTExecutorStats::getMaxRt); + Metrics.gauge(metricName("completed.task.time.min"), tags, vtExecutorStats, VTExecutorStats::getMinRt); + Metrics.gauge(metricName("completed.task.time.tp50"), tags, vtExecutorStats, VTExecutorStats::getTp50); + Metrics.gauge(metricName("completed.task.time.tp75"), tags, vtExecutorStats, VTExecutorStats::getTp75); + Metrics.gauge(metricName("completed.task.time.tp90"), tags, vtExecutorStats, VTExecutorStats::getTp90); + Metrics.gauge(metricName("completed.task.time.tp95"), tags, vtExecutorStats, VTExecutorStats::getTp95); + Metrics.gauge(metricName("completed.task.time.tp99"), tags, vtExecutorStats, VTExecutorStats::getTp99); + Metrics.gauge(metricName("completed.task.time.tp999"), tags, vtExecutorStats, VTExecutorStats::getTp999); } private static String metricName(String name) { @@ -151,9 +154,9 @@ private Iterable getTags(ThreadPoolStats poolStats) { private Iterable getTags(VTExecutorStats vtExecutorStats) { ArrayList tags = new ArrayList<>(3); - tags.add(Tag.of(VTE_NAME_TAG, vtExecutorStats.getName())); + tags.add(Tag.of(VTE_NAME_TAG, vtExecutorStats.getExecutorName())); tags.add(Tag.of(APP_NAME_TAG, CommonUtil.getInstance().getServiceName())); - tags.add(Tag.of(VTE_ALIAS_TAG, Optional.ofNullable(vtExecutorStats.getAliasName()).orElse(vtExecutorStats.getName()))); + tags.add(Tag.of(VTE_ALIAS_TAG, Optional.ofNullable(vtExecutorStats.getExecutorAliasName()).orElse(vtExecutorStats.getExecutorName()))); return tags; } } 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 652a0adb1..073973c2c 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 @@ -19,7 +19,9 @@ import lombok.extern.slf4j.Slf4j; import org.dromara.dynamictp.common.em.CollectorTypeEnum; +import org.dromara.dynamictp.common.entity.Metrics; import org.dromara.dynamictp.common.entity.ThreadPoolStats; +import org.dromara.dynamictp.common.entity.VTExecutorStats; import org.dromara.dynamictp.core.monitor.collector.AbstractCollector; import org.springframework.beans.BeanUtils; @@ -43,12 +45,12 @@ public class JMXCollector extends AbstractCollector { /** * 缓存的作用是将注册到JMX的数据,每次都是同一个对象 */ - private static final Map GAUGE_CACHE = new ConcurrentHashMap<>(); + 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()); + ThreadPoolStats poolStats = (ThreadPoolStats) GAUGE_CACHE.get(threadPoolStats.getPoolName()); BeanUtils.copyProperties(threadPoolStats, poolStats); } else { try { @@ -63,6 +65,24 @@ public void collect(ThreadPoolStats threadPoolStats) { } } + @Override + public void collect(VTExecutorStats vtExecutorStats) { + if (GAUGE_CACHE.containsKey(vtExecutorStats.getExecutorName())) { + ThreadPoolStats poolStats = (ThreadPoolStats) GAUGE_CACHE.get(vtExecutorStats.getExecutorName()); + BeanUtils.copyProperties(vtExecutorStats, poolStats); + } else { + try { + MBeanServer server = ManagementFactory.getPlatformMBeanServer(); + ObjectName name = new ObjectName(DTP_METRIC_NAME_PREFIX + ":name=" + vtExecutorStats.getExecutorName()); + VTExecutorStatsJMX stats = new VTExecutorStatsJMX(vtExecutorStats); + server.registerMBean(stats, name); + } catch (JMException e) { + log.error("collect thread pool stats error", e); + } + GAUGE_CACHE.put(vtExecutorStats.getExecutorName(), vtExecutorStats); + } + } + @Override public String type() { return CollectorTypeEnum.JMX.name().toLowerCase(); diff --git a/core/src/main/java/org/dromara/dynamictp/core/monitor/collector/jmx/VTExecutorStatsJMX.java b/core/src/main/java/org/dromara/dynamictp/core/monitor/collector/jmx/VTExecutorStatsJMX.java new file mode 100644 index 000000000..d49059078 --- /dev/null +++ b/core/src/main/java/org/dromara/dynamictp/core/monitor/collector/jmx/VTExecutorStatsJMX.java @@ -0,0 +1,29 @@ +package org.dromara.dynamictp.core.monitor.collector.jmx; + +import org.dromara.dynamictp.common.entity.VTExecutorStats; + +/** + * ClassName: VTExecutorStatsJMX + * Package: org.dromara.dynamictp.core.monitor.collector.jmx + * Description: + * + * @author CYC + * @create 2024/11/5 18:29 + */ +public class VTExecutorStatsJMX implements VTExecutorStatsMXBean{ + + private VTExecutorStats vtExecutorStats; + public VTExecutorStatsJMX(VTExecutorStats vtExecutorStats) { + this.vtExecutorStats = vtExecutorStats; + } + + @Override + public VTExecutorStats getVTExecutorStats() { + return vtExecutorStats; + } + + @Override + public void setVTExecutorStats(VTExecutorStats vtExecutorStats) { + this.vtExecutorStats = vtExecutorStats; + } +} diff --git a/core/src/main/java/org/dromara/dynamictp/core/monitor/collector/jmx/VTExecutorStatsMXBean.java b/core/src/main/java/org/dromara/dynamictp/core/monitor/collector/jmx/VTExecutorStatsMXBean.java new file mode 100644 index 000000000..d10fb2bda --- /dev/null +++ b/core/src/main/java/org/dromara/dynamictp/core/monitor/collector/jmx/VTExecutorStatsMXBean.java @@ -0,0 +1,21 @@ +package org.dromara.dynamictp.core.monitor.collector.jmx; + +import org.dromara.dynamictp.common.entity.VTExecutorStats; + +import javax.management.MXBean; + +/** + * ClassName: VTExecutorStatsMXBean + * Package: org.dromara.dynamictp.core.monitor.collector.jmx + * Description: + * + * @author CYC + * @create 2024/11/7 14:44 + */ +@MXBean +public interface VTExecutorStatsMXBean { + + VTExecutorStats getVTExecutorStats(); + + void setVTExecutorStats(VTExecutorStats vtExecutorStats); +} diff --git a/example/example-nacos-cloud/src/main/java/org/dromara/dynamictp/example/collector/EsCollector.java b/example/example-nacos-cloud/src/main/java/org/dromara/dynamictp/example/collector/EsCollector.java index 0dd2c4334..fd02cde1d 100644 --- a/example/example-nacos-cloud/src/main/java/org/dromara/dynamictp/example/collector/EsCollector.java +++ b/example/example-nacos-cloud/src/main/java/org/dromara/dynamictp/example/collector/EsCollector.java @@ -18,6 +18,7 @@ package org.dromara.dynamictp.example.collector; import org.dromara.dynamictp.common.entity.ThreadPoolStats; +import org.dromara.dynamictp.common.entity.VTExecutorStats; import org.dromara.dynamictp.common.util.JsonUtil; import org.dromara.dynamictp.core.monitor.collector.AbstractCollector; @@ -40,6 +41,11 @@ public void collect(ThreadPoolStats poolStats) { esClient.save(JsonUtil.toJson(poolStats)); } + @Override + public void collect(VTExecutorStats vtTaskStats) { + esClient.save(JsonUtil.toJson(vtTaskStats)); + } + @Override public String type() { return "es"; diff --git a/example/example-nacos/src/main/java/org/dromara/dynamictp/example/collector/EsCollector.java b/example/example-nacos/src/main/java/org/dromara/dynamictp/example/collector/EsCollector.java index 0dd2c4334..fd02cde1d 100644 --- a/example/example-nacos/src/main/java/org/dromara/dynamictp/example/collector/EsCollector.java +++ b/example/example-nacos/src/main/java/org/dromara/dynamictp/example/collector/EsCollector.java @@ -18,6 +18,7 @@ package org.dromara.dynamictp.example.collector; import org.dromara.dynamictp.common.entity.ThreadPoolStats; +import org.dromara.dynamictp.common.entity.VTExecutorStats; import org.dromara.dynamictp.common.util.JsonUtil; import org.dromara.dynamictp.core.monitor.collector.AbstractCollector; @@ -40,6 +41,11 @@ public void collect(ThreadPoolStats poolStats) { esClient.save(JsonUtil.toJson(poolStats)); } + @Override + public void collect(VTExecutorStats vtTaskStats) { + esClient.save(JsonUtil.toJson(vtTaskStats)); + } + @Override public String type() { return "es"; diff --git a/starter/starter-common/src/main/java/org/dromara/dynamictp/starter/common/monitor/DtpEndpoint.java b/starter/starter-common/src/main/java/org/dromara/dynamictp/starter/common/monitor/DtpEndpoint.java index 3eca94ade..807ef2719 100644 --- a/starter/starter-common/src/main/java/org/dromara/dynamictp/starter/common/monitor/DtpEndpoint.java +++ b/starter/starter-common/src/main/java/org/dromara/dynamictp/starter/common/monitor/DtpEndpoint.java @@ -49,7 +49,7 @@ public List invoke() { DtpRegistry.getAllExecutorNames().forEach(x -> { ExecutorWrapper wrapper = DtpRegistry.getExecutorWrapper(x); if(wrapper.isVirtualThreadExecutor()) { - metricsList.add(ExecutorConverter.toVTTaskMetrics(wrapper)); + metricsList.add(ExecutorConverter.toVTExecutorMetrics(wrapper)); } else { metricsList.add(ExecutorConverter.toMetrics(wrapper)); } From e29ebd489b430aca047d7f92ad2923af1a257f0b Mon Sep 17 00:00:00 2001 From: TheFatRatre <1981590178@qq.com> Date: Wed, 13 Nov 2024 10:48:09 +0800 Subject: [PATCH 16/26] complete EsCollector,format --- .../dynamictp/common/entity/Metrics.java | 71 +++++++++++++++ .../common/entity/ThreadPoolStats.java | 69 --------------- .../common/entity/VTExecutorStats.java | 87 ++++--------------- .../core/handler/CollectorHandler.java | 6 +- .../dynamictp/core/monitor/DtpMonitor.java | 8 +- .../collector/InternalLogCollector.java | 4 +- .../core/monitor/collector/LogCollector.java | 4 +- .../monitor/collector/MetricsCollector.java | 4 +- .../example/collector/EsCollector.java | 5 ++ .../example/collector/EsCollector.java | 6 ++ .../example/collector/EsCollector.java | 5 ++ .../example/collector/EsCollector.java | 6 ++ .../example/collector/EsCollector.java | 4 +- .../example/collector/EsCollector.java | 4 +- .../example/collector/EsCollector.java | 6 ++ .../example/collector/EsCollector.java | 6 ++ .../example/collector/EsCollector.java | 6 ++ 17 files changed, 145 insertions(+), 156 deletions(-) diff --git a/common/src/main/java/org/dromara/dynamictp/common/entity/Metrics.java b/common/src/main/java/org/dromara/dynamictp/common/entity/Metrics.java index e7f464f43..cd20c7713 100644 --- a/common/src/main/java/org/dromara/dynamictp/common/entity/Metrics.java +++ b/common/src/main/java/org/dromara/dynamictp/common/entity/Metrics.java @@ -27,4 +27,75 @@ **/ @Data public class Metrics { + + /** + * 正在执行任务的活跃线程大致总数 + */ + private int activeCount; + + /** + * 大致任务总数 + */ + private long taskCount; + + /** + * 执行超时任务数量 + */ + private long runTimeoutCount; + + /** + * 是否为DtpExecutor + */ + private boolean dynamic; + + /** + * tps + */ + private double tps; + + /** + * 最大任务耗时 + */ + private long maxRt; + + /** + * 最小任务耗时 + */ + private long minRt; + + /** + * 任务平均耗时(单位:ms) + */ + private double avg; + + /** + * 满足50%的任务执行所需的最低耗时 + */ + private double tp50; + + /** + * 满足75%的任务执行所需的最低耗时 + */ + private double tp75; + + /** + * 满足90%的任务执行所需的最低耗时 + */ + private double tp90; + + /** + * 满足95%的任务执行所需的最低耗时 + */ + private double tp95; + + /** + * 满足99%的任务执行所需的最低耗时 + */ + private double tp99; + + /** + * 满足99.9%的任务执行所需的最低耗时 + */ + private double tp999; + } 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 06b4928b1..0e82a9a10 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 @@ -81,16 +81,6 @@ public class ThreadPoolStats extends Metrics { */ private int queueRemainingCapacity; - /** - * 正在执行任务的活跃线程大致总数 - */ - private int activeCount; - - /** - * 大致任务总数 - */ - private long taskCount; - /** * 已执行完成的大致任务总数 */ @@ -121,68 +111,9 @@ public class ThreadPoolStats extends Metrics { */ private String rejectHandlerName; - /** - * 是否DtpExecutor线程池 - */ - private boolean dynamic; - - /** - * 执行超时任务数量 - */ - private long runTimeoutCount; - /** * 在队列等待超时任务数量 */ private long queueTimeoutCount; - /** - * tps - */ - private double tps; - - /** - * 最大任务耗时 - */ - private long maxRt; - - /** - * 最小任务耗时 - */ - private long minRt; - - /** - * 任务平均耗时(单位:ms) - */ - private double avg; - - /** - * 满足50%的任务执行所需的最低耗时 - */ - private double tp50; - - /** - * 满足75%的任务执行所需的最低耗时 - */ - private double tp75; - - /** - * 满足90%的任务执行所需的最低耗时 - */ - private double tp90; - - /** - * 满足95%的任务执行所需的最低耗时 - */ - private double tp95; - - /** - * 满足99%的任务执行所需的最低耗时 - */ - private double tp99; - - /** - * 满足99.9%的任务执行所需的最低耗时 - */ - private double tp999; } diff --git a/common/src/main/java/org/dromara/dynamictp/common/entity/VTExecutorStats.java b/common/src/main/java/org/dromara/dynamictp/common/entity/VTExecutorStats.java index f67fde4df..41c59631e 100644 --- a/common/src/main/java/org/dromara/dynamictp/common/entity/VTExecutorStats.java +++ b/common/src/main/java/org/dromara/dynamictp/common/entity/VTExecutorStats.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.common.entity; import lombok.Data; @@ -25,74 +42,4 @@ public class VTExecutorStats extends Metrics { */ private String executorAliasName; - /** - * 正在执行任务的活跃线程大致总数 - */ - private int activeCount; - - /** - * 大致任务总数 - */ - private long taskCount; - - /** - * 执行超时任务数量 - */ - private long runTimeoutCount; - - /** - * 是否为DtpExecutor - */ - private boolean dynamic; - - /** - * tps - */ - private double tps; - - /** - * 最大任务耗时 - */ - private long maxRt; - - /** - * 最小任务耗时 - */ - private long minRt; - - /** - * 任务平均耗时(单位:ms) - */ - private double avg; - - /** - * 满足50%的任务执行所需的最低耗时 - */ - private double tp50; - - /** - * 满足75%的任务执行所需的最低耗时 - */ - private double tp75; - - /** - * 满足90%的任务执行所需的最低耗时 - */ - private double tp90; - - /** - * 满足95%的任务执行所需的最低耗时 - */ - private double tp95; - - /** - * 满足99%的任务执行所需的最低耗时 - */ - private double tp99; - - /** - * 满足99.9%的任务执行所需的最低耗时 - */ - private double tp999; - } diff --git a/core/src/main/java/org/dromara/dynamictp/core/handler/CollectorHandler.java b/core/src/main/java/org/dromara/dynamictp/core/handler/CollectorHandler.java index 10cb96e40..95c3620a3 100644 --- a/core/src/main/java/org/dromara/dynamictp/core/handler/CollectorHandler.java +++ b/core/src/main/java/org/dromara/dynamictp/core/handler/CollectorHandler.java @@ -69,14 +69,14 @@ public void collect(ThreadPoolStats poolStats, List types) { } } - public void collectVTtask(VTExecutorStats vtTaskStats, List types) { - if (vtTaskStats == null || CollectionUtils.isEmpty(types)) { + public void collectVTExecutor(VTExecutorStats vtExecutorStats, List types) { + if (vtExecutorStats == null || CollectionUtils.isEmpty(types)) { return; } for (String collectorType : types) { MetricsCollector collector = COLLECTORS.get(collectorType.toLowerCase()); if (collector != null) { - collector.collect(vtTaskStats); + collector.collect(vtExecutorStats); } } } diff --git a/core/src/main/java/org/dromara/dynamictp/core/monitor/DtpMonitor.java b/core/src/main/java/org/dromara/dynamictp/core/monitor/DtpMonitor.java index 2ea4b1586..ac5bcab8c 100644 --- a/core/src/main/java/org/dromara/dynamictp/core/monitor/DtpMonitor.java +++ b/core/src/main/java/org/dromara/dynamictp/core/monitor/DtpMonitor.java @@ -97,7 +97,7 @@ private void collectMetrics(Set executorNames) { executorNames.forEach(x -> { ExecutorWrapper wrapper = DtpRegistry.getExecutorWrapper(x); if(wrapper.isVirtualThreadExecutor()) { - doCollectVTTask(ExecutorConverter.toVTExecutorMetrics(wrapper)); + doCollectVTExecutor(ExecutorConverter.toVTExecutorMetrics(wrapper)); } else { doCollect(ExecutorConverter.toMetrics(wrapper)); } @@ -105,11 +105,11 @@ private void collectMetrics(Set executorNames) { publishCollectEvent(); } - private void doCollectVTTask(VTExecutorStats vtTaskStats) { + private void doCollectVTExecutor(VTExecutorStats vtExecutorStats) { try { - CollectorHandler.getInstance().collectVTtask(vtTaskStats, dtpProperties.getCollectorTypes()); + CollectorHandler.getInstance().collectVTExecutor(vtExecutorStats, dtpProperties.getCollectorTypes()); } catch (Exception e) { - log.error("DynamicTp monitor, VTTask collect error.", e); + log.error("DynamicTp monitor, VTExecutor collect error.", e); } } diff --git a/core/src/main/java/org/dromara/dynamictp/core/monitor/collector/InternalLogCollector.java b/core/src/main/java/org/dromara/dynamictp/core/monitor/collector/InternalLogCollector.java index c43ea2d17..2b718920d 100644 --- a/core/src/main/java/org/dromara/dynamictp/core/monitor/collector/InternalLogCollector.java +++ b/core/src/main/java/org/dromara/dynamictp/core/monitor/collector/InternalLogCollector.java @@ -35,8 +35,8 @@ public void collect(ThreadPoolStats poolStats) { } @Override - public void collect(VTExecutorStats vtTaskStats) { - log.info("dynamic.vt metrics: {}", JsonUtil.toJson(vtTaskStats)); + public void collect(VTExecutorStats vtExecutorStats) { + log.info("dynamic.vt metrics: {}", JsonUtil.toJson(vtExecutorStats)); } @Override diff --git a/core/src/main/java/org/dromara/dynamictp/core/monitor/collector/LogCollector.java b/core/src/main/java/org/dromara/dynamictp/core/monitor/collector/LogCollector.java index 48b468c97..40939bd9f 100644 --- a/core/src/main/java/org/dromara/dynamictp/core/monitor/collector/LogCollector.java +++ b/core/src/main/java/org/dromara/dynamictp/core/monitor/collector/LogCollector.java @@ -44,8 +44,8 @@ public void collect(ThreadPoolStats threadPoolStats) { } @Override - public void collect(VTExecutorStats vtTaskStats) { - String metrics = JsonUtil.toJson(vtTaskStats); + public void collect(VTExecutorStats vtExecutorStats) { + String metrics = JsonUtil.toJson(vtExecutorStats); if (LogHelper.getMonitorLogger() == null) { log.error("Cannot find monitor logger..."); return; diff --git a/core/src/main/java/org/dromara/dynamictp/core/monitor/collector/MetricsCollector.java b/core/src/main/java/org/dromara/dynamictp/core/monitor/collector/MetricsCollector.java index 0d2aaa7ca..f9617a042 100644 --- a/core/src/main/java/org/dromara/dynamictp/core/monitor/collector/MetricsCollector.java +++ b/core/src/main/java/org/dromara/dynamictp/core/monitor/collector/MetricsCollector.java @@ -36,9 +36,9 @@ public interface MetricsCollector { /** * Collect virtual thread key metrics. - * @param vtTaskStats + * @param vtExecutorStats */ - void collect(VTExecutorStats vtTaskStats); + void collect(VTExecutorStats vtExecutorStats); /** * Collector type. diff --git a/example/example-apollo/src/main/java/org/dromara/dynamictp/example/collector/EsCollector.java b/example/example-apollo/src/main/java/org/dromara/dynamictp/example/collector/EsCollector.java index 0dd2c4334..94d3b56d2 100644 --- a/example/example-apollo/src/main/java/org/dromara/dynamictp/example/collector/EsCollector.java +++ b/example/example-apollo/src/main/java/org/dromara/dynamictp/example/collector/EsCollector.java @@ -18,6 +18,7 @@ package org.dromara.dynamictp.example.collector; import org.dromara.dynamictp.common.entity.ThreadPoolStats; +import org.dromara.dynamictp.common.entity.VTExecutorStats; import org.dromara.dynamictp.common.util.JsonUtil; import org.dromara.dynamictp.core.monitor.collector.AbstractCollector; @@ -40,6 +41,10 @@ public void collect(ThreadPoolStats poolStats) { esClient.save(JsonUtil.toJson(poolStats)); } + @Override + public void collect(VTExecutorStats vtExecutorStats) { + esClient.save(JsonUtil.toJson(vtExecutorStats)); + } @Override public String type() { return "es"; diff --git a/example/example-consul-cloud/src/main/java/org/dromara/dynamictp/example/collector/EsCollector.java b/example/example-consul-cloud/src/main/java/org/dromara/dynamictp/example/collector/EsCollector.java index 0dd2c4334..fecc9f6a2 100644 --- a/example/example-consul-cloud/src/main/java/org/dromara/dynamictp/example/collector/EsCollector.java +++ b/example/example-consul-cloud/src/main/java/org/dromara/dynamictp/example/collector/EsCollector.java @@ -18,6 +18,7 @@ package org.dromara.dynamictp.example.collector; import org.dromara.dynamictp.common.entity.ThreadPoolStats; +import org.dromara.dynamictp.common.entity.VTExecutorStats; import org.dromara.dynamictp.common.util.JsonUtil; import org.dromara.dynamictp.core.monitor.collector.AbstractCollector; @@ -40,6 +41,11 @@ public void collect(ThreadPoolStats poolStats) { esClient.save(JsonUtil.toJson(poolStats)); } + @Override + public void collect(VTExecutorStats vtExecutorStats) { + esClient.save(JsonUtil.toJson(vtExecutorStats)); + } + @Override public String type() { return "es"; diff --git a/example/example-etcd/src/main/java/org/dromara/dynamictp/example/collector/EsCollector.java b/example/example-etcd/src/main/java/org/dromara/dynamictp/example/collector/EsCollector.java index 0dd2c4334..94d3b56d2 100644 --- a/example/example-etcd/src/main/java/org/dromara/dynamictp/example/collector/EsCollector.java +++ b/example/example-etcd/src/main/java/org/dromara/dynamictp/example/collector/EsCollector.java @@ -18,6 +18,7 @@ package org.dromara.dynamictp.example.collector; import org.dromara.dynamictp.common.entity.ThreadPoolStats; +import org.dromara.dynamictp.common.entity.VTExecutorStats; import org.dromara.dynamictp.common.util.JsonUtil; import org.dromara.dynamictp.core.monitor.collector.AbstractCollector; @@ -40,6 +41,10 @@ public void collect(ThreadPoolStats poolStats) { esClient.save(JsonUtil.toJson(poolStats)); } + @Override + public void collect(VTExecutorStats vtExecutorStats) { + esClient.save(JsonUtil.toJson(vtExecutorStats)); + } @Override public String type() { return "es"; diff --git a/example/example-huawei-cloud/src/main/java/org/dromara/dynamictp/example/collector/EsCollector.java b/example/example-huawei-cloud/src/main/java/org/dromara/dynamictp/example/collector/EsCollector.java index 0dd2c4334..fecc9f6a2 100644 --- a/example/example-huawei-cloud/src/main/java/org/dromara/dynamictp/example/collector/EsCollector.java +++ b/example/example-huawei-cloud/src/main/java/org/dromara/dynamictp/example/collector/EsCollector.java @@ -18,6 +18,7 @@ package org.dromara.dynamictp.example.collector; import org.dromara.dynamictp.common.entity.ThreadPoolStats; +import org.dromara.dynamictp.common.entity.VTExecutorStats; import org.dromara.dynamictp.common.util.JsonUtil; import org.dromara.dynamictp.core.monitor.collector.AbstractCollector; @@ -40,6 +41,11 @@ public void collect(ThreadPoolStats poolStats) { esClient.save(JsonUtil.toJson(poolStats)); } + @Override + public void collect(VTExecutorStats vtExecutorStats) { + esClient.save(JsonUtil.toJson(vtExecutorStats)); + } + @Override public String type() { return "es"; diff --git a/example/example-nacos-cloud/src/main/java/org/dromara/dynamictp/example/collector/EsCollector.java b/example/example-nacos-cloud/src/main/java/org/dromara/dynamictp/example/collector/EsCollector.java index fd02cde1d..fecc9f6a2 100644 --- a/example/example-nacos-cloud/src/main/java/org/dromara/dynamictp/example/collector/EsCollector.java +++ b/example/example-nacos-cloud/src/main/java/org/dromara/dynamictp/example/collector/EsCollector.java @@ -42,8 +42,8 @@ public void collect(ThreadPoolStats poolStats) { } @Override - public void collect(VTExecutorStats vtTaskStats) { - esClient.save(JsonUtil.toJson(vtTaskStats)); + public void collect(VTExecutorStats vtExecutorStats) { + esClient.save(JsonUtil.toJson(vtExecutorStats)); } @Override diff --git a/example/example-nacos/src/main/java/org/dromara/dynamictp/example/collector/EsCollector.java b/example/example-nacos/src/main/java/org/dromara/dynamictp/example/collector/EsCollector.java index fd02cde1d..fecc9f6a2 100644 --- a/example/example-nacos/src/main/java/org/dromara/dynamictp/example/collector/EsCollector.java +++ b/example/example-nacos/src/main/java/org/dromara/dynamictp/example/collector/EsCollector.java @@ -42,8 +42,8 @@ public void collect(ThreadPoolStats poolStats) { } @Override - public void collect(VTExecutorStats vtTaskStats) { - esClient.save(JsonUtil.toJson(vtTaskStats)); + public void collect(VTExecutorStats vtExecutorStats) { + esClient.save(JsonUtil.toJson(vtExecutorStats)); } @Override diff --git a/example/example-polaris-cloud/src/main/java/org/dromara/dynamictp/example/collector/EsCollector.java b/example/example-polaris-cloud/src/main/java/org/dromara/dynamictp/example/collector/EsCollector.java index 0dd2c4334..fecc9f6a2 100644 --- a/example/example-polaris-cloud/src/main/java/org/dromara/dynamictp/example/collector/EsCollector.java +++ b/example/example-polaris-cloud/src/main/java/org/dromara/dynamictp/example/collector/EsCollector.java @@ -18,6 +18,7 @@ package org.dromara.dynamictp.example.collector; import org.dromara.dynamictp.common.entity.ThreadPoolStats; +import org.dromara.dynamictp.common.entity.VTExecutorStats; import org.dromara.dynamictp.common.util.JsonUtil; import org.dromara.dynamictp.core.monitor.collector.AbstractCollector; @@ -40,6 +41,11 @@ public void collect(ThreadPoolStats poolStats) { esClient.save(JsonUtil.toJson(poolStats)); } + @Override + public void collect(VTExecutorStats vtExecutorStats) { + esClient.save(JsonUtil.toJson(vtExecutorStats)); + } + @Override public String type() { return "es"; diff --git a/example/example-zookeeper-cloud/src/main/java/org/dromara/dynamictp/example/collector/EsCollector.java b/example/example-zookeeper-cloud/src/main/java/org/dromara/dynamictp/example/collector/EsCollector.java index 0dd2c4334..fecc9f6a2 100644 --- a/example/example-zookeeper-cloud/src/main/java/org/dromara/dynamictp/example/collector/EsCollector.java +++ b/example/example-zookeeper-cloud/src/main/java/org/dromara/dynamictp/example/collector/EsCollector.java @@ -18,6 +18,7 @@ package org.dromara.dynamictp.example.collector; import org.dromara.dynamictp.common.entity.ThreadPoolStats; +import org.dromara.dynamictp.common.entity.VTExecutorStats; import org.dromara.dynamictp.common.util.JsonUtil; import org.dromara.dynamictp.core.monitor.collector.AbstractCollector; @@ -40,6 +41,11 @@ public void collect(ThreadPoolStats poolStats) { esClient.save(JsonUtil.toJson(poolStats)); } + @Override + public void collect(VTExecutorStats vtExecutorStats) { + esClient.save(JsonUtil.toJson(vtExecutorStats)); + } + @Override public String type() { return "es"; diff --git a/example/example-zookeeper/src/main/java/org/dromara/dynamictp/example/collector/EsCollector.java b/example/example-zookeeper/src/main/java/org/dromara/dynamictp/example/collector/EsCollector.java index 0dd2c4334..fecc9f6a2 100644 --- a/example/example-zookeeper/src/main/java/org/dromara/dynamictp/example/collector/EsCollector.java +++ b/example/example-zookeeper/src/main/java/org/dromara/dynamictp/example/collector/EsCollector.java @@ -18,6 +18,7 @@ package org.dromara.dynamictp.example.collector; import org.dromara.dynamictp.common.entity.ThreadPoolStats; +import org.dromara.dynamictp.common.entity.VTExecutorStats; import org.dromara.dynamictp.common.util.JsonUtil; import org.dromara.dynamictp.core.monitor.collector.AbstractCollector; @@ -40,6 +41,11 @@ public void collect(ThreadPoolStats poolStats) { esClient.save(JsonUtil.toJson(poolStats)); } + @Override + public void collect(VTExecutorStats vtExecutorStats) { + esClient.save(JsonUtil.toJson(vtExecutorStats)); + } + @Override public String type() { return "es"; From 3dc7ee6ebc26b2d40a376902e7468dcffbe4c6e7 Mon Sep 17 00:00:00 2001 From: TheFatRatre <1981590178@qq.com> Date: Wed, 13 Nov 2024 12:08:46 +0800 Subject: [PATCH 17/26] fix conflict and adapt --- .../org/dromara/dynamictp/common/em/JreEnum.java | 6 +++++- .../dynamictp/core/executor/ExecutorType.java | 2 +- .../core/monitor/collector/MicroMeterCollector.java | 12 ++++++++---- .../core/monitor/collector/jmx/JMXCollector.java | 6 +++--- .../dynamictp/core/support/ExecutorWrapper.java | 1 + .../{ => proxy}/VirtualThreadExecutorProxy.java | 2 +- example/example-nacos-cloud/pom.xml | 5 +++++ .../example/service/impl/TestServiceImpl.java | 6 +----- extension/extension-limiter-redis/pom.xml | 5 +++++ .../dromara/dynamictp/spring/DtpPostProcessor.java | 10 +++++----- .../annotation/DtpBeanDefinitionRegistrar.java | 10 +++++----- .../starter-adapter/starter-adapter-common/pom.xml | 5 +++++ starter/starter-common/pom.xml | 5 +++++ .../starter-configcenter/cloud-starter-nacos/pom.xml | 5 +++++ .../autoconfigure/RedisLimiterAutoConfiguration.java | 6 +++--- 15 files changed, 58 insertions(+), 28 deletions(-) rename core/src/main/java/org/dromara/dynamictp/core/support/{ => proxy}/VirtualThreadExecutorProxy.java (99%) diff --git a/common/src/main/java/org/dromara/dynamictp/common/em/JreEnum.java b/common/src/main/java/org/dromara/dynamictp/common/em/JreEnum.java index 859da958e..efa4a5def 100644 --- a/common/src/main/java/org/dromara/dynamictp/common/em/JreEnum.java +++ b/common/src/main/java/org/dromara/dynamictp/common/em/JreEnum.java @@ -53,7 +53,11 @@ public enum JreEnum { JAVA_18, - JAVA_19; + JAVA_19, + + JAVA_20, + + JAVA_21; private static final JreEnum VERSION = getJre(); diff --git a/core/src/main/java/org/dromara/dynamictp/core/executor/ExecutorType.java b/core/src/main/java/org/dromara/dynamictp/core/executor/ExecutorType.java index b59a04518..9e97dfc77 100644 --- a/core/src/main/java/org/dromara/dynamictp/core/executor/ExecutorType.java +++ b/core/src/main/java/org/dromara/dynamictp/core/executor/ExecutorType.java @@ -21,7 +21,7 @@ import org.dromara.dynamictp.core.executor.eager.EagerDtpExecutor; import lombok.Getter; import org.dromara.dynamictp.core.executor.priority.PriorityDtpExecutor; -import org.dromara.dynamictp.core.support.VirtualThreadExecutorProxy; +import org.dromara.dynamictp.core.support.proxy.VirtualThreadExecutorProxy; /** * ExecutorType related diff --git a/core/src/main/java/org/dromara/dynamictp/core/monitor/collector/MicroMeterCollector.java b/core/src/main/java/org/dromara/dynamictp/core/monitor/collector/MicroMeterCollector.java index dc91ea6d1..ae9d97980 100644 --- a/core/src/main/java/org/dromara/dynamictp/core/monitor/collector/MicroMeterCollector.java +++ b/core/src/main/java/org/dromara/dynamictp/core/monitor/collector/MicroMeterCollector.java @@ -23,10 +23,14 @@ import org.dromara.dynamictp.common.em.CollectorTypeEnum; import org.dromara.dynamictp.common.entity.ThreadPoolStats; import org.dromara.dynamictp.common.entity.VTExecutorStats; +import org.dromara.dynamictp.common.util.BeanCopierUtil; import org.dromara.dynamictp.common.util.CommonUtil; -import org.springframework.beans.BeanUtils; -import java.util.*; +import java.util.ArrayList; +import java.util.List; +import java.util.Map; +import java.util.Objects; +import java.util.Optional; import java.util.concurrent.ConcurrentHashMap; /** @@ -64,7 +68,7 @@ public void collect(ThreadPoolStats threadPoolStats) { if (Objects.isNull(oldStats)) { GAUGE_CACHE.put(threadPoolStats.getPoolName(), threadPoolStats); } else { - BeanUtils.copyProperties(threadPoolStats, oldStats); + BeanCopierUtil.copyProperties(threadPoolStats, oldStats); } gauge((ThreadPoolStats) GAUGE_CACHE.get(threadPoolStats.getPoolName())); } @@ -75,7 +79,7 @@ public void collect(VTExecutorStats vtExecutorStats) { if (Objects.isNull(oldStats)) { GAUGE_CACHE.put(vtExecutorStats.getExecutorName(), vtExecutorStats); } else { - BeanUtils.copyProperties(vtExecutorStats, oldStats); + BeanCopierUtil.copyProperties(vtExecutorStats, oldStats); } gauge((VTExecutorStats) GAUGE_CACHE.get(vtExecutorStats.getExecutorName())); } 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 073973c2c..761c5efaa 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 @@ -22,8 +22,8 @@ import org.dromara.dynamictp.common.entity.Metrics; import org.dromara.dynamictp.common.entity.ThreadPoolStats; import org.dromara.dynamictp.common.entity.VTExecutorStats; +import org.dromara.dynamictp.common.util.BeanCopierUtil; import org.dromara.dynamictp.core.monitor.collector.AbstractCollector; -import org.springframework.beans.BeanUtils; import javax.management.JMException; import javax.management.MBeanServer; @@ -51,7 +51,7 @@ public class JMXCollector extends AbstractCollector { public void collect(ThreadPoolStats threadPoolStats) { if (GAUGE_CACHE.containsKey(threadPoolStats.getPoolName())) { ThreadPoolStats poolStats = (ThreadPoolStats) GAUGE_CACHE.get(threadPoolStats.getPoolName()); - BeanUtils.copyProperties(threadPoolStats, poolStats); + BeanCopierUtil.copyProperties(threadPoolStats, poolStats); } else { try { MBeanServer server = ManagementFactory.getPlatformMBeanServer(); @@ -69,7 +69,7 @@ public void collect(ThreadPoolStats threadPoolStats) { public void collect(VTExecutorStats vtExecutorStats) { if (GAUGE_CACHE.containsKey(vtExecutorStats.getExecutorName())) { ThreadPoolStats poolStats = (ThreadPoolStats) GAUGE_CACHE.get(vtExecutorStats.getExecutorName()); - BeanUtils.copyProperties(vtExecutorStats, poolStats); + BeanCopierUtil.copyProperties(vtExecutorStats, poolStats); } else { try { MBeanServer server = ManagementFactory.getPlatformMBeanServer(); diff --git a/core/src/main/java/org/dromara/dynamictp/core/support/ExecutorWrapper.java b/core/src/main/java/org/dromara/dynamictp/core/support/ExecutorWrapper.java index 56cd81b85..ff5cb80c5 100644 --- a/core/src/main/java/org/dromara/dynamictp/core/support/ExecutorWrapper.java +++ b/core/src/main/java/org/dromara/dynamictp/core/support/ExecutorWrapper.java @@ -28,6 +28,7 @@ import org.dromara.dynamictp.core.notifier.capture.CapturedExecutor; import org.dromara.dynamictp.core.notifier.manager.AlarmManager; import org.dromara.dynamictp.core.reject.RejectHandlerGetter; +import org.dromara.dynamictp.core.support.proxy.VirtualThreadExecutorProxy; import org.dromara.dynamictp.core.support.task.wrapper.TaskWrapper; import java.util.HashSet; diff --git a/core/src/main/java/org/dromara/dynamictp/core/support/VirtualThreadExecutorProxy.java b/core/src/main/java/org/dromara/dynamictp/core/support/proxy/VirtualThreadExecutorProxy.java similarity index 99% rename from core/src/main/java/org/dromara/dynamictp/core/support/VirtualThreadExecutorProxy.java rename to core/src/main/java/org/dromara/dynamictp/core/support/proxy/VirtualThreadExecutorProxy.java index 7a77e5e94..2cbfb41c5 100644 --- a/core/src/main/java/org/dromara/dynamictp/core/support/VirtualThreadExecutorProxy.java +++ b/core/src/main/java/org/dromara/dynamictp/core/support/proxy/VirtualThreadExecutorProxy.java @@ -1,4 +1,4 @@ -package org.dromara.dynamictp.core.support; +package org.dromara.dynamictp.core.support.proxy; import com.google.common.collect.Sets; import org.dromara.dynamictp.common.em.NotifyItemEnum; diff --git a/example/example-nacos-cloud/pom.xml b/example/example-nacos-cloud/pom.xml index 22490e3b1..567093974 100644 --- a/example/example-nacos-cloud/pom.xml +++ b/example/example-nacos-cloud/pom.xml @@ -88,5 +88,10 @@ org.springframework.boot spring-boot-starter-data-redis + + org.dromara.dynamictp + dynamic-tp-spring + compile + \ No newline at end of file diff --git a/example/example-nacos-cloud/src/main/java/org/dromara/dynamictp/example/service/impl/TestServiceImpl.java b/example/example-nacos-cloud/src/main/java/org/dromara/dynamictp/example/service/impl/TestServiceImpl.java index c53689098..7140e8f67 100644 --- a/example/example-nacos-cloud/src/main/java/org/dromara/dynamictp/example/service/impl/TestServiceImpl.java +++ b/example/example-nacos-cloud/src/main/java/org/dromara/dynamictp/example/service/impl/TestServiceImpl.java @@ -59,7 +59,7 @@ public TestServiceImpl(ThreadPoolExecutor jucThreadPoolExecutor, DtpExecutor eagerDtpExecutor, ScheduledExecutorService scheduledDtpExecutor, OrderedDtpExecutor orderedDtpExecutor, - @Qualifier("VirtualThreadExecutor1") ExecutorService virtualThreadExecutor) { + @Qualifier("virtualThreadExecutor1") ExecutorService virtualThreadExecutor) { this.jucThreadPoolExecutor = jucThreadPoolExecutor; this.threadPoolTaskExecutor = threadPoolTaskExecutor; this.eagerDtpExecutor = eagerDtpExecutor; @@ -68,10 +68,6 @@ public TestServiceImpl(ThreadPoolExecutor jucThreadPoolExecutor, this.virtualThreadExecutor = virtualThreadExecutor; } -// public TestServiceImpl(@Qualifier("VirtualThreadExecutor1") ExecutorService virtualThreadExecutor) { -// this.VirtualThreadExecutor = virtualThreadExecutor; -// } - @Override public void testJucTp() { for (int i = 0; i < 10; i++) { diff --git a/extension/extension-limiter-redis/pom.xml b/extension/extension-limiter-redis/pom.xml index c4874be75..8e7c31edc 100644 --- a/extension/extension-limiter-redis/pom.xml +++ b/extension/extension-limiter-redis/pom.xml @@ -15,6 +15,11 @@ org.springframework.data spring-data-redis + + javax.annotation + javax.annotation-api + compile + diff --git a/spring/src/main/java/org/dromara/dynamictp/spring/DtpPostProcessor.java b/spring/src/main/java/org/dromara/dynamictp/spring/DtpPostProcessor.java index 5d7c9ca9d..4468aea21 100644 --- a/spring/src/main/java/org/dromara/dynamictp/spring/DtpPostProcessor.java +++ b/spring/src/main/java/org/dromara/dynamictp/spring/DtpPostProcessor.java @@ -15,7 +15,7 @@ * limitations under the License. */ -package org.dromara.dynamictp.core.spring; +package org.dromara.dynamictp.spring; import com.google.common.collect.Lists; import lombok.extern.slf4j.Slf4j; @@ -30,9 +30,9 @@ import org.dromara.dynamictp.core.executor.eager.TaskQueue; import org.dromara.dynamictp.core.support.DynamicTp; import org.dromara.dynamictp.core.support.ExecutorWrapper; -import org.dromara.dynamictp.core.support.ScheduledThreadPoolExecutorProxy; -import org.dromara.dynamictp.core.support.ThreadPoolExecutorProxy; -import org.dromara.dynamictp.core.support.VirtualThreadExecutorProxy; +import org.dromara.dynamictp.core.support.proxy.VirtualThreadExecutorProxy; +import org.dromara.dynamictp.core.support.proxy.ScheduledThreadPoolExecutorProxy; +import org.dromara.dynamictp.core.support.proxy.ThreadPoolExecutorProxy; import org.dromara.dynamictp.core.support.task.wrapper.TaskWrapper; import org.dromara.dynamictp.core.support.task.wrapper.TaskWrappers; import org.springframework.beans.BeansException; @@ -125,7 +125,7 @@ private Object registerAndReturnCommon(Object bean, String beanName) { } else { BeanDefinition beanDefinition = beanFactory.getBeanDefinition(beanName); if (!(beanDefinition instanceof AnnotatedBeanDefinition)) { - if(beanDefinition.getBeanClassName().equals("org.dromara.dynamictp.core.support.VirtualThreadExecutorProxy")) { + if(beanDefinition.getBeanClassName().equals("org.dromara.dynamictp.core.support.proxy.VirtualThreadExecutorProxy")) { return doRegisterAndReturnCommon(bean, beanName); } return bean; diff --git a/spring/src/main/java/org/dromara/dynamictp/spring/annotation/DtpBeanDefinitionRegistrar.java b/spring/src/main/java/org/dromara/dynamictp/spring/annotation/DtpBeanDefinitionRegistrar.java index c7895cf89..19d5489e7 100644 --- a/spring/src/main/java/org/dromara/dynamictp/spring/annotation/DtpBeanDefinitionRegistrar.java +++ b/spring/src/main/java/org/dromara/dynamictp/spring/annotation/DtpBeanDefinitionRegistrar.java @@ -15,7 +15,7 @@ * limitations under the License. */ -package org.dromara.dynamictp.core.spring; +package org.dromara.dynamictp.spring.annotation; import com.google.common.collect.Maps; import lombok.extern.slf4j.Slf4j; @@ -23,16 +23,16 @@ import org.apache.commons.collections4.CollectionUtils; import org.dromara.dynamictp.common.entity.DtpExecutorProps; import org.dromara.dynamictp.common.properties.DtpProperties; -import org.dromara.dynamictp.common.spring.SpringBeanHelper; import org.dromara.dynamictp.core.executor.ExecutorType; import org.dromara.dynamictp.core.executor.NamedThreadFactory; import org.dromara.dynamictp.core.executor.eager.EagerDtpExecutor; import org.dromara.dynamictp.core.executor.eager.TaskQueue; import org.dromara.dynamictp.core.executor.priority.PriorityDtpExecutor; import org.dromara.dynamictp.core.reject.RejectHandlerGetter; -import org.dromara.dynamictp.core.support.BinderHelper; -import org.dromara.dynamictp.core.support.VirtualThreadExecutorProxy; +import org.dromara.dynamictp.core.support.binder.BinderHelper; +import org.dromara.dynamictp.core.support.proxy.VirtualThreadExecutorProxy; import org.dromara.dynamictp.core.support.task.wrapper.TaskWrappers; +import org.dromara.dynamictp.spring.util.BeanRegistrationUtil; import org.springframework.beans.factory.support.BeanDefinitionRegistry; import org.springframework.context.EnvironmentAware; import org.springframework.context.annotation.ImportBeanDefinitionRegistrar; @@ -103,7 +103,7 @@ public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, B propertyValues = buildPropertyValues(e); } Object[] args = buildConstructorArgs(executorTypeClass, e); - SpringBeanHelper.register(registry, e.getThreadPoolName(), executorTypeClass, propertyValues, args); + BeanRegistrationUtil.register(registry, e.getThreadPoolName(), executorTypeClass, propertyValues, args); }); } diff --git a/starter/starter-adapter/starter-adapter-common/pom.xml b/starter/starter-adapter/starter-adapter-common/pom.xml index 9072e5f00..458da4b47 100644 --- a/starter/starter-adapter/starter-adapter-common/pom.xml +++ b/starter/starter-adapter/starter-adapter-common/pom.xml @@ -16,5 +16,10 @@ org.dromara.dynamictp dynamic-tp-adapter-common + + org.dromara.dynamictp + dynamic-tp-spring + compile + diff --git a/starter/starter-common/pom.xml b/starter/starter-common/pom.xml index 33eb477e2..3692d9650 100644 --- a/starter/starter-common/pom.xml +++ b/starter/starter-common/pom.xml @@ -30,5 +30,10 @@ org.dromara.dynamictp dynamic-tp-spring + + org.dromara.dynamictp + dynamic-tp-spring + compile + \ No newline at end of file diff --git a/starter/starter-configcenter/cloud-starter-nacos/pom.xml b/starter/starter-configcenter/cloud-starter-nacos/pom.xml index 035de2112..f12c55525 100644 --- a/starter/starter-configcenter/cloud-starter-nacos/pom.xml +++ b/starter/starter-configcenter/cloud-starter-nacos/pom.xml @@ -16,6 +16,11 @@ spring-cloud-starter-alibaba-nacos-config true + + org.dromara.dynamictp + dynamic-tp-spring + compile + diff --git a/starter/starter-extension/starter-extension-limiter-redis/src/main/java/org/dromara/dynamictp/starter/extension/limiter/redis/autoconfigure/RedisLimiterAutoConfiguration.java b/starter/starter-extension/starter-extension-limiter-redis/src/main/java/org/dromara/dynamictp/starter/extension/limiter/redis/autoconfigure/RedisLimiterAutoConfiguration.java index d050bb25d..976da43a1 100644 --- a/starter/starter-extension/starter-extension-limiter-redis/src/main/java/org/dromara/dynamictp/starter/extension/limiter/redis/autoconfigure/RedisLimiterAutoConfiguration.java +++ b/starter/starter-extension/starter-extension-limiter-redis/src/main/java/org/dromara/dynamictp/starter/extension/limiter/redis/autoconfigure/RedisLimiterAutoConfiguration.java @@ -46,7 +46,7 @@ public RedisRateLimiter> redisScriptRateLimiter(StringRedisTemplate s @Bean @ConditionalOnMissingBean - public NotifyRedisRateLimiterFilter notifyRedisRateLimiterFilter(RedisRateLimiter> redisScriptRateLimiter) { - return new NotifyRedisRateLimiterFilter(redisScriptRateLimiter); + public NotifyRedisRateLimiterFilter notifyRedisRateLimiterFilter() { + return new NotifyRedisRateLimiterFilter(); } -} +} \ No newline at end of file From 4154092ae321719a71aff73a5991109374c52e6d Mon Sep 17 00:00:00 2001 From: TheFatRatre <1981590178@qq.com> Date: Wed, 13 Nov 2024 14:49:49 +0800 Subject: [PATCH 18/26] fix and format --- example/example-nacos-cloud/pom.xml | 5 ----- .../example/config/ThreadPoolConfiguration.java | 6 +++++- .../example/service/impl/TestServiceImpl.java | 6 +++++- extension/extension-limiter-redis/pom.xml | 5 ----- .../starter-adapter-common/pom.xml | 5 ----- .../starter-adapter-webserver/pom.xml | 15 +++++++++++++++ starter/starter-common/pom.xml | 5 ----- .../cloud-starter-nacos/pom.xml | 5 ----- 8 files changed, 25 insertions(+), 27 deletions(-) diff --git a/example/example-nacos-cloud/pom.xml b/example/example-nacos-cloud/pom.xml index 567093974..22490e3b1 100644 --- a/example/example-nacos-cloud/pom.xml +++ b/example/example-nacos-cloud/pom.xml @@ -88,10 +88,5 @@ org.springframework.boot spring-boot-starter-data-redis - - org.dromara.dynamictp - dynamic-tp-spring - compile - \ No newline at end of file diff --git a/example/example-nacos-cloud/src/main/java/org/dromara/dynamictp/example/config/ThreadPoolConfiguration.java b/example/example-nacos-cloud/src/main/java/org/dromara/dynamictp/example/config/ThreadPoolConfiguration.java index 0888af6b9..d1d06d22f 100644 --- a/example/example-nacos-cloud/src/main/java/org/dromara/dynamictp/example/config/ThreadPoolConfiguration.java +++ b/example/example-nacos-cloud/src/main/java/org/dromara/dynamictp/example/config/ThreadPoolConfiguration.java @@ -26,7 +26,11 @@ import org.springframework.context.annotation.Configuration; import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor; -import java.util.concurrent.*; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.ScheduledExecutorService; +import java.util.concurrent.ThreadPoolExecutor; +import java.util.concurrent.TimeUnit; import static org.dromara.dynamictp.common.em.QueueTypeEnum.MEMORY_SAFE_LINKED_BLOCKING_QUEUE; import static org.dromara.dynamictp.common.em.RejectedTypeEnum.CALLER_RUNS_POLICY; diff --git a/example/example-nacos-cloud/src/main/java/org/dromara/dynamictp/example/service/impl/TestServiceImpl.java b/example/example-nacos-cloud/src/main/java/org/dromara/dynamictp/example/service/impl/TestServiceImpl.java index 7140e8f67..4211b020b 100644 --- a/example/example-nacos-cloud/src/main/java/org/dromara/dynamictp/example/service/impl/TestServiceImpl.java +++ b/example/example-nacos-cloud/src/main/java/org/dromara/dynamictp/example/service/impl/TestServiceImpl.java @@ -30,7 +30,11 @@ import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor; import org.springframework.stereotype.Service; -import java.util.concurrent.*; +import java.util.concurrent.Executor; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.ScheduledExecutorService; +import java.util.concurrent.ThreadPoolExecutor; +import java.util.concurrent.TimeUnit; /** * TestServiceImpl related diff --git a/extension/extension-limiter-redis/pom.xml b/extension/extension-limiter-redis/pom.xml index 8e7c31edc..c4874be75 100644 --- a/extension/extension-limiter-redis/pom.xml +++ b/extension/extension-limiter-redis/pom.xml @@ -15,11 +15,6 @@ org.springframework.data spring-data-redis - - javax.annotation - javax.annotation-api - compile - diff --git a/starter/starter-adapter/starter-adapter-common/pom.xml b/starter/starter-adapter/starter-adapter-common/pom.xml index 458da4b47..9072e5f00 100644 --- a/starter/starter-adapter/starter-adapter-common/pom.xml +++ b/starter/starter-adapter/starter-adapter-common/pom.xml @@ -16,10 +16,5 @@ org.dromara.dynamictp dynamic-tp-adapter-common - - org.dromara.dynamictp - dynamic-tp-spring - compile - diff --git a/starter/starter-adapter/starter-adapter-webserver/pom.xml b/starter/starter-adapter/starter-adapter-webserver/pom.xml index 859c6f40a..8daf6f20c 100644 --- a/starter/starter-adapter/starter-adapter-webserver/pom.xml +++ b/starter/starter-adapter/starter-adapter-webserver/pom.xml @@ -34,5 +34,20 @@ spring-boot-starter-jetty true + + org.dromara.dynamictp + dynamic-tp-adapter-common + + + org.dromara.dynamictp + dynamic-tp-spring + compile + + + org.dromara.dynamictp + dynamic-tp-spring + 1.1.9-3.x + compile + \ No newline at end of file diff --git a/starter/starter-common/pom.xml b/starter/starter-common/pom.xml index 3692d9650..33eb477e2 100644 --- a/starter/starter-common/pom.xml +++ b/starter/starter-common/pom.xml @@ -30,10 +30,5 @@ org.dromara.dynamictp dynamic-tp-spring - - org.dromara.dynamictp - dynamic-tp-spring - compile - \ No newline at end of file diff --git a/starter/starter-configcenter/cloud-starter-nacos/pom.xml b/starter/starter-configcenter/cloud-starter-nacos/pom.xml index f12c55525..035de2112 100644 --- a/starter/starter-configcenter/cloud-starter-nacos/pom.xml +++ b/starter/starter-configcenter/cloud-starter-nacos/pom.xml @@ -16,11 +16,6 @@ spring-cloud-starter-alibaba-nacos-config true - - org.dromara.dynamictp - dynamic-tp-spring - compile - From d9b5d4d1dfb070c6f6726d9666dd61e9ab3dc5a7 Mon Sep 17 00:00:00 2001 From: TheFatRatre <1981590178@qq.com> Date: Wed, 13 Nov 2024 15:29:57 +0800 Subject: [PATCH 19/26] fix dependency --- pom.xml | 2 +- .../starter-adapter-webserver/pom.xml | 15 --------------- 2 files changed, 1 insertion(+), 16 deletions(-) diff --git a/pom.xml b/pom.xml index 02bb1181b..4aa373a37 100644 --- a/pom.xml +++ b/pom.xml @@ -14,7 +14,7 @@ - 1.1.9-3.x + 1.1.9.1-3.x 21 21 diff --git a/starter/starter-adapter/starter-adapter-webserver/pom.xml b/starter/starter-adapter/starter-adapter-webserver/pom.xml index 8daf6f20c..859c6f40a 100644 --- a/starter/starter-adapter/starter-adapter-webserver/pom.xml +++ b/starter/starter-adapter/starter-adapter-webserver/pom.xml @@ -34,20 +34,5 @@ spring-boot-starter-jetty true - - org.dromara.dynamictp - dynamic-tp-adapter-common - - - org.dromara.dynamictp - dynamic-tp-spring - compile - - - org.dromara.dynamictp - dynamic-tp-spring - 1.1.9-3.x - compile - \ No newline at end of file From 119d2e2720439b684af4ac12c3a7fee38c90f6a2 Mon Sep 17 00:00:00 2001 From: TheFatRatre <1981590178@qq.com> Date: Wed, 13 Nov 2024 15:41:44 +0800 Subject: [PATCH 20/26] update mvn jdk --- dependencies/pom.xml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/dependencies/pom.xml b/dependencies/pom.xml index af9e426e4..a93e668d3 100644 --- a/dependencies/pom.xml +++ b/dependencies/pom.xml @@ -15,8 +15,8 @@ 1.1.9.1-3.x UTF-8 - 17 - 17 + 21 + 21 1.18.24 1.7.36 From 7944b9ba03dd73c121fb7ffbe57df3908d9498b9 Mon Sep 17 00:00:00 2001 From: TheFatRatre <1981590178@qq.com> Date: Wed, 13 Nov 2024 15:48:51 +0800 Subject: [PATCH 21/26] try fix gitgub err --- pom.xml | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/pom.xml b/pom.xml index 4aa373a37..f31e2585c 100644 --- a/pom.xml +++ b/pom.xml @@ -207,6 +207,10 @@ org.apache.maven.plugins maven-compiler-plugin ${maven-compiler-plugin.version} + + ${maven.compiler.source} + ${maven.compiler.target} + true From 33a4214602e431a2edec4d15fa6e61db26eaf70e Mon Sep 17 00:00:00 2001 From: TheFatRatre <1981590178@qq.com> Date: Wed, 13 Nov 2024 15:51:21 +0800 Subject: [PATCH 22/26] del --- pom.xml | 4 ---- 1 file changed, 4 deletions(-) diff --git a/pom.xml b/pom.xml index f31e2585c..4aa373a37 100644 --- a/pom.xml +++ b/pom.xml @@ -207,10 +207,6 @@ org.apache.maven.plugins maven-compiler-plugin ${maven-compiler-plugin.version} - - ${maven.compiler.source} - ${maven.compiler.target} - true From fe40c63dd9478ab70905edfc34333e3eac32f912 Mon Sep 17 00:00:00 2001 From: TheFatRatre <1981590178@qq.com> Date: Wed, 13 Nov 2024 22:22:26 +0800 Subject: [PATCH 23/26] format --- .../dromara/dynamictp/core/DtpRegistry.java | 4 +- .../dynamictp/core/monitor/DtpMonitor.java | 4 +- .../collector/jmx/VTExecutorStatsJMX.java | 20 +++++++- .../collector/jmx/VTExecutorStatsMXBean.java | 17 +++++++ .../support/VirtualThreadExecutorAdapter.java | 16 ++++++ .../proxy/VirtualThreadExecutorProxy.java | 50 +++++++++++++------ .../example/service/impl/TestServiceImpl.java | 2 - .../dynamictp/spring/DtpPostProcessor.java | 4 +- .../DtpBeanDefinitionRegistrar.java | 25 ++-------- .../starter/common/monitor/DtpEndpoint.java | 6 +-- .../RedisLimiterAutoConfiguration.java | 2 +- ...InstrumentedQueuedThreadPoolProxyTest.java | 2 +- 12 files changed, 100 insertions(+), 52 deletions(-) diff --git a/core/src/main/java/org/dromara/dynamictp/core/DtpRegistry.java b/core/src/main/java/org/dromara/dynamictp/core/DtpRegistry.java index f28a97a7b..b49a183b1 100644 --- a/core/src/main/java/org/dromara/dynamictp/core/DtpRegistry.java +++ b/core/src/main/java/org/dromara/dynamictp/core/DtpRegistry.java @@ -227,7 +227,7 @@ private static void doRefresh(ExecutorWrapper executorWrapper, DtpExecutorProps if (!Objects.equals(executor.allowsCoreThreadTimeOut(), props.isAllowCoreThreadTimeOut())) { executor.allowCoreThreadTimeOut(props.isAllowCoreThreadTimeOut()); } - if(!executorWrapper.isVirtualThreadExecutor()) { + if (!executorWrapper.isVirtualThreadExecutor()) { // update queue updateQueueProps(executor, props); } @@ -244,7 +244,7 @@ private static void doRefreshCommon(ExecutorWrapper executorWrapper, DtpExecutor if (StringUtils.isNotBlank(props.getThreadPoolAliasName())) { executorWrapper.setThreadPoolAliasName(props.getThreadPoolAliasName()); } - if(!executorWrapper.isVirtualThreadExecutor()) { + if (!executorWrapper.isVirtualThreadExecutor()) { ExecutorAdapter executor = executorWrapper.getExecutor(); // update reject handler String currentRejectHandlerType = executor.getRejectHandlerType(); diff --git a/core/src/main/java/org/dromara/dynamictp/core/monitor/DtpMonitor.java b/core/src/main/java/org/dromara/dynamictp/core/monitor/DtpMonitor.java index 0e022d2cf..a55d7430c 100644 --- a/core/src/main/java/org/dromara/dynamictp/core/monitor/DtpMonitor.java +++ b/core/src/main/java/org/dromara/dynamictp/core/monitor/DtpMonitor.java @@ -25,9 +25,7 @@ import org.dromara.dynamictp.common.event.CollectEvent; import org.dromara.dynamictp.common.event.CustomContextRefreshedEvent; import org.dromara.dynamictp.common.manager.EventBusManager; - import org.dromara.dynamictp.common.properties.DtpProperties; - import org.dromara.dynamictp.core.DtpRegistry; import org.dromara.dynamictp.core.converter.ExecutorConverter; import org.dromara.dynamictp.core.handler.CollectorHandler; @@ -99,7 +97,7 @@ private void collectMetrics(Set executorNames) { } executorNames.forEach(x -> { ExecutorWrapper wrapper = DtpRegistry.getExecutorWrapper(x); - if(wrapper.isVirtualThreadExecutor()) { + if (wrapper.isVirtualThreadExecutor()) { doCollectVTExecutor(ExecutorConverter.toVTExecutorMetrics(wrapper)); } else { doCollect(ExecutorConverter.toMetrics(wrapper)); diff --git a/core/src/main/java/org/dromara/dynamictp/core/monitor/collector/jmx/VTExecutorStatsJMX.java b/core/src/main/java/org/dromara/dynamictp/core/monitor/collector/jmx/VTExecutorStatsJMX.java index d49059078..47283608e 100644 --- a/core/src/main/java/org/dromara/dynamictp/core/monitor/collector/jmx/VTExecutorStatsJMX.java +++ b/core/src/main/java/org/dromara/dynamictp/core/monitor/collector/jmx/VTExecutorStatsJMX.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.core.monitor.collector.jmx; import org.dromara.dynamictp.common.entity.VTExecutorStats; @@ -10,9 +27,10 @@ * @author CYC * @create 2024/11/5 18:29 */ -public class VTExecutorStatsJMX implements VTExecutorStatsMXBean{ +public class VTExecutorStatsJMX implements VTExecutorStatsMXBean { private VTExecutorStats vtExecutorStats; + public VTExecutorStatsJMX(VTExecutorStats vtExecutorStats) { this.vtExecutorStats = vtExecutorStats; } diff --git a/core/src/main/java/org/dromara/dynamictp/core/monitor/collector/jmx/VTExecutorStatsMXBean.java b/core/src/main/java/org/dromara/dynamictp/core/monitor/collector/jmx/VTExecutorStatsMXBean.java index d10fb2bda..7dca8b76c 100644 --- a/core/src/main/java/org/dromara/dynamictp/core/monitor/collector/jmx/VTExecutorStatsMXBean.java +++ b/core/src/main/java/org/dromara/dynamictp/core/monitor/collector/jmx/VTExecutorStatsMXBean.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.core.monitor.collector.jmx; import org.dromara.dynamictp.common.entity.VTExecutorStats; diff --git a/core/src/main/java/org/dromara/dynamictp/core/support/VirtualThreadExecutorAdapter.java b/core/src/main/java/org/dromara/dynamictp/core/support/VirtualThreadExecutorAdapter.java index b5e484201..ac6a9de96 100644 --- a/core/src/main/java/org/dromara/dynamictp/core/support/VirtualThreadExecutorAdapter.java +++ b/core/src/main/java/org/dromara/dynamictp/core/support/VirtualThreadExecutorAdapter.java @@ -1,3 +1,19 @@ +/* + * 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.support; import java.util.concurrent.Executor; diff --git a/core/src/main/java/org/dromara/dynamictp/core/support/proxy/VirtualThreadExecutorProxy.java b/core/src/main/java/org/dromara/dynamictp/core/support/proxy/VirtualThreadExecutorProxy.java index 2cbfb41c5..434d209a3 100644 --- a/core/src/main/java/org/dromara/dynamictp/core/support/proxy/VirtualThreadExecutorProxy.java +++ b/core/src/main/java/org/dromara/dynamictp/core/support/proxy/VirtualThreadExecutorProxy.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.core.support.proxy; import com.google.common.collect.Sets; @@ -31,7 +48,7 @@ */ public class VirtualThreadExecutorProxy implements TaskEnhanceAware, ExecutorService { - private final ExecutorService THREAD_PER_TASK_EXECUTOR; + private final ExecutorService threadPerTaskExecutor; /** * Notify platform ids. @@ -76,7 +93,7 @@ public class VirtualThreadExecutorProxy implements TaskEnhanceAware, ExecutorSer public VirtualThreadExecutorProxy(ExecutorService executor) { super(); - THREAD_PER_TASK_EXECUTOR = executor; + threadPerTaskExecutor = executor; } @Override @@ -84,11 +101,11 @@ public void execute(Runnable command) { command = getEnhancedTask(command); EnhancedRunnable.of(command, this); AwareManager.execute(this, command); - THREAD_PER_TASK_EXECUTOR.execute(command); + threadPerTaskExecutor.execute(command); } public ExecutorService getThreadPerTaskExecutor() { - return THREAD_PER_TASK_EXECUTOR; + return threadPerTaskExecutor; } @Override @@ -104,27 +121,27 @@ public void setTaskWrappers(List taskWrappers) { @Override public void shutdown() { - THREAD_PER_TASK_EXECUTOR.shutdown(); + threadPerTaskExecutor.shutdown(); } @Override public List shutdownNow() { - return THREAD_PER_TASK_EXECUTOR.shutdownNow(); + return threadPerTaskExecutor.shutdownNow(); } @Override public boolean isShutdown() { - return THREAD_PER_TASK_EXECUTOR.isShutdown(); + return threadPerTaskExecutor.isShutdown(); } @Override public boolean isTerminated() { - return THREAD_PER_TASK_EXECUTOR.isTerminated(); + return threadPerTaskExecutor.isTerminated(); } @Override public boolean awaitTermination(long l, TimeUnit timeUnit) throws InterruptedException { - return THREAD_PER_TASK_EXECUTOR.awaitTermination(l, timeUnit); + return threadPerTaskExecutor.awaitTermination(l, timeUnit); } @Override @@ -133,7 +150,7 @@ public Future submit(Callable callable) { futureTask = (FutureTask) getEnhancedTask(futureTask); EnhancedRunnable.of(futureTask, this); AwareManager.execute(this, futureTask); - return THREAD_PER_TASK_EXECUTOR.submit(callable); + return threadPerTaskExecutor.submit(callable); } @@ -142,7 +159,7 @@ public Future submit(Runnable runnable, T t) { runnable = getEnhancedTask(runnable); EnhancedRunnable.of(runnable, this); AwareManager.execute(this, runnable); - return THREAD_PER_TASK_EXECUTOR.submit(runnable, t); + return threadPerTaskExecutor.submit(runnable, t); } @Override @@ -150,27 +167,27 @@ public Future submit(Runnable runnable) { runnable = getEnhancedTask(runnable); EnhancedRunnable.of(runnable, this); AwareManager.execute(this, runnable); - return THREAD_PER_TASK_EXECUTOR.submit(runnable); + return threadPerTaskExecutor.submit(runnable); } @Override public List> invokeAll(Collection> collection) throws InterruptedException { - return THREAD_PER_TASK_EXECUTOR.invokeAll(collection); + return threadPerTaskExecutor.invokeAll(collection); } @Override public List> invokeAll(Collection> collection, long l, TimeUnit timeUnit) throws InterruptedException { - return THREAD_PER_TASK_EXECUTOR.invokeAll(collection, l, timeUnit); + return threadPerTaskExecutor.invokeAll(collection, l, timeUnit); } @Override public T invokeAny(Collection> collection) throws InterruptedException, ExecutionException { - return THREAD_PER_TASK_EXECUTOR.invokeAny(collection); + return threadPerTaskExecutor.invokeAny(collection); } @Override public T invokeAny(Collection> collection, long l, TimeUnit timeUnit) throws InterruptedException, ExecutionException, TimeoutException { - return THREAD_PER_TASK_EXECUTOR.invokeAny(collection, l, timeUnit); + return threadPerTaskExecutor.invokeAny(collection, l, timeUnit); } public String getThreadPoolName() { @@ -196,6 +213,7 @@ public boolean isNotifyEnabled() { public void setNotifyEnabled(boolean notifyEnabled) { this.notifyEnabled = notifyEnabled; } + public Set getPluginNames() { return pluginNames; } diff --git a/example/example-nacos-cloud/src/main/java/org/dromara/dynamictp/example/service/impl/TestServiceImpl.java b/example/example-nacos-cloud/src/main/java/org/dromara/dynamictp/example/service/impl/TestServiceImpl.java index 4211b020b..650849e5e 100644 --- a/example/example-nacos-cloud/src/main/java/org/dromara/dynamictp/example/service/impl/TestServiceImpl.java +++ b/example/example-nacos-cloud/src/main/java/org/dromara/dynamictp/example/service/impl/TestServiceImpl.java @@ -159,8 +159,6 @@ public void testVTExecutor() { } catch (InterruptedException e) { throw new RuntimeException(e); } - System.out.println("i am a VTExecutor's task"); - }); } } diff --git a/spring/src/main/java/org/dromara/dynamictp/spring/DtpPostProcessor.java b/spring/src/main/java/org/dromara/dynamictp/spring/DtpPostProcessor.java index 4468aea21..fa97b387c 100644 --- a/spring/src/main/java/org/dromara/dynamictp/spring/DtpPostProcessor.java +++ b/spring/src/main/java/org/dromara/dynamictp/spring/DtpPostProcessor.java @@ -30,9 +30,9 @@ import org.dromara.dynamictp.core.executor.eager.TaskQueue; import org.dromara.dynamictp.core.support.DynamicTp; import org.dromara.dynamictp.core.support.ExecutorWrapper; -import org.dromara.dynamictp.core.support.proxy.VirtualThreadExecutorProxy; import org.dromara.dynamictp.core.support.proxy.ScheduledThreadPoolExecutorProxy; import org.dromara.dynamictp.core.support.proxy.ThreadPoolExecutorProxy; +import org.dromara.dynamictp.core.support.proxy.VirtualThreadExecutorProxy; import org.dromara.dynamictp.core.support.task.wrapper.TaskWrapper; import org.dromara.dynamictp.core.support.task.wrapper.TaskWrappers; import org.springframework.beans.BeansException; @@ -125,7 +125,7 @@ private Object registerAndReturnCommon(Object bean, String beanName) { } else { BeanDefinition beanDefinition = beanFactory.getBeanDefinition(beanName); if (!(beanDefinition instanceof AnnotatedBeanDefinition)) { - if(beanDefinition.getBeanClassName().equals("org.dromara.dynamictp.core.support.proxy.VirtualThreadExecutorProxy")) { + if (beanDefinition.getBeanClassName().equals("org.dromara.dynamictp.core.support.proxy.VirtualThreadExecutorProxy")) { return doRegisterAndReturnCommon(bean, beanName); } return bean; diff --git a/spring/src/main/java/org/dromara/dynamictp/spring/annotation/DtpBeanDefinitionRegistrar.java b/spring/src/main/java/org/dromara/dynamictp/spring/annotation/DtpBeanDefinitionRegistrar.java index 19d5489e7..65331f160 100644 --- a/spring/src/main/java/org/dromara/dynamictp/spring/annotation/DtpBeanDefinitionRegistrar.java +++ b/spring/src/main/java/org/dromara/dynamictp/spring/annotation/DtpBeanDefinitionRegistrar.java @@ -44,23 +44,7 @@ import java.util.concurrent.Executors; import java.util.concurrent.PriorityBlockingQueue; -import static org.dromara.dynamictp.common.constant.DynamicTpConst.ALLOW_CORE_THREAD_TIMEOUT; -import static org.dromara.dynamictp.common.constant.DynamicTpConst.AWAIT_TERMINATION_SECONDS; -import static org.dromara.dynamictp.common.constant.DynamicTpConst.AWARE_NAMES; -import static org.dromara.dynamictp.common.constant.DynamicTpConst.NOTIFY_ENABLED; -import static org.dromara.dynamictp.common.constant.DynamicTpConst.NOTIFY_ITEMS; -import static org.dromara.dynamictp.common.constant.DynamicTpConst.PLATFORM_IDS; -import static org.dromara.dynamictp.common.constant.DynamicTpConst.PLUGIN_NAMES; -import static org.dromara.dynamictp.common.constant.DynamicTpConst.PRE_START_ALL_CORE_THREADS; -import static org.dromara.dynamictp.common.constant.DynamicTpConst.QUEUE_TIMEOUT; -import static org.dromara.dynamictp.common.constant.DynamicTpConst.REJECT_ENHANCED; -import static org.dromara.dynamictp.common.constant.DynamicTpConst.REJECT_HANDLER_TYPE; -import static org.dromara.dynamictp.common.constant.DynamicTpConst.RUN_TIMEOUT; -import static org.dromara.dynamictp.common.constant.DynamicTpConst.TASK_WRAPPERS; -import static org.dromara.dynamictp.common.constant.DynamicTpConst.THREAD_POOL_ALIAS_NAME; -import static org.dromara.dynamictp.common.constant.DynamicTpConst.THREAD_POOL_NAME; -import static org.dromara.dynamictp.common.constant.DynamicTpConst.TRY_INTERRUPT_WHEN_TIMEOUT; -import static org.dromara.dynamictp.common.constant.DynamicTpConst.WAIT_FOR_TASKS_TO_COMPLETE_ON_SHUTDOWN; +import static org.dromara.dynamictp.common.constant.DynamicTpConst.*; import static org.dromara.dynamictp.common.em.QueueTypeEnum.buildLbq; import static org.dromara.dynamictp.common.entity.NotifyItem.mergeAllNotifyItems; @@ -96,10 +80,9 @@ public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, B } Class executorTypeClass = ExecutorType.getClass(e.getExecutorType()); Map propertyValues; - if(executorTypeClass.equals(VirtualThreadExecutorProxy.class)) { + if (executorTypeClass.equals(VirtualThreadExecutorProxy.class)) { propertyValues = buildVTPropertyValues(e); - } - else { + } else { propertyValues = buildPropertyValues(e); } Object[] args = buildConstructorArgs(executorTypeClass, e); @@ -155,7 +138,7 @@ private Object[] buildConstructorArgs(Class clazz, DtpExecutorProps props) { } else if (clazz.equals(PriorityDtpExecutor.class)) { taskQueue = new PriorityBlockingQueue<>(props.getQueueCapacity(), PriorityDtpExecutor.getRunnableComparator()); } else if (clazz.equals(VirtualThreadExecutorProxy.class)) { - return new Object[] { + return new Object[]{ Executors.newVirtualThreadPerTaskExecutor() }; } else { diff --git a/starter/starter-common/src/main/java/org/dromara/dynamictp/starter/common/monitor/DtpEndpoint.java b/starter/starter-common/src/main/java/org/dromara/dynamictp/starter/common/monitor/DtpEndpoint.java index 9f36d3422..df9d5d458 100644 --- a/starter/starter-common/src/main/java/org/dromara/dynamictp/starter/common/monitor/DtpEndpoint.java +++ b/starter/starter-common/src/main/java/org/dromara/dynamictp/starter/common/monitor/DtpEndpoint.java @@ -23,11 +23,11 @@ import org.apache.commons.collections4.MapUtils; import org.dromara.dynamictp.common.entity.JvmStats; import org.dromara.dynamictp.common.entity.Metrics; +import org.dromara.dynamictp.common.manager.ContextManagerHelper; import org.dromara.dynamictp.core.DtpRegistry; +import org.dromara.dynamictp.core.aware.MetricsAware; import org.dromara.dynamictp.core.converter.ExecutorConverter; -import org.dromara.dynamictp.common.manager.ContextManagerHelper; import org.dromara.dynamictp.core.support.ExecutorWrapper; -import org.dromara.dynamictp.core.aware.MetricsAware; import org.springframework.boot.actuate.endpoint.annotation.Endpoint; import org.springframework.boot.actuate.endpoint.annotation.ReadOperation; @@ -48,7 +48,7 @@ public List invoke() { List metricsList = Lists.newArrayList(); DtpRegistry.getAllExecutorNames().forEach(x -> { ExecutorWrapper wrapper = DtpRegistry.getExecutorWrapper(x); - if(wrapper.isVirtualThreadExecutor()) { + if (wrapper.isVirtualThreadExecutor()) { metricsList.add(ExecutorConverter.toVTExecutorMetrics(wrapper)); } else { metricsList.add(ExecutorConverter.toMetrics(wrapper)); diff --git a/starter/starter-extension/starter-extension-limiter-redis/src/main/java/org/dromara/dynamictp/starter/extension/limiter/redis/autoconfigure/RedisLimiterAutoConfiguration.java b/starter/starter-extension/starter-extension-limiter-redis/src/main/java/org/dromara/dynamictp/starter/extension/limiter/redis/autoconfigure/RedisLimiterAutoConfiguration.java index c13804987..d050bb25d 100644 --- a/starter/starter-extension/starter-extension-limiter-redis/src/main/java/org/dromara/dynamictp/starter/extension/limiter/redis/autoconfigure/RedisLimiterAutoConfiguration.java +++ b/starter/starter-extension/starter-extension-limiter-redis/src/main/java/org/dromara/dynamictp/starter/extension/limiter/redis/autoconfigure/RedisLimiterAutoConfiguration.java @@ -49,4 +49,4 @@ public RedisRateLimiter> redisScriptRateLimiter(StringRedisTemplate s public NotifyRedisRateLimiterFilter notifyRedisRateLimiterFilter(RedisRateLimiter> redisScriptRateLimiter) { return new NotifyRedisRateLimiterFilter(redisScriptRateLimiter); } -} \ No newline at end of file +} diff --git a/test/test-adapter/src/test/java/org/dromara/dynamictp/test/adapter/webserver/jetty/InstrumentedQueuedThreadPoolProxyTest.java b/test/test-adapter/src/test/java/org/dromara/dynamictp/test/adapter/webserver/jetty/InstrumentedQueuedThreadPoolProxyTest.java index 15352d778..cfef4ae10 100644 --- a/test/test-adapter/src/test/java/org/dromara/dynamictp/test/adapter/webserver/jetty/InstrumentedQueuedThreadPoolProxyTest.java +++ b/test/test-adapter/src/test/java/org/dromara/dynamictp/test/adapter/webserver/jetty/InstrumentedQueuedThreadPoolProxyTest.java @@ -39,7 +39,7 @@ public void testParam() { Iterable tags = new ArrayList<>(); InstrumentedQueuedThreadPool executor = new InstrumentedQueuedThreadPool(meterRegistry, tags); BlockingQueue queue = (BlockingQueue) ReflectionUtil.getFieldValue("_jobs", executor); - InstrumentedQueuedThreadPoolProxy proxy = new InstrumentedQueuedThreadPoolProxy(executor,meterRegistry,tags, queue); + InstrumentedQueuedThreadPoolProxy proxy = new InstrumentedQueuedThreadPoolProxy(executor, meterRegistry, tags, queue); Assert.assertEquals(executor.getMaxThreads(), proxy.getMaxThreads()); Assert.assertEquals(executor.getIdleTimeout(), proxy.getIdleTimeout()); From f44986386352d19a78e30b28d653ada431b3feeb Mon Sep 17 00:00:00 2001 From: TheFatRatre <1981590178@qq.com> Date: Fri, 15 Nov 2024 18:32:03 +0800 Subject: [PATCH 24/26] core rebuild and JDK version check --- .../core/converter/ExecutorConverter.java | 37 ++-------- .../dynamictp/core/monitor/DtpMonitor.java | 6 +- .../config/ThreadPoolConfiguration.java | 2 +- .../example/service/impl/TestServiceImpl.java | 2 +- .../dynamictp/spring/DtpPostProcessor.java | 10 +-- .../DtpBeanDefinitionRegistrar.java | 74 +++++++++++-------- .../starter/common/monitor/DtpEndpoint.java | 6 +- 7 files changed, 59 insertions(+), 78 deletions(-) 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 529fc8c01..db7f0eb46 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 @@ -65,11 +65,14 @@ public static ThreadPoolStats toMetrics(ExecutorWrapper wrapper) { ThreadPoolStats poolStats = convertCommon(executor); poolStats.setPoolName(wrapper.getThreadPoolName()); poolStats.setPoolAliasName(wrapper.getThreadPoolAliasName()); - poolStats.setRunTimeoutCount(provider.getRunTimeoutCount()); - poolStats.setQueueTimeoutCount(provider.getQueueTimeoutCount()); - poolStats.setRejectCount(provider.getRejectedTaskCount()); - poolStats.setDynamic(executor instanceof DtpExecutor); + if (!wrapper.isVirtualThreadExecutor()) { + poolStats.setRunTimeoutCount(provider.getRunTimeoutCount()); + poolStats.setQueueTimeoutCount(provider.getQueueTimeoutCount()); + poolStats.setRejectCount(provider.getRejectedTaskCount()); + } + + poolStats.setDynamic(executor instanceof DtpExecutor); poolStats.setTps(performanceSnapshot.getTps()); poolStats.setAvg(performanceSnapshot.getAvg()); poolStats.setMaxRt(performanceSnapshot.getMaxRt()); @@ -83,32 +86,6 @@ public static ThreadPoolStats toMetrics(ExecutorWrapper wrapper) { return poolStats; } - public static VTExecutorStats toVTExecutorMetrics(ExecutorWrapper wrapper) { - ExecutorAdapter executor = wrapper.getExecutor(); - if (executor == null) { - return null; - } - ThreadPoolStatProvider provider = wrapper.getThreadPoolStatProvider(); - PerformanceProvider performanceProvider = provider.getPerformanceProvider(); - val performanceSnapshot = performanceProvider.getSnapshotAndReset(); - VTExecutorStats executorStats = convertCommonVT(executor); - executorStats.setExecutorName(wrapper.getThreadPoolName()); - executorStats.setExecutorAliasName(wrapper.getThreadPoolAliasName()); - executorStats.setDynamic(executor instanceof DtpExecutor); - - executorStats.setTps(performanceSnapshot.getTps()); - executorStats.setAvg(performanceSnapshot.getAvg()); - executorStats.setMaxRt(performanceSnapshot.getMaxRt()); - executorStats.setMinRt(performanceSnapshot.getMinRt()); - executorStats.setTp50(performanceSnapshot.getTp50()); - executorStats.setTp75(performanceSnapshot.getTp75()); - executorStats.setTp90(performanceSnapshot.getTp90()); - executorStats.setTp95(performanceSnapshot.getTp95()); - executorStats.setTp99(performanceSnapshot.getTp99()); - executorStats.setTp999(performanceSnapshot.getTp999()); - return executorStats; - } - private static ThreadPoolStats convertCommon(ExecutorAdapter executor) { ThreadPoolStats poolStats = new ThreadPoolStats(); poolStats.setCorePoolSize(executor.getCorePoolSize()); diff --git a/core/src/main/java/org/dromara/dynamictp/core/monitor/DtpMonitor.java b/core/src/main/java/org/dromara/dynamictp/core/monitor/DtpMonitor.java index a55d7430c..b0fa3fe75 100644 --- a/core/src/main/java/org/dromara/dynamictp/core/monitor/DtpMonitor.java +++ b/core/src/main/java/org/dromara/dynamictp/core/monitor/DtpMonitor.java @@ -97,11 +97,7 @@ private void collectMetrics(Set executorNames) { } executorNames.forEach(x -> { ExecutorWrapper wrapper = DtpRegistry.getExecutorWrapper(x); - if (wrapper.isVirtualThreadExecutor()) { - doCollectVTExecutor(ExecutorConverter.toVTExecutorMetrics(wrapper)); - } else { - doCollect(ExecutorConverter.toMetrics(wrapper)); - } + doCollect(ExecutorConverter.toMetrics(wrapper)); }); publishCollectEvent(); } diff --git a/example/example-nacos-cloud/src/main/java/org/dromara/dynamictp/example/config/ThreadPoolConfiguration.java b/example/example-nacos-cloud/src/main/java/org/dromara/dynamictp/example/config/ThreadPoolConfiguration.java index d1d06d22f..17a9c5220 100644 --- a/example/example-nacos-cloud/src/main/java/org/dromara/dynamictp/example/config/ThreadPoolConfiguration.java +++ b/example/example-nacos-cloud/src/main/java/org/dromara/dynamictp/example/config/ThreadPoolConfiguration.java @@ -68,7 +68,7 @@ public ThreadPoolTaskExecutor threadPoolTaskExecutor() { * * @return 虚拟线程执行器实例 */ - @DynamicTp("VirtualThreadExecutor") + @DynamicTp("virtualThreadExecutor") @Bean public ExecutorService virtualThreadExecutor() { return Executors.newVirtualThreadPerTaskExecutor(); diff --git a/example/example-nacos-cloud/src/main/java/org/dromara/dynamictp/example/service/impl/TestServiceImpl.java b/example/example-nacos-cloud/src/main/java/org/dromara/dynamictp/example/service/impl/TestServiceImpl.java index 650849e5e..1c7b49f2a 100644 --- a/example/example-nacos-cloud/src/main/java/org/dromara/dynamictp/example/service/impl/TestServiceImpl.java +++ b/example/example-nacos-cloud/src/main/java/org/dromara/dynamictp/example/service/impl/TestServiceImpl.java @@ -63,7 +63,7 @@ public TestServiceImpl(ThreadPoolExecutor jucThreadPoolExecutor, DtpExecutor eagerDtpExecutor, ScheduledExecutorService scheduledDtpExecutor, OrderedDtpExecutor orderedDtpExecutor, - @Qualifier("virtualThreadExecutor1") ExecutorService virtualThreadExecutor) { + @Qualifier("virtualThreadExecutor") ExecutorService virtualThreadExecutor) { this.jucThreadPoolExecutor = jucThreadPoolExecutor; this.threadPoolTaskExecutor = threadPoolTaskExecutor; this.eagerDtpExecutor = eagerDtpExecutor; diff --git a/spring/src/main/java/org/dromara/dynamictp/spring/DtpPostProcessor.java b/spring/src/main/java/org/dromara/dynamictp/spring/DtpPostProcessor.java index fa97b387c..e3e4c4f62 100644 --- a/spring/src/main/java/org/dromara/dynamictp/spring/DtpPostProcessor.java +++ b/spring/src/main/java/org/dromara/dynamictp/spring/DtpPostProcessor.java @@ -78,7 +78,7 @@ public class DtpPostProcessor implements BeanPostProcessor, BeanFactoryAware, Pr /** * Compatible with lower versions of Spring. * - * @param bean the new bean instance + * @param bean the new bean instance * @param beanName the name of the bean * @return the bean instance to use * @throws BeansException in case of errors @@ -163,9 +163,10 @@ private Object doRegisterAndReturnCommon(Object bean, String poolName) { Executor proxy; if (bean instanceof ScheduledThreadPoolExecutor) { proxy = newScheduledTpProxy(poolName, (ScheduledThreadPoolExecutor) bean); - } else if (bean.getClass().getName().equals("java.util.concurrent.ThreadPerTaskExecutor") - || bean instanceof VirtualThreadExecutorProxy) { + } else if (bean.getClass().getName().equals("java.util.concurrent.ThreadPerTaskExecutor")) { proxy = newVirtualThreadProxy(poolName, (ExecutorService) bean); + } else if (bean instanceof VirtualThreadExecutorProxy) { + proxy = (Executor) bean; } else { proxy = newProxy(poolName, (ThreadPoolExecutor) bean); } @@ -196,8 +197,7 @@ private ScheduledThreadPoolExecutorProxy newScheduledTpProxy(String name, Schedu } private VirtualThreadExecutorProxy newVirtualThreadProxy(String name, ExecutorService originExecutor) { - val proxy = new VirtualThreadExecutorProxy(originExecutor); - return proxy; + return new VirtualThreadExecutorProxy(originExecutor); } private void tryWrapTaskDecorator(String poolName, ThreadPoolTaskExecutor poolTaskExecutor, ThreadPoolExecutorProxy proxy) throws IllegalAccessException { diff --git a/spring/src/main/java/org/dromara/dynamictp/spring/annotation/DtpBeanDefinitionRegistrar.java b/spring/src/main/java/org/dromara/dynamictp/spring/annotation/DtpBeanDefinitionRegistrar.java index 65331f160..25fe4f69d 100644 --- a/spring/src/main/java/org/dromara/dynamictp/spring/annotation/DtpBeanDefinitionRegistrar.java +++ b/spring/src/main/java/org/dromara/dynamictp/spring/annotation/DtpBeanDefinitionRegistrar.java @@ -23,6 +23,7 @@ import org.apache.commons.collections4.CollectionUtils; import org.dromara.dynamictp.common.entity.DtpExecutorProps; import org.dromara.dynamictp.common.properties.DtpProperties; +import org.dromara.dynamictp.common.util.VersionUtil; import org.dromara.dynamictp.core.executor.ExecutorType; import org.dromara.dynamictp.core.executor.NamedThreadFactory; import org.dromara.dynamictp.core.executor.eager.EagerDtpExecutor; @@ -44,7 +45,23 @@ import java.util.concurrent.Executors; import java.util.concurrent.PriorityBlockingQueue; -import static org.dromara.dynamictp.common.constant.DynamicTpConst.*; +import static org.dromara.dynamictp.common.constant.DynamicTpConst.ALLOW_CORE_THREAD_TIMEOUT; +import static org.dromara.dynamictp.common.constant.DynamicTpConst.AWAIT_TERMINATION_SECONDS; +import static org.dromara.dynamictp.common.constant.DynamicTpConst.AWARE_NAMES; +import static org.dromara.dynamictp.common.constant.DynamicTpConst.NOTIFY_ENABLED; +import static org.dromara.dynamictp.common.constant.DynamicTpConst.NOTIFY_ITEMS; +import static org.dromara.dynamictp.common.constant.DynamicTpConst.PLATFORM_IDS; +import static org.dromara.dynamictp.common.constant.DynamicTpConst.PLUGIN_NAMES; +import static org.dromara.dynamictp.common.constant.DynamicTpConst.PRE_START_ALL_CORE_THREADS; +import static org.dromara.dynamictp.common.constant.DynamicTpConst.QUEUE_TIMEOUT; +import static org.dromara.dynamictp.common.constant.DynamicTpConst.REJECT_ENHANCED; +import static org.dromara.dynamictp.common.constant.DynamicTpConst.REJECT_HANDLER_TYPE; +import static org.dromara.dynamictp.common.constant.DynamicTpConst.RUN_TIMEOUT; +import static org.dromara.dynamictp.common.constant.DynamicTpConst.TASK_WRAPPERS; +import static org.dromara.dynamictp.common.constant.DynamicTpConst.THREAD_POOL_ALIAS_NAME; +import static org.dromara.dynamictp.common.constant.DynamicTpConst.THREAD_POOL_NAME; +import static org.dromara.dynamictp.common.constant.DynamicTpConst.TRY_INTERRUPT_WHEN_TIMEOUT; +import static org.dromara.dynamictp.common.constant.DynamicTpConst.WAIT_FOR_TASKS_TO_COMPLETE_ON_SHUTDOWN; import static org.dromara.dynamictp.common.em.QueueTypeEnum.buildLbq; import static org.dromara.dynamictp.common.entity.NotifyItem.mergeAllNotifyItems; @@ -57,6 +74,8 @@ @Slf4j public class DtpBeanDefinitionRegistrar implements ImportBeanDefinitionRegistrar, EnvironmentAware { + private static final String JDK_VERSION_21 = "21"; + private Environment environment; @Override @@ -80,45 +99,34 @@ public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, B } Class executorTypeClass = ExecutorType.getClass(e.getExecutorType()); Map propertyValues; - if (executorTypeClass.equals(VirtualThreadExecutorProxy.class)) { - propertyValues = buildVTPropertyValues(e); - } else { - propertyValues = buildPropertyValues(e); + propertyValues = buildPropertyValues(e); + Object[] args; + try { + args = buildConstructorArgs(executorTypeClass, e); + } catch (UnsupportedOperationException exception) { + return; } - Object[] args = buildConstructorArgs(executorTypeClass, e); BeanRegistrationUtil.register(registry, e.getThreadPoolName(), executorTypeClass, propertyValues, args); }); } - private Map buildVTPropertyValues(DtpExecutorProps props) { + private Map buildPropertyValues(DtpExecutorProps props) { Map propertyValues = Maps.newHashMap(); propertyValues.put(THREAD_POOL_NAME, props.getThreadPoolName()); propertyValues.put(THREAD_POOL_ALIAS_NAME, props.getThreadPoolAliasName()); - val notifyItems = mergeAllNotifyItems(props.getNotifyItems()); - propertyValues.put(NOTIFY_ITEMS, notifyItems); - propertyValues.put(PLATFORM_IDS, props.getPlatformIds()); - propertyValues.put(NOTIFY_ENABLED, props.isNotifyEnabled()); - val taskWrappers = TaskWrappers.getInstance().getByNames(props.getTaskWrapperNames()); - propertyValues.put(TASK_WRAPPERS, taskWrappers); - propertyValues.put(PLUGIN_NAMES, props.getPluginNames()); - propertyValues.put(AWARE_NAMES, props.getAwareNames()); - return propertyValues; - } + if (!ExecutorType.getClass(props.getExecutorType()).equals(VirtualThreadExecutorProxy.class)) { + propertyValues.put(ALLOW_CORE_THREAD_TIMEOUT, props.isAllowCoreThreadTimeOut()); + propertyValues.put(WAIT_FOR_TASKS_TO_COMPLETE_ON_SHUTDOWN, props.isWaitForTasksToCompleteOnShutdown()); + propertyValues.put(AWAIT_TERMINATION_SECONDS, props.getAwaitTerminationSeconds()); + propertyValues.put(PRE_START_ALL_CORE_THREADS, props.isPreStartAllCoreThreads()); + propertyValues.put(REJECT_HANDLER_TYPE, props.getRejectedHandlerType()); + propertyValues.put(REJECT_ENHANCED, props.isRejectEnhanced()); + propertyValues.put(RUN_TIMEOUT, props.getRunTimeout()); + propertyValues.put(TRY_INTERRUPT_WHEN_TIMEOUT, props.isTryInterrupt()); + propertyValues.put(QUEUE_TIMEOUT, props.getQueueTimeout()); + } - private Map buildPropertyValues(DtpExecutorProps props) { - Map propertyValues = Maps.newHashMap(); - propertyValues.put(THREAD_POOL_NAME, props.getThreadPoolName()); - propertyValues.put(THREAD_POOL_ALIAS_NAME, props.getThreadPoolAliasName()); - propertyValues.put(ALLOW_CORE_THREAD_TIMEOUT, props.isAllowCoreThreadTimeOut()); - propertyValues.put(WAIT_FOR_TASKS_TO_COMPLETE_ON_SHUTDOWN, props.isWaitForTasksToCompleteOnShutdown()); - propertyValues.put(AWAIT_TERMINATION_SECONDS, props.getAwaitTerminationSeconds()); - propertyValues.put(PRE_START_ALL_CORE_THREADS, props.isPreStartAllCoreThreads()); - propertyValues.put(REJECT_HANDLER_TYPE, props.getRejectedHandlerType()); - propertyValues.put(REJECT_ENHANCED, props.isRejectEnhanced()); - propertyValues.put(RUN_TIMEOUT, props.getRunTimeout()); - propertyValues.put(TRY_INTERRUPT_WHEN_TIMEOUT, props.isTryInterrupt()); - propertyValues.put(QUEUE_TIMEOUT, props.getQueueTimeout()); val notifyItems = mergeAllNotifyItems(props.getNotifyItems()); propertyValues.put(NOTIFY_ITEMS, notifyItems); propertyValues.put(PLATFORM_IDS, props.getPlatformIds()); @@ -131,13 +139,17 @@ private Map buildPropertyValues(DtpExecutorProps props) { return propertyValues; } - private Object[] buildConstructorArgs(Class clazz, DtpExecutorProps props) { + private Object[] buildConstructorArgs(Class clazz, DtpExecutorProps props) throws UnsupportedOperationException { BlockingQueue taskQueue; if (clazz.equals(EagerDtpExecutor.class)) { taskQueue = new TaskQueue(props.getQueueCapacity()); } else if (clazz.equals(PriorityDtpExecutor.class)) { taskQueue = new PriorityBlockingQueue<>(props.getQueueCapacity(), PriorityDtpExecutor.getRunnableComparator()); } else if (clazz.equals(VirtualThreadExecutorProxy.class)) { + if (!VersionUtil.getVersion().equals(JDK_VERSION_21)) { + log.warn("DynamicTp virtual thread executor {} register warn: update your JDK version or don't use virtual thread executor!", props.getThreadPoolName()); + throw new UnsupportedOperationException(); + } return new Object[]{ Executors.newVirtualThreadPerTaskExecutor() }; diff --git a/starter/starter-common/src/main/java/org/dromara/dynamictp/starter/common/monitor/DtpEndpoint.java b/starter/starter-common/src/main/java/org/dromara/dynamictp/starter/common/monitor/DtpEndpoint.java index df9d5d458..077c97b23 100644 --- a/starter/starter-common/src/main/java/org/dromara/dynamictp/starter/common/monitor/DtpEndpoint.java +++ b/starter/starter-common/src/main/java/org/dromara/dynamictp/starter/common/monitor/DtpEndpoint.java @@ -48,11 +48,7 @@ public List invoke() { List metricsList = Lists.newArrayList(); DtpRegistry.getAllExecutorNames().forEach(x -> { ExecutorWrapper wrapper = DtpRegistry.getExecutorWrapper(x); - if (wrapper.isVirtualThreadExecutor()) { - metricsList.add(ExecutorConverter.toVTExecutorMetrics(wrapper)); - } else { - metricsList.add(ExecutorConverter.toMetrics(wrapper)); - } + metricsList.add(ExecutorConverter.toMetrics(wrapper)); }); val handlerMap = ContextManagerHelper.getBeansOfType(MetricsAware.class); From c40e8543a4ba32398a9e546fa09390e612ec7d9f Mon Sep 17 00:00:00 2001 From: TheFatRatre <1981590178@qq.com> Date: Mon, 18 Nov 2024 12:18:05 +0800 Subject: [PATCH 25/26] =?UTF-8?q?=E9=87=8D=E6=9E=84=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../adapter/common/AbstractDtpAdapter.java | 6 +- ...hreadPoolStats.java => ExecutorStats.java} | 19 ++- .../common/entity/VTExecutorStats.java | 45 ------- .../dromara/dynamictp/core/DtpRegistry.java | 6 +- .../dynamictp/core/aware/MetricsAware.java | 6 +- .../core/converter/ExecutorConverter.java | 55 ++++---- .../core/handler/CollectorHandler.java | 23 +--- .../dynamictp/core/monitor/DtpMonitor.java | 12 +- .../collector/InternalLogCollector.java | 9 +- .../core/monitor/collector/LogCollector.java | 15 +-- .../monitor/collector/MetricsCollector.java | 11 +- .../collector/MicroMeterCollector.java | 118 ++++++------------ .../monitor/collector/jmx/JMXCollector.java | 25 +--- .../collector/jmx/ThreadPoolStatsJMX.java | 16 +-- .../collector/jmx/ThreadPoolStatsMXBean.java | 8 +- .../collector/jmx/VTExecutorStatsJMX.java | 47 ------- .../collector/jmx/VTExecutorStatsMXBean.java | 38 ------ .../core/support/ExecutorWrapper.java | 4 +- .../proxy/VirtualThreadExecutorProxy.java | 6 +- dependencies/pom.xml | 4 +- .../example/collector/EsCollector.java | 9 +- .../example/collector/EsCollector.java | 10 +- .../example/collector/EsCollector.java | 9 +- .../example/collector/EsCollector.java | 10 +- .../example/collector/EsCollector.java | 10 +- .../example/collector/EsCollector.java | 10 +- .../example/collector/EsCollector.java | 10 +- .../example/collector/EsCollector.java | 10 +- .../example/collector/EsCollector.java | 10 +- .../DtpBeanDefinitionRegistrar.java | 9 +- 30 files changed, 150 insertions(+), 420 deletions(-) rename common/src/main/java/org/dromara/dynamictp/common/entity/{ThreadPoolStats.java => ExecutorStats.java} (88%) delete mode 100644 common/src/main/java/org/dromara/dynamictp/common/entity/VTExecutorStats.java delete mode 100644 core/src/main/java/org/dromara/dynamictp/core/monitor/collector/jmx/VTExecutorStatsJMX.java delete mode 100644 core/src/main/java/org/dromara/dynamictp/core/monitor/collector/jmx/VTExecutorStatsMXBean.java diff --git a/adapter/adapter-common/src/main/java/org/dromara/dynamictp/adapter/common/AbstractDtpAdapter.java b/adapter/adapter-common/src/main/java/org/dromara/dynamictp/adapter/common/AbstractDtpAdapter.java index 604d0676b..7afbaa441 100644 --- a/adapter/adapter-common/src/main/java/org/dromara/dynamictp/adapter/common/AbstractDtpAdapter.java +++ b/adapter/adapter-common/src/main/java/org/dromara/dynamictp/adapter/common/AbstractDtpAdapter.java @@ -29,7 +29,7 @@ import org.apache.commons.collections4.MapUtils; import org.apache.commons.lang3.StringUtils; import org.dromara.dynamictp.common.entity.NotifyPlatform; -import org.dromara.dynamictp.common.entity.ThreadPoolStats; +import org.dromara.dynamictp.common.entity.ExecutorStats; import org.dromara.dynamictp.common.entity.TpExecutorProps; import org.dromara.dynamictp.common.entity.TpMainFields; import org.dromara.dynamictp.common.event.CustomContextRefreshedEvent; @@ -109,13 +109,13 @@ public Map getExecutorWrappers() { * @return thead pools stats */ @Override - public List getMultiPoolStats() { + public List getMultiPoolStats() { val executorWrappers = getExecutorWrappers(); if (MapUtils.isEmpty(executorWrappers)) { return Collections.emptyList(); } - List threadPoolStats = Lists.newArrayList(); + List threadPoolStats = Lists.newArrayList(); executorWrappers.forEach((k, v) -> threadPoolStats.add(ExecutorConverter.toMetrics(v))); return threadPoolStats; } diff --git a/common/src/main/java/org/dromara/dynamictp/common/entity/ThreadPoolStats.java b/common/src/main/java/org/dromara/dynamictp/common/entity/ExecutorStats.java similarity index 88% rename from common/src/main/java/org/dromara/dynamictp/common/entity/ThreadPoolStats.java rename to common/src/main/java/org/dromara/dynamictp/common/entity/ExecutorStats.java index 0e82a9a10..a5001e0ba 100644 --- a/common/src/main/java/org/dromara/dynamictp/common/entity/ThreadPoolStats.java +++ b/common/src/main/java/org/dromara/dynamictp/common/entity/ExecutorStats.java @@ -29,18 +29,35 @@ **/ @Data @EqualsAndHashCode(callSuper = true) -public class ThreadPoolStats extends Metrics { +public class ExecutorStats extends Metrics { + + /** + * 执行器名字 + */ + private String executorName; + + /** + * 执行器别名 + */ + private String executorAliasName; /** * 线程池名字 */ + @Deprecated private String poolName; /** * 线程池别名 */ + @Deprecated private String poolAliasName; + /** + * 是否为虚拟线程执行器 + */ + private boolean isVirtualExecutor; + /** * 核心线程数 */ diff --git a/common/src/main/java/org/dromara/dynamictp/common/entity/VTExecutorStats.java b/common/src/main/java/org/dromara/dynamictp/common/entity/VTExecutorStats.java deleted file mode 100644 index 41c59631e..000000000 --- a/common/src/main/java/org/dromara/dynamictp/common/entity/VTExecutorStats.java +++ /dev/null @@ -1,45 +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.common.entity; - -import lombok.Data; -import lombok.EqualsAndHashCode; - -/** - * ClassName: VTExecutorStats - * Package: org.dromara.dynamictp.common.entity - * Description: - * - * @author CYC - * @create 2024/11/4 16:52 - */ -@Data -@EqualsAndHashCode(callSuper = true) -public class VTExecutorStats extends Metrics { - - /** - * 虚拟线程执行器名字 - */ - private String executorName; - - /** - * 虚拟线程执行器别名 - */ - private String executorAliasName; - -} diff --git a/core/src/main/java/org/dromara/dynamictp/core/DtpRegistry.java b/core/src/main/java/org/dromara/dynamictp/core/DtpRegistry.java index b49a183b1..9e6721f59 100644 --- a/core/src/main/java/org/dromara/dynamictp/core/DtpRegistry.java +++ b/core/src/main/java/org/dromara/dynamictp/core/DtpRegistry.java @@ -227,10 +227,8 @@ private static void doRefresh(ExecutorWrapper executorWrapper, DtpExecutorProps if (!Objects.equals(executor.allowsCoreThreadTimeOut(), props.isAllowCoreThreadTimeOut())) { executor.allowCoreThreadTimeOut(props.isAllowCoreThreadTimeOut()); } - if (!executorWrapper.isVirtualThreadExecutor()) { - // update queue - updateQueueProps(executor, props); - } + // update queue + updateQueueProps(executor, props); if (executorWrapper.isDtpExecutor()) { doRefreshDtp(executorWrapper, props); diff --git a/core/src/main/java/org/dromara/dynamictp/core/aware/MetricsAware.java b/core/src/main/java/org/dromara/dynamictp/core/aware/MetricsAware.java index 335b0ad08..d9968b7ee 100644 --- a/core/src/main/java/org/dromara/dynamictp/core/aware/MetricsAware.java +++ b/core/src/main/java/org/dromara/dynamictp/core/aware/MetricsAware.java @@ -17,7 +17,7 @@ package org.dromara.dynamictp.core.aware; -import org.dromara.dynamictp.common.entity.ThreadPoolStats; +import org.dromara.dynamictp.common.entity.ExecutorStats; import java.util.Collections; import java.util.List; @@ -35,7 +35,7 @@ public interface MetricsAware extends DtpAware { * * @return the thread pool stats */ - default ThreadPoolStats getPoolStats() { + default ExecutorStats getPoolStats() { return null; } @@ -44,7 +44,7 @@ default ThreadPoolStats getPoolStats() { * * @return thead pools stats */ - default List getMultiPoolStats() { + default List getMultiPoolStats() { return Collections.emptyList(); } } 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 db7f0eb46..c10dfb636 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 @@ -18,9 +18,8 @@ package org.dromara.dynamictp.core.converter; import lombok.val; -import org.dromara.dynamictp.common.entity.ThreadPoolStats; +import org.dromara.dynamictp.common.entity.ExecutorStats; import org.dromara.dynamictp.common.entity.TpMainFields; -import org.dromara.dynamictp.common.entity.VTExecutorStats; import org.dromara.dynamictp.core.executor.DtpExecutor; import org.dromara.dynamictp.core.monitor.PerformanceProvider; import org.dromara.dynamictp.core.support.ExecutorAdapter; @@ -54,7 +53,7 @@ public static TpMainFields toMainFields(ExecutorWrapper executorWrapper) { return mainFields; } - public static ThreadPoolStats toMetrics(ExecutorWrapper wrapper) { + public static ExecutorStats toMetrics(ExecutorWrapper wrapper) { ExecutorAdapter executor = wrapper.getExecutor(); if (executor == null) { return null; @@ -62,32 +61,35 @@ public static ThreadPoolStats toMetrics(ExecutorWrapper wrapper) { ThreadPoolStatProvider provider = wrapper.getThreadPoolStatProvider(); PerformanceProvider performanceProvider = provider.getPerformanceProvider(); val performanceSnapshot = performanceProvider.getSnapshotAndReset(); - ThreadPoolStats poolStats = convertCommon(executor); - poolStats.setPoolName(wrapper.getThreadPoolName()); - poolStats.setPoolAliasName(wrapper.getThreadPoolAliasName()); + ExecutorStats executorStats = convertCommon(executor); + executorStats.setPoolName(wrapper.getThreadPoolName()); + executorStats.setPoolAliasName(wrapper.getThreadPoolAliasName()); if (!wrapper.isVirtualThreadExecutor()) { - poolStats.setRunTimeoutCount(provider.getRunTimeoutCount()); - poolStats.setQueueTimeoutCount(provider.getQueueTimeoutCount()); - poolStats.setRejectCount(provider.getRejectedTaskCount()); + executorStats.setRunTimeoutCount(provider.getRunTimeoutCount()); + executorStats.setQueueTimeoutCount(provider.getQueueTimeoutCount()); + executorStats.setRejectCount(provider.getRejectedTaskCount()); + executorStats.setVirtualExecutor(false); + } else { + executorStats.setVirtualExecutor(true); } - poolStats.setDynamic(executor instanceof DtpExecutor); - poolStats.setTps(performanceSnapshot.getTps()); - poolStats.setAvg(performanceSnapshot.getAvg()); - poolStats.setMaxRt(performanceSnapshot.getMaxRt()); - poolStats.setMinRt(performanceSnapshot.getMinRt()); - poolStats.setTp50(performanceSnapshot.getTp50()); - poolStats.setTp75(performanceSnapshot.getTp75()); - poolStats.setTp90(performanceSnapshot.getTp90()); - poolStats.setTp95(performanceSnapshot.getTp95()); - poolStats.setTp99(performanceSnapshot.getTp99()); - poolStats.setTp999(performanceSnapshot.getTp999()); - return poolStats; + executorStats.setDynamic(executor instanceof DtpExecutor); + executorStats.setTps(performanceSnapshot.getTps()); + executorStats.setAvg(performanceSnapshot.getAvg()); + executorStats.setMaxRt(performanceSnapshot.getMaxRt()); + executorStats.setMinRt(performanceSnapshot.getMinRt()); + executorStats.setTp50(performanceSnapshot.getTp50()); + executorStats.setTp75(performanceSnapshot.getTp75()); + executorStats.setTp90(performanceSnapshot.getTp90()); + executorStats.setTp95(performanceSnapshot.getTp95()); + executorStats.setTp99(performanceSnapshot.getTp99()); + executorStats.setTp999(performanceSnapshot.getTp999()); + return executorStats; } - private static ThreadPoolStats convertCommon(ExecutorAdapter executor) { - ThreadPoolStats poolStats = new ThreadPoolStats(); + private static ExecutorStats convertCommon(ExecutorAdapter executor) { + ExecutorStats poolStats = new ExecutorStats(); poolStats.setCorePoolSize(executor.getCorePoolSize()); poolStats.setMaximumPoolSize(executor.getMaximumPoolSize()); poolStats.setPoolSize(executor.getPoolSize()); @@ -104,11 +106,4 @@ private static ThreadPoolStats convertCommon(ExecutorAdapter executor) { poolStats.setKeepAliveTime(executor.getKeepAliveTime(TimeUnit.MILLISECONDS)); return poolStats; } - - private static VTExecutorStats convertCommonVT(ExecutorAdapter executor) { - VTExecutorStats executorStats = new VTExecutorStats(); - executorStats.setActiveCount(executor.getActiveCount()); - executorStats.setTaskCount(executor.getTaskCount()); - return executorStats; - } } diff --git a/core/src/main/java/org/dromara/dynamictp/core/handler/CollectorHandler.java b/core/src/main/java/org/dromara/dynamictp/core/handler/CollectorHandler.java index 548aab631..6a0313975 100644 --- a/core/src/main/java/org/dromara/dynamictp/core/handler/CollectorHandler.java +++ b/core/src/main/java/org/dromara/dynamictp/core/handler/CollectorHandler.java @@ -19,8 +19,7 @@ import com.google.common.collect.Maps; import lombok.extern.slf4j.Slf4j; -import org.dromara.dynamictp.common.entity.ThreadPoolStats; -import org.dromara.dynamictp.common.entity.VTExecutorStats; +import org.dromara.dynamictp.common.entity.ExecutorStats; import org.dromara.dynamictp.common.util.ExtensionServiceLoader; import org.dromara.dynamictp.core.monitor.collector.InternalLogCollector; import org.dromara.dynamictp.core.monitor.collector.LogCollector; @@ -45,7 +44,7 @@ public final class CollectorHandler { private CollectorHandler() { List loadedCollectors = ExtensionServiceLoader.get(MetricsCollector.class); - loadedCollectors.forEach(collector -> COLLECTORS.put(collector.type(), collector)); + loadedCollectors.forEach(collector -> COLLECTORS.put(collector.type().toLowerCase(), collector)); MetricsCollector microMeterCollector = new MicroMeterCollector(); LogCollector logCollector = new LogCollector(); @@ -57,26 +56,14 @@ private CollectorHandler() { COLLECTORS.put(jmxCollector.type(), jmxCollector); } - public void collect(ThreadPoolStats poolStats, List types) { - if (poolStats == null || CollectionUtils.isEmpty(types)) { + public void collect(ExecutorStats executorStats, List types) { + if (executorStats == null || CollectionUtils.isEmpty(types)) { return; } for (String collectorType : types) { MetricsCollector collector = COLLECTORS.get(collectorType.toLowerCase()); if (collector != null) { - collector.collect(poolStats); - } - } - } - - public void collectVTExecutor(VTExecutorStats vtExecutorStats, List types) { - if (vtExecutorStats == null || CollectionUtils.isEmpty(types)) { - return; - } - for (String collectorType : types) { - MetricsCollector collector = COLLECTORS.get(collectorType.toLowerCase()); - if (collector != null) { - collector.collect(vtExecutorStats); + collector.collect(executorStats); } } } diff --git a/core/src/main/java/org/dromara/dynamictp/core/monitor/DtpMonitor.java b/core/src/main/java/org/dromara/dynamictp/core/monitor/DtpMonitor.java index b0fa3fe75..b4ea4f55d 100644 --- a/core/src/main/java/org/dromara/dynamictp/core/monitor/DtpMonitor.java +++ b/core/src/main/java/org/dromara/dynamictp/core/monitor/DtpMonitor.java @@ -19,8 +19,7 @@ import com.google.common.eventbus.Subscribe; import lombok.extern.slf4j.Slf4j; -import org.dromara.dynamictp.common.entity.ThreadPoolStats; -import org.dromara.dynamictp.common.entity.VTExecutorStats; +import org.dromara.dynamictp.common.entity.ExecutorStats; import org.dromara.dynamictp.common.event.AlarmCheckEvent; import org.dromara.dynamictp.common.event.CollectEvent; import org.dromara.dynamictp.common.event.CustomContextRefreshedEvent; @@ -102,15 +101,8 @@ private void collectMetrics(Set executorNames) { publishCollectEvent(); } - private void doCollectVTExecutor(VTExecutorStats vtExecutorStats) { - try { - CollectorHandler.getInstance().collectVTExecutor(vtExecutorStats, dtpProperties.getCollectorTypes()); - } catch (Exception e) { - log.error("DynamicTp monitor, VTExecutor collect error.", e); - } - } - private void doCollect(ThreadPoolStats threadPoolStats) { + private void doCollect(ExecutorStats threadPoolStats) { try { CollectorHandler.getInstance().collect(threadPoolStats, dtpProperties.getCollectorTypes()); } catch (Exception e) { diff --git a/core/src/main/java/org/dromara/dynamictp/core/monitor/collector/InternalLogCollector.java b/core/src/main/java/org/dromara/dynamictp/core/monitor/collector/InternalLogCollector.java index 2b718920d..8e9b7877a 100644 --- a/core/src/main/java/org/dromara/dynamictp/core/monitor/collector/InternalLogCollector.java +++ b/core/src/main/java/org/dromara/dynamictp/core/monitor/collector/InternalLogCollector.java @@ -17,9 +17,8 @@ package org.dromara.dynamictp.core.monitor.collector; -import org.dromara.dynamictp.common.entity.ThreadPoolStats; +import org.dromara.dynamictp.common.entity.ExecutorStats; import org.dromara.dynamictp.common.em.CollectorTypeEnum; -import org.dromara.dynamictp.common.entity.VTExecutorStats; import org.dromara.dynamictp.common.util.JsonUtil; import lombok.extern.slf4j.Slf4j; @@ -30,14 +29,10 @@ public class InternalLogCollector extends AbstractCollector { @Override - public void collect(ThreadPoolStats poolStats) { + public void collect(ExecutorStats poolStats) { log.info("dynamic.tp metrics: {}", JsonUtil.toJson(poolStats)); } - @Override - public void collect(VTExecutorStats vtExecutorStats) { - log.info("dynamic.vt metrics: {}", JsonUtil.toJson(vtExecutorStats)); - } @Override public String type() { diff --git a/core/src/main/java/org/dromara/dynamictp/core/monitor/collector/LogCollector.java b/core/src/main/java/org/dromara/dynamictp/core/monitor/collector/LogCollector.java index 40939bd9f..187de9b47 100644 --- a/core/src/main/java/org/dromara/dynamictp/core/monitor/collector/LogCollector.java +++ b/core/src/main/java/org/dromara/dynamictp/core/monitor/collector/LogCollector.java @@ -18,8 +18,7 @@ package org.dromara.dynamictp.core.monitor.collector; import org.dromara.dynamictp.common.em.CollectorTypeEnum; -import org.dromara.dynamictp.common.entity.ThreadPoolStats; -import org.dromara.dynamictp.common.entity.VTExecutorStats; +import org.dromara.dynamictp.common.entity.ExecutorStats; import org.dromara.dynamictp.common.util.JsonUtil; import org.dromara.dynamictp.logging.LogHelper; import lombok.extern.slf4j.Slf4j; @@ -34,7 +33,7 @@ public class LogCollector extends AbstractCollector { @Override - public void collect(ThreadPoolStats threadPoolStats) { + public void collect(ExecutorStats threadPoolStats) { String metrics = JsonUtil.toJson(threadPoolStats); if (LogHelper.getMonitorLogger() == null) { log.error("Cannot find monitor logger..."); @@ -43,16 +42,6 @@ public void collect(ThreadPoolStats threadPoolStats) { LogHelper.getMonitorLogger().info("{}", metrics); } - @Override - public void collect(VTExecutorStats vtExecutorStats) { - String metrics = JsonUtil.toJson(vtExecutorStats); - if (LogHelper.getMonitorLogger() == null) { - log.error("Cannot find monitor logger..."); - return; - } - LogHelper.getMonitorLogger().info("{}", metrics); - } - @Override public String type() { return CollectorTypeEnum.LOGGING.name().toLowerCase(); diff --git a/core/src/main/java/org/dromara/dynamictp/core/monitor/collector/MetricsCollector.java b/core/src/main/java/org/dromara/dynamictp/core/monitor/collector/MetricsCollector.java index f9617a042..c06e1540a 100644 --- a/core/src/main/java/org/dromara/dynamictp/core/monitor/collector/MetricsCollector.java +++ b/core/src/main/java/org/dromara/dynamictp/core/monitor/collector/MetricsCollector.java @@ -17,8 +17,7 @@ package org.dromara.dynamictp.core.monitor.collector; -import org.dromara.dynamictp.common.entity.ThreadPoolStats; -import org.dromara.dynamictp.common.entity.VTExecutorStats; +import org.dromara.dynamictp.common.entity.ExecutorStats; /** * MetricsCollector related @@ -32,13 +31,7 @@ public interface MetricsCollector { * Collect key metrics. * @param poolStats ThreadPoolStats instance */ - void collect(ThreadPoolStats poolStats); - - /** - * Collect virtual thread key metrics. - * @param vtExecutorStats - */ - void collect(VTExecutorStats vtExecutorStats); + void collect(ExecutorStats poolStats); /** * Collector type. diff --git a/core/src/main/java/org/dromara/dynamictp/core/monitor/collector/MicroMeterCollector.java b/core/src/main/java/org/dromara/dynamictp/core/monitor/collector/MicroMeterCollector.java index ae9d97980..8f59d4d4f 100644 --- a/core/src/main/java/org/dromara/dynamictp/core/monitor/collector/MicroMeterCollector.java +++ b/core/src/main/java/org/dromara/dynamictp/core/monitor/collector/MicroMeterCollector.java @@ -21,8 +21,7 @@ import io.micrometer.core.instrument.Tag; import lombok.extern.slf4j.Slf4j; import org.dromara.dynamictp.common.em.CollectorTypeEnum; -import org.dromara.dynamictp.common.entity.ThreadPoolStats; -import org.dromara.dynamictp.common.entity.VTExecutorStats; +import org.dromara.dynamictp.common.entity.ExecutorStats; import org.dromara.dynamictp.common.util.BeanCopierUtil; import org.dromara.dynamictp.common.util.CommonUtil; @@ -62,26 +61,15 @@ public class MicroMeterCollector extends AbstractCollector { private static final Map GAUGE_CACHE = new ConcurrentHashMap<>(); @Override - public void collect(ThreadPoolStats threadPoolStats) { + public void collect(ExecutorStats executorStats) { // metrics must be held with a strong reference, even though it is never referenced within this class - ThreadPoolStats oldStats = (ThreadPoolStats) GAUGE_CACHE.get(threadPoolStats.getPoolName()); + ExecutorStats oldStats = (ExecutorStats) GAUGE_CACHE.get(executorStats.getPoolName()); if (Objects.isNull(oldStats)) { - GAUGE_CACHE.put(threadPoolStats.getPoolName(), threadPoolStats); + GAUGE_CACHE.put(executorStats.getPoolName(), executorStats); } else { - BeanCopierUtil.copyProperties(threadPoolStats, oldStats); + BeanCopierUtil.copyProperties(executorStats, oldStats); } - gauge((ThreadPoolStats) GAUGE_CACHE.get(threadPoolStats.getPoolName())); - } - - @Override - public void collect(VTExecutorStats vtExecutorStats) { - VTExecutorStats oldStats = (VTExecutorStats) GAUGE_CACHE.get(vtExecutorStats.getExecutorName()); - if (Objects.isNull(oldStats)) { - GAUGE_CACHE.put(vtExecutorStats.getExecutorName(), vtExecutorStats); - } else { - BeanCopierUtil.copyProperties(vtExecutorStats, oldStats); - } - gauge((VTExecutorStats) GAUGE_CACHE.get(vtExecutorStats.getExecutorName())); + gauge((ExecutorStats) GAUGE_CACHE.get(executorStats.getPoolName())); } @Override @@ -89,78 +77,52 @@ public String type() { return CollectorTypeEnum.MICROMETER.name().toLowerCase(); } - public void gauge(ThreadPoolStats poolStats) { - - Iterable tags = getTags(poolStats); - - Metrics.gauge(metricName("core.size"), tags, poolStats, ThreadPoolStats::getCorePoolSize); - Metrics.gauge(metricName("maximum.size"), tags, poolStats, ThreadPoolStats::getMaximumPoolSize); - Metrics.gauge(metricName("current.size"), tags, poolStats, ThreadPoolStats::getPoolSize); - Metrics.gauge(metricName("largest.size"), tags, poolStats, ThreadPoolStats::getLargestPoolSize); - Metrics.gauge(metricName("active.count"), tags, poolStats, ThreadPoolStats::getActiveCount); - - Metrics.gauge(metricName("task.count"), tags, poolStats, ThreadPoolStats::getTaskCount); - Metrics.gauge(metricName("completed.task.count"), tags, poolStats, ThreadPoolStats::getCompletedTaskCount); - Metrics.gauge(metricName("wait.task.count"), tags, poolStats, ThreadPoolStats::getWaitTaskCount); - - Metrics.gauge(metricName("queue.size"), tags, poolStats, ThreadPoolStats::getQueueSize); - Metrics.gauge(metricName("queue.capacity"), tags, poolStats, ThreadPoolStats::getQueueCapacity); - Metrics.gauge(metricName("queue.remaining.capacity"), tags, poolStats, ThreadPoolStats::getQueueRemainingCapacity); - - Metrics.gauge(metricName("reject.count"), tags, poolStats, ThreadPoolStats::getRejectCount); - Metrics.gauge(metricName("run.timeout.count"), tags, poolStats, ThreadPoolStats::getRunTimeoutCount); - Metrics.gauge(metricName("queue.timeout.count"), tags, poolStats, ThreadPoolStats::getQueueTimeoutCount); - - Metrics.gauge(metricName("tps"), tags, poolStats, ThreadPoolStats::getTps); - Metrics.gauge(metricName("completed.task.time.avg"), tags, poolStats, ThreadPoolStats::getAvg); - Metrics.gauge(metricName("completed.task.time.max"), tags, poolStats, ThreadPoolStats::getMaxRt); - Metrics.gauge(metricName("completed.task.time.min"), tags, poolStats, ThreadPoolStats::getMinRt); - Metrics.gauge(metricName("completed.task.time.tp50"), tags, poolStats, ThreadPoolStats::getTp50); - Metrics.gauge(metricName("completed.task.time.tp75"), tags, poolStats, ThreadPoolStats::getTp75); - Metrics.gauge(metricName("completed.task.time.tp90"), tags, poolStats, ThreadPoolStats::getTp90); - Metrics.gauge(metricName("completed.task.time.tp95"), tags, poolStats, ThreadPoolStats::getTp95); - Metrics.gauge(metricName("completed.task.time.tp99"), tags, poolStats, ThreadPoolStats::getTp99); - Metrics.gauge(metricName("completed.task.time.tp999"), tags, poolStats, ThreadPoolStats::getTp999); - } + public void gauge(ExecutorStats executorStats) { - private void gauge(VTExecutorStats vtExecutorStats) { + Iterable tags = getTags(executorStats); - Iterable tags = getTags(vtExecutorStats); + if (!executorStats.isVirtualExecutor()) { + Metrics.gauge(metricName("core.size"), tags, executorStats, ExecutorStats::getCorePoolSize); + Metrics.gauge(metricName("maximum.size"), tags, executorStats, ExecutorStats::getMaximumPoolSize); + Metrics.gauge(metricName("current.size"), tags, executorStats, ExecutorStats::getPoolSize); + Metrics.gauge(metricName("largest.size"), tags, executorStats, ExecutorStats::getLargestPoolSize); - Metrics.gauge(metricName("active.count"), tags, vtExecutorStats, VTExecutorStats::getActiveCount); - Metrics.gauge(metricName("task.count"), tags, vtExecutorStats, VTExecutorStats::getTaskCount); - Metrics.gauge(metricName("run.timeout.count"), tags, vtExecutorStats, VTExecutorStats::getRunTimeoutCount); + Metrics.gauge(metricName("completed.task.count"), tags, executorStats, ExecutorStats::getCompletedTaskCount); + Metrics.gauge(metricName("wait.task.count"), tags, executorStats, ExecutorStats::getWaitTaskCount); - Metrics.gauge(metricName("tps"), tags, vtExecutorStats, VTExecutorStats::getTps); - Metrics.gauge(metricName("completed.task.time.avg"), tags, vtExecutorStats, VTExecutorStats::getAvg); - Metrics.gauge(metricName("completed.task.time.max"), tags, vtExecutorStats, VTExecutorStats::getMaxRt); - Metrics.gauge(metricName("completed.task.time.min"), tags, vtExecutorStats, VTExecutorStats::getMinRt); - Metrics.gauge(metricName("completed.task.time.tp50"), tags, vtExecutorStats, VTExecutorStats::getTp50); - Metrics.gauge(metricName("completed.task.time.tp75"), tags, vtExecutorStats, VTExecutorStats::getTp75); - Metrics.gauge(metricName("completed.task.time.tp90"), tags, vtExecutorStats, VTExecutorStats::getTp90); - Metrics.gauge(metricName("completed.task.time.tp95"), tags, vtExecutorStats, VTExecutorStats::getTp95); - Metrics.gauge(metricName("completed.task.time.tp99"), tags, vtExecutorStats, VTExecutorStats::getTp99); - Metrics.gauge(metricName("completed.task.time.tp999"), tags, vtExecutorStats, VTExecutorStats::getTp999); + Metrics.gauge(metricName("queue.size"), tags, executorStats, ExecutorStats::getQueueSize); + Metrics.gauge(metricName("queue.capacity"), tags, executorStats, ExecutorStats::getQueueCapacity); + Metrics.gauge(metricName("queue.remaining.capacity"), tags, executorStats, ExecutorStats::getQueueRemainingCapacity); + + Metrics.gauge(metricName("reject.count"), tags, executorStats, ExecutorStats::getRejectCount); + Metrics.gauge(metricName("queue.timeout.count"), tags, executorStats, ExecutorStats::getQueueTimeoutCount); + } + + Metrics.gauge(metricName("active.count"), tags, executorStats, ExecutorStats::getActiveCount); + Metrics.gauge(metricName("task.count"), tags, executorStats, ExecutorStats::getTaskCount); + Metrics.gauge(metricName("run.timeout.count"), tags, executorStats, ExecutorStats::getRunTimeoutCount); + + Metrics.gauge(metricName("tps"), tags, executorStats, ExecutorStats::getTps); + Metrics.gauge(metricName("completed.task.time.avg"), tags, executorStats, ExecutorStats::getAvg); + Metrics.gauge(metricName("completed.task.time.max"), tags, executorStats, ExecutorStats::getMaxRt); + Metrics.gauge(metricName("completed.task.time.min"), tags, executorStats, ExecutorStats::getMinRt); + Metrics.gauge(metricName("completed.task.time.tp50"), tags, executorStats, ExecutorStats::getTp50); + Metrics.gauge(metricName("completed.task.time.tp75"), tags, executorStats, ExecutorStats::getTp75); + Metrics.gauge(metricName("completed.task.time.tp90"), tags, executorStats, ExecutorStats::getTp90); + Metrics.gauge(metricName("completed.task.time.tp95"), tags, executorStats, ExecutorStats::getTp95); + Metrics.gauge(metricName("completed.task.time.tp99"), tags, executorStats, ExecutorStats::getTp99); + Metrics.gauge(metricName("completed.task.time.tp999"), tags, executorStats, ExecutorStats::getTp999); } private static String metricName(String name) { return String.join(".", DTP_METRIC_NAME_PREFIX, name); } - private Iterable getTags(ThreadPoolStats poolStats) { + private Iterable getTags(ExecutorStats executorStats) { List tags = new ArrayList<>(3); - tags.add(Tag.of(POOL_NAME_TAG, poolStats.getPoolName())); - tags.add(Tag.of(APP_NAME_TAG, CommonUtil.getInstance().getServiceName())); - // https://github.com/dromara/dynamic-tp/issues/359 - tags.add(Tag.of(POOL_ALIAS_TAG, Optional.ofNullable(poolStats.getPoolAliasName()).orElse(poolStats.getPoolName()))); - return tags; - } - - private Iterable getTags(VTExecutorStats vtExecutorStats) { - ArrayList tags = new ArrayList<>(3); - tags.add(Tag.of(VTE_NAME_TAG, vtExecutorStats.getExecutorName())); + tags.add(Tag.of(executorStats.isVirtualExecutor() ? VTE_NAME_TAG : POOL_NAME_TAG, executorStats.getPoolName())); tags.add(Tag.of(APP_NAME_TAG, CommonUtil.getInstance().getServiceName())); - tags.add(Tag.of(VTE_ALIAS_TAG, Optional.ofNullable(vtExecutorStats.getExecutorAliasName()).orElse(vtExecutorStats.getExecutorName()))); + tags.add(Tag.of(executorStats.isVirtualExecutor() ? VTE_ALIAS_TAG : POOL_ALIAS_TAG, Optional.ofNullable(executorStats.getExecutorAliasName()).orElse(executorStats.getPoolName()))); return tags; } } 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 761c5efaa..bc2412a37 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 @@ -20,8 +20,7 @@ import lombok.extern.slf4j.Slf4j; import org.dromara.dynamictp.common.em.CollectorTypeEnum; import org.dromara.dynamictp.common.entity.Metrics; -import org.dromara.dynamictp.common.entity.ThreadPoolStats; -import org.dromara.dynamictp.common.entity.VTExecutorStats; +import org.dromara.dynamictp.common.entity.ExecutorStats; import org.dromara.dynamictp.common.util.BeanCopierUtil; import org.dromara.dynamictp.core.monitor.collector.AbstractCollector; @@ -48,9 +47,9 @@ public class JMXCollector extends AbstractCollector { private static final Map GAUGE_CACHE = new ConcurrentHashMap<>(); @Override - public void collect(ThreadPoolStats threadPoolStats) { + public void collect(ExecutorStats threadPoolStats) { if (GAUGE_CACHE.containsKey(threadPoolStats.getPoolName())) { - ThreadPoolStats poolStats = (ThreadPoolStats) GAUGE_CACHE.get(threadPoolStats.getPoolName()); + ExecutorStats poolStats = (ExecutorStats) GAUGE_CACHE.get(threadPoolStats.getPoolName()); BeanCopierUtil.copyProperties(threadPoolStats, poolStats); } else { try { @@ -65,24 +64,6 @@ public void collect(ThreadPoolStats threadPoolStats) { } } - @Override - public void collect(VTExecutorStats vtExecutorStats) { - if (GAUGE_CACHE.containsKey(vtExecutorStats.getExecutorName())) { - ThreadPoolStats poolStats = (ThreadPoolStats) GAUGE_CACHE.get(vtExecutorStats.getExecutorName()); - BeanCopierUtil.copyProperties(vtExecutorStats, poolStats); - } else { - try { - MBeanServer server = ManagementFactory.getPlatformMBeanServer(); - ObjectName name = new ObjectName(DTP_METRIC_NAME_PREFIX + ":name=" + vtExecutorStats.getExecutorName()); - VTExecutorStatsJMX stats = new VTExecutorStatsJMX(vtExecutorStats); - server.registerMBean(stats, name); - } catch (JMException e) { - log.error("collect thread pool stats error", e); - } - GAUGE_CACHE.put(vtExecutorStats.getExecutorName(), vtExecutorStats); - } - } - @Override public String type() { return CollectorTypeEnum.JMX.name().toLowerCase(); diff --git a/core/src/main/java/org/dromara/dynamictp/core/monitor/collector/jmx/ThreadPoolStatsJMX.java b/core/src/main/java/org/dromara/dynamictp/core/monitor/collector/jmx/ThreadPoolStatsJMX.java index 1f43c312f..c8d3d1f3e 100644 --- a/core/src/main/java/org/dromara/dynamictp/core/monitor/collector/jmx/ThreadPoolStatsJMX.java +++ b/core/src/main/java/org/dromara/dynamictp/core/monitor/collector/jmx/ThreadPoolStatsJMX.java @@ -17,26 +17,26 @@ package org.dromara.dynamictp.core.monitor.collector.jmx; -import org.dromara.dynamictp.common.entity.ThreadPoolStats; +import org.dromara.dynamictp.common.entity.ExecutorStats; /** * @author KamTo Hung */ public class ThreadPoolStatsJMX implements ThreadPoolStatsMXBean { - private ThreadPoolStats threadPoolStats; + private ExecutorStats executorStats; - public ThreadPoolStatsJMX(ThreadPoolStats threadPoolStats) { - this.threadPoolStats = threadPoolStats; + public ThreadPoolStatsJMX(ExecutorStats executorStats) { + this.executorStats = executorStats; } @Override - public ThreadPoolStats getThreadPoolStats() { - return this.threadPoolStats; + public ExecutorStats getExecutorStats() { + return this.executorStats; } @Override - public void setThreadPoolStats(ThreadPoolStats threadPoolStats) { - this.threadPoolStats = threadPoolStats; + public void setExecutorStats(ExecutorStats executorStats) { + this.executorStats = executorStats; } } diff --git a/core/src/main/java/org/dromara/dynamictp/core/monitor/collector/jmx/ThreadPoolStatsMXBean.java b/core/src/main/java/org/dromara/dynamictp/core/monitor/collector/jmx/ThreadPoolStatsMXBean.java index 3959cca36..b6996b0ee 100644 --- a/core/src/main/java/org/dromara/dynamictp/core/monitor/collector/jmx/ThreadPoolStatsMXBean.java +++ b/core/src/main/java/org/dromara/dynamictp/core/monitor/collector/jmx/ThreadPoolStatsMXBean.java @@ -17,7 +17,7 @@ package org.dromara.dynamictp.core.monitor.collector.jmx; -import org.dromara.dynamictp.common.entity.ThreadPoolStats; +import org.dromara.dynamictp.common.entity.ExecutorStats; import javax.management.MXBean; @@ -34,13 +34,13 @@ public interface ThreadPoolStatsMXBean { * * @return thread pool stats */ - ThreadPoolStats getThreadPoolStats(); + ExecutorStats getExecutorStats(); /** * set thread pool stats * - * @param threadPoolStats thread pool stats + * @param executorStats thread pool stats */ - void setThreadPoolStats(ThreadPoolStats threadPoolStats); + void setExecutorStats(ExecutorStats executorStats); } diff --git a/core/src/main/java/org/dromara/dynamictp/core/monitor/collector/jmx/VTExecutorStatsJMX.java b/core/src/main/java/org/dromara/dynamictp/core/monitor/collector/jmx/VTExecutorStatsJMX.java deleted file mode 100644 index 47283608e..000000000 --- a/core/src/main/java/org/dromara/dynamictp/core/monitor/collector/jmx/VTExecutorStatsJMX.java +++ /dev/null @@ -1,47 +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.core.monitor.collector.jmx; - -import org.dromara.dynamictp.common.entity.VTExecutorStats; - -/** - * ClassName: VTExecutorStatsJMX - * Package: org.dromara.dynamictp.core.monitor.collector.jmx - * Description: - * - * @author CYC - * @create 2024/11/5 18:29 - */ -public class VTExecutorStatsJMX implements VTExecutorStatsMXBean { - - private VTExecutorStats vtExecutorStats; - - public VTExecutorStatsJMX(VTExecutorStats vtExecutorStats) { - this.vtExecutorStats = vtExecutorStats; - } - - @Override - public VTExecutorStats getVTExecutorStats() { - return vtExecutorStats; - } - - @Override - public void setVTExecutorStats(VTExecutorStats vtExecutorStats) { - this.vtExecutorStats = vtExecutorStats; - } -} diff --git a/core/src/main/java/org/dromara/dynamictp/core/monitor/collector/jmx/VTExecutorStatsMXBean.java b/core/src/main/java/org/dromara/dynamictp/core/monitor/collector/jmx/VTExecutorStatsMXBean.java deleted file mode 100644 index 7dca8b76c..000000000 --- a/core/src/main/java/org/dromara/dynamictp/core/monitor/collector/jmx/VTExecutorStatsMXBean.java +++ /dev/null @@ -1,38 +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.core.monitor.collector.jmx; - -import org.dromara.dynamictp.common.entity.VTExecutorStats; - -import javax.management.MXBean; - -/** - * ClassName: VTExecutorStatsMXBean - * Package: org.dromara.dynamictp.core.monitor.collector.jmx - * Description: - * - * @author CYC - * @create 2024/11/7 14:44 - */ -@MXBean -public interface VTExecutorStatsMXBean { - - VTExecutorStats getVTExecutorStats(); - - void setVTExecutorStats(VTExecutorStats vtExecutorStats); -} diff --git a/core/src/main/java/org/dromara/dynamictp/core/support/ExecutorWrapper.java b/core/src/main/java/org/dromara/dynamictp/core/support/ExecutorWrapper.java index ff5cb80c5..30d71e758 100644 --- a/core/src/main/java/org/dromara/dynamictp/core/support/ExecutorWrapper.java +++ b/core/src/main/java/org/dromara/dynamictp/core/support/ExecutorWrapper.java @@ -178,9 +178,7 @@ public void initialize() { if (isDtpExecutor()) { ((DtpExecutor) getExecutor()).initialize(); AwareManager.register(this); - } else if (isThreadPoolExecutor()) { - AwareManager.register(this); - } else if (isVirtualThreadExecutor()) { + } else if (isThreadPoolExecutor() || isVirtualThreadExecutor()) { AwareManager.register(this); } } diff --git a/core/src/main/java/org/dromara/dynamictp/core/support/proxy/VirtualThreadExecutorProxy.java b/core/src/main/java/org/dromara/dynamictp/core/support/proxy/VirtualThreadExecutorProxy.java index 434d209a3..54e05b428 100644 --- a/core/src/main/java/org/dromara/dynamictp/core/support/proxy/VirtualThreadExecutorProxy.java +++ b/core/src/main/java/org/dromara/dynamictp/core/support/proxy/VirtualThreadExecutorProxy.java @@ -99,9 +99,9 @@ public VirtualThreadExecutorProxy(ExecutorService executor) { @Override public void execute(Runnable command) { command = getEnhancedTask(command); - EnhancedRunnable.of(command, this); - AwareManager.execute(this, command); - threadPerTaskExecutor.execute(command); + EnhancedRunnable enhanceTask = EnhancedRunnable.of(command, this); + AwareManager.execute(this, enhanceTask); + threadPerTaskExecutor.execute(enhanceTask); } public ExecutorService getThreadPerTaskExecutor() { diff --git a/dependencies/pom.xml b/dependencies/pom.xml index a93e668d3..af9e426e4 100644 --- a/dependencies/pom.xml +++ b/dependencies/pom.xml @@ -15,8 +15,8 @@ 1.1.9.1-3.x UTF-8 - 21 - 21 + 17 + 17 1.18.24 1.7.36 diff --git a/example/example-apollo/src/main/java/org/dromara/dynamictp/example/collector/EsCollector.java b/example/example-apollo/src/main/java/org/dromara/dynamictp/example/collector/EsCollector.java index 94d3b56d2..07c722ee3 100644 --- a/example/example-apollo/src/main/java/org/dromara/dynamictp/example/collector/EsCollector.java +++ b/example/example-apollo/src/main/java/org/dromara/dynamictp/example/collector/EsCollector.java @@ -17,8 +17,7 @@ package org.dromara.dynamictp.example.collector; -import org.dromara.dynamictp.common.entity.ThreadPoolStats; -import org.dromara.dynamictp.common.entity.VTExecutorStats; +import org.dromara.dynamictp.common.entity.ExecutorStats; import org.dromara.dynamictp.common.util.JsonUtil; import org.dromara.dynamictp.core.monitor.collector.AbstractCollector; @@ -37,14 +36,10 @@ public EsCollector() { } @Override - public void collect(ThreadPoolStats poolStats) { + public void collect(ExecutorStats poolStats) { esClient.save(JsonUtil.toJson(poolStats)); } - @Override - public void collect(VTExecutorStats vtExecutorStats) { - esClient.save(JsonUtil.toJson(vtExecutorStats)); - } @Override public String type() { return "es"; diff --git a/example/example-consul-cloud/src/main/java/org/dromara/dynamictp/example/collector/EsCollector.java b/example/example-consul-cloud/src/main/java/org/dromara/dynamictp/example/collector/EsCollector.java index fecc9f6a2..07c722ee3 100644 --- a/example/example-consul-cloud/src/main/java/org/dromara/dynamictp/example/collector/EsCollector.java +++ b/example/example-consul-cloud/src/main/java/org/dromara/dynamictp/example/collector/EsCollector.java @@ -17,8 +17,7 @@ package org.dromara.dynamictp.example.collector; -import org.dromara.dynamictp.common.entity.ThreadPoolStats; -import org.dromara.dynamictp.common.entity.VTExecutorStats; +import org.dromara.dynamictp.common.entity.ExecutorStats; import org.dromara.dynamictp.common.util.JsonUtil; import org.dromara.dynamictp.core.monitor.collector.AbstractCollector; @@ -37,15 +36,10 @@ public EsCollector() { } @Override - public void collect(ThreadPoolStats poolStats) { + public void collect(ExecutorStats poolStats) { esClient.save(JsonUtil.toJson(poolStats)); } - @Override - public void collect(VTExecutorStats vtExecutorStats) { - esClient.save(JsonUtil.toJson(vtExecutorStats)); - } - @Override public String type() { return "es"; diff --git a/example/example-etcd/src/main/java/org/dromara/dynamictp/example/collector/EsCollector.java b/example/example-etcd/src/main/java/org/dromara/dynamictp/example/collector/EsCollector.java index 94d3b56d2..07c722ee3 100644 --- a/example/example-etcd/src/main/java/org/dromara/dynamictp/example/collector/EsCollector.java +++ b/example/example-etcd/src/main/java/org/dromara/dynamictp/example/collector/EsCollector.java @@ -17,8 +17,7 @@ package org.dromara.dynamictp.example.collector; -import org.dromara.dynamictp.common.entity.ThreadPoolStats; -import org.dromara.dynamictp.common.entity.VTExecutorStats; +import org.dromara.dynamictp.common.entity.ExecutorStats; import org.dromara.dynamictp.common.util.JsonUtil; import org.dromara.dynamictp.core.monitor.collector.AbstractCollector; @@ -37,14 +36,10 @@ public EsCollector() { } @Override - public void collect(ThreadPoolStats poolStats) { + public void collect(ExecutorStats poolStats) { esClient.save(JsonUtil.toJson(poolStats)); } - @Override - public void collect(VTExecutorStats vtExecutorStats) { - esClient.save(JsonUtil.toJson(vtExecutorStats)); - } @Override public String type() { return "es"; diff --git a/example/example-huawei-cloud/src/main/java/org/dromara/dynamictp/example/collector/EsCollector.java b/example/example-huawei-cloud/src/main/java/org/dromara/dynamictp/example/collector/EsCollector.java index fecc9f6a2..07c722ee3 100644 --- a/example/example-huawei-cloud/src/main/java/org/dromara/dynamictp/example/collector/EsCollector.java +++ b/example/example-huawei-cloud/src/main/java/org/dromara/dynamictp/example/collector/EsCollector.java @@ -17,8 +17,7 @@ package org.dromara.dynamictp.example.collector; -import org.dromara.dynamictp.common.entity.ThreadPoolStats; -import org.dromara.dynamictp.common.entity.VTExecutorStats; +import org.dromara.dynamictp.common.entity.ExecutorStats; import org.dromara.dynamictp.common.util.JsonUtil; import org.dromara.dynamictp.core.monitor.collector.AbstractCollector; @@ -37,15 +36,10 @@ public EsCollector() { } @Override - public void collect(ThreadPoolStats poolStats) { + public void collect(ExecutorStats poolStats) { esClient.save(JsonUtil.toJson(poolStats)); } - @Override - public void collect(VTExecutorStats vtExecutorStats) { - esClient.save(JsonUtil.toJson(vtExecutorStats)); - } - @Override public String type() { return "es"; diff --git a/example/example-nacos-cloud/src/main/java/org/dromara/dynamictp/example/collector/EsCollector.java b/example/example-nacos-cloud/src/main/java/org/dromara/dynamictp/example/collector/EsCollector.java index fecc9f6a2..07c722ee3 100644 --- a/example/example-nacos-cloud/src/main/java/org/dromara/dynamictp/example/collector/EsCollector.java +++ b/example/example-nacos-cloud/src/main/java/org/dromara/dynamictp/example/collector/EsCollector.java @@ -17,8 +17,7 @@ package org.dromara.dynamictp.example.collector; -import org.dromara.dynamictp.common.entity.ThreadPoolStats; -import org.dromara.dynamictp.common.entity.VTExecutorStats; +import org.dromara.dynamictp.common.entity.ExecutorStats; import org.dromara.dynamictp.common.util.JsonUtil; import org.dromara.dynamictp.core.monitor.collector.AbstractCollector; @@ -37,15 +36,10 @@ public EsCollector() { } @Override - public void collect(ThreadPoolStats poolStats) { + public void collect(ExecutorStats poolStats) { esClient.save(JsonUtil.toJson(poolStats)); } - @Override - public void collect(VTExecutorStats vtExecutorStats) { - esClient.save(JsonUtil.toJson(vtExecutorStats)); - } - @Override public String type() { return "es"; diff --git a/example/example-nacos/src/main/java/org/dromara/dynamictp/example/collector/EsCollector.java b/example/example-nacos/src/main/java/org/dromara/dynamictp/example/collector/EsCollector.java index fecc9f6a2..07c722ee3 100644 --- a/example/example-nacos/src/main/java/org/dromara/dynamictp/example/collector/EsCollector.java +++ b/example/example-nacos/src/main/java/org/dromara/dynamictp/example/collector/EsCollector.java @@ -17,8 +17,7 @@ package org.dromara.dynamictp.example.collector; -import org.dromara.dynamictp.common.entity.ThreadPoolStats; -import org.dromara.dynamictp.common.entity.VTExecutorStats; +import org.dromara.dynamictp.common.entity.ExecutorStats; import org.dromara.dynamictp.common.util.JsonUtil; import org.dromara.dynamictp.core.monitor.collector.AbstractCollector; @@ -37,15 +36,10 @@ public EsCollector() { } @Override - public void collect(ThreadPoolStats poolStats) { + public void collect(ExecutorStats poolStats) { esClient.save(JsonUtil.toJson(poolStats)); } - @Override - public void collect(VTExecutorStats vtExecutorStats) { - esClient.save(JsonUtil.toJson(vtExecutorStats)); - } - @Override public String type() { return "es"; diff --git a/example/example-polaris-cloud/src/main/java/org/dromara/dynamictp/example/collector/EsCollector.java b/example/example-polaris-cloud/src/main/java/org/dromara/dynamictp/example/collector/EsCollector.java index fecc9f6a2..07c722ee3 100644 --- a/example/example-polaris-cloud/src/main/java/org/dromara/dynamictp/example/collector/EsCollector.java +++ b/example/example-polaris-cloud/src/main/java/org/dromara/dynamictp/example/collector/EsCollector.java @@ -17,8 +17,7 @@ package org.dromara.dynamictp.example.collector; -import org.dromara.dynamictp.common.entity.ThreadPoolStats; -import org.dromara.dynamictp.common.entity.VTExecutorStats; +import org.dromara.dynamictp.common.entity.ExecutorStats; import org.dromara.dynamictp.common.util.JsonUtil; import org.dromara.dynamictp.core.monitor.collector.AbstractCollector; @@ -37,15 +36,10 @@ public EsCollector() { } @Override - public void collect(ThreadPoolStats poolStats) { + public void collect(ExecutorStats poolStats) { esClient.save(JsonUtil.toJson(poolStats)); } - @Override - public void collect(VTExecutorStats vtExecutorStats) { - esClient.save(JsonUtil.toJson(vtExecutorStats)); - } - @Override public String type() { return "es"; diff --git a/example/example-zookeeper-cloud/src/main/java/org/dromara/dynamictp/example/collector/EsCollector.java b/example/example-zookeeper-cloud/src/main/java/org/dromara/dynamictp/example/collector/EsCollector.java index fecc9f6a2..07c722ee3 100644 --- a/example/example-zookeeper-cloud/src/main/java/org/dromara/dynamictp/example/collector/EsCollector.java +++ b/example/example-zookeeper-cloud/src/main/java/org/dromara/dynamictp/example/collector/EsCollector.java @@ -17,8 +17,7 @@ package org.dromara.dynamictp.example.collector; -import org.dromara.dynamictp.common.entity.ThreadPoolStats; -import org.dromara.dynamictp.common.entity.VTExecutorStats; +import org.dromara.dynamictp.common.entity.ExecutorStats; import org.dromara.dynamictp.common.util.JsonUtil; import org.dromara.dynamictp.core.monitor.collector.AbstractCollector; @@ -37,15 +36,10 @@ public EsCollector() { } @Override - public void collect(ThreadPoolStats poolStats) { + public void collect(ExecutorStats poolStats) { esClient.save(JsonUtil.toJson(poolStats)); } - @Override - public void collect(VTExecutorStats vtExecutorStats) { - esClient.save(JsonUtil.toJson(vtExecutorStats)); - } - @Override public String type() { return "es"; diff --git a/example/example-zookeeper/src/main/java/org/dromara/dynamictp/example/collector/EsCollector.java b/example/example-zookeeper/src/main/java/org/dromara/dynamictp/example/collector/EsCollector.java index fecc9f6a2..07c722ee3 100644 --- a/example/example-zookeeper/src/main/java/org/dromara/dynamictp/example/collector/EsCollector.java +++ b/example/example-zookeeper/src/main/java/org/dromara/dynamictp/example/collector/EsCollector.java @@ -17,8 +17,7 @@ package org.dromara.dynamictp.example.collector; -import org.dromara.dynamictp.common.entity.ThreadPoolStats; -import org.dromara.dynamictp.common.entity.VTExecutorStats; +import org.dromara.dynamictp.common.entity.ExecutorStats; import org.dromara.dynamictp.common.util.JsonUtil; import org.dromara.dynamictp.core.monitor.collector.AbstractCollector; @@ -37,15 +36,10 @@ public EsCollector() { } @Override - public void collect(ThreadPoolStats poolStats) { + public void collect(ExecutorStats poolStats) { esClient.save(JsonUtil.toJson(poolStats)); } - @Override - public void collect(VTExecutorStats vtExecutorStats) { - esClient.save(JsonUtil.toJson(vtExecutorStats)); - } - @Override public String type() { return "es"; diff --git a/spring/src/main/java/org/dromara/dynamictp/spring/annotation/DtpBeanDefinitionRegistrar.java b/spring/src/main/java/org/dromara/dynamictp/spring/annotation/DtpBeanDefinitionRegistrar.java index 25fe4f69d..6999d05e9 100644 --- a/spring/src/main/java/org/dromara/dynamictp/spring/annotation/DtpBeanDefinitionRegistrar.java +++ b/spring/src/main/java/org/dromara/dynamictp/spring/annotation/DtpBeanDefinitionRegistrar.java @@ -74,7 +74,7 @@ @Slf4j public class DtpBeanDefinitionRegistrar implements ImportBeanDefinitionRegistrar, EnvironmentAware { - private static final String JDK_VERSION_21 = "21"; + private static final Integer JDK_VERSION_21 = 21; private Environment environment; @@ -146,7 +146,12 @@ private Object[] buildConstructorArgs(Class clazz, DtpExecutorProps props) th } else if (clazz.equals(PriorityDtpExecutor.class)) { taskQueue = new PriorityBlockingQueue<>(props.getQueueCapacity(), PriorityDtpExecutor.getRunnableComparator()); } else if (clazz.equals(VirtualThreadExecutorProxy.class)) { - if (!VersionUtil.getVersion().equals(JDK_VERSION_21)) { + int jdkVersion = -1; + try { + jdkVersion = Integer.parseInt(VersionUtil.getVersion()); + } catch (NumberFormatException ignored) { + } + if (jdkVersion < JDK_VERSION_21) { log.warn("DynamicTp virtual thread executor {} register warn: update your JDK version or don't use virtual thread executor!", props.getThreadPoolName()); throw new UnsupportedOperationException(); } From 2f7a99deaf1929bbfecefa0bc75fa4b1fb7dee1e Mon Sep 17 00:00:00 2001 From: TheFatRatre <1981590178@qq.com> Date: Mon, 18 Nov 2024 13:18:35 +0800 Subject: [PATCH 26/26] format --- .../common/entity/ExecutorStats.java | 80 +++++++++++++++++-- .../dynamictp/common/entity/Metrics.java | 70 ---------------- 2 files changed, 75 insertions(+), 75 deletions(-) diff --git a/common/src/main/java/org/dromara/dynamictp/common/entity/ExecutorStats.java b/common/src/main/java/org/dromara/dynamictp/common/entity/ExecutorStats.java index a5001e0ba..d3ea9d9e1 100644 --- a/common/src/main/java/org/dromara/dynamictp/common/entity/ExecutorStats.java +++ b/common/src/main/java/org/dromara/dynamictp/common/entity/ExecutorStats.java @@ -53,11 +53,6 @@ public class ExecutorStats extends Metrics { @Deprecated private String poolAliasName; - /** - * 是否为虚拟线程执行器 - */ - private boolean isVirtualExecutor; - /** * 核心线程数 */ @@ -68,6 +63,31 @@ public class ExecutorStats extends Metrics { */ private int maximumPoolSize; + /** + * 正在执行任务的活跃线程大致总数 + */ + private int activeCount; + + /** + * 大致任务总数 + */ + private long taskCount; + + /** + * 执行超时任务数量 + */ + private long runTimeoutCount; + + /** + * 是否为DtpExecutor + */ + private boolean dynamic; + + /** + * 是否为虚拟线程执行器 + */ + private boolean isVirtualExecutor; + /** * 空闲时间 (ms) */ @@ -133,4 +153,54 @@ public class ExecutorStats extends Metrics { */ private long queueTimeoutCount; + /** + * tps + */ + private double tps; + + /** + * 最大任务耗时 + */ + private long maxRt; + + /** + * 最小任务耗时 + */ + private long minRt; + + /** + * 任务平均耗时(单位:ms) + */ + private double avg; + + /** + * 满足50%的任务执行所需的最低耗时 + */ + private double tp50; + + /** + * 满足75%的任务执行所需的最低耗时 + */ + private double tp75; + + /** + * 满足90%的任务执行所需的最低耗时 + */ + private double tp90; + + /** + * 满足95%的任务执行所需的最低耗时 + */ + private double tp95; + + /** + * 满足99%的任务执行所需的最低耗时 + */ + private double tp99; + + /** + * 满足99.9%的任务执行所需的最低耗时 + */ + private double tp999; + } diff --git a/common/src/main/java/org/dromara/dynamictp/common/entity/Metrics.java b/common/src/main/java/org/dromara/dynamictp/common/entity/Metrics.java index cd20c7713..c7209eae0 100644 --- a/common/src/main/java/org/dromara/dynamictp/common/entity/Metrics.java +++ b/common/src/main/java/org/dromara/dynamictp/common/entity/Metrics.java @@ -28,74 +28,4 @@ @Data public class Metrics { - /** - * 正在执行任务的活跃线程大致总数 - */ - private int activeCount; - - /** - * 大致任务总数 - */ - private long taskCount; - - /** - * 执行超时任务数量 - */ - private long runTimeoutCount; - - /** - * 是否为DtpExecutor - */ - private boolean dynamic; - - /** - * tps - */ - private double tps; - - /** - * 最大任务耗时 - */ - private long maxRt; - - /** - * 最小任务耗时 - */ - private long minRt; - - /** - * 任务平均耗时(单位:ms) - */ - private double avg; - - /** - * 满足50%的任务执行所需的最低耗时 - */ - private double tp50; - - /** - * 满足75%的任务执行所需的最低耗时 - */ - private double tp75; - - /** - * 满足90%的任务执行所需的最低耗时 - */ - private double tp90; - - /** - * 满足95%的任务执行所需的最低耗时 - */ - private double tp95; - - /** - * 满足99%的任务执行所需的最低耗时 - */ - private double tp99; - - /** - * 满足99.9%的任务执行所需的最低耗时 - */ - private double tp999; - }