|
| 1 | +/* |
| 2 | + * Licensed to Elasticsearch under one or more contributor |
| 3 | + * license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright |
| 5 | + * ownership. Elasticsearch licenses this file to you under |
| 6 | + * the Apache License, Version 2.0 (the "License"); you may |
| 7 | + * not use this file except in compliance with the License. |
| 8 | + * You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +package org.elasticsearch.snapshots; |
| 21 | + |
| 22 | +import org.apache.logging.log4j.Logger; |
| 23 | +import org.apache.logging.log4j.message.ParameterizedMessage; |
| 24 | +import org.apache.logging.log4j.util.Supplier; |
| 25 | +import org.elasticsearch.action.ActionListener; |
| 26 | +import org.elasticsearch.action.support.ActionFilters; |
| 27 | +import org.elasticsearch.action.support.master.TransportMasterNodeAction; |
| 28 | +import org.elasticsearch.cluster.ClusterState; |
| 29 | +import org.elasticsearch.cluster.ClusterStateTaskConfig; |
| 30 | +import org.elasticsearch.cluster.ClusterStateTaskExecutor; |
| 31 | +import org.elasticsearch.cluster.ClusterStateTaskListener; |
| 32 | +import org.elasticsearch.cluster.NotMasterException; |
| 33 | +import org.elasticsearch.cluster.SnapshotsInProgress; |
| 34 | +import org.elasticsearch.cluster.block.ClusterBlockException; |
| 35 | +import org.elasticsearch.cluster.metadata.IndexNameExpressionResolver; |
| 36 | +import org.elasticsearch.cluster.service.ClusterService; |
| 37 | +import org.elasticsearch.common.Priority; |
| 38 | +import org.elasticsearch.common.collect.ImmutableOpenMap; |
| 39 | +import org.elasticsearch.common.inject.Inject; |
| 40 | +import org.elasticsearch.common.settings.Settings; |
| 41 | +import org.elasticsearch.index.shard.ShardId; |
| 42 | +import org.elasticsearch.threadpool.ThreadPool; |
| 43 | +import org.elasticsearch.transport.TransportService; |
| 44 | + |
| 45 | +import java.util.ArrayList; |
| 46 | +import java.util.List; |
| 47 | + |
| 48 | +import static org.elasticsearch.cluster.SnapshotsInProgress.completed; |
| 49 | + |
| 50 | +/** |
| 51 | + * A {@link TransportSnapshotUpdateStatusAction} receives snapshot state messages from {@link SnapshotShardsService}, |
| 52 | + * then computes and updates the {@link ClusterState}. |
| 53 | + */ |
| 54 | +public class TransportSnapshotUpdateStatusAction extends TransportMasterNodeAction<UpdateSnapshotStatusRequest, |
| 55 | + UpdateSnapshotStatusResponse> { |
| 56 | + private final SnapshotUpdateStateExecutor snapshotStateExecutor; |
| 57 | + |
| 58 | + @Inject |
| 59 | + public TransportSnapshotUpdateStatusAction(Settings settings, TransportService transportService, ClusterService clusterService, |
| 60 | + ThreadPool threadPool, IndexNameExpressionResolver indexNameExpressionResolver, |
| 61 | + ActionFilters actionFilters, SnapshotsService snapshotsService) { |
| 62 | + super(settings, UpdateSnapshotStatusAction.NAME, transportService, clusterService, |
| 63 | + threadPool, actionFilters, indexNameExpressionResolver, UpdateSnapshotStatusRequest::new); |
| 64 | + this.snapshotStateExecutor = new SnapshotUpdateStateExecutor(snapshotsService, logger); |
| 65 | + } |
| 66 | + |
| 67 | + @Override |
| 68 | + protected String executor() { |
| 69 | + return ThreadPool.Names.SAME; |
| 70 | + } |
| 71 | + |
| 72 | + @Override |
| 73 | + protected UpdateSnapshotStatusResponse newResponse() { |
| 74 | + return new UpdateSnapshotStatusResponse(); |
| 75 | + } |
| 76 | + |
| 77 | + @Override |
| 78 | + protected void masterOperation(UpdateSnapshotStatusRequest request, ClusterState state, |
| 79 | + ActionListener<UpdateSnapshotStatusResponse> listener) throws Exception { |
| 80 | + innerUpdateSnapshotState(request, listener); |
| 81 | + } |
| 82 | + |
| 83 | + @Override |
| 84 | + protected ClusterBlockException checkBlock(UpdateSnapshotStatusRequest request, ClusterState state) { |
| 85 | + return null; |
| 86 | + } |
| 87 | + |
| 88 | + void innerUpdateSnapshotState(final UpdateSnapshotStatusRequest request, ActionListener<UpdateSnapshotStatusResponse> listener) { |
| 89 | + logger.trace((Supplier<?>) () -> new ParameterizedMessage("received updated snapshot restore status [{}]", request)); |
| 90 | + clusterService.submitStateUpdateTask( |
| 91 | + "update snapshot state", |
| 92 | + request, |
| 93 | + ClusterStateTaskConfig.build(Priority.NORMAL), |
| 94 | + snapshotStateExecutor, |
| 95 | + new ClusterStateTaskListener() { |
| 96 | + @Override |
| 97 | + public void onFailure(String source, Exception e) { |
| 98 | + logger.error((Supplier<?>) () -> new ParameterizedMessage( |
| 99 | + "unexpected failure while updating snapshot status [{}]", request), e); |
| 100 | + try { |
| 101 | + listener.onFailure(e); |
| 102 | + } catch (Exception channelException) { |
| 103 | + channelException.addSuppressed(e); |
| 104 | + logger.warn((Supplier<?>) () -> new ParameterizedMessage( |
| 105 | + "failed to send failure [{}] while updating snapshot status [{}]", e, request), channelException); |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + @Override |
| 110 | + public void onNoLongerMaster(String source) { |
| 111 | + logger.error("no longer master while updating snapshot status [{}]", request); |
| 112 | + try { |
| 113 | + listener.onFailure(new NotMasterException(source)); |
| 114 | + } catch (Exception channelException) { |
| 115 | + logger.warn((Supplier<?>) () -> new ParameterizedMessage( |
| 116 | + "{} failed to send no longer master updating snapshot[{}]", request.snapshot(), request), channelException); |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + @Override |
| 121 | + public void clusterStateProcessed(String source, ClusterState oldState, ClusterState newState) { |
| 122 | + try { |
| 123 | + listener.onResponse(new UpdateSnapshotStatusResponse()); |
| 124 | + } catch (Exception channelException) { |
| 125 | + logger.warn((Supplier<?>) () -> new ParameterizedMessage( |
| 126 | + "failed to send response after updating snapshot status [{}]", request), channelException); |
| 127 | + } |
| 128 | + } |
| 129 | + } |
| 130 | + ); |
| 131 | + } |
| 132 | + |
| 133 | + // The client node sends the update message to the master, then the master node updates the ClusterState. |
| 134 | + static class SnapshotUpdateStateExecutor implements ClusterStateTaskExecutor<UpdateSnapshotStatusRequest> { |
| 135 | + private final SnapshotsService snapshotsService; |
| 136 | + private final Logger logger; |
| 137 | + |
| 138 | + SnapshotUpdateStateExecutor(SnapshotsService snapshotsService, Logger logger) { |
| 139 | + this.snapshotsService = snapshotsService; |
| 140 | + this.logger = logger; |
| 141 | + } |
| 142 | + |
| 143 | + @Override |
| 144 | + public ClusterTasksResult<UpdateSnapshotStatusRequest> execute(ClusterState currentState, |
| 145 | + List<UpdateSnapshotStatusRequest> tasks) throws Exception { |
| 146 | + final SnapshotsInProgress snapshots = currentState.custom(SnapshotsInProgress.TYPE); |
| 147 | + if (snapshots != null) { |
| 148 | + int changedCount = 0; |
| 149 | + final List<SnapshotsInProgress.Entry> entries = new ArrayList<>(); |
| 150 | + for (SnapshotsInProgress.Entry entry : snapshots.entries()) { |
| 151 | + ImmutableOpenMap.Builder<ShardId, SnapshotsInProgress.ShardSnapshotStatus> shards = ImmutableOpenMap.builder(); |
| 152 | + boolean updated = false; |
| 153 | + |
| 154 | + for (UpdateSnapshotStatusRequest updateSnapshotState : tasks) { |
| 155 | + if (entry.snapshot().equals(updateSnapshotState.snapshot())) { |
| 156 | + if (logger.isTraceEnabled()) { |
| 157 | + logger.trace("[{}] Updating shard [{}] with status [{}]", |
| 158 | + updateSnapshotState.snapshot(), updateSnapshotState.shardId(), updateSnapshotState.status().state()); |
| 159 | + } |
| 160 | + if (updated == false) { |
| 161 | + shards.putAll(entry.shards()); |
| 162 | + updated = true; |
| 163 | + } |
| 164 | + shards.put(updateSnapshotState.shardId(), updateSnapshotState.status()); |
| 165 | + changedCount++; |
| 166 | + } |
| 167 | + } |
| 168 | + |
| 169 | + if (updated) { |
| 170 | + if (completed(shards.values()) == false) { |
| 171 | + entries.add(new SnapshotsInProgress.Entry(entry, shards.build())); |
| 172 | + } else { |
| 173 | + // Snapshot is finished - mark it as done |
| 174 | + // TODO: Add PARTIAL_SUCCESS status? |
| 175 | + SnapshotsInProgress.Entry updatedEntry = new SnapshotsInProgress.Entry(entry, |
| 176 | + SnapshotsInProgress.State.SUCCESS, shards.build()); |
| 177 | + entries.add(updatedEntry); |
| 178 | + // Finalize snapshot in the repository |
| 179 | + snapshotsService.endSnapshot(updatedEntry); |
| 180 | + logger.info("snapshot [{}] is done", updatedEntry.snapshot()); |
| 181 | + } |
| 182 | + } else { |
| 183 | + entries.add(entry); |
| 184 | + } |
| 185 | + } |
| 186 | + if (changedCount > 0) { |
| 187 | + if (logger.isTraceEnabled()) { |
| 188 | + logger.trace("changed cluster state triggered by {} snapshot state updates", changedCount); |
| 189 | + } |
| 190 | + final SnapshotsInProgress updatedSnapshots = new SnapshotsInProgress( |
| 191 | + entries.toArray(new SnapshotsInProgress.Entry[entries.size()])); |
| 192 | + return ClusterTasksResult.<UpdateSnapshotStatusRequest>builder().successes(tasks).build( |
| 193 | + ClusterState.builder(currentState).putCustom(SnapshotsInProgress.TYPE, updatedSnapshots).build()); |
| 194 | + } |
| 195 | + } |
| 196 | + return ClusterTasksResult.<UpdateSnapshotStatusRequest>builder().successes(tasks).build(currentState); |
| 197 | + } |
| 198 | + } |
| 199 | + |
| 200 | +} |
0 commit comments