Browse Source

Support setting flush interval of the metric log via SentinelConfig property (#1919)

master
brothelul GitHub 3 years ago
parent
commit
a343873c81
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 44 additions and 2 deletions
  1. +21
    -0
      sentinel-core/src/main/java/com/alibaba/csp/sentinel/config/SentinelConfig.java
  2. +23
    -2
      sentinel-core/src/main/java/com/alibaba/csp/sentinel/slots/block/flow/FlowRuleManager.java

+ 21
- 0
sentinel-core/src/main/java/com/alibaba/csp/sentinel/config/SentinelConfig.java View File

@@ -55,12 +55,14 @@ public final class SentinelConfig {
public static final String COLD_FACTOR = "csp.sentinel.flow.cold.factor"; public static final String COLD_FACTOR = "csp.sentinel.flow.cold.factor";
public static final String STATISTIC_MAX_RT = "csp.sentinel.statistic.max.rt"; public static final String STATISTIC_MAX_RT = "csp.sentinel.statistic.max.rt";
public static final String SPI_CLASSLOADER = "csp.sentinel.spi.classloader"; public static final String SPI_CLASSLOADER = "csp.sentinel.spi.classloader";
public static final String METRIC_FLUSH_INTERVAL = "csp.sentinel.metric.flush.interval";


public static final String DEFAULT_CHARSET = "UTF-8"; public static final String DEFAULT_CHARSET = "UTF-8";
public static final long DEFAULT_SINGLE_METRIC_FILE_SIZE = 1024 * 1024 * 50; public static final long DEFAULT_SINGLE_METRIC_FILE_SIZE = 1024 * 1024 * 50;
public static final int DEFAULT_TOTAL_METRIC_FILE_COUNT = 6; public static final int DEFAULT_TOTAL_METRIC_FILE_COUNT = 6;
public static final int DEFAULT_COLD_FACTOR = 3; public static final int DEFAULT_COLD_FACTOR = 3;
public static final int DEFAULT_STATISTIC_MAX_RT = 5000; public static final int DEFAULT_STATISTIC_MAX_RT = 5000;
public static final long DEFAULT_METRIC_FLUSH_INTERVAL = 1L;


static { static {
try { try {
@@ -98,6 +100,7 @@ public final class SentinelConfig {
setConfig(TOTAL_METRIC_FILE_COUNT, String.valueOf(DEFAULT_TOTAL_METRIC_FILE_COUNT)); setConfig(TOTAL_METRIC_FILE_COUNT, String.valueOf(DEFAULT_TOTAL_METRIC_FILE_COUNT));
setConfig(COLD_FACTOR, String.valueOf(DEFAULT_COLD_FACTOR)); setConfig(COLD_FACTOR, String.valueOf(DEFAULT_COLD_FACTOR));
setConfig(STATISTIC_MAX_RT, String.valueOf(DEFAULT_STATISTIC_MAX_RT)); setConfig(STATISTIC_MAX_RT, String.valueOf(DEFAULT_STATISTIC_MAX_RT));
setConfig(METRIC_FLUSH_INTERVAL, String.valueOf(DEFAULT_METRIC_FLUSH_INTERVAL));
} }


private static void loadProps() { private static void loadProps() {
@@ -155,6 +158,24 @@ public final class SentinelConfig {
public static String charset() { public static String charset() {
return props.get(CHARSET); return props.get(CHARSET);
} }
/**
* Get the metric log flush interval in second
* @return the metric log flush interval in second
*/
public static long metricLogFlushIntervalSec() {
String flushIntervalStr = SentinelConfig.getConfig(METRIC_FLUSH_INTERVAL);
if (flushIntervalStr == null) {
return DEFAULT_METRIC_FLUSH_INTERVAL;
}
try {
return Long.parseLong(flushIntervalStr);
} catch (Throwable throwable) {
RecordLog.warn("[SentinelConfig] Parse the metricLogFlushInterval fail, use default value: "
+ DEFAULT_METRIC_FLUSH_INTERVAL, throwable);
return DEFAULT_METRIC_FLUSH_INTERVAL;
}
}


public static long singleMetricFileSize() { public static long singleMetricFileSize() {
try { try {


+ 23
- 2
sentinel-core/src/main/java/com/alibaba/csp/sentinel/slots/block/flow/FlowRuleManager.java View File

@@ -26,6 +26,7 @@ import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicReference; import java.util.concurrent.atomic.AtomicReference;


import com.alibaba.csp.sentinel.concurrent.NamedThreadFactory; import com.alibaba.csp.sentinel.concurrent.NamedThreadFactory;
import com.alibaba.csp.sentinel.config.SentinelConfig;
import com.alibaba.csp.sentinel.log.RecordLog; import com.alibaba.csp.sentinel.log.RecordLog;
import com.alibaba.csp.sentinel.util.AssertUtil; import com.alibaba.csp.sentinel.util.AssertUtil;
import com.alibaba.csp.sentinel.util.StringUtil; import com.alibaba.csp.sentinel.util.StringUtil;
@@ -61,9 +62,29 @@ public class FlowRuleManager {
static { static {
flowRules.set(Collections.<String, List<FlowRule>>emptyMap()); flowRules.set(Collections.<String, List<FlowRule>>emptyMap());
currentProperty.addListener(LISTENER); currentProperty.addListener(LISTENER);
SCHEDULER.scheduleAtFixedRate(new MetricTimerListener(), 0, 1, TimeUnit.SECONDS);
startMetricTimerListener();
} }

/**
* <p> Start the MetricTimerListener
* <ol>
* <li>If the flushInterval more than 0,
* the timer will run with the flushInterval as the rate </li>.
* <li>If the flushInterval less than 0(include) or value is not valid,
* then means the timer will not be started </li>
* <ol></p>
*/
private static void startMetricTimerListener() {
long flushInterval = SentinelConfig.metricLogFlushIntervalSec();
if (flushInterval <= 0) {
RecordLog.info("[FlowRuleManager] The MetricTimerListener is'n started. If you want to start it, "
+ "please change the value(current: {}) of config({}) more than 0 to start it.", flushInterval,
SentinelConfig.METRIC_FLUSH_INTERVAL);
return;
}
SCHEDULER.scheduleAtFixedRate(new MetricTimerListener(), 0, flushInterval, TimeUnit.SECONDS);
}
/** /**
* Listen to the {@link SentinelProperty} for {@link FlowRule}s. The property is the source of {@link FlowRule}s. * Listen to the {@link SentinelProperty} for {@link FlowRule}s. The property is the source of {@link FlowRule}s.
* Flow rules can also be set by {@link #loadRules(List)} directly. * Flow rules can also be set by {@link #loadRules(List)} directly.


Loading…
Cancel
Save