Search in sources :

Example 36 with IndexTemplateMetadata

use of org.elasticsearch.cluster.metadata.IndexTemplateMetadata in project crate by crate.

the class AbstractOpenCloseTableClusterStateTaskExecutor method prepare.

protected Context prepare(ClusterState currentState, OpenCloseTableOrPartitionRequest request) {
    RelationName relationName = request.tableIdent();
    String partitionIndexName = request.partitionIndexName();
    Metadata metadata = currentState.metadata();
    String indexToResolve = partitionIndexName != null ? partitionIndexName : relationName.indexNameOrAlias();
    PartitionName partitionName = partitionIndexName != null ? PartitionName.fromIndexOrTemplate(partitionIndexName) : null;
    String[] concreteIndices = indexNameExpressionResolver.concreteIndexNames(currentState, IndicesOptions.lenientExpandOpen(), indexToResolve);
    Set<IndexMetadata> indicesMetadata = DDLClusterStateHelpers.indexMetadataSetFromIndexNames(metadata, concreteIndices, indexState());
    IndexTemplateMetadata indexTemplateMetadata = null;
    if (partitionIndexName == null) {
        indexTemplateMetadata = DDLClusterStateHelpers.templateMetadata(metadata, relationName);
    }
    return new Context(indicesMetadata, indexTemplateMetadata, partitionName);
}
Also used : PartitionName(io.crate.metadata.PartitionName) IndexTemplateMetadata(org.elasticsearch.cluster.metadata.IndexTemplateMetadata) IndexMetadata(org.elasticsearch.cluster.metadata.IndexMetadata) IndexTemplateMetadata(org.elasticsearch.cluster.metadata.IndexTemplateMetadata) Metadata(org.elasticsearch.cluster.metadata.Metadata) RelationName(io.crate.metadata.RelationName) IndexMetadata(org.elasticsearch.cluster.metadata.IndexMetadata)

Example 37 with IndexTemplateMetadata

use of org.elasticsearch.cluster.metadata.IndexTemplateMetadata in project crate by crate.

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.elasticsearch.common.xcontent.ToXContent) IndexTemplateMetadata(org.elasticsearch.cluster.metadata.IndexTemplateMetadata)

Example 38 with IndexTemplateMetadata

use of org.elasticsearch.cluster.metadata.IndexTemplateMetadata in project crate by crate.

the class GetIndexTemplatesResponse method fromXContent.

public static GetIndexTemplatesResponse fromXContent(XContentParser parser) throws IOException {
    final List<IndexTemplateMetadata> templates = new ArrayList<>();
    for (XContentParser.Token token = parser.nextToken(); token != XContentParser.Token.END_OBJECT; token = parser.nextToken()) {
        if (token == XContentParser.Token.FIELD_NAME) {
            final IndexTemplateMetadata templateMetadata = IndexTemplateMetadata.Builder.fromXContent(parser, parser.currentName());
            templates.add(templateMetadata);
        }
    }
    return new GetIndexTemplatesResponse(templates);
}
Also used : IndexTemplateMetadata(org.elasticsearch.cluster.metadata.IndexTemplateMetadata) ArrayList(java.util.ArrayList) XContentParser(org.elasticsearch.common.xcontent.XContentParser)

Example 39 with IndexTemplateMetadata

use of org.elasticsearch.cluster.metadata.IndexTemplateMetadata in project crate by crate.

the class DDLClusterStateHelpers method updateTemplate.

