Skip to content

Commit 789101b

Browse files
authored
Make template substitution in bulk simulation API project-aware (#130852)
Resolve the correct project instead of using the deprecated method.
1 parent 3f32757 commit 789101b

File tree

3 files changed

+21
-21
lines changed

3 files changed

+21
-21
lines changed

server/src/internalClusterTest/java/org/elasticsearch/action/bulk/TransportSimulateBulkActionIT.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import org.elasticsearch.action.search.SearchResponse;
2525
import org.elasticsearch.cluster.metadata.ComponentTemplate;
2626
import org.elasticsearch.cluster.metadata.ComposableIndexTemplate;
27+
import org.elasticsearch.cluster.metadata.ProjectId;
2728
import org.elasticsearch.cluster.metadata.Template;
2829
import org.elasticsearch.common.compress.CompressedXContent;
2930
import org.elasticsearch.common.settings.Settings;
@@ -85,7 +86,8 @@ public void testMappingValidationIndexExists() {
8586
assertThat(searchResponse.getHits().getTotalHits().value(), equalTo(0L));
8687
searchResponse.decRef();
8788
ClusterStateResponse clusterStateResponse = admin().cluster().state(new ClusterStateRequest(TEST_REQUEST_TIMEOUT)).actionGet();
88-
Map<String, Object> indexMapping = clusterStateResponse.getState().metadata().getProject().index(indexName).mapping().sourceAsMap();
89+
final var project = clusterStateResponse.getState().metadata().getProject(ProjectId.DEFAULT);
90+
Map<String, Object> indexMapping = project.index(indexName).mapping().sourceAsMap();
8991
Map<String, Object> fields = (Map<String, Object>) indexMapping.get("properties");
9092
assertThat(fields.size(), equalTo(1));
9193
}
@@ -142,7 +144,8 @@ public void testMappingValidationIndexExistsTemplateSubstitutions() throws IOExc
142144
assertThat(searchResponse.getHits().getTotalHits().value(), equalTo(0L));
143145
searchResponse.decRef();
144146
ClusterStateResponse clusterStateResponse = admin().cluster().state(new ClusterStateRequest(TEST_REQUEST_TIMEOUT)).actionGet();
145-
Map<String, Object> indexMapping = clusterStateResponse.getState().metadata().getProject().index(indexName).mapping().sourceAsMap();
147+
final var project = clusterStateResponse.getState().metadata().getProject(ProjectId.DEFAULT);
148+
Map<String, Object> indexMapping = project.index(indexName).mapping().sourceAsMap();
146149
Map<String, Object> fields = (Map<String, Object>) indexMapping.get("properties");
147150
assertThat(fields.size(), equalTo(1));
148151
}

server/src/main/java/org/elasticsearch/action/bulk/TransportAbstractBulkAction.java

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,13 @@
2626
import org.elasticsearch.cluster.block.ClusterBlockLevel;
2727
import org.elasticsearch.cluster.metadata.ComponentTemplate;
2828
import org.elasticsearch.cluster.metadata.ComposableIndexTemplate;
29-
import org.elasticsearch.cluster.metadata.Metadata;
3029
import org.elasticsearch.cluster.metadata.ProjectId;
3130
import org.elasticsearch.cluster.metadata.ProjectMetadata;
3231
import org.elasticsearch.cluster.project.ProjectResolver;
3332
import org.elasticsearch.cluster.service.ClusterService;
3433
import org.elasticsearch.common.io.stream.Writeable;
3534
import org.elasticsearch.common.util.concurrent.EsExecutors;
3635
import org.elasticsearch.core.Assertions;
37-
import org.elasticsearch.core.FixForMultiProject;
3836
import org.elasticsearch.core.Releasable;
3937
import org.elasticsearch.core.TimeValue;
4038
import org.elasticsearch.index.IndexingPressure;
@@ -193,34 +191,33 @@ private boolean applyPipelines(Task task, BulkRequest bulkRequest, Executor exec
193191
boolean hasIndexRequestsWithPipelines = false;
194192
ClusterState state = clusterService.state();
195193
ProjectId projectId = projectResolver.getProjectId();
196-
final Metadata metadata;
194+
final ProjectMetadata project;
197195
Map<String, ComponentTemplate> componentTemplateSubstitutions = bulkRequest.getComponentTemplateSubstitutions();
198196
Map<String, ComposableIndexTemplate> indexTemplateSubstitutions = bulkRequest.getIndexTemplateSubstitutions();
199197
if (bulkRequest.isSimulated()
200198
&& (componentTemplateSubstitutions.isEmpty() == false || indexTemplateSubstitutions.isEmpty() == false)) {
201199
/*
202-
* If this is a simulated request, and there are template substitutions, then we want to create and use a new metadata that has
200+
* If this is a simulated request, and there are template substitutions, then we want to create and use a new project that has
203201
* those templates. That is, we want to add the new templates (which will replace any that already existed with the same name),
204202
* and remove the indices and data streams that are referred to from the bulkRequest so that we get settings from the templates
205203
* rather than from the indices/data streams.
206204
*/
207-
Metadata originalMetadata = state.metadata();
208-
@FixForMultiProject // properly ensure simulated actions work with MP
209-
Metadata.Builder simulatedMetadataBuilder = Metadata.builder(originalMetadata);
205+
ProjectMetadata originalProject = state.metadata().getProject(projectId);
206+
ProjectMetadata.Builder simulatedMetadataBuilder = ProjectMetadata.builder(originalProject);
210207
if (componentTemplateSubstitutions.isEmpty() == false) {
211208
Map<String, ComponentTemplate> updatedComponentTemplates = new HashMap<>();
212-
updatedComponentTemplates.putAll(originalMetadata.getProject(projectId).componentTemplates());
209+
updatedComponentTemplates.putAll(originalProject.componentTemplates());
213210
updatedComponentTemplates.putAll(componentTemplateSubstitutions);
214211
simulatedMetadataBuilder.componentTemplates(updatedComponentTemplates);
215212
}
216213
if (indexTemplateSubstitutions.isEmpty() == false) {
217214
Map<String, ComposableIndexTemplate> updatedIndexTemplates = new HashMap<>();
218-
updatedIndexTemplates.putAll(originalMetadata.getProject(projectId).templatesV2());
215+
updatedIndexTemplates.putAll(originalProject.templatesV2());
219216
updatedIndexTemplates.putAll(indexTemplateSubstitutions);
220217
simulatedMetadataBuilder.indexTemplates(updatedIndexTemplates);
221218
}
222219
/*
223-
* We now remove the index from the simulated metadata to force the templates to be used. Note that simulated requests are
220+
* We now remove the index from the simulated project to force the templates to be used. Note that simulated requests are
224221
* always index requests -- no other type of request is supported.
225222
*/
226223
for (DocWriteRequest<?> actionRequest : bulkRequest.requests) {
@@ -236,12 +233,11 @@ private boolean applyPipelines(Task task, BulkRequest bulkRequest, Executor exec
236233
}
237234
}
238235
}
239-
metadata = simulatedMetadataBuilder.build();
236+
project = simulatedMetadataBuilder.build();
240237
} else {
241-
metadata = state.getMetadata();
238+
project = state.metadata().getProject(projectId);
242239
}
243240

