前言
如果系统平时流量很低,突然陡增的流量可能会导致系统不稳定,需要需要缓慢增加令牌数到最大阈值。 Sentinel 的 预热限流基于Guava里的算法实现。
一、预热原理
X轴:令牌桶中的令牌数量
Y轴:生产一个令牌消耗的时间,单位是s
stableInterval: 稳定生产一个令牌消耗的时间
coldInterval :冷启动生产一个令牌需要的最大时间,与冷却因子coldFactor有关
thresholdPermits:开启预热的令牌阈值
maxPermits:令牌桶最大令牌数
stable:平稳生产thresholdPermits个令牌的时间
warm up period:预热时间
slope:
当系统刚启动或者长时间没有收到请求时处于冷却状态,这时令牌达到为 maxPermits; 当有慢慢有请求过来时,存在一个预热期,在预热期间获取令牌的时间会比平稳期获取令牌的时间要长,随着令牌的减少,获取单个令牌的时间会慢慢变短,最终到达一个稳定值 stableInterval
二、WarmUpController
1、构造方法
private void construct(double count, int warmUpPeriodInSec, int coldFactor) {
if (coldFactor <= 1) {
throw new IllegalArgumentException("Cold factor should be larger than 1");
}
this.count = count;
this.coldFactor = coldFactor;
warningToken = (int)(warmUpPeriodInSec * count) / (coldFactor - 1);
maxToken = warningToken + (int)(2 * warmUpPeriodInSec * count / (1.0 + coldFactor));
slope = (coldFactor - 1.0) / count / (maxToken - warningToken);
}
coldFactor: 冷却因子,默认是3
count :sentinel界面设置的阈值,假设为5个
warmUpPeriodInSec :sentinel界面设置的预热时间,假设为10s
则:
stableInterval = 1 / count = 0.2
coldInterval = stableInterval * coldFactor = 0.6
stable = warmUpPeriodInSec / 2 = 5
thresholdPermits(warningToken) = stable / stableInterval = 25
maxToken 利用梯形的面积 = (上底+下底)* 高 / 2 计算
maxToken = thresholdPermits + warmUpPeriodInSec * 2 /(stableInterval + coldInterval ) = 50
slope 利用斜率公式k=(y1-y2) / (x1-x2) 计算
slope = (coldInterval - stableInterval )/(maxToken - thresholdPermits) = 0.016
2、canPass( )
@Override
public boolean canPass(Node node, int acquireCount, boolean prioritized) {
long passQps = (long) node.passQps();
long previousQps = (long) node.previousPassQps();
syncToken(previousQps);
long restToken = storedTokens.get();
if (restToken >= warningToken) {
long aboveToken = restToken - warningToken;
double warningQps = Math.nextUp(1.0 / (aboveToken * slope + 1.0 / count));
if (passQps + acquireCount <= warningQps) {
return true;
}
} else {
if (passQps + acquireCount <= count) {
return true;
}
}
return false;
}
3、syncToken(previousQps)
protected void syncToken(long passQps) {
long currentTime = TimeUtil.currentTimeMillis();
currentTime = currentTime - currentTime % 1000;
long oldLastFillTime = lastFilledTime.get();
if (currentTime <= oldLastFillTime) {
return;
}
long oldValue = storedTokens.get();
long newValue = coolDownTokens(currentTime, passQps);
if (storedTokens.compareAndSet(oldValue, newValue)) {
long currentValue = storedTokens.addAndGet(0 - passQps);
if (currentValue < 0) {
storedTokens.set(0L);
}
lastFilledTime.set(currentTime);
}
}
4、coolDownTokens( )
private long coolDownTokens(long currentTime, long passQps) {
long oldValue = storedTokens.get();
long newValue = oldValue;
if (oldValue < warningToken) {
newValue = (long)(oldValue + (currentTime - lastFilledTime.get()) * count / 1000);
} else if (oldValue > warningToken) {
if (passQps < (int)count / coldFactor) {
newValue = (long)(oldValue + (currentTime - lastFilledTime.get()) * count / 1000);
}
}
return Math.min(newValue, maxToken);
}
三、WarmUpRateLimiterController
预热和排队等待想结合的算法,WarmUpRateLimiterController继承了WarmUpController
1、构造方法
public class WarmUpRateLimiterController extends WarmUpController {
private final int timeoutInMs;
private final AtomicLong latestPassedTime = new AtomicLong(-1);
public WarmUpRateLimiterController(double count, int warmUpPeriodSec, int timeOutMs, int coldFactor) {
super(count, warmUpPeriodSec, coldFactor);
this.timeoutInMs = timeOutMs;
}
...
}
2、canPass( )
public boolean canPass(Node node, int acquireCount, boolean prioritized) {
long previousQps = (long) node.previousPassQps();
syncToken(previousQps);
long currentTime = TimeUtil.currentTimeMillis();
long restToken = storedTokens.get();
long costTime = 0;
long expectedTime = 0;
if (restToken >= warningToken) {
long aboveToken = restToken - warningToken;
double warmingQps = Math.nextUp(1.0 / (aboveToken * slope + 1.0 / count));
costTime = Math.round(1.0 * (acquireCount) / warmingQps * 1000);
} else {
costTime = Math.round(1.0 * (acquireCount) / count * 1000);
}
expectedTime = costTime + latestPassedTime.get();
if (expectedTime <= currentTime) {
latestPassedTime.set(currentTime);
return true;
} else {
long waitTime = costTime + latestPassedTime.get() - currentTime;
if (waitTime > timeoutInMs) {
return false;
} else {
long oldTime = latestPassedTime.addAndGet(costTime);
try {
waitTime = oldTime - TimeUtil.currentTimeMillis();
if (waitTime > timeoutInMs) {
latestPassedTime.addAndGet(-costTime);
return false;
}
if (waitTime > 0) {
Thread.sleep(waitTime);
}
return true;
} catch (InterruptedException e) {
}
}
}
return false;
}
四、总结
WarmUpController 根据预热的qps直接判断 passQps + acquireCount <= warningQps;
WarmUpRateLimiterController 根据预热的qps计算期望时间,再判断期望时间有没有到来或者是否在允许的超时时间范围内。
|