public static IndexTemplateMetadata updateTemplate(IndexTemplateMetadata indexTemplateMetadata, Map<String, Object> newMappings, Map<String, Object> mappingsToRemove, Settings newSettings, BiConsumer<String, Settings> settingsValidator, Predicate<String> settingsFilter) {
    // merge mappings & remove mappings
    Map<String, Object> mapping = removeFromMapping(mergeTemplateMapping(indexTemplateMetadata, newMappings), mappingsToRemove);
    // merge settings
    final Settings settings = Settings.builder().put(indexTemplateMetadata.settings()).put(newSettings).normalizePrefix(IndexMetadata.INDEX_SETTING_PREFIX).build().filter(settingsFilter);
    settingsValidator.accept(indexTemplateMetadata.getName(), settings);
    // wrap it in a type map if its not
    if (mapping.size() != 1 || mapping.containsKey(Constants.DEFAULT_MAPPING_TYPE) == false) {
        mapping = MapBuilder.<String, Object>newMapBuilder().put(Constants.DEFAULT_MAPPING_TYPE, mapping).map();
    }
    try {
        return new IndexTemplateMetadata.Builder(indexTemplateMetadata).settings(settings).putMapping(Constants.DEFAULT_MAPPING_TYPE, Strings.toString(XContentFactory.jsonBuilder().map(mapping))).build();
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}
Also used : IndexTemplateMetadata(org.elasticsearch.cluster.metadata.IndexTemplateMetadata) IOException(java.io.IOException) Settings(org.elasticsearch.common.settings.Settings)

Example 40 with IndexTemplateMetadata

use of org.elasticsearch.cluster.metadata.IndexTemplateMetadata in project crate by crate.

the class IndexTemplateUpgrader method archiveUnknownOrInvalidSettings.

/**
 * Filter out all unknown/old/invalid settings. Archiving them *only* is not working as they would be "un-archived"
 * by {@link IndexTemplateMetadata.Builder#fromXContent} logic to prefix all settings with `index.` when applying
 * the new cluster state.
 */
private HashMap<String, IndexTemplateMetadata> archiveUnknownOrInvalidSettings(Map<String, IndexTemplateMetadata> templates) {
    HashMap<String, IndexTemplateMetadata> upgradedTemplates = new HashMap<>(templates.size());
    for (Map.Entry<String, IndexTemplateMetadata> entry : templates.entrySet()) {
        IndexTemplateMetadata templateMetadata = entry.getValue();
        Settings.Builder settingsBuilder = Settings.builder().put(templateMetadata.settings());
        String templateName = entry.getKey();
        // only process partition table templates
        if (IndexParts.isPartitioned(templateName) == false) {
            upgradedTemplates.put(templateName, templateMetadata);
            continue;
        }
        Settings settings = DEFAULT_SCOPED_SETTINGS.archiveUnknownOrInvalidSettings(settingsBuilder.build(), e -> {
        }, (e, ex) -> {
        }).filter(k -> k.startsWith(ARCHIVED_SETTINGS_PREFIX) == false);
        IndexTemplateMetadata.Builder builder = IndexTemplateMetadata.builder(templateName).patterns(templateMetadata.patterns()).order(templateMetadata.order()).settings(settings);
        try {
            for (ObjectObjectCursor<String, CompressedXContent> cursor : templateMetadata.getMappings()) {
                var mappingSource = XContentHelper.toMap(cursor.value.compressedReference(), XContentType.JSON);
                Object defaultMapping = mappingSource.get("default");
                if (defaultMapping instanceof Map && ((Map<?, ?>) defaultMapping).containsKey("_all")) {
                    Map<?, ?> mapping = (Map<?, ?>) defaultMapping;
                    // Support for `_all` was removed (in favour of `copy_to`.
                    // We never utilized this but always set `_all: {enabled: false}` if you created a table using SQL in earlier version, so we can safely drop it.
                    mapping.remove("_all");
                    builder.putMapping(cursor.key, new CompressedXContent(BytesReference.bytes(XContentFactory.jsonBuilder().value(mappingSource))));
                } else {
                    builder.putMapping(cursor.key, cursor.value);
                }
            }
        } catch (IOException e) {
            logger.error("Error while trying to upgrade template '" + templateName + "'", e);
            continue;
        }
        for (ObjectObjectCursor<String, AliasMetadata> container : templateMetadata.aliases()) {
            builder.putAlias(container.value);
        }
        upgradedTemplates.put(templateName, builder.build());
    }
    return upgradedTemplates;
}
Also used : IndexParts(io.crate.metadata.IndexParts) XContentFactory(org.elasticsearch.common.xcontent.XContentFactory) XContentType(org.elasticsearch.common.xcontent.XContentType) IOException(java.io.IOException) HashMap(java.util.HashMap) UnaryOperator(java.util.function.UnaryOperator) IndexTemplateMetadata(org.elasticsearch.cluster.metadata.IndexTemplateMetadata) BytesReference(org.elasticsearch.common.bytes.BytesReference) XContentHelper(org.elasticsearch.common.xcontent.XContentHelper) ObjectObjectCursor(com.carrotsearch.hppc.cursors.ObjectObjectCursor) Logger(org.apache.logging.log4j.Logger) Settings(org.elasticsearch.common.settings.Settings) CompressedXContent(org.elasticsearch.common.compress.CompressedXContent) Map(java.util.Map) AliasMetadata(org.elasticsearch.cluster.metadata.AliasMetadata) DEFAULT_SCOPED_SETTINGS(org.elasticsearch.common.settings.IndexScopedSettings.DEFAULT_SCOPED_SETTINGS) LogManager(org.apache.logging.log4j.LogManager) ARCHIVED_SETTINGS_PREFIX(org.elasticsearch.common.settings.AbstractScopedSettings.ARCHIVED_SETTINGS_PREFIX) AliasMetadata(org.elasticsearch.cluster.metadata.AliasMetadata) HashMap(java.util.HashMap) IndexTemplateMetadata(org.elasticsearch.cluster.metadata.IndexTemplateMetadata) IOException(java.io.IOException) CompressedXContent(org.elasticsearch.common.compress.CompressedXContent) HashMap(java.util.HashMap) Map(java.util.Map) Settings(org.elasticsearch.common.settings.Settings)

Aggregations

IndexTemplateMetadata (org.elasticsearch.cluster.metadata.IndexTemplateMetadata)36 IndexTemplateMetaData (org.elasticsearch.cluster.metadata.IndexTemplateMetaData)18 Test (org.junit.Test)16 Settings (org.elasticsearch.common.settings.Settings)14 IndexMetadata (org.elasticsearch.cluster.metadata.IndexMetadata)13 Map (java.util.Map)12 Metadata (org.elasticsearch.cluster.metadata.Metadata)11 CompressedXContent (org.elasticsearch.common.compress.CompressedXContent)11 GetIndexTemplatesResponse (org.elasticsearch.action.admin.indices.template.get.GetIndexTemplatesResponse)10 ClusterState (org.elasticsearch.cluster.ClusterState)10 PartitionName (io.crate.metadata.PartitionName)9 ArrayList (java.util.ArrayList)9 HashMap (java.util.HashMap)9 Index (org.elasticsearch.index.Index)9 RelationName (io.crate.metadata.RelationName)8 RoutingTable (org.elasticsearch.cluster.routing.RoutingTable)7 AliasMetadata (org.elasticsearch.cluster.metadata.AliasMetadata)6 ImmutableOpenMap (org.elasticsearch.common.collect.ImmutableOpenMap)6 IOException (java.io.IOException)5 Set (java.util.Set)5