Kaynağa Gözat

HADOOP-2891. DFSClient.close() closes all open files. (dhruba)


git-svn-id: https://svn.apache.org/repos/asf/hadoop/core/trunk@632174 13f79535-47bb-0310-9956-ffa450edef68
Dhruba Borthakur 17 yıl önce
ebeveyn
işleme
f07b0e0112

+ 2 - 0
CHANGES.txt

@@ -89,6 +89,8 @@ Trunk (unreleased changes)
     HADOOP-2800.  Deprecate SetFile.Writer constructor not the whole class.
     (Johan Oskarsson via tomwhite)
 
+    HADOOP-2891.  DFSClient.close() closes all open files. (dhruba)
+
 Release 0.16.1 - Unreleased
 
   IMPROVEMENTS

+ 1 - 0
src/java/org/apache/hadoop/dfs/ClientProtocol.java

@@ -185,6 +185,7 @@ interface ClientProtocol extends VersionedProtocol {
    * @param src The filename
    * @param holder The datanode holding the lease
    */
+  @Deprecated
   public void abandonFileInProgress(String src, 
                                     String holder) throws IOException;
 

+ 5 - 2
src/java/org/apache/hadoop/dfs/DFSClient.java

@@ -172,10 +172,13 @@ class DFSClient implements FSConstants {
         Iterator file_itr = pendingCreates.keySet().iterator();
         while (file_itr.hasNext()) {
           String name = (String) file_itr.next();
+          OutputStream out = pendingCreates.get(name);
           try {
-            namenode.abandonFileInProgress(name, clientName);
+            if (out != null) {
+              out.close();
+            }
           } catch (IOException ie) {
-            System.err.println("Exception abandoning create lock on " + name);
+            System.err.println("Exception closing file " + name);
             ie.printStackTrace();
           }
         }

+ 1 - 0
src/java/org/apache/hadoop/dfs/NameNode.java

@@ -325,6 +325,7 @@ public class NameNode implements ClientProtocol, DatanodeProtocol,
   }
   /**
    */
+  @Deprecated
   public void abandonFileInProgress(String src, 
                                     String holder) throws IOException {
     stateChangeLog.debug("*DIR* NameNode.abandonFileInProgress:" + src);

+ 46 - 0
src/test/org/apache/hadoop/dfs/TestFileCreation.java

@@ -418,6 +418,52 @@ public class TestFileCreation extends TestCase {
     }
   }
 
+  /**
+   * Test that all open files are closed when client dies abnormally.
+   */
+  public void testDFSClientDeath() throws IOException {
+    Configuration conf = new Configuration();
+    System.out.println("Testing adbornal client death.");
+    if (simulatedStorage) {
+      conf.setBoolean(SimulatedFSDataset.CONFIG_PROPERTY_SIMULATED, true);
+    }
+    MiniDFSCluster cluster = new MiniDFSCluster(conf, 1, true, null);
+    FileSystem fs = cluster.getFileSystem();
+    DistributedFileSystem dfs = (DistributedFileSystem) fs;
+    DFSClient dfsclient = dfs.dfs;
+    try {
+
+      // create a new file in home directory. Do not close it.
+      //
+      Path file1 = new Path("/clienttest.dat");
+      FSDataOutputStream stm = createFile(fs, file1, 1);
+      System.out.println("Created file clienttest.dat");
+
+      // write to file
+      writeFile(stm);
+
+      // close the dfsclient before closing the output stream.
+      // This should close all existing file.
+      dfsclient.close();
+
+      try {
+        fs.close();
+        fs = null;
+      } catch (IOException e) {
+      }
+
+      // reopen file system and verify that file exists.
+      fs = cluster.getFileSystem();
+      assertTrue(file1 + " does not exist.", fs.exists(file1));
+
+    } finally {
+      if (fs != null) {
+        fs.close();
+      }
+      cluster.shutdown();
+    }
+  }
+
 /**
  * Test that file data becomes available before file is closed.
  */