Skip to content

Commit a9d09aa

Browse files
authored
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 <gbinlong@amazon.com> * modify change log Signed-off-by: Gao Binlong <gbinlong@amazon.com> * Fix version check Signed-off-by: Gao Binlong <gbinlong@amazon.com> --------- Signed-off-by: Gao Binlong <gbinlong@amazon.com>
1 parent 47171f8 commit a9d09aa

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
@@ -32,6 +32,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
3232
### Fixed
3333
- Fix constraint bug which allows more primary shards than average primary shards per index ([#14908](https://github.com/opensearch-project/OpenSearch/pull/14908))
3434
- Fix missing value of FieldSort for unsigned_long ([#14963](https://github.com/opensearch-project/OpenSearch/pull/14963))
35+
- 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))
3536

3637
### Security
3738

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.99.99"
44+
reason: "fixed in 3.0.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
@@ -560,6 +560,64 @@ public void testRemoveIndexTemplateV2() throws Exception {
560560

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

565623
/**

0 commit comments

Comments
 (0)