Skip to content

Commit 190649d

Browse files
committed
Obtain retention lock earlier
1 parent ec7c423 commit 190649d

File tree

1 file changed

+8
-10
lines changed

1 file changed

+8
-10
lines changed

server/src/main/java/org/elasticsearch/indices/recovery/RecoverySourceHandler.java

+8-10
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,8 @@ public void recoverToTarget(ActionListener<RecoveryResponse> listener) {
168168
ReplicationTracker.getPeerRecoveryRetentionLeaseId(targetShardRouting)) : null);
169169
}, shardId + " validating recovery target ["+ request.targetAllocationId() + "] registered ",
170170
shard, cancellableThreads, logger);
171-
171+
final Closeable retentionLock = shard.acquireRetentionLock();
172+
resources.add(retentionLock);
172173
final long startingSeqNo;
173174
final boolean isSequenceNumberBasedRecovery
174175
= request.startingSeqNo() != SequenceNumbers.UNASSIGNED_SEQ_NO
@@ -177,17 +178,14 @@ && isTargetSameHistory()
177178
&& (useRetentionLeases == false
178179
|| (retentionLeaseRef.get() != null && retentionLeaseRef.get().retainingSequenceNumber() <= request.startingSeqNo()));
179180

180-
final Closeable retentionLock;
181181
if (isSequenceNumberBasedRecovery && useRetentionLeases) {
182182
// all the history we need is retained by an existing retention lease, so we do not need a separate retention lock
183-
retentionLock = () -> {};
183+
retentionLock.close();
184184
logger.trace("history is retained by {}", retentionLeaseRef.get());
185185
} else {
186-
// temporarily prevent any history from being discarded, and do this before acquiring the safe commit so that we can
187-
// be certain that all operations after the safe commit's local checkpoint will be retained for the duration of this
188-
// recovery.
189-
retentionLock = shard.acquireRetentionLock();
190-
resources.add(retentionLock);
186+
// all the history we need is retained by the retention lock, obtained before calling shard.hasCompleteHistoryOperations()
187+
// and before acquiring the safe commit we'll be using, so we can be certain that all operations after the safe commit's
188+
// local checkpoint will be retained for the duration of this recovery.
191189
logger.trace("history is retained by retention lock");
192190
}
193191

@@ -291,8 +289,8 @@ && isTargetSameHistory()
291289
}, onFailure);
292290

293291
establishRetentionLeaseStep.whenComplete(r -> {
294-
if (useRetentionLeases) {
295-
// all the history we need is now retained by a retention lease so we can discard the retention lock
292+
if (useRetentionLeases && isSequenceNumberBasedRecovery == false) {
293+
// all the history we need is now retained by a retention lease so we can close the retention lock
296294
retentionLock.close();
297295
}
298296

0 commit comments

Comments
 (0)