Fix file handle leak due to transaction logs

Patch by Stefania Alborghetti; reviewed by Tyler Hobbs for
CASSANDRA-11594


Project: http://git-wip-us.apache.org/repos/asf/cassandra/repo
Commit: http://git-wip-us.apache.org/repos/asf/cassandra/commit/da07130e
Tree: http://git-wip-us.apache.org/repos/asf/cassandra/tree/da07130e
Diff: http://git-wip-us.apache.org/repos/asf/cassandra/diff/da07130e

Branch: refs/heads/cassandra-3.9
Commit: da07130e4e08cd8645001c6f7fa9ea2acb2072ca
Parents: f092f7b
Author: Stefania Alborghetti <stefania.alborghe...@datastax.com>
Authored: Fri Aug 26 11:58:23 2016 -0500
Committer: Tyler Hobbs <tylerlho...@gmail.com>
Committed: Fri Aug 26 11:58:23 2016 -0500

----------------------------------------------------------------------
 CHANGES.txt                                               |  3 +++
 .../apache/cassandra/db/lifecycle/LogAwareFileLister.java | 10 ++++++----
 src/java/org/apache/cassandra/db/lifecycle/LogFile.java   |  4 ++--
 .../org/apache/cassandra/db/lifecycle/LogReplica.java     |  4 ++--
 .../org/apache/cassandra/db/lifecycle/LogReplicaSet.java  |  5 +++--
 .../org/apache/cassandra/db/lifecycle/LogTransaction.java |  8 ++------
 6 files changed, 18 insertions(+), 16 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cassandra/blob/da07130e/CHANGES.txt
----------------------------------------------------------------------
diff --git a/CHANGES.txt b/CHANGES.txt
index 59c8a4b..cf14f67 100644
--- a/CHANGES.txt
+++ b/CHANGES.txt
@@ -1,4 +1,7 @@
 3.0.9
+ * Fix file handle leaks due to simultaneous compaction/repair and
+   listing snapshots, calculating snapshot sizes, or making schema
+   changes (CASSANDRA-11594)
  * Fix nodetool repair exits with 0 for some errors (CASSANDRA-12508)
  * Do not shut down BatchlogManager twice during drain (CASSANDRA-12504)
  * Disk failure policy should not be invoked on out of space (CASSANDRA-12385)

http://git-wip-us.apache.org/repos/asf/cassandra/blob/da07130e/src/java/org/apache/cassandra/db/lifecycle/LogAwareFileLister.java
----------------------------------------------------------------------
diff --git a/src/java/org/apache/cassandra/db/lifecycle/LogAwareFileLister.java 
b/src/java/org/apache/cassandra/db/lifecycle/LogAwareFileLister.java
index e9072c4..7728f9c 100644
--- a/src/java/org/apache/cassandra/db/lifecycle/LogAwareFileLister.java
+++ b/src/java/org/apache/cassandra/db/lifecycle/LogAwareFileLister.java
@@ -122,10 +122,12 @@ final class LogAwareFileLister
      */
     void classifyFiles(File txnFile)
     {
-        LogFile txn = LogFile.make(txnFile);
-        readTxnLog(txn);
-        classifyFiles(txn);
-        files.put(txnFile, FileType.TXN_LOG);
+        try (LogFile txn = LogFile.make(txnFile))
+        {
+            readTxnLog(txn);
+            classifyFiles(txn);
+            files.put(txnFile, FileType.TXN_LOG);
+        }
     }
 
     void readTxnLog(LogFile txn)

http://git-wip-us.apache.org/repos/asf/cassandra/blob/da07130e/src/java/org/apache/cassandra/db/lifecycle/LogFile.java
----------------------------------------------------------------------
diff --git a/src/java/org/apache/cassandra/db/lifecycle/LogFile.java 
b/src/java/org/apache/cassandra/db/lifecycle/LogFile.java
index 6d0c835..8560410 100644
--- a/src/java/org/apache/cassandra/db/lifecycle/LogFile.java
+++ b/src/java/org/apache/cassandra/db/lifecycle/LogFile.java
@@ -52,7 +52,7 @@ import static org.apache.cassandra.utils.Throwables.merge;
  *
  * @see LogTransaction
  */
