Explorar o código

Merge -r 641601:641602 from trunk to branch 0.16. Fixes HADOOP-3070.

git-svn-id: https://svn.apache.org/repos/asf/hadoop/core/branches/branch-0.16@641603 13f79535-47bb-0310-9956-ffa450edef68
Owen O'Malley %!s(int64=17) %!d(string=hai) anos
pai
achega
eb6fb2d1ff

+ 3 - 0
CHANGES.txt

@@ -43,6 +43,9 @@ Release 0.16.2 - Unreleased
     making sure the directory is created first. (cdouglas and acmurthy 
     via omalley)
 
+    HADOOP-3070. Protect the trash emptier thread from null pointer
+    exceptions. (Koji Noguchi via omalley)
+
 Release 0.16.1 - 2008-03-13
 
   INCOMPATIBLE CHANGES

+ 29 - 20
src/java/org/apache/hadoop/fs/Trash.java

@@ -26,6 +26,7 @@ import org.apache.commons.logging.*;
 
 import org.apache.hadoop.conf.*;
 import org.apache.hadoop.fs.permission.*;
+import org.apache.hadoop.util.StringUtils;
 
 /** Provides a <i>trash</i> feature.  Files are moved to a user's trash
  * directory, a subdirectory of their home directory named ".Trash".  Files are
@@ -142,6 +143,9 @@ public class Trash extends Configured {
   /** Delete old checkpoints. */
   public void expunge() throws IOException {
     Path[] dirs = fs.listPaths(trash);            // scan trash sub-directories
+    if( dirs == null){
+      return;
+    }
     long now = System.currentTimeMillis();
     for (int i = 0; i < dirs.length; i++) {
       Path path = dirs[i];
@@ -211,31 +215,36 @@ public class Trash extends Configured {
           return;                                 // exit on interrupt
         }
           
-        now = System.currentTimeMillis();
-        if (now >= end) {
-
-          FileStatus[] homes = null;
-          try {
-            homes = fs.listStatus(HOMES);         // list all home dirs
-          } catch (IOException e) {
-            LOG.warn("Trash can't list homes: "+e+" Sleeping.");
-            continue;
-          }
+        try {
+          now = System.currentTimeMillis();
+          if (now >= end) {
 
-          if (homes == null)
-            continue;
-
-          for (FileStatus home : homes) {         // dump each trash
-            if (!home.isDir())
-              continue;
+            FileStatus[] homes = null;
             try {
-              Trash trash = new Trash(home.getPath(), conf);
-              trash.expunge();
-              trash.checkpoint();
+              homes = fs.listStatus(HOMES);         // list all home dirs
             } catch (IOException e) {
-              LOG.warn("Trash caught: "+e+". Skipping "+home.getPath()+".");
+              LOG.warn("Trash can't list homes: "+e+" Sleeping.");
+              continue;
+            }
+
+            if (homes == null)
+              continue;
+
+            for (FileStatus home : homes) {         // dump each trash
+              if (!home.isDir())
+                continue;
+              try {
+                Trash trash = new Trash(home.getPath(), conf);
+                trash.expunge();
+                trash.checkpoint();
+              } catch (IOException e) {
+                LOG.warn("Trash caught: "+e+". Skipping "+home.getPath()+".");
+              } 
             }
           }
+        } catch (Exception e) {
+          LOG.warn("RuntimeException during Trash.Emptier.run() " + 
+                   StringUtils.stringifyException(e));
         }
       }
     }

+ 15 - 0
src/test/org/apache/hadoop/dfs/TestTrash.java

@@ -85,6 +85,21 @@ public class TestTrash extends TestCase {
       Path myFile = new Path("/test/mkdirs/myFile");
       writeFile(fs, myFile);
 
+      // Verify that expunge without Trash directory 
+      // won't throw Exception
+      {
+        String[] args = new String[1];
+        args[0] = "-expunge";
+        int val = -1;
+        try {
+          val = shell.run(args);
+        } catch (Exception e) {
+          System.err.println("Exception raised from Trash.run " +
+                             e.getLocalizedMessage()); 
+        }
+        assertTrue(val == 0);
+      }
+
       // Verify that we succeed in removing the file we created.
       // This should go into Trash.
       {