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