244-
ProjectMetadata project = metadata.getProject(projectId);
245241
Map<String, IngestService.Pipelines> resolvedPipelineCache = new HashMap<>();
246242
for (DocWriteRequest<?> actionRequest : bulkRequest.requests) {
247243
IndexRequest indexRequest = getIndexWriteRequest(actionRequest);

server/src/test/java/org/elasticsearch/action/bulk/TransportSimulateBulkActionTests.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@
1919
import org.elasticsearch.cluster.metadata.ComposableIndexTemplate;
2020
import org.elasticsearch.cluster.metadata.IndexMetadata;
2121
import org.elasticsearch.cluster.metadata.IndexTemplateMetadata;
22-
import org.elasticsearch.cluster.metadata.Metadata;
22+
import org.elasticsearch.cluster.metadata.ProjectId;
23+
import org.elasticsearch.cluster.metadata.ProjectMetadata;
2324
import org.elasticsearch.cluster.node.DiscoveryNode;
2425
import org.elasticsearch.cluster.node.DiscoveryNodeUtils;
2526
import org.elasticsearch.cluster.project.TestProjectResolvers;
@@ -229,7 +230,7 @@ public void testIndexDataWithValidation() throws IOException {
229230
Map<String, IndexMetadata> indicesMap = new HashMap<>();
230231
Map<String, IndexTemplateMetadata> v1Templates = new HashMap<>();
231232
Map<String, ComposableIndexTemplate> v2Templates = new HashMap<>();
232-
Metadata.Builder metadataBuilder = new Metadata.Builder();
233+
ProjectMetadata.Builder projectBuilder = ProjectMetadata.builder(ProjectId.DEFAULT);
233234
Set<String> indicesWithInvalidMappings = new HashSet<>();
234235
for (int i = 0; i < bulkItemCount; i++) {
235236
Map<String, ?> source = Map.of(randomAlphaOfLength(10), randomAlphaOfLength(5));
@@ -275,10 +276,10 @@ public void testIndexDataWithValidation() throws IOException {
275276
default -> throw new AssertionError("Illegal branch");
276277
}
277278
}
278-
metadataBuilder.indices(indicesMap);
279-
metadataBuilder.templates(v1Templates);
280-
metadataBuilder.indexTemplates(v2Templates);
281-
ClusterServiceUtils.setState(clusterService, new ClusterState.Builder(clusterService.state()).metadata(metadataBuilder));
279+
projectBuilder.indices(indicesMap);
280+
projectBuilder.templates(v1Templates);
281+
projectBuilder.indexTemplates(v2Templates);
282+
ClusterServiceUtils.setState(clusterService, ClusterState.builder(clusterService.state()).putProjectMetadata(projectBuilder));
282283
AtomicBoolean onResponseCalled = new AtomicBoolean(false);
283284
ActionListener<BulkResponse> listener = new ActionListener<>() {
284285
@Override

0 commit comments

Comments
 (0)