Skip to content

Commit 16af047

Browse files
authored
Separate translog from index deletion conditions (#52556)
Separates the translog from the index deletion conditions (allowing the translog to be cleaned up more eagerly), and avoids taking the write lock on the translog if no clean-up is actually necessary.
1 parent 3473987 commit 16af047

File tree

2 files changed

+28
-13
lines changed

2 files changed

+28
-13
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -539,8 +539,8 @@ public Translog.Location getTranslogLastWriteLocation() {
539539
private void revisitIndexDeletionPolicyOnTranslogSynced() throws IOException {
540540
if (combinedDeletionPolicy.hasUnreferencedCommits()) {
541541
indexWriter.deleteUnusedFiles();
542-
translog.trimUnreferencedReaders();
543542
}
543+
translog.trimUnreferencedReaders();
544544
}
545545

546546
@Override

server/src/main/java/org/elasticsearch/index/translog/Translog.java

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1646,24 +1646,25 @@ public void rollGeneration() throws IOException {
16461646
* required generation
16471647
*/
16481648
public void trimUnreferencedReaders() throws IOException {
1649-
// move most of the data to disk to reduce the time the lock is held
1649+
// first check under read lock if any readers can be trimmed
1650+
try (ReleasableLock ignored = readLock.acquire()) {
1651+
if (closed.get()) {
1652+
// we're shutdown potentially on some tragic event, don't delete anything
1653+
return;
1654+
}
1655+
if (getMinReferencedGen() == getMinFileGeneration()) {
1656+
return;
1657+
}
1658+
}
1659+
1660+
// move most of the data to disk to reduce the time the write lock is held
16501661
sync();
16511662
try (ReleasableLock ignored = writeLock.acquire()) {
16521663
if (closed.get()) {
16531664
// we're shutdown potentially on some tragic event, don't delete anything
16541665
return;
16551666
}
1656-
long minReferencedGen = Math.min(
1657-
deletionPolicy.getMinTranslogGenRequiredByLocks(),
1658-
minGenerationForSeqNo(deletionPolicy.getLocalCheckpointOfSafeCommit() + 1, current, readers));
1659-
assert minReferencedGen >= getMinFileGeneration() :
1660-
"deletion policy requires a minReferenceGen of [" + minReferencedGen + "] but the lowest gen available is ["
1661-
+ getMinFileGeneration() + "]";
1662-
assert minReferencedGen <= currentFileGeneration() :
1663-
"deletion policy requires a minReferenceGen of [" + minReferencedGen + "] which is higher than the current generation ["
1664-
+ currentFileGeneration() + "]";
1665-
1666-
1667+
final long minReferencedGen = getMinReferencedGen();
16671668
for (Iterator<TranslogReader> iterator = readers.iterator(); iterator.hasNext(); ) {
16681669
TranslogReader reader = iterator.next();
16691670
if (reader.getGeneration() >= minReferencedGen) {
@@ -1690,6 +1691,20 @@ public void trimUnreferencedReaders() throws IOException {
16901691
}
16911692
}
16921693

1694+
private long getMinReferencedGen() {
1695+
assert readLock.isHeldByCurrentThread() || writeLock.isHeldByCurrentThread();
1696+
long minReferencedGen = Math.min(
1697+
deletionPolicy.getMinTranslogGenRequiredByLocks(),
1698+
minGenerationForSeqNo(deletionPolicy.getLocalCheckpointOfSafeCommit() + 1, current, readers));
1699+
assert minReferencedGen >= getMinFileGeneration() :
1700+
"deletion policy requires a minReferenceGen of [" + minReferencedGen + "] but the lowest gen available is ["
1701+
+ getMinFileGeneration() + "]";
1702+
assert minReferencedGen <= currentFileGeneration() :
1703+
"deletion policy requires a minReferenceGen of [" + minReferencedGen + "] which is higher than the current generation ["
1704+
+ currentFileGeneration() + "]";
1705+
return minReferencedGen;
1706+
}
1707+
16931708
/**
16941709
* deletes all files associated with a reader. package-private to be able to simulate node failures at this point
16951710
*/

0 commit comments

Comments
 (0)