Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Backport 2.x] Fix SegmentReplicationPressureService to not schedule async tasks when remote failures are disabled #11234

Merged
merged 1 commit into from
Nov 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ public class SegmentReplicationPressureService implements Closeable {

private final ShardStateAction shardStateAction;

private final AsyncFailStaleReplicaTask failStaleReplicaTask;
private volatile AsyncFailStaleReplicaTask failStaleReplicaTask;

@Inject
public SegmentReplicationPressureService(
Expand Down Expand Up @@ -204,6 +204,15 @@ public void setMaxAllowedStaleReplicas(double maxAllowedStaleReplicas) {

public void setReplicationTimeLimitFailReplica(TimeValue replicationTimeLimitFailReplica) {
this.replicationTimeLimitFailReplica = replicationTimeLimitFailReplica;
updateAsyncFailReplicaTask();
}

private synchronized void updateAsyncFailReplicaTask() {
try {
failStaleReplicaTask.close();
} finally {
failStaleReplicaTask = new AsyncFailStaleReplicaTask(this);
}
}

public void setReplicationTimeLimitBackpressure(TimeValue replicationTimeLimitBackpressure) {
Expand All @@ -230,13 +239,13 @@ final static class AsyncFailStaleReplicaTask extends AbstractAsyncTask {

@Override
protected boolean mustReschedule() {
return true;
return pressureService.shouldScheduleAsyncFailTask();
}

@Override
protected void runInternal() {
// Do not fail the replicas if time limit is set to 0 (i.e. disabled).
if (TimeValue.ZERO.equals(pressureService.replicationTimeLimitFailReplica) == false) {
if (pressureService.shouldScheduleAsyncFailTask()) {
final SegmentReplicationStats stats = pressureService.tracker.getStats();

// Find the shardId in node which is having stale replicas with highest current replication time.
Expand Down Expand Up @@ -303,4 +312,9 @@ public String toString() {
}

}

boolean shouldScheduleAsyncFailTask() {
return TimeValue.ZERO.equals(replicationTimeLimitFailReplica) == false;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,8 @@ public void testFailStaleReplicaTask() throws Exception {
assertEquals(5, shardStats.getCheckpointsBehindCount());

// call the background task
assertTrue(service.getFailStaleReplicaTask().mustReschedule());
assertTrue(service.getFailStaleReplicaTask().isScheduled());
service.getFailStaleReplicaTask().runInternal();

// verify that remote shard failed method is called which fails the replica shards falling behind.
Expand Down Expand Up @@ -257,6 +259,41 @@ public void testFailStaleReplicaTaskDisabled() throws Exception {
}
}

public void testFailStaleReplicaTaskToggleOnOff() throws Exception {
final Settings settings = Settings.builder()
.put(IndexMetadata.SETTING_REPLICATION_TYPE, ReplicationType.SEGMENT)
.put(SEGMENT_REPLICATION_INDEXING_PRESSURE_ENABLED.getKey(), true)
.put(MAX_REPLICATION_TIME_BACKPRESSURE_SETTING.getKey(), TimeValue.timeValueMillis(10))
.put(MAX_REPLICATION_LIMIT_STALE_REPLICA_SETTING.getKey(), TimeValue.timeValueMillis(1))
.build();

try (ReplicationGroup shards = createGroup(1, settings, new NRTReplicationEngineFactory())) {
shards.startAll();
final IndexShard primaryShard = shards.getPrimary();
SegmentReplicationPressureService service = buildPressureService(settings, primaryShard);

// index docs in batches without refreshing
indexInBatches(5, shards, primaryShard);

// assert that replica shard is few checkpoints behind primary
Set<SegmentReplicationShardStats> replicationStats = primaryShard.getReplicationStatsForTrackedReplicas();
assertEquals(1, replicationStats.size());
SegmentReplicationShardStats shardStats = replicationStats.stream().findFirst().get();
assertEquals(5, shardStats.getCheckpointsBehindCount());

assertTrue(service.getFailStaleReplicaTask().mustReschedule());
assertTrue(service.getFailStaleReplicaTask().isScheduled());
replicateSegments(primaryShard, shards.getReplicas());

service.setReplicationTimeLimitFailReplica(TimeValue.ZERO);
assertFalse(service.getFailStaleReplicaTask().mustReschedule());
assertFalse(service.getFailStaleReplicaTask().isScheduled());
service.setReplicationTimeLimitFailReplica(TimeValue.timeValueMillis(1));
assertTrue(service.getFailStaleReplicaTask().mustReschedule());
assertTrue(service.getFailStaleReplicaTask().isScheduled());
}
}

private int indexInBatches(int count, ReplicationGroup shards, IndexShard primaryShard) throws Exception {
int totalDocs = 0;
for (int i = 0; i < count; i++) {
Expand Down
Loading