79
79
import java .util .Objects ;
80
80
import java .util .Set ;
81
81
import java .util .concurrent .BlockingQueue ;
82
- import java .util .concurrent .CompletableFuture ;
83
82
import java .util .concurrent .ConcurrentLinkedDeque ;
84
83
import java .util .concurrent .CopyOnWriteArrayList ;
85
84
import java .util .concurrent .LinkedBlockingQueue ;
@@ -200,12 +199,7 @@ public void recoverToTarget(ActionListener<RecoveryResponse> listener) {
200
199
retentionLeaseRef .set (
201
200
shard .getRetentionLeases ().get (ReplicationTracker .getPeerRecoveryRetentionLeaseId (targetShardRouting ))
202
201
);
203
- },
204
- shardId + " validating recovery target [" + request .targetAllocationId () + "] registered " ,
205
- shard ,
206
- cancellableThreads ,
207
- logger
208
- );
202
+ }, shardId + " validating recovery target [" + request .targetAllocationId () + "] registered " , shard , cancellableThreads );
209
203
final Closeable retentionLock = shard .acquireHistoryRetentionLock ();
210
204
resources .add (retentionLock );
211
205
final long startingSeqNo ;
@@ -292,7 +286,7 @@ && isTargetSameHistory()
292
286
logger .debug ("no peer-recovery retention lease for " + request .targetAllocationId ());
293
287
deleteRetentionLeaseStep .onResponse (null );
294
288
}
295
- }, shardId + " removing retention lease for [" + request .targetAllocationId () + "]" , shard , cancellableThreads , logger );
289
+ }, shardId + " removing retention lease for [" + request .targetAllocationId () + "]" , shard , cancellableThreads );
296
290
297
291
deleteRetentionLeaseStep .whenComplete (ignored -> {
298
292
assert Transports .assertNotTransportThread (RecoverySourceHandler .this + "[phase1]" );
@@ -323,8 +317,7 @@ && isTargetSameHistory()
323
317
() -> shard .initiateTracking (request .targetAllocationId ()),
324
318
shardId + " initiating tracking of " + request .targetAllocationId (),
325
319
shard ,
326
- cancellableThreads ,
327
- logger
320
+ cancellableThreads
328
321
);
329
322
330
323
final long endingSeqNo = shard .seqNoStats ().getMaxSeqNo ();
@@ -405,44 +398,21 @@ static void runUnderPrimaryPermit(
405
398
CancellableThreads .Interruptible runnable ,
406
399
String reason ,
407
400
IndexShard primary ,
408
- CancellableThreads cancellableThreads ,
409
- Logger logger
401
+ CancellableThreads cancellableThreads
410
402
) {
411
403
cancellableThreads .execute (() -> {
412
- CompletableFuture <Releasable > permit = new CompletableFuture <>();
413
-
414
- // this wrapping looks unnecessary necessary, see #93290; TODO remove it
415
- final ActionListener <Releasable > onAcquired = new ActionListener <Releasable >() {
416
- @ Override
417
- public void onResponse (Releasable releasable ) {
418
- if (permit .complete (releasable ) == false ) {
419
- releasable .close ();
420
- }
421
- }
422
-
423
- @ Override
424
- public void onFailure (Exception e ) {
425
- permit .completeExceptionally (e );
426
- }
427
- };
428
- primary .acquirePrimaryOperationPermit (onAcquired , ThreadPool .Names .SAME , reason );
429
- try (Releasable ignored = FutureUtils .get (permit )) {
404
+ final var permit = new ListenableActionFuture <Releasable >();
405
+ primary .acquirePrimaryOperationPermit (permit , ThreadPool .Names .SAME , reason );
406
+ try (var ignored = FutureUtils .get (permit )) {
430
407
// check that the IndexShard still has the primary authority. This needs to be checked under operation permit to prevent
431
408
// races, as IndexShard will switch its authority only when it holds all operation permits, see IndexShard.relocated()
432
409
if (primary .isRelocatedPrimary ()) {
433
410
throw new IndexShardRelocatedException (primary .shardId ());
434
411
}
435
412
runnable .run ();
436
413
} finally {
437
- // just in case we got an exception (likely interrupted) while waiting for the get
438
- permit .whenComplete ((r , e ) -> {
439
- if (r != null ) {
440
- r .close ();
441
- }
442
- if (e != null ) {
443
- logger .trace ("suppressing exception on completion (it was already bubbled up or the operation was aborted)" , e );
444
- }
445
- });
414
+ // add a listener to release the permit because we might have been interrupted while waiting (double-releasing is ok)
415
+ permit .addListener (ActionListener .wrap (Releasable ::close , e -> {}));
446
416
}
447
417
});
448
418
}
@@ -1010,7 +980,7 @@ void createRetentionLease(final long startingSeqNo, ActionListener<RetentionLeas
1010
980
addRetentionLeaseStep .addListener (listener .map (rr -> newLease ));
1011
981
logger .trace ("created retention lease with estimated checkpoint of [{}]" , estimatedGlobalCheckpoint );
1012
982
}
1013
- }, shardId + " establishing retention lease for [" + request .targetAllocationId () + "]" , shard , cancellableThreads , logger );
983
+ }, shardId + " establishing retention lease for [" + request .targetAllocationId () + "]" , shard , cancellableThreads );
1014
984
}
1015
985
1016
986
boolean hasSameLegacySyncId (Store .MetadataSnapshot source , Store .MetadataSnapshot target ) {
@@ -1251,8 +1221,7 @@ void finalizeRecovery(long targetLocalCheckpoint, long trimAboveSeqNo, ActionLis
1251
1221
() -> shard .markAllocationIdAsInSync (request .targetAllocationId (), targetLocalCheckpoint ),
1252
1222
shardId + " marking " + request .targetAllocationId () + " as in sync" ,
1253
1223
shard ,
1254
- cancellableThreads ,
1255
- logger
1224
+ cancellableThreads
1256
1225
);
1257
1226
final long globalCheckpoint = shard .getLastKnownGlobalCheckpoint (); // this global checkpoint is persisted in finalizeRecovery
1258
1227
final StepListener <Void > finalizeListener = new StepListener <>();
@@ -1263,8 +1232,7 @@ void finalizeRecovery(long targetLocalCheckpoint, long trimAboveSeqNo, ActionLis
1263
1232
() -> shard .updateGlobalCheckpointForShard (request .targetAllocationId (), globalCheckpoint ),
1264
1233
shardId + " updating " + request .targetAllocationId () + "'s global checkpoint" ,
1265
1234
shard ,
1266
- cancellableThreads ,
1267
- logger
1235
+ cancellableThreads
1268
1236
);
1269
1237
1270
1238
if (request .isPrimaryRelocation ()) {
0 commit comments