Skip to content

Commit

Permalink
fix: resolve PR comments
Browse files Browse the repository at this point in the history
  • Loading branch information
astsiapanay committed Jan 31, 2025
1 parent f7a330b commit 5ef1f93
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -54,19 +54,14 @@ public Upstream next() {
return availableUpstreams.get(0);
}
int total = availableUpstreams.stream().map(Upstream::getWeight).reduce(0, Integer::sum);
// make sure the upper bound `total` is inclusive
// the lowest bound should be 1 otherwise the 1st upstream with weight 1 has more possibility
// to be selected because zero is included in its range
// e.g. there are 3 upstreams with equal weight = 1 and the ranges are [0,1], [2,2] and [3,3]
// definitely the 1st upstream has higher possibility
int random = generator.nextInt(1, total + 1);
int random = generator.nextInt(total);
int current = 0;

Upstream result = null;

for (Upstream upstream : availableUpstreams) {
current += upstream.getWeight();
if (current >= random) {
if (current > random) {
result = upstream;
break;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,25 +31,25 @@ void testWeightedLoadBalancer() {

RandomizedWeightedBalancer balancer = new RandomizedWeightedBalancer("model1", upstreams, generator);

when(generator.nextInt(1, 11)).thenReturn(0);
when(generator.nextInt(10)).thenReturn(0);

Upstream upstream = balancer.next();
assertNotNull(upstream);
assertEquals(upstreams.get(0), upstream);

when(generator.nextInt(1, 11)).thenReturn(2);
when(generator.nextInt(10)).thenReturn(2);

upstream = balancer.next();
assertNotNull(upstream);
assertEquals(upstreams.get(1), upstream);

when(generator.nextInt(1, 11)).thenReturn(6);
when(generator.nextInt(10)).thenReturn(5);

upstream = balancer.next();
assertNotNull(upstream);
assertEquals(upstreams.get(2), upstream);

when(generator.nextInt(1, 11)).thenReturn(10);
when(generator.nextInt(10)).thenReturn(9);

upstream = balancer.next();
assertNotNull(upstream);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,8 @@ void testUpstreamFallback() {
.map(index -> new Upstream("endpoint" + index, null, null, 1, 1))
.toList();
model.setUpstreams(upstreams);
AtomicInteger counter = new AtomicInteger();
when(generator.nextInt(1, 5)).thenAnswer(cb -> counter.incrementAndGet());
AtomicInteger counter = new AtomicInteger(-1);
when(generator.nextInt(4)).thenAnswer(cb -> counter.incrementAndGet());
Supplier<Random> factory = () -> generator;

UpstreamRouteProvider upstreamRouteProvider = new UpstreamRouteProvider(vertx, factory);
Expand Down

0 comments on commit 5ef1f93

Please sign in to comment.