|
11 | 11 |
|
12 | 12 | import org.elasticsearch.action.ActionListener; |
13 | 13 | import org.elasticsearch.action.ActionRequest; |
| 14 | +import org.elasticsearch.action.IndicesRequest; |
14 | 15 | import org.elasticsearch.action.admin.indices.stats.CommonStats; |
15 | 16 | import org.elasticsearch.action.admin.indices.stats.IndexStats; |
16 | 17 | import org.elasticsearch.action.admin.indices.stats.IndicesStatsAction; |
|
24 | 25 | import org.elasticsearch.client.internal.Client; |
25 | 26 | import org.elasticsearch.cluster.ClusterName; |
26 | 27 | import org.elasticsearch.cluster.ClusterState; |
| 28 | +import org.elasticsearch.cluster.block.ClusterBlocks; |
27 | 29 | import org.elasticsearch.cluster.metadata.AliasMetadata; |
28 | 30 | import org.elasticsearch.cluster.metadata.DataStream; |
29 | 31 | import org.elasticsearch.cluster.metadata.IndexMetadata; |
|
43 | 45 | import org.elasticsearch.common.settings.Settings; |
44 | 46 | import org.elasticsearch.common.unit.ByteSizeValue; |
45 | 47 | import org.elasticsearch.core.TimeValue; |
| 48 | +import org.elasticsearch.index.Index; |
46 | 49 | import org.elasticsearch.index.IndexMode; |
47 | 50 | import org.elasticsearch.index.IndexVersion; |
48 | 51 | import org.elasticsearch.index.cache.query.QueryCacheStats; |
@@ -578,6 +581,110 @@ public void testRolloverAliasToDataStreamFails() throws Exception { |
578 | 581 | assertThat(illegalStateException.getMessage(), containsString("Aliases to data streams cannot be rolled over.")); |
579 | 582 | } |
580 | 583 |
|
| 584 | + public void testCheckBlockForIndices() { |
| 585 | + final TransportRolloverAction transportRolloverAction = new TransportRolloverAction( |
| 586 | + mock(TransportService.class), |
| 587 | + mockClusterService, |
| 588 | + mockThreadPool, |
| 589 | + mockActionFilters, |
| 590 | + mockIndexNameExpressionResolver, |
| 591 | + rolloverService, |
| 592 | + mockClient, |
| 593 | + mockAllocationService, |
| 594 | + mockMetadataDataStreamService, |
| 595 | + dataStreamAutoShardingService |
| 596 | + ); |
| 597 | + final IndexMetadata.Builder indexMetadata1 = IndexMetadata.builder("my-index-1") |
| 598 | + .putAlias(AliasMetadata.builder("my-alias").writeIndex(true).build()) |
| 599 | + .settings(settings(IndexVersion.current())) |
| 600 | + .numberOfShards(1) |
| 601 | + .numberOfReplicas(1); |
| 602 | + final IndexMetadata indexMetadata2 = IndexMetadata.builder("my-index-2") |
| 603 | + .settings(settings(IndexVersion.current()).put(IndexMetadata.INDEX_READ_ONLY_SETTING.getKey(), true)) |
| 604 | + .numberOfShards(1) |
| 605 | + .numberOfReplicas(1) |
| 606 | + .build(); |
| 607 | + final ClusterState stateBefore = ClusterState.builder(ClusterName.DEFAULT) |
| 608 | + .metadata(Metadata.builder().put(indexMetadata1).put(indexMetadata2, false)) |
| 609 | + .blocks(ClusterBlocks.builder().addBlocks(indexMetadata2)) |
| 610 | + .build(); |
| 611 | + { |
| 612 | + RolloverRequest rolloverRequest = new RolloverRequest("my-alias", "my-new-index"); |
| 613 | + when(mockIndexNameExpressionResolver.concreteIndexNames(any(), any(), (IndicesRequest) any())).thenReturn( |
| 614 | + new String[] { "my-index-1" } |
| 615 | + ); |
| 616 | + assertNull(transportRolloverAction.checkBlock(rolloverRequest, stateBefore)); |
| 617 | + } |
| 618 | + { |
| 619 | + RolloverRequest rolloverRequest = new RolloverRequest("my-index-2", "my-new-index"); |
| 620 | + when(mockIndexNameExpressionResolver.concreteIndexNames(any(), any(), (IndicesRequest) any())).thenReturn( |
| 621 | + new String[] { "my-index-2" } |
| 622 | + ); |
| 623 | + assertNotNull(transportRolloverAction.checkBlock(rolloverRequest, stateBefore)); |
| 624 | + } |
| 625 | + } |
| 626 | + |
| 627 | + public void testCheckBlockForDataStreams() { |
| 628 | + final TransportRolloverAction transportRolloverAction = new TransportRolloverAction( |
| 629 | + mock(TransportService.class), |
| 630 | + mockClusterService, |
| 631 | + mockThreadPool, |
| 632 | + mockActionFilters, |
| 633 | + mockIndexNameExpressionResolver, |
| 634 | + rolloverService, |
| 635 | + mockClient, |
| 636 | + mockAllocationService, |
| 637 | + mockMetadataDataStreamService, |
| 638 | + dataStreamAutoShardingService |
| 639 | + ); |
| 640 | + { |
| 641 | + // First, make sure checkBlock returns null when there are no blocks |
| 642 | + final ClusterState clusterState = createDataStream(false, false); |
| 643 | + RolloverRequest rolloverRequest = new RolloverRequest("my-data-stream", null); |
| 644 | + assertNull(transportRolloverAction.checkBlock(rolloverRequest, clusterState)); |
| 645 | + } |
| 646 | + { |
| 647 | + // Make sure checkBlock returns null when indices other than the write index have blocks |
| 648 | + final ClusterState clusterState = createDataStream(false, true); |
| 649 | + RolloverRequest rolloverRequest = new RolloverRequest("my-data-stream", null); |
| 650 | + assertNull(transportRolloverAction.checkBlock(rolloverRequest, clusterState)); |
| 651 | + } |
| 652 | + { |
| 653 | + // Make sure checkBlock returns an exception when the write index has a block |
| 654 | + ClusterState clusterState = createDataStream(true, randomBoolean()); |
| 655 | + RolloverRequest rolloverRequest = new RolloverRequest("my-data-stream", null); |
| 656 | + assertNotNull(transportRolloverAction.checkBlock(rolloverRequest, clusterState)); |
| 657 | + } |
| 658 | + } |
| 659 | + |
| 660 | + private ClusterState createDataStream(boolean blockOnWriteIndex, boolean blocksOnNonWriteIndices) { |
| 661 | + ClusterState.Builder clusterStateBuilder = ClusterState.builder(ClusterName.DEFAULT); |
| 662 | + Metadata.Builder metadataBuilder = Metadata.builder(); |
| 663 | + List<Index> indices = new ArrayList<>(); |
| 664 | + int totalIndices = randomIntBetween(1, 20); |
| 665 | + for (int i = 0; i < totalIndices; i++) { |
| 666 | + Settings.Builder settingsBuilder = settings(IndexVersion.current()); |
| 667 | + if ((blockOnWriteIndex && i == totalIndices - 1) || (blocksOnNonWriteIndices && i != totalIndices - 1)) { |
| 668 | + settingsBuilder.put(IndexMetadata.INDEX_READ_ONLY_SETTING.getKey(), true); |
| 669 | + } |
| 670 | + final IndexMetadata backingIndexMetadata = IndexMetadata.builder(".ds-logs-ds-00000" + (i + 1)) |
| 671 | + .settings(settingsBuilder) |
| 672 | + .numberOfShards(1) |
| 673 | + .numberOfReplicas(1) |
| 674 | + .build(); |
| 675 | + metadataBuilder.put(backingIndexMetadata, false); |
| 676 | + indices.add(backingIndexMetadata.getIndex()); |
| 677 | + clusterStateBuilder.blocks(ClusterBlocks.builder().addBlocks(backingIndexMetadata)); |
| 678 | + } |
| 679 | + |
| 680 | + final DataStream dataStream = DataStream.builder("my-data-stream", indices) |
| 681 | + .setMetadata(Map.of()) |
| 682 | + .setIndexMode(IndexMode.STANDARD) |
| 683 | + .build(); |
| 684 | + metadataBuilder.put(dataStream); |
| 685 | + return clusterStateBuilder.metadata(metadataBuilder).build(); |
| 686 | + } |
| 687 | + |
581 | 688 | private IndicesStatsResponse createIndicesStatResponse(String indexName, long totalDocs, long primariesDocs) { |
582 | 689 | final CommonStats primaryStats = mock(CommonStats.class); |
583 | 690 | when(primaryStats.getDocs()).thenReturn(new DocsStats(primariesDocs, 0, between(1, 10000))); |
|
0 commit comments