Author: jbellis
Date: Wed Dec 22 20:27:43 2010
New Revision: 1052057

URL: http://svn.apache.org/viewvc?rev=1052057&view=rev
Log:
add FileUtils.closeQuietly and finally-close blocks
patch by Dave Brosius; reviewed by jbellis for CASSANDRA-1886

Modified:
    
cassandra/branches/cassandra-0.7/src/java/org/apache/cassandra/auth/SimpleAuthenticator.java
    
cassandra/branches/cassandra-0.7/src/java/org/apache/cassandra/db/ColumnFamilyStore.java
    
cassandra/branches/cassandra-0.7/src/java/org/apache/cassandra/db/columniterator/SSTableNamesIterator.java
    
cassandra/branches/cassandra-0.7/src/java/org/apache/cassandra/db/commitlog/CommitLog.java
    
cassandra/branches/cassandra-0.7/src/java/org/apache/cassandra/db/commitlog/CommitLogHeader.java
    
cassandra/branches/cassandra-0.7/src/java/org/apache/cassandra/gms/FailureDetector.java
    
cassandra/branches/cassandra-0.7/src/java/org/apache/cassandra/io/sstable/SSTableReader.java
    
cassandra/branches/cassandra-0.7/src/java/org/apache/cassandra/io/sstable/SSTableTracker.java
    
cassandra/branches/cassandra-0.7/src/java/org/apache/cassandra/io/util/FileUtils.java
    
cassandra/branches/cassandra-0.7/src/java/org/apache/cassandra/io/util/MmappedSegmentedFile.java
    
cassandra/branches/cassandra-0.7/src/java/org/apache/cassandra/locator/PropertyFileSnitch.java
    
cassandra/branches/cassandra-0.7/src/java/org/apache/cassandra/streaming/FileStreamTask.java

Modified: 
cassandra/branches/cassandra-0.7/src/java/org/apache/cassandra/auth/SimpleAuthenticator.java
URL: 
http://svn.apache.org/viewvc/cassandra/branches/cassandra-0.7/src/java/org/apache/cassandra/auth/SimpleAuthenticator.java?rev=1052057&r1=1052056&r2=1052057&view=diff
==============================================================================
--- 
cassandra/branches/cassandra-0.7/src/java/org/apache/cassandra/auth/SimpleAuthenticator.java
 (original)
+++ 
cassandra/branches/cassandra-0.7/src/java/org/apache/cassandra/auth/SimpleAuthenticator.java
 Wed Dec 22 20:27:43 2010
@@ -28,6 +28,7 @@ import java.util.Properties;
 import java.util.Map;
 
 import org.apache.cassandra.config.ConfigurationException;
+import org.apache.cassandra.io.util.FileUtils;
 import org.apache.cassandra.thrift.AuthenticationException;
 import org.apache.cassandra.utils.FBUtilities;
 
@@ -92,12 +93,12 @@ public class SimpleAuthenticator impleme
 
         boolean authenticated = false;
 
