Skip to content

Commit

Permalink
Add ignore_missing_component_tempaltes config option
Browse files Browse the repository at this point in the history
As described in elastic#92426 a config option to not ignore missing component templates is needed. This introduces the config option `ignore_missing_component_templates`. If set to true, missing component templates are ignored. If set to false or not set at all, the existing behaviour applies.

This is currently a draft PR as it only contains the functionality. It still needs tests and docs. Goal is to get an initial set of opinions on it.
  • Loading branch information
ruflin committed Dec 19, 2022
1 parent a852c90 commit 866dbbd
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ public class ComposableIndexTemplate implements SimpleDiffable<ComposableIndexTe
private static final ParseField METADATA = new ParseField("_meta");
private static final ParseField DATA_STREAM = new ParseField("data_stream");
private static final ParseField ALLOW_AUTO_CREATE = new ParseField("allow_auto_create");
private static final ParseField IGNORE_MISSING_COMPONENT_TEMPLATES = new ParseField("ignore_missing_component_templates");

@SuppressWarnings("unchecked")
public static final ConstructingObjectParser<ComposableIndexTemplate, Void> PARSER = new ConstructingObjectParser<>(
Expand All @@ -59,7 +60,8 @@ public class ComposableIndexTemplate implements SimpleDiffable<ComposableIndexTe
(Long) a[4],
(Map<String, Object>) a[5],
(DataStreamTemplate) a[6],
(Boolean) a[7]
(Boolean) a[7],
(Boolean) a[8]
)
);

Expand All @@ -72,6 +74,7 @@ public class ComposableIndexTemplate implements SimpleDiffable<ComposableIndexTe
PARSER.declareObject(ConstructingObjectParser.optionalConstructorArg(), (p, c) -> p.map(), METADATA);
PARSER.declareObject(ConstructingObjectParser.optionalConstructorArg(), DataStreamTemplate.PARSER, DATA_STREAM);
PARSER.declareBoolean(ConstructingObjectParser.optionalConstructorArg(), ALLOW_AUTO_CREATE);
PARSER.declareBoolean(ConstructingObjectParser.optionalConstructorArg(), IGNORE_MISSING_COMPONENT_TEMPLATES);
}

private final List<String> indexPatterns;
Expand All @@ -89,6 +92,8 @@ public class ComposableIndexTemplate implements SimpleDiffable<ComposableIndexTe
private final DataStreamTemplate dataStreamTemplate;
@Nullable
private final Boolean allowAutoCreate;
@Nullable
private final Boolean ignoreMissingComponentTemplates;

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

