diff --git a/hadoop-hdfs-project/hadoop-hdfs/CHANGES.HDFS-2802.txt b/hadoop-hdfs-project/hadoop-hdfs/CHANGES.HDFS-2802.txt index e599bc8ca8..2346ca3ba0 100644 --- a/hadoop-hdfs-project/hadoop-hdfs/CHANGES.HDFS-2802.txt +++ b/hadoop-hdfs-project/hadoop-hdfs/CHANGES.HDFS-2802.txt @@ -341,3 +341,6 @@ Branch-2802 Snapshot (Unreleased) HDFS-4800. Fix INodeDirectoryWithSnapshot#cleanDeletedINode. (Jing Zhao via szetszwo) + + HDFS-4801. lsSnapshottableDir throws IllegalArgumentException when root is + snapshottable. (Jing Zhao via szetszwo) diff --git a/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/protocol/SnapshottableDirectoryStatus.java b/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/protocol/SnapshottableDirectoryStatus.java index c1f9388eef..2300fc31d9 100644 --- a/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/protocol/SnapshottableDirectoryStatus.java +++ b/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/protocol/SnapshottableDirectoryStatus.java @@ -98,10 +98,17 @@ public HdfsFileStatus getDirStatus() { * @return Full path of the file */ public Path getFullPath() { - String parentFullPathStr = (parentFullPath == null || parentFullPath.length == 0) ? null - : DFSUtil.bytes2String(parentFullPath); - return parentFullPathStr == null ? new Path(dirStatus.getLocalName()) - : new Path(parentFullPathStr, dirStatus.getLocalName()); + String parentFullPathStr = + (parentFullPath == null || parentFullPath.length == 0) ? + null : DFSUtil.bytes2String(parentFullPath); + if (parentFullPathStr == null + && dirStatus.getLocalNameInBytes().length == 0) { + // root + return new Path("/"); + } else { + return parentFullPathStr == null ? new Path(dirStatus.getLocalName()) + : new Path(parentFullPathStr, dirStatus.getLocalName()); + } } /** diff --git a/hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/hdfs/server/namenode/snapshot/TestSnapshottableDirListing.java b/hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/hdfs/server/namenode/snapshot/TestSnapshottableDirListing.java index 66728b9ca6..8988806074 100644 --- a/hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/hdfs/server/namenode/snapshot/TestSnapshottableDirListing.java +++ b/hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/hdfs/server/namenode/snapshot/TestSnapshottableDirListing.java @@ -81,6 +81,19 @@ public void testListSnapshottableDir() throws Exception { SnapshottableDirectoryStatus[] dirs = hdfs.getSnapshottableDirListing(); assertNull(dirs); + // Make root as snapshottable + final Path root = new Path("/"); + hdfs.allowSnapshot(root); + dirs = hdfs.getSnapshottableDirListing(); + assertEquals(1, dirs.length); + assertEquals("", dirs[0].getDirStatus().getLocalName()); + assertEquals(root, dirs[0].getFullPath()); + + // Make root non-snaphsottable + hdfs.disallowSnapshot(root); + dirs = hdfs.getSnapshottableDirListing(); + assertNull(dirs); + // Make dir1 as snapshottable hdfs.allowSnapshot(dir1); dirs = hdfs.getSnapshottableDirListing();