Search in sources :

Example 16 with AliasMetadata

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

the class GetIndexIT method assertAliases.

private void assertAliases(GetIndexResponse response, String indexName) {
    ImmutableOpenMap<String, List<AliasMetadata>> aliases = response.aliases();
    assertThat(aliases, notNullValue());
    assertThat(aliases.size(), equalTo(1));
    List<AliasMetadata> indexAliases = aliases.get(indexName);
    assertThat(indexAliases, notNullValue());
    assertThat(indexAliases.size(), equalTo(1));
    AliasMetadata alias = indexAliases.get(0);
    assertThat(alias, notNullValue());
    assertThat(alias.alias(), equalTo("alias_idx"));
}
Also used : AliasMetadata(org.opensearch.cluster.metadata.AliasMetadata) ArrayList(java.util.ArrayList) List(java.util.List)

Example 17 with AliasMetadata

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

the class TransportIndicesAliasesAction method concreteAliases.

private static String[] concreteAliases(IndicesAliasesRequest.AliasActions action, Metadata metadata, String concreteIndex) {
    if (action.expandAliasesWildcards()) {
        // for DELETE we expand the aliases
        String[] indexAsArray = { concreteIndex };
        ImmutableOpenMap<String, List<AliasMetadata>> aliasMetadata = metadata.findAliases(action, indexAsArray);
        List<String> finalAliases = new ArrayList<>();
        for (ObjectCursor<List<AliasMetadata>> curAliases : aliasMetadata.values()) {
            for (AliasMetadata aliasMeta : curAliases.value) {
                finalAliases.add(aliasMeta.alias());
            }
        }
        return finalAliases.toArray(new String[finalAliases.size()]);
    } else {
        // for ADD and REMOVE_INDEX we just return the current aliases
        return action.aliases();
    }
}
Also used : AliasMetadata(org.opensearch.cluster.metadata.AliasMetadata) ArrayList(java.util.ArrayList) Collections.unmodifiableList(java.util.Collections.unmodifiableList) ArrayList(java.util.ArrayList) List(java.util.List)

Example 18 with AliasMetadata

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

the class MetadataRolloverService method rolloverAlias.

private RolloverResult rolloverAlias(ClusterState currentState, IndexAbstraction.Alias alias, String aliasName, String newIndexName, CreateIndexRequest createIndexRequest, List<Condition<?>> metConditions, boolean silent, boolean onlyValidate) throws Exception {
    final Metadata metadata = currentState.metadata();
    final IndexMetadata writeIndex = alias.getWriteIndex();
    final AliasMetadata aliasMetadata = writeIndex.getAliases().get(alias.getName());
    final String sourceProvidedName = writeIndex.getSettings().get(IndexMetadata.SETTING_INDEX_PROVIDED_NAME, writeIndex.getIndex().getName());
    final String sourceIndexName = writeIndex.getIndex().getName();
    final String unresolvedName = (newIndexName != null) ? newIndexName : generateRolloverIndexName(sourceProvidedName, indexNameExpressionResolver);
    final String rolloverIndexName = indexNameExpressionResolver.resolveDateMathExpression(unresolvedName);
    final boolean explicitWriteIndex = Boolean.TRUE.equals(aliasMetadata.writeIndex());
    final Boolean isHidden = IndexMetadata.INDEX_HIDDEN_SETTING.exists(createIndexRequest.settings()) ? IndexMetadata.INDEX_HIDDEN_SETTING.get(createIndexRequest.settings()) : null;
    // fails if the index already exists
    createIndexService.validateIndexName(rolloverIndexName, currentState);
    checkNoDuplicatedAliasInIndexTemplate(metadata, rolloverIndexName, aliasName, isHidden);
    if (onlyValidate) {
        return new RolloverResult(rolloverIndexName, sourceIndexName, currentState);
    }
    CreateIndexClusterStateUpdateRequest createIndexClusterStateRequest = prepareCreateIndexRequest(unresolvedName, rolloverIndexName, createIndexRequest);
    ClusterState newState = createIndexService.applyCreateIndexRequest(currentState, createIndexClusterStateRequest, silent);
    newState = indexAliasesService.applyAliasActions(newState, rolloverAliasToNewIndex(sourceIndexName, rolloverIndexName, explicitWriteIndex, aliasMetadata.isHidden(), aliasName));
    RolloverInfo rolloverInfo = new RolloverInfo(aliasName, metConditions, threadPool.absoluteTimeInMillis());
    newState = ClusterState.builder(newState).metadata(Metadata.builder(newState.metadata()).put(IndexMetadata.builder(newState.metadata().index(sourceIndexName)).putRolloverInfo(rolloverInfo))).build();
    return new RolloverResult(rolloverIndexName, sourceIndexName, newState);
}
Also used : ClusterState(org.opensearch.cluster.ClusterState) AliasMetadata(org.opensearch.cluster.metadata.AliasMetadata) CreateIndexClusterStateUpdateRequest(org.opensearch.action.admin.indices.create.CreateIndexClusterStateUpdateRequest) Metadata(org.opensearch.cluster.metadata.Metadata) IndexMetadata(org.opensearch.cluster.metadata.IndexMetadata) AliasMetadata(org.opensearch.cluster.metadata.AliasMetadata) IndexTemplateMetadata(org.opensearch.cluster.metadata.IndexTemplateMetadata) IndexMetadata(org.opensearch.cluster.metadata.IndexMetadata)

