Search in sources :

Example 11 with IndexTemplateMetaData

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

the class TransportSchemaUpdateAction method updateTemplate.

@VisibleForTesting
static ClusterState updateTemplate(NamedXContentRegistry xContentRegistry, ClusterState currentState, String templateName, Map<String, Object> newMapping) throws Exception {
    IndexTemplateMetadata template = currentState.metadata().templates().get(templateName);
    if (template == null) {
        throw new ResourceNotFoundException("Template \"" + templateName + "\" for partitioned table is missing");
    }
    IndexTemplateMetadata.Builder templateBuilder = new IndexTemplateMetadata.Builder(template);
    for (ObjectObjectCursor<String, CompressedXContent> cursor : template.mappings()) {
        Map<String, Object> source = parseMapping(xContentRegistry, cursor.value.toString());
        mergeIntoSource(source, newMapping);
        try (XContentBuilder xContentBuilder = JsonXContent.contentBuilder()) {
            templateBuilder.putMapping(cursor.key, Strings.toString(xContentBuilder.map(source)));
        }
    }
    Metadata.Builder builder = Metadata.builder(currentState.metadata()).put(templateBuilder);
    return ClusterState.builder(currentState).metadata(builder).build();
}
Also used : IndexTemplateMetadata(org.elasticsearch.cluster.metadata.IndexTemplateMetadata) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder) CompressedXContent(org.elasticsearch.common.compress.CompressedXContent) Metadata(org.elasticsearch.cluster.metadata.Metadata) IndexTemplateMetadata(org.elasticsearch.cluster.metadata.IndexTemplateMetadata) ResourceNotFoundException(org.elasticsearch.ResourceNotFoundException) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder) VisibleForTesting(io.crate.common.annotations.VisibleForTesting)

Example 12 with IndexTemplateMetaData

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

the class DocSchemaInfo method update.

@Override
public void update(ClusterChangedEvent event) {
    assert event.metadataChanged() : "metadataChanged must be true if update is called";
    // search for aliases of deleted and created indices, they must be invalidated also
    Metadata prevMetadata = event.previousState().metadata();
    for (Index index : event.indicesDeleted()) {
        invalidateFromIndex(index, prevMetadata);
    }
    Metadata newMetadata = event.state().metadata();
    for (String index : event.indicesCreated()) {
        invalidateAliases(newMetadata.index(index).getAliases());
    }
    // search for templates with changed meta data => invalidate template aliases
    ImmutableOpenMap<String, IndexTemplateMetadata> newTemplates = newMetadata.templates();
    ImmutableOpenMap<String, IndexTemplateMetadata> prevTemplates = prevMetadata.templates();
    if (!newTemplates.equals(prevTemplates)) {
        for (ObjectCursor<IndexTemplateMetadata> cursor : newTemplates.values()) {
            invalidateAliases(cursor.value.aliases());
        }
        for (ObjectCursor<IndexTemplateMetadata> cursor : prevTemplates.values()) {
            invalidateAliases(cursor.value.aliases());
        }
    }
    // search indices with changed meta data
    Iterator<String> currentTablesIt = docTableByName.keySet().iterator();
    ObjectLookupContainer<String> templates = newTemplates.keys();
    ImmutableOpenMap<String, IndexMetadata> indices = newMetadata.indices();
    while (currentTablesIt.hasNext()) {
        String tableName = currentTablesIt.next();
        String indexName = getIndexName(tableName);
        IndexMetadata newIndexMetadata = newMetadata.index(indexName);
        if (newIndexMetadata == null) {
            docTableByName.remove(tableName);
        } else {
            IndexMetadata oldIndexMetadata = prevMetadata.index(indexName);
            if (oldIndexMetadata != null && ClusterChangedEvent.indexMetadataChanged(oldIndexMetadata, newIndexMetadata)) {
                docTableByName.remove(tableName);
                // invalidate aliases of changed indices
                invalidateAliases(newIndexMetadata.getAliases());
                invalidateAliases(oldIndexMetadata.getAliases());
            } else {
                // this is the case if a single partition has been modified using alter table <t> partition (...)
                String possibleTemplateName = PartitionName.templateName(name(), tableName);
                if (templates.contains(possibleTemplateName)) {
                    for (ObjectObjectCursor<String, IndexMetadata> indexEntry : indices) {
                        if (IndexParts.isPartitioned(indexEntry.key)) {
                            docTableByName.remove(tableName);
                            break;
                        }
                    }
                }
            }
        }
    }
    // re register UDFs for this schema
    UserDefinedFunctionsMetadata udfMetadata = newMetadata.custom(UserDefinedFunctionsMetadata.TYPE);
    if (udfMetadata != null) {
        udfService.updateImplementations(schemaName, udfMetadata.functionsMetadata().stream().filter(f -> schemaName.equals(f.schema())));
    }
}
Also used : BlobIndex(io.crate.blob.v2.BlobIndex) IndexParts(io.crate.metadata.IndexParts) ImmutableOpenMap(org.elasticsearch.common.collect.ImmutableOpenMap) RelationName(io.crate.metadata.RelationName) IndexMetadata(org.elasticsearch.cluster.metadata.IndexMetadata) ViewInfoFactory(io.crate.metadata.view.ViewInfoFactory) ClusterService(org.elasticsearch.cluster.service.ClusterService) UserDefinedFunctionService(io.crate.expression.udf.UserDefinedFunctionService) Index(org.elasticsearch.index.Index) UserDefinedFunctionsMetadata(io.crate.expression.udf.UserDefinedFunctionsMetadata) PartitionName(io.crate.metadata.PartitionName) HashSet(java.util.HashSet) ObjectObjectCursor(com.carrotsearch.hppc.cursors.ObjectObjectCursor) Metadata(org.elasticsearch.cluster.metadata.Metadata) AliasMetadata(org.elasticsearch.cluster.metadata.AliasMetadata) ViewInfo(io.crate.metadata.view.ViewInfo) StreamSupport(java.util.stream.StreamSupport) Nullable(javax.annotation.Nullable) SchemaInfo(io.crate.metadata.table.SchemaInfo) TableInfo(io.crate.metadata.table.TableInfo) NodeContext(io.crate.metadata.NodeContext) Iterator(java.util.Iterator) Predicate(java.util.function.Predicate) Collection(java.util.Collection) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) ViewsMetadata(io.crate.metadata.view.ViewsMetadata) Set(java.util.Set) ResourceUnknownException(io.crate.exceptions.ResourceUnknownException) IndexTemplateMetadata(org.elasticsearch.cluster.metadata.IndexTemplateMetadata) ObjectCursor(com.carrotsearch.hppc.cursors.ObjectCursor) ClusterChangedEvent(org.elasticsearch.cluster.ClusterChangedEvent) Objects(java.util.Objects) Stream(java.util.stream.Stream) Schemas(io.crate.metadata.Schemas) VisibleForTesting(io.crate.common.annotations.VisibleForTesting) Collections(java.util.Collections) ObjectLookupContainer(com.carrotsearch.hppc.ObjectLookupContainer) IndexTemplateMetadata(org.elasticsearch.cluster.metadata.IndexTemplateMetadata) IndexMetadata(org.elasticsearch.cluster.metadata.IndexMetadata) UserDefinedFunctionsMetadata(io.crate.expression.udf.UserDefinedFunctionsMetadata) Metadata(org.elasticsearch.cluster.metadata.Metadata) AliasMetadata(org.elasticsearch.cluster.metadata.AliasMetadata) ViewsMetadata(io.crate.metadata.view.ViewsMetadata) IndexTemplateMetadata(org.elasticsearch.cluster.metadata.IndexTemplateMetadata) BlobIndex(io.crate.blob.v2.BlobIndex) Index(org.elasticsearch.index.Index) UserDefinedFunctionsMetadata(io.crate.expression.udf.UserDefinedFunctionsMetadata) IndexMetadata(org.elasticsearch.cluster.metadata.IndexMetadata)

