|
44 | 44 | import org.elasticsearch.common.settings.Settings;
|
45 | 45 | import org.elasticsearch.common.xcontent.NamedXContentRegistry;
|
46 | 46 | import org.elasticsearch.common.xcontent.XContentFactory;
|
47 |
| -import org.elasticsearch.index.Index; |
48 |
| -import org.elasticsearch.index.IndexService; |
49 | 47 | import org.elasticsearch.index.mapper.MapperService;
|
50 | 48 | import org.elasticsearch.indices.IndicesService;
|
51 | 49 | import org.elasticsearch.tasks.Task;
|
52 | 50 | import org.elasticsearch.threadpool.ThreadPool;
|
53 | 51 | import org.elasticsearch.transport.TransportService;
|
54 | 52 |
|
55 |
| -import java.io.Closeable; |
56 | 53 | import java.io.IOException;
|
57 |
| -import java.util.Collections; |
58 | 54 | import java.util.HashMap;
|
59 | 55 | import java.util.List;
|
60 | 56 | import java.util.Locale;
|
|
68 | 64 | import static org.elasticsearch.cluster.metadata.MetadataIndexTemplateService.findConflictingV2Templates;
|
69 | 65 | import static org.elasticsearch.cluster.metadata.MetadataIndexTemplateService.findV2Template;
|
70 | 66 | import static org.elasticsearch.cluster.metadata.MetadataIndexTemplateService.resolveSettings;
|
71 |
| -import static org.elasticsearch.indices.cluster.IndicesClusterStateService.AllocatedIndices.IndexRemovalReason.NO_LONGER_ASSIGNED; |
72 | 67 |
|
73 | 68 | public class TransportSimulateIndexTemplateAction
|
74 | 69 | extends TransportMasterNodeReadAction<SimulateIndexTemplateRequest, SimulateIndexTemplateResponse> {
|
@@ -139,37 +134,28 @@ protected void masterOperation(Task task, SimulateIndexTemplateRequest request,
|
139 | 134 | .build();
|
140 | 135 | final IndexMetadata indexMetadata = IndexMetadata.builder(request.getIndexName()).settings(dummySettings).build();
|
141 | 136 |
|
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), |
165 | 157 | 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)); |
173 | 159 | }
|
174 | 160 |
|
175 | 161 | @Override
|
|
0 commit comments