正经学徒,佛系记录,不搞事情
dubbo项目在刚启动时经常会出现服务超时,起初并不在意,后来测试负载均衡RoundRobinLoadBalance(权重轮询)调用时,发现调用的服务器老是不对,于是进入RoundRobinLoadBalance.java类断点调试。
@Override
protected <T> Invoker<T> doSelect(List<Invoker<T>> invokers, URL url, Invocation invocation) {
String key = invokers.get(0).getUrl().getServiceKey() + "." + invocation.getMethodName();
int length = invokers.size(); // Number of invokers
int maxWeight = 0; // The maximum weight
int minWeight = Integer.MAX_VALUE; // The minimum weight
final LinkedHashMap<Invoker<T>, IntegerWrapper> invokerToWeightMap = new LinkedHashMap<Invoker<T>, IntegerWrapper>();
int weightSum = 0;
for (int i = 0; i < length; i++) {
int weight = getWeight(invokers.get(i), invocation);
maxWeight = Math.max(maxWeight, weight); // Choose the maximum weight
minWeight = Math.min(minWeight, weight); // Choose the minimum weight
if (weight > 0) {
invokerToWeightMap.put(invokers.get(i), new IntegerWrapper(weight));
weightSum += weight;
}
}
AtomicPositiveInteger sequence = sequences.get(key);
if (sequence == null) {
sequences.putIfAbsent(key, new AtomicPositiveInteger());
sequence = sequences.get(key);
}
int currentSequence = sequence.getAndIncrement();
if (maxWeight > 0 && minWeight < maxWeight) {
int mod = currentSequence % weightSum;
for (int i = 0; i < maxWeight; i++) {
for (Map.Entry<Invoker<T>, IntegerWrapper> each : invokerToWeightMap.entrySet()) {
final Invoker<T> k = each.getKey();
final IntegerWrapper v = each.getValue();
if (mod == 0 && v.getValue() > 0) {
return k;
}
if (v.getValue() > 0) {
v.decrement();
mod--;
}
}
}
}
// Round robin
return invokers.get(currentSequence % length);
}
getWeight方法是用来获取每个服务的权重,但是项目启动时,发起请求,发现这个方法的返回值都是1,这就玄学了,自己设置的权重为什么没有生效,只能进入getWeight方法一探究竟
protected int getWeight(Invoker<?> invoker, Invocation invocation) {
int weight = invoker.getUrl().getMethodParameter(invocation.getMethodName(), Constants.WEIGHT_KEY, Constants.DEFAULT_WEIGHT);
if (weight > 0) {
long timestamp = invoker.getUrl().getParameter(Constants.REMOTE_TIMESTAMP_KEY, 0L);
if (timestamp > 0L) {
int uptime = (int) (System.currentTimeMillis() - timestamp);
int warmup = invoker.getUrl().getParameter(Constants.WARMUP_KEY, Constants.DEFAULT_WARMUP);
if (uptime > 0 && uptime < warmup) {
weight = calculateWarmupWeight(uptime, warmup, weight);
}
}
}
return weight;
}
原来这里第二行的方法才是真正获取设置权重的地方,并且调试到这里获取到的权重也确实是自己设置的权重,其中
uptime获取的是项目启动的时间
warmup按中式英文翻译表示记录的是预热时间,且默认值为10分钟
那剩下的问题就是研究为什么经过方法calculateWarmupWeight后权重值变更了。
static int calculateWarmupWeight(int uptime, int warmup, int weight) {
int ww = (int) ((float) uptime / ((float) warmup / (float) weight));
return ww < 1 ? 1 : (ww > weight ? weight : ww);
}
有一个公式:(启动时间/预热时间)/ 权重。
看起来是在计算百分比,整个过程连接起来的意思就是:
这样看来,当项目刚刚启动时,权重都会是1,并且随着时间慢慢的增加到真实权重,当过了系统的预热时间(十分钟),则进入正常的调用逻辑。
所以问题又归咎于为什么有预热时间这个东西,经查一些资料后得知:由于JVM也是带有预热的过程,所以为了不让服务提供者一启动项目就承担全部的流量,才会使用这个预热来进行缓冲。这个问题尚且只能理解成日常生活中烤箱,烤箱是需要预热的,如果在烤制之前没有对烤箱进行预热,会使得面糊团无法漂亮呈色或是会变得干燥。