+        InputStream in = null;
         try
         {
-            FileInputStream in = new FileInputStream(pfilename);
+            in = new BufferedInputStream(new FileInputStream(pfilename));
             Properties props = new Properties();
             props.load(in);
-            in.close();
 
             // note we keep the message here and for the wrong password 
exactly the same to prevent attackers from guessing what users are valid
             if (null == props.getProperty(username)) throw new 
AuthenticationException(authenticationErrorMessage(mode, username));
@@ -125,6 +126,10 @@ public class SimpleAuthenticator impleme
         {
             throw new RuntimeException("Unexpected authentication problem", e);
         }
+        finally
+        {
+            FileUtils.closeQuietly(in);
+        }
 
         if (!authenticated) throw new 
AuthenticationException(authenticationErrorMessage(mode, username));
 

Modified: 
cassandra/branches/cassandra-0.7/src/java/org/apache/cassandra/db/ColumnFamilyStore.java
URL: 
http://svn.apache.org/viewvc/cassandra/branches/cassandra-0.7/src/java/org/apache/cassandra/db/ColumnFamilyStore.java?rev=1052057&r1=1052056&r2=1052057&view=diff
==============================================================================
--- 
cassandra/branches/cassandra-0.7/src/java/org/apache/cassandra/db/ColumnFamilyStore.java
 (original)
+++ 
cassandra/branches/cassandra-0.7/src/java/org/apache/cassandra/db/ColumnFamilyStore.java
 Wed Dec 22 20:27:43 2010
@@ -266,12 +266,13 @@ public class ColumnFamilyStore implement
         Set<DecoratedKey> keys = new TreeSet<DecoratedKey>();
         if (path.exists())
         {
+            ObjectInputStream in = null;
             try
             {
                 long start = System.currentTimeMillis();
 
                 logger.info(String.format("reading saved cache %s", path));
-                ObjectInputStream in = new ObjectInputStream(new 
BufferedInputStream(new FileInputStream(path)));
+                in = new ObjectInputStream(new BufferedInputStream(new 
FileInputStream(path)));
                 while (in.available() > 0)
                 {
                     int size = in.readInt();
@@ -279,7 +280,6 @@ public class ColumnFamilyStore implement
                     in.readFully(bytes);
                     
keys.add(StorageService.getPartitioner().decorateKey(ByteBuffer.wrap(bytes)));
                 }
-                in.close();
                 if (logger.isDebugEnabled())
                     logger.debug(String.format("completed reading (%d ms; %d 
keys) saved cache %s",
                                                System.currentTimeMillis() - 
start, keys.size(), path));
@@ -288,6 +288,10 @@ public class ColumnFamilyStore implement
             {
                 logger.warn(String.format("error reading saved cache %s", 
path.getAbsolutePath()), ioe);
             }
+            finally
+            {
+                FileUtils.closeQuietly(in);
+            }
         }
         return keys;
     }

Modified: 
cassandra/branches/cassandra-0.7/src/java/org/apache/cassandra/db/columniterator/SSTableNamesIterator.java
URL: 
http://svn.apache.org/viewvc/cassandra/branches/cassandra-0.7/src/java/org/apache/cassandra/db/columniterator/SSTableNamesIterator.java?rev=1052057&r1=1052056&r2=1052057&view=diff
==============================================================================
--- 
cassandra/branches/cassandra-0.7/src/java/org/apache/cassandra/db/columniterator/SSTableNamesIterator.java
 (original)
+++ 
cassandra/branches/cassandra-0.7/src/java/org/apache/cassandra/db/columniterator/SSTableNamesIterator.java
 Wed Dec 22 20:27:43 2010
@@ -39,6 +39,7 @@ import org.apache.cassandra.io.sstable.I
 import org.apache.cassandra.io.sstable.SSTableReader;
 import org.apache.cassandra.io.util.FileDataInput;
 import org.apache.cassandra.io.util.FileMark;
+import org.apache.cassandra.io.util.FileUtils;
 import org.apache.cassandra.utils.BloomFilter;
 import org.apache.cassandra.utils.FBUtilities;
 import org.slf4j.Logger;
@@ -78,14 +79,7 @@ public class SSTableNamesIterator extend
         }
         finally
         {
-            try
-            {
-                file.close();
-            }
-            catch (IOException ioe)
-            {
-                logger.warn("error closing " + file.getPath());
-            }
+            FileUtils.closeQuietly(file);
         }
     }
 

Modified: 
cassandra/branches/cassandra-0.7/src/java/org/apache/cassandra/db/commitlog/CommitLog.java
URL: 
http://svn.apache.org/viewvc/cassandra/branches/cassandra-0.7/src/java/org/apache/cassandra/db/commitlog/CommitLog.java?rev=1052057&r1=1052056&r2=1052057&view=diff
==============================================================================
--- 
cassandra/branches/cassandra-0.7/src/java/org/apache/cassandra/db/commitlog/CommitLog.java
 (original)
+++ 
cassandra/branches/cassandra-0.7/src/java/org/apache/cassandra/db/commitlog/CommitLog.java
 Wed Dec 22 20:27:43 2010
@@ -339,7 +339,7 @@ public class CommitLog
             }
             finally
             {
-                reader.close();
+                FileUtils.closeQuietly(reader);
                 logger.info("Finished reading " + file);
             }
         }

