Search in sources :

Example 26 with IndexNameExpressionResolver

use of org.opensearch.cluster.metadata.IndexNameExpressionResolver in project OpenSearch by opensearch-project.

the class TransportRolloverActionTests method testConditionEvaluationWhenAliasToWriteAndReadIndicesConsidersOnlyPrimariesFromWriteIndex.

public void testConditionEvaluationWhenAliasToWriteAndReadIndicesConsidersOnlyPrimariesFromWriteIndex() throws Exception {
    final TransportService mockTransportService = mock(TransportService.class);
    final ClusterService mockClusterService = mock(ClusterService.class);
    final DiscoveryNode mockNode = mock(DiscoveryNode.class);
    when(mockNode.getId()).thenReturn("mocknode");
    when(mockClusterService.localNode()).thenReturn(mockNode);
    final ThreadPool mockThreadPool = mock(ThreadPool.class);
    final MetadataCreateIndexService mockCreateIndexService = mock(MetadataCreateIndexService.class);
    final IndexNameExpressionResolver mockIndexNameExpressionResolver = mock(IndexNameExpressionResolver.class);
    when(mockIndexNameExpressionResolver.resolveDateMathExpression(any())).thenReturn("logs-index-000003");
    final ActionFilters mockActionFilters = mock(ActionFilters.class);
    final MetadataIndexAliasesService mdIndexAliasesService = mock(MetadataIndexAliasesService.class);
    final Client mockClient = mock(Client.class);
    final Map<String, IndexStats> indexStats = new HashMap<>();
    int total = randomIntBetween(500, 1000);
    indexStats.put("logs-index-000001", createIndexStats(200L, total));
    indexStats.put("logs-index-000002", createIndexStats(300L, total));
    final IndicesStatsResponse statsResponse = createAliasToMultipleIndicesStatsResponse(indexStats);
    doAnswer(invocation -> {
        Object[] args = invocation.getArguments();
        assert args.length == 3;
        ActionListener<IndicesStatsResponse> listener = (ActionListener<IndicesStatsResponse>) args[2];
        listener.onResponse(statsResponse);
        return null;
    }).when(mockClient).execute(eq(IndicesStatsAction.INSTANCE), any(ActionRequest.class), any(ActionListener.class));
    assert statsResponse.getPrimaries().getDocs().getCount() == 500L;
    assert statsResponse.getTotal().getDocs().getCount() == (total + total);
    final IndexMetadata.Builder indexMetadata = IndexMetadata.builder("logs-index-000001").putAlias(AliasMetadata.builder("logs-alias").writeIndex(false).build()).settings(settings(Version.CURRENT)).numberOfShards(1).numberOfReplicas(1);
    final IndexMetadata.Builder indexMetadata2 = IndexMetadata.builder("logs-index-000002").putAlias(AliasMetadata.builder("logs-alias").writeIndex(true).build()).settings(settings(Version.CURRENT)).numberOfShards(1).numberOfReplicas(1);
    final ClusterState stateBefore = ClusterState.builder(ClusterName.DEFAULT).metadata(Metadata.builder().put(indexMetadata).put(indexMetadata2)).build();
    when(mockCreateIndexService.applyCreateIndexRequest(any(), any(), anyBoolean())).thenReturn(stateBefore);
    when(mdIndexAliasesService.applyAliasActions(any(), any())).thenReturn(stateBefore);
    MetadataRolloverService rolloverService = new MetadataRolloverService(mockThreadPool, mockCreateIndexService, mdIndexAliasesService, mockIndexNameExpressionResolver);
    final TransportRolloverAction transportRolloverAction = new TransportRolloverAction(mockTransportService, mockClusterService, mockThreadPool, mockActionFilters, mockIndexNameExpressionResolver, rolloverService, mockClient);
    // For given alias, verify that condition evaluation fails when the condition doc count is greater than the primaries doc count
    // (primaries from only write index is considered)
    PlainActionFuture<RolloverResponse> future = new PlainActionFuture<>();
    RolloverRequest rolloverRequest = new RolloverRequest("logs-alias", "logs-index-000003");
    rolloverRequest.addMaxIndexDocsCondition(500L);
    rolloverRequest.dryRun(true);
    transportRolloverAction.masterOperation(mock(Task.class), rolloverRequest, stateBefore, future);
    RolloverResponse response = future.actionGet();
    assertThat(response.getOldIndex(), equalTo("logs-index-000002"));
    assertThat(response.getNewIndex(), equalTo("logs-index-000003"));
    assertThat(response.isDryRun(), equalTo(true));
    assertThat(response.isRolledOver(), equalTo(false));
    assertThat(response.getConditionStatus().size(), equalTo(1));
    assertThat(response.getConditionStatus().get("[max_docs: 500]"), is(false));
    // For given alias, verify that the condition evaluation is successful when condition doc count is less than the primaries doc count
    // (primaries from only write index is considered)
    future = new PlainActionFuture<>();
    rolloverRequest = new RolloverRequest("logs-alias", "logs-index-000003");
    rolloverRequest.addMaxIndexDocsCondition(300L);
    rolloverRequest.dryRun(true);
    transportRolloverAction.masterOperation(mock(Task.class), rolloverRequest, stateBefore, future);
    response = future.actionGet();
    assertThat(response.getOldIndex(), equalTo("logs-index-000002"));
    assertThat(response.getNewIndex(), equalTo("logs-index-000003"));
    assertThat(response.isDryRun(), equalTo(true));
    assertThat(response.isRolledOver(), equalTo(false));
    assertThat(response.getConditionStatus().size(), equalTo(1));
    assertThat(response.getConditionStatus().get("[max_docs: 300]"), is(true));
}
Also used : DiscoveryNode(org.opensearch.cluster.node.DiscoveryNode) Task(org.opensearch.tasks.Task) HashMap(java.util.HashMap) ThreadPool(org.opensearch.threadpool.ThreadPool) MetadataCreateIndexService(org.opensearch.cluster.metadata.MetadataCreateIndexService) Client(org.opensearch.client.Client) IndexMetadata(org.opensearch.cluster.metadata.IndexMetadata) IndicesStatsResponse(org.opensearch.action.admin.indices.stats.IndicesStatsResponse) ClusterState(org.opensearch.cluster.ClusterState) ActionFilters(org.opensearch.action.support.ActionFilters) ClusterService(org.opensearch.cluster.service.ClusterService) MetadataIndexAliasesService(org.opensearch.cluster.metadata.MetadataIndexAliasesService) ActionListener(org.opensearch.action.ActionListener) TransportService(org.opensearch.transport.TransportService) ActionRequest(org.opensearch.action.ActionRequest) PlainActionFuture(org.opensearch.action.support.PlainActionFuture) IndexNameExpressionResolver(org.opensearch.cluster.metadata.IndexNameExpressionResolver) IndexStats(org.opensearch.action.admin.indices.stats.IndexStats)

