From e171905c6d5e908edd6c8ca25a5344ffc017c31c Mon Sep 17 00:00:00 2001 From: hongqiaowei Date: Fri, 5 Aug 2022 16:45:24 +0800 Subject: [PATCH] Add node rps and concurrents to FizzGatewayNodeStatSchedConfig.java --- .../FizzGatewayNodeStatSchedConfig.java | 39 ++++++++++++++++++- 1 file changed, 37 insertions(+), 2 deletions(-) diff --git a/fizz-core/src/main/java/we/config/FizzGatewayNodeStatSchedConfig.java b/fizz-core/src/main/java/we/config/FizzGatewayNodeStatSchedConfig.java index f994e9f..45a51e7 100644 --- a/fizz-core/src/main/java/we/config/FizzGatewayNodeStatSchedConfig.java +++ b/fizz-core/src/main/java/we/config/FizzGatewayNodeStatSchedConfig.java @@ -19,18 +19,26 @@ package we.config; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.web.reactive.context.ReactiveWebServerApplicationContext; import org.springframework.context.annotation.Configuration; import org.springframework.core.env.ConfigurableEnvironment; import org.springframework.data.redis.core.ReactiveStringRedisTemplate; -import org.springframework.scheduling.annotation.EnableScheduling; import org.springframework.scheduling.annotation.Scheduled; +import org.springframework.util.CollectionUtils; +import we.stats.FlowStat; +import we.stats.ResourceTimeWindowStat; +import we.stats.TimeWindowStat; import we.util.JacksonUtils; import we.util.NetworkUtils; +import we.util.ResourceIdUtils; import javax.annotation.PostConstruct; import javax.annotation.Resource; import java.lang.management.ManagementFactory; +import java.math.BigDecimal; +import java.util.List; /** * @author hongqiaowei @@ -46,6 +54,8 @@ public class FizzGatewayNodeStatSchedConfig extends SchedConfig { public int port; public long ts; public long startTs; + public long concurrents = 0; + public double rps = 0; } private static final Logger LOGGER = LoggerFactory.getLogger(FizzGatewayNodeStatSchedConfig.class); @@ -58,6 +68,15 @@ public class FizzGatewayNodeStatSchedConfig extends SchedConfig { @Resource(name = AggregateRedisConfig.AGGREGATE_REACTIVE_REDIS_TEMPLATE) private ReactiveStringRedisTemplate rt; + @Value("${flowControl:false}") + private boolean flowControl; + + @Autowired(required = false) + private FlowStat flowStat; + + @Value("${izz-gateway-node-stat-sched.recent:3}") + private int recent; + private Stat stat = new Stat(); private String hashKey; @@ -72,12 +91,28 @@ public class FizzGatewayNodeStatSchedConfig extends SchedConfig { stat.startTs = ManagementFactory.getRuntimeMXBean().getStartTime(); } - @Scheduled(cron = "${fizz-gateway-node-stat-sched.cron:*/3 * * * * ?}") + @Scheduled(cron = "${fizz-gateway-node-stat-sched.cron:*/1 * * * * ?}") public void sched() { stat.ts = System.currentTimeMillis(); + if (flowControl) { + long currentTimeSlot = flowStat.currentTimeSlotId(); + long startTimeSlot = currentTimeSlot - recent * 1000; + List resourceTimeWindowStats = flowStat.getResourceTimeWindowStats(ResourceIdUtils.NODE_RESOURCE, startTimeSlot, currentTimeSlot, recent); + if (!CollectionUtils.isEmpty(resourceTimeWindowStats)) { + TimeWindowStat timeWindowStat = resourceTimeWindowStats.get(0).getWindows().get(0); + BigDecimal rps = timeWindowStat.getRps(); + if (rps != null) { + stat.rps = rps.doubleValue(); + } + } + stat.concurrents = flowStat.getConcurrentRequests(ResourceIdUtils.NODE_RESOURCE); + } String s; try { s = JacksonUtils.writeValueAsString(stat); + if (LOGGER.isDebugEnabled()) { + LOGGER.debug("gateway stat: {}", s); + } } catch (RuntimeException e) { LOGGER.error("serial fizz gateway node stat error", e); return;