In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly shows you "what are the current-limiting frameworks commonly used in java", which are easy to understand and well-organized, hoping to help you solve your doubts, the following let the editor lead you to study and learn "what are the commonly used current-limiting frameworks in java" this article.
As one of the means to deal with high concurrency, current restriction is not a new topic. From Guava's Ratelimiter to Hystrix, as well as Sentinel can be used as a current-limiting tool.
Adaptive current limit
In general, it is often necessary to specify a fixed value (qps) as the threshold of the current limiting switch, which is judged by experience and obtained by a large number of test data. But this threshold may become less appropriate after traffic surges, the system automatically scales, or so-and-so commit has a piece of toxic code. And the general business side is not able to correctly evaluate their own capacity, to set an appropriate current limit threshold.
At this time, adaptive current limiting is to solve this problem, the current limiting threshold does not need to be specified manually, nor does it need to estimate the capacity of the system, and the threshold can change with the changes of the relevant indicators of the system.
The adaptive current limiting algorithm draws lessons from the TCP congestion algorithm, estimates the threshold of current limiting according to various indicators, and constantly adjusts it. The approximate results are as follows:
As can be seen from the figure, the request is first sent with a reduced initial concurrency value, and the higher concurrency of the system is detected by increasing the current limit window. Once the delay increases to a certain extent, it will fall back to a smaller current-limiting window. The concurrency limit is continuously detected over and over again, resulting in a jagged time relation function.
TCP Vegas
Vegas is a congestion control algorithm that actively adjusts cwnd. It mainly sets two thresholds, alpha and beta, and then adjusts cwnd by calculating the difference diff between the target rate and the actual rate, and then comparing the relationship between the poor diff and alpha and beta. The pseudo code is as follows:
Diff = cwnd* (1-baseRTT/RTT) if (diff
< alpha) set: cwndcwnd = cwnd + 1 else if (diff >= beta) set: cwndcwnd = cwnd-1 else set: cwndcwnd = cwnd
Where baseRTT refers to the minimum round-trip time measured, RTT refers to the currently measured round-trip time, and cwnd refers to the current TCP window size. Usually in tcp, alpha will be set to 2-3 and the beta will be set to 4-6. In this way, cwnd is kept in a state of equilibrium.
Netflix-concuurency-limits
Concuurency-limits is an adaptive current limiting component introduced by netflix, which draws lessons from the relevant congestion control algorithm of TCP. It mainly adjusts the current limiting window dynamically according to the request delay and the queue length directly affected by it.
Alpha, beta & threshold
The vegas algorithm is implemented in the VegasLimit class. First, take a look at the initialization related code:
Private int initialLimit = 20; private int maxConcurrency = 1000; private MetricRegistry registry = EmptyMetricRegistry.INSTANCE; private double smoothing = 1.0; private Function alphaFunc = (limit)-> 3 * LOG10.apply (limit.intValue ()); private Function betaFunc = (limit)-> 6 * LOG10.apply (limit.intValue ()); private Function thresholdFunc = (limit)-> LOG10.apply (limit.intValue ()) Private Function increaseFunc = (limit)-> limit + LOG10.apply (limit.intValue ()); private Function decreaseFunc = (limit)-> limit-LOG10.apply (limit.intValue ())
Here we first define an initialization value initialLimit of 20 and a maximum value of maxConcurrency1000. Secondly, there are three threshold functions alphaFunc,betaFunc and thresholdFunc. Finally, there are two increase and decrease functions increaseFunc and decreaseFunc.
Functions are based on the current concurrency value limit.
AlphaFunc can be compared to alpha in the vegas algorithm, and the implementation here is 3*log limit. When the threshold value increases from the initial 20 to the maximum 0, the corresponding alpha increases from 1000 to 9.
BetaFunc can be compared to beta in the vegas algorithm, and the implementation here is 6*log limit. When the threshold value increases from the initial 20 to the maximum, the corresponding alpha increases from 1000 to 18.
ThresholdFunc is a new function that represents an initial threshold, and limit will adopt a more aggressive incremental algorithm when it is less than this value. The implementation here is twice the log limit. When the MIT value increases from the initial 20 to the maximum 1000, the corresponding alpha increases from 1.3 to 3.
These three function values can be considered to determine the four interval ranges of the dynamic adjustment function. Different adjustment functions are applied when the variable queueSize = limit × (1 − RTTnoLoad/RTTactual) falls into these four intervals.
Variable queueSize
The variable is queueSize, and the calculation method is limit × (1 − RTTnoLoad/RTTactual). Why do you actually have a little insight into this calculation?
We think of the process of the system processing the request as a water pipe, and the incoming request is to pour water into the water pipe. When the system is processed smoothly, the request does not need to queue up and passes directly through the water pipe. The RT of this request is the shortest, that is, RTTnoLoad.
On the contrary, when the request is stacked, the processing time of the request will become: queue time + minimum processing time, that is, RTTactual = inQueueTime + RTTnoLoad. And obviously the queue length is
Total queue time / processing time of each request and queueSize = (limit * inQueueTime) / (inQueueTime + RTTnoLoad) = limit × (1 − RTTnoLoad/RTTactual).
Take another chestnut, because assuming that the current delay is the best delay, then naturally there is no need to queue, that is, queueSize=0. Assuming that the current delay is twice the optimal delay, it can be considered that the processing capacity is halved, 50 requests are queued for half of the incoming traffic, and queueSize= 100 * (1 − 1 − 2) = 50.
Dynamic adjustment function
The most important adjustment function is the increasing function and the decreasing function. From the initialization code, we know that the increase function increaseFunc is implemented as limit+log limit, and the subtraction function decreaseFunc is implemented as limit-log limit, which is relatively conservative.
Take a look at the code that applies the dynamic adjustment function:
Private int updateEstimatedLimit (long rtt, int inflight, boolean didDrop) {final int queueSize = (int) Math.ceil (estimatedLimit * (1-(double) rtt_noload / rtt)); double newLimit; / / Treat any drop (i.e timeout) as needing to reduce the limit / / find errors directly apply the subtraction function decreaseFunc if (didDrop) {newLimit = decreaseFunc.apply (estimatedLimit) / / Prevent upward drift if not close to the limit} else if (inflight * 2 < estimatedLimit) {return (int) estimatedLimit;} else {int alpha = alphaFunc.apply ((int) estimatedLimit); int beta = betaFunc.apply ((int) estimatedLimit); int threshold = this.thresholdFunc.apply ((int) estimatedLimit) / / Aggressive increase when no queuing if (queueSize beta) {newLimit = decreaseFunc.apply (estimatedLimit); / / We're within he sweet spot so nothing to do} else {return (int) estimatedLimit;}} newLimit = Math.max (1, Math.min (maxLimit, newLimit)) NewLimit = (1-smoothing) * estimatedLimit + smoothing * newLimit If ((int) newLimit! = (int) estimatedLimit & & LOG.isDebugEnabled ()) {LOG.debug ("New limit= {} minRtt= {} ms winRtt= {} ms queueSize= {}", (int) newLimit, TimeUnit.NANOSECONDS.toMicros (rtt_noload) / 1000.0, TimeUnit.NANOSECONDS.toMicros (rtt) / 1000.0 QueueSize) } estimatedLimit = newLimit; return (int) estimatedLimit;}
The dynamic adjustment function rules are as follows:
When the variable queueSize < threshold, choose the more radical incremental function, newLimit = limit+beta
When the variable queueSize < alpha, you need to increase the current limit window and select the increment function increaseFunc, that is, newLimit = limit + log limit
When the variable queueSize is between alpha,beta, limit does not change
When the variable queueSize is greater than beta, you need to collapse the current limit window and select the subtractive function decreaseFunc, that is, newLimit = limit-log limit
Smooth decreasing smoothingDecrease
Notice that the variable smoothing can be set, where the initial value is 1, indicating that smooth decrement does not work. If necessary, you can set it as needed, for example, if you set smoothing to 0.5, then the effect will be halved by using the subtractive function decreaseFunc, and the implementation method is newLimitAfterSmoothing = 0.5 newLimit + 0.5 limit.
These are all the contents of the article "what are the current-limiting frameworks commonly used in java?" Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.