|
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; |
15 | 16 | import org.elasticsearch.cluster.ClusterState; |
16 | 17 | import org.elasticsearch.cluster.ESAllocationTestCase; |
17 | 18 | import org.elasticsearch.cluster.action.shard.ShardStateAction.StartedShardEntry; |
18 | 19 | import org.elasticsearch.cluster.action.shard.ShardStateAction.StartedShardUpdateTask; |
| 20 | +import org.elasticsearch.cluster.block.ClusterBlocks; |
19 | 21 | import org.elasticsearch.cluster.metadata.IndexMetadata; |
20 | 22 | import org.elasticsearch.cluster.metadata.Metadata; |
| 23 | +import org.elasticsearch.cluster.routing.AllocationId; |
| 24 | +import org.elasticsearch.cluster.routing.IndexRoutingTable; |
21 | 25 | import org.elasticsearch.cluster.routing.IndexShardRoutingTable; |
| 26 | +import org.elasticsearch.cluster.routing.RoutingTable; |
22 | 27 | import org.elasticsearch.cluster.routing.ShardRouting; |
23 | 28 | import org.elasticsearch.cluster.routing.ShardRoutingState; |
24 | 29 | import org.elasticsearch.cluster.routing.allocation.AllocationService; |
25 | 30 | import org.elasticsearch.cluster.service.ClusterStateTaskExecutorUtils; |
26 | 31 | import org.elasticsearch.common.Priority; |
27 | 32 | import org.elasticsearch.common.settings.Settings; |
| 33 | +import org.elasticsearch.core.Tuple; |
28 | 34 | import org.elasticsearch.index.shard.IndexLongFieldRange; |
29 | 35 | import org.elasticsearch.index.shard.ShardId; |
30 | 36 | import org.elasticsearch.index.shard.ShardLongFieldRange; |
31 | 37 |
|
32 | 38 | import java.util.List; |
| 39 | +import java.util.stream.Collectors; |
33 | 40 | import java.util.stream.IntStream; |
34 | 41 | import java.util.stream.Stream; |
35 | 42 |
|
36 | 43 | import static org.elasticsearch.action.support.replication.ClusterStateCreationUtils.state; |
37 | 44 | import static org.elasticsearch.action.support.replication.ClusterStateCreationUtils.stateWithActivePrimary; |
38 | 45 | import static org.elasticsearch.action.support.replication.ClusterStateCreationUtils.stateWithAssignedPrimariesAndReplicas; |
39 | 46 | import static org.elasticsearch.action.support.replication.ClusterStateCreationUtils.stateWithNoShard; |
| 47 | +import static org.elasticsearch.cluster.metadata.IndexMetadata.INDEX_REFRESH_BLOCK; |
40 | 48 | import static org.elasticsearch.cluster.routing.allocation.decider.ThrottlingAllocationDecider.CLUSTER_ROUTING_ALLOCATION_NODE_CONCURRENT_RECOVERIES_SETTING; |
41 | 49 | import static org.hamcrest.Matchers.equalTo; |
42 | 50 | import static org.hamcrest.Matchers.is; |
| 51 | +import static org.hamcrest.Matchers.notNullValue; |
43 | 52 | import static org.hamcrest.Matchers.sameInstance; |
44 | 53 |
|
45 | 54 | public class ShardStartedClusterStateTaskExecutorTests extends ESAllocationTestCase { |
@@ -479,6 +488,114 @@ public void testExpandsTimestampRangeForReplica() throws Exception { |
479 | 488 | assertThat(latestIndexMetadata.getEventIngestedRange(), sameInstance(IndexLongFieldRange.UNKNOWN)); |
480 | 489 | } |
481 | 490 |
|
| 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 | + |
482 | 599 | private ClusterState executeTasks(final ClusterState state, final List<StartedShardUpdateTask> tasks) throws Exception { |
483 | 600 | return ClusterStateTaskExecutorUtils.executeAndAssertSuccessful(state, executor, tasks); |
484 | 601 | } |
|
0 commit comments