瀏覽代碼

HDFS-17261. RBF: Fix getFileInfo return wrong path when get mountTable path which is multi-level (#6288). Contributed by liuguanghua.

Reviewed-by: Inigo Goiri <inigoiri@apache.org>
Signed-off-by: Ayush Saxena <ayushsaxena@apache.org>
LiuGuH 1 年之前
父節點
當前提交
071f850841

+ 25 - 7
hadoop-hdfs-project/hadoop-hdfs-rbf/src/main/java/org/apache/hadoop/hdfs/server/federation/router/RouterClientProtocol.java

@@ -993,10 +993,10 @@ public class RouterClientProtocol implements ClientProtocol {
         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 @@ public class RouterClientProtocol implements ClientProtocol {
   @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 @@ public class RouterClientProtocol implements ClientProtocol {
       }
     }
     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)

+ 23 - 0
hadoop-hdfs-project/hadoop-hdfs-rbf/src/test/java/org/apache/hadoop/hdfs/server/federation/router/TestRouterMountTable.java

@@ -372,6 +372,10 @@ public class TestRouterMountTable {
     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 class TestRouterMountTable {
     }
   }
 
+  @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