Skip to content

Commit fc5bdbc

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 9c4bb79 commit fc5bdbc

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
@@ -458,6 +458,7 @@ public void syncTranslog() throws IOException {
458458
private void revisitIndexDeletionPolicyOnTranslogSynced() throws IOException {
459459
if (combinedDeletionPolicy.hasUnreferencedCommits()) {
460460
indexWriter.deleteUnusedFiles();
461+
translog.trimUnreferencedReaders();
461462
}
462463
}
463464

@@ -1746,6 +1747,8 @@ private void releaseIndexCommit(IndexCommit snapshot) throws IOException {
17461747
// Revisit the deletion policy if we can clean up the snapshotting commit.
17471748
if (combinedDeletionPolicy.releaseCommit(snapshot)) {
17481749
ensureOpen();
1750+
// Here we don't have to trim translog because snapshotting an index commit
1751+
// does not lock translog or prevents unreferenced files from trimming.
17491752
indexWriter.deleteUnusedFiles();
17501753
}
17511754
}

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

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

43114311
public void testCleanUpCommitsWhenGlobalCheckpointAdvanced() throws Exception {
43124312
IOUtils.close(engine, store);
4313-
store = createStore();
4313+
final IndexSettings indexSettings = IndexSettingsModule.newIndexSettings("test",
4314+
Settings.builder().put(defaultSettings.getSettings())
4315+
.put(IndexSettings.INDEX_TRANSLOG_RETENTION_SIZE_SETTING.getKey(), -1)
4316+
.put(IndexSettings.INDEX_TRANSLOG_RETENTION_AGE_SETTING.getKey(), -1).build());
43144317
final AtomicLong globalCheckpoint = new AtomicLong(SequenceNumbers.NO_OPS_PERFORMED);
4315-
try (InternalEngine engine = createEngine(store, createTempDir(), globalCheckpoint::get)) {
4318+
try (Store store = createStore();
4319+
InternalEngine engine =
4320+
createEngine(config(indexSettings, store, createTempDir(), newMergePolicy(), null, null, globalCheckpoint::get))) {
43164321
final int numDocs = scaledRandomIntBetween(10, 100);
43174322
for (int docId = 0; docId < numDocs; docId++) {
43184323
index(engine, docId);
4319-
if (frequently()) {
4324+
if (rarely()) {
43204325
engine.flush(randomBoolean(), randomBoolean());
43214326
}
43224327
}
@@ -4330,6 +4335,7 @@ public void testCleanUpCommitsWhenGlobalCheckpointAdvanced() throws Exception {
43304335
globalCheckpoint.set(randomLongBetween(engine.getLocalCheckpointTracker().getCheckpoint(), Long.MAX_VALUE));
43314336
engine.syncTranslog();
43324337
assertThat(DirectoryReader.listCommits(store.directory()), contains(commits.get(commits.size() - 1)));
4338+
assertThat(engine.estimateTranslogOperationsFromMinSeq(0L), equalTo(0));
43334339
}
43344340
}
43354341

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)