Skip to content

Commit b1c6f8e

Browse files
authored
Avoid background sync on relocated primary (#40800)
There were some test failures caused by the background retention lease sync running on a relocated primary. This commit fixes the situation that triggered the assertion and reactivates the failing test. Closes #40731
1 parent b100f04 commit b1c6f8e

File tree

3 files changed

+32
-3
lines changed

3 files changed

+32
-3
lines changed

qa/rolling-upgrade/src/test/java/org/elasticsearch/upgrades/RecoveryIT.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,6 @@ protected void doRun() throws Exception {
121121
return future;
122122
}
123123

124-
@AwaitsFix(bugUrl = "https://github.com/elastic/elasticsearch/issues/40731")
125124
public void testRecoveryWithConcurrentIndexing() throws Exception {
126125
final String index = "recovery_with_concurrent_indexing";
127126
Response response = client().performRequest(new Request("GET", "_nodes"));

server/src/main/java/org/elasticsearch/index/IndexService.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -830,7 +830,11 @@ private void sync(final Consumer<IndexShard> sync, final String source) {
830830
case STARTED:
831831
try {
832832
shard.runUnderPrimaryPermit(
833-
() -> sync.accept(shard),
833+
() -> {
834+
if (shard.isRelocatedPrimary() == false) {
835+
sync.accept(shard);
836+
}
837+
},
834838
e -> {
835839
if (e instanceof AlreadyClosedException == false
836840
&& e instanceof IndexShardClosedException == false) {

server/src/test/java/org/elasticsearch/recovery/RecoveryWhileUnderLoadIT.java

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,20 +30,28 @@
3030
import org.elasticsearch.cluster.routing.ShardRouting;
3131
import org.elasticsearch.cluster.service.ClusterService;
3232
import org.elasticsearch.common.Priority;
33+
import org.elasticsearch.common.settings.Setting;
3334
import org.elasticsearch.common.settings.Settings;
3435
import org.elasticsearch.common.unit.TimeValue;
36+
import org.elasticsearch.index.IndexService;
3537
import org.elasticsearch.index.IndexSettings;
3638
import org.elasticsearch.index.shard.DocsStats;
3739
import org.elasticsearch.index.shard.ShardId;
3840
import org.elasticsearch.index.translog.Translog;
41+
import org.elasticsearch.plugins.Plugin;
3942
import org.elasticsearch.search.sort.SortOrder;
4043
import org.elasticsearch.test.BackgroundIndexer;
4144
import org.elasticsearch.test.ESIntegTestCase;
4245
import org.elasticsearch.test.junit.annotations.TestLogging;
4346

4447
import java.util.Arrays;
48+
import java.util.Collection;
49+
import java.util.Collections;
50+
import java.util.List;
4551
import java.util.Set;
4652
import java.util.concurrent.TimeUnit;
53+
import java.util.stream.Collectors;
54+
import java.util.stream.Stream;
4755

4856
import static org.elasticsearch.cluster.metadata.IndexMetaData.SETTING_NUMBER_OF_REPLICAS;
4957
import static org.elasticsearch.cluster.metadata.IndexMetaData.SETTING_NUMBER_OF_SHARDS;
@@ -58,6 +66,23 @@
5866
public class RecoveryWhileUnderLoadIT extends ESIntegTestCase {
5967
private final Logger logger = LogManager.getLogger(RecoveryWhileUnderLoadIT.class);
6068

69+
public static final class RetentionLeaseSyncIntervalSettingPlugin extends Plugin {
70+
71+
@Override
72+
public List<Setting<?>> getSettings() {
73+
return Collections.singletonList(IndexService.RETENTION_LEASE_SYNC_INTERVAL_SETTING);
74+
}
75+
76+
}
77+
78+
@Override
79+
protected Collection<Class<? extends Plugin>> nodePlugins() {
80+
return Stream.concat(
81+
super.nodePlugins().stream(),
82+
Stream.of(RetentionLeaseSyncIntervalSettingPlugin.class))
83+
.collect(Collectors.toList());
84+
}
85+
6186
public void testRecoverWhileUnderLoadAllocateReplicasTest() throws Exception {
6287
logger.info("--> creating test index ...");
6388
int numberOfShards = numberOfShards();
@@ -260,7 +285,8 @@ public void testRecoverWhileRelocating() throws Exception {
260285
assertAcked(prepareCreate("test", 3, Settings.builder()
261286
.put(SETTING_NUMBER_OF_SHARDS, numShards)
262287
.put(SETTING_NUMBER_OF_REPLICAS, numReplicas)
263-
.put(IndexSettings.INDEX_TRANSLOG_DURABILITY_SETTING.getKey(), Translog.Durability.ASYNC)));
288+
.put(IndexSettings.INDEX_TRANSLOG_DURABILITY_SETTING.getKey(), Translog.Durability.ASYNC)
289+
.put(IndexService.RETENTION_LEASE_SYNC_INTERVAL_SETTING.getKey(), randomFrom("100ms", "1s", "5s", "30s", "60s"))));
264290

265291
final int numDocs = scaledRandomIntBetween(200, 9999);
266292

0 commit comments

Comments
 (0)