-
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
Changes from 4 commits
20a6a75
e4d598c
88c64de
0d834b5
b0a0579
cb3b333
6cbbc7f
08e594c
55d1229
2594cff
77acfd9
d552852
a98bc61
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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)); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,197 @@ | ||
| /* | ||
| * 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.apache.logging.log4j.LogManager; | ||
| import org.apache.logging.log4j.Logger; | ||
| import org.elasticsearch.action.ActionListener; | ||
| import org.elasticsearch.action.datastreams.UpdateDataStreamMappingsAction; | ||
| import org.elasticsearch.action.support.ActionFilters; | ||
| import org.elasticsearch.action.support.CountDownActionListener; | ||
| import org.elasticsearch.action.support.IndicesOptions; | ||
| import org.elasticsearch.action.support.master.TransportMasterNodeAction; | ||
| import org.elasticsearch.cluster.ClusterState; | ||
| import org.elasticsearch.cluster.block.ClusterBlockException; | ||
| import org.elasticsearch.cluster.block.ClusterBlockLevel; | ||
| import org.elasticsearch.cluster.metadata.IndexNameExpressionResolver; | ||
| import org.elasticsearch.cluster.metadata.MetadataDataStreamsService; | ||
| import org.elasticsearch.cluster.project.ProjectResolver; | ||
| import org.elasticsearch.cluster.service.ClusterService; | ||
| import org.elasticsearch.common.Strings; | ||
| import org.elasticsearch.common.compress.CompressedXContent; | ||
| import org.elasticsearch.common.util.concurrent.EsExecutors; | ||
| import org.elasticsearch.core.TimeValue; | ||
| import org.elasticsearch.index.mapper.Mapping; | ||
| import org.elasticsearch.indices.SystemIndices; | ||
| import org.elasticsearch.injection.guice.Inject; | ||
| import org.elasticsearch.tasks.Task; | ||
| import org.elasticsearch.threadpool.ThreadPool; | ||
| import org.elasticsearch.transport.TransportService; | ||
|
|
||
| import java.io.IOException; | ||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
|
|
||
| public class TransportUpdateDataStreamMappingsAction extends TransportMasterNodeAction< | ||
| UpdateDataStreamMappingsAction.Request, | ||
| UpdateDataStreamMappingsAction.Response> { | ||
| private static final Logger logger = LogManager.getLogger(TransportUpdateDataStreamMappingsAction.class); | ||
| private final MetadataDataStreamsService metadataDataStreamsService; | ||
| private final IndexNameExpressionResolver indexNameExpressionResolver; | ||
| private final SystemIndices systemIndices; | ||
| private final ProjectResolver projectResolver; | ||
|
|
||
| @Inject | ||
| public TransportUpdateDataStreamMappingsAction( | ||
| TransportService transportService, | ||
| ClusterService clusterService, | ||
| ThreadPool threadPool, | ||
| ActionFilters actionFilters, | ||
| ProjectResolver projectResolver, | ||
| MetadataDataStreamsService metadataDataStreamsService, | ||
| IndexNameExpressionResolver indexNameExpressionResolver, | ||
| SystemIndices systemIndices | ||
| ) { | ||
| super( | ||
| UpdateDataStreamMappingsAction.NAME, | ||
| transportService, | ||
| clusterService, | ||
| threadPool, | ||
| actionFilters, | ||
| UpdateDataStreamMappingsAction.Request::new, | ||
| UpdateDataStreamMappingsAction.Response::new, | ||
| EsExecutors.DIRECT_EXECUTOR_SERVICE | ||
| ); | ||
| this.projectResolver = projectResolver; | ||
| this.metadataDataStreamsService = metadataDataStreamsService; | ||
| this.indexNameExpressionResolver = indexNameExpressionResolver; | ||
| this.systemIndices = systemIndices; | ||
| } | ||
|
|
||
| @Override | ||
| protected void masterOperation( | ||
| Task task, | ||
| UpdateDataStreamMappingsAction.Request request, | ||
| ClusterState state, | ||
| ActionListener<UpdateDataStreamMappingsAction.Response> listener | ||
| ) throws Exception { | ||
| List<String> dataStreamNames = indexNameExpressionResolver.dataStreamNames( | ||
| clusterService.state(), | ||
|
||
| IndicesOptions.DEFAULT, | ||
| request.indices() | ||
| ); | ||
| List<UpdateDataStreamMappingsAction.DataStreamMappingsResponse> dataStreamMappingsResponse = new ArrayList<>(); | ||
| CountDownActionListener countDownListener = new CountDownActionListener( | ||
| dataStreamNames.size() + 1, | ||
| listener.delegateFailure( | ||
| (responseActionListener, unused) -> responseActionListener.onResponse( | ||
| new UpdateDataStreamMappingsAction.Response(dataStreamMappingsResponse) | ||
| ) | ||
| ) | ||
| ); | ||
| countDownListener.onResponse(null); | ||
| for (String dataStreamName : dataStreamNames) { | ||
| updateSingleDataStream( | ||
| dataStreamName, | ||
| request.getMappings(), | ||
| request.masterNodeTimeout(), | ||
| request.ackTimeout(), | ||
| request.isDryRun(), | ||
| ActionListener.wrap(dataStreamResponse -> { | ||
| dataStreamMappingsResponse.add(dataStreamResponse); | ||
| countDownListener.onResponse(null); | ||
| }, e -> { | ||
| dataStreamMappingsResponse.add( | ||
| new UpdateDataStreamMappingsAction.DataStreamMappingsResponse( | ||
| dataStreamName, | ||
| false, | ||
| Strings.hasText(e.getMessage()) ? e.getMessage() : e.toString(), | ||
| Mapping.EMPTY.toCompressedXContent(), | ||
| Mapping.EMPTY.toCompressedXContent() | ||
| ) | ||
| ); | ||
| countDownListener.onResponse(null); | ||
| }) | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| private void updateSingleDataStream( | ||
| String dataStreamName, | ||
| CompressedXContent mappingsOverrides, | ||
| TimeValue masterNodeTimeout, | ||
| TimeValue ackTimeout, | ||
| boolean dryRun, | ||
| ActionListener<UpdateDataStreamMappingsAction.DataStreamMappingsResponse> listener | ||
| ) { | ||
| logger.debug("updating mappings for {}", dataStreamName); | ||
| if (systemIndices.isSystemDataStream(dataStreamName)) { | ||
| listener.onResponse( | ||
| new UpdateDataStreamMappingsAction.DataStreamMappingsResponse( | ||
| dataStreamName, | ||
| false, | ||
| "Cannot update a system data stream", | ||
| Mapping.EMPTY.toCompressedXContent(), | ||
| Mapping.EMPTY.toCompressedXContent() | ||
| ) | ||
| ); | ||
| return; | ||
| } | ||
| metadataDataStreamsService.updateMappings( | ||
| projectResolver.getProjectId(), | ||
| masterNodeTimeout, | ||
| ackTimeout, | ||
| dataStreamName, | ||
| mappingsOverrides, | ||
| dryRun, | ||
| listener.delegateFailure((dataStreamMappingsResponseActionListener, dataStream) -> { | ||
| if (dataStream != null) { | ||
| try { | ||
| dataStreamMappingsResponseActionListener.onResponse( | ||
| new UpdateDataStreamMappingsAction.DataStreamMappingsResponse( | ||
| dataStreamName, | ||
| true, | ||
| null, | ||
| mappingsOverrides, | ||
| dataStream.getEffectiveMappings( | ||
| clusterService.state().projectState(projectResolver.getProjectId()).metadata() | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We can also avoid constructing the
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we have to construct a ProjectState here, right (using a stored-off projectId)? The alternative is to grab the ProjectMetadata in the earlier thread and store it off, but that ProjectMetadata could be out of date at the time this thread is executed.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We can do sorry for not explicitly mentioning that before |
||
| ) | ||
| ) | ||
| ); | ||
| } catch (IOException e) { | ||
| dataStreamMappingsResponseActionListener.onResponse( | ||
| new UpdateDataStreamMappingsAction.DataStreamMappingsResponse( | ||
| dataStreamName, | ||
| false, | ||
| e.getMessage(), | ||
| Mapping.EMPTY.toCompressedXContent(), | ||
| Mapping.EMPTY.toCompressedXContent() | ||
| ) | ||
| ); | ||
| } | ||
| } else { | ||
| dataStreamMappingsResponseActionListener.onResponse( | ||
| new UpdateDataStreamMappingsAction.DataStreamMappingsResponse( | ||
| dataStreamName, | ||
| false, | ||
| "Updating mappings not accepted for unknown reasons", | ||
| Mapping.EMPTY.toCompressedXContent(), | ||
| Mapping.EMPTY.toCompressedXContent() | ||
| ) | ||
| ); | ||
| } | ||
| }) | ||
| ); | ||
| } | ||
|
|
||
| @Override | ||
| protected ClusterBlockException checkBlock(UpdateDataStreamMappingsAction.Request request, ClusterState state) { | ||
| return state.blocks().globalBlockedException(ClusterBlockLevel.METADATA_WRITE); | ||
| } | ||
| } | ||
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.