介绍
这篇文章主要介绍”阿里巴巴哨兵LeapArray源码分析”,在日常操作中,相信很多人在阿里巴巴哨兵LeapArray源码分析问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答“阿里巴巴哨兵LeapArray源码分析”的疑惑有所帮助!接下来,请跟着小编一起来学习吧!
最近在使用阿里巴巴前哨来做服务的限流,熔断和降级。一直有一个比较好奇的点,哨兵是如果做到高效的数据统计的。通过官方文档介绍:
- <李>
<代码> StatisticSlot:则用于记录,统计不同纬度的运行时指标监控信息;(做实时统计)代码>
李> <李><代码>哨兵底层采用高性能的滑动窗口数据结构代码> LeapArray <代码>来统计实时的秒级指标数据,可以很好地支撑写多于读的高并发场景。代码>
李>由此可以发现哨兵使用了滑动窗口算法来做数据统计,并且具体实现是在<代码> LeapArray> 代码类中。
哨兵总体的框架如下:
通过架构图我们可以看到<代码> StatisticSlot> 代码中的<代码> LeapArray 代码>采用了一个环性数组的数据结构,这个和一致性哈希算法的图类似,如图:
在这个结构中,每一个下标位就代表一个滑动窗口,至于这个窗口是怎么滑动的我们可以结合原来看。
LeapArray源码
源码路径
<代码> StatisticSlot> 代码作为统计的入口,在其<代码>条目()代码>方法中我们可以看到<代码> StatisticSlot> 代码会使用<代码> StatisticNode> 代码,然后<代码> StatisticNode> 代码回去引用<代码> ArrayMetric> 代码,最终使用<代码> LeapArray> 代码。
根据当前时间获取滑动窗口
public WindowWrap, currentWindow (long timeMillis), { ,,,if (timeMillis & lt;, 0), { ,,,,,,,return 零; ,,,} ,,,//,根据当前时间计算出当前时间属于那个滑动窗口的数组下标 ,,,int idx =, calculateTimeIdx (timeMillis); ,,,//,根据当前时间计算出当前滑动窗口的开始时间 ,,,long windowStart =, calculateWindowStart (timeMillis); ,,/* ,,,,*,根据下脚标在环形数组中获取滑动窗口(桶) ,,,, ,,,,*,(1),如果桶不存在则创建新的桶,并通过中科院将新桶赋值到数组下标位。 ,,,,*,(2),如果获取到的桶不为空,并且桶的开始时间等于刚刚算出来的时间,那么返回当前获取到的桶。 ,,,,*,(3),如果获取到的桶不为空,并且桶的开始时间小于刚刚算出来的开始时间,那么说明这个桶是上一圈用过的桶,重置当前桶 ,,,,*,(4),如果获取到的桶不为空,并且桶的开始时间大于刚刚算出来的开始时间,理论上不应该出现这种情况。 ,,,*/,,,while (真实),{ ,,,,,,,WindowWrap , old =, array.get (idx); ,,,,,,,if (old ==, null), { ,,,,,,,,,,/* ,,,,,,,,,,,,*,,,,,B0 ,,,,,, B1 ,,,,, B2 ,,, NULL ,,,, B4 ,,,,,,,,,,,,*,| | - |累积| |累积| | | ___ ,,,,,,,,,,,,*,200,,,,,400,,,,,600,,,,,800,,,,,1000,,,,1200,,时间戳 ,,,,,,,,,,,,*,,,,,,,,,,,,,,,,,,,,,,,,,,,,^ ,,,,,,,,,,,,*,,,,,,,,,,,,,,,,,,,,,,,,,,时间=888 ,,,,,,,,,,,,*,,,,,,,,,,,,bucket is 空,,so create new 以及更新 ,,,,,,,,,,,, ,,,,,,,,,,,,*,If 从而old bucket is 缺席,,then create 我方表示歉意a new bucket at {@code windowStart}, ,,,,,,,,,,,,*,then try 用update circular array via a CAS 操作只Only one thread 可以 ,,,,,,,,,,,,*,succeed 用更新,while other threads 油品收率its time 切片。 ,,,,,,,,,,,*/null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null 阿里巴巴哨兵LeapArray源码分析