Example 27 with IndexNameExpressionResolver

use of org.opensearch.cluster.metadata.IndexNameExpressionResolver in project OpenSearch by opensearch-project.

the class PutMappingRequestTests method testResolveIndicesWithoutWriteIndexOnlyAndDataStreamsAndWriteAliases.

public void testResolveIndicesWithoutWriteIndexOnlyAndDataStreamsAndWriteAliases() {
    String[] dataStreamNames = { "foo", "bar", "baz" };
    List<Tuple<String, Integer>> dsMetadata = org.opensearch.common.collect.List.of(tuple(dataStreamNames[0], randomIntBetween(1, 3)), tuple(dataStreamNames[1], randomIntBetween(1, 3)), tuple(dataStreamNames[2], randomIntBetween(1, 3)));
    ClusterState cs = DeleteDataStreamRequestTests.getClusterStateWithDataStreams(dsMetadata, org.opensearch.common.collect.List.of("index1", "index2", "index3"));
    cs = addAliases(cs, org.opensearch.common.collect.List.of(tuple("alias1", org.opensearch.common.collect.List.of(tuple("index1", false), tuple("index2", true))), tuple("alias2", org.opensearch.common.collect.List.of(tuple("index2", false), tuple("index3", true)))));
    PutMappingRequest request = new PutMappingRequest().indices("foo", "alias1", "alias2");
    Index[] indices = TransportPutMappingAction.resolveIndices(cs, request, new IndexNameExpressionResolver(new ThreadContext(Settings.EMPTY)));
    List<String> indexNames = Arrays.stream(indices).map(Index::getName).collect(Collectors.toList());
    IndexAbstraction expectedDs = cs.metadata().getIndicesLookup().get("foo");
    List<String> expectedIndices = expectedDs.getIndices().stream().map(im -> im.getIndex().getName()).collect(Collectors.toList());
    expectedIndices.addAll(org.opensearch.common.collect.List.of("index1", "index2", "index3"));
    // should resolve the data stream and each alias to _all_ their respective indices
    assertThat(indexNames, containsInAnyOrder(expectedIndices.toArray()));
}
Also used : Arrays(java.util.Arrays) IndexAbstraction(org.opensearch.cluster.metadata.IndexAbstraction) Metadata(org.opensearch.cluster.metadata.Metadata) BytesReference(org.opensearch.common.bytes.BytesReference) IndexMetadata(org.opensearch.cluster.metadata.IndexMetadata) AliasMetadata(org.opensearch.cluster.metadata.AliasMetadata) ThreadContext(org.opensearch.common.util.concurrent.ThreadContext) ActionRequestValidationException(org.opensearch.action.ActionRequestValidationException) Strings(org.opensearch.common.Strings) XContentParser(org.opensearch.common.xcontent.XContentParser) ClusterState(org.opensearch.cluster.ClusterState) RandomCreateIndexGenerator(org.opensearch.index.RandomCreateIndexGenerator) DeleteDataStreamRequestTests(org.opensearch.action.admin.indices.datastream.DeleteDataStreamRequestTests) Index(org.opensearch.index.Index) OpenSearchTestCase(org.opensearch.test.OpenSearchTestCase) Settings(org.opensearch.common.settings.Settings) IOException(java.io.IOException) Tuple.tuple(org.opensearch.common.collect.Tuple.tuple) EMPTY_PARAMS(org.opensearch.common.xcontent.ToXContent.EMPTY_PARAMS) Collectors(java.util.stream.Collectors) Tuple(org.opensearch.common.collect.Tuple) XContentBuilder(org.opensearch.common.xcontent.XContentBuilder) List(java.util.List) Matchers.containsInAnyOrder(org.hamcrest.Matchers.containsInAnyOrder) JsonXContent(org.opensearch.common.xcontent.json.JsonXContent) XContentType(org.opensearch.common.xcontent.XContentType) Matchers.containsString(org.hamcrest.Matchers.containsString) IndexNameExpressionResolver(org.opensearch.cluster.metadata.IndexNameExpressionResolver) ClusterState(org.opensearch.cluster.ClusterState) ThreadContext(org.opensearch.common.util.concurrent.ThreadContext) Index(org.opensearch.index.Index) Matchers.containsString(org.hamcrest.Matchers.containsString) IndexAbstraction(org.opensearch.cluster.metadata.IndexAbstraction) IndexNameExpressionResolver(org.opensearch.cluster.metadata.IndexNameExpressionResolver) Tuple(org.opensearch.common.collect.Tuple)

