@@ -108,11 +108,13 @@ public ReadOnlyEngine(EngineConfig config, SeqNoStats seqNoStats, TranslogStats
108108 searcherManager = new SearcherManager (reader , searcherFactory );
109109 if (seqNoStats == null ) {
110110 seqNoStats = buildSeqNoStats (config , lastCommittedSegmentInfos );
111- ensureNoUncommittedOperation (seqNoStats , lastCommittedSegmentInfos );
112111 }
113112 this .seqNoStats = seqNoStats ;
114113 this .docsStats = docsStats (lastCommittedSegmentInfos );
115114 this .indexWriterLock = indexWriterLock ;
115+ if (obtainLock ) {
116+ ensureNoUncommittedOperation (this .seqNoStats , lastCommittedSegmentInfos );
117+ }
116118 success = true ;
117119 } finally {
118120 if (success == false ) {
@@ -129,8 +131,13 @@ public ReadOnlyEngine(EngineConfig config, SeqNoStats seqNoStats, TranslogStats
129131 * so peer recovery of closed indices can skip phase 2 (i.e., not replaying translog operations) without losing data.
130132 */
131133 private void ensureNoUncommittedOperation (SeqNoStats seqNoStats , SegmentInfos segmentInfos ) throws IOException {
132- // we can't enforce this check on an old index - should we prevent this engine as a recovery source?
134+ assert indexWriterLock != null ;
133135 if (config ().getIndexSettings ().getIndexVersionCreated ().before (Version .V_6_7_0 )) {
136+ // we can't enforce this check on an old index
137+ return ;
138+ }
139+ if (seqNoStats .getGlobalCheckpoint () == seqNoStats .getMaxSeqNo ()) {
140+ // we are good - no need to open translog
134141 return ;
135142 }
136143 final String translogUUID = segmentInfos .userData .get (Translog .TRANSLOG_UUID_KEY );
@@ -139,13 +146,13 @@ private void ensureNoUncommittedOperation(SeqNoStats seqNoStats, SegmentInfos se
139146 final TranslogDeletionPolicy translogDeletionPolicy = new TranslogDeletionPolicy (Long .MAX_VALUE , Long .MAX_VALUE );
140147 translogDeletionPolicy .setTranslogGenerationOfLastCommit (recoverTranslogGeneration .translogFileGeneration );
141148 translogDeletionPolicy .setMinTranslogGenerationForRecovery (recoverTranslogGeneration .translogFileGeneration );
142- final LocalCheckpointTracker localCheckpointTracker ;
143- try (DirectoryReader reader = DirectoryReader .open (indexCommit )) {
144- localCheckpointTracker = createLocalCheckpointTracker (engineConfig , segmentInfos , logger ,
145- () -> new Searcher ("build_checkpoint_tracker" , new IndexSearcher (reader ), () -> {}), LocalCheckpointTracker ::new );
146- }
147149 try (Translog translog = new Translog (engineConfig .getTranslogConfig (), translogUUID , translogDeletionPolicy ,
148150 engineConfig .getGlobalCheckpointSupplier (), engineConfig .getPrimaryTermSupplier ())) {
151+ final LocalCheckpointTracker localCheckpointTracker ;
152+ try (DirectoryReader reader = DirectoryReader .open (indexCommit )) {
153+ localCheckpointTracker = createLocalCheckpointTracker (engineConfig , segmentInfos , logger ,
154+ () -> new Searcher ("build_checkpoint_tracker" , new IndexSearcher (reader ), () -> {}), LocalCheckpointTracker ::new );
155+ }
149156 try (Translog .Snapshot snapshot = translog .newSnapshotFromGen (recoverTranslogGeneration , Long .MAX_VALUE )) {
150157 Translog .Operation op ;
151158 while ((op = snapshot .next ()) != null ) {
0 commit comments