@@ -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