Example 28 with IndexNameExpressionResolver

use of org.opensearch.cluster.metadata.IndexNameExpressionResolver in project OpenSearch by opensearch-project.

the class PutMappingRequestTests method testResolveIndicesWithWriteIndexOnlyAndNoSingleWriteIndex.

public void testResolveIndicesWithWriteIndexOnlyAndNoSingleWriteIndex() {
    String[] dataStreamNames = { "foo", "bar", "baz" };
    List<Tuple<String, Integer>> dsMetadata = org.opensearch.common.collect.List.of(tuple(dataStreamNames[0], randomIntBetween(1, 3)), tuple(dataStreamNames[1], randomIntBetween(1, 3)), tuple(dataStreamNames[2], randomIntBetween(1, 3)));
    ClusterState cs = DeleteDataStreamRequestTests.getClusterStateWithDataStreams(dsMetadata, org.opensearch.common.collect.List.of("index1", "index2", "index3"));
    final ClusterState cs2 = addAliases(cs, org.opensearch.common.collect.List.of(tuple("alias1", org.opensearch.common.collect.List.of(tuple("index1", false), tuple("index2", true))), tuple("alias2", org.opensearch.common.collect.List.of(tuple("index2", false), tuple("index3", true)))));
    PutMappingRequest request = new PutMappingRequest().indices("*").writeIndexOnly(true);
    IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> TransportPutMappingAction.resolveIndices(cs2, request, new IndexNameExpressionResolver(new ThreadContext(Settings.EMPTY))));
    assertThat(e.getMessage(), containsString("The index expression [*] and options provided did not point to a single write-index"));
}
Also used : ClusterState(org.opensearch.cluster.ClusterState) ThreadContext(org.opensearch.common.util.concurrent.ThreadContext) Matchers.containsString(org.hamcrest.Matchers.containsString) IndexNameExpressionResolver(org.opensearch.cluster.metadata.IndexNameExpressionResolver) Tuple(org.opensearch.common.collect.Tuple)

Example 29 with IndexNameExpressionResolver

use of org.opensearch.cluster.metadata.IndexNameExpressionResolver in project OpenSearch by opensearch-project.

the class PutMappingRequestTests method testResolveIndicesWithWriteIndexOnlyAndDataStreamsAndWriteAliases.

