@@ -1646,24 +1646,25 @@ public void rollGeneration() throws IOException {
1646
1646
* required generation
1647
1647
*/
1648
1648
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
1650
1661
sync ();
1651
1662
try (ReleasableLock ignored = writeLock .acquire ()) {
1652
1663
if (closed .get ()) {
1653
1664
// we're shutdown potentially on some tragic event, don't delete anything
1654
1665
return ;
1655
1666
}
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 ();
1667
1668
for (Iterator <TranslogReader > iterator = readers .iterator (); iterator .hasNext (); ) {
1668
1669
TranslogReader reader = iterator .next ();
1669
1670
if (reader .getGeneration () >= minReferencedGen ) {
@@ -1690,6 +1691,20 @@ public void trimUnreferencedReaders() throws IOException {
1690
1691
}
1691
1692
}
1692
1693
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
+
1693
1708
/**
1694
1709
* deletes all files associated with a reader. package-private to be able to simulate node failures at this point
1695
1710
*/
0 commit comments