Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ignore_missing_component_templates config option #92436

Merged
merged 39 commits into from
Jan 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
866dbbd
Add `ignore_missing_component_tempaltes` config option
ruflin Dec 19, 2022
5aaa91b
Merge branch 'main' into ignore-missing-component-template
ruflin Jan 9, 2023
3befa9b
adjust from bool to ignore_missing_component_templates as an array
ruflin Jan 10, 2023
21ca694
update unit tests
ruflin Jan 10, 2023
d4a6c31
add one more constructor to keep old behaviour
ruflin Jan 10, 2023
5b8a46f
Merge branch 'main' into ignore-missing-component-template
ruflin Jan 10, 2023
ab46b1b
fix some unit tests
ruflin Jan 10, 2023
64e141f
add basic tests
ruflin Jan 16, 2023
1a18d50
remove not needed test
ruflin Jan 16, 2023
0512edf
Merge branch 'main' into ignore-missing-component-template
ruflin Jan 16, 2023
5dd2ee7
fixing test cases
ruflin Jan 16, 2023
c28b24b
add documentation
ruflin Jan 16, 2023
9c68539
add more docs
ruflin Jan 16, 2023
1cd0127
add code reviews for docs
ruflin Jan 16, 2023
29a34fb
add JSON to source blocks
ruflin Jan 16, 2023
71ae211
apply formatting rules
ruflin Jan 16, 2023
23559c0
fix tests, cleanup log message
ruflin Jan 16, 2023
0374941
Update docs/changelog/92436.yaml
ruflin Jan 17, 2023
91b1837
Update docs/reference/indices/ignore-missing-component-templates.asci…
ruflin Jan 18, 2023
d5ff03a
Update docs/reference/indices/ignore-missing-component-templates.asci…
ruflin Jan 18, 2023
54c4a3d
Update docs/reference/indices/ignore-missing-component-templates.asci…
ruflin Jan 18, 2023
84c4a57
update docs with review and add random tests
ruflin Jan 18, 2023
9e40dd3
add null test for component template
ruflin Jan 18, 2023
157ba01
add issue reference to changelog
ruflin Jan 18, 2023
7fa7ab8
add failing rest test
ruflin Jan 18, 2023
824be38
try another fix for the rest tests
ruflin Jan 18, 2023
1d84c00
push enhanced test to catch error and then check for index template
ruflin Jan 18, 2023
7a66bf3
fix changelog area entry
ruflin Jan 18, 2023
cc9cc5c
fix yaml test
ruflin Jan 18, 2023
1de49ca
Update rest-api-spec/src/yamlRestTest/resources/rest-api-spec/test/in…
ruflin Jan 30, 2023
973c49f
Update server/src/test/java/org/elasticsearch/cluster/metadata/Compos…
ruflin Jan 30, 2023
072c81a
add initial pr review cleanup
ruflin Jan 30, 2023
da7e66d
update more tests
ruflin Jan 30, 2023
cc824c5
remove comment from changelog and remove old exception code
ruflin Jan 30, 2023
2bb57f5
cleanup code / spotless
ruflin Jan 30, 2023
f12afe8
fix yaml test to use indices again
ruflin Jan 30, 2023
57a9ce9
Merge branch 'main' into ignore-missing-component-template
ruflin Jan 30, 2023
55ef4bc
fix import after merge
ruflin Jan 30, 2023
0f64578
Merge branch 'main' into ignore-missing-component-template
ruflin Jan 31, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions docs/changelog/92436.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
pr: 92436
summary: Add `ignore_missing_component_templates` config option
area: Indices APIs
type: enhancement
issues:
- 92426
95 changes: 95 additions & 0 deletions docs/reference/indices/ignore-missing-component-templates.asciidoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
[[ignore_missing_component_templates]]
== Config ignore_missing_component_templates

The configuration option `ignore_missing_component_templates` can be used when an index template references a component template that might not exist. Every time a data stream is created based on the index template, the existence of the component template will be checked. If it exists, it will used to form the index's composite settings. If it does not exist, it is ignored.

=== Usage example

