Skip to content
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

[type:feat] use binary search optimize random load balance #4935

Closed
wants to merge 32 commits into from
Closed
Changes from all commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
b5e6678
[type:feat] use binary search optimize random load balance
Jul 31, 2023
38722b1
Merge branch 'master' into 4932
yu199195 Aug 3, 2023
ff90c66
Merge branch 'master' into 4932
847850277 Aug 5, 2023
3a5012d
Merge branch 'master' into 4932
damonxue Sep 8, 2023
cdeadc2
Merge branch 'master' into 4932
damonxue Sep 28, 2023
930455e
Merge branch 'master' into 4932
loongs-zhang Jan 9, 2024
384270b
Merge branch 'master' into 4932
loongs-zhang Jan 17, 2024
5631bb8
Merge branch 'master' into 4932
loongs-zhang Feb 20, 2024
747f0d0
Merge branch 'master' into 4932
yu199195 May 6, 2024
d8e07d5
Merge branch 'master' into 4932
Aias00 Aug 30, 2024
3365beb
Merge branch 'master' into 4932
Aias00 Nov 20, 2024
5bee1e6
Merge branch 'master' into 4932
Aias00 Dec 5, 2024
1791594
Merge branch 'master' into 4932
Aias00 Dec 5, 2024
f6e51be
Merge branch 'master' into 4932
Aias00 Dec 6, 2024
0286703
Merge branch 'master' into 4932
Aias00 Dec 9, 2024
c21bf1e
Merge branch 'master' into 4932
Aias00 Dec 14, 2024
6bd442b
Merge branch 'master' into 4932
Aias00 Dec 17, 2024
0cf0f01
Merge branch 'master' into 4932
Aias00 Dec 19, 2024
96b71f7
Merge branch 'master' into 4932
Aias00 Dec 26, 2024
9c4d259
Merge branch 'master' into 4932
Aias00 Dec 26, 2024
d77fe31
Merge branch 'master' into 4932
Aias00 Dec 27, 2024
cfbea78
Merge branch 'master' into 4932
Aias00 Dec 31, 2024
3afd8cf
Merge branch 'master' into 4932
Aias00 Dec 31, 2024
3685850
Merge branch 'master' into 4932
Aias00 Jan 2, 2025
10c1108
Merge branch 'master' into 4932
Aias00 Jan 2, 2025
f422d32
Merge branch 'master' into 4932
Aias00 Jan 3, 2025
e22fbe8
Merge branch 'master' into 4932
Aias00 Jan 6, 2025
822cbf2
Merge branch 'master' into 4932
Aias00 Jan 9, 2025
8c728da
Merge branch 'master' into 4932
Aias00 Jan 13, 2025
16ad5c3
Merge branch 'master' into 4932
Aias00 Jan 14, 2025
e0e0af0
Merge branch 'master' into 4932
Aias00 Jan 15, 2025
144b4f9
Merge branch 'master' into 4932
Aias00 Jan 16, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package org.apache.shenyu.loadbalancer.spi;

import java.security.SecureRandom;
import java.util.Arrays;
import java.util.List;
import org.apache.shenyu.loadbalancer.entity.Upstream;
import org.apache.shenyu.spi.Join;
Expand All @@ -41,42 +42,41 @@ public Upstream doSelect(final List<Upstream> upstreamList, final String ip) {
weights[0] = firstUpstreamWeight;
// init the totalWeight
int totalWeight = firstUpstreamWeight;
int halfLengthTotalWeight = 0;
for (int i = 1; i < length; i++) {
int currentUpstreamWeight = getWeight(upstreamList.get(i));
if (i <= (length + 1) / 2) {
halfLengthTotalWeight = totalWeight;
}
weights[i] = currentUpstreamWeight;
totalWeight += currentUpstreamWeight;
weights[i] = totalWeight;
if (sameWeight && currentUpstreamWeight != firstUpstreamWeight) {
// Calculate whether the weight of ownership is the same.
sameWeight = false;
}
}
if (totalWeight > 0 && !sameWeight) {
return random(totalWeight, halfLengthTotalWeight, weights, upstreamList);
return random(totalWeight, weights, upstreamList, length);
}
return random(upstreamList);
}

private Upstream random(final int totalWeight, final int halfLengthTotalWeight, final int[] weights, final List<Upstream> upstreamList) {
private Upstream random(final int totalWeight, final int[] weights, final List<Upstream> upstreamList, final int length) {
// If the weights are not the same and the weights are greater than 0, then random by the total number of weights.
int offset = RANDOM.nextInt(totalWeight);
int index = 0;
int end = weights.length;
if (offset >= halfLengthTotalWeight) {
index = (weights.length + 1) / 2;
offset -= halfLengthTotalWeight;
if (length <= 4) {
moremind marked this conversation as resolved.
Show resolved Hide resolved
for (int i = 0; i < length; i++) {
if (offset < weights[i]) {
return upstreamList.get(i);
}
}
} else {
end = (weights.length + 1) / 2;
}
// Determine which segment the random value falls on
for (; index < end; index++) {
offset -= weights[index];
if (offset < 0) {
return upstreamList.get(index);
int i = Arrays.binarySearch(weights, offset);
if (i < 0) {
i = -i - 1;
} else {
while (weights[i + 1] == offset) {
i++;
}
i++;
}
return upstreamList.get(i);
}
return random(upstreamList);
}
Expand Down
Loading