Skip to content

Commit e95d18e

Browse files
authored
Adjust CombinedDeletionPolicy for multiple commits (#27456)
Today, we keep only the last index commit and use only it to calculate the minimum required translog generation. This may no longer be correct as we introduced a new deletion policy which keeps multiple index commits. This change adjusts the CombinedDeletionPolicy so that it can work correctly with a new index deletion policy. Relates to #10708, #27367
1 parent d1b1d71 commit e95d18e

2 files changed

Lines changed: 20 additions & 11 deletions

File tree

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

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -71,12 +71,18 @@ public void onCommit(List<? extends IndexCommit> commits) throws IOException {
7171
}
7272

7373
private void setLastCommittedTranslogGeneration(List<? extends IndexCommit> commits) throws IOException {
74-
// when opening an existing lucene index, we currently always open the last commit.
75-
// we therefore use the translog gen as the one that will be required for recovery
76-
final IndexCommit indexCommit = commits.get(commits.size() - 1);
77-
assert indexCommit.isDeleted() == false : "last commit is deleted";
78-
long minGen = Long.parseLong(indexCommit.getUserData().get(Translog.TRANSLOG_GENERATION_KEY));
79-
translogDeletionPolicy.setMinTranslogGenerationForRecovery(minGen);
74+
// We need to keep translog since the smallest translog generation of un-deleted commits.
75+
// However, there are commits that are not deleted just because they are being snapshotted (rather than being kept by the policy).
76+
// TODO: We need to distinguish those commits and skip them in calculating the minimum required translog generation.
77+
long minRequiredGen = Long.MAX_VALUE;
78+
for (IndexCommit indexCommit : commits) {
79+
if (indexCommit.isDeleted() == false) {
80+
long translogGen = Long.parseLong(indexCommit.getUserData().get(Translog.TRANSLOG_GENERATION_KEY));
81+
minRequiredGen = Math.min(translogGen, minRequiredGen);
82+
}
83+
}
84+
assert minRequiredGen != Long.MAX_VALUE : "All commits are deleted";
85+
translogDeletionPolicy.setMinTranslogGenerationForRecovery(minRequiredGen);
8086
}
8187

8288
public SnapshotDeletionPolicy getIndexDeletionPolicy() {

core/src/test/java/org/elasticsearch/index/engine/CombinedDeletionPolicyTests.java

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -60,20 +60,23 @@ public void testSettingMinTranslogGen() throws IOException {
6060
EngineConfig.OpenMode.OPEN_INDEX_AND_TRANSLOG);
6161
List<IndexCommit> commitList = new ArrayList<>();
6262
long count = randomIntBetween(10, 20);
63-
long lastGen = 0;
63+
long minGen = Long.MAX_VALUE;
6464
for (int i = 0; i < count; i++) {
65-
lastGen += randomIntBetween(10, 20000);
65+
long lastGen = randomIntBetween(10, 20000);
66+
minGen = Math.min(minGen, lastGen);
6667
commitList.add(mockIndexCommitWithTranslogGen(lastGen));
6768
}
6869
combinedDeletionPolicy.onInit(commitList);
69-
verify(translogDeletionPolicy, times(1)).setMinTranslogGenerationForRecovery(lastGen);
70+
verify(translogDeletionPolicy, times(1)).setMinTranslogGenerationForRecovery(minGen);
7071
commitList.clear();
72+
minGen = Long.MAX_VALUE;
7173
for (int i = 0; i < count; i++) {
72-
lastGen += randomIntBetween(10, 20000);
74+
long lastGen = randomIntBetween(10, 20000);
75+
minGen = Math.min(minGen, lastGen);
7376
commitList.add(mockIndexCommitWithTranslogGen(lastGen));
7477
}
7578
combinedDeletionPolicy.onCommit(commitList);
76-
verify(translogDeletionPolicy, times(1)).setMinTranslogGenerationForRecovery(lastGen);
79+
verify(translogDeletionPolicy, times(1)).setMinTranslogGenerationForRecovery(minGen);
7780
}
7881

7982
IndexCommit mockIndexCommitWithTranslogGen(long gen) throws IOException {

0 commit comments

Comments
 (0)