Sentinel实时数据采集
要实现限流需要进行数据采集,这一篇文章将重点解析数据采集的实现原理。
由上图可知,Sentinel中负责数据采集StatisticSlot是在责任链靠前的位置,StatisticSlot组件负责收集调用信息为后面的Slot进行相应的限流、熔断提供支撑,可以说StatisticSlot是Sentinel中核心之一。
StatisticSlot类的注释,当进入这个Slot时我们需要分别计算以下各项:
-
- 资源ID的集群节点的总统计信息
- 来自不同调用者/来源的群集节点的统计信息
- 特定上下文中特定资源名称的统计信息
- 统计所有入口的总和
1. StatisticSlot源码分析
1.1 StatisticSlot entry 详解
@Override
public void entry(Context context, ResourceWrapper resourceWrapper, DefaultNode node, int count,
boolean prioritized, Object... args) throws Throwable {
try {
// Do some checking.
fireEntry(context, resourceWrapper, node, count, prioritized, args);//@1
// Request passed, add thread count and pass count.
node.increaseThreadNum();//@2
node.addPassRequest(count);
if (context.getCurEntry().getOriginNode() != null) {//@3
// Add count for origin node.
context.getCurEntry().getOriginNode().increaseThreadNum();
context.getCurEntry().getOriginNode().addPassRequest(count);
}
if (resourceWrapper.getEntryType() == EntryType.IN) {//@4
// Add count for global inbound entry node for global statistics.
Constants.ENTRY_NODE.increaseThreadNum();
Constants.ENTRY_NODE.addPassRequest(count);
}
// Handle pass event with registered entry callback handlers.
for (ProcessorSlotEntryCallback<DefaultNode> handler : StatisticSlotCallbackRegistry.getEntryCallbacks()) { //@5
handler.onPass(context, resourceWrapper, node, count, args);
}
} catch (PriorityWaitException ex) {//@6
node.increaseThreadNum();
if (context.getCurEntry().getOriginNode() != null) {
// Add count for origin node.
context.getCurEntry().getOriginNode().increaseThreadNum();
}
if (resourceWrapper.getEntryType() == EntryType.IN) {
// Add count for global inbound entry node for glob