From dd4e2336bf6bca3823dce72bbe924f3851a4007f Mon Sep 17 00:00:00 2001 From: "chen.ma" Date: Fri, 20 May 2022 20:53:11 +0800 Subject: [PATCH] Hippo4j core add framework thread pool adaptation --- .../hippo4j-core-spring-boot-starter/pom.xml | 8 ++- .../config/AdapterExecutorProperties.java | 30 +++++++++++ .../config/BootstrapCoreProperties.java | 5 ++ ...ynamicThreadPoolCoreAutoConfiguration.java | 12 +++++ .../event/AdapterExecutorsListener.java | 52 +++++++++++++++++++ .../Hippo4jCoreDynamicRefreshEventOrder.java | 2 + .../support/ThreadPoolAdapterRegister.java | 39 ++++++++++++++ 7 files changed, 147 insertions(+), 1 deletion(-) create mode 100644 hippo4j-spring-boot/hippo4j-core-spring-boot-starter/src/main/java/cn/hippo4j/core/springboot/starter/config/AdapterExecutorProperties.java create mode 100644 hippo4j-spring-boot/hippo4j-core-spring-boot-starter/src/main/java/cn/hippo4j/core/springboot/starter/refresher/event/AdapterExecutorsListener.java create mode 100644 hippo4j-spring-boot/hippo4j-core-spring-boot-starter/src/main/java/cn/hippo4j/core/springboot/starter/support/ThreadPoolAdapterRegister.java diff --git a/hippo4j-spring-boot/hippo4j-core-spring-boot-starter/pom.xml b/hippo4j-spring-boot/hippo4j-core-spring-boot-starter/pom.xml index c87b0388..afe2f48f 100644 --- a/hippo4j-spring-boot/hippo4j-core-spring-boot-starter/pom.xml +++ b/hippo4j-spring-boot/hippo4j-core-spring-boot-starter/pom.xml @@ -12,7 +12,8 @@ jar ${project.artifactId} - Thread pool dynamic parameter adjustment, alarming, status viewing and monitoring functions + Thread pool dynamic parameter adjustment, alarming, status viewing and monitoring functions + @@ -85,6 +86,11 @@ 1.8.4 true + + + cn.hippo4j + hippo4j-adapter-base + diff --git a/hippo4j-spring-boot/hippo4j-core-spring-boot-starter/src/main/java/cn/hippo4j/core/springboot/starter/config/AdapterExecutorProperties.java b/hippo4j-spring-boot/hippo4j-core-spring-boot-starter/src/main/java/cn/hippo4j/core/springboot/starter/config/AdapterExecutorProperties.java new file mode 100644 index 00000000..9834de88 --- /dev/null +++ b/hippo4j-spring-boot/hippo4j-core-spring-boot-starter/src/main/java/cn/hippo4j/core/springboot/starter/config/AdapterExecutorProperties.java @@ -0,0 +1,30 @@ +package cn.hippo4j.core.springboot.starter.config; + +import lombok.Data; + +/** + * Adapter executor properties. + */ +@Data +public class AdapterExecutorProperties { + + /** + * Mark + */ + private String mark; + + /** + * Thread-pool key + */ + private String threadPoolKey; + + /** + * Core pool size + */ + private Integer corePoolSize; + + /** + * Maximum pool size + */ + private Integer maximumPoolSize; +} diff --git a/hippo4j-spring-boot/hippo4j-core-spring-boot-starter/src/main/java/cn/hippo4j/core/springboot/starter/config/BootstrapCoreProperties.java b/hippo4j-spring-boot/hippo4j-core-spring-boot-starter/src/main/java/cn/hippo4j/core/springboot/starter/config/BootstrapCoreProperties.java index f0245339..d93e8f4d 100644 --- a/hippo4j-spring-boot/hippo4j-core-spring-boot-starter/src/main/java/cn/hippo4j/core/springboot/starter/config/BootstrapCoreProperties.java +++ b/hippo4j-spring-boot/hippo4j-core-spring-boot-starter/src/main/java/cn/hippo4j/core/springboot/starter/config/BootstrapCoreProperties.java @@ -146,4 +146,9 @@ public class BootstrapCoreProperties implements BootstrapPropertiesInterface { * Executors. */ private List executors; + + /** + * Adapter executors + */ + private List adapterExecutors; } diff --git a/hippo4j-spring-boot/hippo4j-core-spring-boot-starter/src/main/java/cn/hippo4j/core/springboot/starter/config/DynamicThreadPoolCoreAutoConfiguration.java b/hippo4j-spring-boot/hippo4j-core-spring-boot-starter/src/main/java/cn/hippo4j/core/springboot/starter/config/DynamicThreadPoolCoreAutoConfiguration.java index d5c729d0..0a6dee9a 100644 --- a/hippo4j-spring-boot/hippo4j-core-spring-boot-starter/src/main/java/cn/hippo4j/core/springboot/starter/config/DynamicThreadPoolCoreAutoConfiguration.java +++ b/hippo4j-spring-boot/hippo4j-core-spring-boot-starter/src/main/java/cn/hippo4j/core/springboot/starter/config/DynamicThreadPoolCoreAutoConfiguration.java @@ -39,10 +39,12 @@ import cn.hippo4j.core.springboot.starter.refresher.ApolloRefresherHandler; import cn.hippo4j.core.springboot.starter.refresher.NacosCloudRefresherHandler; import cn.hippo4j.core.springboot.starter.refresher.NacosRefresherHandler; import cn.hippo4j.core.springboot.starter.refresher.ZookeeperRefresherHandler; +import cn.hippo4j.core.springboot.starter.refresher.event.AdapterExecutorsListener; import cn.hippo4j.core.springboot.starter.refresher.event.ExecutorsListener; import cn.hippo4j.core.springboot.starter.refresher.event.PlatformsListener; import cn.hippo4j.core.springboot.starter.refresher.event.WebExecutorListener; import cn.hippo4j.core.springboot.starter.support.DynamicThreadPoolPostProcessor; +import cn.hippo4j.core.springboot.starter.support.ThreadPoolAdapterRegister; import lombok.AllArgsConstructor; import org.springframework.boot.autoconfigure.ImportAutoConfiguration; import org.springframework.boot.autoconfigure.condition.*; @@ -173,6 +175,11 @@ public class DynamicThreadPoolCoreAutoConfiguration { return new ExecutorsListener(threadPoolNotifyAlarmHandler); } + @Bean + public AdapterExecutorsListener hippo4jAdapterExecutorsListener() { + return new AdapterExecutorsListener(); + } + @Bean public PlatformsListener hippo4jPlatformsListener() { return new PlatformsListener(); @@ -182,4 +189,9 @@ public class DynamicThreadPoolCoreAutoConfiguration { public WebExecutorListener hippo4jWebExecutorListener() { return new WebExecutorListener(); } + + @Bean + public ThreadPoolAdapterRegister threadPoolAdapterRegister() { + return new ThreadPoolAdapterRegister(bootstrapCoreProperties); + } } diff --git a/hippo4j-spring-boot/hippo4j-core-spring-boot-starter/src/main/java/cn/hippo4j/core/springboot/starter/refresher/event/AdapterExecutorsListener.java b/hippo4j-spring-boot/hippo4j-core-spring-boot-starter/src/main/java/cn/hippo4j/core/springboot/starter/refresher/event/AdapterExecutorsListener.java new file mode 100644 index 00000000..60aaf4f2 --- /dev/null +++ b/hippo4j-spring-boot/hippo4j-core-spring-boot-starter/src/main/java/cn/hippo4j/core/springboot/starter/refresher/event/AdapterExecutorsListener.java @@ -0,0 +1,52 @@ +package cn.hippo4j.core.springboot.starter.refresher.event; + +import cn.hippo4j.adapter.base.ThreadPoolAdapter; +import cn.hippo4j.adapter.base.ThreadPoolAdapterParameter; +import cn.hippo4j.common.config.ApplicationContextHolder; +import cn.hippo4j.common.toolkit.CollectionUtil; +import cn.hippo4j.core.springboot.starter.config.AdapterExecutorProperties; +import cn.hutool.core.bean.BeanUtil; +import lombok.extern.slf4j.Slf4j; +import org.springframework.context.ApplicationListener; +import org.springframework.core.annotation.Order; + +import java.util.List; +import java.util.Map; +import java.util.Objects; + +import static cn.hippo4j.common.constant.Constants.IDENTIFY_SLICER_SYMBOL; +import static cn.hippo4j.core.springboot.starter.refresher.event.Hippo4jCoreDynamicRefreshEventOrder.ADAPTER_EXECUTORS_LISTENER; +import static cn.hippo4j.core.springboot.starter.support.ThreadPoolAdapterRegister.ADAPTER_EXECUTORS_MAP; + +/** + * Adapter executors listener. + */ +@Slf4j +@Order(ADAPTER_EXECUTORS_LISTENER) +public class AdapterExecutorsListener implements ApplicationListener { + + @Override + public void onApplicationEvent(Hippo4jCoreDynamicRefreshEvent event) { + List adapterExecutors; + Map threadPoolAdapterMap = ApplicationContextHolder.getBeansOfType(ThreadPoolAdapter.class); + if (CollectionUtil.isEmpty(adapterExecutors = event.getBootstrapCoreProperties().getAdapterExecutors()) || CollectionUtil.isEmpty(threadPoolAdapterMap)) { + return; + } + for (AdapterExecutorProperties each : adapterExecutors) { + String buildKey = each.getMark() + IDENTIFY_SLICER_SYMBOL + each.getThreadPoolKey(); + AdapterExecutorProperties adapterExecutorProperties = ADAPTER_EXECUTORS_MAP.get(buildKey); + if (adapterExecutorProperties == null) { + continue; + } + if (!Objects.equals(adapterExecutorProperties.getCorePoolSize(), each.getCorePoolSize()) + || !Objects.equals(adapterExecutorProperties.getMaximumPoolSize(), each.getMaximumPoolSize())) { + threadPoolAdapterMap.forEach((key, val) -> { + if (Objects.equals(val.mark(), each.getMark())) { + val.updateThreadPool(BeanUtil.toBean(each, ThreadPoolAdapterParameter.class)); + ADAPTER_EXECUTORS_MAP.put(buildKey, each); + } + }); + } + } + } +} diff --git a/hippo4j-spring-boot/hippo4j-core-spring-boot-starter/src/main/java/cn/hippo4j/core/springboot/starter/refresher/event/Hippo4jCoreDynamicRefreshEventOrder.java b/hippo4j-spring-boot/hippo4j-core-spring-boot-starter/src/main/java/cn/hippo4j/core/springboot/starter/refresher/event/Hippo4jCoreDynamicRefreshEventOrder.java index 09961c9d..9766c90d 100644 --- a/hippo4j-spring-boot/hippo4j-core-spring-boot-starter/src/main/java/cn/hippo4j/core/springboot/starter/refresher/event/Hippo4jCoreDynamicRefreshEventOrder.java +++ b/hippo4j-spring-boot/hippo4j-core-spring-boot-starter/src/main/java/cn/hippo4j/core/springboot/starter/refresher/event/Hippo4jCoreDynamicRefreshEventOrder.java @@ -27,4 +27,6 @@ public interface Hippo4jCoreDynamicRefreshEventOrder { int PLATFORMS_LISTENER = 1; int EXECUTORS_LISTENER = 2; + + int ADAPTER_EXECUTORS_LISTENER = 3; } diff --git a/hippo4j-spring-boot/hippo4j-core-spring-boot-starter/src/main/java/cn/hippo4j/core/springboot/starter/support/ThreadPoolAdapterRegister.java b/hippo4j-spring-boot/hippo4j-core-spring-boot-starter/src/main/java/cn/hippo4j/core/springboot/starter/support/ThreadPoolAdapterRegister.java new file mode 100644 index 00000000..2414f8b8 --- /dev/null +++ b/hippo4j-spring-boot/hippo4j-core-spring-boot-starter/src/main/java/cn/hippo4j/core/springboot/starter/support/ThreadPoolAdapterRegister.java @@ -0,0 +1,39 @@ +package cn.hippo4j.core.springboot.starter.support; + +import cn.hippo4j.common.toolkit.CollectionUtil; +import cn.hippo4j.core.springboot.starter.config.AdapterExecutorProperties; +import cn.hippo4j.core.springboot.starter.config.BootstrapCoreProperties; +import com.google.common.collect.Maps; +import lombok.AllArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.springframework.boot.ApplicationArguments; +import org.springframework.boot.ApplicationRunner; + +import java.util.List; +import java.util.Map; + +import static cn.hippo4j.common.constant.Constants.IDENTIFY_SLICER_SYMBOL; + +/** + * Thread-pool adapter register. + */ +@Slf4j +@AllArgsConstructor +public class ThreadPoolAdapterRegister implements ApplicationRunner { + + private final BootstrapCoreProperties bootstrapCoreProperties; + + public static final Map ADAPTER_EXECUTORS_MAP = Maps.newConcurrentMap(); + + @Override + public void run(ApplicationArguments args) throws Exception { + List adapterExecutors; + if (CollectionUtil.isEmpty(adapterExecutors = bootstrapCoreProperties.getAdapterExecutors())) { + return; + } + for (AdapterExecutorProperties each : adapterExecutors) { + String buildKey = each.getMark() + IDENTIFY_SLICER_SYMBOL + each.getThreadPoolKey(); + ADAPTER_EXECUTORS_MAP.put(buildKey, each); + } + } +}