浏览代码

HADOOP-3639. Exception when closing DFSClient while multiple files are open. Contributed by Benjamin Gufler.

git-svn-id: https://svn.apache.org/repos/asf/hadoop/core/trunk@672921 13f79535-47bb-0310-9956-ffa450edef68
Hairong Kuang 17 年之前
父节点
当前提交
3ec80be9c0

+ 3 - 0
CHANGES.txt

@@ -715,6 +715,9 @@ Release 0.18.0 - Unreleased
     HADOOP-3536. Uncaught exception in DataBlockScanner.
     (Tsz Wo (Nicholas), SZE via hairong)
 
+    HADOOP-3539. Exception when closing DFSClient while multiple files are
+    open. (Benjamin Gufler via hairong)
+
 Release 0.17.1 - Unreleased
 
   INCOMPATIBLE CHANGES

+ 8 - 10
src/hdfs/org/apache/hadoop/dfs/DFSClient.java

@@ -207,20 +207,18 @@ class DFSClient implements FSConstants {
     synchronized (this) {
       checkOpen();
       synchronized (pendingCreates) {
-        Iterator file_itr = pendingCreates.keySet().iterator();
-        while (file_itr.hasNext()) {
-          String name = (String) file_itr.next();
-          OutputStream out = pendingCreates.get(name);
-          try {
-            if (out != null) {
+        while (!pendingCreates.isEmpty()) {
+          String name = pendingCreates.firstKey();
+          OutputStream out = pendingCreates.remove(name);
+          if (out != null) {
+            try {
               out.close();
+            } catch (IOException ie) {
+              System.err.println("Exception closing file " + name);
+              ie.printStackTrace();
             }
-          } catch (IOException ie) {
-            System.err.println("Exception closing file " + name);
-            ie.printStackTrace();
           }
         }
-        pendingCreates.clear();
       }
       this.clientRunning = false;
       try {

+ 23 - 0
src/test/org/apache/hadoop/dfs/TestDistributedFileSystem.java

@@ -22,6 +22,7 @@ import java.net.URI;
 
 import org.apache.hadoop.conf.Configuration;
 import org.apache.hadoop.fs.FileSystem;
+import org.apache.hadoop.fs.Path;
 
 public class TestDistributedFileSystem extends junit.framework.TestCase {
   public void testFileSystemCloseAll() throws Exception {
@@ -42,4 +43,26 @@ public class TestDistributedFileSystem extends junit.framework.TestCase {
       if (cluster != null) {cluster.shutdown();}
     }
   }
+  
+  /**
+   * Tests DFSClient.close throws no ConcurrentModificationException if 
+   * multiple files are open.
+   */
+  public void testDFSClose() throws Exception {
+    Configuration conf = new Configuration();
+    MiniDFSCluster cluster = new MiniDFSCluster(conf, 2, true, null);
+    FileSystem fileSys = cluster.getFileSystem();
+
+    try {
+      // create two files
+      fileSys.create(new Path("/test/dfsclose/file-0"));
+      fileSys.create(new Path("/test/dfsclose/file-1"));
+
+      fileSys.close();
+    }
+    finally {
+      if (cluster != null) {cluster.shutdown();}
+    }
+  }
+
 }