Skip to content

Commit d193f29

Browse files
authored
Only run retention lease actions on active primary (#40386)
In some cases, a request to perform a retention lease action can arrive on a primary shard before it is active. In this case, the primary shard would not yet be in primary mode, tripping an assertion in the replication tracker. Instead, we should not attempt to perform such actions on an initializing shard. This commit addresses this by not returning the primary shard in the single shard iterator if the primary shard is not yet active.
1 parent 41d0a03 commit d193f29

File tree

2 files changed

+10
-9
lines changed

2 files changed

+10
-9
lines changed

server/src/main/java/org/elasticsearch/index/seqno/RetentionLeaseActions.java

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@
2828
import org.elasticsearch.action.support.single.shard.TransportSingleShardAction;
2929
import org.elasticsearch.cluster.ClusterState;
3030
import org.elasticsearch.cluster.metadata.IndexNameExpressionResolver;
31+
import org.elasticsearch.cluster.routing.IndexShardRoutingTable;
32+
import org.elasticsearch.cluster.routing.PlainShardIterator;
3133
import org.elasticsearch.cluster.routing.ShardsIterator;
3234
import org.elasticsearch.cluster.service.ClusterService;
3335
import org.elasticsearch.common.inject.Inject;
@@ -42,6 +44,7 @@
4244
import org.elasticsearch.transport.TransportService;
4345

4446
import java.io.IOException;
47+
import java.util.Collections;
4548
import java.util.Objects;
4649
import java.util.function.Supplier;
4750

@@ -84,10 +87,14 @@ abstract static class TransportRetentionLeaseAction<T extends Request<T>> extend
8487

8588
@Override
8689
protected ShardsIterator shards(final ClusterState state, final InternalRequest request) {
87-
return state
90+
final IndexShardRoutingTable shardRoutingTable = state
8891
.routingTable()
89-
.shardRoutingTable(request.concreteIndex(), request.request().getShardId().id())
90-
.primaryShardIt();
92+
.shardRoutingTable(request.concreteIndex(), request.request().getShardId().id());
93+
if (shardRoutingTable.primaryShard().active()) {
94+
return shardRoutingTable.primaryShardIt();
95+
} else {
96+
return new PlainShardIterator(request.request().getShardId(), Collections.emptyList());
97+
}
9198
}
9299

93100
@Override

x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/ccr/CcrRetentionLeaseIT.java

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@
4343
import org.elasticsearch.plugins.Plugin;
4444
import org.elasticsearch.snapshots.RestoreInfo;
4545
import org.elasticsearch.snapshots.RestoreService;
46-
import org.elasticsearch.test.junit.annotations.TestLogging;
4746
import org.elasticsearch.test.transport.MockTransportService;
4847
import org.elasticsearch.transport.ConnectTransportException;
4948
import org.elasticsearch.transport.RemoteTransportException;
@@ -266,7 +265,6 @@ public void testRetentionLeaseIsRenewedDuringRecovery() throws Exception {
266265

267266
}
268267

269-
@AwaitsFix(bugUrl = "https://github.com/elastic/elasticsearch/issues/40089")
270268
public void testRetentionLeasesAreNotBeingRenewedAfterRecoveryCompletes() throws Exception {
271269
final String leaderIndex = "leader";
272270
final int numberOfShards = randomIntBetween(1, 3);
@@ -463,7 +461,6 @@ public void testUnfollowRemovesRetentionLeases() throws Exception {
463461
}
464462
}
465463

466-
@AwaitsFix(bugUrl = "https://github.com/elastic/elasticsearch/issues/40089")
467464
public void testUnfollowFailsToRemoveRetentionLeases() throws Exception {
468465
final String leaderIndex = "leader";
469466
final String followerIndex = "follower";
@@ -534,7 +531,6 @@ public void testUnfollowFailsToRemoveRetentionLeases() throws Exception {
534531
}
535532
}
536533

537-
@AwaitsFix(bugUrl = "https://github.com/elastic/elasticsearch/issues/40089")
538534
public void testRetentionLeaseRenewedWhileFollowing() throws Exception {
539535
final String leaderIndex = "leader";
540536
final String followerIndex = "follower";
@@ -618,7 +614,6 @@ public void testRetentionLeaseAdvancesWhileFollowing() throws Exception {
618614
}
619615

620616
@AwaitsFix(bugUrl = "https://github.com/elastic/elasticsearch/issues/39509")
621-
@TestLogging(value = "org.elasticsearch.xpack.ccr:trace")
622617
public void testRetentionLeaseRenewalIsCancelledWhenFollowingIsPaused() throws Exception {
623618
final String leaderIndex = "leader";
624619
final String followerIndex = "follower";
@@ -748,7 +743,6 @@ public void testRetentionLeaseRenewalIsResumedWhenFollowingIsResumed() throws Ex
748743
assertRetentionLeaseRenewal(numberOfShards, numberOfReplicas, followerIndex, leaderIndex);
749744
}
750745

751-
@AwaitsFix(bugUrl = "https://github.com/elastic/elasticsearch/issues/40089")
752746
public void testRetentionLeaseIsAddedIfItDisappearsWhileFollowing() throws Exception {
753747
final String leaderIndex = "leader";
754748
final String followerIndex = "follower";

0 commit comments

Comments
 (0)