Apache9 commented on code in PR #4954:
URL: https://github.com/apache/hbase/pull/4954#discussion_r1067756498
##########
hbase-it/src/test/java/org/apache/hadoop/hbase/chaos/monkies/PolicyBasedChaosMonkey.java:
##########
@@ -116,13 +119,16 @@ 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);
-
- List<T> originalItems = Arrays.asList(items);
+ final int selectedNumber = (int) Math.ceil(items.length * ratio);
+ final List<T> originalItems = Arrays.asList(items);
Review Comment:
Arrays.asList will create a ArrayList which is backed by the given array, so
the later shuffle will change the order of the items array which is passed in
as a parameter. If we think this is acceptable, we can just shuffle on the
given array instead of wrapping it? If not, I think we should copy the array.
##########
hbase-it/src/test/java/org/apache/hadoop/hbase/chaos/monkies/PolicyBasedChaosMonkey.java:
##########
@@ -116,13 +119,16 @@ 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);
-
- List<T> originalItems = Arrays.asList(items);
+ final int selectedNumber = (int) Math.ceil(items.length * ratio);
+ final List<T> originalItems = Arrays.asList(items);
Collections.shuffle(originalItems);
-
- int startIndex = ThreadLocalRandom.current().nextInt(items.length -
selectedNumber);
- return originalItems.subList(startIndex, startIndex + selectedNumber);
+ if (selectedNumber == items.length) {
Review Comment:
Use >= for safety?
##########
hbase-it/src/test/java/org/apache/hadoop/hbase/chaos/monkies/PolicyBasedChaosMonkey.java:
##########
@@ -116,13 +116,13 @@ 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);
-
- List<T> originalItems = Arrays.asList(items);
+ final int selectedNumber = (int) Math.ceil(items.length * ratio);
+ final List<T> originalItems = Arrays.asList(items);
Collections.shuffle(originalItems);
-
- int startIndex = ThreadLocalRandom.current().nextInt(items.length -
selectedNumber);
- return originalItems.subList(startIndex, startIndex + selectedNumber);
+ final int startIndex =
Review Comment:
I think we'd better add some comments here to say why we need these guards?
Is it because of the floating point precision?
--
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]