Search in sources :

Example 1 with IndexTemplateMetadata

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

the class GatewayMetaState method upgradeMetadata.

/**
 * This method calls {@link MetadataIndexUpgradeService} to makes sure that indices are compatible with the current
 * version. The MetadataIndexUpgradeService might also update obsolete settings if needed.
 *
 * @return input <code>metadata</code> if no upgrade is needed or an upgraded metadata
 */
static Metadata upgradeMetadata(Metadata metadata, MetadataIndexUpgradeService metadataIndexUpgradeService, MetadataUpgrader metadataUpgrader) {
    // upgrade index meta data
    boolean changed = false;
    final Metadata.Builder upgradedMetadata = Metadata.builder(metadata);
    for (IndexMetadata indexMetadata : metadata) {
        IndexMetadata newMetadata = metadataIndexUpgradeService.upgradeIndexMetadata(indexMetadata, Version.CURRENT.minimumIndexCompatibilityVersion());
        changed |= indexMetadata != newMetadata;
        upgradedMetadata.put(newMetadata, false);
    }
    // upgrade current templates
    if (applyPluginUpgraders(metadata.getTemplates(), metadataUpgrader.indexTemplateMetadataUpgraders, upgradedMetadata::removeTemplate, (s, indexTemplateMetadata) -> upgradedMetadata.put(indexTemplateMetadata))) {
        changed = true;
    }
    return changed ? upgradedMetadata.build() : metadata;
}
Also used : Metadata(org.opensearch.cluster.metadata.Metadata) NodeMetadata(org.opensearch.env.NodeMetadata) IndexMetadata(org.opensearch.cluster.metadata.IndexMetadata) CoordinationMetadata(org.opensearch.cluster.coordination.CoordinationMetadata) IndexTemplateMetadata(org.opensearch.cluster.metadata.IndexTemplateMetadata) IndexMetadata(org.opensearch.cluster.metadata.IndexMetadata)

Example 2 with IndexTemplateMetadata

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

the class GetIndexTemplatesResponse method toXContent.

@Override
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
    params = new ToXContent.DelegatingMapParams(singletonMap("reduce_mappings", "true"), params);
    builder.startObject();
    for (IndexTemplateMetadata indexTemplateMetadata : getIndexTemplates()) {
        IndexTemplateMetadata.Builder.toXContent(indexTemplateMetadata, builder, params);
    }
    builder.endObject();
    return builder;
}
Also used : ToXContent(org.opensearch.common.xcontent.ToXContent) IndexTemplateMetadata(org.opensearch.cluster.metadata.IndexTemplateMetadata)

Example 3 with IndexTemplateMetadata

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

the class MetadataRolloverService method checkNoDuplicatedAliasInIndexTemplate.

/**
 * If the newly created index matches with an index template whose aliases contains the rollover alias,
 * the rollover alias will point to multiple indices. This causes indexing requests to be rejected.
 * To avoid this, we make sure that there is no duplicated alias in index templates before creating a new index.
 */
static void checkNoDuplicatedAliasInIndexTemplate(Metadata metadata, String rolloverIndexName, String rolloverRequestAlias, @Nullable Boolean isHidden) {
    final List<IndexTemplateMetadata> matchedTemplates = findV1Templates(metadata, rolloverIndexName, isHidden);
    for (IndexTemplateMetadata template : matchedTemplates) {
        if (template.aliases().containsKey(rolloverRequestAlias)) {
            throw new IllegalArgumentException(String.format(Locale.ROOT, "Rollover alias [%s] can point to multiple indices, found duplicated alias [%s] in index template [%s]", rolloverRequestAlias, template.aliases().keys(), template.name()));
        }
    }
    final String matchedV2Template = findV2Template(metadata, rolloverIndexName, isHidden == null ? false : isHidden);
    if (matchedV2Template != null) {
        List<Map<String, AliasMetadata>> aliases = MetadataIndexTemplateService.resolveAliases(metadata, matchedV2Template);
        for (Map<String, AliasMetadata> aliasConfig : aliases) {
            if (aliasConfig.containsKey(rolloverRequestAlias)) {
                throw new IllegalArgumentException(String.format(Locale.ROOT, "Rollover alias [%s] can point to multiple indices, found duplicated alias [%s] in index template [%s]", rolloverRequestAlias, aliasConfig.keySet(), matchedV2Template));
            }
        }
    }
}
Also used : AliasMetadata(org.opensearch.cluster.metadata.AliasMetadata) IndexTemplateMetadata(org.opensearch.cluster.metadata.IndexTemplateMetadata) Map(java.util.Map)

Example 4 with IndexTemplateMetadata

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

the class MetadataRolloverServiceTests method testRejectDuplicateAlias.

