Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.cluster.metadata.ComponentTemplate;
import org.elasticsearch.cluster.metadata.ComposableIndexTemplate;
import org.elasticsearch.cluster.metadata.ProjectId;
import org.elasticsearch.cluster.metadata.Template;
import org.elasticsearch.common.compress.CompressedXContent;
import org.elasticsearch.common.settings.Settings;
Expand Down Expand Up @@ -85,7 +86,8 @@ public void testMappingValidationIndexExists() {
assertThat(searchResponse.getHits().getTotalHits().value(), equalTo(0L));
searchResponse.decRef();
ClusterStateResponse clusterStateResponse = admin().cluster().state(new ClusterStateRequest(TEST_REQUEST_TIMEOUT)).actionGet();
Map<String, Object> indexMapping = clusterStateResponse.getState().metadata().getProject().index(indexName).mapping().sourceAsMap();
final var project = clusterStateResponse.getState().metadata().getProject(ProjectId.DEFAULT);
Map<String, Object> indexMapping = project.index(indexName).mapping().sourceAsMap();
Map<String, Object> fields = (Map<String, Object>) indexMapping.get("properties");
assertThat(fields.size(), equalTo(1));
}
Expand Down Expand Up @@ -142,7 +144,8 @@ public void testMappingValidationIndexExistsTemplateSubstitutions() throws IOExc
assertThat(searchResponse.getHits().getTotalHits().value(), equalTo(0L));
searchResponse.decRef();
ClusterStateResponse clusterStateResponse = admin().cluster().state(new ClusterStateRequest(TEST_REQUEST_TIMEOUT)).actionGet();
Map<String, Object> indexMapping = clusterStateResponse.getState().metadata().getProject().index(indexName).mapping().sourceAsMap();
final var project = clusterStateResponse.getState().metadata().getProject(ProjectId.DEFAULT);
Map<String, Object> indexMapping = project.index(indexName).mapping().sourceAsMap();
Map<String, Object> fields = (Map<String, Object>) indexMapping.get("properties");
assertThat(fields.size(), equalTo(1));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,13 @@
import org.elasticsearch.cluster.block.ClusterBlockLevel;
import org.elasticsearch.cluster.metadata.ComponentTemplate;
import org.elasticsearch.cluster.metadata.ComposableIndexTemplate;
import org.elasticsearch.cluster.metadata.Metadata;
import org.elasticsearch.cluster.metadata.ProjectId;
import org.elasticsearch.cluster.metadata.ProjectMetadata;
import org.elasticsearch.cluster.project.ProjectResolver;
import org.elasticsearch.cluster.service.ClusterService;
import org.elasticsearch.common.io.stream.Writeable;
import org.elasticsearch.common.util.concurrent.EsExecutors;
import org.elasticsearch.core.Assertions;
import org.elasticsearch.core.FixForMultiProject;
import org.elasticsearch.core.Releasable;
import org.elasticsearch.core.TimeValue;
import org.elasticsearch.index.IndexingPressure;
Expand Down Expand Up @@ -193,34 +191,33 @@ private boolean applyPipelines(Task task, BulkRequest bulkRequest, Executor exec
boolean hasIndexRequestsWithPipelines = false;
ClusterState state = clusterService.state();
ProjectId projectId = projectResolver.getProjectId();
final Metadata metadata;
final ProjectMetadata project;
Map<String, ComponentTemplate> componentTemplateSubstitutions = bulkRequest.getComponentTemplateSubstitutions();
Map<String, ComposableIndexTemplate> indexTemplateSubstitutions = bulkRequest.getIndexTemplateSubstitutions();
if (bulkRequest.isSimulated()
&& (componentTemplateSubstitutions.isEmpty() == false || indexTemplateSubstitutions.isEmpty() == false)) {
/*
* If this is a simulated request, and there are template substitutions, then we want to create and use a new metadata that has
* If this is a simulated request, and there are template substitutions, then we want to create and use a new project that has
* those templates. That is, we want to add the new templates (which will replace any that already existed with the same name),
* and remove the indices and data streams that are referred to from the bulkRequest so that we get settings from the templates
* rather than from the indices/data streams.
*/
Metadata originalMetadata = state.metadata();
@FixForMultiProject // properly ensure simulated actions work with MP
Metadata.Builder simulatedMetadataBuilder = Metadata.builder(originalMetadata);
ProjectMetadata originalProject = state.metadata().getProject(projectId);
ProjectMetadata.Builder simulatedMetadataBuilder = ProjectMetadata.builder(originalProject);
if (componentTemplateSubstitutions.isEmpty() == false) {
Map<String, ComponentTemplate> updatedComponentTemplates = new HashMap<>();
updatedComponentTemplates.putAll(originalMetadata.getProject(projectId).componentTemplates());
updatedComponentTemplates.putAll(originalProject.componentTemplates());
updatedComponentTemplates.putAll(componentTemplateSubstitutions);
simulatedMetadataBuilder.componentTemplates(updatedComponentTemplates);
}
if (indexTemplateSubstitutions.isEmpty() == false) {
Map<String, ComposableIndexTemplate> updatedIndexTemplates = new HashMap<>();
updatedIndexTemplates.putAll(originalMetadata.getProject(projectId).templatesV2());
updatedIndexTemplates.putAll(originalProject.templatesV2());
updatedIndexTemplates.putAll(indexTemplateSubstitutions);
simulatedMetadataBuilder.indexTemplates(updatedIndexTemplates);
}
/*
* We now remove the index from the simulated metadata to force the templates to be used. Note that simulated requests are
* We now remove the index from the simulated project to force the templates to be used. Note that simulated requests are
* always index requests -- no other type of request is supported.
*/
for (DocWriteRequest<?> actionRequest : bulkRequest.requests) {
Expand All @@ -236,12 +233,11 @@ private boolean applyPipelines(Task task, BulkRequest bulkRequest, Executor exec
}
}
}
metadata = simulatedMetadataBuilder.build();
project = simulatedMetadataBuilder.build();
} else {
metadata = state.getMetadata();
project = state.metadata().getProject(projectId);
}

