Monitor resources size

This commit is contained in:
lancer.hong
2022-04-20 17:25:30 +08:00
parent 5f8364e9a3
commit 380f00036d
5 changed files with 46 additions and 5 deletions

View File

@@ -17,6 +17,11 @@
</properties> </properties>
<dependencies> <dependencies>
<dependency>
<groupId>org.openjdk.jol</groupId>
<artifactId>jol-core</artifactId>
</dependency>
<dependency> <dependency>
<groupId>org.springframework.cloud</groupId> <groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-context</artifactId> <artifactId>spring-cloud-context</artifactId>

View File

@@ -101,6 +101,12 @@ public final class Consts {
public static final String PROD = "prod"; public static final String PROD = "prod";
} }
public static final class UN {
public static final int KB = 1024;
public static final int MB = 1024 * KB;
public static final int GB = 1024 * MB;
}
public static final String HTTP_SERVER = "http_server"; public static final String HTTP_SERVER = "http_server";
public static final String HTTP_CLIENT = "http_client"; public static final String HTTP_CLIENT = "http_client";
public static final String MYSQL = "mysql"; public static final String MYSQL = "mysql";

View File

@@ -17,6 +17,7 @@
package we.controller; package we.controller;
import org.openjdk.jol.info.GraphLayout;
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
@@ -32,10 +33,13 @@ import we.stats.ResourceStat;
import we.stats.circuitbreaker.CircuitBreakManager; import we.stats.circuitbreaker.CircuitBreakManager;
import we.stats.ratelimit.ResourceRateLimitConfig; import we.stats.ratelimit.ResourceRateLimitConfig;
import we.stats.ratelimit.ResourceRateLimitConfigService; import we.stats.ratelimit.ResourceRateLimitConfigService;
import we.util.Consts;
import we.util.JacksonUtils; import we.util.JacksonUtils;
import we.util.ResourceIdUtils; import we.util.ResourceIdUtils;
import javax.annotation.Resource; import javax.annotation.Resource;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
@@ -115,7 +119,7 @@ public class CacheCheckController {
@GetMapping("/resourceStats") @GetMapping("/resourceStats")
public Mono<String> resourceStats(ServerWebExchange exchange) { public Mono<String> resourceStats(ServerWebExchange exchange) {
Map<String, Integer> map = new HashMap<>(); Map<String, Object> map = new HashMap<>();
int nodeCnt = 0, serviceDefaultCnt = 0, serviceCnt = 0, appDefaultCnt = 0, appCnt = 0, ipCnt = 0, hostCnt = 0; int nodeCnt = 0, serviceDefaultCnt = 0, serviceCnt = 0, appDefaultCnt = 0, appCnt = 0, ipCnt = 0, hostCnt = 0;
ConcurrentMap<String, ResourceStat> resourceStats = flowStat.resourceStats; ConcurrentMap<String, ResourceStat> resourceStats = flowStat.resourceStats;
Set<Map.Entry<String, ResourceStat>> entrySet = resourceStats.entrySet(); Set<Map.Entry<String, ResourceStat>> entrySet = resourceStats.entrySet();
@@ -179,6 +183,23 @@ public class CacheCheckController {
map.put("ip", ipCnt); map.put("ip", ipCnt);
map.put("host", hostCnt); map.put("host", hostCnt);
long size = GraphLayout.parseInstance(resourceStats).totalSize();
BigDecimal bigDecimalSize = new BigDecimal(size);
String resourceStatsSize;
if (size >= Consts.UN.GB) {
float r = bigDecimalSize.divide(new BigDecimal(Consts.UN.GB), 2, RoundingMode.HALF_UP).floatValue();
resourceStatsSize = r + " GB";
} else if (size >= Consts.UN.MB) {
float r = bigDecimalSize.divide(new BigDecimal(Consts.UN.MB), 2, RoundingMode.HALF_UP).floatValue();
resourceStatsSize = r + " MB";
} else if (size >= Consts.UN.KB) {
float r = bigDecimalSize.divide(new BigDecimal(Consts.UN.KB), 2, RoundingMode.HALF_UP).floatValue();
resourceStatsSize = r + " KB";
} else {
resourceStatsSize = size + " B";
}
map.put("resourceStatsSize", resourceStatsSize);
return Mono.just(JacksonUtils.writeValueAsString(map)); return Mono.just(JacksonUtils.writeValueAsString(map));
} }
} }

View File

@@ -616,13 +616,16 @@ public class FlowStat {
// log.debug("PeakConcurrentJob start"); // log.debug("PeakConcurrentJob start");
Set<Map.Entry<String, ResourceStat>> entrys = stat.resourceStats.entrySet(); Set<Map.Entry<String, ResourceStat>> entrys = stat.resourceStats.entrySet();
for (Entry<String, ResourceStat> entry : entrys) { for (Entry<String, ResourceStat> entry : entrys) {
String resourceId = entry.getKey(); String resource = entry.getKey();
// log.debug("PeakConcurrentJob: resourceId={} slotId=={}", resourceId, // log.debug("PeakConcurrentJob: resourceId={} slotId=={}", resourceId,
// curTimeSlotId); // curTimeSlotId);
ResourceStat resourceStat = entry.getValue();
resourceStat.getTimeSlot(curTimeSlotId);
String resource = resourceStat.getResourceId(); ResourceStat resourceStat = entry.getValue();
if (resourceStat.getConcurrentRequests().get() > 0) {
resourceStat.getTimeSlot(curTimeSlotId);
}
// String resource = resourceStat.getResourceId();
CircuitBreaker cb = circuitBreakManager.getCircuitBreaker(resource); CircuitBreaker cb = circuitBreakManager.getCircuitBreaker(resource);
if (cb != null) { if (cb != null) {
cb.correctState(curTimeSlotId, stat); cb.correctState(curTimeSlotId, stat);

View File

@@ -69,6 +69,12 @@
<dependencyManagement> <dependencyManagement>
<dependencies> <dependencies>
<dependency>
<groupId>org.openjdk.jol</groupId>
<artifactId>jol-core</artifactId>
<version>0.16</version>
</dependency>
<dependency> <dependency>
<groupId>com.lmax</groupId> <groupId>com.lmax</groupId>
<artifactId>disruptor</artifactId> <artifactId>disruptor</artifactId>