Procházet zdrojové kódy

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>
master
Eric Zhao GitHub před 5 roky
rodič
revize
c70565167f
V databázi nebyl nalezen žádný známý klíč pro tento podpis ID GPG klíče: 4AEE18F83AFDEB23
8 změnil soubory, kde provedl 39 přidání a 50 odebrání
  1. +4
    -9
      sentinel-core/src/main/java/com/alibaba/csp/sentinel/Constants.java
  2. +22
    -5
      sentinel-core/src/main/java/com/alibaba/csp/sentinel/config/SentinelConfig.java
  3. +2
    -2
      sentinel-core/src/main/java/com/alibaba/csp/sentinel/slots/block/degrade/DegradeRuleManager.java
  4. +5
    -3
      sentinel-core/src/main/java/com/alibaba/csp/sentinel/slots/statistic/StatisticSlot.java
  5. +2
    -2
      sentinel-core/src/main/java/com/alibaba/csp/sentinel/slots/statistic/data/MetricBucket.java
  6. +2
    -2
      sentinel-core/src/main/java/com/alibaba/csp/sentinel/slots/statistic/metric/ArrayMetric.java
  7. +0
    -25
      sentinel-core/src/test/java/com/alibaba/csp/sentinel/ConstantsTest.java
  8. +2
    -2
      sentinel-core/src/test/java/com/alibaba/csp/sentinel/node/StatisticNodeTest.java

+ 4
- 9
sentinel-core/src/main/java/com/alibaba/csp/sentinel/Constants.java Zobrazit soubor

@@ -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.
*/


+ 22
- 5
sentinel-core/src/main/java/com/alibaba/csp/sentinel/config/SentinelConfig.java Zobrazit soubor

@@ -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 {
}
}

/**
* <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() {}
}

+ 2
- 2
sentinel-core/src/main/java/com/alibaba/csp/sentinel/slots/block/degrade/DegradeRuleManager.java Zobrazit soubor

@@ -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;


+ 5
- 3
sentinel-core/src/main/java/com/alibaba/csp/sentinel/slots/statistic/StatisticSlot.java Zobrazit soubor

@@ -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> {
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.


+ 2
- 2
sentinel-core/src/main/java/com/alibaba/csp/sentinel/slots/statistic/data/MetricBucket.java Zobrazit soubor

@@ -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();
}

/**


+ 2
- 2
sentinel-core/src/main/java/com/alibaba/csp/sentinel/slots/statistic/metric/ArrayMetric.java Zobrazit soubor

@@ -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<MetricBucket> list = data.values();
for (MetricBucket window : list) {
if (window.minRt() < rt) {


+ 0
- 25
sentinel-core/src/test/java/com/alibaba/csp/sentinel/ConstantsTest.java Zobrazit soubor

@@ -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);
}
}

+ 2
- 2
sentinel-core/src/test/java/com/alibaba/csp/sentinel/node/StatisticNodeTest.java Zobrazit soubor

@@ -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");


Načítá se…
Zrušit
Uložit