public void testRejectDuplicateAlias() {
    final IndexTemplateMetadata template = IndexTemplateMetadata.builder("test-template").patterns(Arrays.asList("foo-*", "bar-*")).putAlias(AliasMetadata.builder("foo-write")).putAlias(AliasMetadata.builder("bar-write").writeIndex(randomBoolean())).build();
    final Metadata metadata = Metadata.builder().put(createMetadata(randomAlphaOfLengthBetween(5, 7)), false).put(template).build();
    String indexName = randomFrom("foo-123", "bar-xyz");
    String aliasName = randomFrom("foo-write", "bar-write");
    final IllegalArgumentException ex = expectThrows(IllegalArgumentException.class, () -> MetadataRolloverService.checkNoDuplicatedAliasInIndexTemplate(metadata, indexName, aliasName, randomBoolean()));
    assertThat(ex.getMessage(), containsString("index template [test-template]"));
}
Also used : IndexTemplateMetadata(org.opensearch.cluster.metadata.IndexTemplateMetadata) 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)

Example 5 with IndexTemplateMetadata

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

the class TransportBulkActionIngestTests method testFindDefaultPipelineFromTemplateMatch.

public void testFindDefaultPipelineFromTemplateMatch() {
    Exception exception = new Exception("fake exception");
    ClusterState state = clusterService.state();
    ImmutableOpenMap.Builder<String, IndexTemplateMetadata> templateMetadataBuilder = ImmutableOpenMap.builder();
    templateMetadataBuilder.put("template1", IndexTemplateMetadata.builder("template1").patterns(Arrays.asList("missing_index")).order(1).settings(Settings.builder().put(IndexSettings.DEFAULT_PIPELINE.getKey(), "pipeline1").build()).build());
    templateMetadataBuilder.put("template2", IndexTemplateMetadata.builder("template2").patterns(Arrays.asList("missing_*")).order(2).settings(Settings.builder().put(IndexSettings.DEFAULT_PIPELINE.getKey(), "pipeline2").build()).build());
    templateMetadataBuilder.put("template3", IndexTemplateMetadata.builder("template3").patterns(Arrays.asList("missing*")).order(3).build());
    templateMetadataBuilder.put("template4", IndexTemplateMetadata.builder("template4").patterns(Arrays.asList("nope")).order(4).settings(Settings.builder().put(IndexSettings.DEFAULT_PIPELINE.getKey(), "pipeline4").build()).build());
    Metadata metadata = mock(Metadata.class);
    when(state.metadata()).thenReturn(metadata);
    when(state.getMetadata()).thenReturn(metadata);
    when(metadata.templates()).thenReturn(templateMetadataBuilder.build());
    when(metadata.getTemplates()).thenReturn(templateMetadataBuilder.build());
    when(metadata.indices()).thenReturn(ImmutableOpenMap.of());
    IndexRequest indexRequest = new IndexRequest("missing_index").id("id");
    indexRequest.source(emptyMap());
    AtomicBoolean responseCalled = new AtomicBoolean(false);
    AtomicBoolean failureCalled = new AtomicBoolean(false);
    singleItemBulkWriteAction.execute(null, indexRequest, ActionListener.wrap(response -> responseCalled.set(true), e -> {
        assertThat(e, sameInstance(exception));
        failureCalled.set(true);
    }));
    assertEquals("pipeline2", indexRequest.getPipeline());
    verify(ingestService).executeBulkRequest(eq(1), bulkDocsItr.capture(), failureHandler.capture(), completionHandler.capture(), any(), eq(Names.WRITE));
}
Also used : ImmutableOpenMap(org.opensearch.common.collect.ImmutableOpenMap) Arrays(java.util.Arrays) Metadata(org.opensearch.cluster.metadata.Metadata) IndexResponse(org.opensearch.action.index.IndexResponse) Version(org.opensearch.Version) ThreadContext(org.opensearch.common.util.concurrent.ThreadContext) ClusterStateApplier(org.opensearch.cluster.ClusterStateApplier) Mockito.verifyNoInteractions(org.mockito.Mockito.verifyNoInteractions) DiscoveryNode(org.opensearch.cluster.node.DiscoveryNode) ComposableIndexTemplate(org.opensearch.cluster.metadata.ComposableIndexTemplate) Map(java.util.Map) Mockito.doAnswer(org.mockito.Mockito.doAnswer) ActionListener(org.opensearch.action.ActionListener) TimeValue(org.opensearch.common.unit.TimeValue) IndexingPressureService(org.opensearch.index.IndexingPressureService) OpenSearchTestCase(org.opensearch.test.OpenSearchTestCase) Settings(org.opensearch.common.settings.Settings) Task(org.opensearch.tasks.Task) TransportService(org.opensearch.transport.TransportService) Nullable(org.opensearch.common.Nullable) ActionFilters(org.opensearch.action.support.ActionFilters) ActionTestUtils(org.opensearch.action.support.ActionTestUtils) IndexSettings(org.opensearch.index.IndexSettings) IndexAction(org.opensearch.action.index.IndexAction) UpdateRequest(org.opensearch.action.update.UpdateRequest) Mockito.any(org.mockito.Mockito.any) Matchers.containsString(org.hamcrest.Matchers.containsString) IndexNameExpressionResolver(org.opensearch.cluster.metadata.IndexNameExpressionResolver) Names(org.opensearch.threadpool.ThreadPool.Names) Mockito.eq(org.mockito.Mockito.eq) Mockito.mock(org.mockito.Mockito.mock) MapBuilder(org.opensearch.common.collect.MapBuilder) DiscoveryNodes(org.opensearch.cluster.node.DiscoveryNodes) IndexMetadata(org.opensearch.cluster.metadata.IndexMetadata) ThreadPool(org.opensearch.threadpool.ThreadPool) DocWriteRequest(org.opensearch.action.DocWriteRequest) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Template(org.opensearch.cluster.metadata.Template) AliasMetadata(org.opensearch.cluster.metadata.AliasMetadata) OpenSearchExecutors(org.opensearch.common.util.concurrent.OpenSearchExecutors) AutoCreateIndex(org.opensearch.action.support.AutoCreateIndex) ClusterState(org.opensearch.cluster.ClusterState) ArgumentCaptor(org.mockito.ArgumentCaptor) VersionUtils(org.opensearch.test.VersionUtils) BiConsumer(java.util.function.BiConsumer) ClusterSettings(org.opensearch.common.settings.ClusterSettings) Mockito.anyString(org.mockito.Mockito.anyString) ExecutorService(java.util.concurrent.ExecutorService) Before(org.junit.Before) Collections.emptyMap(java.util.Collections.emptyMap) IndexTemplateMetadata(org.opensearch.cluster.metadata.IndexTemplateMetadata) IngestService(org.opensearch.ingest.IngestService) Iterator(java.util.Iterator) IndexNotFoundException(org.opensearch.index.IndexNotFoundException) TransportResponseHandler(org.opensearch.transport.TransportResponseHandler) CreateIndexResponse(org.opensearch.action.admin.indices.create.CreateIndexResponse) Mockito.when(org.mockito.Mockito.when) Mockito.verify(org.mockito.Mockito.verify) SystemIndices(org.opensearch.indices.SystemIndices) Mockito.never(org.mockito.Mockito.never) AtomicArray(org.opensearch.common.util.concurrent.AtomicArray) Matchers.sameInstance(org.hamcrest.Matchers.sameInstance) ClusterService(org.opensearch.cluster.service.ClusterService) Mockito.anyInt(org.mockito.Mockito.anyInt) IndexRequest(org.opensearch.action.index.IndexRequest) Collections(java.util.Collections) Mockito.reset(org.mockito.Mockito.reset) ClusterChangedEvent(org.opensearch.cluster.ClusterChangedEvent) ClusterState(org.opensearch.cluster.ClusterState) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) IndexTemplateMetadata(org.opensearch.cluster.metadata.IndexTemplateMetadata) 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) Mockito.anyString(org.mockito.Mockito.anyString) ImmutableOpenMap(org.opensearch.common.collect.ImmutableOpenMap) IndexRequest(org.opensearch.action.index.IndexRequest) IndexNotFoundException(org.opensearch.index.IndexNotFoundException)

Aggregations

IndexTemplateMetadata (org.opensearch.cluster.metadata.IndexTemplateMetadata)14 IndexMetadata (org.opensearch.cluster.metadata.IndexMetadata)8 Metadata (org.opensearch.cluster.metadata.Metadata)8 ArrayList (java.util.ArrayList)5 AliasMetadata (org.opensearch.cluster.metadata.AliasMetadata)5 Map (java.util.Map)4 Matchers.containsString (org.hamcrest.Matchers.containsString)3 Arrays (java.util.Arrays)2 Collections (java.util.Collections)2 List (java.util.List)2 Version (org.opensearch.Version)2 ActionListener (org.opensearch.action.ActionListener)2 ClusterState (org.opensearch.cluster.ClusterState)2 ComposableIndexTemplate (org.opensearch.cluster.metadata.ComposableIndexTemplate)2 RoutingTable (org.opensearch.cluster.routing.RoutingTable)2 Settings (org.opensearch.common.settings.Settings)2 IndexSettings (org.opensearch.index.IndexSettings)2 IntHashSet (com.carrotsearch.hppc.IntHashSet)1 IntSet (com.carrotsearch.hppc.IntSet)1 ObjectCursor (com.carrotsearch.hppc.cursors.ObjectCursor)1