In the following, one component template and an index template are created. The index template references two component templates, but only the `@package` one exists.


Create the component template `logs-foo_component1`. This has to be created before the index template as it is not optional:

[source,console]
----
PUT _component_template/logs-foo_component1
{
"template": {
"mappings": {
"properties": {
"host.name": {
"type": "keyword"
}
}
}
}
}
----

Next, the index template will be created and it references two component templates:

[source,JSON]
----
"composed_of": ["logs-foo_component1", "logs-foo_component2"]
----

Before, only the `logs-foo_component1` compontent template was created, meaning the `logs-foo_component2` is missing. Because of this the following entry was added to the config:

[source,JSON]
----
"ignore_missing_component_templates": ["logs-foo_component2"],
----

During creation of the template, it will not validate that `logs-foo_component2` exists:


[source,console]
----
PUT _index_template/logs-foo
{
"index_patterns": ["logs-foo-*"],
"data_stream": { },
"composed_of": ["logs-foo_component1", "logs-foo_component2"],
"ignore_missing_component_templates": ["logs-foo_component2"],
"priority": 500
}
----
// TEST[continued]

The index template `logs-foo` was successfully created. A data stream can be created based on this template:

[source,console]
----
PUT _data_stream/logs-foo-bar
----
// TEST[continued]

Looking at the mappings of the data stream, it will contain the `host.name` field.

At a later stage, the missing component template might be added:

[source,console]
----
PUT _component_template/logs-foo_component2
{
"template": {
"mappings": {
"properties": {
"host.ip": {
"type": "ip"
}
}
}
}
}
----
// TEST[continued]

This will not have an immediate effect on the data stream. The mapping `host.ip` will only show up in the data stream mappings when the data stream is rolled over automatically next time or a manual rollover is triggered:

[source,console]
----
POST logs-foo-bar/_rollover
----
// TEST[continued]
// TEST[teardown:data_stream_cleanup]
2 changes: 2 additions & 0 deletions docs/reference/indices/index-templates.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -161,3 +161,5 @@ DELETE _component_template/component_template1
////

include::simulate-multi-component-templates.asciidoc[]

include::ignore-missing-component-templates.asciidoc[]
Original file line number Diff line number Diff line change
Expand Up @@ -286,3 +286,66 @@

- is_false: purple-index.mappings.properties.nested.include_in_root
- is_true: purple-index.mappings.properties.nested.include_in_parent

---
"Index template ignore_missing_component_template valid":
- skip:
version: " - 8.6.99"
reason: "index template v2 ignore_missing_component_template config not available before 8.7"
features: allowed_warnings

- do:
cluster.put_component_template:
name: red
body:
template:
mappings:
properties:
foo:
type: keyword

- do:
allowed_warnings:
- "index template [blue] has index patterns [purple-index] matching patterns from existing older templates [global] with patterns (global => [*]); this template [blue] will take precedence during new index creation"
indices.put_index_template:
name: blue
body:
index_patterns: ["purple-index"]
composed_of: ["red", "blue"]
ignore_missing_component_templates: ["blue"]

- do:
indices.create:
index: purple-index

- do:
indices.get:
index: purple-index

- match: {purple-index.mappings.properties.foo: {type: keyword}}

---
"Index template ignore_missing_component_template invalid":
- skip:
version: " - 8.6.99"
reason: "index template v2 ignore_missing_component_template config not available before 8.7"
features: allowed_warnings

- do:
cluster.put_component_template:
name: red
body:
template:
mappings:
properties:
foo:
type: keyword

- do:
catch: /index_template \[blue\] invalid, cause \[index template \[blue\] specifies a missing component templates \[blue\] that does not exist/
indices.put_index_template:
name: blue
body:
index_patterns: ["purple-index"]
composed_of: ["red", "blue"]
ignore_missing_component_templates: ["foo"]
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
package org.elasticsearch.cluster.metadata;

import org.elasticsearch.TransportVersion;
import org.elasticsearch.Version;
import org.elasticsearch.cluster.Diff;
import org.elasticsearch.cluster.SimpleDiffable;
import org.elasticsearch.common.Strings;
Expand Down Expand Up @@ -46,6 +47,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 +61,8 @@ public class ComposableIndexTemplate implements SimpleDiffable<ComposableIndexTe
(Long) a[4],
(Map<String, Object>) a[5],
(DataStreamTemplate) a[6],
(Boolean) a[7]
(Boolean) a[7],
(List<String>) a[8]
)
);

