Skip to content

Commit

Permalink
Template creation using context (#14811)
Browse files Browse the repository at this point in the history
* Template creation using context

Signed-off-by: Mohit Godwani <[email protected]>
  • Loading branch information
mgodwan committed Jul 24, 2024
1 parent 46bd06b commit 7f924b8
Show file tree
Hide file tree
Showing 13 changed files with 861 additions and 43 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
- Enabling term version check on local state for all ClusterManager Read Transport Actions ([#14273](https://github.com/opensearch-project/OpenSearch/pull/14273))
- Optimize Cluster Stats Indices to precomute node level stats ([#14426](https://github.com/opensearch-project/OpenSearch/pull/14426))
- Create listener to refresh search thread resource usage ([#14832](https://github.com/opensearch-project/OpenSearch/pull/14832))
- Add logic to create index templates (v2) using context field ([#14811](https://github.com/opensearch-project/OpenSearch/pull/14811))

### Dependencies
- Update to Apache Lucene 9.11.1 ([#14042](https://github.com/opensearch-project/OpenSearch/pull/14042), [#14576](https://github.com/opensearch-project/OpenSearch/pull/14576))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,8 @@ protected void clusterManagerOperation(
MetadataIndexTemplateService.validateV2TemplateRequest(
state.metadata(),
simulateTemplateToAdd,
request.getIndexTemplateRequest().indexTemplate()
request.getIndexTemplateRequest().indexTemplate(),
clusterService.getClusterSettings()

Check warning on line 144 in server/src/main/java/org/opensearch/action/admin/indices/template/post/TransportSimulateIndexTemplateAction.java

View check run for this annotation

Codecov / codecov/patch

server/src/main/java/org/opensearch/action/admin/indices/template/post/TransportSimulateIndexTemplateAction.java#L143-L144

Added lines #L143 - L144 were not covered by tests
);
stateWithTemplate = indexTemplateService.addIndexTemplateV2(
state,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,8 @@ protected void clusterManagerOperation(
MetadataIndexTemplateService.validateV2TemplateRequest(
state.metadata(),
simulateTemplateToAdd,
request.getIndexTemplateRequest().indexTemplate()
request.getIndexTemplateRequest().indexTemplate(),
clusterService.getClusterSettings()

Check warning on line 138 in server/src/main/java/org/opensearch/action/admin/indices/template/post/TransportSimulateTemplateAction.java

View check run for this annotation

Codecov / codecov/patch

server/src/main/java/org/opensearch/action/admin/indices/template/post/TransportSimulateTemplateAction.java#L137-L138

Added lines #L137 - L138 were not covered by tests
);
stateWithTemplate = indexTemplateService.addIndexTemplateV2(
state,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

import org.opensearch.common.annotation.ExperimentalApi;

import java.util.Objects;

/**
* Metadata information about a template available in a template repository.
*/
Expand Down Expand Up @@ -48,13 +50,14 @@ public long version() {
* @return Metadata object based on name
*/
public static SystemTemplateMetadata fromComponentTemplate(String fullyQualifiedName) {
assert fullyQualifiedName.length() > 1 : "System template name must have at least one component";
assert fullyQualifiedName.substring(1, fullyQualifiedName.indexOf(DELIMITER, 1)).equals(COMPONENT_TEMPLATE_TYPE);
assert fullyQualifiedName.length() > DELIMITER.length() * 3 + 2 + COMPONENT_TEMPLATE_TYPE.length()
: "System template name must have all defined components";
assert (DELIMITER + fullyQualifiedName.substring(1, fullyQualifiedName.indexOf(DELIMITER, 1))).equals(COMPONENT_TEMPLATE_TYPE);

return new SystemTemplateMetadata(
Long.parseLong(fullyQualifiedName.substring(fullyQualifiedName.lastIndexOf(DELIMITER))),
Long.parseLong(fullyQualifiedName.substring(fullyQualifiedName.lastIndexOf(DELIMITER) + 1)),
COMPONENT_TEMPLATE_TYPE,
fullyQualifiedName.substring(0, fullyQualifiedName.lastIndexOf(DELIMITER))
fullyQualifiedName.substring(fullyQualifiedName.indexOf(DELIMITER, 2) + 1, fullyQualifiedName.lastIndexOf(DELIMITER))
);
}

Expand All @@ -65,4 +68,22 @@ public static SystemTemplateMetadata fromComponentTemplateInfo(String name, long
public final String fullyQualifiedName() {
return type + DELIMITER + name + DELIMITER + version;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
SystemTemplateMetadata that = (SystemTemplateMetadata) o;

Check warning on line 76 in server/src/main/java/org/opensearch/cluster/applicationtemplates/SystemTemplateMetadata.java

View check run for this annotation

Codecov / codecov/patch

server/src/main/java/org/opensearch/cluster/applicationtemplates/SystemTemplateMetadata.java#L76

Added line #L76 was not covered by tests
return version == that.version && Objects.equals(type, that.type) && Objects.equals(name, that.name);
}

@Override
public int hashCode() {
return Objects.hash(version, type, name);

Check warning on line 82 in server/src/main/java/org/opensearch/cluster/applicationtemplates/SystemTemplateMetadata.java

View check run for this annotation

Codecov / codecov/patch

server/src/main/java/org/opensearch/cluster/applicationtemplates/SystemTemplateMetadata.java#L82

Added line #L82 was not covered by tests
}

@Override
public String toString() {
return "SystemTemplateMetadata{" + "version=" + version + ", type='" + type + '\'' + ", name='" + name + '\'' + '}';

Check warning on line 87 in server/src/main/java/org/opensearch/cluster/applicationtemplates/SystemTemplateMetadata.java

View check run for this annotation

Codecov / codecov/patch

server/src/main/java/org/opensearch/cluster/applicationtemplates/SystemTemplateMetadata.java#L87

Added line #L87 was not covered by tests
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ void refreshTemplates(boolean verification) {
int failedLoadingRepositories = 0;
List<Exception> exceptions = new ArrayList<>();

if (loaded.compareAndSet(false, true) && enabledTemplates) {
if ((verification || loaded.compareAndSet(false, true)) && enabledTemplates) {
for (SystemTemplatesPlugin plugin : systemTemplatesPluginList) {
try (SystemTemplateRepository repository = plugin.loadRepository()) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

import org.opensearch.common.annotation.ExperimentalApi;

import java.util.Objects;

/**
* The information to uniquely identify a template repository.
*/
Expand All @@ -31,4 +33,22 @@ public String id() {
public long version() {
return version;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
TemplateRepositoryMetadata that = (TemplateRepositoryMetadata) o;

Check warning on line 41 in server/src/main/java/org/opensearch/cluster/applicationtemplates/TemplateRepositoryMetadata.java

View check run for this annotation

Codecov / codecov/patch

server/src/main/java/org/opensearch/cluster/applicationtemplates/TemplateRepositoryMetadata.java#L41

Added line #L41 was not covered by tests
return version == that.version && Objects.equals(id, that.id);
}

@Override
public int hashCode() {
return Objects.hash(id, version);

Check warning on line 47 in server/src/main/java/org/opensearch/cluster/applicationtemplates/TemplateRepositoryMetadata.java

View check run for this annotation

Codecov / codecov/patch

server/src/main/java/org/opensearch/cluster/applicationtemplates/TemplateRepositoryMetadata.java#L47

Added line #L47 was not covered by tests
}

@Override
public String toString() {
return "TemplateRepositoryMetadata{" + "id='" + id + '\'' + ", version=" + version + '}';

Check warning on line 52 in server/src/main/java/org/opensearch/cluster/applicationtemplates/TemplateRepositoryMetadata.java

View check run for this annotation

Codecov / codecov/patch

server/src/main/java/org/opensearch/cluster/applicationtemplates/TemplateRepositoryMetadata.java#L52

Added line #L52 was not covered by tests
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
import org.opensearch.cluster.ClusterName;
import org.opensearch.cluster.ClusterState;
import org.opensearch.cluster.Diff;
import org.opensearch.cluster.metadata.ComponentTemplateMetadata;
import org.opensearch.cluster.metadata.DataStreamMetadata;
import org.opensearch.cluster.metadata.Metadata;
import org.opensearch.common.collect.Tuple;
Expand Down Expand Up @@ -94,9 +95,10 @@ public abstract class OpenSearchNodeCommand extends EnvironmentAwareCommand {
public <T, C> T parseNamedObject(Class<T> categoryClass, String name, XContentParser parser, C context) throws IOException {
// Currently, two unknown top-level objects are present
if (Metadata.Custom.class.isAssignableFrom(categoryClass)) {
if (DataStreamMetadata.TYPE.equals(name)) {
if (DataStreamMetadata.TYPE.equals(name) || ComponentTemplateMetadata.TYPE.equals(name)) {
// DataStreamMetadata is used inside Metadata class for validation purposes and building the indicesLookup,
// therefor even es node commands need to be able to parse it.
// ComponentTemplateMetadata is used inside Metadata class for building the systemTemplatesLookup,
// therefor even OpenSearch node commands need to be able to parse it.
return super.parseNamedObject(categoryClass, name, parser, context);
// TODO: Try to parse other named objects (e.g. stored scripts, ingest pipelines) that are part of core es as well?
// Note that supporting PersistentTasksCustomMetadata is trickier, because PersistentTaskParams is a named object too.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ public class ComposableIndexTemplate extends AbstractDiffable<ComposableIndexTem
private static final ParseField VERSION = new ParseField("version");
private static final ParseField METADATA = new ParseField("_meta");
private static final ParseField DATA_STREAM = new ParseField("data_stream");
private static final ParseField CONTEXT = new ParseField("context");

@SuppressWarnings("unchecked")
public static final ConstructingObjectParser<ComposableIndexTemplate, Void> PARSER = new ConstructingObjectParser<>(
Expand All @@ -89,7 +90,8 @@ public class ComposableIndexTemplate extends AbstractDiffable<ComposableIndexTem
(Long) a[3],
(Long) a[4],
(Map<String, Object>) a[5],
(DataStreamTemplate) a[6]
(DataStreamTemplate) a[6],
(Context) a[7]
)
);

Expand All @@ -101,6 +103,7 @@ public class ComposableIndexTemplate extends AbstractDiffable<ComposableIndexTem
PARSER.declareLong(ConstructingObjectParser.optionalConstructorArg(), VERSION);
PARSER.declareObject(ConstructingObjectParser.optionalConstructorArg(), (p, c) -> p.map(), METADATA);
PARSER.declareObject(ConstructingObjectParser.optionalConstructorArg(), DataStreamTemplate.PARSER, DATA_STREAM);
PARSER.declareObject(ConstructingObjectParser.optionalConstructorArg(), Context.PARSER, CONTEXT);
}

private final List<String> indexPatterns;
Expand All @@ -116,6 +119,8 @@ public class ComposableIndexTemplate extends AbstractDiffable<ComposableIndexTem
private final Map<String, Object> metadata;
@Nullable
private final DataStreamTemplate dataStreamTemplate;
@Nullable
private final Context context;

static Diff<ComposableIndexTemplate> readITV2DiffFrom(StreamInput in) throws IOException {
return AbstractDiffable.readDiffFrom(ComposableIndexTemplate::new, in);
Expand All @@ -133,7 +138,7 @@ public ComposableIndexTemplate(
@Nullable Long version,
@Nullable Map<String, Object> metadata
) {
this(indexPatterns, template, componentTemplates, priority, version, metadata, null);
this(indexPatterns, template, componentTemplates, priority, version, metadata, null, null);
}

public ComposableIndexTemplate(
Expand All @@ -144,6 +149,19 @@ public ComposableIndexTemplate(
@Nullable Long version,
@Nullable Map<String, Object> metadata,
@Nullable DataStreamTemplate dataStreamTemplate
) {
this(indexPatterns, template, componentTemplates, priority, version, metadata, dataStreamTemplate, null);
}

public ComposableIndexTemplate(
List<String> indexPatterns,
@Nullable Template template,
@Nullable List<String> componentTemplates,
@Nullable Long priority,
@Nullable Long version,
@Nullable Map<String, Object> metadata,
@Nullable DataStreamTemplate dataStreamTemplate,
@Nullable Context context
) {
this.indexPatterns = indexPatterns;
this.template = template;
Expand All @@ -152,6 +170,7 @@ public ComposableIndexTemplate(
this.version = version;
this.metadata = metadata;
this.dataStreamTemplate = dataStreamTemplate;
this.context = context;
}

public ComposableIndexTemplate(StreamInput in) throws IOException {
Expand All @@ -170,6 +189,11 @@ public ComposableIndexTemplate(StreamInput in) throws IOException {
} else {
this.dataStreamTemplate = null;
}
if (in.getVersion().onOrAfter(Version.V_2_16_0)) {
this.context = in.readOptionalWriteable(Context::new);
} else {
this.context = null;

Check warning on line 195 in server/src/main/java/org/opensearch/cluster/metadata/ComposableIndexTemplate.java

View check run for this annotation

Codecov / codecov/patch

server/src/main/java/org/opensearch/cluster/metadata/ComposableIndexTemplate.java#L195

Added line #L195 was not covered by tests
}
}

public List<String> indexPatterns() {
Expand Down Expand Up @@ -211,6 +235,10 @@ public DataStreamTemplate getDataStreamTemplate() {
return dataStreamTemplate;
}

public Context context() {
return context;
}

@Override
public void writeTo(StreamOutput out) throws IOException {
out.writeStringCollection(this.indexPatterns);
Expand All @@ -227,6 +255,9 @@ public void writeTo(StreamOutput out) throws IOException {
if (out.getVersion().onOrAfter(LegacyESVersion.V_7_9_0)) {
out.writeOptionalWriteable(dataStreamTemplate);
}
if (out.getVersion().onOrAfter(Version.V_2_16_0)) {
out.writeOptionalWriteable(context);
}
}

@Override
Expand All @@ -251,6 +282,9 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws
if (this.dataStreamTemplate != null) {
builder.field(DATA_STREAM.getPreferredName(), dataStreamTemplate);
}
if (this.context != null) {
builder.field(CONTEXT.getPreferredName(), context);
}
builder.endObject();
return builder;
}
Expand All @@ -264,7 +298,8 @@ public int hashCode() {
this.priority,
this.version,
this.metadata,
this.dataStreamTemplate
this.dataStreamTemplate,
this.context
);
}

Expand All @@ -283,7 +318,8 @@ public boolean equals(Object obj) {
&& Objects.equals(this.priority, other.priority)
&& Objects.equals(this.version, other.version)
&& Objects.equals(this.metadata, other.metadata)
&& Objects.equals(this.dataStreamTemplate, other.dataStreamTemplate);
&& Objects.equals(this.dataStreamTemplate, other.dataStreamTemplate)
&& Objects.equals(this.context, other.context);
}

@Override
Expand Down
Loading

0 comments on commit 7f924b8

Please sign in to comment.