-final class LogFile
+final class LogFile implements AutoCloseable
 {
     private static final Logger logger = 
LoggerFactory.getLogger(LogFile.class);
 
@@ -374,7 +374,7 @@ final class LogFile
         return replicas.exists();
     }
 
-    void close()
+    public void close()
     {
         replicas.close();
     }

http://git-wip-us.apache.org/repos/asf/cassandra/blob/da07130e/src/java/org/apache/cassandra/db/lifecycle/LogReplica.java
----------------------------------------------------------------------
diff --git a/src/java/org/apache/cassandra/db/lifecycle/LogReplica.java 
b/src/java/org/apache/cassandra/db/lifecycle/LogReplica.java
index 79b9749..712a22d 100644
--- a/src/java/org/apache/cassandra/db/lifecycle/LogReplica.java
+++ b/src/java/org/apache/cassandra/db/lifecycle/LogReplica.java
@@ -34,7 +34,7 @@ import org.apache.cassandra.utils.CLibrary;
  *
  * @see LogFile
  */
-final class LogReplica
+final class LogReplica implements AutoCloseable
 {
     private final File file;
     private int folderDescriptor;
@@ -88,7 +88,7 @@ final class LogReplica
         return file.exists();
     }
 
-    void close()
+    public void close()
     {
         if (folderDescriptor >= 0)
         {

http://git-wip-us.apache.org/repos/asf/cassandra/blob/da07130e/src/java/org/apache/cassandra/db/lifecycle/LogReplicaSet.java
----------------------------------------------------------------------
diff --git a/src/java/org/apache/cassandra/db/lifecycle/LogReplicaSet.java 
b/src/java/org/apache/cassandra/db/lifecycle/LogReplicaSet.java
index d9d9213..d8b2141 100644
--- a/src/java/org/apache/cassandra/db/lifecycle/LogReplicaSet.java
+++ b/src/java/org/apache/cassandra/db/lifecycle/LogReplicaSet.java
@@ -41,7 +41,7 @@ import org.apache.cassandra.utils.Throwables;
  *
  * @see LogReplica, LogFile
  */
-public class LogReplicaSet
+public class LogReplicaSet implements AutoCloseable
 {
     private static final Logger logger = 
LoggerFactory.getLogger(LogReplicaSet.class);
 
@@ -72,6 +72,7 @@ public class LogReplicaSet
         if (replicasByFile.containsKey(folder))
             return;
 
+        @SuppressWarnings("resource")  // LogReplicas are closed in 
LogReplicaSet::close
         final LogReplica replica = LogReplica.create(folder, fileName);
 
         records.forEach(replica::append);
@@ -201,7 +202,7 @@ public class LogReplicaSet
                : false;
     }
 
-    void close()
+    public void close()
     {
         Throwables.maybeFail(Throwables.perform(null, 
replicas().stream().map(r -> r::close)));
     }

http://git-wip-us.apache.org/repos/asf/cassandra/blob/da07130e/src/java/org/apache/cassandra/db/lifecycle/LogTransaction.java
----------------------------------------------------------------------
diff --git a/src/java/org/apache/cassandra/db/lifecycle/LogTransaction.java 
b/src/java/org/apache/cassandra/db/lifecycle/LogTransaction.java
index b34ca60..ca644eb 100644
--- a/src/java/org/apache/cassandra/db/lifecycle/LogTransaction.java
+++ b/src/java/org/apache/cassandra/db/lifecycle/LogTransaction.java
@@ -425,8 +425,8 @@ class LogTransaction extends 
Transactional.AbstractTransactional implements Tran
 
         static void removeUnfinishedLeftovers(String name, List<File> logFiles)
         {
-            LogFile txn = LogFile.make(name, logFiles);
-            try
+
+            try(LogFile txn = LogFile.make(name, logFiles))
             {
                 if (txn.verify())
                 {
@@ -439,10 +439,6 @@ class LogTransaction extends 
Transactional.AbstractTransactional implements Tran
                     logger.error("Unexpected disk state: failed to read 
transaction txn {}", txn);
                 }
             }
-            finally
-            {
-                txn.close();
-            }
         }
     }
 }

Reply via email to