Skip to content

Commit 03de3f9

Browse files
fixed feedback
1 parent 21ceb85 commit 03de3f9

File tree

2 files changed

+24
-30
lines changed

2 files changed

+24
-30
lines changed

xds/src/main/java/io/grpc/xds/WeightedRoundRobinLoadBalancer.java

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -332,10 +332,12 @@ public boolean isEquivalentTo(RoundRobinPicker picker) {
332332
* Implementation of Static Stride Scheduler, replaces EDFScheduler.
333333
* <p>
334334
* The Static Stride Scheduler works by iterating through the list of subchannel weights
335-
* and using modular arithmetic to evenly distribute picks and skips, favoring entries with the
336-
* highest weight. It generates a practically equivalent sequence of picks as the EDFScheduler.
337-
* Albeit needing more bandwidth, the Static Stride Scheduler is more performant than the
338-
* EDFScheduler, as it removes the need for a priority queue (and thus mutex locks).
335+
* and using modular arithmetic to proportionally distribute picks, favoring entries
336+
* with higher weights. It is based on the observation that the intended sequence generated
337+
* from the EDF scheduler is a periodic one that can be achieved through modular arithmetic.
338+
* This scheduler generates a practically equivalent sequence of picks as the EDFScheduler.
339+
* The Static Stride Scheduler is more performant than the EDFScheduler, as it removes
340+
* the need for a priority queue (and thus mutex locks).
339341
* <p>
340342
* go/static-stride-scheduler
341343
* <p>
@@ -359,7 +361,7 @@ static final class StaticStrideScheduler {
359361
float maxWeight = 0;
360362
int meanWeight = 0;
361363
for (float weight : weights) {
362-
if (weight > 0.0001) {
364+
if (weight > 0) {
363365
sumWeight += weight;
364366
maxWeight = Math.max(weight, maxWeight);
365367
numWeightedChannels++;
@@ -376,7 +378,7 @@ static final class StaticStrideScheduler {
376378
// scales weights s.t. max(weights) == K_MAX_WEIGHT, meanWeight is scaled accordingly
377379
int[] scaledWeights = new int[numChannels];
378380
for (int i = 0; i < numChannels; i++) {
379-
if (weights[i] < 0.0001) {
381+
if (weights[i] <= 0) {
380382
scaledWeights[i] = meanWeight;
381383
} else {
382384
scaledWeights[i] = (int) Math.round(weights[i] * scalingFactor);
@@ -394,7 +396,7 @@ private long nextSequence() {
394396
return Integer.toUnsignedLong(sequence.getAndIncrement());
395397
}
396398

397-
public long getSequence() {
399+
long getSequence() {
398400
return Integer.toUnsignedLong(sequence.get());
399401
}
400402

xds/src/test/java/io/grpc/xds/WeightedRoundRobinLoadBalancerTest.java

Lines changed: 15 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ public Void answer(InvocationOnMock invocation) throws Throwable {
175175
}
176176
});
177177
wrr = new WeightedRoundRobinLoadBalancer(helper, fakeClock.getDeadlineTicker(),
178-
new FakeRandom());
178+
new FakeRandom(0));
179179
}
180180

181181
@Test
@@ -919,15 +919,15 @@ public void testAllInvalidWeightsUseOne() {
919919
}
920920

921921
@Test
922-
public void testLacrgestWeightIndexPickedEveryGeneration() {
922+
public void testLargestWeightIndexPickedEveryGeneration() {
923923
float[] weights = {1.0f, 2.0f, 3.0f};
924-
int mean = 2;
924+
int largestWeightIndex = 2;
925925
Random random = new Random();
926926
StaticStrideScheduler sss = new StaticStrideScheduler(weights, random);
927927
int largestWeightPickCount = 0;
928928
int kMaxWeight = 65535;
929-
for (int i = 0; i < mean * kMaxWeight; i++) {
930-
if (sss.pick() == mean) {
929+
for (int i = 0; i < largestWeightIndex * kMaxWeight; i++) {
930+
if (sss.pick() == largestWeightIndex) {
931931
largestWeightPickCount += 1;
932932
}
933933
}
@@ -1022,7 +1022,7 @@ public void testManyComplexWeights() {
10221022
@Test
10231023
public void testDeterministicPicks() {
10241024
float[] weights = {2.0f, 3.0f, 6.0f};
1025-
Random random = new FakeRandom();
1025+
Random random = new FakeRandom(0);
10261026
StaticStrideScheduler sss = new StaticStrideScheduler(weights, random);
10271027
assertThat(sss.getSequence()).isEqualTo(0);
10281028
assertThat(sss.pick()).isEqualTo(1);
@@ -1042,7 +1042,7 @@ public void testDeterministicPicks() {
10421042
@Test
10431043
public void testImmediateWraparound() {
10441044
float[] weights = {1.0f, 2.0f, 3.0f, 4.0f, 5.0f};
1045-
Random random = new FakeRandomWraparound();
1045+
Random random = new FakeRandom(-1);
10461046
StaticStrideScheduler sss = new StaticStrideScheduler(weights, random);
10471047
double totalWeight = 15;
10481048
Map<Integer, Integer> pickCount = new HashMap<>();
@@ -1059,7 +1059,7 @@ public void testImmediateWraparound() {
10591059
@Test
10601060
public void testWraparound() {
10611061
float[] weights = {1.0f, 2.0f, 3.0f, 4.0f, 5.0f};
1062-
Random random = new FakeRandomWraparound2();
1062+
Random random = new FakeRandom(-500);
10631063
StaticStrideScheduler sss = new StaticStrideScheduler(weights, random);
10641064
double totalWeight = 15;
10651065
Map<Integer, Integer> pickCount = new HashMap<>();
@@ -1076,7 +1076,7 @@ public void testWraparound() {
10761076
@Test
10771077
public void testDeterministicWraparound() {
10781078
float[] weights = {2.0f, 3.0f, 6.0f};
1079-
Random random = new FakeRandomWraparound();
1079+
Random random = new FakeRandom(-1);
10801080
StaticStrideScheduler sss = new StaticStrideScheduler(weights, random);
10811081
assertThat(sss.getSequence()).isEqualTo(0xFFFF_FFFFL);
10821082
assertThat(sss.pick()).isEqualTo(1);
@@ -1106,25 +1106,17 @@ private static class FakeSocketAddress extends SocketAddress {
11061106
}
11071107

11081108
private static class FakeRandom extends Random {
1109-
@Override
1110-
public int nextInt() {
1111-
// return constant value to disable init deadline randomization in the scheduler
1112-
// sequence will be initialized to 0
1113-
return 0;
1114-
}
1115-
}
1109+
private int nextInt;
11161110

1117-
private static class FakeRandomWraparound extends Random {
1118-
@Override
1119-
public int nextInt() {
1120-
return -1;
1111+
public FakeRandom(int nextInt) {
1112+
this.nextInt = nextInt;
11211113
}
1122-
}
11231114

1124-
private static class FakeRandomWraparound2 extends Random {
11251115
@Override
11261116
public int nextInt() {
1127-
return -500;
1117+
// return constant value to disable init deadline randomization in the scheduler
1118+
// sequence will be initialized to 0
1119+
return nextInt;
11281120
}
11291121
}
11301122
}

0 commit comments

Comments
 (0)