From 9f957ff95ef4270d89870f9b9e236180149c5c65 Mon Sep 17 00:00:00 2001 From: hongqiaowei Date: Sat, 9 Jan 2021 19:00:48 +0800 Subject: [PATCH] chore: 210109 release --- .../java/we/config/FlowStatSchedConfig.java | 62 +++++++++++-------- src/main/java/we/filter/PreFilter.java | 8 --- 2 files changed, 37 insertions(+), 33 deletions(-) diff --git a/src/main/java/we/config/FlowStatSchedConfig.java b/src/main/java/we/config/FlowStatSchedConfig.java index 18b63d1..bb19f75 100644 --- a/src/main/java/we/config/FlowStatSchedConfig.java +++ b/src/main/java/we/config/FlowStatSchedConfig.java @@ -91,41 +91,23 @@ public class FlowStatSchedConfig extends SchedConfig { @Resource(name = AggregateRedisConfig.AGGREGATE_REACTIVE_REDIS_TEMPLATE) private ReactiveStringRedisTemplate rt; - private boolean firstTime = true; - private final String ip = NetworkUtils.getServerIp(); + private long startTimeSlot = 0; + @Scheduled(cron = "${flow-stat-sched.cron}") public void sched() { if (!flowControl) { return; } - if (firstTime) { - firstTime = false; + FlowStat flowStat = flowControlFilter.getFlowStat(); + if (startTimeSlot == 0) { + startTimeSlot = getRecentEndTimeSlot(flowStat); return; } - FlowStat flowStat = flowControlFilter.getFlowStat(); - long currentTimeSlot = flowStat.currentTimeSlotId(); - int second = DateTimeUtils.from(currentTimeSlot).getSecond(); - long interval; - if (second > 49) { - interval = second - 50; - } else if (second > 39) { - interval = second - 40; - } else if (second > 29) { - interval = second - 30; - } else if (second > 19) { - interval = second - 20; - } else if (second > 9) { - interval = second - 10; - } else if (second > 0) { - interval = second - 0; - } else { - interval = 0; - } - long recentEndTimeSlot = currentTimeSlot - interval * 1000; - long startTimeSlot = recentEndTimeSlot - 10 * 1000; + long st = System.currentTimeMillis(); + long recentEndTimeSlot = getRecentEndTimeSlot(flowStat); List resourceTimeWindowStats = flowStat.getResourceTimeWindowStats(null, startTimeSlot, recentEndTimeSlot, 10); if (resourceTimeWindowStats == null || resourceTimeWindowStats.isEmpty()) { log.info(toDP19(startTimeSlot) + " - " + toDP19(recentEndTimeSlot) + " no flow stat data"); @@ -186,12 +168,42 @@ public class FlowStatSchedConfig extends SchedConfig { ); } ); + + startTimeSlot = recentEndTimeSlot; + log.info(toDP23(st) + " fss " + toDP23(System.currentTimeMillis())); + } + + private long getRecentEndTimeSlot(FlowStat flowStat) { + long currentTimeSlot = flowStat.currentTimeSlotId(); + int second = DateTimeUtils.from(currentTimeSlot).getSecond(); + long interval; + if (second > 49) { + interval = second - 50; + } else if (second > 39) { + interval = second - 40; + } else if (second > 29) { + interval = second - 30; + } else if (second > 19) { + interval = second - 20; + } else if (second > 9) { + interval = second - 10; + } else if (second > 0) { + interval = second - 0; + } else { + interval = 0; + } + long recentEndTimeSlot = currentTimeSlot - interval * 1000; + return recentEndTimeSlot; } private String toDP19(long startTimeSlot) { return DateTimeUtils.toDate(startTimeSlot, Constants.DatetimePattern.DP19); } + private String toDP23(long startTimeSlot) { + return DateTimeUtils.toDate(startTimeSlot, Constants.DatetimePattern.DP23); + } + private static void toJsonStringValue(StringBuilder b, String value) { b.append(Constants.Symbol.DOUBLE_QUOTE).append(value).append(Constants.Symbol.DOUBLE_QUOTE); } diff --git a/src/main/java/we/filter/PreFilter.java b/src/main/java/we/filter/PreFilter.java index a6aef4d..423d138 100644 --- a/src/main/java/we/filter/PreFilter.java +++ b/src/main/java/we/filter/PreFilter.java @@ -25,7 +25,6 @@ import org.springframework.beans.factory.annotation.Value; import org.springframework.context.ApplicationContext; import org.springframework.core.annotation.Order; import org.springframework.http.HttpStatus; -import org.springframework.http.server.reactive.ServerHttpResponse; import org.springframework.stereotype.Component; import org.springframework.web.server.ServerWebExchange; import org.springframework.web.server.WebFilterChain; @@ -37,7 +36,6 @@ import we.plugin.auth.ApiConfig; import we.plugin.auth.ApiConfigService; import we.plugin.auth.AuthPluginFilter; import we.plugin.stat.StatPluginFilter; -import we.util.JacksonUtils; import we.util.ReactorUtils; import we.util.WebUtils; @@ -76,12 +74,6 @@ public class PreFilter extends ProxyAggrFilter { @Override public Mono doFilter(ServerWebExchange exchange, WebFilterChain chain) { - String clientReqPath = WebUtils.getClientReqPath(exchange); - if ("/flowControl/mock".equals(clientReqPath)) { - ServerHttpResponse resp = exchange.getResponse(); - return resp.writeWith(Mono.just(resp.bufferFactory().wrap("ok".getBytes()))); - } - Map fc = new HashMap<>(6, 1.0f); fc.put(WebUtils.PREV_FILTER_RESULT, succFr); Map appendHdrs = new HashMap<>(6, 1.0f); Map eas = exchange.getAttributes(); eas.put(WebUtils.FILTER_CONTEXT, fc);