Example 13 with IndexTemplateMetaData

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

the class DocTableInfoBuilder method buildDocIndexMetadataFromTemplate.

private DocIndexMetadata buildDocIndexMetadataFromTemplate(String index, String templateName) {
    IndexTemplateMetadata indexTemplateMetadata = metadata.getTemplates().get(templateName);
    DocIndexMetadata docIndexMetadata;
    try {
        IndexMetadata.Builder builder = new IndexMetadata.Builder(index);
        builder.putMapping(Constants.DEFAULT_MAPPING_TYPE, indexTemplateMetadata.getMappings().get(Constants.DEFAULT_MAPPING_TYPE).toString());
        Settings.Builder settingsBuilder = Settings.builder().put(indexTemplateMetadata.settings()).put(IndexMetadata.SETTING_VERSION_CREATED, Version.CURRENT);
        Settings settings = settingsBuilder.build();
        builder.settings(settings);
        builder.numberOfShards(settings.getAsInt(SETTING_NUMBER_OF_SHARDS, 5));
        builder.numberOfReplicas(settings.getAsInt(SETTING_NUMBER_OF_REPLICAS, 1));
        docIndexMetadata = new DocIndexMetadata(nodeCtx, builder.build(), ident);
    } catch (IOException e) {
        throw new UnhandledServerException("Unable to build DocIndexMetadata from template", e);
    }
    return docIndexMetadata.build();
}
Also used : IndexTemplateMetadata(org.elasticsearch.cluster.metadata.IndexTemplateMetadata) UnhandledServerException(io.crate.exceptions.UnhandledServerException) IOException(java.io.IOException) IndexMetadata(org.elasticsearch.cluster.metadata.IndexMetadata) Settings(org.elasticsearch.common.settings.Settings)

Example 14 with IndexTemplateMetaData

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

the class AlterTableClusterStateExecutor method updateTemplate.

