From 380f00036d69400d30cedc29291af7fb8415092c Mon Sep 17 00:00:00 2001 From: "lancer.hong" Date: Wed, 20 Apr 2022 17:25:30 +0800 Subject: [PATCH] Monitor resources size --- fizz-common/pom.xml | 5 ++++ fizz-common/src/main/java/we/util/Consts.java | 6 +++++ .../we/controller/CacheCheckController.java | 23 ++++++++++++++++++- .../src/main/java/we/stats/FlowStat.java | 11 +++++---- pom.xml | 6 +++++ 5 files changed, 46 insertions(+), 5 deletions(-) diff --git a/fizz-common/pom.xml b/fizz-common/pom.xml index c29ae7f..7210962 100644 --- a/fizz-common/pom.xml +++ b/fizz-common/pom.xml @@ -17,6 +17,11 @@ + + org.openjdk.jol + jol-core + + org.springframework.cloud spring-cloud-context diff --git a/fizz-common/src/main/java/we/util/Consts.java b/fizz-common/src/main/java/we/util/Consts.java index e9a1852..014a1b2 100644 --- a/fizz-common/src/main/java/we/util/Consts.java +++ b/fizz-common/src/main/java/we/util/Consts.java @@ -101,6 +101,12 @@ public final class Consts { 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_CLIENT = "http_client"; public static final String MYSQL = "mysql"; diff --git a/fizz-core/src/main/java/we/controller/CacheCheckController.java b/fizz-core/src/main/java/we/controller/CacheCheckController.java index fd925a1..8cda69d 100644 --- a/fizz-core/src/main/java/we/controller/CacheCheckController.java +++ b/fizz-core/src/main/java/we/controller/CacheCheckController.java @@ -17,6 +17,7 @@ package we.controller; +import org.openjdk.jol.info.GraphLayout; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @@ -32,10 +33,13 @@ import we.stats.ResourceStat; import we.stats.circuitbreaker.CircuitBreakManager; import we.stats.ratelimit.ResourceRateLimitConfig; import we.stats.ratelimit.ResourceRateLimitConfigService; +import we.util.Consts; import we.util.JacksonUtils; import we.util.ResourceIdUtils; import javax.annotation.Resource; +import java.math.BigDecimal; +import java.math.RoundingMode; import java.util.HashMap; import java.util.Map; import java.util.Set; @@ -115,7 +119,7 @@ public class CacheCheckController { @GetMapping("/resourceStats") public Mono resourceStats(ServerWebExchange exchange) { - Map map = new HashMap<>(); + Map map = new HashMap<>(); int nodeCnt = 0, serviceDefaultCnt = 0, serviceCnt = 0, appDefaultCnt = 0, appCnt = 0, ipCnt = 0, hostCnt = 0; ConcurrentMap resourceStats = flowStat.resourceStats; Set> entrySet = resourceStats.entrySet(); @@ -179,6 +183,23 @@ public class CacheCheckController { map.put("ip", ipCnt); 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)); } } diff --git a/fizz-core/src/main/java/we/stats/FlowStat.java b/fizz-core/src/main/java/we/stats/FlowStat.java index 128b648..0dd9e7e 100644 --- a/fizz-core/src/main/java/we/stats/FlowStat.java +++ b/fizz-core/src/main/java/we/stats/FlowStat.java @@ -616,13 +616,16 @@ public class FlowStat { // log.debug("PeakConcurrentJob start"); Set> entrys = stat.resourceStats.entrySet(); for (Entry entry : entrys) { - String resourceId = entry.getKey(); + String resource = entry.getKey(); // log.debug("PeakConcurrentJob: resourceId={} slotId=={}", resourceId, // 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); if (cb != null) { cb.correctState(curTimeSlotId, stat); diff --git a/pom.xml b/pom.xml index b31953a..821685c 100644 --- a/pom.xml +++ b/pom.xml @@ -69,6 +69,12 @@ + + org.openjdk.jol + jol-core + 0.16 + + com.lmax disruptor