Skip to content

Commit

Permalink
refactor: Get the max allowed RT directly from SentinelConfig.statist…
Browse files Browse the repository at this point in the history
…icMaxRt() (alibaba#1173)

- to avoid the dependency chain: Constants -> SentinelConfig

Signed-off-by: Eric Zhao <[email protected]>
  • Loading branch information
sczyh30 authored Nov 21, 2019
1 parent 24f8d75 commit c705651
Show file tree
Hide file tree
Showing 8 changed files with 39 additions and 50 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down Expand Up @@ -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.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
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;
Expand All @@ -30,7 +31,7 @@
* @author leyou
* @author Eric Zhao
*/
public class SentinelConfig {
public final class SentinelConfig {

/**
* The default application type.
Expand All @@ -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 {
Expand Down Expand Up @@ -186,13 +188,28 @@ public static int coldFactor() {
}
}

/**
* <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() {
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() {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
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;
Expand Down Expand Up @@ -211,7 +211,7 @@ public static boolean isValidRule(DegradeRule rule) {
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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

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;
Expand Down Expand Up @@ -133,10 +134,11 @@ public void exit(Context context, ResourceWrapper resourceWrapper, int count, Ob
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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -50,7 +50,7 @@ public MetricBucket reset(MetricBucket bucket) {
}

private void initMinRt() {
this.minRt = Constants.TIME_DROP_VALVE;
this.minRt = SentinelConfig.statisticMaxRt();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
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;
Expand Down Expand Up @@ -141,7 +141,7 @@ public long rt() {
@Override
public long minRt() {
data.currentWindow();
long rt = Constants.TIME_DROP_VALVE;
long rt = SentinelConfig.statisticMaxRt();
List<MetricBucket> list = data.values();
for (MetricBucket window : list) {
if (window.minRt() < rt) {
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -111,7 +111,7 @@ public void testStatisticThreadNumAndQps() {
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");
Expand Down

0 comments on commit c705651

Please sign in to comment.