-
Notifications
You must be signed in to change notification settings - Fork 25.6k
Adding actions to get and update data stream mappings #130042
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
masseyke
merged 13 commits into
elastic:main
from
masseyke:data-stream-mappings-actions
Jun 27, 2025
Merged
Changes from 10 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
20a6a75
Adding actions to get and update data stream mappings
masseyke e4d598c
cleanup
masseyke 88c64de
Merge branch 'main' into data-stream-mappings-actions
masseyke 0d834b5
Merge branch 'main' into data-stream-mappings-actions
masseyke b0a0579
copilot feedback
masseyke cb3b333
Fixing DataStreamTests
masseyke 6cbbc7f
Adding an integration test
masseyke 08e594c
Merge branch 'main' into data-stream-mappings-actions
masseyke 55d1229
Enabling logs-stream feature flag in integ tests for release builds
masseyke 2594cff
removing accidentally committed code
masseyke 77acfd9
stored mapping content type is always json
masseyke d552852
multiproject code review feedback
masseyke a98bc61
Merge branch 'main' into data-stream-mappings-actions
masseyke File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
241 changes: 241 additions & 0 deletions
241
...terTest/java/org/elasticsearch/datastreams/TransportUpdateDataStreamMappingsActionIT.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,241 @@ | ||
| /* | ||
| * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
| * or more contributor license agreements. Licensed under the "Elastic License | ||
| * 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side | ||
| * Public License v 1"; you may not use this file except in compliance with, at | ||
| * your election, the "Elastic License 2.0", the "GNU Affero General Public | ||
| * License v3.0 only", or the "Server Side Public License, v 1". | ||
| */ | ||
|
|
||
| package org.elasticsearch.datastreams; | ||
|
|
||
| import org.elasticsearch.action.ActionType; | ||
| import org.elasticsearch.action.admin.indices.mapping.get.GetMappingsAction; | ||
| import org.elasticsearch.action.admin.indices.mapping.get.GetMappingsRequest; | ||
| import org.elasticsearch.action.admin.indices.mapping.get.GetMappingsResponse; | ||
| import org.elasticsearch.action.admin.indices.rollover.RolloverRequest; | ||
| import org.elasticsearch.action.admin.indices.template.put.TransportPutComposableIndexTemplateAction; | ||
| import org.elasticsearch.action.bulk.BulkRequest; | ||
| import org.elasticsearch.action.bulk.BulkResponse; | ||
| import org.elasticsearch.action.bulk.TransportBulkAction; | ||
| import org.elasticsearch.action.datastreams.GetDataStreamAction; | ||
| import org.elasticsearch.action.datastreams.GetDataStreamMappingsAction; | ||
| import org.elasticsearch.action.datastreams.UpdateDataStreamMappingsAction; | ||
| import org.elasticsearch.action.index.IndexRequest; | ||
| import org.elasticsearch.cluster.metadata.ComposableIndexTemplate; | ||
| import org.elasticsearch.cluster.metadata.DataStream; | ||
| import org.elasticsearch.cluster.metadata.MappingMetadata; | ||
| import org.elasticsearch.cluster.metadata.Template; | ||
| import org.elasticsearch.common.compress.CompressedXContent; | ||
| import org.elasticsearch.common.settings.Settings; | ||
| import org.elasticsearch.common.xcontent.XContentHelper; | ||
| import org.elasticsearch.core.TimeValue; | ||
| import org.elasticsearch.plugins.Plugin; | ||
| import org.elasticsearch.test.ESIntegTestCase; | ||
| import org.elasticsearch.xcontent.XContentType; | ||
|
|
||
| import java.io.IOException; | ||
| import java.util.Collection; | ||
| import java.util.List; | ||
| import java.util.Locale; | ||
| import java.util.Map; | ||
|
|
||
| import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertAcked; | ||
| import static org.hamcrest.Matchers.equalTo; | ||
|
|
||
| public class TransportUpdateDataStreamMappingsActionIT extends ESIntegTestCase { | ||
|
|
||
| @Override | ||
| protected Collection<Class<? extends Plugin>> nodePlugins() { | ||
| return List.of(DataStreamsPlugin.class); | ||
| } | ||
|
|
||
| public void testGetAndUpdateMappings() throws IOException { | ||
| String dataStreamName = "my-data-stream-" + randomAlphaOfLength(5).toLowerCase(Locale.ROOT); | ||
| createDataStream(dataStreamName); | ||
|
|
||
| Map<String, Object> originalMappings = Map.of( | ||
| "dynamic", | ||
| "strict", | ||
| "properties", | ||
| Map.of("foo1", Map.of("type", "text"), "foo2", Map.of("type", "text")) | ||
| ); | ||
| Map<String, Object> mappingOverrides = Map.of( | ||
| "properties", | ||
| Map.of("foo2", Map.of("type", "keyword"), "foo3", Map.of("type", "text")) | ||
| ); | ||
| Map<String, Object> expectedEffectiveMappings = Map.of( | ||
| "dynamic", | ||
| "strict", | ||
| "properties", | ||
| Map.of("foo1", Map.of("type", "text"), "foo2", Map.of("type", "keyword"), "foo3", Map.of("type", "text")) | ||
| ); | ||
| assertExpectedMappings(dataStreamName, Map.of(), originalMappings); | ||
| updateMappings(dataStreamName, mappingOverrides, expectedEffectiveMappings, true); | ||
| assertExpectedMappings(dataStreamName, Map.of(), originalMappings); | ||
| updateMappings(dataStreamName, mappingOverrides, expectedEffectiveMappings, false); | ||
| assertExpectedMappings(dataStreamName, mappingOverrides, expectedEffectiveMappings); | ||
|
|
||
| // Now make sure that the backing index still has the original mappings: | ||
| Map<String, Object> originalIndexMappings = Map.of( | ||
| "dynamic", | ||
| "strict", | ||
| "_data_stream_timestamp", | ||
| Map.of("enabled", true), | ||
| "properties", | ||
| Map.of("@timestamp", Map.of("type", "date"), "foo1", Map.of("type", "text"), "foo2", Map.of("type", "text")) | ||
| ); | ||
| assertExpectedMappingsOnIndex(getDataStream(dataStreamName).getIndices().getFirst().getName(), originalIndexMappings); | ||
|
|
||
| // Do a rollover, and then make sure that the updated mappnigs are on the new write index: | ||
| assertAcked(indicesAdmin().rolloverIndex(new RolloverRequest(dataStreamName, null)).actionGet()); | ||
| Map<String, Object> updatedIndexMappings = Map.of( | ||
| "dynamic", | ||
| "strict", | ||
| "_data_stream_timestamp", | ||
| Map.of("enabled", true), | ||
| "properties", | ||
| Map.of( | ||
| "@timestamp", | ||
| Map.of("type", "date"), | ||
| "foo1", | ||
| Map.of("type", "text"), | ||
| "foo2", | ||
| Map.of("type", "keyword"), | ||
| "foo3", | ||
| Map.of("type", "text") | ||
| ) | ||
| ); | ||
| assertExpectedMappingsOnIndex(getDataStream(dataStreamName).getIndices().get(1).getName(), updatedIndexMappings); | ||
|
|
||
| // Now undo the mapping overrides, and expect the original mapping to be in effect: | ||
| updateMappings(dataStreamName, Map.of(), originalMappings, false); | ||
| assertExpectedMappings(dataStreamName, Map.of(), originalMappings); | ||
| assertAcked(indicesAdmin().rolloverIndex(new RolloverRequest(dataStreamName, null)).actionGet()); | ||
| assertExpectedMappingsOnIndex(getDataStream(dataStreamName).getIndices().get(2).getName(), originalIndexMappings); | ||
| } | ||
|
|
||
| private void createDataStream(String dataStreamName) throws IOException { | ||
| String mappingString = """ | ||
| { | ||
| "_doc":{ | ||
| "dynamic":"strict", | ||
| "properties":{ | ||
| "foo1":{ | ||
| "type":"text" | ||
| }, | ||
| "foo2":{ | ||
| "type":"text" | ||
| } | ||
| } | ||
| } | ||
| } | ||
| """; | ||
| CompressedXContent mapping = CompressedXContent.fromJSON(mappingString); | ||
| Template template = new Template(Settings.EMPTY, mapping, null); | ||
| ComposableIndexTemplate.DataStreamTemplate dataStreamTemplate = new ComposableIndexTemplate.DataStreamTemplate(); | ||
| ComposableIndexTemplate composableIndexTemplate = ComposableIndexTemplate.builder() | ||
| .indexPatterns(List.of("my-data-stream-*")) | ||
| .dataStreamTemplate(dataStreamTemplate) | ||
| .template(template) | ||
| .build(); | ||
| TransportPutComposableIndexTemplateAction.Request request = new TransportPutComposableIndexTemplateAction.Request("test"); | ||
| request.indexTemplate(composableIndexTemplate); | ||
|
|
||
| client().execute(TransportPutComposableIndexTemplateAction.TYPE, request).actionGet(); | ||
|
|
||
| BulkRequest bulkRequest = new BulkRequest(); | ||
| bulkRequest.add(new IndexRequest(dataStreamName).source(""" | ||
| { | ||
| "@timestamp": "2024-08-27", | ||
| "foo1": "baz" | ||
| } | ||
| """, XContentType.JSON).id(randomUUID())); | ||
| bulkRequest.add(new IndexRequest(dataStreamName).source(""" | ||
| { | ||
| "@timestamp": "2024-08-27", | ||
| "foo3": "baz" | ||
| } | ||
| """, XContentType.JSON).id(randomUUID())); | ||
| BulkResponse response = client().execute(new ActionType<BulkResponse>(TransportBulkAction.NAME), bulkRequest).actionGet(); | ||
| assertThat(response.getItems().length, equalTo(2)); | ||
| } | ||
|
|
||
| private void assertExpectedMappings( | ||
| String dataStreamName, | ||
| Map<String, Object> expectedMappingOverrides, | ||
| Map<String, Object> expectedEffectiveMappings | ||
| ) { | ||
| GetDataStreamMappingsAction.Response getMappingsResponse = client().execute( | ||
| new ActionType<GetDataStreamMappingsAction.Response>(GetDataStreamMappingsAction.NAME), | ||
| new GetDataStreamMappingsAction.Request(TimeValue.THIRTY_SECONDS).indices(dataStreamName) | ||
| ).actionGet(); | ||
| List<GetDataStreamMappingsAction.DataStreamMappingsResponse> responses = getMappingsResponse.getDataStreamMappingsResponses(); | ||
| assertThat(responses.size(), equalTo(1)); | ||
| GetDataStreamMappingsAction.DataStreamMappingsResponse mappingsResponse = responses.getFirst(); | ||
| assertThat(mappingsResponse.dataStreamName(), equalTo(dataStreamName)); | ||
| assertThat( | ||
| XContentHelper.convertToMap(mappingsResponse.mappings().uncompressed(), true, XContentType.JSON).v2(), | ||
| equalTo(expectedMappingOverrides) | ||
| ); | ||
| assertThat( | ||
| XContentHelper.convertToMap(mappingsResponse.effectiveMappings().uncompressed(), true, XContentType.JSON).v2(), | ||
| equalTo(expectedEffectiveMappings) | ||
| ); | ||
|
|
||
| DataStream dataStream = getDataStream(dataStreamName); | ||
| assertThat( | ||
| XContentHelper.convertToMap(dataStream.getMappings().uncompressed(), true, XContentType.JSON).v2(), | ||
| equalTo(expectedMappingOverrides) | ||
| ); | ||
| } | ||
|
|
||
| private void assertExpectedMappingsOnIndex(String indexName, Map<String, Object> expectedMappings) { | ||
| GetMappingsResponse mappingsResponse = client().execute( | ||
| new ActionType<GetMappingsResponse>(GetMappingsAction.NAME), | ||
| new GetMappingsRequest(TimeValue.THIRTY_SECONDS).indices(indexName) | ||
| ).actionGet(); | ||
| Map<String, MappingMetadata> mappings = mappingsResponse.mappings(); | ||
| assertThat(mappings.size(), equalTo(1)); | ||
| assertThat(mappings.values().iterator().next().sourceAsMap(), equalTo(expectedMappings)); | ||
| } | ||
|
|
||
| private void updateMappings( | ||
| String dataStreamName, | ||
| Map<String, Object> mappingOverrides, | ||
| Map<String, Object> expectedEffectiveMappings, | ||
| boolean dryRun | ||
| ) throws IOException { | ||
| CompressedXContent mappingOverride = new CompressedXContent(mappingOverrides); | ||
| UpdateDataStreamMappingsAction.Response putMappingsResponse = client().execute( | ||
| new ActionType<UpdateDataStreamMappingsAction.Response>(UpdateDataStreamMappingsAction.NAME), | ||
| new UpdateDataStreamMappingsAction.Request(mappingOverride, dryRun, TimeValue.THIRTY_SECONDS, TimeValue.THIRTY_SECONDS).indices( | ||
| dataStreamName | ||
| ) | ||
| ).actionGet(); | ||
| assertThat(putMappingsResponse.getDataStreamMappingsResponses().size(), equalTo(1)); | ||
| UpdateDataStreamMappingsAction.DataStreamMappingsResponse firstPutMappingsResponse = putMappingsResponse | ||
| .getDataStreamMappingsResponses() | ||
| .getFirst(); | ||
| assertThat(firstPutMappingsResponse.dataStreamName(), equalTo(dataStreamName)); | ||
| assertThat( | ||
| XContentHelper.convertToMap(firstPutMappingsResponse.mappings().uncompressed(), true, XContentType.JSON).v2(), | ||
| equalTo(mappingOverrides) | ||
| ); | ||
| assertThat( | ||
| XContentHelper.convertToMap(firstPutMappingsResponse.effectiveMappings().uncompressed(), true, XContentType.JSON).v2(), | ||
| equalTo(expectedEffectiveMappings) | ||
| ); | ||
| } | ||
|
|
||
| private DataStream getDataStream(String dataStreamName) { | ||
| GetDataStreamAction.Request getDataStreamRequest = new GetDataStreamAction.Request( | ||
| TEST_REQUEST_TIMEOUT, | ||
| new String[] { dataStreamName } | ||
| ); | ||
| GetDataStreamAction.Response getDataStreamResponse = client().execute(GetDataStreamAction.INSTANCE, getDataStreamRequest) | ||
| .actionGet(); | ||
| assertThat(getDataStreamResponse.getDataStreams().size(), equalTo(1)); | ||
| return getDataStreamResponse.getDataStreams().get(0).getDataStream(); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
90 changes: 90 additions & 0 deletions
90
.../main/java/org/elasticsearch/datastreams/action/TransportGetDataStreamMappingsAction.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,90 @@ | ||
| /* | ||
| * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
| * or more contributor license agreements. Licensed under the "Elastic License | ||
| * 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side | ||
| * Public License v 1"; you may not use this file except in compliance with, at | ||
| * your election, the "Elastic License 2.0", the "GNU Affero General Public | ||
| * License v3.0 only", or the "Server Side Public License, v 1". | ||
| */ | ||
|
|
||
| package org.elasticsearch.datastreams.action; | ||
|
|
||
| import org.elasticsearch.action.ActionListener; | ||
| import org.elasticsearch.action.admin.indices.settings.get.GetSettingsAction; | ||
| import org.elasticsearch.action.datastreams.GetDataStreamMappingsAction; | ||
| import org.elasticsearch.action.support.ActionFilters; | ||
| import org.elasticsearch.action.support.IndicesOptions; | ||
| import org.elasticsearch.action.support.local.TransportLocalProjectMetadataAction; | ||
| import org.elasticsearch.cluster.ProjectState; | ||
| import org.elasticsearch.cluster.block.ClusterBlockException; | ||
| import org.elasticsearch.cluster.block.ClusterBlockLevel; | ||
| import org.elasticsearch.cluster.metadata.DataStream; | ||
| import org.elasticsearch.cluster.metadata.IndexNameExpressionResolver; | ||
| import org.elasticsearch.cluster.project.ProjectResolver; | ||
| import org.elasticsearch.cluster.service.ClusterService; | ||
| import org.elasticsearch.injection.guice.Inject; | ||
| import org.elasticsearch.tasks.Task; | ||
| import org.elasticsearch.threadpool.ThreadPool; | ||
| import org.elasticsearch.transport.TransportService; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
|
|
||
| public class TransportGetDataStreamMappingsAction extends TransportLocalProjectMetadataAction< | ||
| GetDataStreamMappingsAction.Request, | ||
| GetDataStreamMappingsAction.Response> { | ||
| private final IndexNameExpressionResolver indexNameExpressionResolver; | ||
|
|
||
| @Inject | ||
| public TransportGetDataStreamMappingsAction( | ||
| TransportService transportService, | ||
| ClusterService clusterService, | ||
| ThreadPool threadPool, | ||
| ActionFilters actionFilters, | ||
| ProjectResolver projectResolver, | ||
| IndexNameExpressionResolver indexNameExpressionResolver | ||
| ) { | ||
| super( | ||
| GetSettingsAction.NAME, | ||
| actionFilters, | ||
| transportService.getTaskManager(), | ||
| clusterService, | ||
| threadPool.executor(ThreadPool.Names.MANAGEMENT), | ||
| projectResolver | ||
| ); | ||
| this.indexNameExpressionResolver = indexNameExpressionResolver; | ||
| } | ||
|
|
||
| @Override | ||
| protected ClusterBlockException checkBlock(GetDataStreamMappingsAction.Request request, ProjectState state) { | ||
| return state.blocks().globalBlockedException(ClusterBlockLevel.METADATA_READ); | ||
| } | ||
|
|
||
| @Override | ||
| protected void localClusterStateOperation( | ||
| Task task, | ||
| GetDataStreamMappingsAction.Request request, | ||
| ProjectState project, | ||
| ActionListener<GetDataStreamMappingsAction.Response> listener | ||
| ) throws Exception { | ||
| List<String> dataStreamNames = indexNameExpressionResolver.dataStreamNames( | ||
| clusterService.state(), | ||
| IndicesOptions.DEFAULT, | ||
| request.indices() | ||
| ); | ||
| Map<String, DataStream> dataStreamMap = project.metadata().dataStreams(); | ||
| List<GetDataStreamMappingsAction.DataStreamMappingsResponse> responseList = new ArrayList<>(dataStreamNames.size()); | ||
| for (String dataStreamName : dataStreamNames) { | ||
| DataStream dataStream = dataStreamMap.get(dataStreamName); | ||
| responseList.add( | ||
| new GetDataStreamMappingsAction.DataStreamMappingsResponse( | ||
| dataStreamName, | ||
| dataStream.getMappings(), | ||
| dataStream.getEffectiveMappings(project.metadata()) | ||
| ) | ||
| ); | ||
| } | ||
| listener.onResponse(new GetDataStreamMappingsAction.Response(responseList)); | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
While this works because the
IndexNameExpressionResolvermakes use of the project resolver as well, it's a bit more explicit if we pass aProjectMetadatain directly.