本文基于:1.3.0-GA版本
這里截取官網(wǎng)的一張圖:
Slot是從第一個(gè)往后一個(gè)個(gè)傳遞的校赤,當(dāng)數(shù)據(jù)到了StatisticSlot時(shí),就開始進(jìn)行統(tǒng)計(jì)了午绳。
后面的所有的Slot都依賴這個(gè)統(tǒng)計(jì)進(jìn)行校驗(yàn)。
StatisticSlot就是根據(jù)滑動(dòng)窗口進(jìn)行數(shù)據(jù)統(tǒng)計(jì)的映之,下面開始講解拦焚。
一蜡坊、包結(jié)構(gòu)
如圖:
紅框內(nèi)既是本文要分析的內(nèi)容。
1赎败、base包的內(nèi)容主要就是滑動(dòng)窗口的設(shè)計(jì)及多線程計(jì)數(shù)統(tǒng)計(jì)
2秕衙、metic包主要是指標(biāo)的統(tǒng)計(jì)及指標(biāo)數(shù)據(jù)的集合封裝
3、StatisticSlot就是統(tǒng)計(jì)核心入口了僵刮,主要統(tǒng)計(jì)了正常情況据忘、發(fā)生BlockException、其他異常的統(tǒng)計(jì)數(shù)據(jù)
4搞糕、StatisticSlotCallbackRegistry回調(diào)注冊(cè)器勇吊,后續(xù)熱點(diǎn)參數(shù)限流會(huì)涉及到
二、Metric及ArrayMetric
Metric
Metric是一個(gè)接口窍仰,用來統(tǒng)計(jì)各項(xiàng)指標(biāo)的一個(gè)入口汉规,如:success(成功數(shù))、exception(異常數(shù))驹吮、block(阻塞數(shù))针史、pass(請(qǐng)求數(shù))、rt(響應(yīng)時(shí)間)碟狞;并定義了這些指標(biāo)的增加方法啄枕。
ArrayMetric
ArrayMetric是Metric的實(shí)現(xiàn)類,ArrayMetric實(shí)現(xiàn)了Metric定義的所有的方法族沃;
注意ArrayMeric的構(gòu)造方法频祝,通過調(diào)用查詢,可以發(fā)現(xiàn)StatisticNode類中定義的竭业,如:
public class StatisticNode implements Node {
private transient volatile Metric rollingCounterInSecond = new ArrayMetric(1000 / SampleCountProperty.SAMPLE_COUNT,
IntervalProperty.INTERVAL);
/**
* Holds statistics of the recent 60 seconds. The windowLengthInMs is deliberately set to 1000 milliseconds,
* meaning each bucket per second, in this way we can get accurate statistics of each second.
*/
private transient Metric rollingCounterInMinute = new ArrayMetric(1000, 60);
//下面代碼省略
}
/**
* 數(shù)據(jù)保存到地方
*/
private final MetricsLeapArray data;
/**
* Constructor
*
* @param windowLengthInMs a single window bucket's time length in milliseconds.
* @param intervalInSec the total time span of this {@link ArrayMetric} in seconds.
*/
public ArrayMetric(int windowLengthInMs, int intervalInSec) {
this.data = new MetricsLeapArray(windowLengthInMs, intervalInSec);
}
然后通過MetricsLeapArray的構(gòu)造函數(shù)智润,構(gòu)造滑動(dòng)時(shí)間窗口:
這里通過注釋的解釋就能發(fā)現(xiàn)了
windowLengthInMs:一個(gè)單口滑動(dòng)時(shí)間窗口的大小,單位是毫秒
intervalInSec:窗口的區(qū)間大小未辆,單位秒
MetricsLeapArray又調(diào)用父類的LeapArray的構(gòu)造方法進(jìn)行構(gòu)造窟绷。
三、LeapArray及WindowWrap
LeapArray
//單口滑動(dòng)時(shí)間窗口的大小咐柜,單位是毫秒
protected int windowLengthInMs;
//滑動(dòng)時(shí)間窗口個(gè)數(shù)
protected int sampleCount;
//統(tǒng)計(jì)時(shí)間區(qū)間
protected int intervalInMs;
/**
保存統(tǒng)計(jì)數(shù)據(jù)的地方兼蜈,數(shù)組
*/
protected final AtomicReferenceArray<WindowWrap<T>> array;
//構(gòu)造函數(shù)
public LeapArray(int windowLengthInMs, int intervalInSec) {
this.windowLengthInMs = windowLengthInMs;
//因?yàn)閕ntervalInSec單位是秒,所以乘以1000
this.intervalInMs = intervalInSec * 1000;
this.sampleCount = intervalInMs / windowLengthInMs;
this.array = new AtomicReferenceArray<WindowWrap<T>>(sampleCount);
}
可以看到窗口的統(tǒng)計(jì)數(shù)據(jù)被被包裝到WindowWrap了
WindowWrap
/**
* a single window bucket's time length in milliseconds.
*/
private final long windowLengthInMs;
/**
* Start time of the window in milliseconds.
*/
private long windowStart;
/**
* Statistic value.
*/
private T value;
WindowWrap是一個(gè)對(duì)象拙友,真正的數(shù)據(jù)被保存到泛型T的value中为狸,在這里使用的是MetricBucket對(duì)象。
MetricBucket
private final LongAdder pass = new LongAdder();
private final LongAdder block = new LongAdder();
private final LongAdder exception = new LongAdder();
private final LongAdder rt = new LongAdder();
private final LongAdder success = new LongAdder();
這里使用的LongAdder遗契,LongAdder是JDK8新增的一個(gè)類辐棒,和AtomicLong類似,但是LongAdder支持在多線程下有更高的吞吐量。
四漾根、調(diào)用鏈
通過分析上述關(guān)系泰涂,可以發(fā)現(xiàn)完整的流程如下:
具體分析
1、StatisticSolt是一個(gè)統(tǒng)計(jì)插槽辐怕,也是我們調(diào)用鏈入口:
public class StatisticSlot extends AbstractLinkedProcessorSlot<DefaultNode> {
@Override
public void entry(Context context, ResourceWrapper resourceWrapper, DefaultNode node, int count, Object... args)
throws Throwable {
try {
fireEntry(context, resourceWrapper, node, count, args);
node.increaseThreadNum();
//這里開始調(diào)用增加統(tǒng)計(jì)次數(shù)
node.addPassRequest();
if (context.getCurEntry().getOriginNode() != null) {
context.getCurEntry().getOriginNode().increaseThreadNum();
context.getCurEntry().getOriginNode().addPassRequest();
}
//以下代碼省略
//......
}
2逼蒙、通過DefaultNote調(diào)用父類StatisticNode的addPassRequest方法
3、StatisticNode創(chuàng)建了兩個(gè)統(tǒng)計(jì)Metric:rollingCounterInSecond寄疏、rollingCounterInMinute是牢,進(jìn)行調(diào)用ArrayMetric的addPass方法
4、構(gòu)建了時(shí)間窗口MetricsLeapArray陕截,并調(diào)用:
@Override
public void addPass() {
WindowWrap<MetricBucket> wrap = data.currentWindow();
wrap.value().addPass();
}
從LeadArray數(shù)組中獲取到當(dāng)前時(shí)間的窗口包裝器驳棱,
獲取當(dāng)前時(shí)間的時(shí)間窗口方法currentWindow,下一節(jié)詳細(xì)介紹:http://www.reibang.com/p/05677381e155
5艘策、在該時(shí)間包裝器對(duì)數(shù)據(jù)MetricBucket進(jìn)行指標(biāo)的操作
public void addPass() {
pass.add(1L);
}
五蹈胡、總結(jié)
1、本文分析整個(gè)滑動(dòng)時(shí)間窗口的構(gòu)建過程及詳細(xì)的調(diào)用鏈朋蔫。
2罚渐、StatisticSlot是sentinel統(tǒng)計(jì)的核心,后續(xù)的流控驯妄,降級(jí)等都是根據(jù)此階段的統(tǒng)計(jì)數(shù)據(jù)進(jìn)行的。
3青扔、本文目前還沒有介紹滑動(dòng)時(shí)間窗口的具體算法源织,請(qǐng)關(guān)注下一篇文章。
4微猖、統(tǒng)計(jì)指標(biāo)數(shù)據(jù)是根據(jù)LongAdder數(shù)據(jù)結(jié)構(gòu)進(jìn)行各項(xiàng)統(tǒng)計(jì)的谈息,主要原理就是利用分段寫入,減少競(jìng)爭(zhēng)凛剥。