mirror of https://github.com/longtai-cn/hippo4j
commit
7e1a2a6cb8
@ -0,0 +1,29 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||||
|
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
|
<parent>
|
||||||
|
<artifactId>hippo4j-agent-adapter-plugins</artifactId>
|
||||||
|
<groupId>cn.hippo4j</groupId>
|
||||||
|
<version>${revision}</version>
|
||||||
|
</parent>
|
||||||
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
|
||||||
|
<artifactId>dubbo-plugin</artifactId>
|
||||||
|
|
||||||
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.apache.dubbo</groupId>
|
||||||
|
<artifactId>dubbo</artifactId>
|
||||||
|
<scope>provided</scope>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>cn.hippo4j</groupId>
|
||||||
|
<artifactId>hippo4j-threadpool-infra-common</artifactId>
|
||||||
|
<version>${project.version}</version>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
|
||||||
|
|
||||||
|
</project>
|
@ -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<String, Object> executors = dataStore.get(poolKey);
|
||||||
|
executors.forEach((key, value) -> putHolder(mark() + key, (ThreadPoolExecutor) value));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
ExecutorRepository executorRepository = ExtensionLoader.getExtensionLoader(ExecutorRepository.class).getDefaultExtension();
|
||||||
|
ConcurrentMap<String, ConcurrentMap<Integer, ExecutorService>> data =
|
||||||
|
(ConcurrentMap<String, ConcurrentMap<Integer, ExecutorService>>) ReflectUtil.getFieldValue(executorRepository, "data");
|
||||||
|
ConcurrentMap<Integer, ExecutorService> 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";
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,110 @@
|
|||||||
|
/*
|
||||||
|
* 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.common.toolkit;
|
||||||
|
|
||||||
|
import cn.hippo4j.common.model.ThreadPoolRunStateInfo;
|
||||||
|
import cn.hippo4j.common.monitor.*;
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.junit.jupiter.api.Assertions;
|
||||||
|
|
||||||
|
import java.time.LocalDateTime;
|
||||||
|
import java.time.format.DateTimeFormatter;
|
||||||
|
import java.util.*;
|
||||||
|
|
||||||
|
/***
|
||||||
|
* @description : Todo
|
||||||
|
* @author : DDDreame
|
||||||
|
* @date : 2023/5/27 23:24
|
||||||
|
*/
|
||||||
|
public class MessageConvertTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testConvert() {
|
||||||
|
AbstractMessage message = new RuntimeMessage();
|
||||||
|
List<Message> runtimeMessages = new ArrayList<>();
|
||||||
|
ThreadPoolRunStateInfo poolRunState = ThreadPoolRunStateInfo.builder()
|
||||||
|
.tpId("testTPid")
|
||||||
|
.activeSize(4)
|
||||||
|
.poolSize(12)
|
||||||
|
.completedTaskCount(8L)
|
||||||
|
.largestPoolSize(12)
|
||||||
|
.currentLoad("6")
|
||||||
|
.clientLastRefreshTime(LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")))
|
||||||
|
.peakLoad("20")
|
||||||
|
.queueSize(0)
|
||||||
|
.queueRemainingCapacity(512)
|
||||||
|
.rejectCount(0L)
|
||||||
|
.timestamp(System.currentTimeMillis())
|
||||||
|
.build();
|
||||||
|
RuntimeMessage runtimeMessage = BeanUtil.convert(poolRunState, RuntimeMessage.class);
|
||||||
|
runtimeMessage.setGroupKey("test-groupKeys");
|
||||||
|
runtimeMessages.add(runtimeMessage);
|
||||||
|
|
||||||
|
message.setMessageType(MessageTypeEnum.RUNTIME);
|
||||||
|
message.setMessages(runtimeMessages);
|
||||||
|
MessageWrapper messageWrapper = MessageConvert.convert(message);
|
||||||
|
Assertions.assertNotNull(messageWrapper);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testMessageWrapperConvert() {
|
||||||
|
AbstractMessage message = new RuntimeMessage();
|
||||||
|
List<Message> runtimeMessages = new ArrayList<>();
|
||||||
|
ThreadPoolRunStateInfo poolRunState = ThreadPoolRunStateInfo.builder()
|
||||||
|
.tpId("testTPid")
|
||||||
|
.activeSize(4)
|
||||||
|
.poolSize(12)
|
||||||
|
.completedTaskCount(8L)
|
||||||
|
.largestPoolSize(12)
|
||||||
|
.currentLoad("6")
|
||||||
|
.clientLastRefreshTime(LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")))
|
||||||
|
.peakLoad("20")
|
||||||
|
.queueSize(0)
|
||||||
|
.queueRemainingCapacity(512)
|
||||||
|
.rejectCount(0L)
|
||||||
|
.timestamp(System.currentTimeMillis())
|
||||||
|
.build();
|
||||||
|
RuntimeMessage runtimeMessage = BeanUtil.convert(poolRunState, RuntimeMessage.class);
|
||||||
|
runtimeMessage.setGroupKey("test-groupKeys");
|
||||||
|
runtimeMessages.add(runtimeMessage);
|
||||||
|
|
||||||
|
message.setMessageType(MessageTypeEnum.RUNTIME);
|
||||||
|
message.setMessages(runtimeMessages);
|
||||||
|
MessageWrapper messageWrapper = MessageConvert.convert(message);
|
||||||
|
Message messageResult = MessageConvert.convert(messageWrapper);
|
||||||
|
Assertions.assertNotNull(messageResult);
|
||||||
|
Assertions.assertEquals(message, messageResult);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testMessageWrapperConvertException() {
|
||||||
|
Assertions.assertThrows(Exception.class, ()->{
|
||||||
|
Map<String, Object> data1 = new HashMap<>();
|
||||||
|
data1.put("key1", "value1");
|
||||||
|
data1.put("key2", 123);
|
||||||
|
Map<String, Object> data2 = new HashMap<>();
|
||||||
|
data2.put("key3", true);
|
||||||
|
data2.put("key4", 3.14);
|
||||||
|
List<Map<String, Object>> contentParams = Arrays.asList(data1, data2);
|
||||||
|
Class responseClass = String.class;
|
||||||
|
MessageTypeEnum messageType = MessageTypeEnum.DEFAULT;
|
||||||
|
MessageWrapper messageWrapper = new MessageWrapper(contentParams, responseClass, messageType);
|
||||||
|
MessageConvert.convert(messageWrapper);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in new issue