ProjectMetadata project = metadata.getProject(projectId);
Map<String, IngestService.Pipelines> resolvedPipelineCache = new HashMap<>();
for (DocWriteRequest<?> actionRequest : bulkRequest.requests) {
IndexRequest indexRequest = getIndexWriteRequest(actionRequest);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
import org.elasticsearch.cluster.metadata.ComposableIndexTemplate;
import org.elasticsearch.cluster.metadata.IndexMetadata;
import org.elasticsearch.cluster.metadata.IndexTemplateMetadata;
import org.elasticsearch.cluster.metadata.Metadata;
import org.elasticsearch.cluster.metadata.ProjectId;
import org.elasticsearch.cluster.metadata.ProjectMetadata;
import org.elasticsearch.cluster.node.DiscoveryNode;
import org.elasticsearch.cluster.node.DiscoveryNodeUtils;
import org.elasticsearch.cluster.project.TestProjectResolvers;
Expand Down Expand Up @@ -229,7 +230,7 @@ public void testIndexDataWithValidation() throws IOException {
Map<String, IndexMetadata> indicesMap = new HashMap<>();
Map<String, IndexTemplateMetadata> v1Templates = new HashMap<>();
Map<String, ComposableIndexTemplate> v2Templates = new HashMap<>();
Metadata.Builder metadataBuilder = new Metadata.Builder();
ProjectMetadata.Builder projectBuilder = ProjectMetadata.builder(ProjectId.DEFAULT);
Set<String> indicesWithInvalidMappings = new HashSet<>();
for (int i = 0; i < bulkItemCount; i++) {
Map<String, ?> source = Map.of(randomAlphaOfLength(10), randomAlphaOfLength(5));
Expand Down Expand Up @@ -275,10 +276,10 @@ public void testIndexDataWithValidation() throws IOException {
default -> throw new AssertionError("Illegal branch");
}
}
metadataBuilder.indices(indicesMap);
metadataBuilder.templates(v1Templates);
metadataBuilder.indexTemplates(v2Templates);
ClusterServiceUtils.setState(clusterService, new ClusterState.Builder(clusterService.state()).metadata(metadataBuilder));
projectBuilder.indices(indicesMap);
projectBuilder.templates(v1Templates);
projectBuilder.indexTemplates(v2Templates);
ClusterServiceUtils.setState(clusterService, ClusterState.builder(clusterService.state()).putProjectMetadata(projectBuilder));
AtomicBoolean onResponseCalled = new AtomicBoolean(false);
ActionListener<BulkResponse> listener = new ActionListener<>() {
@Override
Expand Down