Skip to content

Commit

Permalink
Separate translog from index deletion conditions (#52556)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
ywelsch authored Feb 20, 2020
1 parent 3473987 commit 16af047
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -539,8 +539,8 @@ public Translog.Location getTranslogLastWriteLocation() {
private void revisitIndexDeletionPolicyOnTranslogSynced() throws IOException {
if (combinedDeletionPolicy.hasUnreferencedCommits()) {
indexWriter.deleteUnusedFiles();
translog.trimUnreferencedReaders();
}
translog.trimUnreferencedReaders();
}

@Override
Expand Down
39 changes: 27 additions & 12 deletions server/src/main/java/org/elasticsearch/index/translog/Translog.java
Original file line number Diff line number Diff line change
Expand Up @@ -1646,24 +1646,25 @@ public void rollGeneration() throws IOException {
* required generation
*/
public void trimUnreferencedReaders() throws IOException {
// move most of the data to disk to reduce the time the lock is held
// first check under read lock if any readers can be trimmed
try (ReleasableLock ignored = readLock.acquire()) {
if (closed.get()) {
// we're shutdown potentially on some tragic event, don't delete anything
return;
}
if (getMinReferencedGen() == getMinFileGeneration()) {
return;
}
}

// move most of the data to disk to reduce the time the write lock is held
sync();
try (ReleasableLock ignored = writeLock.acquire()) {
if (closed.get()) {
// we're shutdown potentially on some tragic event, don't delete anything
return;
}
long minReferencedGen = Math.min(
deletionPolicy.getMinTranslogGenRequiredByLocks(),
minGenerationForSeqNo(deletionPolicy.getLocalCheckpointOfSafeCommit() + 1, current, readers));
assert minReferencedGen >= getMinFileGeneration() :
"deletion policy requires a minReferenceGen of [" + minReferencedGen + "] but the lowest gen available is ["
+ getMinFileGeneration() + "]";
assert minReferencedGen <= currentFileGeneration() :
"deletion policy requires a minReferenceGen of [" + minReferencedGen + "] which is higher than the current generation ["
+ currentFileGeneration() + "]";


final long minReferencedGen = getMinReferencedGen();
for (Iterator<TranslogReader> iterator = readers.iterator(); iterator.hasNext(); ) {
TranslogReader reader = iterator.next();
if (reader.getGeneration() >= minReferencedGen) {
Expand All @@ -1690,6 +1691,20 @@ public void trimUnreferencedReaders() throws IOException {
}
}

private long getMinReferencedGen() {
assert readLock.isHeldByCurrentThread() || writeLock.isHeldByCurrentThread();
long minReferencedGen = Math.min(
deletionPolicy.getMinTranslogGenRequiredByLocks(),
minGenerationForSeqNo(deletionPolicy.getLocalCheckpointOfSafeCommit() + 1, current, readers));
assert minReferencedGen >= getMinFileGeneration() :
"deletion policy requires a minReferenceGen of [" + minReferencedGen + "] but the lowest gen available is ["
+ getMinFileGeneration() + "]";
assert minReferencedGen <= currentFileGeneration() :
"deletion policy requires a minReferenceGen of [" + minReferencedGen + "] which is higher than the current generation ["
+ currentFileGeneration() + "]";
return minReferencedGen;
}

/**
* deletes all files associated with a reader. package-private to be able to simulate node failures at this point
*/
Expand Down

0 comments on commit 16af047

Please sign in to comment.