-
Notifications
You must be signed in to change notification settings - Fork 25.6k
Snapshot: Migrate TransportRequestHandler to TransportMasterNodeAction #27165
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
Merged
Changes from 1 commit
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
1f6bf50
Snapshot: Use TransportMasterNodeAction to update
dnhatn 37b8050
Revert "Snapshot: Use TransportMasterNodeAction to update"
dnhatn 82a98ef
Merge branch 'master' into snapshot-migrate-transport
dnhatn 97c4724
Uses two handlers
dnhatn 82aedcc
Merge branch 'master' into snapshot-migrate-transport
dnhatn 06292d1
add a disruption test
dnhatn ea7ec38
post update messages to the local node
dnhatn cb73eea
try posting status forever
dnhatn 85da45d
do not use sleep
dnhatn 57daa0d
Merge branch 'master' into snapshot-migrate-transport
dnhatn 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
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
224 changes: 32 additions & 192 deletions
224
core/src/main/java/org/elasticsearch/snapshots/SnapshotShardsService.java
Large diffs are not rendered by default.
Oops, something went wrong.
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
200 changes: 200 additions & 0 deletions
200
core/src/main/java/org/elasticsearch/snapshots/TransportSnapshotUpdateStatusAction.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,200 @@ | ||
| /* | ||
| * Licensed to Elasticsearch under one or more contributor | ||
| * license agreements. See the NOTICE file distributed with | ||
| * this work for additional information regarding copyright | ||
| * ownership. Elasticsearch licenses this file to you under | ||
| * the Apache License, Version 2.0 (the "License"); you may | ||
| * not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, | ||
| * software distributed under the License is distributed on an | ||
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| * KIND, either express or implied. See the License for the | ||
| * specific language governing permissions and limitations | ||
| * under the License. | ||
| */ | ||
|
|
||
| package org.elasticsearch.snapshots; | ||
|
|
||
| import org.apache.logging.log4j.Logger; | ||
| import org.apache.logging.log4j.message.ParameterizedMessage; | ||
| import org.apache.logging.log4j.util.Supplier; | ||
| import org.elasticsearch.action.ActionListener; | ||
| import org.elasticsearch.action.support.ActionFilters; | ||
| import org.elasticsearch.action.support.master.TransportMasterNodeAction; | ||
| import org.elasticsearch.cluster.ClusterState; | ||
| import org.elasticsearch.cluster.ClusterStateTaskConfig; | ||
| import org.elasticsearch.cluster.ClusterStateTaskExecutor; | ||
| import org.elasticsearch.cluster.ClusterStateTaskListener; | ||
| import org.elasticsearch.cluster.NotMasterException; | ||
| import org.elasticsearch.cluster.SnapshotsInProgress; | ||
| import org.elasticsearch.cluster.block.ClusterBlockException; | ||
| import org.elasticsearch.cluster.metadata.IndexNameExpressionResolver; | ||
| import org.elasticsearch.cluster.service.ClusterService; | ||
| import org.elasticsearch.common.Priority; | ||
| import org.elasticsearch.common.collect.ImmutableOpenMap; | ||
| import org.elasticsearch.common.inject.Inject; | ||
| import org.elasticsearch.common.settings.Settings; | ||
| import org.elasticsearch.index.shard.ShardId; | ||
| import org.elasticsearch.threadpool.ThreadPool; | ||
| import org.elasticsearch.transport.TransportService; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
|
|
||
| import static org.elasticsearch.cluster.SnapshotsInProgress.completed; | ||
|
|
||
| /** | ||
| * A {@link TransportSnapshotUpdateStatusAction} receives snapshot state messages from {@link SnapshotShardsService}, | ||
| * then computes and updates the {@link ClusterState}. | ||
| */ | ||
| public class TransportSnapshotUpdateStatusAction extends TransportMasterNodeAction<UpdateSnapshotStatusRequest, | ||
| UpdateSnapshotStatusResponse> { | ||
| private final SnapshotUpdateStateExecutor snapshotStateExecutor; | ||
|
|
||
| @Inject | ||
| public TransportSnapshotUpdateStatusAction(Settings settings, TransportService transportService, ClusterService clusterService, | ||
| ThreadPool threadPool, IndexNameExpressionResolver indexNameExpressionResolver, | ||
| ActionFilters actionFilters, SnapshotsService snapshotsService) { | ||
| super(settings, UpdateSnapshotStatusAction.NAME, transportService, clusterService, | ||
| threadPool, actionFilters, indexNameExpressionResolver, UpdateSnapshotStatusRequest::new); | ||
| this.snapshotStateExecutor = new SnapshotUpdateStateExecutor(snapshotsService, logger); | ||
| } | ||
|
|
||
| @Override | ||
| protected String executor() { | ||
| return ThreadPool.Names.SAME; | ||
| } | ||
|
|
||
| @Override | ||
| protected UpdateSnapshotStatusResponse newResponse() { | ||
| return new UpdateSnapshotStatusResponse(); | ||
| } | ||
|
|
||
| @Override | ||
| protected void masterOperation(UpdateSnapshotStatusRequest request, ClusterState state, | ||
| ActionListener<UpdateSnapshotStatusResponse> listener) throws Exception { | ||
| innerUpdateSnapshotState(request, listener); | ||
| } | ||
|
|
||
| @Override | ||
| protected ClusterBlockException checkBlock(UpdateSnapshotStatusRequest request, ClusterState state) { | ||
| return null; | ||
| } | ||
|
|
||
| void innerUpdateSnapshotState(final UpdateSnapshotStatusRequest request, ActionListener<UpdateSnapshotStatusResponse> listener) { | ||
| logger.trace((Supplier<?>) () -> new ParameterizedMessage("received updated snapshot restore status [{}]", request)); | ||
| clusterService.submitStateUpdateTask( | ||
| "update snapshot state", | ||
| request, | ||
| ClusterStateTaskConfig.build(Priority.NORMAL), | ||
| snapshotStateExecutor, | ||
| new ClusterStateTaskListener() { | ||
| @Override | ||
| public void onFailure(String source, Exception e) { | ||
| logger.error((Supplier<?>) () -> new ParameterizedMessage( | ||
| "unexpected failure while updating snapshot status [{}]", request), e); | ||
| try { | ||
| listener.onFailure(e); | ||
| } catch (Exception channelException) { | ||
| channelException.addSuppressed(e); | ||
| logger.warn((Supplier<?>) () -> new ParameterizedMessage( | ||
| "failed to send failure [{}] while updating snapshot status [{}]", e, request), channelException); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public void onNoLongerMaster(String source) { | ||
| logger.error("no longer master while updating snapshot status [{}]", request); | ||
| try { | ||
| listener.onFailure(new NotMasterException(source)); | ||
| } catch (Exception channelException) { | ||
| logger.warn((Supplier<?>) () -> new ParameterizedMessage( | ||
| "{} failed to send no longer master updating snapshot[{}]", request.snapshot(), request), channelException); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public void clusterStateProcessed(String source, ClusterState oldState, ClusterState newState) { | ||
| try { | ||
| listener.onResponse(new UpdateSnapshotStatusResponse()); | ||
| } catch (Exception channelException) { | ||
| logger.warn((Supplier<?>) () -> new ParameterizedMessage( | ||
| "failed to send response after updating snapshot status [{}]", request), channelException); | ||
| } | ||
| } | ||
| } | ||
| ); | ||
| } | ||
|
|
||
| // The client node sends the update message to the master, then the master node updates the ClusterState. | ||
| static class SnapshotUpdateStateExecutor implements ClusterStateTaskExecutor<UpdateSnapshotStatusRequest> { | ||
| private final SnapshotsService snapshotsService; | ||
| private final Logger logger; | ||
|
|
||
| SnapshotUpdateStateExecutor(SnapshotsService snapshotsService, Logger logger) { | ||
| this.snapshotsService = snapshotsService; | ||
| this.logger = logger; | ||
| } | ||
|
|
||
| @Override | ||
| public ClusterTasksResult<UpdateSnapshotStatusRequest> execute(ClusterState currentState, | ||
| List<UpdateSnapshotStatusRequest> tasks) throws Exception { | ||
| final SnapshotsInProgress snapshots = currentState.custom(SnapshotsInProgress.TYPE); | ||
| if (snapshots != null) { | ||
| int changedCount = 0; | ||
| final List<SnapshotsInProgress.Entry> entries = new ArrayList<>(); | ||
| for (SnapshotsInProgress.Entry entry : snapshots.entries()) { | ||
| ImmutableOpenMap.Builder<ShardId, SnapshotsInProgress.ShardSnapshotStatus> shards = ImmutableOpenMap.builder(); | ||
| boolean updated = false; | ||
|
|
||
| for (UpdateSnapshotStatusRequest updateSnapshotState : tasks) { | ||
| if (entry.snapshot().equals(updateSnapshotState.snapshot())) { | ||
| if (logger.isTraceEnabled()) { | ||
| logger.trace("[{}] Updating shard [{}] with status [{}]", | ||
| updateSnapshotState.snapshot(), updateSnapshotState.shardId(), updateSnapshotState.status().state()); | ||
| } | ||
| if (updated == false) { | ||
| shards.putAll(entry.shards()); | ||
| updated = true; | ||
| } | ||
| shards.put(updateSnapshotState.shardId(), updateSnapshotState.status()); | ||
| changedCount++; | ||
| } | ||
| } | ||
|
|
||
| if (updated) { | ||
| if (completed(shards.values()) == false) { | ||
| entries.add(new SnapshotsInProgress.Entry(entry, shards.build())); | ||
| } else { | ||
| // Snapshot is finished - mark it as done | ||
| // TODO: Add PARTIAL_SUCCESS status? | ||
| SnapshotsInProgress.Entry updatedEntry = new SnapshotsInProgress.Entry(entry, | ||
| SnapshotsInProgress.State.SUCCESS, shards.build()); | ||
| entries.add(updatedEntry); | ||
| // Finalize snapshot in the repository | ||
| snapshotsService.endSnapshot(updatedEntry); | ||
| logger.info("snapshot [{}] is done", updatedEntry.snapshot()); | ||
| } | ||
| } else { | ||
| entries.add(entry); | ||
| } | ||
| } | ||
| if (changedCount > 0) { | ||
| if (logger.isTraceEnabled()) { | ||
| logger.trace("changed cluster state triggered by {} snapshot state updates", changedCount); | ||
| } | ||
| final SnapshotsInProgress updatedSnapshots = new SnapshotsInProgress( | ||
| entries.toArray(new SnapshotsInProgress.Entry[entries.size()])); | ||
| return ClusterTasksResult.<UpdateSnapshotStatusRequest>builder().successes(tasks).build( | ||
| ClusterState.builder(currentState).putCustom(SnapshotsInProgress.TYPE, updatedSnapshots).build()); | ||
| } | ||
| } | ||
| return ClusterTasksResult.<UpdateSnapshotStatusRequest>builder().successes(tasks).build(currentState); | ||
| } | ||
| } | ||
|
|
||
| } |
43 changes: 43 additions & 0 deletions
43
core/src/main/java/org/elasticsearch/snapshots/UpdateSnapshotStatusAction.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,43 @@ | ||
| /* | ||
| * Licensed to Elasticsearch under one or more contributor | ||
| * license agreements. See the NOTICE file distributed with | ||
| * this work for additional information regarding copyright | ||
| * ownership. Elasticsearch licenses this file to you under | ||
| * the Apache License, Version 2.0 (the "License"); you may | ||
| * not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, | ||
| * software distributed under the License is distributed on an | ||
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| * KIND, either express or implied. See the License for the | ||
| * specific language governing permissions and limitations | ||
| * under the License. | ||
| */ | ||
|
|
||
| package org.elasticsearch.snapshots; | ||
|
|
||
| import org.elasticsearch.action.Action; | ||
| import org.elasticsearch.client.ElasticsearchClient; | ||
|
|
||
| public class UpdateSnapshotStatusAction extends Action<UpdateSnapshotStatusRequest, UpdateSnapshotStatusResponse, | ||
| UpdateSnapshotStatusRequestBuilder> { | ||
| public static final UpdateSnapshotStatusAction INSTANCE = new UpdateSnapshotStatusAction(); | ||
| public static final String NAME = "internal:cluster/snapshot/update_snapshot"; | ||
|
|
||
| public UpdateSnapshotStatusAction() { | ||
| super(NAME); | ||
| } | ||
|
|
||
| @Override | ||
| public UpdateSnapshotStatusRequestBuilder newRequestBuilder(ElasticsearchClient client) { | ||
| return new UpdateSnapshotStatusRequestBuilder(client, UpdateSnapshotStatusAction.INSTANCE); | ||
| } | ||
|
|
||
| @Override | ||
| public UpdateSnapshotStatusResponse newResponse() { | ||
| return new UpdateSnapshotStatusResponse(); | ||
| } | ||
| } |
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.
This is clever, but at the same time might be trap-y. This class is used in a lot of places and I think I would rather have 2 different clean implementation in 6.x and one clean implementation in 7.x then this here.
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.
I agree, having two handlers may be safer than this approach.