Modified: 
cassandra/branches/cassandra-0.7/src/java/org/apache/cassandra/db/commitlog/CommitLogHeader.java
URL: 
http://svn.apache.org/viewvc/cassandra/branches/cassandra-0.7/src/java/org/apache/cassandra/db/commitlog/CommitLogHeader.java?rev=1052057&r1=1052056&r2=1052057&view=diff
==============================================================================
--- 
cassandra/branches/cassandra-0.7/src/java/org/apache/cassandra/db/commitlog/CommitLogHeader.java
 (original)
+++ 
cassandra/branches/cassandra-0.7/src/java/org/apache/cassandra/db/commitlog/CommitLogHeader.java
 Wed Dec 22 20:27:43 2010
@@ -25,8 +25,8 @@ import java.util.Map;
 import java.util.zip.CRC32;
 import java.util.zip.Checksum;
 
-import org.apache.cassandra.config.CFMetaData;
 import org.apache.cassandra.io.ICompactSerializer2;
+import org.apache.cassandra.io.util.FileUtils;
 
 public class CommitLogHeader
 {
@@ -132,13 +132,12 @@ public class CommitLogHeader
         DataInputStream reader = null;
         try
         {
-            reader = new DataInputStream(new FileInputStream(headerFile));
+            reader = new DataInputStream(new BufferedInputStream(new 
FileInputStream(headerFile)));
             return serializer.deserialize(reader);
         }
         finally
         {
-            if (reader != null)
-                reader.close();
+            FileUtils.closeQuietly(reader);
         }
     }
 

Modified: 
cassandra/branches/cassandra-0.7/src/java/org/apache/cassandra/gms/FailureDetector.java
URL: 
http://svn.apache.org/viewvc/cassandra/branches/cassandra-0.7/src/java/org/apache/cassandra/gms/FailureDetector.java?rev=1052057&r1=1052056&r2=1052057&view=diff
==============================================================================
--- 
cassandra/branches/cassandra-0.7/src/java/org/apache/cassandra/gms/FailureDetector.java
 (original)
+++ 
cassandra/branches/cassandra-0.7/src/java/org/apache/cassandra/gms/FailureDetector.java
 Wed Dec 22 20:27:43 2010
@@ -18,10 +18,12 @@
 
 package org.apache.cassandra.gms;
 
+import java.io.BufferedOutputStream;
 import java.io.File;
 import java.io.FileOutputStream;
 import java.io.IOException;
 import java.io.IOError;
+import java.io.OutputStream;
 import java.lang.management.ManagementFactory;
 import java.util.*;
 import javax.management.MBeanServer;
@@ -31,6 +33,7 @@ import org.apache.commons.lang.StringUti
 
 import java.net.InetAddress;
 import org.apache.cassandra.config.DatabaseDescriptor;
+import org.apache.cassandra.io.util.FileUtils;
 import org.apache.cassandra.utils.FBUtilities;
 import org.apache.cassandra.utils.BoundedStatsDeque;
 import org.slf4j.Logger;
@@ -83,17 +86,21 @@ public class FailureDetector implements 
      */
     public void dumpInterArrivalTimes()
     {
+        OutputStream os = null;
         try
         {
             File file = File.createTempFile("failuredetector-", ".dat");
-            FileOutputStream fos = new FileOutputStream(file, true);
-            fos.write(toString().getBytes());
-            fos.close();
+            os = new BufferedOutputStream(new FileOutputStream(file, true));
+            os.write(toString().getBytes());
         }
         catch (IOException e)
         {
             throw new IOError(e);
         }
+        finally
+        {
+            FileUtils.closeQuietly(os);
+        }
     }
     
     public void setPhiConvictThreshold(int phi)

Modified: 
cassandra/branches/cassandra-0.7/src/java/org/apache/cassandra/io/sstable/SSTableReader.java
URL: 
http://svn.apache.org/viewvc/cassandra/branches/cassandra-0.7/src/java/org/apache/cassandra/io/sstable/SSTableReader.java?rev=1052057&r1=1052056&r2=1052057&view=diff
==============================================================================
--- 
cassandra/branches/cassandra-0.7/src/java/org/apache/cassandra/io/sstable/SSTableReader.java
 (original)
