diff --git a/docs/changelog/129507.yaml b/docs/changelog/129507.yaml new file mode 100644 index 0000000000000..fa555433c34d6 --- /dev/null +++ b/docs/changelog/129507.yaml @@ -0,0 +1,6 @@ +pr: 129507 +summary: Using a temp `IndexService` for template validation +area: Indices APIs +type: bug +issues: + - 129473 diff --git a/server/src/main/java/org/elasticsearch/cluster/metadata/MetadataIndexTemplateService.java b/server/src/main/java/org/elasticsearch/cluster/metadata/MetadataIndexTemplateService.java index 1c438b993cd6a..69dd4359fd764 100644 --- a/server/src/main/java/org/elasticsearch/cluster/metadata/MetadataIndexTemplateService.java +++ b/server/src/main/java/org/elasticsearch/cluster/metadata/MetadataIndexTemplateService.java @@ -37,8 +37,6 @@ import org.elasticsearch.core.Nullable; import org.elasticsearch.core.TimeValue; import org.elasticsearch.core.Tuple; -import org.elasticsearch.index.CloseUtils; -import org.elasticsearch.index.Index; import org.elasticsearch.index.IndexService; import org.elasticsearch.index.IndexSettingProvider; import org.elasticsearch.index.IndexSettingProviders; @@ -83,7 +81,6 @@ import static java.util.Collections.emptyMap; import static org.elasticsearch.cluster.metadata.MetadataCreateDataStreamService.validateTimestampFieldMapping; -import static org.elasticsearch.indices.cluster.IndexRemovalReason.NO_LONGER_ASSIGNED; /** * Service responsible for submitting index templates updates @@ -1950,45 +1947,28 @@ static void validateTemplate(Settings validateSettings, CompressedXContent mappi settings = Settings.EMPTY; } - Index createdIndex = null; final String temporaryIndexName = UUIDs.randomBase64UUID(); - try { - // use the provided values, otherwise just pick valid dummy values - int dummyPartitionSize = IndexMetadata.INDEX_ROUTING_PARTITION_SIZE_SETTING.get(settings); - int dummyShards = settings.getAsInt( - IndexMetadata.SETTING_NUMBER_OF_SHARDS, - dummyPartitionSize == 1 ? 1 : dummyPartitionSize + 1 - ); - int shardReplicas = settings.getAsInt(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 0); - - // create index service for parsing and validating "mappings" - Settings dummySettings = Settings.builder() - .put(IndexMetadata.SETTING_VERSION_CREATED, IndexVersion.current()) - .put(settings) - .put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, dummyShards) - .put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, shardReplicas) - .put(IndexMetadata.SETTING_INDEX_UUID, UUIDs.randomBase64UUID()) - .build(); + int dummyPartitionSize = IndexMetadata.INDEX_ROUTING_PARTITION_SIZE_SETTING.get(settings); + int dummyShards = settings.getAsInt(IndexMetadata.SETTING_NUMBER_OF_SHARDS, dummyPartitionSize == 1 ? 1 : dummyPartitionSize + 1); + int shardReplicas = settings.getAsInt(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 0); - final IndexMetadata tmpIndexMetadata = IndexMetadata.builder(temporaryIndexName).settings(dummySettings).build(); - IndexService dummyIndexService = indicesService.createIndex(tmpIndexMetadata, Collections.emptyList(), false); - createdIndex = dummyIndexService.index(); + // create index service for parsing and validating "mappings" + Settings dummySettings = Settings.builder() + .put(IndexMetadata.SETTING_VERSION_CREATED, IndexVersion.current()) + .put(settings) + .put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, dummyShards) + .put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, shardReplicas) + .put(IndexMetadata.SETTING_INDEX_UUID, UUIDs.randomBase64UUID()) + .build(); + final IndexMetadata tmpIndexMetadata = IndexMetadata.builder(temporaryIndexName).settings(dummySettings).build(); + + indicesService.withTempIndexService(tmpIndexMetadata, dummyIndexService -> { if (mappings != null) { dummyIndexService.mapperService().merge(MapperService.SINGLE_MAPPING_NAME, mappings, MergeReason.INDEX_TEMPLATE); } - - } finally { - if (createdIndex != null) { - indicesService.removeIndex( - createdIndex, - NO_LONGER_ASSIGNED, - " created for parsing template mapping", - CloseUtils.NO_SHARDS_CREATED_EXECUTOR, - ActionListener.noop() - ); - } - } + return null; + }); } private void validate(String name, ComponentTemplate template) {