Skip to content

Commit

Permalink
Template creation using context
Browse files Browse the repository at this point in the history
Signed-off-by: Mohit Godwani <[email protected]>
  • Loading branch information
mgodwan committed Jul 18, 2024
1 parent a2cef8f commit 5e9e426
Show file tree
Hide file tree
Showing 9 changed files with 310 additions and 33 deletions.
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()
);
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()
);
stateWithTemplate = indexTemplateService.addIndexTemplateV2(
state,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,13 @@ 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 Down
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 @@ -75,6 +75,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 @@ -87,7 +88,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 @@ -99,6 +101,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 @@ -114,6 +117,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 @@ -131,7 +136,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 @@ -142,6 +147,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 @@ -150,6 +168,7 @@ public ComposableIndexTemplate(
this.version = version;
this.metadata = metadata;
this.dataStreamTemplate = dataStreamTemplate;
this.context = context;
}

public ComposableIndexTemplate(StreamInput in) throws IOException {
Expand All @@ -164,6 +183,7 @@ public ComposableIndexTemplate(StreamInput in) throws IOException {
this.version = in.readOptionalVLong();
this.metadata = in.readMap();
this.dataStreamTemplate = in.readOptionalWriteable(DataStreamTemplate::new);
this.context = in.readOptionalWriteable(Context::new);
}

public List<String> indexPatterns() {
Expand Down Expand Up @@ -205,6 +225,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 @@ -219,6 +243,7 @@ public void writeTo(StreamOutput out) throws IOException {
out.writeOptionalVLong(this.version);
out.writeMap(this.metadata);
out.writeOptionalWriteable(dataStreamTemplate);
out.writeOptionalWriteable(context);
}

@Override
Expand All @@ -243,6 +268,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 Down
114 changes: 114 additions & 0 deletions server/src/main/java/org/opensearch/cluster/metadata/Context.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
/*
* SPDX-License-Identifier: Apache-2.0
*
* The OpenSearch Contributors require contributions made to
* this file be licensed under the Apache-2.0 license or a
* compatible open source license.
*/

package org.opensearch.cluster.metadata;

import org.opensearch.cluster.AbstractDiffable;
import org.opensearch.common.annotation.ExperimentalApi;
import org.opensearch.core.ParseField;
import org.opensearch.core.common.io.stream.StreamInput;
import org.opensearch.core.common.io.stream.StreamOutput;
import org.opensearch.core.xcontent.ConstructingObjectParser;
import org.opensearch.core.xcontent.ToXContentObject;
import org.opensearch.core.xcontent.XContentBuilder;
import org.opensearch.core.xcontent.XContentParser;

import java.io.IOException;
import java.util.Map;

/**
* Class encapsulating the context metadata associated with an index template/index.
*/
@ExperimentalApi
public class Context extends AbstractDiffable<ComposableIndexTemplate> implements ToXContentObject {

private static final ParseField NAME = new ParseField("name");
private static final ParseField VERSION = new ParseField("version");
private static final ParseField PARAMS = new ParseField("params");

public static final String LATEST_VERSION = "_latest";

private String name;
private String version = LATEST_VERSION;
private Map<String, Object> params;

public static final ConstructingObjectParser<Context, Void> PARSER = new ConstructingObjectParser<>(
"index_template",
false,
a -> new Context(
(String) a[0],
(String) a[1],
(Map<String, Object>) a[2]
)
);

static {
PARSER.declareString(ConstructingObjectParser.constructorArg(), NAME);
PARSER.declareString(ConstructingObjectParser.optionalConstructorArg(), VERSION);
PARSER.declareObject(ConstructingObjectParser.optionalConstructorArg(), (p, c) -> p.map(), PARAMS);
}

public Context(String name, String version, Map<String, Object> params) {
this.name = name;
this.version = version;
this.params = params;
}

public Context(StreamInput in) throws IOException {
this.name = in.readString();
this.version = in.readOptionalString();
this.params = in.readMap();
}

public String name() {
return name;
}

public void name(String name) {
this.name = name;
}

public String version() {
return version;
}

public void version(String version) {
this.version = version;
}

public Map<String, Object> params() {
return params;
}

public void params(Map<String, Object> params) {
this.params = params;
}

@Override
public void writeTo(StreamOutput out) throws IOException {
out.writeString(name);
out.writeOptionalString(version);
out.writeMap(params);
}

@Override
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
builder.startObject();
builder.field(NAME.getPreferredName(), this.name);
builder.field("version", this.version);
if (params != null) {
builder.field("params", this.params);
}
builder.endObject();
return builder;
}

public static Context fromXContent(XContentParser parser) {
return PARSER.apply(parser, null);
}
}
Loading

0 comments on commit 5e9e426

Please sign in to comment.