|
| 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 | +package org.elasticsearch.action.admin.indices.close; |
| 20 | + |
| 21 | +import org.elasticsearch.action.ActionListener; |
| 22 | +import org.elasticsearch.action.admin.indices.flush.FlushRequest; |
| 23 | +import org.elasticsearch.action.support.ActionFilters; |
| 24 | +import org.elasticsearch.action.support.replication.ReplicationOperation; |
| 25 | +import org.elasticsearch.action.support.replication.ReplicationRequest; |
| 26 | +import org.elasticsearch.action.support.replication.ReplicationResponse; |
| 27 | +import org.elasticsearch.action.support.replication.TransportReplicationAction; |
| 28 | +import org.elasticsearch.cluster.action.shard.ShardStateAction; |
| 29 | +import org.elasticsearch.cluster.block.ClusterBlock; |
| 30 | +import org.elasticsearch.cluster.block.ClusterBlocks; |
| 31 | +import org.elasticsearch.cluster.metadata.IndexNameExpressionResolver; |
| 32 | +import org.elasticsearch.cluster.service.ClusterService; |
| 33 | +import org.elasticsearch.common.inject.Inject; |
| 34 | +import org.elasticsearch.common.io.stream.StreamInput; |
| 35 | +import org.elasticsearch.common.io.stream.StreamOutput; |
| 36 | +import org.elasticsearch.common.lease.Releasable; |
| 37 | +import org.elasticsearch.common.settings.Settings; |
| 38 | +import org.elasticsearch.index.shard.IndexShard; |
| 39 | +import org.elasticsearch.index.shard.ShardId; |
| 40 | +import org.elasticsearch.indices.IndicesService; |
| 41 | +import org.elasticsearch.tasks.TaskId; |
| 42 | +import org.elasticsearch.threadpool.ThreadPool; |
| 43 | +import org.elasticsearch.transport.TransportService; |
| 44 | + |
| 45 | +import java.io.IOException; |
| 46 | +import java.util.Objects; |
| 47 | +import java.util.function.Consumer; |
| 48 | + |
| 49 | +public class TransportVerifyShardBeforeCloseAction extends TransportReplicationAction< |
| 50 | + TransportVerifyShardBeforeCloseAction.ShardRequest, TransportVerifyShardBeforeCloseAction.ShardRequest, ReplicationResponse> { |
| 51 | + |
| 52 | + public static final String NAME = CloseIndexAction.NAME + "[s]"; |
| 53 | + |
| 54 | + @Inject |
| 55 | + public TransportVerifyShardBeforeCloseAction(final Settings settings, final TransportService transportService, |
| 56 | + final ClusterService clusterService, final IndicesService indicesService, |
| 57 | + final ThreadPool threadPool, final ShardStateAction stateAction, |
| 58 | + final ActionFilters actionFilters, final IndexNameExpressionResolver resolver) { |
| 59 | + super(settings, NAME, transportService, clusterService, indicesService, threadPool, stateAction, actionFilters, resolver, |
| 60 | + ShardRequest::new, ShardRequest::new, ThreadPool.Names.MANAGEMENT); |
| 61 | + } |
| 62 | + |
| 63 | + @Override |
| 64 | + protected ReplicationResponse newResponseInstance() { |
| 65 | + return new ReplicationResponse(); |
| 66 | + } |
| 67 | + |
| 68 | + @Override |
| 69 | + protected void acquirePrimaryOperationPermit(final IndexShard primary, |
| 70 | + final ShardRequest request, |
| 71 | + final ActionListener<Releasable> onAcquired) { |
| 72 | + primary.acquireAllPrimaryOperationsPermits(onAcquired, request.timeout()); |
| 73 | + } |
| 74 | + |
| 75 | + @Override |
| 76 | + protected void acquireReplicaOperationPermit(final IndexShard replica, |
| 77 | + final ShardRequest request, |
| 78 | + final ActionListener<Releasable> onAcquired, |
| 79 | + final long primaryTerm, |
| 80 | + final long globalCheckpoint, |
| 81 | + final long maxSeqNoOfUpdateOrDeletes) { |
| 82 | + replica.acquireAllReplicaOperationsPermits(primaryTerm, globalCheckpoint, maxSeqNoOfUpdateOrDeletes, onAcquired, request.timeout()); |
| 83 | + } |
| 84 | + |
| 85 | + @Override |
| 86 | + protected PrimaryResult<ShardRequest, ReplicationResponse> shardOperationOnPrimary(final ShardRequest shardRequest, |
| 87 | + final IndexShard primary) throws Exception { |
| 88 | + executeShardOperation(shardRequest, primary); |
| 89 | + return new PrimaryResult<>(shardRequest, new ReplicationResponse()); |
| 90 | + } |
| 91 | + |
| 92 | + @Override |
| 93 | + protected ReplicaResult shardOperationOnReplica(final ShardRequest shardRequest, final IndexShard replica) throws Exception { |
| 94 | + executeShardOperation(shardRequest, replica); |
| 95 | + return new ReplicaResult(); |
| 96 | + } |
| 97 | + |
| 98 | + private void executeShardOperation(final ShardRequest request, final IndexShard indexShard) { |
| 99 | + final ShardId shardId = indexShard.shardId(); |
| 100 | + if (indexShard.getActiveOperationsCount() != 0) { |
| 101 | + throw new IllegalStateException("On-going operations in progress while checking index shard " + shardId + " before closing"); |
| 102 | + } |
| 103 | + |
| 104 | + final ClusterBlocks clusterBlocks = clusterService.state().blocks(); |
| 105 | + if (clusterBlocks.hasIndexBlock(shardId.getIndexName(), request.clusterBlock()) == false) { |
| 106 | + throw new IllegalStateException("Index shard " + shardId + " must be blocked by " + request.clusterBlock() + " before closing"); |
| 107 | + } |
| 108 | + |
| 109 | + final long maxSeqNo = indexShard.seqNoStats().getMaxSeqNo(); |
| 110 | + if (indexShard.getGlobalCheckpoint() != maxSeqNo) { |
| 111 | + throw new IllegalStateException("Global checkpoint [" + indexShard.getGlobalCheckpoint() |
| 112 | + + "] mismatches maximum sequence number [" + maxSeqNo + "] on index shard " + shardId); |
| 113 | + } |
| 114 | + indexShard.flush(new FlushRequest()); |
| 115 | + logger.debug("{} shard is ready for closing", shardId); |
| 116 | + } |
| 117 | + |
| 118 | + @Override |
| 119 | + protected ReplicationOperation.Replicas<ShardRequest> newReplicasProxy(final long primaryTerm) { |
| 120 | + return new VerifyShardBeforeCloseActionReplicasProxy(primaryTerm); |
| 121 | + } |
| 122 | + |
| 123 | + /** |
| 124 | + * A {@link ReplicasProxy} that marks as stale the shards that are unavailable during the verification |
| 125 | + * and the flush of the shard. This is done to ensure that such shards won't be later promoted as primary |
| 126 | + * or reopened in an unverified state with potential non flushed translog operations. |
| 127 | + */ |
| 128 | + class VerifyShardBeforeCloseActionReplicasProxy extends ReplicasProxy { |
| 129 | + |
| 130 | + VerifyShardBeforeCloseActionReplicasProxy(final long primaryTerm) { |
| 131 | + super(primaryTerm); |
| 132 | + } |
| 133 | + |
| 134 | + @Override |
| 135 | + public void markShardCopyAsStaleIfNeeded(final ShardId shardId, final String allocationId, final Runnable onSuccess, |
| 136 | + final Consumer<Exception> onPrimaryDemoted, final Consumer<Exception> onIgnoredFailure) { |
| 137 | + shardStateAction.remoteShardFailed(shardId, allocationId, primaryTerm, true, "mark copy as stale", null, |
| 138 | + createShardActionListener(onSuccess, onPrimaryDemoted, onIgnoredFailure)); |
| 139 | + } |
| 140 | + } |
| 141 | + |
| 142 | + public static class ShardRequest extends ReplicationRequest<ShardRequest> { |
| 143 | + |
| 144 | + private ClusterBlock clusterBlock; |
| 145 | + |
| 146 | + ShardRequest(){ |
| 147 | + } |
| 148 | + |
| 149 | + public ShardRequest(final ShardId shardId, final ClusterBlock clusterBlock, final TaskId parentTaskId) { |
| 150 | + super(shardId); |
| 151 | + this.clusterBlock = Objects.requireNonNull(clusterBlock); |
| 152 | + setParentTask(parentTaskId); |
| 153 | + } |
| 154 | + |
| 155 | + @Override |
| 156 | + public String toString() { |
| 157 | + return "verify shard " + shardId + " before close with block " + clusterBlock; |
| 158 | + } |
| 159 | + |
| 160 | + @Override |
| 161 | + public void readFrom(final StreamInput in) throws IOException { |
| 162 | + super.readFrom(in); |
| 163 | + clusterBlock = ClusterBlock.readClusterBlock(in); |
| 164 | + } |
| 165 | + |
| 166 | + @Override |
| 167 | + public void writeTo(final StreamOutput out) throws IOException { |
| 168 | + super.writeTo(out); |
| 169 | + clusterBlock.writeTo(out); |
| 170 | + } |
| 171 | + |
| 172 | + public ClusterBlock clusterBlock() { |
| 173 | + return clusterBlock; |
| 174 | + } |
| 175 | + } |
| 176 | +} |
0 commit comments