@@ -168,7 +168,8 @@ public void recoverToTarget(ActionListener<RecoveryResponse> listener) {
168
168
ReplicationTracker .getPeerRecoveryRetentionLeaseId (targetShardRouting )) : null );
169
169
}, shardId + " validating recovery target [" + request .targetAllocationId () + "] registered " ,
170
170
shard , cancellableThreads , logger );
171
-
171
+ final Closeable retentionLock = shard .acquireRetentionLock ();
172
+ resources .add (retentionLock );
172
173
final long startingSeqNo ;
173
174
final boolean isSequenceNumberBasedRecovery
174
175
= request .startingSeqNo () != SequenceNumbers .UNASSIGNED_SEQ_NO
@@ -177,17 +178,14 @@ && isTargetSameHistory()
177
178
&& (useRetentionLeases == false
178
179
|| (retentionLeaseRef .get () != null && retentionLeaseRef .get ().retainingSequenceNumber () <= request .startingSeqNo ()));
179
180
180
- final Closeable retentionLock ;
181
181
if (isSequenceNumberBasedRecovery && useRetentionLeases ) {
182
182
// 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 () ;
184
184
logger .trace ("history is retained by {}" , retentionLeaseRef .get ());
185
185
} 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.
191
189
logger .trace ("history is retained by retention lock" );
192
190
}
193
191
@@ -291,8 +289,8 @@ && isTargetSameHistory()
291
289
}, onFailure );
292
290
293
291
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
296
294
retentionLock .close ();
297
295
}
298
296
0 commit comments