From 10bbb082a4414831b80fc4e12328299734a5495f Mon Sep 17 00:00:00 2001 From: Jason Tedor Date: Sat, 23 Mar 2019 09:38:49 -0400 Subject: [PATCH] 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. --- .../index/seqno/RetentionLeaseActions.java | 13 ++++++++++--- .../xpack/ccr/CcrRetentionLeaseIT.java | 6 ------ 2 files changed, 10 insertions(+), 9 deletions(-) diff --git a/server/src/main/java/org/elasticsearch/index/seqno/RetentionLeaseActions.java b/server/src/main/java/org/elasticsearch/index/seqno/RetentionLeaseActions.java index 3493271e8d7..2f9043580a6 100644 --- a/server/src/main/java/org/elasticsearch/index/seqno/RetentionLeaseActions.java +++ b/server/src/main/java/org/elasticsearch/index/seqno/RetentionLeaseActions.java @@ -28,6 +28,8 @@ import org.elasticsearch.action.support.single.shard.SingleShardRequest; import org.elasticsearch.action.support.single.shard.TransportSingleShardAction; import org.elasticsearch.cluster.ClusterState; import org.elasticsearch.cluster.metadata.IndexNameExpressionResolver; +import org.elasticsearch.cluster.routing.IndexShardRoutingTable; +import org.elasticsearch.cluster.routing.PlainShardIterator; import org.elasticsearch.cluster.routing.ShardsIterator; import org.elasticsearch.cluster.service.ClusterService; import org.elasticsearch.common.inject.Inject; @@ -42,6 +44,7 @@ import org.elasticsearch.threadpool.ThreadPool; import org.elasticsearch.transport.TransportService; import java.io.IOException; +import java.util.Collections; import java.util.Objects; import java.util.function.Supplier; @@ -84,10 +87,14 @@ public class RetentionLeaseActions { @Override protected ShardsIterator shards(final ClusterState state, final InternalRequest request) { - return state + final IndexShardRoutingTable shardRoutingTable = state .routingTable() - .shardRoutingTable(request.concreteIndex(), request.request().getShardId().id()) - .primaryShardIt(); + .shardRoutingTable(request.concreteIndex(), request.request().getShardId().id()); + if (shardRoutingTable.primaryShard().active()) { + return shardRoutingTable.primaryShardIt(); + } else { + return new PlainShardIterator(request.request().getShardId(), Collections.emptyList()); + } } @Override diff --git a/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/ccr/CcrRetentionLeaseIT.java b/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/ccr/CcrRetentionLeaseIT.java index eb15cef3b1c..c3bb73f0452 100644 --- a/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/ccr/CcrRetentionLeaseIT.java +++ b/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/ccr/CcrRetentionLeaseIT.java @@ -43,7 +43,6 @@ import org.elasticsearch.indices.IndicesService; import org.elasticsearch.plugins.Plugin; import org.elasticsearch.snapshots.RestoreInfo; import org.elasticsearch.snapshots.RestoreService; -import org.elasticsearch.test.junit.annotations.TestLogging; import org.elasticsearch.test.transport.MockTransportService; import org.elasticsearch.transport.ConnectTransportException; import org.elasticsearch.transport.RemoteTransportException; @@ -266,7 +265,6 @@ public class CcrRetentionLeaseIT extends CcrIntegTestCase { } - @AwaitsFix(bugUrl = "https://github.com/elastic/elasticsearch/issues/40089") public void testRetentionLeasesAreNotBeingRenewedAfterRecoveryCompletes() throws Exception { final String leaderIndex = "leader"; final int numberOfShards = randomIntBetween(1, 3); @@ -463,7 +461,6 @@ public class CcrRetentionLeaseIT extends CcrIntegTestCase { } } - @AwaitsFix(bugUrl = "https://github.com/elastic/elasticsearch/issues/40089") public void testUnfollowFailsToRemoveRetentionLeases() throws Exception { final String leaderIndex = "leader"; final String followerIndex = "follower"; @@ -534,7 +531,6 @@ public class CcrRetentionLeaseIT extends CcrIntegTestCase { } } - @AwaitsFix(bugUrl = "https://github.com/elastic/elasticsearch/issues/40089") public void testRetentionLeaseRenewedWhileFollowing() throws Exception { final String leaderIndex = "leader"; final String followerIndex = "follower"; @@ -618,7 +614,6 @@ public class CcrRetentionLeaseIT extends CcrIntegTestCase { } @AwaitsFix(bugUrl = "https://github.com/elastic/elasticsearch/issues/39509") - @TestLogging(value = "org.elasticsearch.xpack.ccr:trace") public void testRetentionLeaseRenewalIsCancelledWhenFollowingIsPaused() throws Exception { final String leaderIndex = "leader"; final String followerIndex = "follower"; @@ -748,7 +743,6 @@ public class CcrRetentionLeaseIT extends CcrIntegTestCase { assertRetentionLeaseRenewal(numberOfShards, numberOfReplicas, followerIndex, leaderIndex); } - @AwaitsFix(bugUrl = "https://github.com/elastic/elasticsearch/issues/40089") public void testRetentionLeaseIsAddedIfItDisappearsWhileFollowing() throws Exception { final String leaderIndex = "leader"; final String followerIndex = "follower";