Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix delete index template failed when the index template matches a data stream but is unused #15080

Merged
merged 5 commits into from
Aug 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading