Skip to content

Commit 3bfcc60

Browse files
authored
Update translog policy before the next safe commit (#54839)
IndexShardIT#testMaybeFlush relies on the assumption that the safe commit and translog deletion policy have advanced after IndexShard#sync returns . This assumption does not hold if there's a race with the global checkpoint sync. Closes #52223
1 parent b093353 commit 3bfcc60

File tree

3 files changed

+44
-9
lines changed

3 files changed

+44
-9
lines changed

server/src/main/java/org/elasticsearch/index/engine/CombinedDeletionPolicy.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -84,17 +84,17 @@ public void onCommit(List<? extends IndexCommit> commits) throws IOException {
8484
this.safeCommitInfo = SafeCommitInfo.EMPTY;
8585
this.lastCommit = commits.get(commits.size() - 1);
8686
this.safeCommit = commits.get(keptPosition);
87-
if (keptPosition == commits.size() - 1) {
88-
this.maxSeqNoOfNextSafeCommit = Long.MAX_VALUE;
89-
} else {
90-
this.maxSeqNoOfNextSafeCommit = Long.parseLong(commits.get(keptPosition + 1).getUserData().get(SequenceNumbers.MAX_SEQ_NO));
91-
}
9287
for (int i = 0; i < keptPosition; i++) {
9388
if (snapshottedCommits.containsKey(commits.get(i)) == false) {
9489
deleteCommit(commits.get(i));
9590
}
9691
}
9792
updateRetentionPolicy();
93+
if (keptPosition == commits.size() - 1) {
94+
this.maxSeqNoOfNextSafeCommit = Long.MAX_VALUE;
95+
} else {
96+
this.maxSeqNoOfNextSafeCommit = Long.parseLong(commits.get(keptPosition + 1).getUserData().get(SequenceNumbers.MAX_SEQ_NO));
97+
}
9898
safeCommit = this.safeCommit;
9999
}
100100

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

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -977,6 +977,45 @@ public void testCommitAdvancesMinTranslogForRecovery() throws IOException {
977977
assertThat(engine.getTranslog().getMinFileGeneration(), equalTo(5L));
978978
}
979979

980+
public void testSyncTranslogConcurrently() throws Exception {
981+
IOUtils.close(engine, store);
982+
final Path translogPath = createTempDir();
983+
store = createStore();
984+
final AtomicLong globalCheckpoint = new AtomicLong(SequenceNumbers.NO_OPS_PERFORMED);
985+
engine = createEngine(config(defaultSettings, store, translogPath, newMergePolicy(), null, null, globalCheckpoint::get));
986+
List<Engine.Operation> ops = generateHistoryOnReplica(between(1, 50), false, randomBoolean(), randomBoolean());
987+
applyOperations(engine, ops);
988+
engine.flush(true, true);
989+
final CheckedRunnable<IOException> checker = () -> {
990+
assertThat(engine.getTranslogStats().getUncommittedOperations(), equalTo(0));
991+
assertThat(engine.getLastSyncedGlobalCheckpoint(), equalTo(globalCheckpoint.get()));
992+
try (Engine.IndexCommitRef safeCommit = engine.acquireSafeIndexCommit()) {
993+
SequenceNumbers.CommitInfo commitInfo =
994+
SequenceNumbers.loadSeqNoInfoFromLuceneCommit(safeCommit.getIndexCommit().getUserData().entrySet());
995+
assertThat(commitInfo.localCheckpoint, equalTo(engine.getProcessedLocalCheckpoint()));
996+
}
997+
};
998+
final Thread[] threads = new Thread[randomIntBetween(2, 4)];
999+
final Phaser phaser = new Phaser(threads.length);
1000+
globalCheckpoint.set(engine.getProcessedLocalCheckpoint());
1001+
for (int i = 0; i < threads.length; i++) {
1002+
threads[i] = new Thread(() -> {
1003+
phaser.arriveAndAwaitAdvance();
1004+
try {
1005+
engine.syncTranslog();
1006+
checker.run();
1007+
} catch (IOException e) {
1008+
throw new AssertionError(e);
1009+
}
1010+
});
1011+
threads[i].start();
1012+
}
1013+
for (Thread thread : threads) {
1014+
thread.join();
1015+
}
1016+
checker.run();
1017+
}
1018+
9801019
public void testSyncedFlushSurvivesEngineRestart() throws IOException {
9811020
final AtomicLong globalCheckpoint = new AtomicLong(SequenceNumbers.NO_OPS_PERFORMED);
9821021
IOUtils.close(store, engine);

server/src/test/java/org/elasticsearch/index/shard/IndexShardIT.java

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,6 @@
8181
import org.elasticsearch.test.ESSingleNodeTestCase;
8282
import org.elasticsearch.test.IndexSettingsModule;
8383
import org.elasticsearch.test.InternalSettingsPlugin;
84-
import org.elasticsearch.test.junit.annotations.TestIssueLogging;
8584
import org.junit.Assert;
8685

8786
import java.io.IOException;
@@ -319,9 +318,6 @@ public void testIndexCanChangeCustomDataPath() throws Exception {
319318
assertPathHasBeenCleared(newIndexDataPath.toAbsolutePath());
320319
}
321320

322-
@TestIssueLogging(
323-
value = "org.elasticsearch.index.engine:DEBUG",
324-
issueUrl = "https://github.com/elastic/elasticsearch/issues/52223")
325321
public void testMaybeFlush() throws Exception {
326322
createIndex("test", Settings.builder().put(IndexSettings.INDEX_TRANSLOG_DURABILITY_SETTING.getKey(), Translog.Durability.REQUEST)
327323
.build());

0 commit comments

Comments
 (0)