sentinel源码分析第九篇一核心流程一lookProcessChain查找资源处理链
原创
©著作权归作者所有:来自51CTO博客作者ren5201313的原创作品,请联系作者获取转载授权,否则将追究法律责任
文章目录
- 源码分析一lookProcessChain
- 源码分析一SlotChainProvider构建slotChain
- 源码分析一slotChainBuilder.build
- 总结
源码分析一lookProcessChain
- 通过chainMap获取resource对应的chain
- 存在则直接返回
- SLOT_CHAIN实例超过6000则返回null,不进行处理
- 通过SlotChainProvider构建chain
- 加入chainMap
ProcessorSlot<Object> lookProcessChain(ResourceWrapper resourceWrapper) {
chainMap 缓存resourceWrapper对应的Slot
ProcessorSlotChain chain = chainMap.get(resourceWrapper);
if (chain == null) {
synchronized (LOCK) {
chain = chainMap.get(resourceWrapper);
if (chain == null) {
如果资源数过多则直接返回null不进行限流处理
if (chainMap.size() >= Constants.MAX_SLOT_CHAIN_SIZE) {
return null;
}
创建新的ProcessorSlotChain
chain = SlotChainProvider.newSlotChain();
构建新的资源和处理器链池
Map<ResourceWrapper, ProcessorSlotChain> newMap = new HashMap<ResourceWrapper, ProcessorSlotChain>(
chainMap.size() + 1);
newMap.putAll(chainMap);
newMap.put(resourceWrapper, chain);
chainMap = newMap;
}
}
}
return chain;
}
源码分析一SlotChainProvider构建slotChain
- 通过spi获取建造者DefaultSlotChainBuilder
public final class SlotChainProvider {
private static volatile SlotChainBuilder slotChainBuilder = null;
public static ProcessorSlotChain newSlotChain() {
step-1: slotChain建造者存在则构建
if (slotChainBuilder != null) {
return slotChainBuilder.build();
}
step-2: spi==spi和默认的都是:DefaultSlotChainBuilder
slotChainBuilder = SpiLoader.loadFirstInstanceOrDefault(SlotChainBuilder.class, DefaultSlotChainBuilder.class);
step-3: 默认策略
if (slotChainBuilder == null) {
slotChainBuilder = new DefaultSlotChainBuilder();
}
return slotChainBuilder.build();
}
}
源码分析一slotChainBuilder.build
public class DefaultSlotChainBuilder implements SlotChainBuilder {
@Override
public ProcessorSlotChain build() {
ProcessorSlotChain chain = new DefaultProcessorSlotChain();
// Note: the instances of ProcessorSlot should be different, since they are not stateless.
List<ProcessorSlot> sortedSlotList = SpiLoader.loadPrototypeInstanceListSorted(ProcessorSlot.class);
/**
*
* 单向链表
* first end
* DefaultProcessorSlotChain --> --> spi
* spi结果:
* 构建node树
* com.alibaba.csp.sentinel.slots.nodeselector.NodeSelectorSlot
* 构建clusterNode
* com.alibaba.csp.sentinel.slots.clusterbuilder.ClusterBuilderSlot
* 日志
* com.alibaba.csp.sentinel.slots.logger.LogSlot
* 统计
* com.alibaba.csp.sentinel.slots.statistic.StatisticSlot
* 授权 白名单 黑名单
* com.alibaba.csp.sentinel.slots.block.authority.AuthoritySlot
* 系统
* com.alibaba.csp.sentinel.slots.system.SystemSlot
* 限流
* com.alibaba.csp.sentinel.slots.block.flow.FlowSlot
* 降级
* com.alibaba.csp.sentinel.slots.block.degrade.DegradeSlot
*/
for (ProcessorSlot slot : sortedSlotList) {
if (!(slot instanceof AbstractLinkedProcessorSlot)) {
RecordLog.warn("The ProcessorSlot(" + slot.getClass().getCanonicalName() + ") is not an instance of AbstractLinkedProcessorSlot, can't be added into ProcessorSlotChain");
continue;
}
chain.addLast((AbstractLinkedProcessorSlot<?>) slot);
}
return chain;
}
}
总结
- lookProcessChain通过spi完成Slot的链式加载
- slot链负责限流的核心工作
- slot链每个资源一个实例
- slot链的数量不可超过6000