Search in sources :

Example 1 with MetadataCreateIndexService

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

the class MetadataRolloverServiceTests method testRolloverClusterStateForDataStreamNoTemplate.

public void testRolloverClusterStateForDataStreamNoTemplate() throws Exception {
    final DataStream dataStream = DataStreamTests.randomInstance();
    Metadata.Builder builder = Metadata.builder();
    for (Index index : dataStream.getIndices()) {
        builder.put(DataStreamTestHelper.getIndexMetadataBuilderForIndex(index));
    }
    builder.put(dataStream);
    final ClusterState clusterState = ClusterState.builder(new ClusterName("test")).metadata(builder).build();
    ThreadPool testThreadPool = mock(ThreadPool.class);
    ClusterService clusterService = ClusterServiceUtils.createClusterService(testThreadPool);
    Environment env = mock(Environment.class);
    AllocationService allocationService = mock(AllocationService.class);
    IndicesService indicesService = mockIndicesServices();
    IndexNameExpressionResolver mockIndexNameExpressionResolver = mock(IndexNameExpressionResolver.class);
    MetadataCreateIndexService createIndexService = new MetadataCreateIndexService(Settings.EMPTY, clusterService, indicesService, allocationService, null, null, env, null, testThreadPool, null, new SystemIndices(emptyMap()), false);
    MetadataIndexAliasesService indexAliasesService = new MetadataIndexAliasesService(clusterService, indicesService, new AliasValidator(), null, xContentRegistry());
    MetadataRolloverService rolloverService = new MetadataRolloverService(testThreadPool, createIndexService, indexAliasesService, mockIndexNameExpressionResolver);
    MaxDocsCondition condition = new MaxDocsCondition(randomNonNegativeLong());
    List<Condition<?>> metConditions = Collections.singletonList(condition);
    CreateIndexRequest createIndexRequest = new CreateIndexRequest("_na_");
    Exception e = expectThrows(IllegalArgumentException.class, () -> rolloverService.rolloverClusterState(clusterState, dataStream.getName(), null, createIndexRequest, metConditions, false, randomBoolean()));
    assertThat(e.getMessage(), equalTo("no matching index template found for data stream [" + dataStream.getName() + "]"));
}
Also used : ClusterState(org.opensearch.cluster.ClusterState) DataStream(org.opensearch.cluster.metadata.DataStream) AliasValidator(org.opensearch.cluster.metadata.AliasValidator) Metadata(org.opensearch.cluster.metadata.Metadata) IndexMetadata(org.opensearch.cluster.metadata.IndexMetadata) AliasMetadata(org.opensearch.cluster.metadata.AliasMetadata) IndexTemplateMetadata(org.opensearch.cluster.metadata.IndexTemplateMetadata) TestThreadPool(org.opensearch.threadpool.TestThreadPool) ThreadPool(org.opensearch.threadpool.ThreadPool) IndicesService(org.opensearch.indices.IndicesService) Index(org.opensearch.index.Index) InvalidIndexNameException(org.opensearch.indices.InvalidIndexNameException) IOException(java.io.IOException) MetadataCreateIndexService(org.opensearch.cluster.metadata.MetadataCreateIndexService) ClusterService(org.opensearch.cluster.service.ClusterService) MetadataIndexAliasesService(org.opensearch.cluster.metadata.MetadataIndexAliasesService) ClusterName(org.opensearch.cluster.ClusterName) Environment(org.opensearch.env.Environment) IndexNameExpressionResolver(org.opensearch.cluster.metadata.IndexNameExpressionResolver) SystemIndices(org.opensearch.indices.SystemIndices) CreateIndexRequest(org.opensearch.action.admin.indices.create.CreateIndexRequest) AllocationService(org.opensearch.cluster.routing.allocation.AllocationService)

Example 2 with MetadataCreateIndexService

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

the class MetadataRolloverServiceTests method testRolloverClusterState.

/**
 * Test the main rolloverClusterState method. This does not validate every detail to depth, rather focuses on observing that each
 * parameter is used for the purpose intended.
 */
