Skip to content

Commit bbdcd5a

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 46a8405 commit bbdcd5a

File tree

3 files changed

+12
-5
lines changed

3 files changed

+12
-5
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
@@ -4365,13 +4365,18 @@ public void testAcquireIndexCommit() throws Exception {
43654365

43664366
public void testCleanUpCommitsWhenGlobalCheckpointAdvanced() throws Exception {
43674367
IOUtils.close(engine, store);
4368-
store = createStore();
4368+
final IndexSettings indexSettings = IndexSettingsModule.newIndexSettings("test",
4369+
Settings.builder().put(defaultSettings.getSettings())
4370+
.put(IndexSettings.INDEX_TRANSLOG_RETENTION_SIZE_SETTING.getKey(), -1)
4371+
.put(IndexSettings.INDEX_TRANSLOG_RETENTION_AGE_SETTING.getKey(), -1).build());
43694372
final AtomicLong globalCheckpoint = new AtomicLong(SequenceNumbers.NO_OPS_PERFORMED);
4370-
try (InternalEngine engine = createEngine(store, createTempDir(), globalCheckpoint::get)) {
4373+
try (Store store = createStore();
4374+
InternalEngine engine =
4375+
createEngine(config(indexSettings, store, createTempDir(), newMergePolicy(), null, null, globalCheckpoint::get))) {
43714376
final int numDocs = scaledRandomIntBetween(10, 100);
43724377
for (int docId = 0; docId < numDocs; docId++) {
43734378
index(engine, docId);
4374-
if (frequently()) {
4379+
if (rarely()) {
43754380
engine.flush(randomBoolean(), randomBoolean());
43764381
}
43774382
}
@@ -4385,6 +4390,7 @@ public void testCleanUpCommitsWhenGlobalCheckpointAdvanced() throws Exception {
43854390
globalCheckpoint.set(randomLongBetween(engine.getLocalCheckpoint(), Long.MAX_VALUE));
43864391
engine.syncTranslog();
43874392
assertThat(DirectoryReader.listCommits(store.directory()), contains(commits.get(commits.size() - 1)));
4393+
assertThat(engine.estimateTranslogOperationsFromMinSeq(0L), equalTo(0));
43884394
}
43894395
}
43904396

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@
4343
import org.elasticsearch.index.shard.IndexShard;
4444
import org.elasticsearch.index.translog.SnapshotMatchers;
4545
import org.elasticsearch.index.translog.Translog;
46-
import org.elasticsearch.test.junit.annotations.TestLogging;
4746

4847
import java.util.HashMap;
4948
import java.util.List;
@@ -74,7 +73,6 @@ public void testTranslogHistoryTransferred() throws Exception {
7473
}
7574
}
7675

77-
@TestLogging("_root:TRACE")
7876
public void testRetentionPolicyChangeDuringRecovery() throws Exception {
7977
try (ReplicationGroup shards = createGroup(0)) {
8078
shards.startPrimary();

0 commit comments

Comments
 (0)