Skip to content

Commit fd437ca

Browse files
Fix Simulate Template Endpoint Temporary Index Handling (#56406)
Use proper facility for creating temporary index service for the simulation that does not add itself to the `IndicesService` unnecessarily (breaking an assertion about the internal consistency of the cluster state and the `IndicesService`). Closes #56298
1 parent 9ab920c commit fd437ca

File tree

1 file changed

+21
-35
lines changed

1 file changed

+21
-35
lines changed

server/src/main/java/org/elasticsearch/action/admin/indices/template/post/TransportSimulateIndexTemplateAction.java

+21-35
Original file line numberDiff line numberDiff line change
@@ -44,17 +44,13 @@
4444
import org.elasticsearch.common.settings.Settings;
4545
import org.elasticsearch.common.xcontent.NamedXContentRegistry;
4646
import org.elasticsearch.common.xcontent.XContentFactory;
47-
import org.elasticsearch.index.Index;
48-
import org.elasticsearch.index.IndexService;
4947
import org.elasticsearch.index.mapper.MapperService;
5048
import org.elasticsearch.indices.IndicesService;
5149
import org.elasticsearch.tasks.Task;
5250
import org.elasticsearch.threadpool.ThreadPool;
5351
import org.elasticsearch.transport.TransportService;
5452

55-
import java.io.Closeable;
5653
import java.io.IOException;
57-
import java.util.Collections;
5854
import java.util.HashMap;
5955
import java.util.List;
6056
import java.util.Locale;
@@ -68,7 +64,6 @@
6864
import static org.elasticsearch.cluster.metadata.MetadataIndexTemplateService.findConflictingV2Templates;
6965
import static org.elasticsearch.cluster.metadata.MetadataIndexTemplateService.findV2Template;
7066
import static org.elasticsearch.cluster.metadata.MetadataIndexTemplateService.resolveSettings;
71-
import static org.elasticsearch.indices.cluster.IndicesClusterStateService.AllocatedIndices.IndexRemovalReason.NO_LONGER_ASSIGNED;
7267

7368
public class TransportSimulateIndexTemplateAction
7469
extends TransportMasterNodeReadAction<SimulateIndexTemplateRequest, SimulateIndexTemplateResponse> {
@@ -139,37 +134,28 @@ protected void masterOperation(Task task, SimulateIndexTemplateRequest request,
139134
.build();
140135
final IndexMetadata indexMetadata = IndexMetadata.builder(request.getIndexName()).settings(dummySettings).build();
141136

142-
simulateOnClusterState = ClusterState.builder(simulateOnClusterState)
143-
.metadata(Metadata.builder(simulateOnClusterState.metadata())
144-
.put(indexMetadata, true)
145-
.build())
146-
.build();
147-
148-
IndexService tempIndexService = indicesService.createIndex(indexMetadata, Collections.emptyList(), false);
149-
final Index index = tempIndexService.index();
150-
try (Closeable dummy = () -> tempIndexService.close("temp", false)) {
151-
List<AliasMetadata> aliases = MetadataCreateIndexService.resolveAndValidateAliases(request.getIndexName(), Set.of(),
152-
resolvedAliases, simulateOnClusterState.metadata(), aliasValidator, xContentRegistry,
153-
// the context is only used for validation so it's fine to pass fake values for the
154-
// shard id and the current timestamp
155-
tempIndexService.newQueryShardContext(0, null, () -> 0L, null));
156-
157-
IndexTemplateV2 templateV2 = simulateOnClusterState.metadata().templatesV2().get(matchingTemplate);
158-
assert templateV2 != null : "the matched template must exist";
159-
160-
Map<String, List<String>> overlapping = new HashMap<>();
161-
overlapping.putAll(findConflictingV1Templates(simulateOnClusterState, matchingTemplate, templateV2.indexPatterns()));
162-
overlapping.putAll(findConflictingV2Templates(simulateOnClusterState, matchingTemplate, templateV2.indexPatterns()));
163-
164-
Template template = new Template(settings, mappingsJson == null ? null : new CompressedXContent(mappingsJson),
137+
final ClusterState tempClusterState = ClusterState.builder(simulateOnClusterState)
138+
.metadata(Metadata.builder(simulateOnClusterState.metadata())
139+
.put(indexMetadata, true)
140+
.build())
141+
.build();
142+
List<AliasMetadata> aliases = indicesService.withTempIndexService(indexMetadata, tempIndexService ->
143+
MetadataCreateIndexService.resolveAndValidateAliases(request.getIndexName(), Set.of(),
144+
resolvedAliases, tempClusterState.metadata(), aliasValidator, xContentRegistry,
145+
// the context is only used for validation so it's fine to pass fake values for the
146+
// shard id and the current timestamp
147+
tempIndexService.newQueryShardContext(0, null, () -> 0L, null)));
148+
149+
IndexTemplateV2 templateV2 = tempClusterState.metadata().templatesV2().get(matchingTemplate);
150+
assert templateV2 != null : "the matched template must exist";
151+
152+
Map<String, List<String>> overlapping = new HashMap<>();
153+
overlapping.putAll(findConflictingV1Templates(tempClusterState, matchingTemplate, templateV2.indexPatterns()));
154+
overlapping.putAll(findConflictingV2Templates(tempClusterState, matchingTemplate, templateV2.indexPatterns()));
155+
156+
Template template = new Template(settings, mappingsJson == null ? null : new CompressedXContent(mappingsJson),
165157
aliases.stream().collect(Collectors.toMap(AliasMetadata::getAlias, Function.identity())));
166-
listener.onResponse(new SimulateIndexTemplateResponse(template, overlapping));
167-
} finally {
168-
if (index != null) {
169-
indicesService.removeIndex(index, NO_LONGER_ASSIGNED,
170-
"created as part of a simulation for an index name matching the index templates in the system");
171-
}
172-
}
158+
listener.onResponse(new SimulateIndexTemplateResponse(template, overlapping));
173159
}
174160

175161
@Override

0 commit comments

Comments
 (0)