static ClusterState updateTemplate(ClusterState currentState, RelationName relationName, Settings newSetting, Map<String, Object> newMapping, BiConsumer<String, Settings> settingsValidator, IndexScopedSettings indexScopedSettings) throws IOException {
    String templateName = PartitionName.templateName(relationName.schema(), relationName.name());
    IndexTemplateMetadata indexTemplateMetadata = currentState.metadata().templates().get(templateName);
    IndexTemplateMetadata newIndexTemplateMetadata = DDLClusterStateHelpers.updateTemplate(indexTemplateMetadata, newMapping, Collections.emptyMap(), newSetting, settingsValidator, k -> indexScopedSettings.isPrivateSetting(k) == false);
    final Metadata.Builder metadata = Metadata.builder(currentState.metadata()).put(newIndexTemplateMetadata);
    return ClusterState.builder(currentState).metadata(metadata).build();
}
Also used : IndexTemplateMetadata(org.elasticsearch.cluster.metadata.IndexTemplateMetadata) IndexMetadata(org.elasticsearch.cluster.metadata.IndexMetadata) Metadata(org.elasticsearch.cluster.metadata.Metadata) IndexTemplateMetadata(org.elasticsearch.cluster.metadata.IndexTemplateMetadata)

Example 15 with IndexTemplateMetaData

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

the class RenameTableClusterStateExecutor method execute.

public ClusterState execute(ClusterState currentState, RenameTableRequest request) throws Exception {
    RelationName source = request.sourceTableIdent();
    RelationName target = request.targetTableIdent();
    boolean isPartitioned = request.isPartitioned();
    Metadata currentMetadata = currentState.getMetadata();
    Metadata.Builder newMetadata = Metadata.builder(currentMetadata);
    if (isPartitioned) {
        IndexTemplateMetadata indexTemplateMetadata = DDLClusterStateHelpers.templateMetadata(currentMetadata, source);
        if (indexTemplateMetadata == null) {
            throw new IndexTemplateMissingException("Template for partitioned table is missing");
        }
        renameTemplate(newMetadata, indexTemplateMetadata, target);
    }
    RoutingTable.Builder newRoutingTable = RoutingTable.builder(currentState.routingTable());
    ClusterBlocks.Builder blocksBuilder = ClusterBlocks.builder().blocks(currentState.blocks());
    logger.info("renaming table '{}' to '{}'", source.fqn(), target.fqn());
    try {
        Index[] sourceIndices = indexNameExpressionResolver.concreteIndices(currentState, STRICT_INDICES_OPTIONS, source.indexNameOrAlias());
        for (Index sourceIndex : sourceIndices) {
            IndexMetadata sourceIndexMetadata = currentMetadata.getIndexSafe(sourceIndex);
            String sourceIndexName = sourceIndex.getName();
            newMetadata.remove(sourceIndexName);
            newRoutingTable.remove(sourceIndexName);
            blocksBuilder.removeIndexBlocks(sourceIndexName);
            IndexMetadata targetMd;
            if (isPartitioned) {
                PartitionName partitionName = PartitionName.fromIndexOrTemplate(sourceIndexName);
                String targetIndexName = IndexParts.toIndexName(target, partitionName.ident());
                targetMd = IndexMetadata.builder(sourceIndexMetadata).removeAllAliases().putAlias(AliasMetadata.builder(target.indexNameOrAlias()).build()).index(targetIndexName).build();
            } else {
                targetMd = IndexMetadata.builder(sourceIndexMetadata).index(target.indexNameOrAlias()).build();
            }
            newMetadata.put(targetMd, true);
            newRoutingTable.addAsFromCloseToOpen(targetMd);
            blocksBuilder.addBlocks(targetMd);
        }
    } catch (IndexNotFoundException e) {
        if (isPartitioned == false) {
            throw e;
        }
    // empty partition case, no indices, just a template exists
    }
    ClusterState clusterStateAfterRename = ClusterState.builder(currentState).metadata(newMetadata).routingTable(newRoutingTable.build()).blocks(blocksBuilder).build();
    return allocationService.reroute(ddlClusterStateService.onRenameTable(clusterStateAfterRename, source, target, request.isPartitioned()), "rename-table");
}
Also used : ClusterState(org.elasticsearch.cluster.ClusterState) IndexTemplateMissingException(org.elasticsearch.indices.IndexTemplateMissingException) ClusterBlocks(org.elasticsearch.cluster.block.ClusterBlocks) IndexTemplateMetadata(org.elasticsearch.cluster.metadata.IndexTemplateMetadata) IndexMetadata(org.elasticsearch.cluster.metadata.IndexMetadata) IndexTemplateMetadata(org.elasticsearch.cluster.metadata.IndexTemplateMetadata) Metadata(org.elasticsearch.cluster.metadata.Metadata) AliasMetadata(org.elasticsearch.cluster.metadata.AliasMetadata) Index(org.elasticsearch.index.Index) PartitionName(io.crate.metadata.PartitionName) RoutingTable(org.elasticsearch.cluster.routing.RoutingTable) RelationName(io.crate.metadata.RelationName) IndexNotFoundException(org.elasticsearch.index.IndexNotFoundException) IndexMetadata(org.elasticsearch.cluster.metadata.IndexMetadata)

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