hadoop-common-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From sra...@apache.org
Subject svn commit: r1352390 - in /hadoop/common/branches/branch-1-win: CHANGES.txt src/core/org/apache/hadoop/io/SequenceFile.java src/test/org/apache/hadoop/io/TestSequenceFile.java
Date Thu, 21 Jun 2012 01:44:23 GMT
Author: sradia
Date: Thu Jun 21 01:44:22 2012
New Revision: 1352390

URL: http://svn.apache.org/viewvc?rev=1352390&view=rev
Log:
HADOOP-8486 Resource leak ... SequenceFile  (Kanna Karanam via Sanjay)

Modified:
    hadoop/common/branches/branch-1-win/CHANGES.txt
    hadoop/common/branches/branch-1-win/src/core/org/apache/hadoop/io/SequenceFile.java
    hadoop/common/branches/branch-1-win/src/test/org/apache/hadoop/io/TestSequenceFile.java

Modified: hadoop/common/branches/branch-1-win/CHANGES.txt
URL: http://svn.apache.org/viewvc/hadoop/common/branches/branch-1-win/CHANGES.txt?rev=1352390&r1=1352389&r2=1352390&view=diff
==============================================================================
--- hadoop/common/branches/branch-1-win/CHANGES.txt (original)
+++ hadoop/common/branches/branch-1-win/CHANGES.txt Thu Jun 21 01:44:22 2012
@@ -42,6 +42,8 @@ branch-hadoop-1-win - unreleased
     MAPREDUCE-4203. Added an implementation of the process tree for Windows.
     (Bikas Saha via acmurthy) 
 
+    HADOOP-8486 Resource leak ... SequenceFile  (Kanna Karanam via Sanjay)
+
 Release 1.1.0 - unreleased
 
   NEW FEATURES

Modified: hadoop/common/branches/branch-1-win/src/core/org/apache/hadoop/io/SequenceFile.java
URL: http://svn.apache.org/viewvc/hadoop/common/branches/branch-1-win/src/core/org/apache/hadoop/io/SequenceFile.java?rev=1352390&r1=1352389&r2=1352390&view=diff
==============================================================================
--- hadoop/common/branches/branch-1-win/src/core/org/apache/hadoop/io/SequenceFile.java (original)
+++ hadoop/common/branches/branch-1-win/src/core/org/apache/hadoop/io/SequenceFile.java Thu
Jun 21 01:44:22 2012
@@ -1414,7 +1414,7 @@ public class SequenceFile {
   /** Reads key/value pairs from a sequence-format file. */
   public static class Reader implements java.io.Closeable {
     private Path file;
-    private FSDataInputStream in;
+    private FSDataInputStream in = null;
     private DataOutputBuffer outBuf = new DataOutputBuffer();
 
     private byte version;
@@ -1465,8 +1465,8 @@ public class SequenceFile {
     private DataInputStream valIn = null;
     private Decompressor valDecompressor = null;
     
-    private Deserializer keyDeserializer;
-    private Deserializer valDeserializer;
+    private Deserializer keyDeserializer = null;
+    private Deserializer valDeserializer = null;
 
     /** Open the named file. */
     public Reader(FileSystem fs, Path file, Configuration conf)
@@ -1482,12 +1482,19 @@ public class SequenceFile {
     private Reader(FileSystem fs, Path file, int bufferSize, long start,
                    long length, Configuration conf, boolean tempReader) 
     throws IOException {
-      this.file = file;
-      this.in = openFile(fs, file, bufferSize, length);
-      this.conf = conf;
-      seek(start);
-      this.end = in.getPos() + length;
-      init(tempReader);
+      try {
+        this.file = file;
+        this.in = openFile(fs, file, bufferSize, length);
+        this.conf = conf;
+        seek(start);
+        this.end = in.getPos() + length;
+        init(tempReader);
+      } catch(IOException e) {
+        // Close if there are any open resources before throwing exceptions
+        // from constructor.
+        close();
+        throw e;
+      }
     }
 
     /**
@@ -1636,14 +1643,19 @@ public class SequenceFile {
       valLenDecompressor = valDecompressor = null;
       
       if (keyDeserializer != null) {
-    	keyDeserializer.close();
+        keyDeserializer.close();
+        keyDeserializer = null;
       }
       if (valDeserializer != null) {
         valDeserializer.close();
+        valDeserializer = null;
       }
       
       // Close the input-stream
-      in.close();
+      if(in != null) {
+        in.close();
+        in = null;
+      }
     }
 
     /** Returns the name of the key class. */

Modified: hadoop/common/branches/branch-1-win/src/test/org/apache/hadoop/io/TestSequenceFile.java
URL: http://svn.apache.org/viewvc/hadoop/common/branches/branch-1-win/src/test/org/apache/hadoop/io/TestSequenceFile.java?rev=1352390&r1=1352389&r2=1352390&view=diff
==============================================================================
--- hadoop/common/branches/branch-1-win/src/test/org/apache/hadoop/io/TestSequenceFile.java
(original)
+++ hadoop/common/branches/branch-1-win/src/test/org/apache/hadoop/io/TestSequenceFile.java
Thu Jun 21 01:44:22 2012
@@ -47,7 +47,34 @@ public class TestSequenceFile extends Te
     compressedSeqFileTest(new DefaultCodec());
     LOG.info("Successfully tested SequenceFile with DefaultCodec");
   }
-  
+
+  /* Test to ensure that file handle is closed properly when the 
+   * SequenceFile.Reader throws an exception from the constructor.*/
+  public void testReadNonSequenceFile() throws Exception {
+    LOG.info("Testing SequenceFile Reader with non-sequence file");
+    Path nonSeqFile =
+        new Path(System.getProperty("test.build.data",".")+"/nonSequenceFile.seq");
+    SequenceFile.Reader reader = null;
+    FileSystem fs = FileSystem.getLocal(conf);
+    File f = new File(nonSeqFile.getUriPath());
+    if(!f.exists()) {
+      fs.createNewFile(nonSeqFile);
+    }
+    try {
+      reader = new SequenceFile.Reader(fs, nonSeqFile, conf);
+      // expecting an exception for a invalid sequence file format
+      fail("Expecting an IOException while reading the data from non-sequence file");
+    } catch (IOException e) {
+      // Ensure that we are able to delete the file in Windows.
+      // Note: Didn't find a way to detect the file handle leaks on UNIX
+      // so it always passes on UNIX
+      assertEquals(true, fs.delete(nonSeqFile, true));
+    } finally {
+      IOUtils.closeStream(reader);
+    }
+    LOG.info("Successfully tested SequenceFile Reader with non-sequence file");
+  }
+
   public void compressedSeqFileTest(CompressionCodec codec) throws Exception {
     int count = 1024 * 10;
     int megabytes = 1;



Mime
View raw message