Skip to content

Commit

Permalink
update that how to judge the same weight (apache#6479)
Browse files Browse the repository at this point in the history
update that weights and how to judge the index depend on offset

Co-authored-by: yangmengle <yangmengle>
  • Loading branch information
mengle-yang authored Sep 30, 2020
1 parent cd20009 commit 2d9583a
Showing 1 changed file with 7 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,20 +49,17 @@ protected <T> Invoker<T> doSelect(List<Invoker<T>> invokers, URL url, Invocation
int length = invokers.size();
// Every invoker has the same weight?
boolean sameWeight = true;
// the weight of every invokers
// the maxWeight of every invokers, the minWeight = 0 or the maxWeight of the last invoker
int[] weights = new int[length];
// the first invoker's weight
int firstWeight = getWeight(invokers.get(0), invocation);
weights[0] = firstWeight;
// The sum of weights
int totalWeight = firstWeight;
for (int i = 1; i < length; i++) {
int totalWeight = 0;
for (int i = 0; i < length; i++) {
int weight = getWeight(invokers.get(i), invocation);
// save for later use
weights[i] = weight;
// Sum
totalWeight += weight;
if (sameWeight && weight != firstWeight) {
// save for later use
weights[i] = totalWeight;
if (sameWeight && totalWeight != weight * (i + 1)) {
sameWeight = false;
}
}
Expand All @@ -71,8 +68,7 @@ protected <T> Invoker<T> doSelect(List<Invoker<T>> invokers, URL url, Invocation
int offset = ThreadLocalRandom.current().nextInt(totalWeight);
// Return a invoker based on the random value.
for (int i = 0; i < length; i++) {
offset -= weights[i];
if (offset < 0) {
if (offset < weights[i]) {
return invokers.get(i);
}
}
Expand Down

0 comments on commit 2d9583a

Please sign in to comment.