From 2789fe4179021335393176c1011befe74607c8df Mon Sep 17 00:00:00 2001 From: Lee Hinman Date: Thu, 12 Mar 2020 15:33:32 -0600 Subject: [PATCH] [7.x] Add ComponentTemplate to MetaData (#53290) (#53489) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Add ComponentTemplate to MetaData (#53290) * Add ComponentTemplate to MetaData This adds a `ComponentTemplate` datastructure that will be used as part of #53101 (Index Templates v2) to the `MetaData` class. Currently there are no APIs for interacting with this class, so it will always be an empty map (other than in tests). This infrastructure will be built upon to add APIs in a subsequent commit. A `ComponentTemplate` is made up of a `Template`, a version, and a MetaData.Custom class. The `Template` contains similar information to an `IndexTemplateMetaData` object— settings, mappings, and alias configuration. * Update minimal supported version constant Co-authored-by: Elastic Machine --- .../elasticsearch/cluster/ClusterModule.java | 5 + .../cluster/metadata/ComponentTemplate.java | 300 ++++++++++++++++++ .../metadata/ComponentTemplateMetadata.java | 169 ++++++++++ .../cluster/metadata/MetaData.java | 35 ++ .../ComponentTemplateMetadataTests.java | 70 ++++ .../metadata/ComponentTemplateTests.java | 163 ++++++++++ .../cluster/metadata/MetaDataTests.java | 36 ++- .../metadata/ToAndFromJsonMetaDataTests.java | 16 + 8 files changed, 793 insertions(+), 1 deletion(-) create mode 100644 server/src/main/java/org/elasticsearch/cluster/metadata/ComponentTemplate.java create mode 100644 server/src/main/java/org/elasticsearch/cluster/metadata/ComponentTemplateMetadata.java create mode 100644 server/src/test/java/org/elasticsearch/cluster/metadata/ComponentTemplateMetadataTests.java create mode 100644 server/src/test/java/org/elasticsearch/cluster/metadata/ComponentTemplateTests.java diff --git a/server/src/main/java/org/elasticsearch/cluster/ClusterModule.java b/server/src/main/java/org/elasticsearch/cluster/ClusterModule.java index 99428aaad87..1f134e5d9e6 100644 --- a/server/src/main/java/org/elasticsearch/cluster/ClusterModule.java +++ b/server/src/main/java/org/elasticsearch/cluster/ClusterModule.java @@ -22,6 +22,7 @@ package org.elasticsearch.cluster; import org.elasticsearch.cluster.action.index.MappingUpdatedAction; import org.elasticsearch.cluster.action.index.NodeMappingRefreshAction; import org.elasticsearch.cluster.action.shard.ShardStateAction; +import org.elasticsearch.cluster.metadata.ComponentTemplateMetadata; import org.elasticsearch.cluster.metadata.IndexGraveyard; import org.elasticsearch.cluster.metadata.IndexNameExpressionResolver; import org.elasticsearch.cluster.metadata.MetaData; @@ -130,6 +131,8 @@ public class ClusterModule extends AbstractModule { registerMetaDataCustom(entries, IndexGraveyard.TYPE, IndexGraveyard::new, IndexGraveyard::readDiffFrom); registerMetaDataCustom(entries, PersistentTasksCustomMetaData.TYPE, PersistentTasksCustomMetaData::new, PersistentTasksCustomMetaData::readDiffFrom); + registerMetaDataCustom(entries, ComponentTemplateMetadata.TYPE, ComponentTemplateMetadata::new, + ComponentTemplateMetadata::readDiffFrom); // Task Status (not Diffable) entries.add(new Entry(Task.Status.class, PersistentTasksNodeService.Status.NAME, PersistentTasksNodeService.Status::new)); return entries; @@ -177,6 +180,8 @@ public class ClusterModule extends AbstractModule { IndexGraveyard::fromXContent)); entries.add(new NamedXContentRegistry.Entry(MetaData.Custom.class, new ParseField(PersistentTasksCustomMetaData.TYPE), PersistentTasksCustomMetaData::fromXContent)); + entries.add(new NamedXContentRegistry.Entry(MetaData.Custom.class, new ParseField(ComponentTemplateMetadata.TYPE), + ComponentTemplateMetadata::fromXContent)); return entries; } diff --git a/server/src/main/java/org/elasticsearch/cluster/metadata/ComponentTemplate.java b/server/src/main/java/org/elasticsearch/cluster/metadata/ComponentTemplate.java new file mode 100644 index 00000000000..2b160b1b2fb --- /dev/null +++ b/server/src/main/java/org/elasticsearch/cluster/metadata/ComponentTemplate.java @@ -0,0 +1,300 @@ +/* + * Licensed to Elasticsearch under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.elasticsearch.cluster.metadata; + +import org.elasticsearch.cluster.AbstractDiffable; +import org.elasticsearch.cluster.Diff; +import org.elasticsearch.common.Nullable; +import org.elasticsearch.common.ParseField; +import org.elasticsearch.common.Strings; +import org.elasticsearch.common.bytes.BytesArray; +import org.elasticsearch.common.compress.CompressedXContent; +import org.elasticsearch.common.io.stream.StreamInput; +import org.elasticsearch.common.io.stream.StreamOutput; +import org.elasticsearch.common.settings.Settings; +import org.elasticsearch.common.xcontent.ConstructingObjectParser; +import org.elasticsearch.common.xcontent.ToXContentObject; +import org.elasticsearch.common.xcontent.XContentBuilder; +import org.elasticsearch.common.xcontent.XContentFactory; +import org.elasticsearch.common.xcontent.XContentHelper; +import org.elasticsearch.common.xcontent.XContentParser; +import org.elasticsearch.common.xcontent.XContentType; + +import java.io.IOException; +import java.util.HashMap; +import java.util.Map; +import java.util.Objects; + +/** + * A component template is a re-usable template as well as metadata about the template. Each + * component template is expected to be valid on its own. For example, if a component template + * contains a field "foo", it's expected to contain all the necessary settings/mappings/etc for the + * "foo" field. These component templates make up the individual pieces composing an index template. + */ +public class ComponentTemplate extends AbstractDiffable implements ToXContentObject { + private static final ParseField TEMPLATE = new ParseField("template"); + private static final ParseField VERSION = new ParseField("version"); + private static final ParseField METADATA = new ParseField("_meta"); + + @SuppressWarnings("unchecked") + private static final ConstructingObjectParser PARSER = + new ConstructingObjectParser<>("component_template", false, + a -> new ComponentTemplate((Template) a[0], (Long) a[1], (Map) a[2])); + + static { + PARSER.declareObject(ConstructingObjectParser.constructorArg(), Template.PARSER, TEMPLATE); + PARSER.declareLong(ConstructingObjectParser.optionalConstructorArg(), VERSION); + PARSER.declareObject(ConstructingObjectParser.optionalConstructorArg(), (p, c) -> p.map(), METADATA); + } + + private final Template template; + @Nullable + private final Long version; + @Nullable + private final Map metadata; + + static Diff readComponentTemplateDiffFrom(StreamInput in) throws IOException { + return AbstractDiffable.readDiffFrom(ComponentTemplate::new, in); + } + + public static ComponentTemplate parse(XContentParser parser) { + return PARSER.apply(parser, null); + } + + public ComponentTemplate(Template template, @Nullable Long version, @Nullable Map metadata) { + this.template = template; + this.version = version; + this.metadata = metadata; + } + + public ComponentTemplate(StreamInput in) throws IOException { + this.template = new Template(in); + this.version = in.readOptionalVLong(); + if (in.readBoolean()) { + this.metadata = in.readMap(); + } else { + this.metadata = null; + } + } + + public Template template() { + return template; + } + + @Nullable + public Long version() { + return version; + } + + @Nullable + public Map metadata() { + return metadata; + } + + @Override + public void writeTo(StreamOutput out) throws IOException { + this.template.writeTo(out); + out.writeOptionalVLong(this.version); + if (this.metadata == null) { + out.writeBoolean(false); + } else { + out.writeBoolean(true); + out.writeMap(this.metadata); + } + } + + @Override + public int hashCode() { + return Objects.hash(template, version, metadata); + } + + @Override + public boolean equals(Object obj) { + if (obj == null) { + return false; + } + if (obj.getClass() != getClass()) { + return false; + } + ComponentTemplate other = (ComponentTemplate) obj; + return Objects.equals(template, other.template) && + Objects.equals(version, other.version) && + Objects.equals(metadata, other.metadata); + } + + @Override + public String toString() { + return Strings.toString(this); + } + + @Override + public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { + builder.startObject(); + builder.field(TEMPLATE.getPreferredName(), this.template); + if (this.version != null) { + builder.field(VERSION.getPreferredName(), this.version); + } + if (this.metadata != null) { + builder.field(METADATA.getPreferredName(), this.metadata); + } + builder.endObject(); + return builder; + } + + static class Template extends AbstractDiffable