refactor: Get the max allowed RT directly from SentinelConfig.statisticMaxRt() (#1173)
- to avoid the dependency chain: Constants -> SentinelConfig Signed-off-by: Eric Zhao <sczyh16@gmail.com>
This commit is contained in:
parent
24f8d75601
commit
c70565167f
|
|
@ -15,18 +15,19 @@
|
||||||
*/
|
*/
|
||||||
package com.alibaba.csp.sentinel;
|
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.ClusterNode;
|
||||||
import com.alibaba.csp.sentinel.node.DefaultNode;
|
import com.alibaba.csp.sentinel.node.DefaultNode;
|
||||||
import com.alibaba.csp.sentinel.node.EntranceNode;
|
import com.alibaba.csp.sentinel.node.EntranceNode;
|
||||||
import com.alibaba.csp.sentinel.slotchain.StringResourceWrapper;
|
import com.alibaba.csp.sentinel.slotchain.StringResourceWrapper;
|
||||||
import com.alibaba.csp.sentinel.slots.system.SystemRule;
|
|
||||||
import com.alibaba.csp.sentinel.util.VersionUtil;
|
import com.alibaba.csp.sentinel.util.VersionUtil;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
* Universal constants of Sentinel.
|
||||||
|
*
|
||||||
* @author qinan.qn
|
* @author qinan.qn
|
||||||
* @author youji.zj
|
* @author youji.zj
|
||||||
* @author jialiang.linjl
|
* @author jialiang.linjl
|
||||||
|
* @author Eric Zhao
|
||||||
*/
|
*/
|
||||||
public final class Constants {
|
public final class Constants {
|
||||||
|
|
||||||
|
|
@ -60,16 +61,10 @@ public final class Constants {
|
||||||
new ClusterNode(ROOT_ID, ResourceTypeConstants.COMMON));
|
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);
|
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.
|
* The global switch for Sentinel.
|
||||||
*/
|
*/
|
||||||
|
|
|
||||||
|
|
@ -18,6 +18,7 @@ package com.alibaba.csp.sentinel.config;
|
||||||
import com.alibaba.csp.sentinel.log.RecordLog;
|
import com.alibaba.csp.sentinel.log.RecordLog;
|
||||||
import com.alibaba.csp.sentinel.util.AppNameUtil;
|
import com.alibaba.csp.sentinel.util.AppNameUtil;
|
||||||
import com.alibaba.csp.sentinel.util.AssertUtil;
|
import com.alibaba.csp.sentinel.util.AssertUtil;
|
||||||
|
import com.alibaba.csp.sentinel.util.StringUtil;
|
||||||
|
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Properties;
|
import java.util.Properties;
|
||||||
|
|
@ -30,7 +31,7 @@ import java.util.concurrent.ConcurrentHashMap;
|
||||||
* @author leyou
|
* @author leyou
|
||||||
* @author Eric Zhao
|
* @author Eric Zhao
|
||||||
*/
|
*/
|
||||||
public class SentinelConfig {
|
public final class SentinelConfig {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The default application type.
|
* The default application type.
|
||||||
|
|
@ -54,7 +55,8 @@ public class SentinelConfig {
|
||||||
static final long DEFAULT_SINGLE_METRIC_FILE_SIZE = 1024 * 1024 * 50;
|
static final long DEFAULT_SINGLE_METRIC_FILE_SIZE = 1024 * 1024 * 50;
|
||||||
static final int DEFAULT_TOTAL_METRIC_FILE_COUNT = 6;
|
static final int DEFAULT_TOTAL_METRIC_FILE_COUNT = 6;
|
||||||
static final int DEFAULT_COLD_FACTOR = 3;
|
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 {
|
static {
|
||||||
try {
|
try {
|
||||||
|
|
@ -186,13 +188,28 @@ public class SentinelConfig {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* <p>Get the max RT value that Sentinel could accept.</p>
|
||||||
|
* <p>Response time that exceeds {@code statisticMaxRt} will be recorded as this value.
|
||||||
|
* The default value is {@link #DEFAULT_STATISTIC_MAX_RT}.</p>
|
||||||
|
*
|
||||||
|
* @return the max allowed RT value
|
||||||
|
* @since 1.4.1
|
||||||
|
*/
|
||||||
public static int statisticMaxRt() {
|
public static int statisticMaxRt() {
|
||||||
|
String v = props.get(STATISTIC_MAX_RT);
|
||||||
try {
|
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) {
|
} catch (Throwable throwable) {
|
||||||
RecordLog.warn("[SentinelConfig] Parse statisticMaxRt fail, use default value: "
|
RecordLog.warn("[SentinelConfig] Invalid statisticMaxRt value: {0}, using the default value instead: "
|
||||||
+ DEFAULT_STATISTIC_MAX_RT, throwable);
|
+ DEFAULT_STATISTIC_MAX_RT, v, throwable);
|
||||||
|
SentinelConfig.setConfig(STATISTIC_MAX_RT, String.valueOf(DEFAULT_STATISTIC_MAX_RT));
|
||||||
return DEFAULT_STATISTIC_MAX_RT;
|
return DEFAULT_STATISTIC_MAX_RT;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private SentinelConfig() {}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -23,7 +23,7 @@ import java.util.Map;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
import java.util.concurrent.ConcurrentHashMap;
|
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.context.Context;
|
||||||
import com.alibaba.csp.sentinel.log.RecordLog;
|
import com.alibaba.csp.sentinel.log.RecordLog;
|
||||||
import com.alibaba.csp.sentinel.node.DefaultNode;
|
import com.alibaba.csp.sentinel.node.DefaultNode;
|
||||||
|
|
@ -211,7 +211,7 @@ public final class DegradeRuleManager {
|
||||||
if (!baseValid) {
|
if (!baseValid) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
int maxAllowedRt = Constants.TIME_DROP_VALVE;
|
int maxAllowedRt = SentinelConfig.statisticMaxRt();
|
||||||
if (rule.getGrade() == RuleConstant.DEGRADE_GRADE_RT) {
|
if (rule.getGrade() == RuleConstant.DEGRADE_GRADE_RT) {
|
||||||
if (rule.getRtSlowRequestAmount() <= 0) {
|
if (rule.getRtSlowRequestAmount() <= 0) {
|
||||||
return false;
|
return false;
|
||||||
|
|
|
||||||
|
|
@ -17,6 +17,7 @@ package com.alibaba.csp.sentinel.slots.statistic;
|
||||||
|
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
|
|
||||||
|
import com.alibaba.csp.sentinel.config.SentinelConfig;
|
||||||
import com.alibaba.csp.sentinel.slotchain.ProcessorSlotEntryCallback;
|
import com.alibaba.csp.sentinel.slotchain.ProcessorSlotEntryCallback;
|
||||||
import com.alibaba.csp.sentinel.slotchain.ProcessorSlotExitCallback;
|
import com.alibaba.csp.sentinel.slotchain.ProcessorSlotExitCallback;
|
||||||
import com.alibaba.csp.sentinel.slots.block.flow.PriorityWaitException;
|
import com.alibaba.csp.sentinel.slots.block.flow.PriorityWaitException;
|
||||||
|
|
@ -133,10 +134,11 @@ public class StatisticSlot extends AbstractLinkedProcessorSlot<DefaultNode> {
|
||||||
DefaultNode node = (DefaultNode)context.getCurNode();
|
DefaultNode node = (DefaultNode)context.getCurNode();
|
||||||
|
|
||||||
if (context.getCurEntry().getError() == null) {
|
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();
|
long rt = TimeUtil.currentTimeMillis() - context.getCurEntry().getCreateTime();
|
||||||
if (rt > Constants.TIME_DROP_VALVE) {
|
int maxStatisticRt = SentinelConfig.statisticMaxRt();
|
||||||
rt = Constants.TIME_DROP_VALVE;
|
if (rt > maxStatisticRt) {
|
||||||
|
rt = maxStatisticRt;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Record response time and success count.
|
// Record response time and success count.
|
||||||
|
|
|
||||||
|
|
@ -15,7 +15,7 @@
|
||||||
*/
|
*/
|
||||||
package com.alibaba.csp.sentinel.slots.statistic.data;
|
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.MetricEvent;
|
||||||
import com.alibaba.csp.sentinel.slots.statistic.base.LongAdder;
|
import com.alibaba.csp.sentinel.slots.statistic.base.LongAdder;
|
||||||
|
|
||||||
|
|
@ -50,7 +50,7 @@ public class MetricBucket {
|
||||||
}
|
}
|
||||||
|
|
||||||
private void initMinRt() {
|
private void initMinRt() {
|
||||||
this.minRt = Constants.TIME_DROP_VALVE;
|
this.minRt = SentinelConfig.statisticMaxRt();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
||||||
|
|
@ -18,7 +18,7 @@ package com.alibaba.csp.sentinel.slots.statistic.metric;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
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.node.metric.MetricNode;
|
||||||
import com.alibaba.csp.sentinel.slots.statistic.MetricEvent;
|
import com.alibaba.csp.sentinel.slots.statistic.MetricEvent;
|
||||||
import com.alibaba.csp.sentinel.slots.statistic.base.LeapArray;
|
import com.alibaba.csp.sentinel.slots.statistic.base.LeapArray;
|
||||||
|
|
@ -141,7 +141,7 @@ public class ArrayMetric implements Metric {
|
||||||
@Override
|
@Override
|
||||||
public long minRt() {
|
public long minRt() {
|
||||||
data.currentWindow();
|
data.currentWindow();
|
||||||
long rt = Constants.TIME_DROP_VALVE;
|
long rt = SentinelConfig.statisticMaxRt();
|
||||||
List<MetricBucket> list = data.values();
|
List<MetricBucket> list = data.values();
|
||||||
for (MetricBucket window : list) {
|
for (MetricBucket window : list) {
|
||||||
if (window.minRt() < rt) {
|
if (window.minRt() < rt) {
|
||||||
|
|
|
||||||
|
|
@ -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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -15,7 +15,7 @@
|
||||||
*/
|
*/
|
||||||
package com.alibaba.csp.sentinel.node;
|
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 com.alibaba.csp.sentinel.util.TimeUtil;
|
||||||
import org.junit.Assert;
|
import org.junit.Assert;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
@ -111,7 +111,7 @@ public class StatisticNodeTest {
|
||||||
assertEquals(totalRequest, node.totalSuccess());
|
assertEquals(totalRequest, node.totalSuccess());
|
||||||
|
|
||||||
// now there are no data in time span, so the minRT should be equals to TIME_DROP_VALVE
|
// 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("====================================================");
|
||||||
log("testStatisticThreadNumAndQps done, cost " + (TimeUtil.currentTimeMillis() - testStartTime) + "ms");
|
log("testStatisticThreadNumAndQps done, cost " + (TimeUtil.currentTimeMillis() - testStartTime) + "ms");
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue