|
22 | 22 | import org.elasticsearch.ExceptionsHelper; |
23 | 23 | import org.elasticsearch.cluster.ClusterService; |
24 | 24 | import org.elasticsearch.cluster.ClusterState; |
| 25 | +import org.elasticsearch.cluster.ClusterStateObserver; |
25 | 26 | import org.elasticsearch.cluster.ClusterStateTaskConfig; |
26 | 27 | import org.elasticsearch.cluster.ClusterStateTaskExecutor; |
27 | 28 | import org.elasticsearch.cluster.ClusterStateTaskListener; |
| 29 | +import org.elasticsearch.cluster.MasterNodeChangePredicate; |
28 | 30 | import org.elasticsearch.cluster.NotMasterException; |
29 | 31 | import org.elasticsearch.cluster.metadata.IndexMetaData; |
30 | 32 | import org.elasticsearch.cluster.node.DiscoveryNode; |
|
42 | 44 | import org.elasticsearch.common.logging.ESLogger; |
43 | 45 | import org.elasticsearch.common.settings.Settings; |
44 | 46 | import org.elasticsearch.common.unit.TimeValue; |
| 47 | +import org.elasticsearch.discovery.Discovery; |
| 48 | +import org.elasticsearch.node.NodeClosedException; |
45 | 49 | import org.elasticsearch.threadpool.ThreadPool; |
46 | 50 | import org.elasticsearch.transport.EmptyTransportResponseHandler; |
| 51 | +import org.elasticsearch.transport.NodeDisconnectedException; |
47 | 52 | import org.elasticsearch.transport.TransportChannel; |
48 | 53 | import org.elasticsearch.transport.TransportException; |
49 | 54 | import org.elasticsearch.transport.TransportRequest; |
50 | 55 | import org.elasticsearch.transport.TransportRequestHandler; |
51 | | -import org.elasticsearch.transport.TransportRequestOptions; |
52 | 56 | import org.elasticsearch.transport.TransportResponse; |
53 | 57 | import org.elasticsearch.transport.TransportService; |
54 | 58 |
|
55 | 59 | import java.io.IOException; |
56 | 60 | import java.util.ArrayList; |
| 61 | +import java.util.Arrays; |
| 62 | +import java.util.HashSet; |
57 | 63 | import java.util.List; |
58 | 64 | import java.util.Locale; |
| 65 | +import java.util.Set; |
59 | 66 |
|
60 | 67 | import static org.elasticsearch.cluster.routing.ShardRouting.readShardRoutingEntry; |
61 | 68 |
|
62 | 69 | public class ShardStateAction extends AbstractComponent { |
| 70 | + |
63 | 71 | public static final String SHARD_STARTED_ACTION_NAME = "internal:cluster/shard/started"; |
64 | 72 | public static final String SHARD_FAILED_ACTION_NAME = "internal:cluster/shard/failure"; |
65 | 73 |
|
66 | 74 | private final TransportService transportService; |
| 75 | + private final ClusterService clusterService; |
67 | 76 |
|
68 | 77 | @Inject |
69 | 78 | public ShardStateAction(Settings settings, ClusterService clusterService, TransportService transportService, |
70 | 79 | AllocationService allocationService, RoutingService routingService) { |
71 | 80 | super(settings); |
72 | 81 | this.transportService = transportService; |
| 82 | + this.clusterService = clusterService; |
73 | 83 |
|
74 | 84 | transportService.registerRequestHandler(SHARD_STARTED_ACTION_NAME, ShardRoutingEntry::new, ThreadPool.Names.SAME, new ShardStartedTransportHandler(clusterService, new ShardStartedClusterStateTaskExecutor(allocationService, logger), logger)); |
75 | 85 | transportService.registerRequestHandler(SHARD_FAILED_ACTION_NAME, ShardRoutingEntry::new, ThreadPool.Names.SAME, new ShardFailedTransportHandler(clusterService, new ShardFailedClusterStateTaskExecutor(allocationService, routingService, logger), logger)); |
76 | 86 | } |
77 | 87 |
|
78 | | - public void shardFailed(final ClusterState clusterState, final ShardRouting shardRouting, final String indexUUID, final String message, @Nullable final Throwable failure, Listener listener) { |
79 | | - shardFailed(clusterState, shardRouting, indexUUID, message, failure, null, listener); |
| 88 | + public void shardFailed(final ShardRouting shardRouting, final String indexUUID, final String message, @Nullable final Throwable failure, Listener listener) { |
| 89 | + ClusterStateObserver observer = new ClusterStateObserver(clusterService, null, logger); |
| 90 | + ShardRoutingEntry shardRoutingEntry = new ShardRoutingEntry(shardRouting, indexUUID, message, failure); |
| 91 | + sendShardFailed(observer, shardRoutingEntry, listener); |
80 | 92 | } |
81 | 93 |
|
82 | | - public void resendShardFailed(final ClusterState clusterState, final ShardRouting shardRouting, final String indexUUID, final String message, @Nullable final Throwable failure, Listener listener) { |
| 94 | + public void resendShardFailed(final ShardRouting shardRouting, final String indexUUID, final String message, @Nullable final Throwable failure, Listener listener) { |
83 | 95 | logger.trace("{} re-sending failed shard [{}], index UUID [{}], reason [{}]", shardRouting.shardId(), failure, shardRouting, indexUUID, message); |
84 | | - shardFailed(clusterState, shardRouting, indexUUID, message, failure, listener); |
| 96 | + shardFailed(shardRouting, indexUUID, message, failure, listener); |
85 | 97 | } |
86 | 98 |
|
87 | | - public void shardFailed(final ClusterState clusterState, final ShardRouting shardRouting, final String indexUUID, final String message, @Nullable final Throwable failure, TimeValue timeout, Listener listener) { |
88 | | - DiscoveryNode masterNode = clusterState.nodes().masterNode(); |
| 99 | + private void sendShardFailed(ClusterStateObserver observer, ShardRoutingEntry shardRoutingEntry, Listener listener) { |
| 100 | + DiscoveryNode masterNode = observer.observedState().nodes().masterNode(); |
89 | 101 | if (masterNode == null) { |
90 | | - logger.warn("{} no master known to fail shard [{}]", shardRouting.shardId(), shardRouting); |
91 | | - listener.onShardFailedNoMaster(); |
92 | | - return; |
93 | | - } |
94 | | - ShardRoutingEntry shardRoutingEntry = new ShardRoutingEntry(shardRouting, indexUUID, message, failure); |
95 | | - TransportRequestOptions options = TransportRequestOptions.EMPTY; |
96 | | - if (timeout != null) { |
97 | | - options = TransportRequestOptions.builder().withTimeout(timeout).build(); |
| 102 | + logger.warn("{} no master known to fail shard [{}]", shardRoutingEntry.getShardRouting().shardId(), shardRoutingEntry.getShardRouting()); |
| 103 | + waitForNewMasterAndRetry(observer, shardRoutingEntry, listener); |
| 104 | + } else { |
| 105 | + transportService.sendRequest(masterNode, |
| 106 | + SHARD_FAILED_ACTION_NAME, shardRoutingEntry, new EmptyTransportResponseHandler(ThreadPool.Names.SAME) { |
| 107 | + @Override |
| 108 | + public void handleResponse(TransportResponse.Empty response) { |
| 109 | + listener.onSuccess(); |
| 110 | + } |
| 111 | + |
| 112 | + @Override |
| 113 | + public void handleException(TransportException exp) { |
| 114 | + assert exp.getCause() != null : exp; |
| 115 | + if (isMasterChannelException(exp.getCause())) { |
| 116 | + waitForNewMasterAndRetry(observer, shardRoutingEntry, listener); |
| 117 | + } else { |
| 118 | + logger.warn("{} unexpected failure while sending request to [{}] to fail shard [{}]", exp, shardRoutingEntry.getShardRouting().shardId(), masterNode, shardRoutingEntry); |
| 119 | + listener.onShardFailedFailure(exp); |
| 120 | + } |
| 121 | + } |
| 122 | + }); |
98 | 123 | } |
99 | | - transportService.sendRequest(masterNode, |
100 | | - SHARD_FAILED_ACTION_NAME, shardRoutingEntry, options, new EmptyTransportResponseHandler(ThreadPool.Names.SAME) { |
101 | | - @Override |
102 | | - public void handleResponse(TransportResponse.Empty response) { |
103 | | - listener.onSuccess(); |
104 | | - } |
| 124 | + } |
105 | 125 |
|
106 | | - @Override |
107 | | - public void handleException(TransportException exp) { |
108 | | - logger.warn("{} unexpected failure while sending request to [{}] to fail shard [{}]", exp, shardRoutingEntry.shardRouting.shardId(), masterNode, shardRoutingEntry); |
109 | | - listener.onShardFailedFailure(masterNode, exp); |
| 126 | + private static Set<Class<?>> MASTER_CHANNEL_EXCEPTIONS = |
| 127 | + new HashSet<>(Arrays.asList( |
| 128 | + NotMasterException.class, |
| 129 | + NodeDisconnectedException.class, |
| 130 | + Discovery.FailedToCommitClusterStateException.class |
| 131 | + )); |
| 132 | + private static boolean isMasterChannelException(Throwable cause) { |
| 133 | + return MASTER_CHANNEL_EXCEPTIONS.contains(cause.getClass()); |
| 134 | + } |
| 135 | + |
| 136 | + // visible for testing |
| 137 | + protected void waitForNewMasterAndRetry(ClusterStateObserver observer, ShardRoutingEntry shardRoutingEntry, Listener listener) { |
| 138 | + observer.waitForNextChange(new ClusterStateObserver.Listener() { |
| 139 | + @Override |
| 140 | + public void onNewClusterState(ClusterState state) { |
| 141 | + if (logger.isTraceEnabled()) { |
| 142 | + logger.trace("new cluster state [{}] after waiting for master election to fail shard [{}]", shardRoutingEntry.getShardRouting().shardId(), state.prettyPrint(), shardRoutingEntry); |
110 | 143 | } |
111 | | - }); |
| 144 | + sendShardFailed(observer, shardRoutingEntry, listener); |
| 145 | + } |
| 146 | + |
| 147 | + @Override |
| 148 | + public void onClusterServiceClose() { |
| 149 | + logger.warn("{} node closed while handling failed shard [{}]", shardRoutingEntry.failure, shardRoutingEntry.getShardRouting().getId(), shardRoutingEntry.getShardRouting()); |
| 150 | + listener.onShardFailedFailure(new NodeClosedException(clusterService.localNode())); |
| 151 | + } |
| 152 | + |
| 153 | + @Override |
| 154 | + public void onTimeout(TimeValue timeout) { |
| 155 | + // we wait indefinitely for a new master |
| 156 | + assert false; |
| 157 | + } |
| 158 | + }, MasterNodeChangePredicate.INSTANCE); |
112 | 159 | } |
113 | 160 |
|
114 | 161 | private static class ShardFailedTransportHandler implements TransportRequestHandler<ShardRoutingEntry> { |
@@ -334,10 +381,22 @@ public interface Listener { |
334 | 381 | default void onSuccess() { |
335 | 382 | } |
336 | 383 |
|
337 | | - default void onShardFailedNoMaster() { |
338 | | - } |
339 | | - |
340 | | - default void onShardFailedFailure(final DiscoveryNode master, final TransportException e) { |
| 384 | + /** |
| 385 | + * Notification for non-channel exceptions that are not handled |
| 386 | + * by {@link ShardStateAction}. |
| 387 | + * |
| 388 | + * The exceptions that are handled by {@link ShardStateAction} |
| 389 | + * are: |
| 390 | + * - {@link NotMasterException} |
| 391 | + * - {@link NodeDisconnectedException} |
| 392 | + * - {@link Discovery.FailedToCommitClusterStateException} |
| 393 | + * |
| 394 | + * Any other exception is communicated to the requester via |
| 395 | + * this notification. |
| 396 | + * |
| 397 | + * @param e the unexpected cause of the failure on the master |
| 398 | + */ |
| 399 | + default void onShardFailedFailure(final Exception e) { |
341 | 400 | } |
342 | 401 | } |
343 | 402 | } |
0 commit comments