Sentinel 源码学习
引入依赖
<dependency>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-core</artifactId>
<version>1.8.7</version>
</dependency>
基本用法
try (Entry entry = SphU.entry("HelloWorld")) {
// 被保护的逻辑
System.out.println("hello world");
} catch (BlockException ex) {
// 处理被流控的逻辑
System.out.println("blocked!");
}
接下来,阅读源码,我们从SphU.entry()开始






每个SphU#entry()将返回一个Entry。这个类维护了当前调用的一些信息:
- createTime :这个entry的创建时间,用于响应时间统计
- current Node :在当前上下文中的资源的统计
- origin Node :原始节点的统计
- ResourceWrapper :资源名称
CtSph#entryWithPriority()方法就是整个流控的基本流程:
1、首先,获取当前线程上下文,如果为空,则创建一个
2、然后,查找处理器链
3、最后,依次执行处理器
这是一个典型的责任链
接下来,挨个来看,首先看一下上下文。上下文是一个线程局部变量 ThreadLocal<Context>

如果当前线程还没有上下文,则创建一个


有了Context之后,接下来查找处理器





这些功能插槽(slot chain)有不同的职责:
- NodeSelectorSlot :负责收集资源的路径,并将这些资源的调用路径,以树状结构存储起来,用于根据调用路径来限流降级;
- ClusterBuilderSlot :用于存储资源的统计信息以及调用者信息,例如该资源的 RT, QPS, thread count 等等,这些信息将用作为多维度限流,降级的依据;
- StatisticSlot :用于记录、统计不同纬度的 runtime 指标监控信息;
- FlowSlot :用于根据预设的限流规则以及前面 slot 统计的状态,来进行流量控制;
- AuthoritySlot :根据配置的黑白名单和调用来源信息,来做黑白名单控制;
- DegradeSlot :通过统计信息以及预设的规则,来做熔断降级;
- SystemSlot :通过系统的状态,例如 load1 等,来控制总的入口流量;
到这里为止,资源有了,上下文有了,处理器链有了,于是,接下来就可以对资源应用所有的处理器了












关于功能插槽的学习就先到这里,下面补充一个知识点:Node

Node 用于保存资源的实时统计信息
StatisticNode 保存三种实时统计指标:
- 秒级指标
- 分钟级指标
- 线程数
DefaultNode 用于保存特定上下文中特定资源名称的统计信息
EntranceNode 代表调用树的入口
总之一句话,Node是用于保存统计信息的。那么,这些指标数据是如何计数的呢?


Sentinel 使用滑动窗口实时记录和统计资源指标。ArrayMetric背后的滑动窗口基础结构是LeapArray。
下面重点看一下StatisticNode
StatisticNode是用于实时统计的处理器插槽。在进入这个槽位时,需要分别计算以下信息:
- ClusterNode :该资源ID的集群节点统计信息总和
- Origin node :来自不同调用者/起源的集群节点的统计信息
- DefaultNode :特定上下文中特定资源名称的统计信息
- 最后,是所有入口的总和统计