+++ 
cassandra/branches/cassandra-0.7/src/java/org/apache/cassandra/io/sstable/SSTableReader.java
 Wed Dec 22 20:27:43 2010
@@ -19,6 +19,7 @@
 
 package org.apache.cassandra.io.sstable;
 
+import java.io.BufferedInputStream;
 import java.io.DataInput;
 import java.io.DataInputStream;
 import java.io.File;
@@ -53,6 +54,7 @@ import org.apache.cassandra.dht.Range;
 import org.apache.cassandra.io.ICompactSerializer2;
 import org.apache.cassandra.io.util.BufferedRandomAccessFile;
 import org.apache.cassandra.io.util.FileDataInput;
+import org.apache.cassandra.io.util.FileUtils;
 import org.apache.cassandra.io.util.SegmentedFile;
 import org.apache.cassandra.service.StorageService;
 import org.apache.cassandra.utils.*;
@@ -174,11 +176,18 @@ public class SSTableReader extends SSTab
         File statsFile = new 
File(descriptor.filenameFor(SSTable.COMPONENT_STATS));
         if (statsFile.exists())
         {
-            logger.debug("Load statistics for {}", descriptor);
-            DataInputStream dis = new DataInputStream(new 
FileInputStream(statsFile));
-            rowSizes = EstimatedHistogram.serializer.deserialize(dis);
-            columnCounts = EstimatedHistogram.serializer.deserialize(dis);
-            dis.close();
+            DataInputStream dis = null;
+            try
+            {
+                logger.debug("Load statistics for {}", descriptor);
+                dis = new DataInputStream(new BufferedInputStream(new 
FileInputStream(statsFile)));
+                rowSizes = EstimatedHistogram.serializer.deserialize(dis);
+                columnCounts = EstimatedHistogram.serializer.deserialize(dis);
+            }
+            finally
+            {
+                FileUtils.closeQuietly(dis);
+            }
         }
         else
         {
@@ -253,14 +262,15 @@ public class SSTableReader extends SSTab
 
     void loadBloomFilter() throws IOException
     {
-        DataInputStream stream = new DataInputStream(new 
FileInputStream(descriptor.filenameFor(Component.FILTER)));
+        DataInputStream stream = null;
         try
         {
+            stream = new DataInputStream(new BufferedInputStream(new 
FileInputStream(descriptor.filenameFor(Component.FILTER))));
             bf = BloomFilter.serializer().deserialize(stream);
         }
         finally
         {
-            stream.close();
+            FileUtils.closeQuietly(stream);
         }
     }
 
@@ -316,7 +326,7 @@ public class SSTableReader extends SSTab
         }
         finally
         {
-            input.close();
+            FileUtils.closeQuietly(input);
         }
 
         // finalize the state of the reader
@@ -497,14 +507,7 @@ public class SSTableReader extends SSTab
             }
             finally
             {
-                try
-                {
-                    input.close();
-                }
-                catch (IOException e)
-                {
-                    logger.error("error closing file", e);
-                }
+                FileUtils.closeQuietly(input);
             }
         }
 

Modified: 
cassandra/branches/cassandra-0.7/src/java/org/apache/cassandra/io/sstable/SSTableTracker.java
URL: 
http://svn.apache.org/viewvc/cassandra/branches/cassandra-0.7/src/java/org/apache/cassandra/io/sstable/SSTableTracker.java?rev=1052057&r1=1052056&r2=1052057&view=diff
==============================================================================
--- 
cassandra/branches/cassandra-0.7/src/java/org/apache/cassandra/io/sstable/SSTableTracker.java
 (original)
+++ 
cassandra/branches/cassandra-0.7/src/java/org/apache/cassandra/io/sstable/SSTableTracker.java
 Wed Dec 22 20:27:43 2010
@@ -37,7 +37,9 @@ import org.apache.cassandra.cache.JMXIns
 import org.apache.cassandra.config.DatabaseDescriptor;
 import org.apache.cassandra.db.ColumnFamily;
 import org.apache.cassandra.db.DecoratedKey;
+import org.apache.cassandra.io.util.FileUtils;
 import org.apache.cassandra.utils.Pair;
