|
53 | 53 | import org.elasticsearch.snapshots.SnapshotId; |
54 | 54 | import org.elasticsearch.snapshots.SnapshotShardSizeInfo; |
55 | 55 | import org.elasticsearch.test.MockLog; |
56 | | -import org.elasticsearch.threadpool.ThreadPool; |
57 | 56 |
|
58 | 57 | import java.util.ArrayList; |
59 | 58 | import java.util.HashMap; |
|
85 | 84 | import static org.hamcrest.Matchers.hasEntry; |
86 | 85 | import static org.hamcrest.Matchers.lessThanOrEqualTo; |
87 | 86 | import static org.hamcrest.Matchers.notNullValue; |
88 | | -import static org.mockito.Mockito.mock; |
89 | | -import static org.mockito.Mockito.when; |
90 | 87 |
|
91 | 88 | public class DesiredBalanceComputerTests extends ESAllocationTestCase { |
92 | 89 |
|
@@ -1205,43 +1202,43 @@ public void testShouldLogComputationIteration() { |
1205 | 1202 | } |
1206 | 1203 |
|
1207 | 1204 | private void checkIterationLogging(int iterations, long eachIterationDuration, MockLog.AbstractEventExpectation expectation) { |
1208 | | - |
1209 | | - var mockThreadPool = mock(ThreadPool.class); |
1210 | 1205 | var currentTime = new AtomicLong(0L); |
1211 | | - when(mockThreadPool.relativeTimeInMillis()).thenAnswer(invocation -> currentTime.addAndGet(eachIterationDuration)); |
1212 | | - |
1213 | 1206 | // Some runs of this test try to simulate a long desired balance computation. Setting a high value on the following setting |
1214 | 1207 | // prevents interrupting a long computation. |
1215 | 1208 | var clusterSettings = createBuiltInClusterSettings( |
1216 | 1209 | Settings.builder().put(DesiredBalanceComputer.MAX_BALANCE_COMPUTATION_TIME_DURING_INDEX_CREATION_SETTING.getKey(), "2m").build() |
1217 | 1210 | ); |
1218 | | - var desiredBalanceComputer = new DesiredBalanceComputer(clusterSettings, mockThreadPool, new ShardsAllocator() { |
1219 | | - @Override |
1220 | | - public void allocate(RoutingAllocation allocation) { |
1221 | | - final var unassignedIterator = allocation.routingNodes().unassigned().iterator(); |
1222 | | - while (unassignedIterator.hasNext()) { |
1223 | | - final var shardRouting = unassignedIterator.next(); |
1224 | | - if (shardRouting.primary()) { |
1225 | | - unassignedIterator.initialize("node-0", null, 0L, allocation.changes()); |
1226 | | - } else { |
1227 | | - unassignedIterator.removeAndIgnore(UnassignedInfo.AllocationStatus.NO_ATTEMPT, allocation.changes()); |
| 1211 | + var desiredBalanceComputer = new DesiredBalanceComputer( |
| 1212 | + clusterSettings, |
| 1213 | + () -> currentTime.addAndGet(eachIterationDuration), |
| 1214 | + new ShardsAllocator() { |
| 1215 | + @Override |
| 1216 | + public void allocate(RoutingAllocation allocation) { |
| 1217 | + final var unassignedIterator = allocation.routingNodes().unassigned().iterator(); |
| 1218 | + while (unassignedIterator.hasNext()) { |
| 1219 | + final var shardRouting = unassignedIterator.next(); |
| 1220 | + if (shardRouting.primary()) { |
| 1221 | + unassignedIterator.initialize("node-0", null, 0L, allocation.changes()); |
| 1222 | + } else { |
| 1223 | + unassignedIterator.removeAndIgnore(UnassignedInfo.AllocationStatus.NO_ATTEMPT, allocation.changes()); |
| 1224 | + } |
1228 | 1225 | } |
1229 | | - } |
1230 | 1226 |
|
1231 | | - // move shard on each iteration |
1232 | | - for (var shard : allocation.routingNodes().node("node-0").shardsWithState(STARTED).toList()) { |
1233 | | - allocation.routingNodes().relocateShard(shard, "node-1", 0L, "test", allocation.changes()); |
1234 | | - } |
1235 | | - for (var shard : allocation.routingNodes().node("node-1").shardsWithState(STARTED).toList()) { |
1236 | | - allocation.routingNodes().relocateShard(shard, "node-0", 0L, "test", allocation.changes()); |
| 1227 | + // move shard on each iteration |
| 1228 | + for (var shard : allocation.routingNodes().node("node-0").shardsWithState(STARTED).toList()) { |
| 1229 | + allocation.routingNodes().relocateShard(shard, "node-1", 0L, "test", allocation.changes()); |
| 1230 | + } |
| 1231 | + for (var shard : allocation.routingNodes().node("node-1").shardsWithState(STARTED).toList()) { |
| 1232 | + allocation.routingNodes().relocateShard(shard, "node-0", 0L, "test", allocation.changes()); |
| 1233 | + } |
1237 | 1234 | } |
1238 | | - } |
1239 | 1235 |
|
1240 | | - @Override |
1241 | | - public ShardAllocationDecision decideShardAllocation(ShardRouting shard, RoutingAllocation allocation) { |
1242 | | - throw new AssertionError("only used for allocation explain"); |
| 1236 | + @Override |
| 1237 | + public ShardAllocationDecision decideShardAllocation(ShardRouting shard, RoutingAllocation allocation) { |
| 1238 | + throw new AssertionError("only used for allocation explain"); |
| 1239 | + } |
1243 | 1240 | } |
1244 | | - }); |
| 1241 | + ); |
1245 | 1242 |
|
1246 | 1243 | assertThatLogger(() -> { |
1247 | 1244 | var iteration = new AtomicInteger(0); |
@@ -1349,7 +1346,7 @@ public ShardAllocationDecision decideShardAllocation(ShardRouting shard, Routing |
1349 | 1346 | } |
1350 | 1347 |
|
1351 | 1348 | private static DesiredBalanceComputer createDesiredBalanceComputer(ShardsAllocator allocator) { |
1352 | | - return new DesiredBalanceComputer(createBuiltInClusterSettings(), mock(ThreadPool.class), allocator); |
| 1349 | + return new DesiredBalanceComputer(createBuiltInClusterSettings(), () -> 0L, allocator); |
1353 | 1350 | } |
1354 | 1351 |
|
1355 | 1352 | private static void assertDesiredAssignments(DesiredBalance desiredBalance, Map<ShardId, ShardAssignment> expected) { |
|
0 commit comments