|
12 | 12 | import org.elasticsearch.TransportVersions; |
13 | 13 | import org.elasticsearch.action.ActionListener; |
14 | 14 | import org.elasticsearch.action.support.ActionTestUtils; |
15 | | -import org.elasticsearch.action.support.replication.ClusterStateCreationUtils; |
16 | 15 | import org.elasticsearch.cluster.ClusterState; |
17 | 16 | import org.elasticsearch.cluster.ESAllocationTestCase; |
18 | 17 | import org.elasticsearch.cluster.action.shard.ShardStateAction.StartedShardEntry; |
19 | 18 | import org.elasticsearch.cluster.action.shard.ShardStateAction.StartedShardUpdateTask; |
20 | | -import org.elasticsearch.cluster.block.ClusterBlocks; |
21 | 19 | import org.elasticsearch.cluster.metadata.IndexMetadata; |
22 | 20 | import org.elasticsearch.cluster.metadata.Metadata; |
23 | | -import org.elasticsearch.cluster.routing.AllocationId; |
24 | | -import org.elasticsearch.cluster.routing.IndexRoutingTable; |
25 | 21 | import org.elasticsearch.cluster.routing.IndexShardRoutingTable; |
26 | | -import org.elasticsearch.cluster.routing.RoutingTable; |
27 | 22 | import org.elasticsearch.cluster.routing.ShardRouting; |
28 | 23 | import org.elasticsearch.cluster.routing.ShardRoutingState; |
29 | 24 | import org.elasticsearch.cluster.routing.allocation.AllocationService; |
30 | 25 | import org.elasticsearch.cluster.service.ClusterStateTaskExecutorUtils; |
31 | 26 | import org.elasticsearch.common.Priority; |
32 | 27 | import org.elasticsearch.common.settings.Settings; |
33 | | -import org.elasticsearch.core.Tuple; |
34 | 28 | import org.elasticsearch.index.shard.IndexLongFieldRange; |
35 | 29 | import org.elasticsearch.index.shard.ShardId; |
36 | 30 | import org.elasticsearch.index.shard.ShardLongFieldRange; |
37 | 31 |
|
38 | 32 | import java.util.List; |
39 | | -import java.util.stream.Collectors; |
40 | 33 | import java.util.stream.IntStream; |
41 | 34 | import java.util.stream.Stream; |
42 | 35 |
|
43 | 36 | import static org.elasticsearch.action.support.replication.ClusterStateCreationUtils.state; |
44 | 37 | import static org.elasticsearch.action.support.replication.ClusterStateCreationUtils.stateWithActivePrimary; |
45 | 38 | import static org.elasticsearch.action.support.replication.ClusterStateCreationUtils.stateWithAssignedPrimariesAndReplicas; |
46 | 39 | import static org.elasticsearch.action.support.replication.ClusterStateCreationUtils.stateWithNoShard; |
47 | | -import static org.elasticsearch.cluster.metadata.IndexMetadata.INDEX_REFRESH_BLOCK; |
48 | 40 | import static org.elasticsearch.cluster.routing.allocation.decider.ThrottlingAllocationDecider.CLUSTER_ROUTING_ALLOCATION_NODE_CONCURRENT_RECOVERIES_SETTING; |
49 | 41 | import static org.hamcrest.Matchers.equalTo; |
50 | 42 | import static org.hamcrest.Matchers.is; |
51 | | -import static org.hamcrest.Matchers.notNullValue; |
52 | 43 | import static org.hamcrest.Matchers.sameInstance; |
53 | 44 |
|
54 | 45 | public class ShardStartedClusterStateTaskExecutorTests extends ESAllocationTestCase { |
@@ -488,114 +479,6 @@ public void testExpandsTimestampRangeForReplica() throws Exception { |
488 | 479 | assertThat(latestIndexMetadata.getEventIngestedRange(), sameInstance(IndexLongFieldRange.UNKNOWN)); |
489 | 480 | } |
490 | 481 |
|
491 | | - public void testIndexRefreshBlockIsClearedOnceTheIndexIsReadyToBeSearched() throws Exception { |
492 | | - final var indexName = "test"; |
493 | | - final var numberOfShards = randomIntBetween(1, 4); |
494 | | - final var numberOfReplicas = randomIntBetween(1, 4); |
495 | | - var clusterState = ClusterStateCreationUtils.stateWithAssignedPrimariesAndReplicasWithState( |
496 | | - new String[] { indexName }, |
497 | | - numberOfShards, |
498 | | - ShardRouting.Role.INDEX_ONLY, |
499 | | - IntStream.range(0, numberOfReplicas) |
500 | | - .mapToObj(unused -> Tuple.tuple(ShardRoutingState.UNASSIGNED, ShardRouting.Role.SEARCH_ONLY)) |
501 | | - .toList() |
502 | | - ); |
503 | | - |
504 | | - clusterState = ClusterState.builder(clusterState) |
505 | | - .metadata(Metadata.builder(clusterState.metadata()).put(withActiveShardsInSyncAllocationIds(clusterState, indexName))) |
506 | | - .blocks(ClusterBlocks.builder(clusterState.blocks()).addIndexBlock(indexName, INDEX_REFRESH_BLOCK)) |
507 | | - .build(); |
508 | | - |
509 | | - while (clusterState.blocks().hasIndexBlock(indexName, INDEX_REFRESH_BLOCK)) { |
510 | | - clusterState = maybeInitializeUnassignedReplicaShard(clusterState); |
511 | | - |
512 | | - final IndexMetadata indexMetadata = clusterState.metadata().index(indexName); |
513 | | - |
514 | | - final var initializingReplicaShardOpt = clusterState.routingTable() |
515 | | - .allShards() |
516 | | - .filter(shardRouting -> shardRouting.isPromotableToPrimary() == false) |
517 | | - .filter(shardRouting -> shardRouting.state().equals(ShardRoutingState.INITIALIZING)) |
518 | | - .findFirst(); |
519 | | - |
520 | | - assertThat(clusterState.routingTable().allShards().toList().toString(), initializingReplicaShardOpt.isPresent(), is(true)); |
521 | | - |
522 | | - var initializingReplicaShard = initializingReplicaShardOpt.get(); |
523 | | - |
524 | | - final var shardId = initializingReplicaShard.shardId(); |
525 | | - final var primaryTerm = indexMetadata.primaryTerm(shardId.id()); |
526 | | - final var replicaAllocationId = initializingReplicaShard.allocationId().getId(); |
527 | | - final var task = new StartedShardUpdateTask( |
528 | | - new StartedShardEntry( |
529 | | - shardId, |
530 | | - replicaAllocationId, |
531 | | - primaryTerm, |
532 | | - "test", |
533 | | - ShardLongFieldRange.UNKNOWN, |
534 | | - ShardLongFieldRange.UNKNOWN |
535 | | - ), |
536 | | - createTestListener() |
537 | | - ); |
538 | | - |
539 | | - final var resultingState = executeTasks(clusterState, List.of(task)); |
540 | | - assertNotSame(clusterState, resultingState); |
541 | | - |
542 | | - clusterState = resultingState; |
543 | | - } |
544 | | - |
545 | | - var indexRoutingTable = clusterState.routingTable().index(indexName); |
546 | | - assertThat(indexRoutingTable.readyForSearch(), is(true)); |
547 | | - for (int i = 0; i < numberOfShards; i++) { |
548 | | - var shardRoutingTable = indexRoutingTable.shard(i); |
549 | | - assertThat(shardRoutingTable, is(notNullValue())); |
550 | | - // Ensure that at least one unpromotable shard is either STARTED or RELOCATING |
551 | | - assertThat(shardRoutingTable.unpromotableShards().isEmpty(), is(false)); |
552 | | - } |
553 | | - assertThat(clusterState.blocks().hasIndexBlock(indexName, INDEX_REFRESH_BLOCK), is(false)); |
554 | | - } |
555 | | - |
556 | | - private static ClusterState maybeInitializeUnassignedReplicaShard(ClusterState clusterState) { |
557 | | - var unassignedShardRoutingOpt = clusterState.routingTable() |
558 | | - .allShards() |
559 | | - .filter(shardRouting -> shardRouting.state().equals(ShardRoutingState.UNASSIGNED)) |
560 | | - .findFirst(); |
561 | | - |
562 | | - if (unassignedShardRoutingOpt.isEmpty()) { |
563 | | - return clusterState; |
564 | | - } |
565 | | - |
566 | | - var unassignedShardRouting = unassignedShardRoutingOpt.get(); |
567 | | - var initializedShard = unassignedShardRouting.initialize(randomUUID(), null, 1); |
568 | | - |
569 | | - RoutingTable routingTable = clusterState.routingTable(); |
570 | | - IndexRoutingTable indexRoutingTable = routingTable.index(unassignedShardRouting.getIndexName()); |
571 | | - IndexRoutingTable.Builder newIndexRoutingTable = IndexRoutingTable.builder(indexRoutingTable.getIndex()); |
572 | | - for (int shardId = 0; shardId < indexRoutingTable.size(); shardId++) { |
573 | | - IndexShardRoutingTable shardRoutingTable = indexRoutingTable.shard(shardId); |
574 | | - for (int copy = 0; copy < shardRoutingTable.size(); copy++) { |
575 | | - ShardRouting shardRouting = shardRoutingTable.shard(copy); |
576 | | - newIndexRoutingTable.addShard(shardRouting == unassignedShardRouting ? initializedShard : shardRouting); |
577 | | - } |
578 | | - } |
579 | | - routingTable = RoutingTable.builder(routingTable).add(newIndexRoutingTable).build(); |
580 | | - return ClusterState.builder(clusterState).routingTable(routingTable).build(); |
581 | | - } |
582 | | - |
583 | | - private static IndexMetadata.Builder withActiveShardsInSyncAllocationIds(ClusterState clusterState, String indexName) { |
584 | | - IndexMetadata.Builder indexMetadataBuilder = new IndexMetadata.Builder(clusterState.metadata().index(indexName)); |
585 | | - var indexRoutingTable = clusterState.routingTable().index(indexName); |
586 | | - for (IndexShardRoutingTable indexShardRoutingTable : indexRoutingTable.allShards().toList()) { |
587 | | - indexMetadataBuilder.putInSyncAllocationIds( |
588 | | - indexShardRoutingTable.shardId().getId(), |
589 | | - indexShardRoutingTable.activeShards() |
590 | | - .stream() |
591 | | - .map(ShardRouting::allocationId) |
592 | | - .map(AllocationId::getId) |
593 | | - .collect(Collectors.toSet()) |
594 | | - ); |
595 | | - } |
596 | | - return indexMetadataBuilder; |
597 | | - } |
598 | | - |
599 | 482 | private ClusterState executeTasks(final ClusterState state, final List<StartedShardUpdateTask> tasks) throws Exception { |
600 | 483 | return ClusterStateTaskExecutorUtils.executeAndAssertSuccessful(state, executor, tasks); |
601 | 484 | } |
|
0 commit comments