Skip to content

Commit

Permalink
Fix delete index template failed when the index template matches a da…
Browse files Browse the repository at this point in the history
…ta 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]>
  • Loading branch information
gaobinlong authored Aug 3, 2024
1 parent 47171f8 commit a9d09aa
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
### Fixed
- Fix constraint bug which allows more primary shards than average primary shards per index ([#14908](https://github.com/opensearch-project/OpenSearch/pull/14908))
- Fix missing value of FieldSort for unsigned_long ([#14963](https://github.com/opensearch-project/OpenSearch/pull/14963))
- 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))

### Security

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
setup:
- do:
indices.put_index_template:
name: test_template_1
body:
index_patterns: test-*
template:
settings:
number_of_shards: 1
number_of_replicas: 0
"priority": 50

- do:
indices.put_index_template:
name: test_template_2
body:
index_patterns: test-*
data_stream: {}
template:
settings:
number_of_shards: 1
number_of_replicas: 0
"priority": 51

---
teardown:
- do:
indices.delete_data_stream:
name: test-1
ignore: 404
- do:
indices.delete_index_template:
name: test_template_1
ignore: 404
- do:
indices.delete_index_template:
name: test_template_2
ignore: 404

---
"Delete index template which is not used by data stream but index pattern matches":
- skip:
version: " - 2.99.99"
reason: "fixed in 3.0.0"

- do:
indices.create_data_stream:
name: test-1

- do:
indices.delete_index_template:
name: test_template_1
Original file line number Diff line number Diff line change
Expand Up @@ -944,7 +944,7 @@ static ClusterState innerRemoveIndexTemplateV2(ClusterState currentState, String

static Set<String> dataStreamsUsingTemplate(final ClusterState state, final String templateName) {
final ComposableIndexTemplate template = state.metadata().templatesV2().get(templateName);
if (template == null) {
if (template == null || template.getDataStreamTemplate() == null) {
return Collections.emptySet();
}
final Set<String> dataStreams = state.metadata().dataStreams().keySet();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -560,6 +560,64 @@ public void testRemoveIndexTemplateV2() throws Exception {

ClusterState updatedState = MetadataIndexTemplateService.innerRemoveIndexTemplateV2(state, "foo");
assertNull(updatedState.metadata().templatesV2().get("foo"));

// test remove a template which is not used by a data stream but index patterns can match
Settings settings = Settings.builder()
.put(IndexMetadata.SETTING_BLOCKS_READ, randomBoolean())
.put(IndexMetadata.SETTING_BLOCKS_WRITE, randomBoolean())
.put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, randomIntBetween(1, 10))
.put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, randomIntBetween(0, 5))
.put(IndexMetadata.SETTING_BLOCKS_WRITE, randomBoolean())
.put(IndexMetadata.SETTING_PRIORITY, randomIntBetween(0, 100000))
.build();
CompressedXContent mappings = new CompressedXContent(
"{\"properties\":{\"" + randomAlphaOfLength(5) + "\":{\"type\":\"keyword\"}}}"
);

Map<String, Object> meta = Collections.singletonMap(randomAlphaOfLength(4), randomAlphaOfLength(4));
List<String> indexPatterns = List.of("foo*");
List<String> componentTemplates = randomList(0, 10, () -> randomAlphaOfLength(5));
ComposableIndexTemplate templateToRemove = new ComposableIndexTemplate(
indexPatterns,
new Template(settings, mappings, null),
componentTemplates,
randomBoolean() ? null : randomNonNegativeLong(),
randomBoolean() ? null : randomNonNegativeLong(),
meta,
null
);

ClusterState stateWithDS = ClusterState.builder(state)
.metadata(
Metadata.builder(state.metadata())
.put(
new DataStream(
"foo",
new DataStream.TimestampField("@timestamp"),
Collections.singletonList(new Index(".ds-foo-000001", "uuid2"))
)
)
.put(
IndexMetadata.builder(".ds-foo-000001")
.settings(
Settings.builder()
.put(IndexMetadata.SETTING_INDEX_UUID, "uuid2")
.put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1)
.put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 0)
.put(IndexMetadata.SETTING_VERSION_CREATED, Version.CURRENT)
.build()
)
)
.build()
)
.build();

final ClusterState clusterState = metadataIndexTemplateService.addIndexTemplateV2(stateWithDS, false, "foo", templateToRemove);
assertNotNull(clusterState.metadata().templatesV2().get("foo"));
assertTemplatesEqual(clusterState.metadata().templatesV2().get("foo"), templateToRemove);

updatedState = MetadataIndexTemplateService.innerRemoveIndexTemplateV2(clusterState, "foo");
assertNull(updatedState.metadata().templatesV2().get("foo"));
}

/**
Expand Down

0 comments on commit a9d09aa

Please sign in to comment.