Skip to content

Commit 712862c

Browse files
committed
Trim translog when safe commit advanced (#32967)
Since #28140 when the global checkpoint is advanced, we try to move the safe commit forward, and clean up old index commits if possible. However, we forget to trim unreferenced translog. This change makes sure that we prune both old translog and index commits when the safe commit advanced. Relates #28140 Closes #32089
1 parent 630a1b8 commit 712862c

File tree

3 files changed

+12
-4
lines changed

3 files changed

+12
-4
lines changed

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -491,6 +491,7 @@ public Translog.Location getTranslogLastWriteLocation() {
491491
private void revisitIndexDeletionPolicyOnTranslogSynced() throws IOException {
492492
if (combinedDeletionPolicy.hasUnreferencedCommits()) {
493493
indexWriter.deleteUnusedFiles();
494+
translog.trimUnreferencedReaders();
494495
}
495496
}
496497

@@ -1808,6 +1809,8 @@ private void releaseIndexCommit(IndexCommit snapshot) throws IOException {
18081809
// Revisit the deletion policy if we can clean up the snapshotting commit.
18091810
if (combinedDeletionPolicy.releaseCommit(snapshot)) {
18101811
ensureOpen();
1812+
// Here we don't have to trim translog because snapshotting an index commit
1813+
// does not lock translog or prevents unreferenced files from trimming.
18111814
indexWriter.deleteUnusedFiles();
18121815
}
18131816
}

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

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4367,13 +4367,18 @@ public void testAcquireIndexCommit() throws Exception {
43674367

43684368
public void testCleanUpCommitsWhenGlobalCheckpointAdvanced() throws Exception {
43694369
IOUtils.close(engine, store);
4370-
store = createStore();
4370+
final IndexSettings indexSettings = IndexSettingsModule.newIndexSettings("test",
4371+
Settings.builder().put(defaultSettings.getSettings())
4372+
.put(IndexSettings.INDEX_TRANSLOG_RETENTION_SIZE_SETTING.getKey(), -1)
4373+
.put(IndexSettings.INDEX_TRANSLOG_RETENTION_AGE_SETTING.getKey(), -1).build());
43714374
final AtomicLong globalCheckpoint = new AtomicLong(SequenceNumbers.NO_OPS_PERFORMED);
4372-
try (InternalEngine engine = createEngine(store, createTempDir(), globalCheckpoint::get)) {
4375+
try (Store store = createStore();
4376+
InternalEngine engine =
4377+
createEngine(config(indexSettings, store, createTempDir(), newMergePolicy(), null, null, globalCheckpoint::get))) {
43734378
final int numDocs = scaledRandomIntBetween(10, 100);
43744379
for (int docId = 0; docId < numDocs; docId++) {
43754380
index(engine, docId);
4376-
if (frequently()) {
4381+
if (rarely()) {
43774382
engine.flush(randomBoolean(), randomBoolean());
43784383
}
43794384
}
@@ -4387,6 +4392,7 @@ public void testCleanUpCommitsWhenGlobalCheckpointAdvanced() throws Exception {
43874392
globalCheckpoint.set(randomLongBetween(engine.getLocalCheckpoint(), Long.MAX_VALUE));
43884393
engine.syncTranslog();
43894394
assertThat(DirectoryReader.listCommits(store.directory()), contains(commits.get(commits.size() - 1)));
4395+
assertThat(engine.estimateTranslogOperationsFromMinSeq(0L), equalTo(0));
43904396
}
43914397
}
43924398

server/src/test/java/org/elasticsearch/indices/recovery/RecoveryTests.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,6 @@ public void testTranslogHistoryTransferred() throws Exception {
7373
}
7474
}
7575

76-
@AwaitsFix(bugUrl = "https://github.com/elastic/elasticsearch/issues/32089")
7776
public void testRetentionPolicyChangeDuringRecovery() throws Exception {
7877
try (ReplicationGroup shards = createGroup(0)) {
7978
shards.startPrimary();

0 commit comments

Comments
 (0)