public void testRolloverClusterState() throws Exception {
    final String aliasName = "logs-alias";
    final String indexPrefix = "logs-index-00000";
    String sourceIndexName = indexPrefix + "1";
    final IndexMetadata.Builder indexMetadata = IndexMetadata.builder(sourceIndexName).putAlias(AliasMetadata.builder(aliasName).writeIndex(true).build()).settings(settings(Version.CURRENT)).numberOfShards(1).numberOfReplicas(1);
    final ClusterState clusterState = ClusterState.builder(new ClusterName("test")).metadata(Metadata.builder().put(indexMetadata)).build();
    ThreadPool testThreadPool = new TestThreadPool(getTestName());
    try {
        ClusterService clusterService = ClusterServiceUtils.createClusterService(testThreadPool);
        Environment env = mock(Environment.class);
        when(env.sharedDataFile()).thenReturn(null);
        AllocationService allocationService = mock(AllocationService.class);
        when(allocationService.reroute(any(ClusterState.class), any(String.class))).then(i -> i.getArguments()[0]);
        IndicesService indicesService = mockIndicesServices();
        IndexNameExpressionResolver mockIndexNameExpressionResolver = mock(IndexNameExpressionResolver.class);
        when(mockIndexNameExpressionResolver.resolveDateMathExpression(any())).then(returnsFirstArg());
        ShardLimitValidator shardLimitValidator = new ShardLimitValidator(Settings.EMPTY, clusterService);
        MetadataCreateIndexService createIndexService = new MetadataCreateIndexService(Settings.EMPTY, clusterService, indicesService, allocationService, null, shardLimitValidator, env, IndexScopedSettings.DEFAULT_SCOPED_SETTINGS, testThreadPool, null, new SystemIndices(emptyMap()), false);
        MetadataIndexAliasesService indexAliasesService = new MetadataIndexAliasesService(clusterService, indicesService, new AliasValidator(), null, xContentRegistry());
        MetadataRolloverService rolloverService = new MetadataRolloverService(testThreadPool, createIndexService, indexAliasesService, mockIndexNameExpressionResolver);
        MaxDocsCondition condition = new MaxDocsCondition(randomNonNegativeLong());
        List<Condition<?>> metConditions = Collections.singletonList(condition);
        String newIndexName = randomBoolean() ? "logs-index-9" : null;
        int numberOfShards = randomIntBetween(1, 5);
        CreateIndexRequest createIndexRequest = new CreateIndexRequest("_na_");
        createIndexRequest.settings(Settings.builder().put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, numberOfShards));
        long before = testThreadPool.absoluteTimeInMillis();
        MetadataRolloverService.RolloverResult rolloverResult = rolloverService.rolloverClusterState(clusterState, aliasName, newIndexName, createIndexRequest, metConditions, randomBoolean(), false);
        long after = testThreadPool.absoluteTimeInMillis();
        newIndexName = newIndexName == null ? indexPrefix + "2" : newIndexName;
        assertEquals(sourceIndexName, rolloverResult.sourceIndexName);
        assertEquals(newIndexName, rolloverResult.rolloverIndexName);
        Metadata rolloverMetadata = rolloverResult.clusterState.metadata();
        assertEquals(2, rolloverMetadata.indices().size());
        IndexMetadata rolloverIndexMetadata = rolloverMetadata.index(newIndexName);
        assertThat(rolloverIndexMetadata.getNumberOfShards(), equalTo(numberOfShards));
        IndexAbstraction alias = rolloverMetadata.getIndicesLookup().get(aliasName);
        assertThat(alias.getType(), equalTo(IndexAbstraction.Type.ALIAS));
        assertThat(alias.getIndices(), hasSize(2));
        assertThat(alias.getIndices(), hasItem(rolloverMetadata.index(sourceIndexName)));
        assertThat(alias.getIndices(), hasItem(rolloverIndexMetadata));
        assertThat(alias.getWriteIndex(), equalTo(rolloverIndexMetadata));
        RolloverInfo info = rolloverMetadata.index(sourceIndexName).getRolloverInfos().get(aliasName);
        assertThat(info.getTime(), lessThanOrEqualTo(after));
        assertThat(info.getTime(), greaterThanOrEqualTo(before));
        assertThat(info.getMetConditions(), hasSize(1));
        assertThat(info.getMetConditions().get(0).value(), equalTo(condition.value()));
    } finally {
        testThreadPool.shutdown();
    }
}
Also used : TestThreadPool(org.opensearch.threadpool.TestThreadPool) ThreadPool(org.opensearch.threadpool.ThreadPool) Metadata(org.opensearch.cluster.metadata.Metadata) IndexMetadata(org.opensearch.cluster.metadata.IndexMetadata) AliasMetadata(org.opensearch.cluster.metadata.AliasMetadata) IndexTemplateMetadata(org.opensearch.cluster.metadata.IndexTemplateMetadata) Matchers.containsString(org.hamcrest.Matchers.containsString) TestThreadPool(org.opensearch.threadpool.TestThreadPool) MetadataCreateIndexService(org.opensearch.cluster.metadata.MetadataCreateIndexService) ShardLimitValidator(org.opensearch.indices.ShardLimitValidator) ClusterName(org.opensearch.cluster.ClusterName) IndexMetadata(org.opensearch.cluster.metadata.IndexMetadata) AllocationService(org.opensearch.cluster.routing.allocation.AllocationService) ClusterState(org.opensearch.cluster.ClusterState) AliasValidator(org.opensearch.cluster.metadata.AliasValidator) IndicesService(org.opensearch.indices.IndicesService) IndexAbstraction(org.opensearch.cluster.metadata.IndexAbstraction) ClusterService(org.opensearch.cluster.service.ClusterService) MetadataIndexAliasesService(org.opensearch.cluster.metadata.MetadataIndexAliasesService) Environment(org.opensearch.env.Environment) IndexNameExpressionResolver(org.opensearch.cluster.metadata.IndexNameExpressionResolver) SystemIndices(org.opensearch.indices.SystemIndices) CreateIndexRequest(org.opensearch.action.admin.indices.create.CreateIndexRequest)