private int calculateTimeIdx(/*@Valid*/ long timeMillis) {
long timeId = timeMillis / windowLengthInMs;
// Calculate current index so we can map the timestamp to the leap array.
return (int)(timeId % array.length());
}
protected long calculateWindowStart(/*@Valid*/ long timeMillis) {
return timeMillis - timeMillis % windowLengthInMs;
}
/**
* Get bucket item at provided timestamp.
*
* @param timeMillis a valid timestamp in milliseconds
* @return current bucket item at provided timestamp if the time is valid; null if time is invalid
*/
public WindowWrap<T> currentWindow(long timeMillis) {
if (timeMillis < 0) {
return null;
}
int idx = calculateTimeIdx(timeMillis);
// Calculate current bucket start time.
long windowStart = calculateWindowStart(timeMillis);
/*
* Get bucket item at given time from the array.
*
* (1) Bucket is absent, then just create a new bucket and CAS update to circular array.
* (2) Bucket is up-to-date, then just return the bucket.
* (3) Bucket is deprecated, then reset current bucket.
*/
while (true) {
WindowWrap<T> old = array.get(idx);
if (old == null) {
/*
* B0 B1 B2 NULL B4
* ||_______|_______|_______|_______|_______||___
* 200 400 600 800 1000 1200 timestamp
* ^
* time=888
* bucket is empty, so create new and update
*
* If the old bucket is absent, then we create a new bucket at {@code windowStart},
* then try to update circular array via a CAS operation. Only one thread can
* succeed to update, while other threads yield its time slice.
*/
WindowWrap<T> window = new WindowWrap<T>(windowLengthInMs, windowStart, newEmptyBucket(timeMillis));
if (array.compareAndSet(idx, null, window)) {
// Successfully updated, return the created bucket.
return window;
} else {
// Contention failed, the thread will yield its time slice to wait for bucket available.
Thread.yield();
}
} else if (windowStart == old.windowStart()) {
/*
* B0 B1 B2 B3 B4
* ||_______|_______|_______|_______|_______||___
* 200 400 600 800 1000 1200 timestamp
* ^
* time=888
* startTime of Bucket 3: 800, so it's up-to-date
*
* If current {@code windowStart} is equal to the start timestamp of old bucket,
* that means the time is within the bucket, so directly return the bucket.
*/
return old;
} else if (windowStart > old.windowStart()) {
/*
* (old)
* B0 B1 B2 NULL B4
* |_______||_______|_______|_______|_______|_______||___
* ... 1200 1400 1600 1800 2000 2200 timestamp
* ^
* time=1676
* startTime of Bucket 2: 400, deprecated, should be reset
*
* If the start timestamp of old bucket is behind provided time, that means
* the bucket is deprecated. We have to reset the bucket to current {@code windowStart}.
* Note that the reset and clean-up operations are hard to be atomic,
* so we need a update lock to guarantee the correctness of bucket update.
*
* The update lock is conditional (tiny scope) and will take effect only when
* bucket is deprecated, so in most cases it won't lead to performance loss.
*/
if (updateLock.tryLock()) {
try {
// Successfully get the update lock, now we reset the bucket.
return resetWindowTo(old, windowStart);
} finally {
updateLock.unlock();
}
} else {
// Contention failed, the thread will yield its time slice to wait for bucket available.
Thread.yield();
}
} else if (windowStart < old.windowStart()) {
// Should not go through here, as the provided time is already behind.
return new WindowWrap<T>(windowLengthInMs, windowStart, newEmptyBucket(timeMillis));
}
}
}
现在,有2个窗口,每个窗口500ms,2个窗口总共1000ms
假设,当前时间戳是1200ms,那么 (1200 / 500) % 2 = 0, 1200 - 1200 % 500 = 1000
这个时候,如果0这个位置没有窗口,则创建一个新的窗口,新窗口的窗口开始时间是1000ms
如果0这个位置有窗口,则继续判断旧窗口的窗口开始时间是否为1000ms,如果是,则表示窗口没有过期,直接返回该窗口。如果旧窗口的开始时间小于1000ms,则表示旧窗口过期了,于是重置旧窗口的统计数据,重新设置窗口开始时间(PS:相当于将窗口向后移动)


窗口(桶)数据保存在MetricBucket中




总结一下:
1、每个线程过来之后,创建上下文,然后依次经过各个功能插槽
2、每个资源都有自己的处理器链,也就是说多次访问同一个资源时,用的同一套处理器链(插槽)
3、Node相当于是一个载体,用于保存资源的实时统计信息
4、第一次进入插槽后,创建一个新Node,后面再补充Node的信息;第二次进入的时候,由于上下文的名称都是一样的,所以不会再创建Node,而是用之前的Node,也就是还是在之前的基础上记录统计信息。可以这样理解,每个DefaultNode就对应一个特定的资源。
5、StatisticNode中保存三种类型的指标数据:每秒的指标数据,每分钟的指标数据,线程数。
6、指标数据统计采用滑动窗口,利用当前时间戳和窗口长度计算数据应该落在哪个窗口数组区间,通过窗口开始时间判断窗口是否过期。实际数据保存在MetricBucket中
最后,千言万语汇聚成这张原理图