+
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -75,19 +77,29 @@ public class SSTableTracker implements I
             logger.info("saving " + msgSuffix);
             int count = 0;
             File tmpFile = File.createTempFile(savedCachePath.getName(), null, 
savedCachePath.getParentFile());
-            FileOutputStream fout = new FileOutputStream(tmpFile);
-            ObjectOutputStream out = new ObjectOutputStream(new 
BufferedOutputStream(fout));
-            FileDescriptor fd = fout.getFD();
-            for (K key : cache.getKeySet())
-            {
-                ByteBuffer bytes = converter.apply(key);
-                out.writeInt(bytes.remaining());
-                
out.write(bytes.array(),bytes.position()+bytes.arrayOffset(),bytes.remaining());
-                ++count;
-            }
-            out.flush();
-            fd.sync();
-            out.close();
+            
+            FileOutputStream fout = null;
+            ObjectOutputStream out = null;
+            try
+            {
+                fout = new FileOutputStream(tmpFile);
+                out = new ObjectOutputStream(new BufferedOutputStream(fout));
+                FileDescriptor fd = fout.getFD();
+                for (K key : cache.getKeySet())
+                {
+                    ByteBuffer bytes = converter.apply(key);
+                    out.writeInt(bytes.remaining());
+                    
out.write(bytes.array(),bytes.position()+bytes.arrayOffset(),bytes.remaining());
+                    ++count;
+                }
+                out.flush();
+                fd.sync();
+            }
+            finally
+            {
+                FileUtils.closeQuietly(out);
+                FileUtils.closeQuietly(fout);
+            }
             if (!tmpFile.renameTo(savedCachePath))
                 throw new IOException("Unable to rename cache to " + 
savedCachePath);
             if (logger.isDebugEnabled())

Modified: 
cassandra/branches/cassandra-0.7/src/java/org/apache/cassandra/io/util/FileUtils.java
URL: 
http://svn.apache.org/viewvc/cassandra/branches/cassandra-0.7/src/java/org/apache/cassandra/io/util/FileUtils.java?rev=1052057&r1=1052056&r2=1052057&view=diff
==============================================================================
--- 
cassandra/branches/cassandra-0.7/src/java/org/apache/cassandra/io/util/FileUtils.java
 (original)
+++ 
cassandra/branches/cassandra-0.7/src/java/org/apache/cassandra/io/util/FileUtils.java
 Wed Dec 22 20:27:43 2010
@@ -18,14 +18,12 @@
 
 package org.apache.cassandra.io.util;
 
-import java.io.File;
-import java.io.FileNotFoundException;
-import java.io.IOException;
-import java.io.RandomAccessFile;
+import java.io.*;
 import java.text.DecimalFormat;
 import java.util.Comparator;
 import java.util.List;
 
+import org.apache.cassandra.utils.FBUtilities;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -85,6 +83,19 @@ public class FileUtils
         }
     }
 
+    public static void closeQuietly(Closeable c)
+    {
+        try
+        {
+            if (c != null)
+                c.close();
+        }
+        catch (Exception e)
+        {
+            logger_.warn("Failed closing stream", e);
+        }
+    }
+
     public static class FileComparator implements Comparator<File>
     {
         public int compare(File f, File f2)

Modified: 
cassandra/branches/cassandra-0.7/src/java/org/apache/cassandra/io/util/MmappedSegmentedFile.java
URL: 
http://svn.apache.org/viewvc/cassandra/branches/cassandra-0.7/src/java/org/apache/cassandra/io/util/MmappedSegmentedFile.java?rev=1052057&r1=1052056&r2=1052057&view=diff
==============================================================================
--- 
cassandra/branches/cassandra-0.7/src/java/org/apache/cassandra/io/util/MmappedSegmentedFile.java
 (original)
+++ 
cassandra/branches/cassandra-0.7/src/java/org/apache/cassandra/io/util/MmappedSegmentedFile.java
 Wed Dec 22 20:27:43 2010
@@ -173,14 +173,7 @@ public class MmappedSegmentedFile extend
             }
             finally
             {
-                try
-                {
-                    if (raf != null) raf.close();
-                }
-                catch (IOException e)
-                {
-                    throw new IOError(e);
-                }
+                FileUtils.closeQuietly(raf);
             }
             return segments;
         }