Example 3 with MetadataCreateIndexService

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

the class MetadataRolloverServiceTests method testValidation.

public void testValidation() throws Exception {
    final String rolloverTarget;
    final String sourceIndexName;
    final String defaultRolloverIndexName;
    final boolean useDataStream = randomBoolean();
    final Metadata.Builder builder = Metadata.builder();
    if (useDataStream) {
        DataStream dataStream = DataStreamTests.randomInstance();
        rolloverTarget = dataStream.getName();
        sourceIndexName = dataStream.getIndices().get(dataStream.getIndices().size() - 1).getName();
        defaultRolloverIndexName = DataStream.getDefaultBackingIndexName(dataStream.getName(), dataStream.getGeneration() + 1);
        ComposableIndexTemplate template = new ComposableIndexTemplate(Collections.singletonList(dataStream.getName() + "*"), null, null, null, null, null, new ComposableIndexTemplate.DataStreamTemplate());
        builder.put("template", template);
        for (Index index : dataStream.getIndices()) {
            builder.put(DataStreamTestHelper.getIndexMetadataBuilderForIndex(index));
        }
        builder.put(dataStream);
    } else {
        String indexPrefix = "logs-index-00000";
        rolloverTarget = "logs-alias";
        sourceIndexName = indexPrefix + "1";
        defaultRolloverIndexName = indexPrefix + "2";
        final IndexMetadata.Builder indexMetadata = IndexMetadata.builder(sourceIndexName).putAlias(AliasMetadata.builder(rolloverTarget).writeIndex(true).build()).settings(settings(Version.CURRENT)).numberOfShards(1).numberOfReplicas(1);
        builder.put(indexMetadata);
    }
    final ClusterState clusterState = ClusterState.builder(new ClusterName("test")).metadata(builder).build();
    MetadataCreateIndexService createIndexService = mock(MetadataCreateIndexService.class);
    MetadataIndexAliasesService metadataIndexAliasesService = mock(MetadataIndexAliasesService.class);
    IndexNameExpressionResolver mockIndexNameExpressionResolver = mock(IndexNameExpressionResolver.class);
    when(mockIndexNameExpressionResolver.resolveDateMathExpression(any())).then(returnsFirstArg());
    MetadataRolloverService rolloverService = new MetadataRolloverService(null, createIndexService, metadataIndexAliasesService, mockIndexNameExpressionResolver);
    String newIndexName = useDataStream == false && randomBoolean() ? "logs-index-9" : null;
    MetadataRolloverService.RolloverResult rolloverResult = rolloverService.rolloverClusterState(clusterState, rolloverTarget, newIndexName, new CreateIndexRequest("_na_"), null, randomBoolean(), true);
    newIndexName = newIndexName == null ? defaultRolloverIndexName : newIndexName;
    assertEquals(sourceIndexName, rolloverResult.sourceIndexName);
    assertEquals(newIndexName, rolloverResult.rolloverIndexName);
    assertSame(rolloverResult.clusterState, clusterState);
    verify(createIndexService).validateIndexName(any(), same(clusterState));
    verifyNoMoreInteractions(createIndexService);
    verifyNoMoreInteractions(metadataIndexAliasesService);
    reset(createIndexService);
    doThrow(new InvalidIndexNameException("test", "invalid")).when(createIndexService).validateIndexName(any(), any());
    expectThrows(InvalidIndexNameException.class, () -> rolloverService.rolloverClusterState(clusterState, rolloverTarget, null, new CreateIndexRequest("_na_"), null, randomBoolean(), randomBoolean()));
    verify(createIndexService).validateIndexName(any(), same(clusterState));
    verifyNoMoreInteractions(createIndexService);
    verifyNoMoreInteractions(metadataIndexAliasesService);
}
Also used : ComposableIndexTemplate(org.opensearch.cluster.metadata.ComposableIndexTemplate) ClusterState(org.opensearch.cluster.ClusterState) DataStream(org.opensearch.cluster.metadata.DataStream) Metadata(org.opensearch.cluster.metadata.Metadata) IndexMetadata(org.opensearch.cluster.metadata.IndexMetadata) AliasMetadata(org.opensearch.cluster.metadata.AliasMetadata) IndexTemplateMetadata(org.opensearch.cluster.metadata.IndexTemplateMetadata) InvalidIndexNameException(org.opensearch.indices.InvalidIndexNameException) Index(org.opensearch.index.Index) Matchers.containsString(org.hamcrest.Matchers.containsString) MetadataCreateIndexService(org.opensearch.cluster.metadata.MetadataCreateIndexService) MetadataIndexAliasesService(org.opensearch.cluster.metadata.MetadataIndexAliasesService) ClusterName(org.opensearch.cluster.ClusterName) IndexNameExpressionResolver(org.opensearch.cluster.metadata.IndexNameExpressionResolver) IndexMetadata(org.opensearch.cluster.metadata.IndexMetadata) CreateIndexRequest(org.opensearch.action.admin.indices.create.CreateIndexRequest)

