Skip to content

Commit 0df6e9e

Browse files
opensearch-trigger-bot[bot]github-actions[bot]reta
authored
[Backport 2.x] Fix delete index template failed when the index template matches a data stream but is unused (#15098)
* Fix delete index template failed when the index template matches a data stream but is unused (#15080) * Fix delete not-using index template failed when the index pattern matches a data stream Signed-off-by: Gao Binlong <[email protected]> * modify change log Signed-off-by: Gao Binlong <[email protected]> * Fix version check Signed-off-by: Gao Binlong <[email protected]> --------- Signed-off-by: Gao Binlong <[email protected]> (cherry picked from commit a9d09aa) Signed-off-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com> * Update 10_basic.yml to for 2.17.0 Signed-off-by: Andriy Redko <[email protected]> --------- Signed-off-by: Gao Binlong <[email protected]> Signed-off-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com> Signed-off-by: Andriy Redko <[email protected]> Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com> Co-authored-by: Andriy Redko <[email protected]>
1 parent 883af54 commit 0df6e9e

File tree

4 files changed

+112
-1
lines changed

4 files changed

+112
-1
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
2929
### Fixed
3030
- Fix constraint bug which allows more primary shards than average primary shards per index ([#14908](https://github.com/opensearch-project/OpenSearch/pull/14908))
3131
- Fix missing value of FieldSort for unsigned_long ([#14963](https://github.com/opensearch-project/OpenSearch/pull/14963))
32+
- Fix delete index template failed when the index template matches a data stream but is unused ([#15080](https://github.com/opensearch-project/OpenSearch/pull/15080))
3233

3334
### Security
3435

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
setup:
2+
- do:
3+
indices.put_index_template:
4+
name: test_template_1
5+
body:
6+
index_patterns: test-*
7+
template:
8+
settings:
9+
number_of_shards: 1
10+
number_of_replicas: 0
11+
"priority": 50
12+
13+
- do:
14+
indices.put_index_template:
15+
name: test_template_2
16+
body:
17+
index_patterns: test-*
18+
data_stream: {}
19+
template:
20+
settings:
21+
number_of_shards: 1
22+
number_of_replicas: 0
23+
"priority": 51
24+
25+
---
26+
teardown:
27+
- do:
28+
indices.delete_data_stream:
29+
name: test-1
30+
ignore: 404
31+
- do:
32+
indices.delete_index_template:
33+
name: test_template_1
34+
ignore: 404
35+
- do:
36+
indices.delete_index_template:
37+
name: test_template_2
38+
ignore: 404
39+
40+
---
41+
"Delete index template which is not used by data stream but index pattern matches":
42+
- skip:
43+
version: " - 2.16.99"
44+
reason: "fixed in 2.17.0"
45+
46+
- do:
47+
indices.create_data_stream:
48+
name: test-1
49+
50+
- do:
51+
indices.delete_index_template:
52+
name: test_template_1

server/src/main/java/org/opensearch/cluster/metadata/MetadataIndexTemplateService.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -944,7 +944,7 @@ static ClusterState innerRemoveIndexTemplateV2(ClusterState currentState, String
944944

945945
static Set<String> dataStreamsUsingTemplate(final ClusterState state, final String templateName) {
946946
final ComposableIndexTemplate template = state.metadata().templatesV2().get(templateName);
947-
if (template == null) {
947+
if (template == null || template.getDataStreamTemplate() == null) {
948948
return Collections.emptySet();
949949
}
950950
final Set<String> dataStreams = state.metadata().dataStreams().keySet();

server/src/test/java/org/opensearch/cluster/metadata/MetadataIndexTemplateServiceTests.java

+58
Original file line numberDiff line numberDiff line change
@@ -559,6 +559,64 @@ public void testRemoveIndexTemplateV2() throws Exception {
559559

560560
ClusterState updatedState = MetadataIndexTemplateService.innerRemoveIndexTemplateV2(state, "foo");
561561
assertNull(updatedState.metadata().templatesV2().get("foo"));
562+
563+
// test remove a template which is not used by a data stream but index patterns can match
564+
Settings settings = Settings.builder()
565+
.put(IndexMetadata.SETTING_BLOCKS_READ, randomBoolean())
566+
.put(IndexMetadata.SETTING_BLOCKS_WRITE, randomBoolean())
567+
.put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, randomIntBetween(1, 10))
568+
.put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, randomIntBetween(0, 5))
569+
.put(IndexMetadata.SETTING_BLOCKS_WRITE, randomBoolean())
570+
.put(IndexMetadata.SETTING_PRIORITY, randomIntBetween(0, 100000))
571+
.build();
572+
CompressedXContent mappings = new CompressedXContent(
573+
"{\"properties\":{\"" + randomAlphaOfLength(5) + "\":{\"type\":\"keyword\"}}}"
574+
);
575+
576+
Map<String, Object> meta = Collections.singletonMap(randomAlphaOfLength(4), randomAlphaOfLength(4));
577+
List<String> indexPatterns = List.of("foo*");
578+
List<String> componentTemplates = randomList(0, 10, () -> randomAlphaOfLength(5));
579+
ComposableIndexTemplate templateToRemove = new ComposableIndexTemplate(
580+
indexPatterns,
581+
new Template(settings, mappings, null),
582+
componentTemplates,
583+
randomBoolean() ? null : randomNonNegativeLong(),
584+
randomBoolean() ? null : randomNonNegativeLong(),
585+
meta,
586+
null
587+
);
588+
589+
ClusterState stateWithDS = ClusterState.builder(state)
590+
.metadata(
591+
Metadata.builder(state.metadata())
592+
.put(
593+
new DataStream(
594+
"foo",
595+
new DataStream.TimestampField("@timestamp"),
596+
Collections.singletonList(new Index(".ds-foo-000001", "uuid2"))
597+
)
598+
)
599+
.put(
600+
IndexMetadata.builder(".ds-foo-000001")
601+
.settings(
602+
Settings.builder()
603+
.put(IndexMetadata.SETTING_INDEX_UUID, "uuid2")
604+
.put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1)
605+
.put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 0)
606+
.put(IndexMetadata.SETTING_VERSION_CREATED, Version.CURRENT)
607+
.build()
608+
)
609+
)
610+
.build()
611+
)
612+
.build();
613+
614+
final ClusterState clusterState = metadataIndexTemplateService.addIndexTemplateV2(stateWithDS, false, "foo", templateToRemove);
615+
assertNotNull(clusterState.metadata().templatesV2().get("foo"));
616+
assertTemplatesEqual(clusterState.metadata().templatesV2().get("foo"), templateToRemove);
617+
618+
updatedState = MetadataIndexTemplateService.innerRemoveIndexTemplateV2(clusterState, "foo");
619+
assertNull(updatedState.metadata().templatesV2().get("foo"));
562620
}
563621

564622
/**

0 commit comments

Comments
 (0)