Apache9 commented on code in PR #4954:
URL: https://github.com/apache/hbase/pull/4954#discussion_r1068074857


##########
hbase-it/src/test/java/org/apache/hadoop/hbase/chaos/monkies/PolicyBasedChaosMonkey.java:
##########
@@ -116,13 +120,31 @@ public static <T> T selectWeightedRandomItem(List<Pair<T, 
Integer>> items) {
 
   /** Selects and returns ceil(ratio * items.length) random items from the 
given array */
   public static <T> List<T> selectRandomItems(T[] items, float ratio) {
-    int selectedNumber = (int) Math.ceil(items.length * ratio);
+    /*
+     * N.b. `ratio` values are not validated. Be aware of excessive values and 
floating point
+     * arithmetic rounding. Guard against negative input to Random#next() and 
exceeding boundaries
+     * in call to List#subList.
+     */
 
-    List<T> originalItems = Arrays.asList(items);
-    Collections.shuffle(originalItems);
+    // clamp ratio to [0.0,1.0]
+    ratio = Math.max(Math.min(ratio, 1.0f), 0.0f);
 
-    int startIndex = ThreadLocalRandom.current().nextInt(items.length - 
selectedNumber);
-    return originalItems.subList(startIndex, startIndex + selectedNumber);
+    final int selectedNumber = (int) Math.ceil(items.length * ratio);
+
+    // shuffle a copy of the input, not the input.
+    final List<T> shuffledItems = new ArrayList<>(items.length);
+    shuffledItems.addAll(Arrays.asList(items));
+    Collections.shuffle(shuffledItems);
+
+    if (selectedNumber >= items.length) {
+      return shuffledItems;
+    }
+
+    // apply basic sanity check on sublist selection range.
+    final int startIndex =
+      Math.max(0, ThreadLocalRandom.current().nextInt(items.length - 
selectedNumber));
+    final int endIndex = Math.min(items.length, startIndex + selectedNumber);
+    return shuffledItems.subList(startIndex, endIndex);

Review Comment:
   Maybe here you could make use of 
org.apache.hadoop.hbase.util.ReservoirSample so we do not need to copy the 
whole array? Not a blocker issue anyway.



-- 
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