|
17 | 17 | */ |
18 | 18 | package org.apache.hadoop.hbase.chaos.monkies; |
19 | 19 |
|
20 | | -import java.util.ArrayList; |
21 | 20 | import java.util.Arrays; |
22 | 21 | import java.util.Collection; |
23 | 22 | import java.util.Collections; |
|
31 | 30 | import org.apache.hadoop.hbase.IntegrationTestingUtility; |
32 | 31 | import org.apache.hadoop.hbase.chaos.policies.Policy; |
33 | 32 | import org.apache.hadoop.hbase.util.Pair; |
34 | | -import org.slf4j.Logger; |
35 | | -import org.slf4j.LoggerFactory; |
36 | 33 |
|
37 | 34 | import org.apache.hbase.thirdparty.com.google.common.util.concurrent.ThreadFactoryBuilder; |
38 | 35 |
|
39 | 36 | /** |
40 | 37 | * Chaos monkey that given multiple policies will run actions against the cluster. |
41 | 38 | */ |
42 | 39 | public class PolicyBasedChaosMonkey extends ChaosMonkey { |
43 | | - private static final Logger LOG = LoggerFactory.getLogger(PolicyBasedChaosMonkey.class); |
44 | 40 |
|
45 | 41 | private static final long ONE_SEC = 1000; |
46 | 42 | private static final long ONE_MIN = 60 * ONE_SEC; |
@@ -120,31 +116,13 @@ public static <T> T selectWeightedRandomItem(List<Pair<T, Integer>> items) { |
120 | 116 |
|
121 | 117 | /** Selects and returns ceil(ratio * items.length) random items from the given array */ |
122 | 118 | public static <T> List<T> selectRandomItems(T[] items, float ratio) { |
123 | | - /* |
124 | | - * N.b. `ratio` values are not validated. Be aware of excessive values and floating point |
125 | | - * arithmetic rounding. Guard against negative input to Random#next() and exceeding boundaries |
126 | | - * in call to List#subList. |
127 | | - */ |
| 119 | + int selectedNumber = (int) Math.ceil(items.length * ratio); |
128 | 120 |
|
129 | | - // clamp ratio to [0.0,1.0] |
130 | | - ratio = Math.max(Math.min(ratio, 1.0f), 0.0f); |
| 121 | + List<T> originalItems = Arrays.asList(items); |
| 122 | + Collections.shuffle(originalItems); |
131 | 123 |
|
132 | | - final int selectedNumber = (int) Math.ceil(items.length * ratio); |
133 | | - |
134 | | - // shuffle a copy of the input, not the input. |
135 | | - final List<T> shuffledItems = new ArrayList<>(items.length); |
136 | | - shuffledItems.addAll(Arrays.asList(items)); |
137 | | - Collections.shuffle(shuffledItems); |
138 | | - |
139 | | - if (selectedNumber >= items.length) { |
140 | | - return shuffledItems; |
141 | | - } |
142 | | - |
143 | | - // apply basic sanity check on sublist selection range. |
144 | | - final int startIndex = |
145 | | - Math.max(0, ThreadLocalRandom.current().nextInt(items.length - selectedNumber)); |
146 | | - final int endIndex = Math.min(items.length, startIndex + selectedNumber); |
147 | | - return shuffledItems.subList(startIndex, endIndex); |
| 124 | + int startIndex = ThreadLocalRandom.current().nextInt(items.length - selectedNumber); |
| 125 | + return originalItems.subList(startIndex, startIndex + selectedNumber); |
148 | 126 | } |
149 | 127 |
|
150 | 128 | @Override |
@@ -173,10 +151,7 @@ public boolean isStopped() { |
173 | 151 |
|
174 | 152 | @Override |
175 | 153 | public void waitForStop() throws InterruptedException { |
176 | | - if (!monkeyThreadPool.awaitTermination(1, TimeUnit.MINUTES)) { |
177 | | - LOG.warn("Some pool threads failed to terminate. Forcing. {}", monkeyThreadPool); |
178 | | - monkeyThreadPool.shutdownNow(); |
179 | | - } |
| 154 | + monkeyThreadPool.awaitTermination(1, TimeUnit.MINUTES); |
180 | 155 | } |
181 | 156 |
|
182 | 157 | @Override |
|
0 commit comments