Skip to content

Commit

Permalink
Trigger index settings providers when updating component templates. (#…
Browse files Browse the repository at this point in the history
…91615) (#91792)

If index templates make use of the auto index.routing_path generation
and use component templates then making any chang to component templates
will fail. This commit addresses this, by changing the logic that
creates/updates component templates to use the index settings provider
when validating index templates that use the component templates being
updated.

Closes #91592
  • Loading branch information
martijnvg authored Nov 22, 2022
1 parent 952ffde commit e558161
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 31 deletions.
6 changes: 6 additions & 0 deletions docs/changelog/91615.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
pr: 91615
summary: Trigger index settings providers when updating component templates
area: Indices APIs
type: bug
issues:
- 91592
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import org.elasticsearch.common.time.FormatNames;
import org.elasticsearch.test.rest.ESRestTestCase;
import org.elasticsearch.test.rest.ObjectPath;
import org.junit.Before;

import java.io.IOException;
import java.time.Instant;
Expand All @@ -35,6 +36,14 @@

public class TsdbDataStreamRestIT extends ESRestTestCase {

private static final String COMPONENT_TEMPLATE = """
{
"template": {
"settings": {}
}
}
""";

private static final String TEMPLATE = """
{
"index_patterns": ["k8s*"],
Expand Down Expand Up @@ -97,6 +106,7 @@ public class TsdbDataStreamRestIT extends ESRestTestCase {
}
}
},
"composed_of": ["custom_template"],
"data_stream": {
}
}""";
Expand Down Expand Up @@ -190,12 +200,19 @@ public class TsdbDataStreamRestIT extends ESRestTestCase {
{"@timestamp": "$now", "metricset": "pod", "k8s": {"pod": {"name": "elephant", "uid":"df3145b3-0563-4d3b-a0f7-897eb2876eb4", "ip": "10.10.55.3", "network": {"tx": 1434595272, "rx": 530605511}}}}
""";

public void testTsdbDataStreams() throws Exception {
// Create a template
var putComposableIndexTemplateRequest = new Request("POST", "/_index_template/1");
putComposableIndexTemplateRequest.setJsonEntity(TEMPLATE);
assertOK(client().performRequest(putComposableIndexTemplateRequest));
@Before
public void setup() throws IOException {
// Add component template:
var request = new Request("POST", "/_component_template/custom_template");
request.setJsonEntity(COMPONENT_TEMPLATE);
assertOK(client().performRequest(request));
// Add composable index template
request = new Request("POST", "/_index_template/1");
request.setJsonEntity(TEMPLATE);
assertOK(client().performRequest(request));
}

public void testTsdbDataStreams() throws Exception {
var bulkRequest = new Request("POST", "/k8s/_bulk");
bulkRequest.setJsonEntity(BULK.replace("$now", formatInstant(Instant.now())));
bulkRequest.addParameter("refresh", "true");
Expand Down Expand Up @@ -331,10 +348,6 @@ public void testTsdbDataStreamsNanos() throws Exception {
}

public void testSimulateTsdbDataStreamTemplate() throws Exception {
var putComposableIndexTemplateRequest = new Request("POST", "/_index_template/1");
putComposableIndexTemplateRequest.setJsonEntity(TEMPLATE);
assertOK(client().performRequest(putComposableIndexTemplateRequest));

var simulateIndexTemplateRequest = new Request("POST", "/_index_template/_simulate_index/k8s");
var response = client().performRequest(simulateIndexTemplateRequest);
assertOK(response);
Expand All @@ -353,11 +366,6 @@ public void testSimulateTsdbDataStreamTemplate() throws Exception {
}

public void testSubsequentRollovers() throws Exception {
// Create a template
var putComposableIndexTemplateRequest = new Request("POST", "/_index_template/1");
putComposableIndexTemplateRequest.setJsonEntity(TEMPLATE);
assertOK(client().performRequest(putComposableIndexTemplateRequest));

var createDataStreamRequest = new Request("PUT", "/_data_stream/k8s");
assertOK(client().performRequest(createDataStreamRequest));

Expand Down Expand Up @@ -461,12 +469,6 @@ public void testMigrateRegularDataStreamToTsdbDataStream() throws Exception {
}

public void testChangeTemplateIndexMode() throws Exception {
// Create a template
{
var putComposableIndexTemplateRequest = new Request("POST", "/_index_template/1");
putComposableIndexTemplateRequest.setJsonEntity(TEMPLATE);
assertOK(client().performRequest(putComposableIndexTemplateRequest));
}
{
var indexRequest = new Request("POST", "/k8s/_doc");
var time = Instant.now();
Expand All @@ -489,6 +491,22 @@ public void testChangeTemplateIndexMode() throws Exception {
}
}

public void testUpdateComponentTemplateDoesNotFailIndexTemplateValidation() throws IOException {
var request = new Request("POST", "/_component_template/custom_template");
request.setJsonEntity("""
{
"template": {
"settings": {
"index": {
"number_of_replicas": 1
}
}
}
}
""");
client().performRequest(request);
}

private static Map<?, ?> getIndex(String indexName) throws IOException {
var getIndexRequest = new Request("GET", "/" + indexName + "?human");
var response = client().performRequest(getIndexRequest);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -309,14 +309,7 @@ public ClusterState addComponentTemplate(
final String composableTemplateName = entry.getKey();
final ComposableIndexTemplate composableTemplate = entry.getValue();
try {
validateCompositeTemplate(
tempStateWithComponentTemplateAdded,
composableTemplateName,
composableTemplate,
indicesService,
xContentRegistry,
systemIndices
);
validateIndexTemplateV2(composableTemplateName, composableTemplate, tempStateWithComponentTemplateAdded);
} catch (Exception e) {
if (validationFailure == null) {
validationFailure = new IllegalArgumentException(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1723,11 +1723,12 @@ public void testUpdateComponentTemplateFailsIfResolvedIndexTemplatesWouldBeInval
);

assertNotNull(e.getCause());
assertThat(e.getCause().getMessage(), containsString("invalid composite mappings for [my-template]"));

assertNotNull(e.getCause().getCause());
assertThat(e.getCause().getCause().getMessage(), containsString("invalid composite mappings for [my-template]"));

assertNotNull(e.getCause().getCause().getCause());
assertThat(
e.getCause().getCause().getMessage(),
e.getCause().getCause().getCause().getMessage(),
containsString("can't merge a non object mapping [field2] with an object mapping")
);
}
Expand Down

0 comments on commit e558161

Please sign in to comment.