Example 4 with MetadataCreateIndexService

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

the class MetadataRolloverServiceTests method testRolloverClusterStateForDataStream.

public void testRolloverClusterStateForDataStream() throws Exception {
    final DataStream dataStream = DataStreamTests.randomInstance();
    ComposableIndexTemplate template = new ComposableIndexTemplate(Collections.singletonList(dataStream.getName() + "*"), null, null, null, null, null, new ComposableIndexTemplate.DataStreamTemplate());
    Metadata.Builder builder = Metadata.builder();
    builder.put("template", template);
    for (Index index : dataStream.getIndices()) {
        builder.put(DataStreamTestHelper.getIndexMetadataBuilderForIndex(index));
    }
    builder.put(dataStream);
    final ClusterState clusterState = ClusterState.builder(new ClusterName("test")).metadata(builder).build();
    ThreadPool testThreadPool = new TestThreadPool(getTestName());
    try {
        Mapper.BuilderContext builderContext = new Mapper.BuilderContext(Settings.EMPTY, new ContentPath(0));
        DateFieldMapper dateFieldMapper = new DateFieldMapper.Builder("@timestamp", DateFieldMapper.Resolution.MILLISECONDS, null, false, Version.CURRENT).build(builderContext);
        MetadataFieldMapper mockedTimestampField = mock(MetadataFieldMapper.class);
        when(mockedTimestampField.name()).thenReturn("_data_stream_timestamp");
        MappedFieldType mockedTimestampFieldType = mock(MappedFieldType.class);
        when(mockedTimestampFieldType.name()).thenReturn("_data_stream_timestamp");
        when(mockedTimestampField.fieldType()).thenReturn(mockedTimestampFieldType);
        when(mockedTimestampField.copyTo()).thenReturn(FieldMapper.CopyTo.empty());
        when(mockedTimestampField.multiFields()).thenReturn(FieldMapper.MultiFields.empty());
        MappingLookup mappingLookup = new MappingLookup(Arrays.asList(mockedTimestampField, dateFieldMapper), Collections.emptyList(), Collections.emptyList(), 0, new StandardAnalyzer());
        ClusterService clusterService = ClusterServiceUtils.createClusterService(testThreadPool);
        Environment env = mock(Environment.class);
        when(env.sharedDataFile()).thenReturn(null);
        AllocationService allocationService = mock(AllocationService.class);
        when(allocationService.reroute(any(ClusterState.class), any(String.class))).then(i -> i.getArguments()[0]);
        DocumentMapper documentMapper = mock(DocumentMapper.class);
        when(documentMapper.mappers()).thenReturn(mappingLookup);
        when(documentMapper.type()).thenReturn("_doc");
        CompressedXContent mapping = new CompressedXContent("{\"_doc\":" + generateMapping(dataStream.getTimeStampField().getName(), "date") + "}");
        when(documentMapper.mappingSource()).thenReturn(mapping);
        RoutingFieldMapper routingFieldMapper = mock(RoutingFieldMapper.class);
        when(routingFieldMapper.required()).thenReturn(false);
        when(documentMapper.routingFieldMapper()).thenReturn(routingFieldMapper);
        IndicesService indicesService = mockIndicesServices(documentMapper);
        IndexNameExpressionResolver mockIndexNameExpressionResolver = mock(IndexNameExpressionResolver.class);
        when(mockIndexNameExpressionResolver.resolveDateMathExpression(any())).then(returnsFirstArg());
        ShardLimitValidator shardLimitValidator = new ShardLimitValidator(Settings.EMPTY, clusterService);
        MetadataCreateIndexService createIndexService = new MetadataCreateIndexService(Settings.EMPTY, clusterService, indicesService, allocationService, null, shardLimitValidator, env, IndexScopedSettings.DEFAULT_SCOPED_SETTINGS, testThreadPool, null, new SystemIndices(emptyMap()), false);
        MetadataIndexAliasesService indexAliasesService = new MetadataIndexAliasesService(clusterService, indicesService, new AliasValidator(), null, xContentRegistry());
        MetadataRolloverService rolloverService = new MetadataRolloverService(testThreadPool, createIndexService, indexAliasesService, mockIndexNameExpressionResolver);
        MaxDocsCondition condition = new MaxDocsCondition(randomNonNegativeLong());
        List<Condition<?>> metConditions = Collections.singletonList(condition);
        CreateIndexRequest createIndexRequest = new CreateIndexRequest("_na_");
        long before = testThreadPool.absoluteTimeInMillis();
        MetadataRolloverService.RolloverResult rolloverResult = rolloverService.rolloverClusterState(clusterState, dataStream.getName(), null, createIndexRequest, metConditions, randomBoolean(), false);
        long after = testThreadPool.absoluteTimeInMillis();
        String sourceIndexName = DataStream.getDefaultBackingIndexName(dataStream.getName(), dataStream.getGeneration());
        String newIndexName = DataStream.getDefaultBackingIndexName(dataStream.getName(), dataStream.getGeneration() + 1);
        assertEquals(sourceIndexName, rolloverResult.sourceIndexName);
        assertEquals(newIndexName, rolloverResult.rolloverIndexName);
        Metadata rolloverMetadata = rolloverResult.clusterState.metadata();
        assertEquals(dataStream.getIndices().size() + 1, rolloverMetadata.indices().size());
        IndexMetadata rolloverIndexMetadata = rolloverMetadata.index(newIndexName);
        IndexAbstraction ds = rolloverMetadata.getIndicesLookup().get(dataStream.getName());
        assertThat(ds.getType(), equalTo(IndexAbstraction.Type.DATA_STREAM));
        assertThat(ds.getIndices(), hasSize(dataStream.getIndices().size() + 1));
        assertThat(ds.getIndices(), hasItem(rolloverMetadata.index(sourceIndexName)));
        assertThat(ds.getIndices(), hasItem(rolloverIndexMetadata));
        assertThat(ds.getWriteIndex(), equalTo(rolloverIndexMetadata));
        RolloverInfo info = rolloverMetadata.index(sourceIndexName).getRolloverInfos().get(dataStream.getName());
        assertThat(info.getTime(), lessThanOrEqualTo(after));
        assertThat(info.getTime(), greaterThanOrEqualTo(before));
        assertThat(info.getMetConditions(), hasSize(1));
        assertThat(info.getMetConditions().get(0).value(), equalTo(condition.value()));
    } finally {
        testThreadPool.shutdown();
    }
}
Also used : ComposableIndexTemplate(org.opensearch.cluster.metadata.ComposableIndexTemplate) DataStream(org.opensearch.cluster.metadata.DataStream) Metadata(org.opensearch.cluster.metadata.Metadata) IndexMetadata(org.opensearch.cluster.metadata.IndexMetadata) AliasMetadata(org.opensearch.cluster.metadata.AliasMetadata) IndexTemplateMetadata(org.opensearch.cluster.metadata.IndexTemplateMetadata) TestThreadPool(org.opensearch.threadpool.TestThreadPool) ThreadPool(org.opensearch.threadpool.ThreadPool) Index(org.opensearch.index.Index) Matchers.containsString(org.hamcrest.Matchers.containsString) TestThreadPool(org.opensearch.threadpool.TestThreadPool) MetadataFieldMapper(org.opensearch.index.mapper.MetadataFieldMapper) RoutingFieldMapper(org.opensearch.index.mapper.RoutingFieldMapper) FieldMapper(org.opensearch.index.mapper.FieldMapper) Mapper(org.opensearch.index.mapper.Mapper) DocumentMapper(org.opensearch.index.mapper.DocumentMapper) DateFieldMapper(org.opensearch.index.mapper.DateFieldMapper) MetadataCreateIndexService(org.opensearch.cluster.metadata.MetadataCreateIndexService) MappingLookup(org.opensearch.index.mapper.MappingLookup) ShardLimitValidator(org.opensearch.indices.ShardLimitValidator) MappedFieldType(org.opensearch.index.mapper.MappedFieldType) CompressedXContent(org.opensearch.common.compress.CompressedXContent) ClusterName(org.opensearch.cluster.ClusterName) IndexMetadata(org.opensearch.cluster.metadata.IndexMetadata) AllocationService(org.opensearch.cluster.routing.allocation.AllocationService) DateFieldMapper(org.opensearch.index.mapper.DateFieldMapper) ClusterState(org.opensearch.cluster.ClusterState) DocumentMapper(org.opensearch.index.mapper.DocumentMapper) AliasValidator(org.opensearch.cluster.metadata.AliasValidator) RoutingFieldMapper(org.opensearch.index.mapper.RoutingFieldMapper) IndicesService(org.opensearch.indices.IndicesService) ContentPath(org.opensearch.index.mapper.ContentPath) IndexAbstraction(org.opensearch.cluster.metadata.IndexAbstraction) MetadataFieldMapper(org.opensearch.index.mapper.MetadataFieldMapper) ClusterService(org.opensearch.cluster.service.ClusterService) MetadataIndexAliasesService(org.opensearch.cluster.metadata.MetadataIndexAliasesService) StandardAnalyzer(org.apache.lucene.analysis.standard.StandardAnalyzer) Environment(org.opensearch.env.Environment) IndexNameExpressionResolver(org.opensearch.cluster.metadata.IndexNameExpressionResolver) SystemIndices(org.opensearch.indices.SystemIndices) CreateIndexRequest(org.opensearch.action.admin.indices.create.CreateIndexRequest)

