Last Sentinel introduces the basic concepts, and introduces the basic usage in the last article. This will be the main flow and usage instructions do realize that some of the major source involved in the sentinel-core module.
1.token get
As for the main flow of the acquired token, will first obtain the first thread context object Context, and then find the corresponding processing tank in accordance with the ResourceName chain, after obtaining SlotChain, generating Entry operation invocation of the objects, the associated object will correspond SlotChain. Internal calls SlotChain the entry way for entry into each slot action, follow-up required exit Entry method calls, allowing exit of action into each slot SlotChain.
Wherein the third step of generating object CtEntry Entry object model is a chain thereof, the operation will be generated every entry in series Entry object
Above, each new a CtEntry, will pass context object. Since each operation sets the current context of the assignment Entry curEntry, new one every time, checks this property, if it is empty, then the first node is copied directly to curEntry; if not empty, then the value on a node, the value is copied to the current value of the parent, and the child node points to the current value. After these actions are done context curEntry points to the current node. DETAILED procedure illustrated above.
Performing entry.exit, determines whether the internal time of entry context.curEntry is performed, in order to control the move holding LIFO exit sequence. If it is judged not through, not performed in the order described LIFO exit, the entry will begin execution, one by one to the root exit, and an exception is thrown. If it is determined by calling the corresponding execution SlotChain exit, and change context.curEntry, which points to the parent of the current node, but the relationship is not released Entry chain.
2. Find a treatment tank chain
We will find out whether the chain has been processed groove corresponding to the resources to start the implementation of the local cache, if not, then re-generate a new. New load, the use of SPI, find the system provided SlotChainBuilder achieve, if achieved in addition to the default DefaultSlotChainBuilder in, then the first, otherwise the default Builder. Builder default treatment tank chain are as follows
3. Perform chain process tank
Process tank as follows:
ProcessorSlotChain as a linked list, the entry will be method of performing slot Slot into the interior of the inside can be performed by fireEntry next slot of the list entry process (if present). As above, before and after fireEntry each slot may have its own processing logic, thereby forming a structure similar filter chain. Similarly, exit the process is similar
3.1. NodeSelectorSlot
Responsible path to collect resources, and these resources call path, stored in a tree structure, according to calls for limiting the path to demote; the action takes place in front of fireEntry action.
The following code corresponding to the constructed call path:
You will first get thread context corresponding Context execution SphU.entry, if not then add one. For node1C, direct calls SphU.entry, will automatically generate a default Context, internal calls ContextUtil.enter, and provided EntranceNode (sentinel_default_context), then access to the node list EntranceNode child virtual EntranceNode (machine-root) of . For node2A and node3A, because the call ContextUtil.enter, corresponding to the display specified Context, and set up EntraceNode (entrance1) and EntraceNode (entrance2). SphU.entry EntryType is not specified, it is set to EntryType OUT.
Codes are as follows:
NodeSelectorSlot will be used to the same object the realization, by virtue of sharing the same ProcessorSlotChain objects of the same resources, so different Context call the same resources. Code directly using the equivalent ContextName ResourceName-ContextName judgment. In order to construct a call chain corresponding Context, internal maintains a Map <String, DefaultNode>, where key corresponding thread contexts ContextName, value for each call context Node chain. For resource first visit, will add a Node in the corresponding Context chain, and as a child node on the link to the chain's recent visit to that node, thus completing the construction of the call chain. For recurring resources, the order will only use the first occurrence. The node Node incoming slot obtained in each subsequent processing tank.
3.2.ClusterBuilderSlot
Statistics, and caller information for storage resources, such as the RT resources, QPS, thread count, etc. This information will be used as a multi-dimensional limit, according to the downgrade; the action takes place in front of fireEntry action.
The above example will pass through the slot as the new node ClusterNode
Mentioned above, by virtue of sharing the same ProcessorSlotChain objects of the same resources, so different Context will be used to invoke the same NodeSelectorSlot object with a resource, Cluster for statistical information about the kinds of resources directly to the use of a ClusterNode node represents.
The realization, ClusterBuilderSlot also holds a static ClusterNodeMap, for ClusterNode all cached information resources. When through the slot, the node will determine whether there is information about the resource Map, not the new one.
The above content is still some information content Origin node settings node. As shown below, ClusterNode statistics statistics for the same resources, without distinguishing between different sources of Context, internal use statistics to distinguish between different originCountMap sources.
For the default sentinel_default_context, which orgin set to null ( ""), and therefore the Context Cluster no information of Origin
3.3.LogSlot
A print log, the event log specific output stream or downgrade limit; fireEntry the action occurs after the operation.
3.4.StatisticSlot
For recording, statistical indicators runtime monitoring information from different latitudes; the action takes place after fireEntry action.
The operation occurs in Slot fireEntry after SlotChain executes according to the above, the operation will be executed after the inspection performed subsequent Slot. Follow-up inspections include checking permissions, system metrics, limiting, and user-defined rules to downgrade.
Below, the operation of Slot follows:
If successful follow-up examination after each Slot, equivalent to get a token, it will update the statistics, including increasing the number of (ThreadNum) thread, by increasing the number of requests (PassRequest), nodes involved include:
Current node; Origin of the current node (if present)
Global Entrance_Node node (if the current node type EntryType.IN); onPass after calling the callback function.
Other cases As shown, comprising:Acquiring token set priority strategy, waiting for the timeout throws PriorityWaitExeption (Note:? Why only increase ThreadNum without increasing PassRequest, but performed onPass callback function here PriorityWatitException not BlockException, thrown PriorityWaitException, which has acquired the token request , subsequent operations may be performed, but not in the current window, this description will follow)
Slot rules do not follow through, throw BlockException
Other exceptions, this time will set the value of the current node Error occurred, to make a judgment for the exit action
Exit action follows, this behavior occurs before fireExit, response time statistics success, minus the number of threads to obtain the token, increasing the number of successful requests (SuccessRequest)
Specific statistical methods, follow-up will do StatisticNode explained.
3.5.SystemSlot
By the state of the system, e.g. load1 the like to control the total flow of inlet;
Check the current system index is normal, just check inlet flow node. QPS including global, overall number of threads, the overall average response time, system load, CPU load.
3.6.AuthoritySlot
According to the configuration of black and white lists and call information sources, do black and white list control;
3.7.FlowSlot
According to a preset limiting state rules and statistical front slot, to control the flow rate;
First will be according to the rules set mode, mode selection process, there are Local and Cluster are two types Here we introduce to Local mode.
When Local mode, select the first node statistics, reacquisition token set in accordance with the flow restrictor, to achieve the purpose of limiting.
3.7.1 Select statistics node
This step will be selected depending on the application node corresponding to a given range of settings, and limiting policy.
Here to introduce default application range and limiting policy, divided into:
Applications: default, other
Limiting policy: DIRECT, RELATE, CHAIN
When selected, the corresponding node will be selected according to the caller LimitApp. If the rule is applied to the origin and in addition to default and the other, if it is DIRECT strategy, return to origin node; if acting on default, if it is DIRECT strategy, returns Cluster nodes; if acting on the other, if it is DIRECT strategy, return to origin node. The selection process above other conditions are the same, namely: if the resource name is empty, return empty; if RELATE strategy is to use ClusterNode node data; if it is CHIAN strategy, and consistent with the rules of the current node name name, using the current node data otherwise empty, the details can look at the code.
After obtaining the data node, you can use the data flow restrictor check node specified in the rule, to obtain the token.
3.7.2 The token acquisition flow restrictor
The system provides a flow restrictor comprising:
The default current limiter: direct refusal
The default flow restrictor by way of direct refusal, if the current rule has been acquired and the number of token token number is greater than the need to set directly rejected, and the number of threads supported by QPS to count. When the count by QPS, supports priority mode allows the use of reference to the previous, in a certain desired time, the token borrowed from subsequent time, request to ensure that the current through. Support priority mode can make full use of system resources, as much as possible to accept the request, the request is to prevent unnecessary interception
Pre-restrictor: Reference guava, offers warm / cold start function of a token bucket method
Reference guava preheater flow restrictor, calculated according to the number of requests. Token start number is not reached the upper limit, but there is a preheating process in the preheating time, the token generation rate is fixed, if after that time exceeds the number of available token according, to adjust the rate of so as to increase the set value. Doing so can effectively prevent the penetration of burst traffic to back-office services.
A constant flow restrictor rate: controlled according to a constant predetermined rate
The QPS set, the time constant can be obtained for each token required for the desired number of token can estimate the time required. If the wait time is greater than the maximum waiting time is rejected, otherwise the last update (the rule has been acquired after the expected time token on request) by the time, plus the time required times, and have the time to wait for requests. To ensure smooth token acquisition rate constant, reaches peak load shifting, preventing the request by the get together in the front stage of the window.
Preheating constant rate flow restrictor: preheating with the same pre-restrictor, After a warm-up time, the acquired token at a constant rate.
3.8.DegradeSlot
The statistical information and a preset rule, do the fuse downgrade;
Degradation process described above:
When the flow comes in, if the rule (resource) is already in a degraded state, the verification is not returned directly by
If in a degraded state, the node data is acquired ClusterNode the resource, the type of degradation is judged according to the set:
1) Response time is determined according to: if the average response time is less than the value of the resource rule, not by the number of times is reset to 0, and returns true; The number of times smaller than the default value is not passed, return true; otherwise, proceed to downgrade process
2) determination of abnormality: if the number of times smaller than the default resource error rate is less than the set value or an abnormal value, the return true; otherwise flow into the downgrade
3) The number of abnormal determination: If the number of abnormality is smaller than the set value, returns true; otherwise flow into the downgrade
Downgrade procedure: the resource is set to degraded state, and starts a timer, the timer will be executed after the rule set time, resets the resource specified by the rule execution degraded state
4.Node
4.1 sliding window model
Sentinel underlying high-performance sliding window data structures in real-time statistics to LeapArray-second indicator data, writing can be well supported high concurrency scenarios than read.
LeapArray includes the following attributes:
Sliding window model as follows:
LeapArray body to achieve the following (removal of the original notes, put his remarks):
The abstract of a ring array, the above process can be seen in the following figure:
The main process:
Calculating a given time window where the index: i
To calculate the time window where the start time: ws
i Gets existing window object according to the index number: window
According to the prior window object: window, determining whether to update the current window
1) If the existing window object is empty, then the initialization current window
2) If the start time is consistent with the existing window ws, then the existing window has not expired, continue to use the current window
3) If the start time is less than the existing window WS, described existing window has expired, the window needs to be updated
4.2 StatisticNode
Sentinel StatisticNode to achieve the necessary statistical monitoring assembly, the assembly can be realized in different particle sizes, different dimensions of the data monitoring statistics.
Pictured Statistic necessary component consists of:
LeapArray: sliding windows, sliding window behavior to achieve different temporal granularity, LeapArray is an abstract class
BucketLeapArray: LeapArray implementation class, the main implementation method
1) newEmptyBucket: initializing operation of the window, provided the wrapper class initialization here MetricBucket
2) resetWindowTo: updating operation of the window, where the reset start of the time window, and the reset window value packaging
MetricBucket: stores the count of each dimension statistics, statistical enumeration value dimension MetricEvent
MetricEvent: statistical dimensions, including
1) PASS: obtaining a count of the token
2) BLOCK: not token counts obtained
3) EXCEPTION: computing an abnormality occurs
4) SUCCESS: obtaining a count of the token returned successfully
5) RT: Request Time
Metric: Interface Statistics, translated by interface can be understood as "available statistics", the polymerization method and collecting statistical information generating statistical information. The method of collecting statistics for each dimension add, generates statistics and details methods using windows, each containing statistics returns MetricNode
1) windows: Returns statistics LeapArray are now to form MetricBucket
2) details: Returns statistics LeapArray are now to form MetricNode
MetricNode: Bean, each attribute value of statistical information, data corresponding to a current mirror system, calculates the count data in accordance with the dimensions through.
ArrayMetric: Metric implementation class, internal LeapArray statistical data of each window as a sliding window
StatisticNode: statistics node, and the internal one second to one minute sliding window statistics, while also record the current number of threads
1) rollingCounterInSecond: sampleCounter is 2, intervalInMs 1 second sliding window
2) rollingCounterInMinute: sampleCounter to 60, intervalInMs 1 minute sliding window
ClusterNode: inherited from StatisticNode, a global resource for statistics
DefaultNode: inherited from StatisticNode, implemented in the appropriate context for a particular resource, it holds a reference to the ClusterNode. It also holds the list of child nodes, when in the same context call different resource SphU.entry many times will create a child node
The main call flow is as follows:
Finally the process, the dimensions of the accumulated count data MetricBucket maintained. These data can be converted MetricNode when calling to provide formatted data.
4.3 LeapArray
LeapArray implementations include:
BucketLeapArray: each window holds a MetricBucket, the object of the current count value stored in each dimension of the window
FutureLeapArray: only stores larger than the current time window
BorrowBucketLeapArray: hold FutureLeapArray, support resources borrowed from the subsequent window
LeapArray the default implementation of each window are effective in intervalInMs
As shown, at a time 800, each window is effective in intervalInMs, this time is calculated using the statistical values qps each window and.
FutureLeapArray rewrite isWindowDeprecated method, as follows
As long as the given time window is greater than a given starting time window is considered failure; when a given time is the current time, a window or the window where the current time window is failure, only a given time after storage window. Figure above, at a given time is 800, only the window 1000 is valid.
BorrowBucketLeapArray, mainly used to support the limited mode when the current limiting, when a current token is not enough to allow subsequent start token acquisition window. Holds inside a FutureLeapArray, the window is a window for a queue stored in the current time. By rewriting LeapArray addWaiting method, the count value of the specified occupancy subsequent windows, and through currentWaiting methods for obtaining the current time how many subsequent windows already occupied resources.
Because borrowArray the window may have been initialized or used before, therefore, BorrowBucketLeapArray at initialization window or windows update will take into account the existing data borrowArray the window, newEmptyBucket follows rewrite and resetWindowTo method.
When newEmptyBucket initialization method, if the window is the time where the findings have appeared in FutureBucketArray, it will use the value of the window. Similarly, restWindowTo when updating, if the given time window exists, the plus count value previously present.
StatisticNode when using prior request will be based on statistics, can be used to estimate the subsequent request window, and then borrowed token from the subsequent window, specifically implemented as follows:
First calculates a starting value from the current valid window time when performing the farthest, as shown below, assuming that the current window is Curr, beginning at the earliest 1 earliestTime fall of. Then starting from earliest 1, a window is incremented to progressively according to the value before the Pass estimating subsequent request may arise, if a window according to the estimated value of a subsequent window before there is an empty token, and the latency within a desired time, then the current request hold live, so that to reach a particular window and then continue. FIG assumed that the rule is set to the maximum QPS 20, when the front window 3, every time the maximum value does not reach the maximum defined, it may be considered, in a window after the value of curr is the case. When curr sudden arrival of a request 22, the rules will have two request-rejected out, but according to the situation before the window, these two requests may be done in a second subsequent request, in order to fully utilize the system resources.
5.RuleManager
Each management model rules are the same, the main use of the following three components
RuleManager, implementation class specific rules, there is no specific implementation of the interface, but the method has loadRules
ProertyListener, regular listeners
SentinelProperty, the viewer, holding each listener
Internal Internal RuleManaer hold PropertyListener and SentinelProerty, and RuleManager have PropertyListener implementation class
Process-specific, RuleManager call SentinelProepety.addListener initialization, set the listener. SentinelProperty calls PropertyListener.configLoad, to complete the initialization. When you call back again RuleManager.loadRules change the rules, internal caller SentinelProerty.updateValue, which will traverse all Listener internal SentinelProperty held, one by one execution PropertyListner.configUpdate, thereby informing the RuleManager rules changed to allow RuleManager make a deal .
Personal Public Number: ah camel