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>
This commit is contained in:
LiuGuH 2023-12-01 19:35:58 +08:00 committed by GitHub
parent 89e78a76a0
commit 071f850841
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 48 additions and 7 deletions

View File

@ -993,10 +993,10 @@ public HdfsFileStatus getFileInfo(String src) throws IOException {
if (dates != null && dates.containsKey(src)) { if (dates != null && dates.containsKey(src)) {
date = dates.get(src); date = dates.get(src);
} }
ret = getMountPointStatus(src, children.size(), date); ret = getMountPointStatus(src, children.size(), date, false);
} else if (children != null) { } else if (children != null) {
// The src is a mount point, but there are no files or directories // 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 @VisibleForTesting
HdfsFileStatus getMountPointStatus( HdfsFileStatus getMountPointStatus(
String name, int childrenNum, long date) { 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 modTime = date;
long accessTime = date; long accessTime = date;
FsPermission permission = FsPermission.getDirDefault(); FsPermission permission = FsPermission.getDirDefault();
@ -2242,17 +2257,20 @@ HdfsFileStatus getMountPointStatus(
} }
} }
long inodeId = 0; long inodeId = 0;
HdfsFileStatus.Builder builder = new HdfsFileStatus.Builder();
if (setPath) {
Path path = new Path(name); Path path = new Path(name);
String nameStr = path.getName(); String nameStr = path.getName();
return new HdfsFileStatus.Builder() builder.path(DFSUtil.string2Bytes(nameStr));
.isdir(true) }
return builder.isdir(true)
.mtime(modTime) .mtime(modTime)
.atime(accessTime) .atime(accessTime)
.perm(permission) .perm(permission)
.owner(owner) .owner(owner)
.group(group) .group(group)
.symlink(new byte[0]) .symlink(new byte[0])
.path(DFSUtil.string2Bytes(nameStr))
.fileId(inodeId) .fileId(inodeId)
.children(childrenNum) .children(childrenNum)
.flags(flags) .flags(flags)

View File

@ -372,6 +372,10 @@ public void testGetMountPointStatus() throws IOException {
HdfsFileStatus dirStatus2 = HdfsFileStatus dirStatus2 =
clientProtocol.getMountPointStatus(childPath2.toString(), 0, 0); clientProtocol.getMountPointStatus(childPath2.toString(), 0, 0);
assertEquals(child2, dirStatus2.getLocalName()); assertEquals(child2, dirStatus2.getLocalName());
HdfsFileStatus dirStatus3 =
clientProtocol.getMountPointStatus(childPath2.toString(), 0, 0, false);
assertTrue(dirStatus3.isEmptyLocalName());
} }
/** /**
* GetListing of testPath through router. * 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. * Regression test for HDFS-13255.
* Verify that delete fails if the path is a mount point or * Verify that delete fails if the path is a mount point or