Search in sources :

Example 1 with ARCHIVED_SETTINGS_PREFIX

use of org.elasticsearch.common.settings.AbstractScopedSettings.ARCHIVED_SETTINGS_PREFIX 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)

Example 2 with ARCHIVED_SETTINGS_PREFIX

use of org.elasticsearch.common.settings.AbstractScopedSettings.ARCHIVED_SETTINGS_PREFIX in project crate by crate.

the class SettingsUpdater method partitionKnownAndValidSettings.

/**
 * Partitions the settings into those that are known and valid versus those that are unknown or invalid. The resulting tuple contains
 * the known and valid settings in the first component and the unknown or invalid settings in the second component. Note that archived
 * settings contained in the settings to partition are included in the first component.
 *
 * @param settings     the settings to partition
 * @param settingsType a string to identify the settings (for logging)
 * @param logger       a logger to sending warnings to
 * @return the partitioned settings
 */
private Tuple<Settings, Settings> partitionKnownAndValidSettings(final Settings settings, final String settingsType, final Logger logger) {
    final Settings existingArchivedSettings = settings.filter(k -> k.startsWith(ARCHIVED_SETTINGS_PREFIX));
    final Settings settingsExcludingExistingArchivedSettings = settings.filter(k -> k.startsWith(ARCHIVED_SETTINGS_PREFIX) == false);
    final Settings settingsWithUnknownOrInvalidArchived = clusterSettings.archiveUnknownOrInvalidSettings(settingsExcludingExistingArchivedSettings, e -> logUnknownSetting(settingsType, e, logger), (e, ex) -> logInvalidSetting(settingsType, e, ex, logger));
    return Tuple.tuple(Settings.builder().put(settingsWithUnknownOrInvalidArchived.filter(k -> k.startsWith(ARCHIVED_SETTINGS_PREFIX) == false)).put(existingArchivedSettings).build(), settingsWithUnknownOrInvalidArchived.filter(k -> k.startsWith(ARCHIVED_SETTINGS_PREFIX)));
}
Also used : ClusterState(org.elasticsearch.cluster.ClusterState) Metadata(org.elasticsearch.cluster.metadata.Metadata) ClusterSettings(org.elasticsearch.common.settings.ClusterSettings) Logger(org.apache.logging.log4j.Logger) Tuple(io.crate.common.collections.Tuple) Settings(org.elasticsearch.common.settings.Settings) Supplier(org.apache.logging.log4j.util.Supplier) Map(java.util.Map) ClusterState.builder(org.elasticsearch.cluster.ClusterState.builder) ClusterBlocks(org.elasticsearch.cluster.block.ClusterBlocks) ParameterizedMessage(org.apache.logging.log4j.message.ParameterizedMessage) ARCHIVED_SETTINGS_PREFIX(org.elasticsearch.common.settings.AbstractScopedSettings.ARCHIVED_SETTINGS_PREFIX) ClusterSettings(org.elasticsearch.common.settings.ClusterSettings) Settings(org.elasticsearch.common.settings.Settings)

Aggregations

Map (java.util.Map)2 Logger (org.apache.logging.log4j.Logger)2 ARCHIVED_SETTINGS_PREFIX (org.elasticsearch.common.settings.AbstractScopedSettings.ARCHIVED_SETTINGS_PREFIX)2 Settings (org.elasticsearch.common.settings.Settings)2 ObjectObjectCursor (com.carrotsearch.hppc.cursors.ObjectObjectCursor)1 Tuple (io.crate.common.collections.Tuple)1 IndexParts (io.crate.metadata.IndexParts)1 IOException (java.io.IOException)1 HashMap (java.util.HashMap)1 UnaryOperator (java.util.function.UnaryOperator)1 LogManager (org.apache.logging.log4j.LogManager)1 ParameterizedMessage (org.apache.logging.log4j.message.ParameterizedMessage)1 Supplier (org.apache.logging.log4j.util.Supplier)1 ClusterState (org.elasticsearch.cluster.ClusterState)1 ClusterState.builder (org.elasticsearch.cluster.ClusterState.builder)1 ClusterBlocks (org.elasticsearch.cluster.block.ClusterBlocks)1 AliasMetadata (org.elasticsearch.cluster.metadata.AliasMetadata)1 IndexTemplateMetadata (org.elasticsearch.cluster.metadata.IndexTemplateMetadata)1 Metadata (org.elasticsearch.cluster.metadata.Metadata)1 BytesReference (org.elasticsearch.common.bytes.BytesReference)1