An overview of the

After writing two articles on current capping (” Interview Tutorial “) in succession, what is current capping? Sentinel, the current limiter, don’t you know? After that, I always feel that the last bit of content is still needed to close the whole content related to current limiting. These two days, after searching the relevant articles, I found an easy to understand Sentinel Decrypting article. Thank You for your help ~ to complete this part of content.

1. Sentinel concept

1.1. Code structure

1.2. Core Concepts

Resource

A resource is an abstraction of what Sentinel is protecting. Any code, function, and so on that Sentinel wants to protect can be defined as a resource through the sphu.entry interface. The first parameter of the sphu.entry interface describes the name of the resource

Node

Node is a memory structure used to store statistics in Sentinel. It is stored in a tree structure and hash:

A resource sets up different statistics nodes according to different entry points to store the statistics of context level. A unified cluster node is set under a resource to store resource granularity statistics. Different Origin nodes are attached to each cluster node based on the source, and the statistics of each source are stored.

Different traffic limiting modes in the configuration actually correspond to different nodes for calculation:

  • Direct mode: Select Cluster node
  • Association mode: Select the cluster node associated with the resource
  • Apply laiSource: Select the Origin Node
  • Link mode: Select Default node

The Node type:

  • StatisticNode: The most basic statistics node. It is the parent class of the following three types of nodes and contains two sliding window structures, second and minute.
  • DefaultNode: link node. It is used to collect the data of a resource on the calling link and maintain the tree structure.
  • ClusterNode: cluster point. It is used to collect global data of each resource (call link is not differentiated) and call data of the resource by source
  • EntranceNode: entry node, a special link node that corresponds to all the call data of a Context entry.

Context

Context is the Context in which operations are performed on resources. The same thread is passed through thread local. Context maintains a currentEntry.

Entry

The call point, which holds information related to a call to a particular resource, such as the start execution time of the call, the end time, whether an exception was thrown, the parent-child relationship in the call link, the resource’s process chain (ProcessorSlotChain) corresponding to the call point, and the resource statistics node corresponding to the call point

Slot

Slot is a rule processor that is cascaded through the chain of responsibility mode.

1.3. Core class diagram

Sentinel mainly realizes Node maintenance statistics through Node, and realizes sliding window algorithm through LeapArray.

2. Sentinel process

2.1 Rule configuration

The application implements a variety of listeners by subscribing to the listening mode. The listeners are then updated for different configurations. The main listeners are listed in the figure, such as: rule configuration, basic configuration, cluster configuration, etc.

2.2. Rule validation

The application invokes the entry to start the rule verification process. The main steps are to obtain the context, build and execute the slot chain. If the rule is violated, record the failure count and clear the Contxt. Otherwise, the success count is recorded and the verification success is returned.

3. Sentinel core

3.1. Traffic limiting algorithm

We are familiar with the current limiting algorithm include: 1 counter 2 fixed window 3. Sliding window 4. Leaky bucket 5. Token bucket etc.

  • 1.counter, maintain a counter, access to enter counter +1, if the threshold is exceeded is rejected, access end counter -1.
  • 2,Fixed window, given a time window, maintain a counter, if the number of access in the window is greater than the threshold, reject.
  • 3,The sliding window, fixed window has critical problem, so the idea of sliding window is put forward. A large time window is cut into more fine-grained child Windows, and each child window is counted separately. Every time passing a child window, a child window is slid to the right.
  • 4,bucket, the sliding window cannot solve the smoothness problem. In order to solve the smoothness problem, the leaky bucket algorithm is proposed to control the flow rate.
  • 5,The token bucket: Contrary to the missing bucket algorithm, the token is generated at a fixed speed, the bucket is full and discarded. When requesting access, the token is removed from the bucket. If it can be obtained, the token is passed, otherwise the token is rejected.

In Sentinel, flow limiting ideas such as counter, sliding window and leaky bucket are mainly used. For example, for the flow control of the number of threads, the counter is directly used to limit, and for the limit of QPS, the idea of sliding window is used. Sliding window is also the main flow control method of Sentinel.

3.2. Sliding window

Sentinel supports second-level Windows. The default window size is 1 second. Assuming there are 5 sub-windows, each sub-window size is 200ms. The key implementation of the algorithm is the currentWindow() method mentioned in the previous class diagram:

  • 1. Calculate the timeId based on the current timestamp
  • 2. Obtain the current child window based on timdId
  • 3, if the child window does not exist, create a child window
  • 4. If the child window already exists, calculate the start time according to the current time and compare the start time of the child window. If the start time is consistent, it means that the child window is the current window
  • 5. Sliding is actually resetWidow, including reset windowStartTime, statistics in Window, etc., as shown in the figure above

3.3 priority

The purpose of the priority is to distinguish normal flow from other low priority flows, such as pressure flow. For normal traffic, if the threshold is exceeded and you do not want to reject directly, try to borrow the indicator of the future window. If the borrowing succeeds, the request passes; if the borrowing fails, the request is rejected. When the time window actually reaches a point in time in the future, you need to restore the counter Settings of the borrowed window.

How can the current request be borrowed? As shown in the figure below, the main maintenance of a borrowing time threshold, calculate whether the total number of seconds in the window exceeds QPS, if not, see whether the borrowing time exceeds the borrowing threshold:

3.4 Hotspot Parameters

The definition of general resources needs to be enumerable and not explosive. If flow limiting is applied to each userId, it will lead to the explosive growth of Sentinel’s storage space and finally lead to OOM. Sentinel provides a hotspot parameter scheme for this fine-grained parameter limiting.

As shown in figure:

Hotspot parameters are added in addition to the normal statistics. Both the number of threads and QPS are supported. For the number of threads, each parameter index maintains an LRU cache. For QPS, an LRU cache is maintained for each parameter index, each sub-window, and each statistical latitude, and the various counts are maintained in the cache. LRU cache can avoid memory explosion and achieve the purpose of hot parameter flow limiting.

Pay attention, don’t get lost

Well folks, that’s all for this post, and I’ll be updating it weekly with a few high-quality articles about big factory interviews and common technology stacks. Thank everyone can see here, if this article is well written, please three!! Creation is not easy, thank you for your support and recognition, we will see the next article!

I am Jiuling, there is a need to communicate children’s shoes can add me WX, JayCE-K, follow the public number: Java tutorial, master first-hand information! If there are any mistakes in this blog, please comment and comment. Thank you very much!