Skip to content

Commit

Permalink
Fix Simulate Template Endpoint Temporary Index Handling (#56406) (#56434
Browse files Browse the repository at this point in the history
)

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
  • Loading branch information
original-brownbear committed May 8, 2020
1 parent 5bd8727 commit 1940db3
Showing 1 changed file with 21 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,11 @@
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.xcontent.NamedXContentRegistry;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.index.Index;
import org.elasticsearch.index.IndexService;
import org.elasticsearch.index.mapper.MapperService;
import org.elasticsearch.indices.IndicesService;
import org.elasticsearch.threadpool.ThreadPool;
import org.elasticsearch.transport.TransportService;

import java.io.Closeable;
import java.io.IOException;
import java.util.Collections;
import java.util.HashMap;
Expand All @@ -66,7 +63,6 @@
import static org.elasticsearch.cluster.metadata.MetadataIndexTemplateService.findConflictingV2Templates;
import static org.elasticsearch.cluster.metadata.MetadataIndexTemplateService.findV2Template;
import static org.elasticsearch.cluster.metadata.MetadataIndexTemplateService.resolveSettings;
import static org.elasticsearch.indices.cluster.IndicesClusterStateService.AllocatedIndices.IndexRemovalReason.NO_LONGER_ASSIGNED;

public class TransportSimulateIndexTemplateAction
extends TransportMasterNodeReadAction<SimulateIndexTemplateRequest, SimulateIndexTemplateResponse> {
Expand Down Expand Up @@ -137,38 +133,28 @@ protected void masterOperation(SimulateIndexTemplateRequest request, ClusterStat
.build();
final IndexMetadata indexMetadata = IndexMetadata.builder(request.getIndexName()).settings(dummySettings).build();

simulateOnClusterState = ClusterState.builder(simulateOnClusterState)
.metadata(Metadata.builder(simulateOnClusterState.metadata())
.put(indexMetadata, true)
.build())
.build();

IndexService tempIndexService = indicesService.createIndex(indexMetadata, Collections.emptyList(), false);
final Index index = tempIndexService.index();
try (Closeable dummy = () -> tempIndexService.close("temp", false)) {
List<AliasMetadata> aliases = MetadataCreateIndexService.resolveAndValidateAliases(request.getIndexName(),
org.elasticsearch.common.collect.Set.of(), resolvedAliases, simulateOnClusterState.metadata(), aliasValidator,
xContentRegistry,
// the context is only used for validation so it's fine to pass fake values for the
// shard id and the current timestamp
tempIndexService.newQueryShardContext(0, null, () -> 0L, null));

IndexTemplateV2 templateV2 = simulateOnClusterState.metadata().templatesV2().get(matchingTemplate);
assert templateV2 != null : "the matched template must exist";

Map<String, List<String>> overlapping = new HashMap<>();
overlapping.putAll(findConflictingV1Templates(simulateOnClusterState, matchingTemplate, templateV2.indexPatterns()));
overlapping.putAll(findConflictingV2Templates(simulateOnClusterState, matchingTemplate, templateV2.indexPatterns()));

Template template = new Template(settings, mappingsJson == null ? null : new CompressedXContent(mappingsJson),
final ClusterState tempClusterState = ClusterState.builder(simulateOnClusterState)
.metadata(Metadata.builder(simulateOnClusterState.metadata())
.put(indexMetadata, true)
.build())
.build();
List<AliasMetadata> aliases = indicesService.withTempIndexService(indexMetadata, tempIndexService ->
MetadataCreateIndexService.resolveAndValidateAliases(request.getIndexName(), Collections.emptySet(),
resolvedAliases, tempClusterState.metadata(), aliasValidator, xContentRegistry,
// the context is only used for validation so it's fine to pass fake values for the
// shard id and the current timestamp
tempIndexService.newQueryShardContext(0, null, () -> 0L, null)));

IndexTemplateV2 templateV2 = tempClusterState.metadata().templatesV2().get(matchingTemplate);
assert templateV2 != null : "the matched template must exist";

Map<String, List<String>> overlapping = new HashMap<>();
overlapping.putAll(findConflictingV1Templates(tempClusterState, matchingTemplate, templateV2.indexPatterns()));
overlapping.putAll(findConflictingV2Templates(tempClusterState, matchingTemplate, templateV2.indexPatterns()));

Template template = new Template(settings, mappingsJson == null ? null : new CompressedXContent(mappingsJson),
aliases.stream().collect(Collectors.toMap(AliasMetadata::getAlias, Function.identity())));
listener.onResponse(new SimulateIndexTemplateResponse(template, overlapping));
} finally {
if (index != null) {
indicesService.removeIndex(index, NO_LONGER_ASSIGNED,
"created as part of a simulation for an index name matching the index templates in the system");
}
}
listener.onResponse(new SimulateIndexTemplateResponse(template, overlapping));
}

@Override
Expand Down

0 comments on commit 1940db3

Please sign in to comment.