Expand All @@ -72,6 +75,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.declareStringArray(ConstructingObjectParser.optionalConstructorArg(), IGNORE_MISSING_COMPONENT_TEMPLATES);
}

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

static Diff<ComposableIndexTemplate> readITV2DiffFrom(StreamInput in) throws IOException {
return SimpleDiffable.readDiffFrom(ComposableIndexTemplate::new, in);
Expand All @@ -106,7 +112,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, null);
}

public ComposableIndexTemplate(
Expand All @@ -118,7 +124,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, null);
}

public ComposableIndexTemplate(
Expand All @@ -130,6 +136,20 @@ public ComposableIndexTemplate(
@Nullable Map<String, Object> metadata,
@Nullable DataStreamTemplate dataStreamTemplate,
@Nullable Boolean allowAutoCreate
) {
this(indexPatterns, template, componentTemplates, priority, version, metadata, dataStreamTemplate, null, 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 Boolean allowAutoCreate,
@Nullable List<String> ignoreMissingComponentTemplates
) {
this.indexPatterns = indexPatterns;
this.template = template;
Expand All @@ -139,6 +159,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 +175,11 @@ public ComposableIndexTemplate(StreamInput in) throws IOException {
this.metadata = in.readMap();
this.dataStreamTemplate = in.readOptionalWriteable(DataStreamTemplate::new);
this.allowAutoCreate = in.readOptionalBoolean();
if (in.getVersion().onOrAfter(Version.V_8_7_0)) {
this.ignoreMissingComponentTemplates = in.readOptionalStringList();
} else {
this.ignoreMissingComponentTemplates = null;
}
}

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

@Nullable
public List<String> getIgnoreMissingComponentTemplates() {
return ignoreMissingComponentTemplates;
}

@Override
public void writeTo(StreamOutput out) throws IOException {
out.writeStringCollection(this.indexPatterns);
Expand All @@ -219,6 +250,9 @@ public void writeTo(StreamOutput out) throws IOException {
out.writeGenericMap(this.metadata);
out.writeOptionalWriteable(dataStreamTemplate);
out.writeOptionalBoolean(allowAutoCreate);
if (out.getVersion().onOrAfter(Version.V_8_7_0)) {
out.writeOptionalStringCollection(ignoreMissingComponentTemplates);
}
}

@Override
Expand Down Expand Up @@ -246,6 +280,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.stringListField(IGNORE_MISSING_COMPONENT_TEMPLATES.getPreferredName(), ignoreMissingComponentTemplates);
}
builder.endObject();
return builder;
}
Expand All @@ -260,7 +297,8 @@ public int hashCode() {
this.version,
this.metadata,
this.dataStreamTemplate,
this.allowAutoCreate
this.allowAutoCreate,
this.ignoreMissingComponentTemplates
);
}

Expand All @@ -280,7 +318,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 @@ -421,6 +460,7 @@ public static class Builder {
private Map<String, Object> metadata;
private DataStreamTemplate dataStreamTemplate;
private Boolean allowAutoCreate;
private List<String> ignoreMissingComponentTemplates;

public Builder() {}

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

public Builder ignoreMissingComponentTemplates(List<String> ignoreMissingComponentTemplates) {
this.ignoreMissingComponentTemplates = ignoreMissingComponentTemplates;
return this;
}

public ComposableIndexTemplate build() {
return new ComposableIndexTemplate(
this.indexPatterns,
Expand All @@ -473,7 +518,8 @@ public ComposableIndexTemplate build() {
this.version,
this.metadata,
this.dataStreamTemplate,
this.allowAutoCreate
this.allowAutoCreate,
this.ignoreMissingComponentTemplates
);
}
}
Expand Down
Loading