Backport "HBASE-27563 ChaosMonkey sometimes generates invalid boundaries for random item selection" to branch-2.4 (#4962)
HBASE-27563 ChaosMonkey sometimes generates invalid boundaries for random item selection Signed-off-by: Duo Zhang <zhangduo@apache.org>
This commit is contained in:
parent
4902c08def
commit
f245c64993
|
@ -17,6 +17,7 @@
|
||||||
*/
|
*/
|
||||||
package org.apache.hadoop.hbase.chaos.monkies;
|
package org.apache.hadoop.hbase.chaos.monkies;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
|
@ -30,6 +31,8 @@ import java.util.concurrent.TimeUnit;
|
||||||
import org.apache.hadoop.hbase.IntegrationTestingUtility;
|
import org.apache.hadoop.hbase.IntegrationTestingUtility;
|
||||||
import org.apache.hadoop.hbase.chaos.policies.Policy;
|
import org.apache.hadoop.hbase.chaos.policies.Policy;
|
||||||
import org.apache.hadoop.hbase.util.Pair;
|
import org.apache.hadoop.hbase.util.Pair;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
import org.apache.hbase.thirdparty.com.google.common.util.concurrent.ThreadFactoryBuilder;
|
import org.apache.hbase.thirdparty.com.google.common.util.concurrent.ThreadFactoryBuilder;
|
||||||
|
|
||||||
|
@ -37,6 +40,7 @@ import org.apache.hbase.thirdparty.com.google.common.util.concurrent.ThreadFacto
|
||||||
* Chaos monkey that given multiple policies will run actions against the cluster.
|
* Chaos monkey that given multiple policies will run actions against the cluster.
|
||||||
*/
|
*/
|
||||||
public class PolicyBasedChaosMonkey extends ChaosMonkey {
|
public class PolicyBasedChaosMonkey extends ChaosMonkey {
|
||||||
|
private static final Logger LOG = LoggerFactory.getLogger(PolicyBasedChaosMonkey.class);
|
||||||
|
|
||||||
private static final long ONE_SEC = 1000;
|
private static final long ONE_SEC = 1000;
|
||||||
private static final long ONE_MIN = 60 * ONE_SEC;
|
private static final long ONE_MIN = 60 * ONE_SEC;
|
||||||
|
@ -116,13 +120,31 @@ public class PolicyBasedChaosMonkey extends ChaosMonkey {
|
||||||
|
|
||||||
/** Selects and returns ceil(ratio * items.length) random items from the given array */
|
/** Selects and returns ceil(ratio * items.length) random items from the given array */
|
||||||
public static <T> List<T> selectRandomItems(T[] items, float ratio) {
|
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);
|
// clamp ratio to [0.0,1.0]
|
||||||
Collections.shuffle(originalItems);
|
ratio = Math.max(Math.min(ratio, 1.0f), 0.0f);
|
||||||
|
|
||||||
int startIndex = ThreadLocalRandom.current().nextInt(items.length - selectedNumber);
|
final int selectedNumber = (int) Math.ceil(items.length * ratio);
|
||||||
return originalItems.subList(startIndex, startIndex + selectedNumber);
|
|
||||||
|
// 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);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -151,7 +173,10 @@ public class PolicyBasedChaosMonkey extends ChaosMonkey {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void waitForStop() throws InterruptedException {
|
public void waitForStop() throws InterruptedException {
|
||||||
monkeyThreadPool.awaitTermination(1, TimeUnit.MINUTES);
|
if (!monkeyThreadPool.awaitTermination(1, TimeUnit.MINUTES)) {
|
||||||
|
LOG.warn("Some pool threads failed to terminate. Forcing. {}", monkeyThreadPool);
|
||||||
|
monkeyThreadPool.shutdownNow();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
Loading…
Reference in New Issue