NodeSelectorSlot构造调用链路,ClusterBuilderSlot构造统计节点,StatisticSlot利用滑动窗口进行指标统计,然后是流量控制
参考文档
https://sentinelguard.io/zh-cn/docs/quick-start.html
https://sentinelguard.io/zh-cn/docs/basic-implementation.html
https://sentinelguard.io/zh-cn/docs/dashboard.html
https://blog.csdn.net/xiaolyuh123/article/details/107937353
https://www.cnblogs.com/magexi/p/13124870.html
https://www.cnblogs.com/mrxiaobai-wen/p/14212637.html
https://www.cnblogs.com/taromilk/p/11750962.html
https://www.cnblogs.com/taromilk/p/11751000.html
https://www.cnblogs.com/wekenyblog/p/17519276.html
https://javadoop.com/post/sentinel
https://www.cnblogs.com/cuzzz/p/17413429.html
Sentinel 源码学习的更多相关文章
- 5.Sentinel源码分析—Sentinel如何实现自适应限流?
Sentinel源码解析系列: 1.Sentinel源码分析-FlowRuleManager加载规则做了什么? 2. Sentinel源码分析-Sentinel是如何进行流量统计的? 3. Senti ...
- 6.Sentinel源码分析—Sentinel是如何动态加载配置限流的?
Sentinel源码解析系列: 1.Sentinel源码分析-FlowRuleManager加载规则做了什么? 2. Sentinel源码分析-Sentinel是如何进行流量统计的? 3. Senti ...
- Java集合专题总结(1):HashMap 和 HashTable 源码学习和面试总结
2017年的秋招彻底结束了,感觉Java上面的最常见的集合相关的问题就是hash--系列和一些常用并发集合和队列,堆等结合算法一起考察,不完全统计,本人经历:先后百度.唯品会.58同城.新浪微博.趣分 ...
- jQuery源码学习感想
还记得去年(2015)九月份的时候,作为一个大四的学生去参加美团霸面,结果被美团技术总监教育了一番,那次问了我很多jQuery源码的知识点,以前虽然喜欢研究框架,但水平还不足够来研究jQuery源码, ...
- MVC系列——MVC源码学习:打造自己的MVC框架(四:了解神奇的视图引擎)
前言:通过之前的三篇介绍,我们基本上完成了从请求发出到路由匹配.再到控制器的激活,再到Action的执行这些个过程.今天还是趁热打铁,将我们的View也来完善下,也让整个系列相对完整,博主不希望烂尾. ...
- MVC系列——MVC源码学习:打造自己的MVC框架(三:自定义路由规则)
前言:上篇介绍了下自己的MVC框架前两个版本,经过两天的整理,版本三基本已经完成,今天还是发出来供大家参考和学习.虽然微软的Routing功能已经非常强大,完全没有必要再“重复造轮子”了,但博主还是觉 ...
- MVC系列——MVC源码学习:打造自己的MVC框架(二:附源码)
前言:上篇介绍了下 MVC5 的核心原理,整篇文章比较偏理论,所以相对比较枯燥.今天就来根据上篇的理论一步一步进行实践,通过自己写的一个简易MVC框架逐步理解,相信通过这一篇的实践,你会对MVC有一个 ...
- MVC系列——MVC源码学习:打造自己的MVC框架(一:核心原理)
前言:最近一段时间在学习MVC源码,说实话,研读源码真是一个痛苦的过程,好多晦涩的语法搞得人晕晕乎乎.这两天算是理解了一小部分,这里先记录下来,也给需要的园友一个参考,奈何博主技术有限,如有理解不妥之 ...
- 我的angularjs源码学习之旅2——依赖注入
依赖注入起源于实现控制反转的典型框架Spring框架,用来削减计算机程序的耦合问题.简单来说,在定义方法的时候,方法所依赖的对象就被隐性的注入到该方法中,在方法中可以直接使用,而不需要在执行该函数的时 ...
- ddms(基于 Express 的表单管理系统)源码学习
ddms是基于express的一个表单管理系统,今天抽时间看了下它的代码,其实算不上源码学习,只是对它其中一些小的开发技巧做一些记录,希望以后在项目开发中能够实践下. 数据层封装 模块只对外暴露mod ...
随机推荐
- 【Tomcat 组成与工作原理】
Tomcat组成与工作原理 Tomcat 是什么 开源的 Java Web 应用服务器,实现了 Java EE(Java Platform Enterprise Edition)的部 分技术规范,比如 ...
- [转帖]彻底搞明白 GB2312、GBK 和 GB18030
https://zhuanlan.zhihu.com/p/453675608 日常工作的过程中,关于字符编码的问题经常让人头疼不已,这篇文章就来捋一捋关于 GB2312.GBK.GB18030 相关的 ...
- [转帖]Oracle 12.2 新特性 | PDB不同字符集变更
https://www.cnblogs.com/cqdba/p/8bef7c432b87807c0680d6791f427b09.html 在oracle12.1版本中,同一CDB中的所有PDB使用的 ...
- [转帖]ntp导致的时钟回拨
https://zhuanlan.zhihu.com/p/587313130 我们的服务器时间校准一般是通过ntp进程去校准的.但由于校准这个动作,会导致时钟跳跃变化的现象.而这种情况里面,往往回拨最 ...
- [转帖]Nginx 性能优化
目录 1.调整 worker 进程数 2.调整 worker 连接数 3.调整 work 进程最大打开文件数 4.开启高效文件传输模式 5.限制文件上传大小 6.开启 gzip 压缩 7.本地缓存静态 ...
- [转帖]Windows自带MD5 SHA1 SHA256命令行工具
https://www.cnblogs.com/huangrt/p/13961399.html 检验工具http://www.zdfans.com/html/4346.html HashMyFiles ...
- [转帖]如何升级vCenter(VCSA)补丁?
https://www.dinghui.org/upgrade-vcsa-patch.html VCSA的升级分为repo源和iso镜像两种方式,由于使用repo源进行升级只支持HTTPS和FTPS协 ...
- [转帖]Prometheus 都可以采集那些指标?-- 常用 Exporter 合集
Prometheus 可以通过各种 Exporter 来获取很多指标,并且只要符合 Prometheus 规范的都可以获取到,本文汇总一些常用的采集器到这里. Prometheus Exporter ...
- 【转帖】Linux 调优篇 :虚拟化调优(irqbalance 网卡中断绑定)* 贰
一.网络流量上不去二.中断绑定2.1 关闭中断平衡守护进程2.2 脱离中断平衡守护进程2.3 手动设置中断的CPU亲和性三. 总结 一.网络流量上不去 在Linux的网络调优方面,如果你发现网络流量上 ...
- [转帖]龙芯总强调同频性能,是因奔腾4要到25+GHz才比得上酷睿11代单核
https://baijiahao.baidu.com/s?id=1734320620568707041 闲话不说,先上图,是SPEC CPU 2006 int(单任务)测试的成绩: 上图中的成绩 ...