Sentinel源码分析
2023-12-26 18:51:50 22 举报
sentinel源码分析
作者其他创作
大纲/内容
StatisticSlot#entry()node.addPassRequest(count);
StatisticSlot#entry()catch (BlockException e) { node.increaseBlockQps(count);
StatisticNode#addPassRequest() rollingCounterInSecond.addPass(count);
分析结束
Y
MetricBucket#add()counters[event.ordinal()].add(n); return this;
LeapArray#calculateTimeIdx()long timeId = timeMillis / windowLengthInMs;return (int)(timeId % array.length());
<dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibab-sentinel</artifactId> </dependency>
CtSph#entryWithPriority()//查找slotchainProcessorSlot<Object> chain = lookProcessChain(resourceWrapper);
DefaultNode#addPassRequest()super.addPassRequest(count);
前面已分析
DefaultNode#increaseThreadNum()super.increaseThreadNum();
滑动时间窗算法
LeapArray#currentWindow()calculateWindowStart(timeMillis);
ArrayMetric#addPass()WindowWrap<MetricBucket> wrap = data.currentWindow();
该方法会将槽链中后续所有slot槽节点执行完毕,然后才会返回到该方法语句后继续执行,将该请求添加到统计数据中。不过,在后续槽点执行过程中,可能会抛出各种异常,而这些异常会在该slot中被捕获,然后将该异常添加到统计数据中。下面将详细分析FlowSlot与DegradeSlot
SentinelAutoConfiguration#SentinelResourceAspectnew SentinelResourceAspect()
FlowRuleChecker#checkFlow()ruleProvider.apply(resource.getName());
AbstractCircuitBreaker#retryTimeoutArrived()TimeUtil.currentTimeMillis() >= nextRetryTimestamp;
AbstractCircuitBreaker#fromOpenToHalfOpen()//通过CAS将状态由open修改为half_open,修改成功,则返回true,否则返回false
LeapArray#currentWindow()calculateTimeIdx(timeMillis)
LongAdder#increment() add(1L);
StatisticNode#increaseThreadNum()curThreadNum.increment();
通过SPI加载以下三个模块中com.alibaba.csp.sentinel.slotchain.ProcessorSlot文件的9个Slot* sentinel-core* sentinel-adapter#sentinel-api-gateway-adapter-common* sentinel-extension#sentinel-paramter-flow-control
SlotChainProvider#newSlotChain()slotChainBuilder.build()
Anonymous in ruleProvier in FlowSlot#apply
LeapArray#currentWindow()WindowWrap<MetricBucket> wrap = data.currentWindow();
ArrayMetric#addPass()wrap.value().addPass(count);
DegradeSlot#performChecking()cb.tryPass(context);
StatisticSlot#entry()node.increaseThreadNum();
CtSph#lookProcessChain()SlotChainProvider.newSlotChain()
AbstractCircuitBreaker#tryPass()fromOpenToHalfOpen(context)
SentinelResourceAspect#@Around(\"sentinelResourceAnnotationPointcut()\")public Object invokeResourceWithSentinel(ProceedingJoinPoint pjp) throws Throwable
DefaultSlotChainBuilder#build()将SPI加载的所有Slot通过以下方法逐个添加到SlotChainchain.addLast((AbstractLinkedProcessorSlot<?>) slot);
开始
BucketLeapArray#resetWindowTo()//更新窗口起始时间w.resetTo(startTime);//将多维度统计数据清零w.value().reset();return w;
canPass()方法是,设置不同的流控效果,会执行不同的controller的实现。* DefaultController :快速失败* RateLimiterController:排队等待* WarmUpController:warm up* WarmUpRateLimiterController:warm up+排队等待
sentinel滑动时间算法源码解析
AbstractCircuitBreaker#tryPass()retryTimeoutArrived();
LeapArray#calculateWindowStart()timeMillis - timeMillis % windowLengthInMs;
spring.factories
StatisticSlot#entry()catch (PriorityWaitException ex) { node.increaseThreadNum();
通过SPI加载sentinel-core模块中com.alibaba.csp.sentinel.slotchain.SlotChainBuilder文件的DefaultSlotChainBuilder
windowStart > old.windowStart()
0 条评论
下一页