Skip to content

Commit

Permalink
Run TransportGetIndexTemplatesAction on local node (#119837)
Browse files Browse the repository at this point in the history
This action solely needs the cluster state, it can run on any node.
Additionally, it needs to be cancellable to avoid doing unnecessary work
after a client failure or timeout.

As a drive-by, this removes another usage of the trappy default master
node timeout.
  • Loading branch information
nielsbauman authored Jan 10, 2025
1 parent 65e4ec1 commit 80e8017
Show file tree
Hide file tree
Showing 27 changed files with 199 additions and 178 deletions.
5 changes: 5 additions & 0 deletions docs/changelog/119837.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
pr: 119837
summary: Run `TransportGetIndexTemplateAction` on local node
area: Indices APIs
type: enhancement
issues: []
6 changes: 3 additions & 3 deletions docs/reference/indices/get-index-template-v1.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
<titleabbrev>Get index template (legacy)</titleabbrev>
++++

IMPORTANT: This documentation is about legacy index templates,
which are deprecated and will be replaced by the composable templates introduced in {es} 7.8.
IMPORTANT: This documentation is about legacy index templates,
which are deprecated and will be replaced by the composable templates introduced in {es} 7.8.
For information about composable templates, see <<index-templates>>.

.New API reference
Expand Down Expand Up @@ -69,7 +69,7 @@ or use a value of `_all` or `*`.

include::{es-ref-dir}/rest-api/common-parms.asciidoc[tag=flat-settings]

include::{es-ref-dir}/rest-api/common-parms.asciidoc[tag=local]
include::{es-ref-dir}/rest-api/common-parms.asciidoc[tag=local-deprecated-9.0.0]

include::{es-ref-dir}/rest-api/common-parms.asciidoc[tag=master-timeout]

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import org.elasticsearch.action.admin.indices.recovery.RecoveryAction;
import org.elasticsearch.action.admin.indices.template.get.GetComponentTemplateAction;
import org.elasticsearch.action.admin.indices.template.get.GetComposableIndexTemplateAction;
import org.elasticsearch.action.admin.indices.template.get.GetIndexTemplatesAction;
import org.elasticsearch.action.support.CancellableActionTestPlugin;
import org.elasticsearch.action.support.PlainActionFuture;
import org.elasticsearch.action.support.RefCountingListener;
Expand Down Expand Up @@ -72,6 +73,10 @@ public void testGetComponentTemplateCancellation() {
runRestActionCancellationTest(new Request(HttpGet.METHOD_NAME, "/_component_template"), GetComponentTemplateAction.NAME);
}

public void testGetIndexTemplateCancellation() {
runRestActionCancellationTest(new Request(HttpGet.METHOD_NAME, "/_template"), GetIndexTemplatesAction.NAME);
}

public void testGetComposableTemplateCancellation() {
runRestActionCancellationTest(new Request(HttpGet.METHOD_NAME, "/_index_template"), GetComposableIndexTemplateAction.NAME);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,10 @@
},
"master_timeout":{
"type":"time",
"description":"Explicit operation timeout for connection to master node"
"description":"Timeout for waiting for new cluster state in case it is blocked"
},
"local":{
"deprecated":true,
"type":"boolean",
"description":"Return local information, do not retrieve the state from master node (default: false)"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,15 @@ setup:

---
"Test indices.exists_template with local flag":
- requires:
test_runner_features: ["allowed_warnings"]

- do:
indices.exists_template:
name: test
local: true
allowed_warnings:
- "the [?local] query parameter to this API has no effect, is now deprecated, and will be removed in a future version"

- is_false: ''

Original file line number Diff line number Diff line change
Expand Up @@ -62,14 +62,50 @@ setup:

---
"Get template with local flag":
- requires:
test_runner_features: ["allowed_warnings"]

- do:
indices.get_template:
name: test
local: true
allowed_warnings:
- "the [?local] query parameter to this API has no effect, is now deprecated, and will be removed in a future version"

- is_true: test

---
"Deprecated local parameter":
- requires:
capabilities:
- method: GET
path: /_template
capabilities: ["local_param_deprecated"]
test_runner_features: ["capabilities", "warnings"]
reason: Deprecation was implemented with capability

- do:
indices.get_template:
name: test
local: true
warnings:
- "the [?local] query parameter to this API has no effect, is now deprecated, and will be removed in a future version"

---
"Deprecated local parameter works in v8 compat mode":
- requires:
test_runner_features: ["headers"]

- do:
headers:
Content-Type: "application/vnd.elasticsearch+json;compatible-with=8"
Accept: "application/vnd.elasticsearch+json;compatible-with=8"
indices.get_template:
name: test
local: true

- exists: test.index_patterns

---
"Get template with flat settings and master timeout":

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,8 @@ public void testIndexTemplates() throws Exception {
ClusterStateResponse clusterStateResponseUnfiltered = clusterAdmin().prepareState(TEST_REQUEST_TIMEOUT).get();
assertThat(clusterStateResponseUnfiltered.getState().metadata().templates().size(), is(greaterThanOrEqualTo(2)));

GetIndexTemplatesResponse getIndexTemplatesResponse = indicesAdmin().prepareGetTemplates("foo_template").get();
GetIndexTemplatesResponse getIndexTemplatesResponse = indicesAdmin().prepareGetTemplates(TEST_REQUEST_TIMEOUT, "foo_template")
.get();
assertIndexTemplateExists(getIndexTemplatesResponse, "foo_template");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,9 @@ public void testTemplateUpdate() throws Exception {
// the updates only happen on cluster state updates, so we need to make sure that the cluster state updates are happening
// so we need to simulate updates to make sure the template upgrade kicks in
updateClusterSettings(Settings.builder().put(TestPlugin.UPDATE_TEMPLATE_DUMMY_SETTING.getKey(), updateCount.incrementAndGet()));
List<IndexTemplateMetadata> templates = indicesAdmin().prepareGetTemplates("test_*").get().getIndexTemplates();
List<IndexTemplateMetadata> templates = indicesAdmin().prepareGetTemplates(TEST_REQUEST_TIMEOUT, "test_*")
.get()
.getIndexTemplates();
assertThat(templates, hasSize(3));
boolean addedFound = false;
boolean changedFound = false;
Expand Down Expand Up @@ -139,7 +141,9 @@ private void assertTemplates() throws Exception {
// so we need to simulate updates to make sure the template upgrade kicks in
updateClusterSettings(Settings.builder().put(TestPlugin.UPDATE_TEMPLATE_DUMMY_SETTING.getKey(), updateCount.incrementAndGet()));

List<IndexTemplateMetadata> templates = indicesAdmin().prepareGetTemplates("test_*").get().getIndexTemplates();
List<IndexTemplateMetadata> templates = indicesAdmin().prepareGetTemplates(TEST_REQUEST_TIMEOUT, "test_*")
.get()
.getIndexTemplates();
assertThat(templates, hasSize(2));
boolean addedFound = false;
boolean changedFound = false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public void testIndexTemplatesWithBlocks() throws IOException {
try {
setClusterReadOnly(true);

GetIndexTemplatesResponse response = indicesAdmin().prepareGetTemplates("template_blocks").get();
GetIndexTemplatesResponse response = indicesAdmin().prepareGetTemplates(TEST_REQUEST_TIMEOUT, "template_blocks").get();
assertThat(response.getIndexTemplates(), hasSize(1));

assertBlocked(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public void testSimpleIndexTemplateTests() throws Exception {
indicesAdmin().prepareDeleteTemplate("*").get();

// check get all templates on an empty index.
GetIndexTemplatesResponse response = indicesAdmin().prepareGetTemplates().get();
GetIndexTemplatesResponse response = indicesAdmin().prepareGetTemplates(TEST_REQUEST_TIMEOUT).get();
assertThat(response.getIndexTemplates(), empty());

indicesAdmin().preparePutTemplate("template_1")
Expand Down Expand Up @@ -145,7 +145,7 @@ public void testSimpleIndexTemplateTests() throws Exception {
);
expectThrows(IllegalArgumentException.class, builder);

response = indicesAdmin().prepareGetTemplates().get();
response = indicesAdmin().prepareGetTemplates(TEST_REQUEST_TIMEOUT).get();
assertThat(response.getIndexTemplates(), hasSize(2));

// index something into test_index, will match on both templates
Expand Down Expand Up @@ -292,15 +292,16 @@ public void testThatGetIndexTemplatesWorks() throws Exception {
.get();

logger.info("--> get template template_1");
GetIndexTemplatesResponse getTemplate1Response = indicesAdmin().prepareGetTemplates("template_1").get();
GetIndexTemplatesResponse getTemplate1Response = indicesAdmin().prepareGetTemplates(TEST_REQUEST_TIMEOUT, "template_1").get();
assertThat(getTemplate1Response.getIndexTemplates(), hasSize(1));
assertThat(getTemplate1Response.getIndexTemplates().get(0), is(notNullValue()));
assertThat(getTemplate1Response.getIndexTemplates().get(0).patterns(), is(Collections.singletonList("te*")));
assertThat(getTemplate1Response.getIndexTemplates().get(0).getOrder(), is(0));
assertThat(getTemplate1Response.getIndexTemplates().get(0).getVersion(), is(123));

logger.info("--> get non-existing-template");
GetIndexTemplatesResponse getTemplate2Response = indicesAdmin().prepareGetTemplates("non-existing-template").get();
GetIndexTemplatesResponse getTemplate2Response = indicesAdmin().prepareGetTemplates(TEST_REQUEST_TIMEOUT, "non-existing-template")
.get();
assertThat(getTemplate2Response.getIndexTemplates(), hasSize(0));
}

Expand Down Expand Up @@ -375,7 +376,7 @@ public void testThatGetIndexTemplatesWithSimpleRegexWorks() throws Exception {
.get();

logger.info("--> get template template_*");
GetIndexTemplatesResponse getTemplate1Response = indicesAdmin().prepareGetTemplates("template_*").get();
GetIndexTemplatesResponse getTemplate1Response = indicesAdmin().prepareGetTemplates(TEST_REQUEST_TIMEOUT, "template_*").get();
assertThat(getTemplate1Response.getIndexTemplates(), hasSize(2));

List<String> templateNames = new ArrayList<>();
Expand All @@ -384,7 +385,7 @@ public void testThatGetIndexTemplatesWithSimpleRegexWorks() throws Exception {
assertThat(templateNames, containsInAnyOrder("template_1", "template_2"));

logger.info("--> get all templates");
getTemplate1Response = indicesAdmin().prepareGetTemplates("template*").get();
getTemplate1Response = indicesAdmin().prepareGetTemplates(TEST_REQUEST_TIMEOUT, "template*").get();
assertThat(getTemplate1Response.getIndexTemplates(), hasSize(3));

templateNames = new ArrayList<>();
Expand All @@ -394,7 +395,7 @@ public void testThatGetIndexTemplatesWithSimpleRegexWorks() throws Exception {
assertThat(templateNames, containsInAnyOrder("template_1", "template_2", "template3"));

logger.info("--> get templates template_1 and template_2");
getTemplate1Response = indicesAdmin().prepareGetTemplates("template_1", "template_2").get();
getTemplate1Response = indicesAdmin().prepareGetTemplates(TEST_REQUEST_TIMEOUT, "template_1", "template_2").get();
assertThat(getTemplate1Response.getIndexTemplates(), hasSize(2));

templateNames = new ArrayList<>();
Expand All @@ -419,7 +420,7 @@ public void testThatInvalidGetIndexTemplatesFails() throws Exception {

private void testExpectActionRequestValidationException(String... names) {
assertRequestBuilderThrows(
indicesAdmin().prepareGetTemplates(names),
indicesAdmin().prepareGetTemplates(TEST_REQUEST_TIMEOUT, names),
ActionRequestValidationException.class,
"get template with " + Arrays.toString(names)
);
Expand All @@ -430,7 +431,7 @@ public void testBrokenMapping() throws Exception {
indicesAdmin().prepareDeleteTemplate("*").get();

// check get all templates on an empty index.
GetIndexTemplatesResponse response = indicesAdmin().prepareGetTemplates().get();
GetIndexTemplatesResponse response = indicesAdmin().prepareGetTemplates(TEST_REQUEST_TIMEOUT).get();
assertThat(response.getIndexTemplates(), empty());

MapperParsingException e = expectThrows(
Expand All @@ -441,7 +442,7 @@ public void testBrokenMapping() throws Exception {
);
assertThat(e.getMessage(), containsString("Failed to parse mapping"));

response = indicesAdmin().prepareGetTemplates().get();
response = indicesAdmin().prepareGetTemplates(TEST_REQUEST_TIMEOUT).get();
assertThat(response.getIndexTemplates(), hasSize(0));
}

Expand All @@ -450,7 +451,7 @@ public void testInvalidSettings() throws Exception {
indicesAdmin().prepareDeleteTemplate("*").get();

// check get all templates on an empty index.
GetIndexTemplatesResponse response = indicesAdmin().prepareGetTemplates().get();
GetIndexTemplatesResponse response = indicesAdmin().prepareGetTemplates(TEST_REQUEST_TIMEOUT).get();
assertThat(response.getIndexTemplates(), empty());

IllegalArgumentException e = expectThrows(
Expand All @@ -465,7 +466,7 @@ public void testInvalidSettings() throws Exception {
e.getMessage()
);

response = indicesAdmin().prepareGetTemplates().get();
response = indicesAdmin().prepareGetTemplates(TEST_REQUEST_TIMEOUT).get();
assertEquals(0, response.getIndexTemplates().size());

createIndex("test");
Expand Down Expand Up @@ -597,7 +598,7 @@ public void testDuplicateAlias() throws Exception {
.addAlias(new Alias("my_alias").filter(termQuery("field", "value2")))
.get();

GetIndexTemplatesResponse response = indicesAdmin().prepareGetTemplates("template_1").get();
GetIndexTemplatesResponse response = indicesAdmin().prepareGetTemplates(TEST_REQUEST_TIMEOUT, "template_1").get();
assertThat(response.getIndexTemplates().size(), equalTo(1));
assertThat(response.getIndexTemplates().get(0).getAliases().size(), equalTo(1));
assertThat(response.getIndexTemplates().get(0).getAliases().get("my_alias").filter().string(), containsString("\"value1\""));
Expand All @@ -610,7 +611,7 @@ public void testAliasInvalidFilterValidJson() throws Exception {
.addAlias(new Alias("invalid_alias").filter("{ \"invalid\": {} }"))
.get();

GetIndexTemplatesResponse response = indicesAdmin().prepareGetTemplates("template_1").get();
GetIndexTemplatesResponse response = indicesAdmin().prepareGetTemplates(TEST_REQUEST_TIMEOUT, "template_1").get();
assertThat(response.getIndexTemplates().size(), equalTo(1));
assertThat(response.getIndexTemplates().get(0).getAliases().size(), equalTo(1));
assertThat(response.getIndexTemplates().get(0).getAliases().get("invalid_alias").filter().string(), equalTo("{\"invalid\":{}}"));
Expand All @@ -631,7 +632,7 @@ public void testAliasInvalidFilterInvalidJson() throws Exception {
);
assertThat(e.getMessage(), equalTo("failed to parse filter for alias [invalid_alias]"));

GetIndexTemplatesResponse response = indicesAdmin().prepareGetTemplates("template_1").get();
GetIndexTemplatesResponse response = indicesAdmin().prepareGetTemplates(TEST_REQUEST_TIMEOUT, "template_1").get();
assertThat(response.getIndexTemplates().size(), equalTo(0));
}

Expand Down Expand Up @@ -747,7 +748,7 @@ public void testCombineTemplates() throws Exception {
indicesAdmin().prepareDeleteTemplate("*").get();

// check get all templates on an empty index.
GetIndexTemplatesResponse response = indicesAdmin().prepareGetTemplates().get();
GetIndexTemplatesResponse response = indicesAdmin().prepareGetTemplates(TEST_REQUEST_TIMEOUT).get();
assertThat(response.getIndexTemplates(), empty());

// Now, a complete mapping with two separated templates is error
Expand Down Expand Up @@ -789,7 +790,7 @@ public void testCombineTemplates() throws Exception {
);
assertThat(e.getMessage(), containsString("analyzer [custom_1] has not been configured in mappings"));

response = indicesAdmin().prepareGetTemplates().get();
response = indicesAdmin().prepareGetTemplates(TEST_REQUEST_TIMEOUT).get();
assertThat(response.getIndexTemplates(), hasSize(1));

}
Expand All @@ -806,7 +807,7 @@ public void testOrderAndVersion() {
.setMapping("field", "type=text")
);

GetIndexTemplatesResponse response = indicesAdmin().prepareGetTemplates("versioned_template").get();
GetIndexTemplatesResponse response = indicesAdmin().prepareGetTemplates(TEST_REQUEST_TIMEOUT, "versioned_template").get();
assertThat(response.getIndexTemplates().size(), equalTo(1));
assertThat(response.getIndexTemplates().get(0).getVersion(), equalTo(version));
assertThat(response.getIndexTemplates().get(0).getOrder(), equalTo(order));
Expand Down Expand Up @@ -856,7 +857,7 @@ public void testPartitionedTemplate() throws Exception {
indicesAdmin().prepareDeleteTemplate("*").get();

// check get all templates on an empty index.
GetIndexTemplatesResponse response = indicesAdmin().prepareGetTemplates().get();
GetIndexTemplatesResponse response = indicesAdmin().prepareGetTemplates(TEST_REQUEST_TIMEOUT).get();
assertThat(response.getIndexTemplates(), empty());

// provide more partitions than shards
Expand All @@ -882,7 +883,7 @@ public void testPartitionedTemplate() throws Exception {
assertThat(eBadMapping.getMessage(), containsString("must have routing required for partitioned index"));

// no templates yet
response = indicesAdmin().prepareGetTemplates().get();
response = indicesAdmin().prepareGetTemplates(TEST_REQUEST_TIMEOUT).get();
assertEquals(0, response.getIndexTemplates().size());

// a valid configuration that only provides the partition size
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -424,7 +424,7 @@ public void testRestoreTemplates() throws Exception {

logger.info("--> delete test template");
assertThat(indicesAdmin().prepareDeleteTemplate("test-template").get().isAcknowledged(), equalTo(true));
GetIndexTemplatesResponse getIndexTemplatesResponse = indicesAdmin().prepareGetTemplates().get();
GetIndexTemplatesResponse getIndexTemplatesResponse = indicesAdmin().prepareGetTemplates(TEST_REQUEST_TIMEOUT).get();
assertIndexTemplateMissing(getIndexTemplatesResponse, "test-template");

logger.info("--> restore cluster state");
Expand All @@ -437,7 +437,7 @@ public void testRestoreTemplates() throws Exception {
assertThat(restoreSnapshotResponse.getRestoreInfo().totalShards(), equalTo(0));

logger.info("--> check that template is restored");
getIndexTemplatesResponse = indicesAdmin().prepareGetTemplates().get();
getIndexTemplatesResponse = indicesAdmin().prepareGetTemplates(TEST_REQUEST_TIMEOUT).get();
assertIndexTemplateExists(getIndexTemplatesResponse, "test-template");
}

Expand Down
Loading

0 comments on commit 80e8017

Please sign in to comment.