diff --git a/hadoop-hdfs-project/hadoop-hdfs/CHANGES.txt b/hadoop-hdfs-project/hadoop-hdfs/CHANGES.txt index 6d0097fdb5..efff10a697 100644 --- a/hadoop-hdfs-project/hadoop-hdfs/CHANGES.txt +++ b/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 (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 70c891258a..bab83a132f 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 @@ private List> getFileStatusList(String path) { 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 @@ private long getINodeId(String strPath) { 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 6c2f8d6066..32efe3431b 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 static void createOriginalFSImage() throws IOException { } } + // 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 void testFileDistributionCalculator() throws IOException { 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 void testWebImageViewer() throws IOException, InterruptedException, // 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 void testWebImageViewer() throws IOException, InterruptedException, 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");