[apache/dubbo]LeastActiveLoadBalance负载均衡

2024-07-31 965 views
2

74行 int offsetWeight = random.nextInt(totalWeight);//这里totalWeight是没有经过warmup降权的值的和 78 offsetWeight -= getWeight(invokers.get(leastIndex), invocation);//这个是经过warmup降权的。

当服务刚启动,经过warmup降权后的getWeight返回的值,可能经过leastCount次循环后,还是offsetWeight >0 造成选不到invoker for (int i = 0; i < leastCount; i++) { int leastIndex = leastIndexs[i]; offsetWeight -= getWeight(invokers.get(leastIndex), invocation); if (offsetWeight <= 0) return invokers.get(leastIndex); }

回答

5

您好,您能发送一个拉取请求 (pull request) 吗?

8

如果循环后 offsetWeight 没有降至 0 以下,则可能意味着所有最不活跃的调用者都处于预热状态,然后 LeastActiveLoadBalance 将从这些相同的活跃调用者中随机选择一个。或者,继续循环,直到 offsetWeight 的值降至 0 以下?

那么像这样:

int offsetWeight = random.nextInt(totalWeight);
while (offsetWeight > 0) {
    // Return a invoker based on the random value.
    for (int i = 0; i < leastCount; i++) {
        int leastIndex = leastIndexs[i];
        offsetWeight -= getWeight(invokers.get(leastIndex), invocation);
        if (offsetWeight <= 0)
            return invokers.get(leastIndex);
    }
}

但这种方式会引起更多的循环,从而使得选择过程更加耗时。

或者!声明一个变量是warmup之后的权重,直接选这个变量作为totalWeight。像这样:

int totalWeightAfterWarmup = ...

// sth...

if (!sameWeight && totalWeightAfterWarmup > 0) {
    int offsetWeight = random.nextInt(totalWeightAfterWarmup);
    for (int i = 0; i < leastCount; i++) {
        int leastIndex = leastIndexs[i];
        offsetWeight -= getWeight(invokers.get(leastIndex), invocation);
        if (offsetWeight <= 0)
            return invokers.get(leastIndex);
    }
}
6

将行
int weight = invoker.getUrl().getMethodParameter(invocation.getMethodName(), Constants.WEIGHT_KEY, Constants.DEFAULT_WEIGHT); // Weight 更改为 int weight = getWeight(invoker, invocation); // have warm up value

1

@wannshan 是的,我就是这么做的。此外,我将 totalWeight 标记为 taotalWeightAfterWarmUp,以告诉编码员该重量是在预热之后。

8

也许 taotalWeightWithWarmUp 会更好,毕竟不是之后而是变暖……

9

这很有道理。你能把这个问题纳入我的 pr 吗?我会尽快修复它。