日本综合一区二区|亚洲中文天堂综合|日韩欧美自拍一区|男女精品天堂一区|欧美自拍第6页亚洲成人精品一区|亚洲黄色天堂一区二区成人|超碰91偷拍第一页|日韩av夜夜嗨中文字幕|久久蜜综合视频官网|精美人妻一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
常用的限流框架,你都會用嗎?

作為應(yīng)對高并發(fā)的手段之一,限流并不是一個新鮮的話題了。從Guava的Ratelimiter到Hystrix,以及Sentinel都可作為限流的工具。

10年積累的成都網(wǎng)站設(shè)計、網(wǎng)站制作經(jīng)驗,可以快速應(yīng)對客戶對網(wǎng)站的新想法和需求。提供各種問題對應(yīng)的解決方案。讓選擇我們的客戶得到更好、更有力的網(wǎng)絡(luò)服務(wù)。我雖然不認識你,你也不認識我。但先網(wǎng)站設(shè)計后付款的網(wǎng)站建設(shè)流程,更有白山免費網(wǎng)站建設(shè)讓你可以放心的選擇與我們合作。

自適應(yīng)限流

一般的限流常常需要指定一個固定值(qps)作為限流開關(guān)的閾值,這個值一是靠經(jīng)驗判斷,二是靠通過大量的測試數(shù)據(jù)得出。但這個閾值,在流量激增、系統(tǒng)自動伸縮或者某某commit了一段有毒代碼后就有可能變得不那么合適了。并且一般業(yè)務(wù)方也不太能夠正確評估自己的容量,去設(shè)置一個合適的限流閾值。

而此時自適應(yīng)限流就是解決這樣的問題的,限流閾值不需要手動指定,也不需要去預(yù)估系統(tǒng)的容量,并且閾值能夠隨著系統(tǒng)相關(guān)指標(biāo)變化而變化。

自適應(yīng)限流算法借鑒了TCP擁塞算法,根據(jù)各種指標(biāo)預(yù)估限流的閾值,并且不斷調(diào)整。大致獲得的效果如下:

從圖上可以看到,首先以一個降低的初始并發(fā)值發(fā)送請求,同時通過增大限流窗口來探測系統(tǒng)更高的并發(fā)性。而一旦延遲增加到一定程度了,又會退回到較小的限流窗口。循環(huán)往復(fù)持續(xù)探測并發(fā)極限,從而產(chǎn)生類似鋸齒狀的時間關(guān)系函數(shù)。

TCP Vegas

vegas是一種主動調(diào)整cwnd的擁塞控制算法,主要是設(shè)置兩個閾值alpha 和 beta,然后通過計算目標(biāo)速率和實際速率的差diff,再比較差diff與alpha和beta的關(guān)系,對cwnd進行調(diào)節(jié)。偽代碼如下: 

 
 
 
 
  1. diff = cwnd*(1-baseRTT/RTT)  
  2. if (diff < alpha)  
  3. set: cwndcwnd = cwnd + 1   
  4. else if (diff >= beta)  
  5. set: cwndcwnd = cwnd - 1  
  6. else  
  7. set: cwndcwnd = cwnd 

其中baseRTT指的是測量的最小往返時間,RTT指的是當(dāng)前測量的往返時間,cwnd指的是當(dāng)前的TCP窗口大小。通常在tcp中alpha會被設(shè)置成2-3,beta會被設(shè)置成4-6。這樣子,cwnd就保持在了一個平衡的狀態(tài)。

netflix-concuurency-limits

concuurency-limits是netflix推出的自適應(yīng)限流組件,借鑒了TCP相關(guān)擁塞控制算法,主要是根據(jù)請求延時,及其直接影響到的排隊長度來進行限流窗口的動態(tài)調(diào)整。

alpha , beta & threshold

vegas算法實現(xiàn)在了VegasLimit類中。先看一下初始化相關(guān)代碼: 

 
 
 
 
  1. private int initialLimit = 20;  
  2.         private int maxConcurrency = 1000;  
  3.         private MetricRegistry registry = EmptyMetricRegistry.INSTANCE;  
  4.         private double smoothing = 1.0;         
  5.         private Function alphaFunc = (limit) -> 3 * LOG10.apply(limit.intValue());  
  6.         private Function betaFunc = (limit) -> 6 * LOG10.apply(limit.intValue());  
  7.         private Function thresholdFunc = (limit) -> LOG10.apply(limit.intValue());  
  8.         private Function increaseFunc = (limit) -> limit + LOG10.apply(limit.intValue());  
  9.         private Function decreaseFunc = (limit) -> limit - LOG10.apply(limit.intValue()); 

這里首先定義了一個初始化值initialLimit為20,以及極大值maxConcurrency1000。其次是三個閾值函數(shù)alphaFunc,betaFunc以及thresholdFunc。最后是兩個增減函數(shù)increaseFunc和decreaseFunc。

函數(shù)都是基于當(dāng)前的并發(fā)值limit做運算的。

alphaFunc可類比vegas算法中的alpha,此處的實現(xiàn)是3*log limit。limit值從初始20增加到極大1000時候,相應(yīng)的alpha從3.9增加到了9。

betaFunc則可類比為vegas算法中的beta,此處的實現(xiàn)是6*log limit。limit值從初始20增加到極大1000時候,相應(yīng)的alpha從7.8增加到了18。

thresholdFunc算是新增的一個函數(shù),表示一個較為初始的閾值,小于這個值的時候limit會采取激進一些的增量算法。這里的實現(xiàn)是1倍的log limit。mit值從初始20增加到極大1000時候,相應(yīng)的alpha從1.3增加到了3。

