@@ -1678,6 +1678,7 @@ public void rollGeneration() throws IOException {
1678
1678
* required generation
1679
1679
*/
1680
1680
public void trimUnreferencedReaders () throws IOException {
1681
+ List <TranslogReader > toDeleteReaderList = new ArrayList <>();
1681
1682
try (ReleasableLock ignored = writeLock .acquire ()) {
1682
1683
if (closed .get ()) {
1683
1684
// we're shutdown potentially on some tragic event, don't delete anything
@@ -1691,22 +1692,14 @@ public void trimUnreferencedReaders() throws IOException {
1691
1692
"deletion policy requires a minReferenceGen of [" + minReferencedGen + "] which is higher than the current generation ["
1692
1693
+ currentFileGeneration () + "]" ;
1693
1694
1694
-
1695
1695
for (Iterator <TranslogReader > iterator = readers .iterator (); iterator .hasNext (); ) {
1696
1696
TranslogReader reader = iterator .next ();
1697
1697
if (reader .getGeneration () >= minReferencedGen ) {
1698
1698
break ;
1699
1699
}
1700
1700
iterator .remove ();
1701
+ toDeleteReaderList .add (reader );
1701
1702
IOUtils .closeWhileHandlingException (reader );
1702
- final Path translogPath = reader .path ();
1703
- logger .trace ("delete translog file [{}], not referenced and not current anymore" , translogPath );
1704
- // The checkpoint is used when opening the translog to know which files should be recovered from.
1705
- // We now update the checkpoint to ignore the file we are going to remove.
1706
- // Note that there is a provision in recoverFromFiles to allow for the case where we synced the checkpoint
1707
- // but crashed before we could delete the file.
1708
- current .sync ();
1709
- deleteReaderFiles (reader );
1710
1703
}
1711
1704
assert readers .isEmpty () == false || current .generation == minReferencedGen :
1712
1705
"all readers were cleaned but the minReferenceGen [" + minReferencedGen + "] is not the current writer's gen [" +
@@ -1715,6 +1708,24 @@ public void trimUnreferencedReaders() throws IOException {
1715
1708
closeOnTragicEvent (ex );
1716
1709
throw ex ;
1717
1710
}
1711
+ // Do sync outside the writeLock to avoid blocking all writing thread.
1712
+ if (toDeleteReaderList .isEmpty () == false ) {
1713
+ try {
1714
+ // The checkpoint is used when opening the translog to know which files should be recovered from.
1715
+ // We now update the checkpoint to ignore the file we are going to remove.
1716
+ // Note that there is a provision in recoverFromFiles to allow for the case where we synced the checkpoint
1717
+ // but crashed before we could delete the file.
1718
+ sync ();
1719
+ for (TranslogReader reader : toDeleteReaderList ) {
1720
+ final Path translogPath = reader .path ();
1721
+ logger .trace ("delete translog file [{}], not referenced and not current anymore" , translogPath );
1722
+ deleteReaderFiles (reader );
1723
+ }
1724
+ } catch (final Exception ex ) {
1725
+ closeOnTragicEvent (ex );
1726
+ throw ex ;
1727
+ }
1728
+ }
1718
1729
}
1719
1730
1720
1731
/**
0 commit comments