diff --git a/sentinel-core/src/main/java/com/alibaba/csp/sentinel/Constants.java b/sentinel-core/src/main/java/com/alibaba/csp/sentinel/Constants.java index 1c8e8d22..c7d56d1c 100755 --- a/sentinel-core/src/main/java/com/alibaba/csp/sentinel/Constants.java +++ b/sentinel-core/src/main/java/com/alibaba/csp/sentinel/Constants.java @@ -15,18 +15,19 @@ */ package com.alibaba.csp.sentinel; -import com.alibaba.csp.sentinel.config.SentinelConfig; import com.alibaba.csp.sentinel.node.ClusterNode; import com.alibaba.csp.sentinel.node.DefaultNode; import com.alibaba.csp.sentinel.node.EntranceNode; import com.alibaba.csp.sentinel.slotchain.StringResourceWrapper; -import com.alibaba.csp.sentinel.slots.system.SystemRule; import com.alibaba.csp.sentinel.util.VersionUtil; /** + * Universal constants of Sentinel. + * * @author qinan.qn * @author youji.zj * @author jialiang.linjl + * @author Eric Zhao */ public final class Constants { @@ -60,16 +61,10 @@ public final class Constants { new ClusterNode(ROOT_ID, ResourceTypeConstants.COMMON)); /** - * Global statistic node for inbound traffic. Usually used for {@link SystemRule} checking. + * Global statistic node for inbound traffic. Usually used for {@code SystemRule} checking. */ public final static ClusterNode ENTRY_NODE = new ClusterNode(TOTAL_IN_RESOURCE_NAME, ResourceTypeConstants.COMMON); - /** - * Response time that exceeds TIME_DROP_VALVE will be calculated as TIME_DROP_VALVE. Default value is 4900 ms. - * It can be configured by property file or JVM parameter via {@code -Dcsp.sentinel.statistic.max.rt=xxx}. - */ - public static final int TIME_DROP_VALVE = SentinelConfig.statisticMaxRt(); - /** * The global switch for Sentinel. */ 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 30d80717..a0d7c226 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 @@ -18,6 +18,7 @@ package com.alibaba.csp.sentinel.config; import com.alibaba.csp.sentinel.log.RecordLog; import com.alibaba.csp.sentinel.util.AppNameUtil; import com.alibaba.csp.sentinel.util.AssertUtil; +import com.alibaba.csp.sentinel.util.StringUtil; import java.util.Map; import java.util.Properties; @@ -30,7 +31,7 @@ import java.util.concurrent.ConcurrentHashMap; * @author leyou * @author Eric Zhao */ -public class SentinelConfig { +public final class SentinelConfig { /** * The default application type. @@ -54,7 +55,8 @@ public class SentinelConfig { static final long DEFAULT_SINGLE_METRIC_FILE_SIZE = 1024 * 1024 * 50; static final int DEFAULT_TOTAL_METRIC_FILE_COUNT = 6; static final int DEFAULT_COLD_FACTOR = 3; - static final int DEFAULT_STATISTIC_MAX_RT = 4900; + + public static final int DEFAULT_STATISTIC_MAX_RT = 4900; static { try { @@ -186,13 +188,28 @@ public class SentinelConfig { } } + /** + *

Get the max RT value that Sentinel could accept.

+ *

Response time that exceeds {@code statisticMaxRt} will be recorded as this value. + * The default value is {@link #DEFAULT_STATISTIC_MAX_RT}.

+ * + * @return the max allowed RT value + * @since 1.4.1 + */ public static int statisticMaxRt() { + String v = props.get(STATISTIC_MAX_RT); try { - return Integer.parseInt(props.get(STATISTIC_MAX_RT)); + if (StringUtil.isEmpty(v)) { + return DEFAULT_STATISTIC_MAX_RT; + } + return Integer.parseInt(v); } catch (Throwable throwable) { - RecordLog.warn("[SentinelConfig] Parse statisticMaxRt fail, use default value: " - + DEFAULT_STATISTIC_MAX_RT, throwable); + RecordLog.warn("[SentinelConfig] Invalid statisticMaxRt value: {0}, using the default value instead: " + + DEFAULT_STATISTIC_MAX_RT, v, throwable); + SentinelConfig.setConfig(STATISTIC_MAX_RT, String.valueOf(DEFAULT_STATISTIC_MAX_RT)); return DEFAULT_STATISTIC_MAX_RT; } } + + private SentinelConfig() {} } diff --git a/sentinel-core/src/main/java/com/alibaba/csp/sentinel/slots/block/degrade/DegradeRuleManager.java b/sentinel-core/src/main/java/com/alibaba/csp/sentinel/slots/block/degrade/DegradeRuleManager.java index 95a967a7..6cb09f8e 100755 --- a/sentinel-core/src/main/java/com/alibaba/csp/sentinel/slots/block/degrade/DegradeRuleManager.java +++ b/sentinel-core/src/main/java/com/alibaba/csp/sentinel/slots/block/degrade/DegradeRuleManager.java @@ -23,7 +23,7 @@ import java.util.Map; import java.util.Set; import java.util.concurrent.ConcurrentHashMap; -import com.alibaba.csp.sentinel.Constants; +import com.alibaba.csp.sentinel.config.SentinelConfig; import com.alibaba.csp.sentinel.context.Context; import com.alibaba.csp.sentinel.log.RecordLog; import com.alibaba.csp.sentinel.node.DefaultNode; @@ -211,7 +211,7 @@ public final class DegradeRuleManager { if (!baseValid) { return false; } - int maxAllowedRt = Constants.TIME_DROP_VALVE; + int maxAllowedRt = SentinelConfig.statisticMaxRt(); if (rule.getGrade() == RuleConstant.DEGRADE_GRADE_RT) { if (rule.getRtSlowRequestAmount() <= 0) { return false; diff --git a/sentinel-core/src/main/java/com/alibaba/csp/sentinel/slots/statistic/StatisticSlot.java b/sentinel-core/src/main/java/com/alibaba/csp/sentinel/slots/statistic/StatisticSlot.java index 30b6a6a8..09affecc 100755 --- a/sentinel-core/src/main/java/com/alibaba/csp/sentinel/slots/statistic/StatisticSlot.java +++ b/sentinel-core/src/main/java/com/alibaba/csp/sentinel/slots/statistic/StatisticSlot.java @@ -17,6 +17,7 @@ package com.alibaba.csp.sentinel.slots.statistic; import java.util.Collection; +import com.alibaba.csp.sentinel.config.SentinelConfig; import com.alibaba.csp.sentinel.slotchain.ProcessorSlotEntryCallback; import com.alibaba.csp.sentinel.slotchain.ProcessorSlotExitCallback; import com.alibaba.csp.sentinel.slots.block.flow.PriorityWaitException; @@ -133,10 +134,11 @@ public class StatisticSlot extends AbstractLinkedProcessorSlot { DefaultNode node = (DefaultNode)context.getCurNode(); if (context.getCurEntry().getError() == null) { - // Calculate response time (max RT is TIME_DROP_VALVE). + // Calculate response time (max RT is statisticMaxRt from SentinelConfig). long rt = TimeUtil.currentTimeMillis() - context.getCurEntry().getCreateTime(); - if (rt > Constants.TIME_DROP_VALVE) { - rt = Constants.TIME_DROP_VALVE; + int maxStatisticRt = SentinelConfig.statisticMaxRt(); + if (rt > maxStatisticRt) { + rt = maxStatisticRt; } // Record response time and success count. diff --git a/sentinel-core/src/main/java/com/alibaba/csp/sentinel/slots/statistic/data/MetricBucket.java b/sentinel-core/src/main/java/com/alibaba/csp/sentinel/slots/statistic/data/MetricBucket.java index ec3f2afa..6c35e174 100755 --- a/sentinel-core/src/main/java/com/alibaba/csp/sentinel/slots/statistic/data/MetricBucket.java +++ b/sentinel-core/src/main/java/com/alibaba/csp/sentinel/slots/statistic/data/MetricBucket.java @@ -15,7 +15,7 @@ */ package com.alibaba.csp.sentinel.slots.statistic.data; -import com.alibaba.csp.sentinel.Constants; +import com.alibaba.csp.sentinel.config.SentinelConfig; import com.alibaba.csp.sentinel.slots.statistic.MetricEvent; import com.alibaba.csp.sentinel.slots.statistic.base.LongAdder; @@ -50,7 +50,7 @@ public class MetricBucket { } private void initMinRt() { - this.minRt = Constants.TIME_DROP_VALVE; + this.minRt = SentinelConfig.statisticMaxRt(); } /** diff --git a/sentinel-core/src/main/java/com/alibaba/csp/sentinel/slots/statistic/metric/ArrayMetric.java b/sentinel-core/src/main/java/com/alibaba/csp/sentinel/slots/statistic/metric/ArrayMetric.java index 521414a8..c21223a7 100755 --- a/sentinel-core/src/main/java/com/alibaba/csp/sentinel/slots/statistic/metric/ArrayMetric.java +++ b/sentinel-core/src/main/java/com/alibaba/csp/sentinel/slots/statistic/metric/ArrayMetric.java @@ -18,7 +18,7 @@ package com.alibaba.csp.sentinel.slots.statistic.metric; import java.util.ArrayList; import java.util.List; -import com.alibaba.csp.sentinel.Constants; +import com.alibaba.csp.sentinel.config.SentinelConfig; import com.alibaba.csp.sentinel.node.metric.MetricNode; import com.alibaba.csp.sentinel.slots.statistic.MetricEvent; import com.alibaba.csp.sentinel.slots.statistic.base.LeapArray; @@ -141,7 +141,7 @@ public class ArrayMetric implements Metric { @Override public long minRt() { data.currentWindow(); - long rt = Constants.TIME_DROP_VALVE; + long rt = SentinelConfig.statisticMaxRt(); List list = data.values(); for (MetricBucket window : list) { if (window.minRt() < rt) { diff --git a/sentinel-core/src/test/java/com/alibaba/csp/sentinel/ConstantsTest.java b/sentinel-core/src/test/java/com/alibaba/csp/sentinel/ConstantsTest.java deleted file mode 100644 index 03084270..00000000 --- a/sentinel-core/src/test/java/com/alibaba/csp/sentinel/ConstantsTest.java +++ /dev/null @@ -1,25 +0,0 @@ -package com.alibaba.csp.sentinel; - -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -/** - * Test cases for {@link Constants}. - * - * @author cdfive - */ -public class ConstantsTest { - - @Test - public void testDefaultTimeDropValue() { - assertEquals(4900, Constants.TIME_DROP_VALVE); - } - -// add JVM parameter -// -Dcsp.sentinel.statistic.max.rt=10000 -// @Test - public void testCustomTimeDropValue() { - assertEquals(10000, Constants.TIME_DROP_VALVE); - } -} \ No newline at end of file diff --git a/sentinel-core/src/test/java/com/alibaba/csp/sentinel/node/StatisticNodeTest.java b/sentinel-core/src/test/java/com/alibaba/csp/sentinel/node/StatisticNodeTest.java index c34b8bf9..d128e686 100644 --- a/sentinel-core/src/test/java/com/alibaba/csp/sentinel/node/StatisticNodeTest.java +++ b/sentinel-core/src/test/java/com/alibaba/csp/sentinel/node/StatisticNodeTest.java @@ -15,7 +15,7 @@ */ package com.alibaba.csp.sentinel.node; -import com.alibaba.csp.sentinel.Constants; +import com.alibaba.csp.sentinel.config.SentinelConfig; import com.alibaba.csp.sentinel.util.TimeUtil; import org.junit.Assert; import org.junit.Test; @@ -111,7 +111,7 @@ public class StatisticNodeTest { assertEquals(totalRequest, node.totalSuccess()); // now there are no data in time span, so the minRT should be equals to TIME_DROP_VALVE - assertEquals(node.minRt(), Constants.TIME_DROP_VALVE, 0.01); + assertEquals(node.minRt(), SentinelConfig.statisticMaxRt(), 0.01); log("===================================================="); log("testStatisticThreadNumAndQps done, cost " + (TimeUtil.currentTimeMillis() - testStartTime) + "ms");