Merge branch 'develop' into feature/console_monitor

pull/1491/head
GRL-bxy 2 years ago committed by GitHub
commit 1c5331ab38
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -24,6 +24,8 @@ import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor; import lombok.NoArgsConstructor;
import org.junit.Assert; import org.junit.Assert;
import org.junit.Test; import org.junit.Test;
import org.json.JSONException;
import org.skyscreamer.jsonassert.JSONAssert;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collections; import java.util.Collections;
@ -42,7 +44,11 @@ public class JSONUtilTest {
@Test @Test
public void assertToJSONString() { public void assertToJSONString() {
Assert.assertNull(JSONUtil.toJSONString(null)); Assert.assertNull(JSONUtil.toJSONString(null));
Assert.assertEquals(EXPECTED_FOO_JSON, JSONUtil.toJSONString(EXPECTED_FOO)); try {
JSONAssert.assertEquals(EXPECTED_FOO_JSON, JSONUtil.toJSONString(EXPECTED_FOO), false);
} catch (JSONException jse) {
throw new RuntimeException(jse);
}
} }
@Test @Test

@ -19,12 +19,16 @@ package cn.hippo4j.common.toolkit;
import cn.hippo4j.common.model.ThreadPoolParameterInfo; import cn.hippo4j.common.model.ThreadPoolParameterInfo;
import org.junit.Test; import org.junit.Test;
import org.mockito.MockedStatic;
import java.io.IOException; import java.io.IOException;
import java.security.NoSuchAlgorithmException; import java.security.NoSuchAlgorithmException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.mockStatic;
public class Md5UtilTest { public class Md5UtilTest {
@Test @Test
@ -50,12 +54,15 @@ public class Md5UtilTest {
@Test @Test
public void assetGetTpContentMd5() { public void assetGetTpContentMd5() {
String md5Result = "ef5ea7cb47377fb9fb85a7125e76715d"; final ThreadPoolParameterInfo threadPoolParameterInfo = new ThreadPoolParameterInfo();
ThreadPoolParameterInfo threadPoolParameterInfo = ThreadPoolParameterInfo.builder().tenantId("prescription") final String mockContent = "mockContent";
.itemId("dynamic-threadpool-example").tpId("message-consume").content("描述信息").corePoolSize(1) final String mockContentMd5 = "34cf17bc632ece6e4c81a4ce8aa97d5e";
.maximumPoolSize(2).queueType(1).capacity(4).keepAliveTime(513L).executeTimeOut(null).rejectedType(4) try (final MockedStatic<ContentUtil> mockedContentUtil = mockStatic(ContentUtil.class)) {
.isAlarm(1).capacityAlarm(80).livenessAlarm(80).allowCoreThreadTimeOut(1).build(); mockedContentUtil.when(() -> ContentUtil.getPoolContent(threadPoolParameterInfo)).thenReturn(mockContent);
Assert.isTrue(md5Result.equals(Md5Util.getTpContentMd5(threadPoolParameterInfo))); final String result = Md5Util.getTpContentMd5(threadPoolParameterInfo);
Assert.isTrue(result.equals(mockContentMd5));
mockedContentUtil.verify(() -> ContentUtil.getPoolContent(threadPoolParameterInfo), times(1));
}
} }
@Test @Test

@ -37,7 +37,7 @@ import java.nio.file.Files;
import java.nio.file.Path; import java.nio.file.Path;
import java.nio.file.SimpleFileVisitor; import java.nio.file.SimpleFileVisitor;
import java.nio.file.attribute.BasicFileAttributes; import java.nio.file.attribute.BasicFileAttributes;
import java.util.HashMap; import java.util.LinkedHashMap;
import java.util.Map; import java.util.Map;
public class HttpUtilsTest { public class HttpUtilsTest {
@ -169,7 +169,7 @@ public class HttpUtilsTest {
@Test @Test
public void buildUrl() { public void buildUrl() {
Map<String, String> map = new HashMap<>(); Map<String, String> map = new LinkedHashMap<>();
map.put(password, passwordValue); map.put(password, passwordValue);
map.put(username, usernameValue); map.put(username, usernameValue);
String s = HttpUtil.buildUrl(url + PORT, map); String s = HttpUtil.buildUrl(url + PORT, map);

@ -73,7 +73,8 @@ public class LogMessageTest {
public void testKvShouldPutAllKeyAndValuePairs() { public void testKvShouldPutAllKeyAndValuePairs() {
logMessage.kv("key1", "value1"); logMessage.kv("key1", "value1");
logMessage.kv("key2", "value2"); logMessage.kv("key2", "value2");
assertEquals("key1=value1||key2=value2", logMessage.toString()); String output = logMessage.toString();
assertTrue(output.equals("key1=value1||key2=value2") || output.equals("key2=value2||key1=value1"));
} }
@Test @Test

@ -101,4 +101,11 @@ public interface BootstrapPropertiesInterface {
return null; return null;
} }
/**
* Get Polaris.
*/
default Map<String, Object> getPolaris() {
return null;
}
} }

@ -76,6 +76,11 @@ public class BootstrapConfigProperties implements BootstrapPropertiesInterface {
*/ */
private Map<String, String> etcd; private Map<String, String> etcd;
/**
* polaris config
*/
private Map<String, Object> polaris;
/** /**
* Web config * Web config
* *

@ -66,7 +66,7 @@ public class WeChatSendMessageHandler extends AbstractRobotSendMessageHandler {
weChatReq.setMarkdown(markdown); weChatReq.setMarkdown(markdown);
String responseBody = HttpUtil.post(serverUrl, weChatReq); String responseBody = HttpUtil.post(serverUrl, weChatReq);
WeChatRobotResponse response = JSONUtil.parseObject(responseBody, WeChatRobotResponse.class); WeChatRobotResponse response = JSONUtil.parseObject(responseBody, WeChatRobotResponse.class);
Assert.isTrue(response != null, "Response is null."); Assert.notNull(response, "Response is null.");
if (response.getErrcode() != 0) { if (response.getErrcode() != 0) {
log.error("WeChat failed to send message, reason : {}", response.errmsg); log.error("WeChat failed to send message, reason : {}", response.errmsg);
} }

@ -71,4 +71,5 @@ public class PolarisRefresherHandler extends AbstractConfigThreadPoolDynamicRefr
return Objects.equals(POLARIS_FILE_TYPE, "yaml") ? configFileService.getConfigYamlFile(namespace, fileGroup, fileName) return Objects.equals(POLARIS_FILE_TYPE, "yaml") ? configFileService.getConfigYamlFile(namespace, fileGroup, fileName)
: configFileService.getConfigPropertiesFile(namespace, fileGroup, fileName); : configFileService.getConfigPropertiesFile(namespace, fileGroup, fileName);
} }
} }

@ -23,6 +23,7 @@ import cn.hippo4j.core.config.ConfigEmptyException;
import cn.hippo4j.threadpool.dynamic.api.BootstrapPropertiesInterface; import cn.hippo4j.threadpool.dynamic.api.BootstrapPropertiesInterface;
import lombok.AllArgsConstructor; import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.ConfigurableEnvironment; import org.springframework.core.env.ConfigurableEnvironment;
@ -144,6 +145,39 @@ public class BeforeCheckConfiguration {
} }
} }
Map<String, Object> polaris = properties.getPolaris();
if (MapUtil.isNotEmpty(polaris)) {
String namespace = polaris.get("namespace").toString();
if (StringUtil.isBlank(namespace)) {
throw new ConfigEmptyException(
"Web server maybe fail to start. The dynamic thread pool polaris namespace is empty.",
"Please check whether the [spring.dynamic.thread-pool.polaris.namespace] configuration is empty or an empty string.");
}
if (!(polaris.get("file") instanceof Map)) {
throw new ConfigEmptyException(
"Web server maybe fail to start. Lack of the dynamic thread pool polaris file configuration.",
"Please check whether the [spring.dynamic.thread-pool.polaris.file.*] configuration is complete.");
}
Map<String, String> polarisFile = (Map<String, String>) polaris.get("file");
String fileGroup = polarisFile.get("group");
if (StringUtil.isBlank(fileGroup)) {
throw new ConfigEmptyException(
"Web server maybe fail to start. The dynamic thread pool polaris file group is empty.",
"Please check whether the [spring.dynamic.thread-pool.polaris.file.group] configuration is empty or an empty string.");
}
String fileName = polarisFile.get("name");
if (StringUtil.isBlank(fileName)) {
throw new ConfigEmptyException(
"Web server maybe fail to start. The dynamic thread pool polaris file name is empty.",
"Please check whether the [spring.dynamic.thread-pool.polaris.file.name] configuration is empty or an empty string.");
}
String fileType = polarisFile.get("type");
if (StringUtil.isBlank(fileType)) {
throw new ConfigEmptyException(
"Web server maybe fail to start. The dynamic thread pool polaris file type is empty.",
"Please check whether the [spring.dynamic.thread-pool.polaris.file.type] configuration is empty or an empty string.");
}
}
break; break;
} }
default: default:

@ -18,6 +18,7 @@
package cn.hippo4j.config.model.biz.monitor; package cn.hippo4j.config.model.biz.monitor;
import lombok.AllArgsConstructor; import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data; import lombok.Data;
import lombok.NoArgsConstructor; import lombok.NoArgsConstructor;
@ -27,6 +28,7 @@ import java.util.List;
* Monitor active resp dto. * Monitor active resp dto.
*/ */
@Data @Data
@Builder
@NoArgsConstructor @NoArgsConstructor
@AllArgsConstructor @AllArgsConstructor
public class MonitorActiveRespDTO { public class MonitorActiveRespDTO {
@ -51,11 +53,21 @@ public class MonitorActiveRespDTO {
*/ */
private List<Long> queueSizeList; private List<Long> queueSizeList;
/**
* Range completed task count list
*/
private List<Long> rangeCompletedTaskCountList;
/** /**
* Completed task count list * Completed task count list
*/ */
private List<Long> completedTaskCountList; private List<Long> completedTaskCountList;
/**
* Range reject count list
*/
private List<Long> rangeRejectCountList;
/** /**
* Reject count list * Reject count list
*/ */
@ -66,11 +78,6 @@ public class MonitorActiveRespDTO {
*/ */
private List<Long> queueRemainingCapacityList; private List<Long> queueRemainingCapacityList;
/**
* Current load list
*/
private List<Long> currentLoadList;
/** /**
* Queue capacity list * Queue capacity list
*/ */

@ -44,4 +44,14 @@ public class MonitorQueryReqDTO {
* Instance id * Instance id
*/ */
private String instanceId; private String instanceId;
/**
* Start time
*/
private Long startTime;
/**
* End time
*/
private Long endTime;
} }

@ -17,13 +17,14 @@
package cn.hippo4j.config.service.biz.impl; package cn.hippo4j.config.service.biz.impl;
import cn.hippo4j.common.model.Result;
import cn.hippo4j.common.monitor.Message; import cn.hippo4j.common.monitor.Message;
import cn.hippo4j.common.monitor.MessageWrapper; import cn.hippo4j.common.monitor.MessageWrapper;
import cn.hippo4j.common.monitor.RuntimeMessage; import cn.hippo4j.common.monitor.RuntimeMessage;
import cn.hippo4j.common.toolkit.BeanUtil;
import cn.hippo4j.common.toolkit.DateUtil; import cn.hippo4j.common.toolkit.DateUtil;
import cn.hippo4j.common.toolkit.GroupKey; import cn.hippo4j.common.toolkit.GroupKey;
import cn.hippo4j.common.toolkit.MessageConvert; import cn.hippo4j.common.toolkit.MessageConvert;
import cn.hippo4j.common.model.Result;
import cn.hippo4j.config.config.ServerBootstrapProperties; import cn.hippo4j.config.config.ServerBootstrapProperties;
import cn.hippo4j.config.mapper.HisRunDataMapper; import cn.hippo4j.config.mapper.HisRunDataMapper;
import cn.hippo4j.config.model.HisRunDataInfo; import cn.hippo4j.config.model.HisRunDataInfo;
@ -33,7 +34,6 @@ import cn.hippo4j.config.model.biz.monitor.MonitorRespDTO;
import cn.hippo4j.config.monitor.QueryMonitorExecuteChoose; import cn.hippo4j.config.monitor.QueryMonitorExecuteChoose;
import cn.hippo4j.config.service.ConfigCacheService; import cn.hippo4j.config.service.ConfigCacheService;
import cn.hippo4j.config.service.biz.HisRunDataService; import cn.hippo4j.config.service.biz.HisRunDataService;
import cn.hippo4j.common.toolkit.BeanUtil;
import cn.hippo4j.server.common.base.Results; import cn.hippo4j.server.common.base.Results;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.AllArgsConstructor; import lombok.AllArgsConstructor;
@ -45,7 +45,6 @@ import java.time.LocalDateTime;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Date; import java.util.Date;
import java.util.List; import java.util.List;
import java.util.concurrent.atomic.AtomicBoolean;
import static cn.hippo4j.common.constant.MagicNumberConstants.INDEX_0; import static cn.hippo4j.common.constant.MagicNumberConstants.INDEX_0;
import static cn.hippo4j.common.constant.MagicNumberConstants.INDEX_1; import static cn.hippo4j.common.constant.MagicNumberConstants.INDEX_1;
@ -84,15 +83,20 @@ public class HisRunDataServiceImpl extends ServiceImpl<HisRunDataMapper, HisRunD
@Override @Override
public MonitorActiveRespDTO queryInfoThreadPoolMonitor(MonitorQueryReqDTO reqDTO) { public MonitorActiveRespDTO queryInfoThreadPoolMonitor(MonitorQueryReqDTO reqDTO) {
Long startTime = reqDTO.getStartTime();
Long endTime = reqDTO.getEndTime();
if (startTime == null || endTime == null) {
LocalDateTime currentDate = LocalDateTime.now(); LocalDateTime currentDate = LocalDateTime.now();
LocalDateTime dateTime = currentDate.plusMinutes(-properties.getCleanHistoryDataPeriod()); LocalDateTime dateTime = currentDate.plusMinutes(-properties.getCleanHistoryDataPeriod());
long startTime = DateUtil.getTime(dateTime); startTime = DateUtil.getTime(dateTime);
endTime = DateUtil.getTime(currentDate);
}
List<HisRunDataInfo> hisRunDataInfos = this.lambdaQuery() List<HisRunDataInfo> hisRunDataInfos = this.lambdaQuery()
.eq(HisRunDataInfo::getTenantId, reqDTO.getTenantId()) .eq(HisRunDataInfo::getTenantId, reqDTO.getTenantId())
.eq(HisRunDataInfo::getItemId, reqDTO.getItemId()) .eq(HisRunDataInfo::getItemId, reqDTO.getItemId())
.eq(HisRunDataInfo::getTpId, reqDTO.getTpId()) .eq(HisRunDataInfo::getTpId, reqDTO.getTpId())
.eq(HisRunDataInfo::getInstanceId, reqDTO.getInstanceId()) .eq(HisRunDataInfo::getInstanceId, reqDTO.getInstanceId())
.between(HisRunDataInfo::getTimestamp, startTime, DateUtil.getTime(currentDate)) .between(HisRunDataInfo::getTimestamp, startTime, endTime)
.orderByAsc(HisRunDataInfo::getTimestamp) .orderByAsc(HisRunDataInfo::getTimestamp)
.list(); .list();
List<String> times = new ArrayList<>(); List<String> times = new ArrayList<>();
@ -100,34 +104,48 @@ public class HisRunDataServiceImpl extends ServiceImpl<HisRunDataMapper, HisRunD
List<Long> activeSizeList = new ArrayList<>(); List<Long> activeSizeList = new ArrayList<>();
List<Long> queueCapacityList = new ArrayList<>(); List<Long> queueCapacityList = new ArrayList<>();
List<Long> queueSizeList = new ArrayList<>(); List<Long> queueSizeList = new ArrayList<>();
List<Long> rangeCompletedTaskCountList = new ArrayList<>();
List<Long> completedTaskCountList = new ArrayList<>(); List<Long> completedTaskCountList = new ArrayList<>();
List<Long> rangeRejectCountList = new ArrayList<>();
List<Long> rejectCountList = new ArrayList<>(); List<Long> rejectCountList = new ArrayList<>();
List<Long> queueRemainingCapacityList = new ArrayList<>(); List<Long> queueRemainingCapacityList = new ArrayList<>();
List<Long> currentLoadList = new ArrayList<>(); long completedTaskCountTemp = 0L;
long countTemp = 0L; long rejectCountTemp = 0L;
AtomicBoolean firstFlag = new AtomicBoolean(Boolean.TRUE); boolean firstFlag = true;
for (HisRunDataInfo each : hisRunDataInfos) { for (HisRunDataInfo each : hisRunDataInfos) {
String time = DateUtil.format(new Date(each.getTimestamp()), NORM_TIME_PATTERN); String time = DateUtil.format(new Date(each.getTimestamp()), NORM_TIME_PATTERN);
times.add(time); times.add(time);
poolSizeList.add(each.getPoolSize()); poolSizeList.add(each.getPoolSize());
activeSizeList.add(each.getActiveSize()); activeSizeList.add(each.getActiveSize());
queueSizeList.add(each.getQueueSize()); queueSizeList.add(each.getQueueSize());
rejectCountList.add(each.getRejectCount());
queueRemainingCapacityList.add(each.getQueueRemainingCapacity()); queueRemainingCapacityList.add(each.getQueueRemainingCapacity());
currentLoadList.add(each.getCurrentLoad());
queueCapacityList.add(each.getQueueCapacity()); queueCapacityList.add(each.getQueueCapacity());
if (firstFlag.get()) { if (firstFlag) {
firstFlag = false;
completedTaskCountList.add(0L); completedTaskCountList.add(0L);
firstFlag.set(Boolean.FALSE); completedTaskCountTemp = each.getCompletedTaskCount();
countTemp = each.getCompletedTaskCount(); rejectCountTemp = each.getRejectCount();
continue; continue;
} }
long completedTaskCount = each.getCompletedTaskCount(); rangeCompletedTaskCountList.add(each.getCompletedTaskCount() - completedTaskCountTemp);
long countTask = completedTaskCount - countTemp; completedTaskCountList.add(each.getCompletedTaskCount());
completedTaskCountList.add(countTask); rangeRejectCountList.add(each.getRejectCount() - rejectCountTemp);
countTemp = each.getCompletedTaskCount(); rejectCountList.add(each.getRejectCount());
completedTaskCountTemp = each.getCompletedTaskCount();
rejectCountTemp = each.getRejectCount();
} }
return new MonitorActiveRespDTO(times, poolSizeList, activeSizeList, queueSizeList, completedTaskCountList, rejectCountList, queueRemainingCapacityList, currentLoadList, queueCapacityList); return MonitorActiveRespDTO.builder()
.times(times)
.poolSizeList(poolSizeList)
.activeSizeList(activeSizeList)
.queueSizeList(queueSizeList)
.queueCapacityList(queueCapacityList)
.rangeRejectCountList(rangeRejectCountList)
.rejectCountList(rejectCountList)
.completedTaskCountList(completedTaskCountList)
.rangeCompletedTaskCountList(rangeCompletedTaskCountList)
.queueRemainingCapacityList(queueRemainingCapacityList)
.build();
} }
@Override @Override

Loading…
Cancel
Save