Skip to content

Commit 4440918

Browse files
cdfivesczyh30
authored andcommitted
Make the default statistic max RT value TIME_DROP_VALVE configurable (#292)
@see #276
1 parent 197c982 commit 4440918

File tree

5 files changed

+127
-26
lines changed

5 files changed

+127
-26
lines changed

sentinel-core/src/main/java/com/alibaba/csp/sentinel/Constants.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616
package com.alibaba.csp.sentinel;
1717

18+
import com.alibaba.csp.sentinel.config.SentinelConfig;
1819
import com.alibaba.csp.sentinel.node.ClusterNode;
1920
import com.alibaba.csp.sentinel.node.DefaultNode;
2021
import com.alibaba.csp.sentinel.node.EntranceNode;
@@ -47,8 +48,11 @@ public final class Constants {
4748

4849
/**
4950
* Response time that exceeds TIME_DROP_VALVE will be calculated as TIME_DROP_VALVE.
51+
* Default value is 4900 ms
52+
* It can be configured by property file or JVM parameter -Dcsp.sentinel.statistic.max.rt=xxx
53+
* See {@link SentinelConfig#statisticMaxRt()}
5054
*/
51-
public final static int TIME_DROP_VALVE = 4900;
55+
public static int TIME_DROP_VALVE = SentinelConfig.statisticMaxRt();
5256

5357
/**
5458
* The global switch for Sentinel.

sentinel-core/src/main/java/com/alibaba/csp/sentinel/config/SentinelConfig.java

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
import com.alibaba.csp.sentinel.log.RecordLog;
2626
import com.alibaba.csp.sentinel.util.AppNameUtil;
2727
import com.alibaba.csp.sentinel.util.AssertUtil;
28-
import com.alibaba.csp.sentinel.util.StringUtil;
2928

3029
/**
3130
* The universal config of Courier. The config is retrieved from
@@ -41,9 +40,13 @@ public class SentinelConfig {
4140
public static final String SINGLE_METRIC_FILE_SIZE = "csp.sentinel.metric.file.single.size";
4241
public static final String TOTAL_METRIC_FILE_COUNT = "csp.sentinel.metric.file.total.count";
4342
public static final String COLD_FACTOR = "csp.sentinel.flow.cold.factor";
43+
public static final String STATISTIC_MAX_RT = "csp.sentinel.statistic.max.rt";
4444

45+
static final String DEFAULT_CHARSET = "UTF-8";
4546
static final long DEFAULT_SINGLE_METRIC_FILE_SIZE = 1024 * 1024 * 50;
4647
static final int DEFAULT_TOTAL_METRIC_FILE_COUNT = 6;
48+
static final int DEFAULT_COLD_FACTOR = 3;
49+
static final int DEFAULT_STATISTIC_MAX_RT = 4900;
4750

4851
static {
4952
initialize();
@@ -52,10 +55,11 @@ public class SentinelConfig {
5255

5356
private static void initialize() {
5457
// Init default properties.
55-
SentinelConfig.setConfig(CHARSET, "UTF-8");
58+
SentinelConfig.setConfig(CHARSET, DEFAULT_CHARSET);
5659
SentinelConfig.setConfig(SINGLE_METRIC_FILE_SIZE, String.valueOf(DEFAULT_SINGLE_METRIC_FILE_SIZE));
5760
SentinelConfig.setConfig(TOTAL_METRIC_FILE_COUNT, String.valueOf(DEFAULT_TOTAL_METRIC_FILE_COUNT));
58-
SentinelConfig.setConfig(COLD_FACTOR, String.valueOf(3));
61+
SentinelConfig.setConfig(COLD_FACTOR, String.valueOf(DEFAULT_COLD_FACTOR));
62+
SentinelConfig.setConfig(STATISTIC_MAX_RT, String.valueOf(DEFAULT_STATISTIC_MAX_RT));
5963
}
6064

6165
private static void loadProps() {
@@ -139,7 +143,7 @@ public static long singleMetricFileSize() {
139143
try {
140144
return Long.parseLong(props.get(SINGLE_METRIC_FILE_SIZE));
141145
} catch (Throwable throwable) {
142-
RecordLog.info("[SentinelConfig] Parse singleMetricFileSize fail, use default value: "
146+
RecordLog.warn("[SentinelConfig] Parse singleMetricFileSize fail, use default value: "
143147
+ DEFAULT_SINGLE_METRIC_FILE_SIZE, throwable);
144148
return DEFAULT_SINGLE_METRIC_FILE_SIZE;
145149
}
@@ -149,9 +153,35 @@ public static int totalMetricFileCount() {
149153
try {
150154
return Integer.parseInt(props.get(TOTAL_METRIC_FILE_COUNT));
151155
} catch (Throwable throwable) {
152-
RecordLog.info("[SentinelConfig] Parse totalMetricFileCount fail, use default value: "
156+
RecordLog.warn("[SentinelConfig] Parse totalMetricFileCount fail, use default value: "
153157
+ DEFAULT_TOTAL_METRIC_FILE_COUNT, throwable);
154158
return DEFAULT_TOTAL_METRIC_FILE_COUNT;
155159
}
156160
}
161+
162+
public static int coldFactor() {
163+
try {
164+
int coldFactor = Integer.parseInt(props.get(COLD_FACTOR));
165+
if (coldFactor <= 1) {// check the cold factor larger than 1
166+
coldFactor = DEFAULT_COLD_FACTOR;
167+
RecordLog.warn("cold factor=" + coldFactor + ", should be larger than 1, use default value: "
168+
+ DEFAULT_COLD_FACTOR);
169+
}
170+
return coldFactor;
171+
} catch (Throwable throwable) {
172+
RecordLog.warn("[SentinelConfig] Parse coldFactor fail, use default value: "
173+
+ DEFAULT_COLD_FACTOR, throwable);
174+
return DEFAULT_COLD_FACTOR;
175+
}
176+
}
177+
178+
public static int statisticMaxRt() {
179+
try {
180+
return Integer.parseInt(props.get(STATISTIC_MAX_RT));
181+
} catch (Throwable throwable) {
182+
RecordLog.warn("[SentinelConfig] Parse statisticMaxRt fail, use default value: "
183+
+ DEFAULT_STATISTIC_MAX_RT, throwable);
184+
return DEFAULT_STATISTIC_MAX_RT;
185+
}
186+
}
157187
}

sentinel-core/src/main/java/com/alibaba/csp/sentinel/slots/block/flow/ColdFactorProperty.java

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -16,30 +16,11 @@
1616
package com.alibaba.csp.sentinel.slots.block.flow;
1717

1818
import com.alibaba.csp.sentinel.config.SentinelConfig;
19-
import com.alibaba.csp.sentinel.log.RecordLog;
20-
import com.alibaba.csp.sentinel.util.StringUtil;
2119

2220
/**
2321
* @author jialiang.linjl
2422
*/
2523
class ColdFactorProperty {
26-
public static volatile int coldFactor = 3;
2724

28-
static {
29-
String strConfig = SentinelConfig.getConfig(SentinelConfig.COLD_FACTOR);
30-
if (StringUtil.isBlank(strConfig)) {
31-
coldFactor = 3;
32-
} else {
33-
try {
34-
coldFactor = Integer.valueOf(strConfig);
35-
} catch (NumberFormatException e) {
36-
RecordLog.info(e.getMessage(), e);
37-
}
38-
39-
if (coldFactor <= 1) {
40-
coldFactor = 3;
41-
RecordLog.info("cold factor should be larger than 3");
42-
}
43-
}
44-
}
25+
public static volatile int coldFactor = SentinelConfig.coldFactor();
4526
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.alibaba.csp.sentinel;
2+
3+
import org.junit.Test;
4+
5+
import static org.junit.Assert.assertEquals;
6+
7+
/**
8+
* Test cases for {@link Constants}.
9+
*
10+
* @author cdfive
11+
*/
12+
public class ConstantsTest {
13+
14+
@Test
15+
public void testDefaultTimeDropValue() {
16+
assertEquals(4900, Constants.TIME_DROP_VALVE);
17+
}
18+
19+
// add JVM parameter
20+
// -Dcsp.sentinel.statistic.max.rt=10000
21+
// @Test
22+
public void testCustomTimeDropValue() {
23+
assertEquals(10000, Constants.TIME_DROP_VALVE);
24+
}
25+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package com.alibaba.csp.sentinel.config;
2+
3+
import org.junit.Test;
4+
5+
import static org.junit.Assert.assertEquals;
6+
7+
/**
8+
* Test cases for {@link SentinelConfig}.
9+
*
10+
* @author cdfive
11+
*/
12+
public class SentinelConfigTest {
13+
14+
@Test
15+
public void testDefaultConfig() {
16+
assertEquals(SentinelConfig.DEFAULT_CHARSET, SentinelConfig.charset());
17+
assertEquals(SentinelConfig.DEFAULT_SINGLE_METRIC_FILE_SIZE, SentinelConfig.singleMetricFileSize());
18+
assertEquals(SentinelConfig.DEFAULT_TOTAL_METRIC_FILE_COUNT, SentinelConfig.totalMetricFileCount());
19+
assertEquals(SentinelConfig.DEFAULT_COLD_FACTOR, SentinelConfig.coldFactor());
20+
assertEquals(SentinelConfig.DEFAULT_STATISTIC_MAX_RT, SentinelConfig.statisticMaxRt());
21+
}
22+
23+
// add JVM parameter
24+
// -Dcsp.sentinel.charset=gbk
25+
// -Dcsp.sentinel.metric.file.single.size=104857600
26+
// -Dcsp.sentinel.metric.file.total.count=10
27+
// -Dcsp.sentinel.flow.cold.factor=5
28+
// -Dcsp.sentinel.statistic.max.rt=10000
29+
// @Test
30+
public void testCustomConfig() {
31+
assertEquals("gbk", SentinelConfig.charset());
32+
assertEquals(104857600L, SentinelConfig.singleMetricFileSize());
33+
assertEquals(10, SentinelConfig.totalMetricFileCount());
34+
assertEquals(5, SentinelConfig.coldFactor());
35+
assertEquals(10000, SentinelConfig.statisticMaxRt());
36+
}
37+
38+
39+
/**
40+
* when set code factor alue equal or smaller than 1, get value
41+
* in SentinelConfig.coldFactor() will return DEFAULT_COLD_FACTOR
42+
* see {@link SentinelConfig#coldFactor()}
43+
*/
44+
@Test
45+
public void testColdFactorEqualOrSmallerThanOne() {
46+
SentinelConfig.setConfig(SentinelConfig.COLD_FACTOR, "0.5");
47+
assertEquals(SentinelConfig.DEFAULT_COLD_FACTOR, SentinelConfig.coldFactor());
48+
49+
SentinelConfig.setConfig(SentinelConfig.COLD_FACTOR, "1");
50+
assertEquals(SentinelConfig.DEFAULT_COLD_FACTOR, SentinelConfig.coldFactor());
51+
}
52+
53+
@Test
54+
public void testColdFactoryLargerThanOne() {
55+
SentinelConfig.setConfig(SentinelConfig.COLD_FACTOR, "2");
56+
assertEquals(2, SentinelConfig.coldFactor());
57+
58+
SentinelConfig.setConfig(SentinelConfig.COLD_FACTOR, "4");
59+
assertEquals(4, SentinelConfig.coldFactor());
60+
}
61+
}

0 commit comments

Comments
 (0)