Skip to content

Commit

Permalink
Support setting flush interval of the metric log via SentinelConfig p…
Browse files Browse the repository at this point in the history
…roperty (#1919)
  • Loading branch information
brotherlu-xcq authored Dec 31, 2020
1 parent 63aeb49 commit a343873
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,14 @@ public final class SentinelConfig {
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 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 long DEFAULT_SINGLE_METRIC_FILE_SIZE = 1024 * 1024 * 50;
public static final int DEFAULT_TOTAL_METRIC_FILE_COUNT = 6;
public static final int DEFAULT_COLD_FACTOR = 3;
public static final int DEFAULT_STATISTIC_MAX_RT = 5000;
public static final long DEFAULT_METRIC_FLUSH_INTERVAL = 1L;

static {
try {
Expand Down Expand Up @@ -98,6 +100,7 @@ private static void initialize() {
setConfig(TOTAL_METRIC_FILE_COUNT, String.valueOf(DEFAULT_TOTAL_METRIC_FILE_COUNT));
setConfig(COLD_FACTOR, String.valueOf(DEFAULT_COLD_FACTOR));
setConfig(STATISTIC_MAX_RT, String.valueOf(DEFAULT_STATISTIC_MAX_RT));
setConfig(METRIC_FLUSH_INTERVAL, String.valueOf(DEFAULT_METRIC_FLUSH_INTERVAL));
}

private static void loadProps() {
Expand Down Expand Up @@ -155,6 +158,24 @@ public static int getAppType() {
public static String 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() {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import java.util.concurrent.atomic.AtomicReference;

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.util.AssertUtil;
import com.alibaba.csp.sentinel.util.StringUtil;
Expand Down Expand Up @@ -61,9 +62,29 @@ public class FlowRuleManager {
static {
flowRules.set(Collections.<String, List<FlowRule>>emptyMap());
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.
* Flow rules can also be set by {@link #loadRules(List)} directly.
Expand Down

0 comments on commit a343873

Please sign in to comment.