Skip to content

Commit 73e9d30

Browse files
ywelschdnhatn
authored andcommitted
Separate translog from index deletion conditions (elastic#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 a14d4bc commit 73e9d30

File tree

2 files changed

+28
-12
lines changed

2 files changed

+28
-12
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
@@ -585,8 +585,8 @@ public Translog.Location getTranslogLastWriteLocation() {
585585
private void revisitIndexDeletionPolicyOnTranslogSynced() throws IOException {
586586
if (combinedDeletionPolicy.hasUnreferencedCommits()) {
587587
indexWriter.deleteUnusedFiles();
588-
translog.trimUnreferencedReaders();
589588
}
589+
translog.trimUnreferencedReaders();
590590
}
591591

592592
@Override

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

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1646,23 +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(deletionPolicy.minTranslogGenRequired(readers, current),
1657-
minGenerationForSeqNo(deletionPolicy.getLocalCheckpointOfSafeCommit() + 1, current, readers));
1658-
assert minReferencedGen >= getMinFileGeneration() :
1659-
"deletion policy requires a minReferenceGen of [" + minReferencedGen + "] but the lowest gen available is ["
1660-
+ getMinFileGeneration() + "]";
1661-
assert minReferencedGen <= currentFileGeneration() :
1662-
"deletion policy requires a minReferenceGen of [" + minReferencedGen + "] which is higher than the current generation ["
1663-
+ currentFileGeneration() + "]";
1664-
1665-
1667+
final long minReferencedGen = getMinReferencedGen();
16661668
for (Iterator<TranslogReader> iterator = readers.iterator(); iterator.hasNext(); ) {
16671669
TranslogReader reader = iterator.next();
16681670
if (reader.getGeneration() >= minReferencedGen) {
@@ -1689,6 +1691,20 @@ public void trimUnreferencedReaders() throws IOException {
16891691
}
16901692
}
16911693

1694+
private long getMinReferencedGen() throws IOException {
1695+
assert readLock.isHeldByCurrentThread() || writeLock.isHeldByCurrentThread();
1696+
long minReferencedGen = Math.min(
1697+
deletionPolicy.minTranslogGenRequired(readers, current),
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+
16921708
/**
16931709
* deletes all files associated with a reader. package-private to be able to simulate node failures at this point
16941710
*/

0 commit comments

Comments
 (0)