Sentinel Circuit Breaker and Fuse Downgrade

foreword

Sentinel's fuse downgrade is realized by circuit breakers. This article summarizes the working principle of circuit breakers by introducing the definition of fuses, how to build fuses, circuit breaker verification logic, circuit breaker state transitions, and how abnormal/slow call fuse traffic is counted. and implementation. 

1. Definition of circuit breaker

 

The fuse downgrade in Sentinel is implemented using circuit breakers. Let's first look at the concept of circuit breakers, from the definition of Wikipedia:

断路器有分简单与较进阶的版本,简单的断路器只需要知道服务是否可用。而较进阶的版本比起前者更有效率。进阶的断路器带有至少三个状态:关闭:断路器在预设的情形下是呈现关闭的状态,而断路器本身“带有”计数功能,每当错误发生一次,计数器也就会进行“累加”的动作,到了一定的错误发生次数断路器就会被“开启”,这个时候亦会在内部启用一个计时器,一旦时间到了就会切换成半开启的状态。开启:在开启的状态下任何请求都会“直接”被拒绝并且抛出异常讯息。半开启:在此状态下断路器会允许部分的请求,如果这些请求都能成功通过,那么就意味着错误已经不存在,则会被“切换回”关闭状态并“重置”计数。倘若请求中有“任一”的错误发生,则会回复到“开启”状态,并且重新计时,给予系统一段休息时间

Description: From the concept, it can be seen that the key point of the circuit breaker is to count the flow and the transition of the three states.


2. Build circuit breakers


When we make the downgrade rule take effect through DegradeRuleManager.loadRules or getProperty().updateValue, it will convert DegradeRule to CircuitBreaker.

The circuit breaker consists of two types, ResponseTimeCircuitBreaker is used for slow calls, and ExceptionCircuitBreaker is used for the number of exceptions and the proportion of exceptions.

Circuit breaker class diagram

3. Circuit breaker calibration


DegradeSlot is responsible for the verification of fuse rules, and the tryPass method performs specific judgments.

the logic of judgment

Returns true if the circuit breaker state is closed, that is, the request is allowed to pass.

If the fuse state is on, and the fuse time has expired and the on state has successfully transitioned to the half-on (probe) state, it returns true, that is, the request is allowed to pass.

If the circuit breaker state is on, and it is still within the fuse duration, it will return false to prohibit the request from passing.

Both the detection and open states allow requests to pass. In the article "Fuse Downgrade Instructions", we know that the detection state only allows one request to pass. Where is this controlled?


4. State transition of circuit breaker


When Entry#exit() is called, the exit call of the slot chain is triggered. It is specific to the DegradeSlot#exit method of fuse downgrade. State switching is performed by the circuitBreaker.onRequestComplete callback circuit breaker.

1. Abnormal fuse state transition
 

ExceptionCircuitBreaker is responsible for the fuse of the number of exceptions/proportion of exceptions, and counts the number of errors and the total number of requests through a sliding window.

Detailed circuit breaker logic

2. Slow call fuse state transition
 

ResponseTimeCircuitBreaker is responsible for the fuse of slow calls, and counts the number of slow calls and the total number of requests through a sliding window.

Detailed circuit breaker logic

五、滑动窗口流量统计

1.异常熔断流量统计
 

异常熔断ExceptionCircuitBreaker的流量统计通过SimpleErrorCounterLeapArray实现,默认采样窗口sampleCount为1,统计区间intervalInMs为1秒。

2.慢调用熔断流量统计
 

慢调用熔断ResponseTimeCircuitBreaker的流量统计通过SlowRequestLeapArray实现,默认采样窗口sampleCount为1,统计区间intervalInMs为1秒。

说明:具体滑动窗口的统计逻辑见文章“Sentinel基于滑动窗口的流量统计【源码笔记】”。



作者介绍:;梁勇,哈罗出行高级技术专家,负责服务框架和消息平台,专注后端中间件方向,公众号“瓜农老梁”维护者。

       

本文分享自微信公众号 - 中间件兴趣圈(dingwpmz_zjj)。
如有侵权,请联系 [email protected] 删除。
本文参与“OSC源创计划”,欢迎正在阅读的你也加入,一起分享。

{{o.name}}
{{m.name}}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324163121&siteId=291194637