這三個函數(shù)值可以認為確定了動態(tài)調(diào)整函數(shù)的四個區(qū)間范圍。當(dāng)變量queueSize = limit × (1 ? RTTnoLoad/RTTactual)落到這四個區(qū)間的時候應(yīng)用不同的調(diào)整函數(shù)。

變量queueSize

其中變量為queueSize,計算方法即為limit × (1 ? RTTnoLoad/RTTactual),為什么這么計算其實稍加領(lǐng)悟一下即可。

我們把系統(tǒng)處理請求的過程想象為一個水管,到來的請求是往這個水管灌水,當(dāng)系統(tǒng)處理順暢的時候,請求不需要排隊,直接從水管中穿過,這個請求的RT是最短的,即RTTnoLoad;

反之,當(dāng)請求堆積的時候,那么處理請求的時間則會變?yōu)椋号抨爼r間+最短處理時間,即RTTactual = inQueueTime + RTTnoLoad。而顯然排隊的隊列長度為

總排隊時間/每個請求的處理時間及queueSize = (limit * inQueueTime) / (inQueueTime + RTTnoLoad) = limit × (1 ? RTTnoLoad/RTTactual)。

再舉個栗子,因為假設(shè)當(dāng)前延時即為最佳延時,那么自然是不用排隊的,即queueSize=0。而假設(shè)當(dāng)前延時為最佳延時的一倍的時候,可以認為處理能力折半,100個流量進來會有一半即50個請求在排隊,及queueSize= 100 * (1 ? 1/2)=50。

動態(tài)調(diào)整函數(shù)

調(diào)整函數(shù)中最重要的即增函數(shù)與減函數(shù)。從初始化的代碼中得知,增函數(shù)increaseFunc實現(xiàn)為limit+log limit,減函數(shù)decreaseFunc實現(xiàn)為limit-log limit,相對來說增減都是比較保守的。

看一下應(yīng)用動態(tài)調(diào)整函數(shù)的相關(guān)代碼: 

 
 
 
 
  1. private int updateEstimatedLimit(long rtt, int inflight, boolean didDrop) {  
  2.         final int queueSize = (int) Math.ceil(estimatedLimit * (1 - (double)rtt_noload / rtt));  
  3.         double newLimit;  
  4.         // Treat any drop (i.e timeout) as needing to reduce the limit  
  5.         // 發(fā)現(xiàn)錯誤直接應(yīng)用減函數(shù)decreaseFunc  
  6.         if (didDrop) {  
  7.             newLimit = decreaseFunc.apply(estimatedLimit);  
  8.         // Prevent upward drift if not close to the limit  
  9.         } else if (inflight * 2 < estimatedLimit) {  
  10.             return (int)estimatedLimit;  
  11.         } else {  
  12.             int alpha = alphaFunc.apply((int)estimatedLimit);  
  13.             int beta = betaFunc.apply((int)estimatedLimit);  
  14.             int threshold = this.thresholdFunc.apply((int)estimatedLimit);  
  15.             // Aggressive increase when no queuing  
  16.             if (queueSize <= threshold) {  
  17.                 newLimit = estimatedLimit + beta;  
  18.             // Increase the limit if queue is still manageable  
  19.             } else if (queueSize < alpha) {  
  20.                 newLimit = increaseFunc.apply(estimatedLimit);  
  21.             // Detecting latency so decrease  
  22.             } else if (queueSize > beta) {  
  23.                 newLimit = decreaseFunc.apply(estimatedLimit);  
  24.             // We're within he sweet spot so nothing to do  
  25.             } else {  
  26.                 return (int)estimatedLimit;  
  27.             }  
  28.         }  
  29.         newLimit = Math.max(1, Math.min(maxLimit, newLimit));  
  30.         newLimit = (1 - smoothing) * estimatedLimit + smoothing * newLimit;  
  31.         if ((int)newLimit != (int)estimatedLimit && LOG.isDebugEnabled()) {  
  32.             LOG.debug("New limit={} minRtt={} ms winRtt={} ms queueSize={}",  
  33.                     (int)newLimit,  
  34.                     TimeUnit.NANOSECONDS.toMicros(rtt_noload) / 1000.0,  
  35.                     TimeUnit.NANOSECONDS.toMicros(rtt) / 1000.0,  
  36.                     queueSize);  
  37.         }  
  38.         estimatedLimit = newLimit;  
  39.         return (int)estimatedLimit;  
  40.     } 

動態(tài)調(diào)整函數(shù)規(guī)則如下:

當(dāng)變量queueSize < threshold時,選取較激進的增量函數(shù),newLimit = limit+beta

當(dāng)變量queueSize < alpha時,需要增大限流窗口,選擇增函數(shù)increaseFunc,即newLimit = limit + log limit

當(dāng)變量queueSize處于alpha,beta之間時候,limit不變

 當(dāng)變量queueSize大于beta時候,需要收攏限流窗口,選擇減函數(shù)decreaseFunc,即newLimit = limit - log limit

平滑遞減 smoothingDecrease

注意到可以設(shè)置變量smoothing,這里初始值為1,表示平滑遞減不起作用。如果有需要的話可以按需設(shè)置,比如設(shè)置smoothing為0.5時候,那么效果就是采用減函數(shù)decreaseFunc時候效果減半,實現(xiàn)方式為newLimitAfterSmoothing = 0.5 newLimit + 0.5 limit。 


網(wǎng)頁題目:常用的限流框架,你都會用嗎?
分享路徑:http://www.dlmjj.cn/article/djcgghh.html