-
Notifications
You must be signed in to change notification settings - Fork 26.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Optimize leastActiveSelect and weight test case #2172
Conversation
Codecov Report
@@ Coverage Diff @@
## master #2172 +/- ##
============================================
- Coverage 55.05% 55.01% -0.04%
- Complexity 5282 5288 +6
============================================
Files 573 573
Lines 25490 25538 +48
Branches 4526 4531 +5
============================================
+ Hits 14033 14050 +17
- Misses 9362 9387 +25
- Partials 2095 2101 +6
Continue to review full report at Codecov.
|
I saw your pr, I think the start warm up feature with the leastactive algorithm for loadbalance will be conflicted, because of the active for the new provider will be the least. So is it necessary to calculate the weight for warm up? Or maybe we can consider another implementation to supports warmup for leastactive loadbalance. |
This is not the problem that this PR has to solve. The problem that this PR has to solve is too many loops and a small bug. |
|
@@ -41,26 +39,26 @@ | |||
int leastActive = -1; // The least active value of all invokers | |||
int leastCount = 0; // The number of invokers having the same least active value (leastActive) | |||
int[] leastIndexs = new int[length]; // The index of invokers having the same least active value (leastActive) | |||
int totalWeight = 0; // The sum of weights | |||
int firstWeight = 0; // Initial value, used for comparision | |||
int taotalWeightWithWarmUp = 0; // The sum of with warmup weights |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
typo
@whanice @chickenlj @zonghaishang |
@diecui1202 |
@carryxyh pls. check the conflict. Agree with back to 2.6.x |
Ok, fix it soon. |
b812b2a
to
5ebab8b
Compare
…tive invokers and all of them are in warm up. After this pr, it will select also by weight and warm up. And fix a bug when two invoker's active is same and weight not same.
5ebab8b
to
a8dcb28
Compare
@zonghaishang @diecui1202 @chickenlj @whanice |
System.out.println(sumInvoker2); | ||
} | ||
|
||
class MyLeastActiveLoadBalance extends AbstractLoadBalance { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you reuse existing code?
org.apache.dubbo.rpc.cluster.loadbalance.LeastActiveBalanceTest#testLeastActiveLoadBalance_select:
public void testLeastActiveLoadBalance_select() {
...
Map<Invoker, AtomicLong> counter = getInvokeCounter(runs, LeastActiveLoadBalance.NAME);
...
Load your repaired code this way:
ExtensionLoader.getExtensionLoader(LoadBalance.class).getExtension(LeastActiveLoadBalance.NAME)
@@ -70,7 +68,7 @@ | |||
} | |||
if (!sameWeight && totalWeight > 0) { | |||
// If (not every invoker has the same weight & at least one invoker's weight>0), select randomly based on totalWeight. | |||
int offsetWeight = ThreadLocalRandom.current().nextInt(totalWeight); | |||
int offsetWeight = ThreadLocalRandom.current().nextInt(totalWeight) + 1; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why change ThreadLocalRandom.current().nextInt(totalWeight)
to ThreadLocalRandom.current().nextInt(totalWeight) + 1
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is it possible that +1 causes offset not equal or less 0?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If not change to +1
, that will cause a bug.
U can look at my unit test for more detail.
@zonghaishang |
@@ -70,7 +68,7 @@ | |||
} | |||
if (!sameWeight && totalWeight > 0) { | |||
// If (not every invoker has the same weight & at least one invoker's weight>0), select randomly based on totalWeight. | |||
int offsetWeight = ThreadLocalRandom.current().nextInt(totalWeight); | |||
int offsetWeight = ThreadLocalRandom.current().nextInt(totalWeight) + 1; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
最近正好在看 LeastActiveLoadBalance 的源码,我觉得 +1 这个逻辑有点突兀,不知道背景的同学可能不知道为什么要+1。更合理的方式,我觉得应该按照 RandomLoadBalance 逻辑去处理。将 if (offsetWeight <= 0) 改为 if (offsetWeight < 0),这样两者的逻辑能够统一起来。只要大家能看懂 RandomLoadBalance 的代码 ,那么此处的代码也一样能看懂,而不用特地去思考为什么要 +1。你觉得呢
Now the select is select a random one when there are several least active invokers and all of them are in warm up.
After this pr, it will select also by weight and warm up.
And fix a bug when two invoker's active is same and weight not same.
issue:
#904