From f8bc6f631aa8f380cc877e8af73854b802dbc0f1 Mon Sep 17 00:00:00 2001
From: Eric Zhao The statistic node keep three kinds of real-time statistics metrics:
+ * Sentinel use sliding window to record and count the resource statistics in real-time.
+ * The sliding window infrastructure behind the {@link ArrayMetric} is {@code LeapArray}.
+ *
+ * case 1: When the first request comes in, Sentinel will create a new window bucket of
+ * a specified time-span to store running statics, such as total response time(rt),
+ * incoming request(QPS), block request(bq), etc. And the time-span is defined by sample count.
+ *
+ * Sentinel use the statics of the valid buckets to decide whether this request can be passed.
+ * For example, if a rule defines that only 100 requests can be passed,
+ * it will sum all qps in valid buckets, and compare it to the threshold defined in rule.
+ * case 2: continuous requests case 3: requests keeps coming, and previous buckets become invalid The sliding window should become:
+ *
+ *
+ *
+ * 0 100ms
+ * +-------+--→ Sliding Windows
+ * ^
+ * |
+ * request
+ *
+ *
+ * 0 100ms 200ms 300ms
+ * +-------+-------+-------+-----→ Sliding Windows
+ * ^
+ * |
+ * request
+ *
+ *
+ *
+ * 0 100ms 200ms 800ms 900ms 1000ms 1300ms
+ * +-------+-------+ ...... +-------+-------+ ...... +-------+-----→ Sliding Windows
+ * ^
+ * |
+ * request
+ *
+ *
+ *
+ * 300ms 800ms 900ms 1000ms 1300ms
+ * + ...... +-------+ ...... +-------+-----→ Sliding Windows
+ * ^
+ * |
+ * request
+ *
+ *
* @author qinan.qn
* @author jialiang.linjl
*/
public class StatisticNode implements Node {
+ /**
+ * Holds statistics of the recent {@code INTERVAL} seconds. The {@code INTERVAL} is divided into time spans
+ * by given {@code sampleCount}.
+ */
private transient volatile Metric rollingCounterInSecond = new ArrayMetric(1000 / SampleCountProperty.SAMPLE_COUNT,
IntervalProperty.INTERVAL);
@@ -40,6 +101,9 @@ public class StatisticNode implements Node {
*/
private transient Metric rollingCounterInMinute = new ArrayMetric(1000, 60);
+ /**
+ * The counter for thread count.
+ */
private AtomicInteger curThreadNum = new AtomicInteger(0);
private long lastFetchTime = -1;
diff --git a/sentinel-core/src/main/java/com/alibaba/csp/sentinel/slots/statistic/base/LeapArray.java b/sentinel-core/src/main/java/com/alibaba/csp/sentinel/slots/statistic/base/LeapArray.java
index 30761e2f..39ee3c04 100755
--- a/sentinel-core/src/main/java/com/alibaba/csp/sentinel/slots/statistic/base/LeapArray.java
+++ b/sentinel-core/src/main/java/com/alibaba/csp/sentinel/slots/statistic/base/LeapArray.java
@@ -32,10 +32,10 @@ import com.alibaba.csp.sentinel.util.TimeUtil;
* {@link #sampleCount} = intervalInMs / windowLengthInMs.
*