From 346725e8f78a7a86ee5d538932080be7db48a336 Mon Sep 17 00:00:00 2001 From: furaul Date: Sun, 28 May 2023 10:41:51 +0800 Subject: [PATCH] prototype version of dubbo threadpool agent. --- .../adapter-plugins/adapter-dubbo/pom.xml | 29 +++++++ .../adapter/dubbo/DubboThreadPoolAdapter.java | 87 +++++++++++++++++++ .../adapter-plugins/pom.xml | 5 ++ .../spring-boot-1x-plugin/pom.xml | 7 ++ .../EventPublishingFinishedInterceptor.java | 2 + 5 files changed, 130 insertions(+) create mode 100644 agent/hippo4j-agent-plugin/adapter-plugins/adapter-dubbo/pom.xml create mode 100644 agent/hippo4j-agent-plugin/adapter-plugins/adapter-dubbo/src/main/java/cn/hippo4j/agent/adapter/dubbo/DubboThreadPoolAdapter.java diff --git a/agent/hippo4j-agent-plugin/adapter-plugins/adapter-dubbo/pom.xml b/agent/hippo4j-agent-plugin/adapter-plugins/adapter-dubbo/pom.xml new file mode 100644 index 00000000..2b4dae8a --- /dev/null +++ b/agent/hippo4j-agent-plugin/adapter-plugins/adapter-dubbo/pom.xml @@ -0,0 +1,29 @@ + + + + hippo4j-all + cn.hippo4j + 2.0.0-SNAPSHOT + + 4.0.0 + + adapter-dubbo + + + + org.apache.dubbo + dubbo + provided + + + + cn.hippo4j + hippo4j-threadpool-infra-common + ${project.version} + + + + + \ No newline at end of file diff --git a/agent/hippo4j-agent-plugin/adapter-plugins/adapter-dubbo/src/main/java/cn/hippo4j/agent/adapter/dubbo/DubboThreadPoolAdapter.java b/agent/hippo4j-agent-plugin/adapter-plugins/adapter-dubbo/src/main/java/cn/hippo4j/agent/adapter/dubbo/DubboThreadPoolAdapter.java new file mode 100644 index 00000000..855972ea --- /dev/null +++ b/agent/hippo4j-agent-plugin/adapter-plugins/adapter-dubbo/src/main/java/cn/hippo4j/agent/adapter/dubbo/DubboThreadPoolAdapter.java @@ -0,0 +1,87 @@ +/* + * 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 cn.hippo4j.agent.adapter.dubbo; + +import cn.hippo4j.common.executor.ThreadPoolRegistry; +import cn.hippo4j.common.executor.support.BlockingQueueTypeEnum; +import cn.hippo4j.common.executor.support.RejectedPolicyTypeEnum; +import cn.hippo4j.common.model.executor.ExecutorProperties; +import cn.hippo4j.common.toolkit.BooleanUtil; +import cn.hippo4j.common.toolkit.ReflectUtil; +import java.util.Map; +import java.util.concurrent.ConcurrentMap; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.ThreadPoolExecutor; +import org.apache.dubbo.common.Version; +import org.apache.dubbo.common.extension.ExtensionLoader; +import org.apache.dubbo.common.store.DataStore; +import org.apache.dubbo.common.threadpool.manager.ExecutorRepository; + +/** + * Dubbo thread-pool adapter. + */ +public class DubboThreadPoolAdapter { + + public static void registerExecutors() { + boolean isLegacyVersion = true; + String poolKey = ExecutorService.class.getName(); + // Since 2.7.5, Dubbo has changed the way thread pools are used + // fixed https://github.com/opengoofy/hippo4j/issues/708 + try { + if (Version.getIntVersion(Version.getVersion()) < 2070500) { + isLegacyVersion = false; + } + } catch (Exception ex) { + } + + try { + if (isLegacyVersion) { + DataStore dataStore = ExtensionLoader.getExtensionLoader(DataStore.class).getDefaultExtension(); + Map executors = dataStore.get(poolKey); + executors.forEach((key, value) -> putHolder(mark() + key, (ThreadPoolExecutor) value)); + return; + } + ExecutorRepository executorRepository = ExtensionLoader.getExtensionLoader(ExecutorRepository.class).getDefaultExtension(); + ConcurrentMap> data = + (ConcurrentMap>) ReflectUtil.getFieldValue(executorRepository, "data"); + ConcurrentMap executorServiceMap = data.get(poolKey); + executorServiceMap.forEach((key, value) -> putHolder(mark() + key, (ThreadPoolExecutor) value)); + } catch (Exception ex) { + } + } + + private static void putHolder(String executorName, ThreadPoolExecutor executor) { + if (executor == null) { + return; + } + ExecutorProperties executorProperties = ExecutorProperties.builder() + .threadPoolId(executorName) + .corePoolSize(executor.getCorePoolSize()) + .maximumPoolSize(executor.getMaximumPoolSize()) + .allowCoreThreadTimeOut(BooleanUtil.toBoolean(String.valueOf(executor.allowsCoreThreadTimeOut()))) + .blockingQueue(BlockingQueueTypeEnum.getBlockingQueueTypeEnumByName(executor.getQueue().getClass().getSimpleName()).getName()) + .queueCapacity(executor.getQueue().remainingCapacity()) + .rejectedHandler(RejectedPolicyTypeEnum.getRejectedPolicyTypeEnumByName(executor.getRejectedExecutionHandler().getClass().getSimpleName()).getName()) + .build(); + ThreadPoolRegistry.putHolder(executorName, executor, executorProperties); + } + + public static String mark() { + return "Dubbo"; + } +} diff --git a/agent/hippo4j-agent-plugin/adapter-plugins/pom.xml b/agent/hippo4j-agent-plugin/adapter-plugins/pom.xml index 38bfb05c..b3b75d52 100644 --- a/agent/hippo4j-agent-plugin/adapter-plugins/pom.xml +++ b/agent/hippo4j-agent-plugin/adapter-plugins/pom.xml @@ -10,6 +10,7 @@ hippo4j-agent-adapter-plugins + pom 8 @@ -17,4 +18,8 @@ UTF-8 + + adapter-dubbo + + \ No newline at end of file diff --git a/agent/hippo4j-agent-plugin/spring-plugins/spring-boot-1x-plugin/pom.xml b/agent/hippo4j-agent-plugin/spring-plugins/spring-boot-1x-plugin/pom.xml index ccec3c7b..bfd36489 100644 --- a/agent/hippo4j-agent-plugin/spring-plugins/spring-boot-1x-plugin/pom.xml +++ b/agent/hippo4j-agent-plugin/spring-plugins/spring-boot-1x-plugin/pom.xml @@ -49,6 +49,13 @@ ${project.version} provided + + + cn.hippo4j + adapter-dubbo + ${project.version} + + \ No newline at end of file diff --git a/agent/hippo4j-agent-plugin/spring-plugins/spring-boot-1x-plugin/src/main/java/cn/hippo4j/agent/plugin/spring/boot/v1/interceptor/EventPublishingFinishedInterceptor.java b/agent/hippo4j-agent-plugin/spring-plugins/spring-boot-1x-plugin/src/main/java/cn/hippo4j/agent/plugin/spring/boot/v1/interceptor/EventPublishingFinishedInterceptor.java index 93b7d1ef..777194f0 100644 --- a/agent/hippo4j-agent-plugin/spring-plugins/spring-boot-1x-plugin/src/main/java/cn/hippo4j/agent/plugin/spring/boot/v1/interceptor/EventPublishingFinishedInterceptor.java +++ b/agent/hippo4j-agent-plugin/spring-plugins/spring-boot-1x-plugin/src/main/java/cn/hippo4j/agent/plugin/spring/boot/v1/interceptor/EventPublishingFinishedInterceptor.java @@ -17,6 +17,7 @@ package cn.hippo4j.agent.plugin.spring.boot.v1.interceptor; +import cn.hippo4j.agent.adapter.dubbo.DubboThreadPoolAdapter; import cn.hippo4j.agent.core.logging.api.ILog; import cn.hippo4j.agent.core.logging.api.LogManager; import cn.hippo4j.agent.core.plugin.interceptor.enhance.EnhancedInstance; @@ -56,6 +57,7 @@ public class EventPublishingFinishedInterceptor implements InstanceMethodsAround SpringPropertiesLoader.loadSpringProperties(context.getEnvironment()); ThreadPoolDynamicRefresh dynamicRefreshSpring1x = new DynamicThreadPoolChangeHandlerSpring1x(context); dynamicRefreshSpring1x.registerListener(); + DubboThreadPoolAdapter.registerExecutors(); return ret; }