一、概述
在 Sentinel 里面,所有的资源都对应一个资源名称(resourceName),每次资源调用都会创建一个 Entry 对象。Entry 可以通过对主流框架的适配自动创建,也可以通过注解的方式或调用 SphU API 显式创建。Entry 创建的时候,同时也会创建一系列功能插槽(slot chain),这些插槽有不同的职责,例如:
-
NodeSelectorSlot负责收集资源的路径,并将这些资源的调用路径,以树状结构存储起来,用于根据调用路径来限流降级; -
ClusterBuilderSlot则用于存储资源的统计信息以及调用者信息,例如该资源的 RT, QPS, thread count 等等,这些信息将用作为多维度限流,降级的依据; LogSlot则用于记录用于记录块异常,为故障排除提供具体的日志StatisticSlot则用于记录、统计不同纬度的 runtime 指标监控信息;-
FlowSlot则用于根据预设的限流规则以及前面 slot 统计的状态,来进行流量控制; -
AuthoritySlot则根据配置的黑白名单和调用来源信息,来做黑白名单控制; -
DegradeSlot则通过统计信息以及预设的规则,来做熔断降级; -
SystemSlot则通过系统的状态,例如 load1 等,来控制总的入口流量;
下面是关系结构图
ProcessorSlot子类及实现类.png
二、LogSlot分析
1.LogSlot介绍
官方文档是这样描述LogSlot的:用于记录块异常,为故障排除提供具体的日志
2.源码解读
@Override
public void entry(Context context, ResourceWrapper resourceWrapper, DefaultNode obj, int count, boolean prioritized, Object... args)
throws Throwable {
try {
fireEntry(context, resourceWrapper, obj, count, prioritized, args);
} catch (BlockException e) {
EagleEyeLogUtil.log(resourceWrapper.getName(), e.getClass().getSimpleName(), e.getRuleLimitApp(),
context.getOrigin(), count);
throw e;
} catch (Throwable e) {
RecordLog.warn("Unexpected entry exception", e);
}
}
@Override
public void exit(Context context, ResourceWrapper resourceWrapper, int count, Object... args) {
try {
fireExit(context, resourceWrapper, count, args);
} catch (Throwable e) {
RecordLog.warn("Unexpected entry exit exception", e);
}
}
1.在entry阶段这个slot没有什么业务逻辑,主要就是拦截了业务异常和Throwable级别的错误
2.如果是com.alibaba.csp.sentinel.slots.block.BlockException的异常,则将异常信息用LogSlot专用的EagleEyeLogUtil记录。
3.在exit阶段,出现的日志是没有记录在EagleEyeLogUtil里面的,因为没有关于com.alibaba.csp.sentinel.slots.block.BlockException的异常的捕获和拦截,直接为一个Throwable级别的catch。
作为sentinel 执行序号第三的slot,和之前的NodeSelectorSlot,ClusterBuilderSlot最大的不同是,这个时候已经初始化好了必要的DefaultSlot和ClusterSlot,功能基本完备,处于sentinle 的业务处理阶段,由于责任链的调用模式,这里拦截的信息多为BlockException,我们可以理解为已识别的业务(sentinel的能力域逻辑)异常。所以使用了一个简单粗暴的方式,直接catch住了下面所有slot的异常返回。
多个相同的resource(name),对应着不同的context(name),那么我们就可以快速统计出某个资源的总统计信息。对应着多个(归属于同一个resource的)DefaultNode对应同一个ClusterNode。
三、StatisticSlot分析
1.StatisticSlot介绍
官方文档是这样描述StatisticSlot的:用于存储资源的统计信息以及调用者信息,例如该资源的 RT, QPS, thread count 等等,这些信息将用作为多维度限流,降级的依据,指的就是ClusterNode;
2.源码解读
@Override
public void entry(Context context, ResourceWrapper resourceWrapper, DefaultNode node, int count, boolean prioritized, Object... args) throws Throwable {
if (clusterNode == null) {
synchronized (lock) {
if (clusterNode == null) {//1
// Create the cluster node.
clusterNode = new ClusterNode(resourceWrapper.getName(), resourceWrapper.getResourceType());
HashMap<ResourceWrapper, ClusterNode> newMap = new HashMap<>(Math.max(clusterNodeMap.size(), 16));
newMap.putAll(clusterNodeMap);
newMap.put(node.getId(), clusterNode);//2
clusterNodeMap = newMap;
}
}
}
node.setClusterNode(clusterNode);
/*
* if context origin is set, we should get or create a new {@link Node} of
* the specific origin.
*/
if (!"".equals(context.getOrigin())) {//3
Node originNode = node.getClusterNode().getOrCreateOriginNode(context.getOrigin());
context.getCurEntry().setOriginNode(originNode);
}
fireEntry(context, resourceWrapper, node, count, prioritized, args);
}
1.判断clusterNode是否需要创建,需要就根据rocourceName创建一个
2.将clusterNode插入clusterNodeMap
3.如果指定了origin还会创造来源维度的node,作为origin级别的统计
项目的resource共享了相同的ProcessorSlotChain,无论在关联了哪个上下围,信息都被收集在resource对应的ClusterNode中,clusterNodeMap则存储了所有resource的统计信息。
四、小结
本期我们讲述了Slot的子类LogSlot和StatisticSlot的基本实现原理。
现在建立我们的知识树
实例化DefaultNode和ClusterNode,创建结构树
创建上下文时,首先会在NodeSelectorSlot中判断是否有DefaultNode。
如果没有则新增一个基于resource的DefaultNode,然后执行下一个slot。
下一个slot是ClusterBuilderSlot,ClusterBuilderSlot会判断是否有对应的ClusterNode,如果没有则新增一个基于resource的ClusterNode并继续下一个流程(slot)。
总结来说,这个两个slot奠定了一个基于resource进行全局控制的基调。
进行信息收集
LogSlot在DefaultNode和ClusterNode初始化后,作为业务实例模块的分界点,收集全局异常并处理。
StatisticSlot作为全局统计的实例,依托于ClusterNode,将全局的RT, QPS, thread count 等等信息存放在clusterNodeMap里面。









网友评论