Skip to content

Commit 04dd738

Browse files
committed
TEST: trim unsafe commits before opening engine
Since #29260, unsafe commits must be trimmed before opening an engine. This makes the engine constructor follow Lucene standard semantics and use the last commit. However, we haven't fully applied this change in some tests. Relates #29260
1 parent eb8b317 commit 04dd738

File tree

1 file changed

+17
-11
lines changed

1 file changed

+17
-11
lines changed

server/src/test/java/org/elasticsearch/index/engine/InternalEngineTests.java

+17-11
Original file line numberDiff line numberDiff line change
@@ -644,6 +644,7 @@ public IndexSearcher wrap(IndexSearcher searcher) throws EngineException {
644644
InternalEngine engine = createEngine(store, translog);
645645
engine.close();
646646

647+
trimUnsafeCommits(engine.config());
647648
engine = new InternalEngine(engine.config());
648649
assertTrue(engine.isRecovering());
649650
engine.recoverFromTranslog();
@@ -659,6 +660,7 @@ public void testFlushIsDisabledDuringTranslogRecovery() throws IOException {
659660
engine.index(indexForDoc(doc));
660661
engine.close();
661662

663+
trimUnsafeCommits(engine.config());
662664
engine = new InternalEngine(engine.config());
663665
expectThrows(IllegalStateException.class, () -> engine.flush(true, true));
664666
assertTrue(engine.isRecovering());
@@ -690,18 +692,14 @@ public void testTranslogMultipleOperationsSameDocument() throws IOException {
690692
} finally {
691693
IOUtils.close(engine);
692694
}
693-
694-
Engine recoveringEngine = null;
695-
try {
696-
recoveringEngine = new InternalEngine(engine.config());
695+
trimUnsafeCommits(engine.config());
696+
try (Engine recoveringEngine = new InternalEngine(engine.config())){
697697
recoveringEngine.recoverFromTranslog();
698698
try (Engine.Searcher searcher = recoveringEngine.acquireSearcher("test")) {
699699
final TotalHitCountCollector collector = new TotalHitCountCollector();
700700
searcher.searcher().search(new MatchAllDocsQuery(), collector);
701701
assertThat(collector.getTotalHits(), equalTo(operations.get(operations.size() - 1) instanceof Engine.Delete ? 0 : 1));
702702
}
703-
} finally {
704-
IOUtils.close(recoveringEngine);
705703
}
706704
}
707705

@@ -722,6 +720,7 @@ public void testTranslogRecoveryDoesNotReplayIntoTranslog() throws IOException {
722720
Engine recoveringEngine = null;
723721
try {
724722
final AtomicBoolean committed = new AtomicBoolean();
723+
trimUnsafeCommits(initialEngine.config());
725724
recoveringEngine = new InternalEngine(initialEngine.config()) {
726725

727726
@Override
@@ -1151,6 +1150,7 @@ public void testSyncedFlushSurvivesEngineRestart() throws IOException {
11511150
SequenceNumbers.UNASSIGNED_SEQ_NO, shardId);
11521151
store.associateIndexWithNewTranslog(translogUUID);
11531152
}
1153+
trimUnsafeCommits(config);
11541154
engine = new InternalEngine(config);
11551155
engine.recoverFromTranslog();
11561156
assertEquals(engine.getLastCommittedSegmentInfos().getUserData().get(Engine.SYNC_COMMIT_ID), syncId);
@@ -2054,9 +2054,8 @@ public void testSeqNoAndCheckpoints() throws IOException {
20542054
IOUtils.close(initialEngine);
20552055
}
20562056

2057-
InternalEngine recoveringEngine = null;
2058-
try {
2059-
recoveringEngine = new InternalEngine(initialEngine.config());
2057+
trimUnsafeCommits(initialEngine.engineConfig);
2058+
try (InternalEngine recoveringEngine = new InternalEngine(initialEngine.config())){
20602059
recoveringEngine.recoverFromTranslog();
20612060

20622061
assertEquals(primarySeqNo, recoveringEngine.getLocalCheckpointTracker().getMaxSeqNo());
@@ -2075,8 +2074,6 @@ public void testSeqNoAndCheckpoints() throws IOException {
20752074
assertThat(recoveringEngine.getLocalCheckpointTracker().getCheckpoint(), equalTo(primarySeqNo));
20762075
assertThat(recoveringEngine.getLocalCheckpointTracker().getMaxSeqNo(), equalTo(primarySeqNo));
20772076
assertThat(recoveringEngine.getLocalCheckpointTracker().generateSeqNo(), equalTo(primarySeqNo + 1));
2078-
} finally {
2079-
IOUtils.close(recoveringEngine);
20802077
}
20812078
}
20822079

@@ -2389,6 +2386,7 @@ public void testCurrentTranslogIDisCommitted() throws IOException {
23892386
// open and recover tlog
23902387
{
23912388
for (int i = 0; i < 2; i++) {
2389+
trimUnsafeCommits(config);
23922390
try (InternalEngine engine = new InternalEngine(config)) {
23932391
assertTrue(engine.isRecovering());
23942392
Map<String, String> userData = engine.getLastCommittedSegmentInfos().getUserData();
@@ -2413,6 +2411,7 @@ public void testCurrentTranslogIDisCommitted() throws IOException {
24132411
final String translogUUID =
24142412
Translog.createEmptyTranslog(config.getTranslogConfig().getTranslogPath(), SequenceNumbers.NO_OPS_PERFORMED, shardId);
24152413
store.associateIndexWithNewTranslog(translogUUID);
2414+
trimUnsafeCommits(config);
24162415
try (InternalEngine engine = new InternalEngine(config)) {
24172416
Map<String, String> userData = engine.getLastCommittedSegmentInfos().getUserData();
24182417
assertEquals("1", userData.get(Translog.TRANSLOG_GENERATION_KEY));
@@ -2426,6 +2425,7 @@ public void testCurrentTranslogIDisCommitted() throws IOException {
24262425
// open and recover tlog with empty tlog
24272426
{
24282427
for (int i = 0; i < 2; i++) {
2428+
trimUnsafeCommits(config);
24292429
try (InternalEngine engine = new InternalEngine(config)) {
24302430
Map<String, String> userData = engine.getLastCommittedSegmentInfos().getUserData();
24312431
assertEquals("1", userData.get(Translog.TRANSLOG_GENERATION_KEY));
@@ -2487,6 +2487,7 @@ public void testTranslogReplayWithFailure() throws IOException {
24872487
boolean started = false;
24882488
InternalEngine engine = null;
24892489
try {
2490+
trimUnsafeCommits(config(defaultSettings, store, translogPath, NoMergePolicy.INSTANCE, null));
24902491
engine = createEngine(store, translogPath);
24912492
started = true;
24922493
} catch (EngineException | IOException e) {
@@ -2567,6 +2568,7 @@ public void testSkipTranslogReplay() throws IOException {
25672568
}
25682569
assertVisibleCount(engine, numDocs);
25692570
engine.close();
2571+
trimUnsafeCommits(engine.config());
25702572
engine = new InternalEngine(engine.config());
25712573
engine.skipTranslogRecovery();
25722574
try (Engine.Searcher searcher = engine.acquireSearcher("test")) {
@@ -2608,6 +2610,7 @@ public void testTranslogReplay() throws IOException {
26082610
parser.mappingUpdate = dynamicUpdate();
26092611

26102612
engine.close();
2613+
trimUnsafeCommits(copy(engine.config(), inSyncGlobalCheckpointSupplier));
26112614
engine = new InternalEngine(copy(engine.config(), inSyncGlobalCheckpointSupplier)); // we need to reuse the engine config unless the parser.mappingModified won't work
26122615
engine.recoverFromTranslog();
26132616

@@ -3685,6 +3688,7 @@ public void testNoOps() throws IOException {
36853688
final BiFunction<Long, Long, LocalCheckpointTracker> supplier = (ms, lcp) -> new LocalCheckpointTracker(
36863689
maxSeqNo,
36873690
localCheckpoint);
3691+
trimUnsafeCommits(engine.config());
36883692
noOpEngine = new InternalEngine(engine.config(), supplier) {
36893693
@Override
36903694
protected long doGenerateSeqNoForOperation(Operation operation) {
@@ -3832,6 +3836,7 @@ public void markSeqNoAsCompleted(long seqNo) {
38323836
completedSeqNos.add(seqNo);
38333837
}
38343838
};
3839+
trimUnsafeCommits(engine.config());
38353840
actualEngine = new InternalEngine(engine.config(), supplier);
38363841
final int operations = randomIntBetween(0, 1024);
38373842
final Set<Long> expectedCompletedSeqNos = new HashSet<>();
@@ -3902,6 +3907,7 @@ public void testFillUpSequenceIdGapsOnRecovery() throws IOException {
39023907
assertEquals(docs - 1, engine.getLocalCheckpointTracker().getCheckpoint());
39033908
assertEquals(maxSeqIDOnReplica, replicaEngine.getLocalCheckpointTracker().getMaxSeqNo());
39043909
assertEquals(checkpointOnReplica, replicaEngine.getLocalCheckpointTracker().getCheckpoint());
3910+
trimUnsafeCommits(copy(replicaEngine.config(), globalCheckpoint::get));
39053911
recoveringEngine = new InternalEngine(copy(replicaEngine.config(), globalCheckpoint::get));
39063912
assertEquals(numDocsOnReplica, recoveringEngine.getTranslog().stats().getUncommittedOperations());
39073913
recoveringEngine.recoverFromTranslog();

0 commit comments

Comments
 (0)