@@ -385,7 +385,7 @@ public int restoreLocalHistoryFromTranslog(TranslogRecoveryRunner translogRecove
385
385
try (ReleasableLock ignored = readLock .acquire ()) {
386
386
ensureOpen ();
387
387
final long localCheckpoint = localCheckpointTracker .getProcessedCheckpoint ();
388
- try (Translog .Snapshot snapshot = getTranslog ().newSnapshotFromMinSeqNo (localCheckpoint + 1 )) {
388
+ try (Translog .Snapshot snapshot = getTranslog ().newSnapshot (localCheckpoint + 1 , Long . MAX_VALUE )) {
389
389
return translogRecoveryRunner .run (this , snapshot );
390
390
}
391
391
}
@@ -458,23 +458,24 @@ public void skipTranslogRecovery() {
458
458
}
459
459
460
460
private void recoverFromTranslogInternal (TranslogRecoveryRunner translogRecoveryRunner , long recoverUpToSeqNo ) throws IOException {
461
- Translog .TranslogGeneration translogGeneration = translog .getGeneration ();
462
461
final int opsRecovered ;
463
- final long translogFileGen = Long .parseLong (lastCommittedSegmentInfos .getUserData ().get (Translog .TRANSLOG_GENERATION_KEY ));
464
- try (Translog .Snapshot snapshot = translog .newSnapshotFromGen (
465
- new Translog .TranslogGeneration (translog .getTranslogUUID (), translogFileGen ), recoverUpToSeqNo )) {
466
- opsRecovered = translogRecoveryRunner .run (this , snapshot );
467
- } catch (Exception e ) {
468
- throw new EngineException (shardId , "failed to recover from translog" , e );
462
+ final long localCheckpoint = getProcessedLocalCheckpoint ();
463
+ if (localCheckpoint < recoverUpToSeqNo ) {
464
+ try (Translog .Snapshot snapshot = translog .newSnapshot (localCheckpoint + 1 , recoverUpToSeqNo )) {
465
+ opsRecovered = translogRecoveryRunner .run (this , snapshot );
466
+ } catch (Exception e ) {
467
+ throw new EngineException (shardId , "failed to recover from translog" , e );
468
+ }
469
+ } else {
470
+ opsRecovered = 0 ;
469
471
}
470
472
// flush if we recovered something or if we have references to older translogs
471
473
// note: if opsRecovered == 0 and we have older translogs it means they are corrupted or 0 length.
472
474
assert pendingTranslogRecovery .get () : "translogRecovery is not pending but should be" ;
473
475
pendingTranslogRecovery .set (false ); // we are good - now we can commit
474
476
if (opsRecovered > 0 ) {
475
- logger .trace ("flushing post recovery from translog. ops recovered [{}]. committed translog id [{}]. current id [{}]" ,
476
- opsRecovered , translogGeneration == null ? null :
477
- translogGeneration .translogFileGeneration , translog .currentFileGeneration ());
477
+ logger .trace ("flushing post recovery from translog: ops recovered [{}], current translog generation [{}]" ,
478
+ opsRecovered , translog .currentFileGeneration ());
478
479
commitIndexWriter (indexWriter , translog );
479
480
refreshLastCommittedSegmentInfos ();
480
481
refresh ("translog_recovery" );
@@ -486,7 +487,8 @@ private Translog openTranslog(EngineConfig engineConfig, TranslogDeletionPolicy
486
487
LongSupplier globalCheckpointSupplier , LongConsumer persistedSequenceNumberConsumer ) throws IOException {
487
488
488
489
final TranslogConfig translogConfig = engineConfig .getTranslogConfig ();
489
- final String translogUUID = loadTranslogUUIDFromLastCommit ();
490
+ final Map <String , String > userData = store .readLastCommittedSegmentsInfo ().getUserData ();
491
+ final String translogUUID = Objects .requireNonNull (userData .get (Translog .TRANSLOG_UUID_KEY ));
490
492
// We expect that this shard already exists, so it must already have an existing translog else something is badly wrong!
491
493
return new Translog (translogConfig , translogUUID , translogDeletionPolicy , globalCheckpointSupplier ,
492
494
engineConfig .getPrimaryTermSupplier (), persistedSequenceNumberConsumer );
@@ -551,18 +553,6 @@ public long getWritingBytes() {
551
553
return indexWriter .getFlushingBytes () + versionMap .getRefreshingBytes ();
552
554
}
553
555
554
- /**
555
- * Reads the current stored translog ID from the last commit data.
556
- */
557
- @ Nullable
558
- private String loadTranslogUUIDFromLastCommit () throws IOException {
559
- final Map <String , String > commitUserData = store .readLastCommittedSegmentsInfo ().getUserData ();
560
- if (commitUserData .containsKey (Translog .TRANSLOG_GENERATION_KEY ) == false ) {
561
- throw new IllegalStateException ("commit doesn't contain translog generation id" );
562
- }
563
- return commitUserData .get (Translog .TRANSLOG_UUID_KEY );
564
- }
565
-
566
556
/**
567
557
* Reads the current stored history ID from the IW commit data.
568
558
*/
@@ -1588,8 +1578,10 @@ public boolean shouldPeriodicallyFlush() {
1588
1578
if (shouldPeriodicallyFlushAfterBigMerge .get ()) {
1589
1579
return true ;
1590
1580
}
1581
+ final long localCheckpointOfLastCommit =
1582
+ Long .parseLong (lastCommittedSegmentInfos .userData .get (SequenceNumbers .LOCAL_CHECKPOINT_KEY ));
1591
1583
final long translogGenerationOfLastCommit =
1592
- Long . parseLong ( lastCommittedSegmentInfos . userData . get ( Translog . TRANSLOG_GENERATION_KEY )) ;
1584
+ translog . getMinGenerationForSeqNo ( localCheckpointOfLastCommit + 1 ). translogFileGeneration ;
1593
1585
final long flushThreshold = config ().getIndexSettings ().getFlushThresholdSize ().getBytes ();
1594
1586
if (translog .sizeInBytesByMinGen (translogGenerationOfLastCommit ) < flushThreshold ) {
1595
1587
return false ;
@@ -2281,11 +2273,6 @@ protected void commitIndexWriter(final IndexWriter writer, final Translog transl
2281
2273
ensureCanFlush ();
2282
2274
try {
2283
2275
final long localCheckpoint = localCheckpointTracker .getProcessedCheckpoint ();
2284
- final Translog .TranslogGeneration translogGeneration = translog .getMinGenerationForSeqNo (localCheckpoint + 1 );
2285
- final String translogFileGeneration = Long .toString (translogGeneration .translogFileGeneration );
2286
- final String translogUUID = translogGeneration .translogUUID ;
2287
- final String localCheckpointValue = Long .toString (localCheckpoint );
2288
-
2289
2276
writer .setLiveCommitData (() -> {
2290
2277
/*
2291
2278
* The user data captured above (e.g. local checkpoint) contains data that must be evaluated *before* Lucene flushes
@@ -2296,10 +2283,9 @@ protected void commitIndexWriter(final IndexWriter writer, final Translog transl
2296
2283
* {@link IndexWriter#commit()} call flushes all documents, we defer computation of the maximum sequence number to the time
2297
2284
* of invocation of the commit data iterator (which occurs after all documents have been flushed to Lucene).
2298
2285
*/
2299
- final Map <String , String > commitData = new HashMap <>(7 );
2300
- commitData .put (Translog .TRANSLOG_GENERATION_KEY , translogFileGeneration );
2301
- commitData .put (Translog .TRANSLOG_UUID_KEY , translogUUID );
2302
- commitData .put (SequenceNumbers .LOCAL_CHECKPOINT_KEY , localCheckpointValue );
2286
+ final Map <String , String > commitData = new HashMap <>(6 );
2287
+ commitData .put (Translog .TRANSLOG_UUID_KEY , translog .getTranslogUUID ());
2288
+ commitData .put (SequenceNumbers .LOCAL_CHECKPOINT_KEY , Long .toString (localCheckpoint ));
2303
2289
commitData .put (SequenceNumbers .MAX_SEQ_NO , Long .toString (localCheckpointTracker .getMaxSeqNo ()));
2304
2290
commitData .put (MAX_UNSAFE_AUTO_ID_TIMESTAMP_COMMIT_ID , Long .toString (maxUnsafeAutoIdTimestamp .get ()));
2305
2291
commitData .put (HISTORY_UUID_KEY , historyUUID );
0 commit comments