瀏覽代碼

HDFS-6240. WebImageViewer returns 404 if LISTSTATUS to an empty directory. Contributed by Akira Ajisaka.

git-svn-id: https://svn.apache.org/repos/asf/hadoop/common/trunk@1593591 13f79535-47bb-0310-9956-ffa450edef68
Haohui Mai 11 年之前
父節點
當前提交
ed6f32c540

+ 3 - 0
hadoop-hdfs-project/hadoop-hdfs/CHANGES.txt

@@ -438,6 +438,9 @@ Release 2.5.0 - UNRELEASED
     HDFS-5381. ExtendedBlock#hashCode should use both blockId and block pool ID    
     HDFS-5381. ExtendedBlock#hashCode should use both blockId and block pool ID    
     (Benoy Antony via Colin Patrick McCabe)
     (Benoy Antony via Colin Patrick McCabe)
 
 
+    HDFS-6240. WebImageViewer returns 404 if LISTSTATUS to an empty directory.
+    (Akira Ajisaka via wheat9)
+
 Release 2.4.1 - UNRELEASED
 Release 2.4.1 - UNRELEASED
 
 
   INCOMPATIBLE CHANGES
   INCOMPATIBLE CHANGES

+ 6 - 1
hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/tools/offlineImageViewer/FSImageLoader.java

@@ -261,6 +261,10 @@ class FSImageLoader {
     long id = getINodeId(path);
     long id = getINodeId(path);
     FsImageProto.INodeSection.INode inode = inodes.get(id);
     FsImageProto.INodeSection.INode inode = inodes.get(id);
     if (inode.getType() == FsImageProto.INodeSection.INode.Type.DIRECTORY) {
     if (inode.getType() == FsImageProto.INodeSection.INode.Type.DIRECTORY) {
+      if (!dirmap.containsKey(id)) {
+        // if the directory is empty, return empty list
+        return list;
+      }
       long[] children = dirmap.get(id);
       long[] children = dirmap.get(id);
       for (long cid : children) {
       for (long cid : children) {
         list.add(getFileStatus(inodes.get(cid), true));
         list.add(getFileStatus(inodes.get(cid), true));
@@ -416,7 +420,8 @@ class FSImageLoader {
         map.put("replication", 0);
         map.put("replication", 0);
         map.put("type", inode.getType());
         map.put("type", inode.getType());
         map.put("fileId", inode.getId());
         map.put("fileId", inode.getId());
-        map.put("childrenNum", dirmap.get(inode.getId()).length);
+        map.put("childrenNum", dirmap.containsKey(inode.getId()) ?
+            dirmap.get(inode.getId()).length : 0);
         return map;
         return map;
       }
       }
       case SYMLINK: {
       case SYMLINK: {

+ 12 - 3
hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/hdfs/tools/offlineImageViewer/TestOfflineImageViewer.java

@@ -120,6 +120,11 @@ public class TestOfflineImageViewer {
         }
         }
       }
       }
 
 
+      // Create an empty directory
+      Path emptydir = new Path("/emptydir");
+      hdfs.mkdirs(emptydir);
+      writtenFiles.put(emptydir.toString(), hdfs.getFileStatus(emptydir));
+
       // Get delegation tokens so we log the delegation token op
       // Get delegation tokens so we log the delegation token op
       Token<?>[] delegationTokens = hdfs
       Token<?>[] delegationTokens = hdfs
           .addDelegationTokens(TEST_RENEWER, null);
           .addDelegationTokens(TEST_RENEWER, null);
@@ -205,8 +210,8 @@ public class TestOfflineImageViewer {
     matcher = p.matcher(output.getBuffer());
     matcher = p.matcher(output.getBuffer());
     assertTrue(matcher.find() && matcher.groupCount() == 1);
     assertTrue(matcher.find() && matcher.groupCount() == 1);
     int totalDirs = Integer.parseInt(matcher.group(1));
     int totalDirs = Integer.parseInt(matcher.group(1));
-    // totalDirs includes root directory
-    assertEquals(NUM_DIRS + 1, totalDirs);
+    // totalDirs includes root directory and empty directory
+    assertEquals(NUM_DIRS + 2, totalDirs);
 
 
     FileStatus maxFile = Collections.max(writtenFiles.values(),
     FileStatus maxFile = Collections.max(writtenFiles.values(),
         new Comparator<FileStatus>() {
         new Comparator<FileStatus>() {
@@ -259,7 +264,7 @@ public class TestOfflineImageViewer {
 
 
       // verify the number of directories
       // verify the number of directories
       FileStatus[] statuses = webhdfs.listStatus(new Path("/"));
       FileStatus[] statuses = webhdfs.listStatus(new Path("/"));
-      assertEquals(NUM_DIRS, statuses.length);
+      assertEquals(NUM_DIRS + 1, statuses.length); // contains empty directory
 
 
       // verify the number of files in the directory
       // verify the number of files in the directory
       statuses = webhdfs.listStatus(new Path("/dir0"));
       statuses = webhdfs.listStatus(new Path("/dir0"));
@@ -270,6 +275,10 @@ public class TestOfflineImageViewer {
       FileStatus expected = writtenFiles.get("/dir0/file0");
       FileStatus expected = writtenFiles.get("/dir0/file0");
       compareFile(expected, status);
       compareFile(expected, status);
 
 
+      // LISTSTATUS operation to an empty directory
+      statuses = webhdfs.listStatus(new Path("/emptydir"));
+      assertEquals(0, statuses.length);
+
       // LISTSTATUS operation to a invalid path
       // LISTSTATUS operation to a invalid path
       URL url = new URL("http://localhost:" + port +
       URL url = new URL("http://localhost:" + port +
                     "/webhdfs/v1/invalid/?op=LISTSTATUS");
                     "/webhdfs/v1/invalid/?op=LISTSTATUS");