public ComposableIndexTemplate(
Expand All @@ -118,7 +123,7 @@ public ComposableIndexTemplate(
@Nullable Map<String, Object> metadata,
@Nullable DataStreamTemplate dataStreamTemplate
) {
this(indexPatterns, template, componentTemplates, priority, version, metadata, dataStreamTemplate, null);
this(indexPatterns, template, componentTemplates, priority, version, metadata, dataStreamTemplate, null, false);
}

public ComposableIndexTemplate(
Expand All @@ -129,7 +134,8 @@ public ComposableIndexTemplate(
@Nullable Long version,
@Nullable Map<String, Object> metadata,
@Nullable DataStreamTemplate dataStreamTemplate,
@Nullable Boolean allowAutoCreate
@Nullable Boolean allowAutoCreate,
@Nullable Boolean ignoreMissingComponentTemplates
) {
this.indexPatterns = indexPatterns;
this.template = template;
Expand All @@ -139,6 +145,7 @@ public ComposableIndexTemplate(
this.metadata = metadata;
this.dataStreamTemplate = dataStreamTemplate;
this.allowAutoCreate = allowAutoCreate;
this.ignoreMissingComponentTemplates = ignoreMissingComponentTemplates;
}

public ComposableIndexTemplate(StreamInput in) throws IOException {
Expand All @@ -154,6 +161,7 @@ public ComposableIndexTemplate(StreamInput in) throws IOException {
this.metadata = in.readMap();
this.dataStreamTemplate = in.readOptionalWriteable(DataStreamTemplate::new);
this.allowAutoCreate = in.readOptionalBoolean();
this.ignoreMissingComponentTemplates = in.readOptionalBoolean();
}

public List<String> indexPatterns() {
Expand Down Expand Up @@ -204,6 +212,15 @@ public Boolean getAllowAutoCreate() {
return this.allowAutoCreate;
}

@Nullable
public Boolean getIgnoreMissingComponentTemplates() {
// If value is not set, ignore_missing_component_templates is disabled
if (this.ignoreMissingComponentTemplates == null) {
return false;
}
return this.ignoreMissingComponentTemplates;
}

@Override
public void writeTo(StreamOutput out) throws IOException {
out.writeStringCollection(this.indexPatterns);
Expand All @@ -219,6 +236,7 @@ public void writeTo(StreamOutput out) throws IOException {
out.writeGenericMap(this.metadata);
out.writeOptionalWriteable(dataStreamTemplate);
out.writeOptionalBoolean(allowAutoCreate);
out.writeOptionalBoolean(ignoreMissingComponentTemplates);
}

@Override
Expand Down Expand Up @@ -246,6 +264,9 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws
if (this.allowAutoCreate != null) {
builder.field(ALLOW_AUTO_CREATE.getPreferredName(), allowAutoCreate);
}
if (this.ignoreMissingComponentTemplates != null) {
builder.field(IGNORE_MISSING_COMPONENT_TEMPLATES.getPreferredName(), ignoreMissingComponentTemplates);
}
builder.endObject();
return builder;
}
Expand All @@ -260,7 +281,8 @@ public int hashCode() {
this.version,
this.metadata,
this.dataStreamTemplate,
this.allowAutoCreate
this.allowAutoCreate,
this.ignoreMissingComponentTemplates
);
}

Expand All @@ -280,7 +302,8 @@ && componentTemplatesEquals(this.componentTemplates, other.componentTemplates)
&& Objects.equals(this.version, other.version)
&& Objects.equals(this.metadata, other.metadata)
&& Objects.equals(this.dataStreamTemplate, other.dataStreamTemplate)
&& Objects.equals(this.allowAutoCreate, other.allowAutoCreate);
&& Objects.equals(this.allowAutoCreate, other.allowAutoCreate)
&& Objects.equals(this.ignoreMissingComponentTemplates, other.ignoreMissingComponentTemplates);
}

static boolean componentTemplatesEquals(List<String> c1, List<String> c2) {
Expand Down Expand Up @@ -420,6 +443,7 @@ public static class Builder {
private Map<String, Object> metadata;
private DataStreamTemplate dataStreamTemplate;
private Boolean allowAutoCreate;
private Boolean ignoreMissingComponentTemplates;

public Builder() {}

Expand Down Expand Up @@ -463,6 +487,11 @@ public Builder allowAutoCreate(Boolean allowAutoCreate) {
return this;
}

public Builder ignoreMissingComponentTemplates(Boolean ignoreMissingComponentTemplates) {
this.ignoreMissingComponentTemplates = ignoreMissingComponentTemplates;
return this;
}

public ComposableIndexTemplate build() {
return new ComposableIndexTemplate(
this.indexPatterns,
Expand All @@ -472,7 +501,8 @@ public ComposableIndexTemplate build() {
this.version,
this.metadata,
this.dataStreamTemplate,
this.allowAutoCreate
this.allowAutoCreate,
this.ignoreMissingComponentTemplates
);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -511,7 +511,8 @@ public static void validateV2TemplateRequest(Metadata metadata, String name, Com
.filter(componentTemplate -> componentTemplates.containsKey(componentTemplate) == false)
.toList();

if (missingComponentTemplates.size() > 0) {
// Validates that all component templates exist in case ignore_missing_component_templates is not set
if (template.getIgnoreMissingComponentTemplates() == false && missingComponentTemplates.size() > 0) {
throw new InvalidIndexTemplateException(
name,
"index template [" + name + "] specifies component templates " + missingComponentTemplates + " that do not exist"
Expand Down Expand Up @@ -580,7 +581,8 @@ public ClusterState addIndexTemplateV2(
template.version(),
template.metadata(),
template.getDataStreamTemplate(),
template.getAllowAutoCreate()
template.getAllowAutoCreate(),
template.getIgnoreMissingComponentTemplates()
);
}

Expand Down Expand Up @@ -680,7 +682,8 @@ private void validateIndexTemplateV2(String name, ComposableIndexTemplate indexT
indexTemplate.version(),
indexTemplate.metadata(),
indexTemplate.getDataStreamTemplate(),
indexTemplate.getAllowAutoCreate()
indexTemplate.getAllowAutoCreate(),
indexTemplate.getIgnoreMissingComponentTemplates()
);

validate(name, templateToValidate);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -693,6 +693,7 @@ static List<String> migrateComposableTemplates(Metadata.Builder mb, ClusterState
migratedComposableTemplateBuilder.metadata(composableTemplate.metadata());
migratedComposableTemplateBuilder.dataStreamTemplate(composableTemplate.getDataStreamTemplate());
migratedComposableTemplateBuilder.allowAutoCreate(composableTemplate.getAllowAutoCreate());
migratedComposableTemplateBuilder.ignoreMissingComponentTemplates(composableTemplate.getIgnoreMissingComponentTemplates());

mb.put(templateEntry.getKey(), migratedComposableTemplateBuilder.build());
migratedComposableTemplates.add(templateEntry.getKey());
Expand Down

0 comments on commit 866dbbd

Please sign in to comment.