Skip to content

Commit cd9f8cc

Browse files
authored
Use unwrapped cause to determine if node is closing (#39723)
We need to unwrap and use the actual cause when determining if the node with primary shard is shutting down because TransportService will throw a TransportException wrapped in a SendRequestTransportException. Relates #39584
1 parent 42649ff commit cd9f8cc

File tree

3 files changed

+13
-5
lines changed

3 files changed

+13
-5
lines changed

server/src/main/java/org/elasticsearch/action/support/replication/ReplicationOperation.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -204,8 +204,9 @@ public String toString() {
204204
}
205205

206206
private void onNoLongerPrimary(Exception failure) {
207-
final boolean nodeIsClosing = failure instanceof NodeClosedException ||
208-
(failure instanceof TransportException && "TransportService is closed stopped can't send request".equals(failure.getMessage()));
207+
final Throwable cause = ExceptionsHelper.unwrapCause(failure);
208+
final boolean nodeIsClosing = cause instanceof NodeClosedException
209+
|| (cause instanceof TransportException && "TransportService is closed stopped can't send request".equals(cause.getMessage()));
209210
final String message;
210211
if (nodeIsClosing) {
211212
message = String.format(Locale.ROOT,

server/src/test/java/org/elasticsearch/action/support/replication/ReplicationOperationTests.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
import org.elasticsearch.index.shard.ShardId;
4444
import org.elasticsearch.node.NodeClosedException;
4545
import org.elasticsearch.test.ESTestCase;
46+
import org.elasticsearch.transport.SendRequestTransportException;
4647
import org.elasticsearch.transport.TransportException;
4748

4849
import java.util.ArrayList;
@@ -203,7 +204,9 @@ public void testNoLongerPrimary() throws Exception {
203204
if (randomBoolean()) {
204205
shardActionFailure = new NodeClosedException(new DiscoveryNode("foo", buildNewFakeTransportAddress(), Version.CURRENT));
205206
} else if (randomBoolean()) {
206-
shardActionFailure = new TransportException("TransportService is closed stopped can't send request");
207+
shardActionFailure = new SendRequestTransportException(
208+
new DiscoveryNode("foo", buildNewFakeTransportAddress(), Version.CURRENT), "internal:cluster/shard/failure",
209+
new TransportException("TransportService is closed stopped can't send request"));
207210
} else {
208211
shardActionFailure = new ShardStateAction.NoLongerPrimaryShardException(failedReplica.shardId(), "the king is dead");
209212
}

server/src/test/java/org/elasticsearch/discovery/ClusterDisruptionIT.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,10 @@
7171
import static org.elasticsearch.action.DocWriteResponse.Result.UPDATED;
7272
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertAcked;
7373
import static org.hamcrest.Matchers.equalTo;
74+
import static org.hamcrest.Matchers.everyItem;
7475
import static org.hamcrest.Matchers.greaterThanOrEqualTo;
7576
import static org.hamcrest.Matchers.is;
77+
import static org.hamcrest.Matchers.isIn;
7678
import static org.hamcrest.Matchers.isOneOf;
7779
import static org.hamcrest.Matchers.not;
7880

@@ -480,8 +482,10 @@ public void testRestartNodeWhileIndexing() throws Exception {
480482
for (ShardRouting shardRouting : clusterState.routingTable().allShards(index)) {
481483
String nodeName = clusterState.nodes().get(shardRouting.currentNodeId()).getName();
482484
IndicesService indicesService = internalCluster().getInstance(IndicesService.class, nodeName);
483-
IndexShard indexShard = indicesService.getShardOrNull(shardRouting.shardId());
484-
assertThat(IndexShardTestCase.getShardDocUIDs(indexShard), equalTo(ackedDocs));
485+
IndexShard shard = indicesService.getShardOrNull(shardRouting.shardId());
486+
Set<String> docs = IndexShardTestCase.getShardDocUIDs(shard);
487+
assertThat("shard [" + shard.routingEntry() + "] docIds [" + docs + "] vs " + " acked docIds [" + ackedDocs + "]",
488+
ackedDocs, everyItem(isIn(docs)));
485489
}
486490
}
487491
}

0 commit comments

Comments
 (0)