sentinel---implementation principle of sliding window

Sentinel has a variety of rules, including: downgrade, current limiting, hotspot and other rules. These rules all involve the time factor, that is, various actions after the request volume per unit time meets various conditions.

Here we explore the implementation of sliding windows in sentinel.

 The above is a schematic diagram of a sliding window.

Let’s not talk about the implementation code of the sliding window in sentinel. Let’s think about a problem clearly and sort out the ideas for subsequent reading of the sliding window code.

 What problem is solved using sliding windows?

We use sliding windows to: count data in a time unit

To count each time unit, it involves the definition of the time unit (whether it is 1 second or 100ms), and statistical data requires a container to load the data. At the same time, in order to avoid the surge of data at a certain time node and reduce statistical errors, You may also need to divide a " unit time " into smaller pieces

Involved classes

com.alibaba.csp.sentinel.slots.statistic.base.LeapArray

LeapArray is the base class of sliding time window in sentinel. First, let’s take a look at the class definition of this class:

 Involves 3 basic properties and a reference type

Among them: windowLengthInMs, sampleCount, intervalInMs are related to the unit time and the cutting of unit time we mentioned above. The array is used to store data, that is: the data storage container within the unit time.

Among them, WindowWrap, as its name implies: is the packaging class of time window data.

Constructor of LeapArray:

 After reading the above two paragraphs, we make the following assumption: we need to count the data within 1000ms. Assume that we divide the unit time of 1000ms into two segments, each segment is 500ms, and the data of each segment is stored in a windowWrapper.

Then when at a certain point in time, we have data that needs to be stored:

1 Need to calculate which time window the time node belongs to (two 500ms segments)

2 Need to calculate which windowWrap corresponds to the time node

The following two methods can solve the above two problems: 

Imagine the following: when the time node reaches 1001: the calculated start position (calculated by the calculateWindowStart method) is: 1000, and the calculated starting position of 1002 is also: 1000. At the same time, the position of the data calculated by 1001 and 1002 in the array (the result of calculateTimeIdx) is also the same, both are: 0

It is not difficult to see from this that the key data we need have been calculated by the above two methods: the starting position of the time window (because the window length is the same, the end position must be the same), the container corresponding to the time window

So, if we want to get the data of the current time node and the calculation method of adding data at the current time node:

First, calculate two key data: container subscript: idx, time window starting position: windowStart

 When the container data is removed from the data, it is empty, a new container is created and stored in the array. Of course, this involves the use of optimistic locking

 When it is not empty, and the current time is the same as the actual location of the old container: the time difference between the event node and a previous operation is within 500ms, and the same container is used to store data

 If the current time node is more than 500ms (or even longer) than the last operation time node, modify the time starting position in the container and reset the container's data (involving the use of pessimistic locks)

The above is the core logic of sentinel's sliding time window

When we need to count data within 1 unit of time where the current time node is located:

 

That is: when we need to obtain the data of the current time node: read the data in the array directly, and if the time starting point of the windowWrap data in the array and the time starting point of the current time do not exceed intervalInMs (in a time unit), take it out

When we need to add data to the container: we found the implementation in the subclass of LeayArray (OccupiableBucketLeapArray):

The above is: the core logic of sentinel’s sliding time window implementation

Guess you like

Origin blog.csdn.net/qq_39203337/article/details/132165631