From 03b04fbe2834cf6c668d934bcd614639ef905071 Mon Sep 17 00:00:00 2001 From: Haohui Mai Date: Fri, 9 May 2014 18:45:19 +0000 Subject: [PATCH] HDFS-6240. Merge r1593591 from trunk. git-svn-id: https://svn.apache.org/repos/asf/hadoop/common/branches/branch-2@1593593 13f79535-47bb-0310-9956-ffa450edef68 --- hadoop-hdfs-project/hadoop-hdfs/CHANGES.txt | 3 +++ .../tools/offlineImageViewer/FSImageLoader.java | 7 ++++++- .../TestOfflineImageViewer.java | 15 ++++++++++++--- 3 files changed, 21 insertions(+), 4 deletions(-) diff --git a/hadoop-hdfs-project/hadoop-hdfs/CHANGES.txt b/hadoop-hdfs-project/hadoop-hdfs/CHANGES.txt index 176a8472ce7..ae05bd40e25 100644 --- a/hadoop-hdfs-project/hadoop-hdfs/CHANGES.txt +++ b/hadoop-hdfs-project/hadoop-hdfs/CHANGES.txt @@ -186,6 +186,9 @@ Release 2.5.0 - UNRELEASED HDFS-5381. ExtendedBlock#hashCode should use both blockId and block pool ID (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 INCOMPATIBLE CHANGES diff --git a/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/tools/offlineImageViewer/FSImageLoader.java b/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/tools/offlineImageViewer/FSImageLoader.java index 70c891258a5..bab83a132f3 100644 --- a/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/tools/offlineImageViewer/FSImageLoader.java +++ b/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); FsImageProto.INodeSection.INode inode = inodes.get(id); 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); for (long cid : children) { list.add(getFileStatus(inodes.get(cid), true)); @@ -416,7 +420,8 @@ class FSImageLoader { map.put("replication", 0); map.put("type", inode.getType()); 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; } case SYMLINK: { diff --git a/hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/hdfs/tools/offlineImageViewer/TestOfflineImageViewer.java b/hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/hdfs/tools/offlineImageViewer/TestOfflineImageViewer.java index 6c2f8d60663..32efe3431b7 100644 --- a/hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/hdfs/tools/offlineImageViewer/TestOfflineImageViewer.java +++ b/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 Token[] delegationTokens = hdfs .addDelegationTokens(TEST_RENEWER, null); @@ -205,8 +210,8 @@ public class TestOfflineImageViewer { matcher = p.matcher(output.getBuffer()); assertTrue(matcher.find() && matcher.groupCount() == 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(), new Comparator() { @@ -259,7 +264,7 @@ public class TestOfflineImageViewer { // verify the number of directories 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 statuses = webhdfs.listStatus(new Path("/dir0")); @@ -270,6 +275,10 @@ public class TestOfflineImageViewer { FileStatus expected = writtenFiles.get("/dir0/file0"); 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 URL url = new URL("http://localhost:" + port + "/webhdfs/v1/invalid/?op=LISTSTATUS");