HADOOP-12154. FileSystem#getUsed() returns the file length only from root '/' (Contributed by J.Andreina)

This commit is contained in:
Vinayakumar B 2015-06-30 15:25:20 +05:30
parent 77588e1d32
commit 6d99017f38
3 changed files with 32 additions and 3 deletions

View File

@ -899,6 +899,9 @@ Release 2.8.0 - UNRELEASED
HADOOP-12089. StorageException complaining " no lease ID" when updating HADOOP-12089. StorageException complaining " no lease ID" when updating
FolderLastModifiedTime in WASB. (Duo Xu via cnauroth) FolderLastModifiedTime in WASB. (Duo Xu via cnauroth)
HADOOP-12154. FileSystem#getUsed() returns the file length only from root '/'
(J.Andreina via vinayakumarb)
Release 2.7.2 - UNRELEASED Release 2.7.2 - UNRELEASED
INCOMPATIBLE CHANGES INCOMPATIBLE CHANGES

View File

@ -2085,9 +2085,9 @@ public abstract class FileSystem extends Configured implements Closeable {
/** Return the total size of all files in the filesystem.*/ /** Return the total size of all files in the filesystem.*/
public long getUsed() throws IOException{ public long getUsed() throws IOException{
long used = 0; long used = 0;
FileStatus[] files = listStatus(new Path("/")); RemoteIterator<LocatedFileStatus> files = listFiles(new Path("/"), true);
for(FileStatus file:files){ while (files.hasNext()) {
used += file.getLen(); used += files.next().getLen();
} }
return used; return used;
} }

View File

@ -913,6 +913,32 @@ public class TestDFSShell {
cluster.shutdown(); cluster.shutdown();
} }
} }
@Test(timeout = 30000)
public void testTotalSizeOfAllFiles() throws Exception {
Configuration conf = new HdfsConfiguration();
MiniDFSCluster cluster = null;
try {
cluster = new MiniDFSCluster.Builder(conf).numDataNodes(1).build();
FileSystem fs = cluster.getFileSystem();
// create file under root
FSDataOutputStream File1 = fs.create(new Path("/File1"));
File1.write("hi".getBytes());
File1.close();
// create file under sub-folder
FSDataOutputStream File2 = fs.create(new Path("/Folder1/File2"));
File2.write("hi".getBytes());
File2.close();
// getUsed() should return total length of all the files in Filesystem
assertEquals(4, fs.getUsed());
} finally {
if (cluster != null) {
cluster.shutdown();
cluster = null;
}
}
}
private static void runCount(String path, long dirs, long files, FsShell shell private static void runCount(String path, long dirs, long files, FsShell shell
) throws IOException { ) throws IOException {
ByteArrayOutputStream bytes = new ByteArrayOutputStream(); ByteArrayOutputStream bytes = new ByteArrayOutputStream();