public void testResolveIndicesWithWriteIndexOnlyAndDataStreamsAndWriteAliases() {
    String[] dataStreamNames = { "foo", "bar", "baz" };
    List<Tuple<String, Integer>> dsMetadata = org.opensearch.common.collect.List.of(tuple(dataStreamNames[0], randomIntBetween(1, 3)), tuple(dataStreamNames[1], randomIntBetween(1, 3)), tuple(dataStreamNames[2], randomIntBetween(1, 3)));
    ClusterState cs = DeleteDataStreamRequestTests.getClusterStateWithDataStreams(dsMetadata, org.opensearch.common.collect.List.of("index1", "index2", "index3"));
    cs = addAliases(cs, org.opensearch.common.collect.List.of(tuple("alias1", org.opensearch.common.collect.List.of(tuple("index1", false), tuple("index2", true))), tuple("alias2", org.opensearch.common.collect.List.of(tuple("index2", false), tuple("index3", true)))));
    PutMappingRequest request = new PutMappingRequest().indices("foo", "alias1", "alias2").writeIndexOnly(true);
    Index[] indices = TransportPutMappingAction.resolveIndices(cs, request, new IndexNameExpressionResolver(new ThreadContext(Settings.EMPTY)));
    List<String> indexNames = Arrays.stream(indices).map(Index::getName).collect(Collectors.toList());
    IndexAbstraction expectedDs = cs.metadata().getIndicesLookup().get("foo");
    // should resolve the data stream and each alias to their respective write indices
    assertThat(indexNames, containsInAnyOrder(expectedDs.getWriteIndex().getIndex().getName(), "index2", "index3"));
}
Also used : IndexAbstraction(org.opensearch.cluster.metadata.IndexAbstraction) ClusterState(org.opensearch.cluster.ClusterState) ThreadContext(org.opensearch.common.util.concurrent.ThreadContext) Index(org.opensearch.index.Index) Matchers.containsString(org.hamcrest.Matchers.containsString) IndexNameExpressionResolver(org.opensearch.cluster.metadata.IndexNameExpressionResolver) Tuple(org.opensearch.common.collect.Tuple)

Example 30 with IndexNameExpressionResolver

use of org.opensearch.cluster.metadata.IndexNameExpressionResolver in project OpenSearch by opensearch-project.

the class PutMappingRequestTests method testResolveIndicesWithWriteIndexOnlyAndAliasWithoutWriteIndex.

public void testResolveIndicesWithWriteIndexOnlyAndAliasWithoutWriteIndex() {
    String[] dataStreamNames = { "foo", "bar", "baz" };
    List<Tuple<String, Integer>> dsMetadata = org.opensearch.common.collect.List.of(tuple(dataStreamNames[0], randomIntBetween(1, 3)), tuple(dataStreamNames[1], randomIntBetween(1, 3)), tuple(dataStreamNames[2], randomIntBetween(1, 3)));
    ClusterState cs = DeleteDataStreamRequestTests.getClusterStateWithDataStreams(dsMetadata, org.opensearch.common.collect.List.of("index1", "index2", "index3"));
    final ClusterState cs2 = addAliases(cs, org.opensearch.common.collect.List.of(tuple("alias1", org.opensearch.common.collect.List.of(tuple("index1", false), tuple("index2", false))), tuple("alias2", org.opensearch.common.collect.List.of(tuple("index2", false), tuple("index3", false)))));
    PutMappingRequest request = new PutMappingRequest().indices("alias2").writeIndexOnly(true);
    IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> TransportPutMappingAction.resolveIndices(cs2, request, new IndexNameExpressionResolver(new ThreadContext(Settings.EMPTY))));
    assertThat(e.getMessage(), containsString("no write index is defined for alias [alias2]"));
}
Also used : ClusterState(org.opensearch.cluster.ClusterState) ThreadContext(org.opensearch.common.util.concurrent.ThreadContext) Matchers.containsString(org.hamcrest.Matchers.containsString) IndexNameExpressionResolver(org.opensearch.cluster.metadata.IndexNameExpressionResolver) Tuple(org.opensearch.common.collect.Tuple)

Aggregations

IndexNameExpressionResolver (org.opensearch.cluster.metadata.IndexNameExpressionResolver)39 ThreadContext (org.opensearch.common.util.concurrent.ThreadContext)26 ClusterState (org.opensearch.cluster.ClusterState)20 Matchers.containsString (org.hamcrest.Matchers.containsString)18 Settings (org.opensearch.common.settings.Settings)15 ThreadPool (org.opensearch.threadpool.ThreadPool)14 ClusterService (org.opensearch.cluster.service.ClusterService)11 Metadata (org.opensearch.cluster.metadata.Metadata)10 Tuple (org.opensearch.common.collect.Tuple)10 IOException (java.io.IOException)9 Before (org.junit.Before)9 ClusterName (org.opensearch.cluster.ClusterName)9 IndexMetadata (org.opensearch.cluster.metadata.IndexMetadata)9 TestThreadPool (org.opensearch.threadpool.TestThreadPool)8 ActionListener (org.opensearch.action.ActionListener)7 ActionFilters (org.opensearch.action.support.ActionFilters)7 ClusterSettings (org.opensearch.common.settings.ClusterSettings)7 Index (org.opensearch.index.Index)7 TransportService (org.opensearch.transport.TransportService)7 HashSet (java.util.HashSet)5