hangc0276 commented on code in PR #4819:
URL: https://github.com/apache/bookkeeper/pull/4819#discussion_r4760656499


##########
bookkeeper-server/src/main/java/org/apache/bookkeeper/client/RoundRobinDistributionSchedule.java:
##########
@@ -247,7 +247,7 @@ public String toString() {
         }
 
         private void checkBounds(int i) {
-            if (i < 0 || i > size) {
+            if (i < 0 || i >= size) {

Review Comment:
   Tightening this to `i >= size` is right. One small side effect: 
`WriteSetImpl.toString()` (just above) calls `get(0)` when `size() == 0`, 
because its loop stops at `size() - 1` and the final `b.append(get(i))` runs 
unconditionally. For an empty set, `writeSetFromValues().toString()` now throws 
`IndexOutOfBoundsException: Index 0 out of bounds, array size = 0`. On the old 
code it threw `ArrayIndexOutOfBoundsException` from the unsized array, so it 
never worked either. I could not find a production path that builds or logs an 
empty `WriteSetImpl` (the write quorum is at least 1), so this is a latent nit. 
A `size() == 0` guard in `toString` would close it.



##########
bookkeeper-server/src/main/java/org/apache/bookkeeper/client/TopologyAwareEnsemblePlacementPolicy.java:
##########
@@ -628,8 +628,8 @@ static void shuffleWithMask(DistributionSchedule.WriteSet 
writeSet,
             }
         }
         if (first != -1) {
-            for (int i = last + 1; i > first; i--) {
-                int swapWith = ThreadLocalRandom.current().nextInt(i);
+            for (int i = last; i > first; i--) {
+                int swapWith = ThreadLocalRandom.current().nextInt(first, i + 
1);

Review Comment:
   The new range is correct: a Fisher–Yates over `[first, last]`, and 
`nextInt(first, i + 1)` is always valid because `i > first` inside the loop. I 
checked it with tests that use real masked values:
   - The group at the start, in the middle and at the end, the whole set, and a 
singleton: no entry outside `[first, last]` moves and the group keeps exactly 
its own entries.
   - 12,000 trials on a 3-entry group reach all 6 orders about equally often 
(each within ±500 of 2,000).
   
   On the old code the same tests fail: the group at the end throws 
`ArrayIndexOutOfBoundsException: Index 6 out of bounds for length 6`, entries 
outside the range get swapped into it, and a 3-entry group produced 24 distinct 
outcomes instead of 6.
   
   But this change has no regression test in the PR. 
`TestRackawareEnsemblePlacementPolicy.testShuffleWithMask` builds its masked 
values as `3 & mask`, which is `0`, so no entry ever matches the mask and 
`shuffleWithMask` never shuffles. I replaced `shuffleWithMask` with a no-op and 
that test still passed. I also reverted only this shuffle change (keeping the 
bounds fix) and ran the existing placement suites (`TestRackaware…`, 
`TestRegionAware…`, `TestZoneaware…` and the new bounds test): 116 tests, 0 
failures. That combination would make `reorderReadSequence` throw when the 
masked group is at the end of the write set, and nothing in the repo would 
catch it. My tests are in the review summary if useful; use `v | mask` rather 
than `v & mask`.



##########
bookkeeper-server/src/test/java/org/apache/bookkeeper/client/RoundRobinDistributionScheduleTest.java:
##########
@@ -157,6 +158,31 @@ public void testMoveAndShift() {
         assertEquals(w, writeSetFromValues(1, 2, 3, 4, 5));
     }
 
+    @Test
+    public void testWriteSetRejectsIndexEqualToSize() {

Review Comment:
   This test covers the bounds check well (`get`, `set`, `moveAndShift` at 
`size`). It doesn't cover the `shuffleWithMask` half of the PR; see my comment 
on `TopologyAwareEnsemblePlacementPolicy.java`. I'd add shuffle coverage with 
real masked values next to it, for example the tests in the review summary.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to