Example 5 with MetadataCreateIndexService

use of org.opensearch.cluster.metadata.MetadataCreateIndexService 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)

Aggregations

ClusterState (org.opensearch.cluster.ClusterState)5 IndexMetadata (org.opensearch.cluster.metadata.IndexMetadata)5 IndexNameExpressionResolver (org.opensearch.cluster.metadata.IndexNameExpressionResolver)5 MetadataCreateIndexService (org.opensearch.cluster.metadata.MetadataCreateIndexService)5 MetadataIndexAliasesService (org.opensearch.cluster.metadata.MetadataIndexAliasesService)5 CreateIndexRequest (org.opensearch.action.admin.indices.create.CreateIndexRequest)4 ClusterName (org.opensearch.cluster.ClusterName)4 AliasMetadata (org.opensearch.cluster.metadata.AliasMetadata)4 IndexTemplateMetadata (org.opensearch.cluster.metadata.IndexTemplateMetadata)4 Metadata (org.opensearch.cluster.metadata.Metadata)4 ClusterService (org.opensearch.cluster.service.ClusterService)4 ThreadPool (org.opensearch.threadpool.ThreadPool)4 Matchers.containsString (org.hamcrest.Matchers.containsString)3 AliasValidator (org.opensearch.cluster.metadata.AliasValidator)3 DataStream (org.opensearch.cluster.metadata.DataStream)3 AllocationService (org.opensearch.cluster.routing.allocation.AllocationService)3 Environment (org.opensearch.env.Environment)3 Index (org.opensearch.index.Index)3 IndicesService (org.opensearch.indices.IndicesService)3 SystemIndices (org.opensearch.indices.SystemIndices)3