From a343873c813fc6fdb0404ca80b43ca9da83fa393 Mon Sep 17 00:00:00 2001 From: brothelul <1285823170@qq.com> Date: Thu, 31 Dec 2020 16:28:44 +0800 Subject: [PATCH] Support setting flush interval of the metric log via SentinelConfig property (#1919) --- .../csp/sentinel/config/SentinelConfig.java | 21 ++++++++++++++++ .../slots/block/flow/FlowRuleManager.java | 25 +++++++++++++++++-- 2 files changed, 44 insertions(+), 2 deletions(-) diff --git a/sentinel-core/src/main/java/com/alibaba/csp/sentinel/config/SentinelConfig.java b/sentinel-core/src/main/java/com/alibaba/csp/sentinel/config/SentinelConfig.java index f86bdc4e..788f97c2 100755 --- a/sentinel-core/src/main/java/com/alibaba/csp/sentinel/config/SentinelConfig.java +++ b/sentinel-core/src/main/java/com/alibaba/csp/sentinel/config/SentinelConfig.java @@ -55,12 +55,14 @@ public final class SentinelConfig { public static final String COLD_FACTOR = "csp.sentinel.flow.cold.factor"; public static final String STATISTIC_MAX_RT = "csp.sentinel.statistic.max.rt"; public static final String SPI_CLASSLOADER = "csp.sentinel.spi.classloader"; + public static final String METRIC_FLUSH_INTERVAL = "csp.sentinel.metric.flush.interval"; public static final String DEFAULT_CHARSET = "UTF-8"; public static final long DEFAULT_SINGLE_METRIC_FILE_SIZE = 1024 * 1024 * 50; public static final int DEFAULT_TOTAL_METRIC_FILE_COUNT = 6; public static final int DEFAULT_COLD_FACTOR = 3; public static final int DEFAULT_STATISTIC_MAX_RT = 5000; + public static final long DEFAULT_METRIC_FLUSH_INTERVAL = 1L; static { try { @@ -98,6 +100,7 @@ public final class SentinelConfig { setConfig(TOTAL_METRIC_FILE_COUNT, String.valueOf(DEFAULT_TOTAL_METRIC_FILE_COUNT)); setConfig(COLD_FACTOR, String.valueOf(DEFAULT_COLD_FACTOR)); setConfig(STATISTIC_MAX_RT, String.valueOf(DEFAULT_STATISTIC_MAX_RT)); + setConfig(METRIC_FLUSH_INTERVAL, String.valueOf(DEFAULT_METRIC_FLUSH_INTERVAL)); } private static void loadProps() { @@ -155,6 +158,24 @@ public final class SentinelConfig { public static String charset() { return props.get(CHARSET); } + + /** + * Get the metric log flush interval in second + * @return the metric log flush interval in second + */ + public static long metricLogFlushIntervalSec() { + String flushIntervalStr = SentinelConfig.getConfig(METRIC_FLUSH_INTERVAL); + if (flushIntervalStr == null) { + return DEFAULT_METRIC_FLUSH_INTERVAL; + } + try { + return Long.parseLong(flushIntervalStr); + } catch (Throwable throwable) { + RecordLog.warn("[SentinelConfig] Parse the metricLogFlushInterval fail, use default value: " + + DEFAULT_METRIC_FLUSH_INTERVAL, throwable); + return DEFAULT_METRIC_FLUSH_INTERVAL; + } + } public static long singleMetricFileSize() { try { diff --git a/sentinel-core/src/main/java/com/alibaba/csp/sentinel/slots/block/flow/FlowRuleManager.java b/sentinel-core/src/main/java/com/alibaba/csp/sentinel/slots/block/flow/FlowRuleManager.java index dfcc4cb5..eda0faaf 100755 --- a/sentinel-core/src/main/java/com/alibaba/csp/sentinel/slots/block/flow/FlowRuleManager.java +++ b/sentinel-core/src/main/java/com/alibaba/csp/sentinel/slots/block/flow/FlowRuleManager.java @@ -26,6 +26,7 @@ import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicReference; import com.alibaba.csp.sentinel.concurrent.NamedThreadFactory; +import com.alibaba.csp.sentinel.config.SentinelConfig; import com.alibaba.csp.sentinel.log.RecordLog; import com.alibaba.csp.sentinel.util.AssertUtil; import com.alibaba.csp.sentinel.util.StringUtil; @@ -61,9 +62,29 @@ public class FlowRuleManager { static { flowRules.set(Collections.>emptyMap()); currentProperty.addListener(LISTENER); - SCHEDULER.scheduleAtFixedRate(new MetricTimerListener(), 0, 1, TimeUnit.SECONDS); + startMetricTimerListener(); } - + + /** + *

Start the MetricTimerListener + *

    + *
  1. If the flushInterval more than 0, + * the timer will run with the flushInterval as the rate
  2. . + *
  3. If the flushInterval less than 0(include) or value is not valid, + * then means the timer will not be started
  4. + *

      + */ + private static void startMetricTimerListener() { + long flushInterval = SentinelConfig.metricLogFlushIntervalSec(); + if (flushInterval <= 0) { + RecordLog.info("[FlowRuleManager] The MetricTimerListener is'n started. If you want to start it, " + + "please change the value(current: {}) of config({}) more than 0 to start it.", flushInterval, + SentinelConfig.METRIC_FLUSH_INTERVAL); + return; + } + SCHEDULER.scheduleAtFixedRate(new MetricTimerListener(), 0, flushInterval, TimeUnit.SECONDS); + } + /** * Listen to the {@link SentinelProperty} for {@link FlowRule}s. The property is the source of {@link FlowRule}s. * Flow rules can also be set by {@link #loadRules(List)} directly.