From 071f850841a6fcf21f8ea0073dc02866f6cbea13 Mon Sep 17 00:00:00 2001 From: LiuGuH <444506464@qq.com> Date: Fri, 1 Dec 2023 19:35:58 +0800 Subject: [PATCH] HDFS-17261. RBF: Fix getFileInfo return wrong path when get mountTable path which is multi-level (#6288). Contributed by liuguanghua. Reviewed-by: Inigo Goiri Signed-off-by: Ayush Saxena --- .../router/RouterClientProtocol.java | 32 +++++++++++++++---- .../router/TestRouterMountTable.java | 23 +++++++++++++ 2 files changed, 48 insertions(+), 7 deletions(-) diff --git a/hadoop-hdfs-project/hadoop-hdfs-rbf/src/main/java/org/apache/hadoop/hdfs/server/federation/router/RouterClientProtocol.java b/hadoop-hdfs-project/hadoop-hdfs-rbf/src/main/java/org/apache/hadoop/hdfs/server/federation/router/RouterClientProtocol.java index 9d8994eb08..a85c032047 100644 --- a/hadoop-hdfs-project/hadoop-hdfs-rbf/src/main/java/org/apache/hadoop/hdfs/server/federation/router/RouterClientProtocol.java +++ b/hadoop-hdfs-project/hadoop-hdfs-rbf/src/main/java/org/apache/hadoop/hdfs/server/federation/router/RouterClientProtocol.java @@ -993,10 +993,10 @@ public HdfsFileStatus getFileInfo(String src) throws IOException { if (dates != null && dates.containsKey(src)) { date = dates.get(src); } - ret = getMountPointStatus(src, children.size(), date); + ret = getMountPointStatus(src, children.size(), date, false); } else if (children != null) { // The src is a mount point, but there are no files or directories - ret = getMountPointStatus(src, 0, 0); + ret = getMountPointStatus(src, 0, 0, false); } } @@ -2193,6 +2193,21 @@ private static FsPermission getParentPermission(final FsPermission mask) { @VisibleForTesting HdfsFileStatus getMountPointStatus( String name, int childrenNum, long date) { + return getMountPointStatus(name, childrenNum, date, true); + } + + /** + * Create a new file status for a mount point. + * + * @param name Name of the mount point. + * @param childrenNum Number of children. + * @param date Map with the dates. + * @param setPath if true should set path in HdfsFileStatus + * @return New HDFS file status representing a mount point. + */ + @VisibleForTesting + HdfsFileStatus getMountPointStatus( + String name, int childrenNum, long date, boolean setPath) { long modTime = date; long accessTime = date; FsPermission permission = FsPermission.getDirDefault(); @@ -2242,17 +2257,20 @@ HdfsFileStatus getMountPointStatus( } } long inodeId = 0; - Path path = new Path(name); - String nameStr = path.getName(); - return new HdfsFileStatus.Builder() - .isdir(true) + HdfsFileStatus.Builder builder = new HdfsFileStatus.Builder(); + if (setPath) { + Path path = new Path(name); + String nameStr = path.getName(); + builder.path(DFSUtil.string2Bytes(nameStr)); + } + + return builder.isdir(true) .mtime(modTime) .atime(accessTime) .perm(permission) .owner(owner) .group(group) .symlink(new byte[0]) - .path(DFSUtil.string2Bytes(nameStr)) .fileId(inodeId) .children(childrenNum) .flags(flags) diff --git a/hadoop-hdfs-project/hadoop-hdfs-rbf/src/test/java/org/apache/hadoop/hdfs/server/federation/router/TestRouterMountTable.java b/hadoop-hdfs-project/hadoop-hdfs-rbf/src/test/java/org/apache/hadoop/hdfs/server/federation/router/TestRouterMountTable.java index 8812f83feb..a1a2658731 100644 --- a/hadoop-hdfs-project/hadoop-hdfs-rbf/src/test/java/org/apache/hadoop/hdfs/server/federation/router/TestRouterMountTable.java +++ b/hadoop-hdfs-project/hadoop-hdfs-rbf/src/test/java/org/apache/hadoop/hdfs/server/federation/router/TestRouterMountTable.java @@ -372,6 +372,10 @@ public void testGetMountPointStatus() throws IOException { HdfsFileStatus dirStatus2 = clientProtocol.getMountPointStatus(childPath2.toString(), 0, 0); assertEquals(child2, dirStatus2.getLocalName()); + + HdfsFileStatus dirStatus3 = + clientProtocol.getMountPointStatus(childPath2.toString(), 0, 0, false); + assertTrue(dirStatus3.isEmptyLocalName()); } /** * GetListing of testPath through router. @@ -666,6 +670,25 @@ public void testGetListingWithTrailingSlash() throws IOException { } } + @Test + public void testGetFileInfoWithMountPoint() throws IOException { + try { + // Add mount table entry + MountTable addEntry = MountTable.newInstance("/testgetfileinfo/ns1/dir", + Collections.singletonMap("ns1", "/testgetfileinfo/ns1/dir")); + assertTrue(addMountTable(addEntry)); + nnFs1.mkdirs(new Path("/testgetfileinfo/ns1/dir")); + + FileStatus fileStatus = routerFs.getFileStatus(new Path("/testgetfileinfo/ns1")); + assertEquals(fileStatus.getPath().toUri().getPath(), "/testgetfileinfo/ns1"); + + fileStatus = routerFs.getFileStatus(new Path("/testgetfileinfo/ns1/dir")); + assertEquals(fileStatus.getPath().toUri().getPath(), "/testgetfileinfo/ns1/dir"); + } finally { + nnFs1.delete(new Path("/testgetfileinfo/ns1/dir"), true); + } + } + /** * Regression test for HDFS-13255. * Verify that delete fails if the path is a mount point or