Example 19 with AliasMetadata

use of org.opensearch.cluster.metadata.AliasMetadata 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 20 with AliasMetadata

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

the class GetIndexResponse method toXContent.

@Override
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
    builder.startObject();
    {
        for (final String index : indices) {
            builder.startObject(index);
            {
                builder.startObject("aliases");
                List<AliasMetadata> indexAliases = aliases.get(index);
                if (indexAliases != null) {
                    for (final AliasMetadata alias : indexAliases) {
                        AliasMetadata.Builder.toXContent(alias, builder, params);
                    }
                }
                builder.endObject();
                MappingMetadata indexMappings = mappings.get(index);
                if (indexMappings == null) {
                    builder.startObject("mappings").endObject();
                } else {
                    builder.field("mappings", indexMappings.sourceAsMap());
                }
                builder.startObject("settings");
                Settings indexSettings = settings.get(index);
                if (indexSettings != null) {
                    indexSettings.toXContent(builder, params);
                }
                builder.endObject();
                Settings defaultIndexSettings = defaultSettings.get(index);
                if (defaultIndexSettings != null && defaultIndexSettings.isEmpty() == false) {
                    builder.startObject("defaults");
                    defaultIndexSettings.toXContent(builder, params);
                    builder.endObject();
                }
                String dataStream = dataStreams.get(index);
                if (dataStream != null) {
                    builder.field("data_stream", dataStream);
                }
            }
            builder.endObject();
        }
    }
    builder.endObject();
    return builder;
}
Also used : AliasMetadata(org.opensearch.cluster.metadata.AliasMetadata) MappingMetadata(org.opensearch.cluster.metadata.MappingMetadata) Settings(org.opensearch.common.settings.Settings)

Aggregations

AliasMetadata (org.opensearch.cluster.metadata.AliasMetadata)52 List (java.util.List)21 Settings (org.opensearch.common.settings.Settings)16 ArrayList (java.util.ArrayList)13 IndexMetadata (org.opensearch.cluster.metadata.IndexMetadata)13 ImmutableOpenMap (org.opensearch.common.collect.ImmutableOpenMap)12 Template (org.opensearch.cluster.metadata.Template)10 HashMap (java.util.HashMap)8 Matchers.containsString (org.hamcrest.Matchers.containsString)8 MappingMetadata (org.opensearch.cluster.metadata.MappingMetadata)8 Metadata (org.opensearch.cluster.metadata.Metadata)8 CompressedXContent (org.opensearch.common.compress.CompressedXContent)8 IOException (java.io.IOException)7 ComponentTemplate (org.opensearch.cluster.metadata.ComponentTemplate)7 ComposableIndexTemplate (org.opensearch.cluster.metadata.ComposableIndexTemplate)7 IndexTemplateMetadata (org.opensearch.cluster.metadata.IndexTemplateMetadata)7 HashSet (java.util.HashSet)6 Map (java.util.Map)6 ActionListener (org.opensearch.action.ActionListener)6 GetAliasesRequest (org.opensearch.action.admin.indices.alias.get.GetAliasesRequest)6