Modified: 
cassandra/branches/cassandra-0.7/src/java/org/apache/cassandra/locator/PropertyFileSnitch.java
URL: 
http://svn.apache.org/viewvc/cassandra/branches/cassandra-0.7/src/java/org/apache/cassandra/locator/PropertyFileSnitch.java?rev=1052057&r1=1052056&r2=1052057&view=diff
==============================================================================
--- 
cassandra/branches/cassandra-0.7/src/java/org/apache/cassandra/locator/PropertyFileSnitch.java
 (original)
+++ 
cassandra/branches/cassandra-0.7/src/java/org/apache/cassandra/locator/PropertyFileSnitch.java
 Wed Dec 22 20:27:43 2010
@@ -18,22 +18,24 @@
 
 package org.apache.cassandra.locator;
 
+import java.io.BufferedReader;
 import java.io.FileReader;
 import java.io.IOException;
+import java.io.Reader;
 import java.net.InetAddress;
 import java.net.UnknownHostException;
 import java.util.HashMap;
 import java.util.Map;
 import java.util.Properties;
 
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
 import org.apache.cassandra.config.ConfigurationException;
+import org.apache.cassandra.io.util.FileUtils;
 import org.apache.cassandra.service.StorageService;
 import org.apache.cassandra.utils.FBUtilities;
 import org.apache.cassandra.utils.ResourceWatcher;
 import org.apache.cassandra.utils.WrappedRunnable;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 /**
  * Used to determine if two IP's are in the same datacenter or on the same 
rack.
@@ -112,14 +114,20 @@ public class PropertyFileSnitch extends 
 
         String rackPropertyFilename = 
FBUtilities.resourceToFile(RACK_PROPERTY_FILENAME);
         Properties properties = new Properties();
+        Reader reader = null;
         try
         {
-            properties.load(new FileReader(rackPropertyFilename));
+            reader = new BufferedReader(new FileReader(rackPropertyFilename));
+            properties.load(reader);
         }
         catch (IOException e)
         {
             throw new ConfigurationException("Unable to read " + 
RACK_PROPERTY_FILENAME, e);
         }
+        finally
+        {
+            FileUtils.closeQuietly(reader);
+        }
 
         for (Map.Entry<Object, Object> entry : properties.entrySet())
         {

Modified: 
cassandra/branches/cassandra-0.7/src/java/org/apache/cassandra/streaming/FileStreamTask.java
URL: 
http://svn.apache.org/viewvc/cassandra/branches/cassandra-0.7/src/java/org/apache/cassandra/streaming/FileStreamTask.java?rev=1052057&r1=1052056&r2=1052057&view=diff
==============================================================================
--- 
cassandra/branches/cassandra-0.7/src/java/org/apache/cassandra/streaming/FileStreamTask.java
 (original)
+++ 
cassandra/branches/cassandra-0.7/src/java/org/apache/cassandra/streaming/FileStreamTask.java
 Wed Dec 22 20:27:43 2010
@@ -25,7 +25,7 @@ import java.nio.ByteBuffer;
 import java.nio.channels.FileChannel;
 import java.nio.channels.SocketChannel;
 
-import org.apache.cassandra.streaming.StreamHeader;
+import org.apache.cassandra.io.util.FileUtils;
 import org.apache.cassandra.utils.FBUtilities;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -35,6 +35,7 @@ import org.apache.cassandra.net.Messagin
 import org.apache.cassandra.utils.Pair;
 import org.apache.cassandra.utils.WrappedRunnable;
 
+
 public class FileStreamTask extends WrappedRunnable
 {
     private static Logger logger = LoggerFactory.getLogger( 
FileStreamTask.class );
@@ -109,14 +110,7 @@ public class FileStreamTask extends Wrap
         }
         finally
         {
-            try
-            {
-                raf.close();
-            }
-            catch (IOException e)
-            {
-                throw new AssertionError(e);
-            }
+            FileUtils.closeQuietly(raf);
         }
     }
 


Reply via email to