diff --git a/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/fs/viewfs/ViewFileSystem.java b/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/fs/viewfs/ViewFileSystem.java index ed1bda2d54..8be666cbe8 100644 --- a/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/fs/viewfs/ViewFileSystem.java +++ b/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/fs/viewfs/ViewFileSystem.java @@ -858,6 +858,24 @@ public FsStatus getStatus(Path p) throws IOException { return res.targetFileSystem.getStatus(p); } + /** + * Return the total size of all files under "/", if {@link + * Constants#CONFIG_VIEWFS_LINK_MERGE_SLASH} is supported and is a valid + * mount point. Else, throw NotInMountpointException. + * + * @throws IOException + */ + @Override + public long getUsed() throws IOException { + InodeTree.ResolveResult res = fsState.resolve( + getUriPath(InodeTree.SlashPath), true); + if (res.isInternalDir()) { + throw new NotInMountpointException(InodeTree.SlashPath, "getUsed"); + } else { + return res.targetFileSystem.getUsed(); + } + } + /** * An instance of this class represents an internal dir of the viewFs * that is internal dir of the mount table. diff --git a/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/fs/viewfs/ViewFileSystemBaseTest.java b/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/fs/viewfs/ViewFileSystemBaseTest.java index 06f9868c18..9a0bf020b3 100644 --- a/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/fs/viewfs/ViewFileSystemBaseTest.java +++ b/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/fs/viewfs/ViewFileSystemBaseTest.java @@ -1108,4 +1108,33 @@ public Object run() throws IOException { } }); } + + @Test + public void testUsed() throws IOException { + try { + fsView.getUsed(); + fail("ViewFileSystem getUsed() should fail for slash root path when the" + + " slash root mount point is not configured."); + } catch (NotInMountpointException e) { + // expected exception. + } + long usedSpaceByPathViaViewFs = fsView.getUsed(new Path("/user")); + long usedSpaceByPathViaTargetFs = + fsTarget.getUsed(new Path(targetTestRoot, "user")); + assertEquals("Space used not matching between ViewFileSystem and " + + "the mounted FileSystem!", + usedSpaceByPathViaTargetFs, usedSpaceByPathViaViewFs); + + Path mountDataRootPath = new Path("/data"); + String fsTargetFileName = "debug.log"; + Path fsTargetFilePath = new Path(targetTestRoot, "data/debug.log"); + Path mountDataFilePath = new Path(mountDataRootPath, fsTargetFileName); + fileSystemTestHelper.createFile(fsTarget, fsTargetFilePath); + + usedSpaceByPathViaViewFs = fsView.getUsed(mountDataFilePath); + usedSpaceByPathViaTargetFs = fsTarget.getUsed(fsTargetFilePath); + assertEquals("Space used not matching between ViewFileSystem and " + + "the mounted FileSystem!", + usedSpaceByPathViaTargetFs, usedSpaceByPathViaViewFs); + } }