博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Alibaba Sentinel 源码阅读(Part 2 LeapArray)
阅读量:6737 次
发布时间:2019-06-25

本文共 3924 字,大约阅读时间需要 13 分钟。

hot3.png

前言

这一篇是上一篇的继续,如果不了解Sentinel ,请先阅读[Alibaba Sentinel 源码阅读(Part1 执行流程)](Alibaba Sentinel 源码阅读(Part1 执行流程))

入口

在上一篇我们看到 我们获取的所有信息,都是从StatisticNode 的这两个数据结构中获取的

private transient volatile Metric rollingCounterInSecond = new ArrayMetric(1000 / SampleCountProperty.SAMPLE_COUNT,        IntervalProperty.INTERVAL);    /**     * Holds statistics of the recent 120 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, 2 * 60);

rollingCounterInMinute 这个两分钟之内的每一秒中数据的一个list,而每一秒中的数据是存储在 MetricBucket,

ArrayMetric

// ArrayMetric 实现了Metric 接口,同时包含了 MetricsLeapArray数据结构,接口的实现就是通过这个MetricsLeapArray来实现的// MetricsLeapArray 是从 LeapArray 继承的,所以这一篇的重点就是LeapArray了public class ArrayMetric implements Metric {    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);    }}

LeapArray

实际上就是一个环形数组,来给张官方的图就明白了

看文档其实很清晰,整个是基于时间窗口滑动算法来实现的

新增当前统计数据

@Override    public void addSuccess() {        WindowWrap
wrap = data.currentWindow(); wrap.value().addSuccess(); }

获取时间窗口内统计数据

@Override    public long success() {        data.currentWindow();        long success = 0;        List
list = data.values(); for (MetricBucket window : list) { success += window.success(); } return success; }

所以重点的方法就是 data.currentWindow()方法了

protected final AtomicReferenceArray
> array;public LeapArray(int windowLengthInMs, int intervalInSec) { this.windowLengthInMs = windowLengthInMs; this.intervalInMs = intervalInSec * 1000; this.sampleCount = intervalInMs / windowLengthInMs; // 初始化容量大小 this.array = new AtomicReferenceArray
>(sampleCount); }
/**     * Get window at provided timestamp.     *     * @param time a valid timestamp     * @return the window at provided timestamp     */    public WindowWrap
currentWindow(long time) { long timeId = time / windowLengthInMs; // Calculate current index. int idx = (int)(timeId % array.length()); // Cut the time to current window start. time = time - time % windowLengthInMs; while (true) { WindowWrap
old = array.get(idx); if (old == null) { WindowWrap
window = new WindowWrap
(windowLengthInMs, time, newEmptyBucket()); if (array.compareAndSet(idx, null, window)) { return window; } else { Thread.yield(); } } else if (time == old.windowStart()) { return old; } else if (time > old.windowStart()) { if (updateLock.tryLock()) { try { // if (old is deprecated) then [LOCK] resetTo currentTime. return resetWindowTo(old, time); } finally { updateLock.unlock(); } } else { Thread.yield(); } } else if (time < old.windowStart()) { // Cannot go through here. return new WindowWrap
(windowLengthInMs, time, newEmptyBucket()); } } }

这部分的内容会维持一个有效的环形数组以统计数据,具体要自己debug 看了。

总结

这里也只是把大致流程梳理了一下方便大家看源码而已,很多地方没有具体分析,这部分还是需要自己亲力亲为。

参考

转载于:https://my.oschina.net/tigerlene/blog/2250158

你可能感兴趣的文章
中国工商银行阿根廷分行用数据运营展现本地特色
查看>>
使用闪存存储的优势与注意事项
查看>>
网络钓鱼防不胜防:大型科技公司竟被骗逾1亿美元
查看>>
网络间谍活动月光迷宫已演变成Turla
查看>>
欧洲运营商展开5GTango项目 应对特定行业市场
查看>>
Windows 10创作者更新将改进蓝牙功能
查看>>
睿联嘉业边缘融合大屏幕多媒体会议系统方案
查看>>
凯立德货车专用导航 应“运”而生
查看>>
光伏组件市场价格战下谁获益?
查看>>
价格血拼战频频上演 光伏业陷入集体焦虑
查看>>
聊天机器人真正的潜力,潜藏在个人金融领域
查看>>
英特尔或推可超频Kaby Lake酷睿i3处理器: 重拾赛扬300A荣光?
查看>>
要想在未来立足 微软等软件公司就必须折本研发硬件
查看>>
QTP使用中的陷阱
查看>>
Cirrus Delaware公司数据中心计划因建设电厂再次受阻
查看>>
前Windows事业部总裁写给CEO和管理者:如何做决策?
查看>>
美国国防部最新报告:美军武器系统可能已经被植入后门
查看>>
Google产品管理副总裁:好产品要不断走出舒适区
查看>>
2016年中国大数据应用将发生质变
查看>>
回忆录:30岁那年,你成长了吗?(上篇)
查看>>