Skip to content

Commit 6d99017

Browse files
committed
HADOOP-12154. FileSystem#getUsed() returns the file length only from root '/' (Contributed by J.Andreina)
1 parent 77588e1 commit 6d99017

File tree

3 files changed

+32
-3
lines changed

3 files changed

+32
-3
lines changed

hadoop-common-project/hadoop-common/CHANGES.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -899,6 +899,9 @@ Release 2.8.0 - UNRELEASED
899899
HADOOP-12089. StorageException complaining " no lease ID" when updating
900900
FolderLastModifiedTime in WASB. (Duo Xu via cnauroth)
901901

902+
HADOOP-12154. FileSystem#getUsed() returns the file length only from root '/'
903+
(J.Andreina via vinayakumarb)
904+
902905
Release 2.7.2 - UNRELEASED
903906

904907
INCOMPATIBLE CHANGES

hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/fs/FileSystem.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2085,9 +2085,9 @@ public void close() throws IOException {
20852085
/** Return the total size of all files in the filesystem.*/
20862086
public long getUsed() throws IOException{
20872087
long used = 0;
2088-
FileStatus[] files = listStatus(new Path("/"));
2089-
for(FileStatus file:files){
2090-
used += file.getLen();
2088+
RemoteIterator<LocatedFileStatus> files = listFiles(new Path("/"), true);
2089+
while (files.hasNext()) {
2090+
used += files.next().getLen();
20912091
}
20922092
return used;
20932093
}

hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/hdfs/TestDFSShell.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -913,6 +913,32 @@ public void testCount() throws Exception {
913913
cluster.shutdown();
914914
}
915915
}
916+
917+
@Test(timeout = 30000)
918+
public void testTotalSizeOfAllFiles() throws Exception {
919+
Configuration conf = new HdfsConfiguration();
920+
MiniDFSCluster cluster = null;
921+
try {
922+
cluster = new MiniDFSCluster.Builder(conf).numDataNodes(1).build();
923+
FileSystem fs = cluster.getFileSystem();
924+
// create file under root
925+
FSDataOutputStream File1 = fs.create(new Path("/File1"));
926+
File1.write("hi".getBytes());
927+
File1.close();
928+
// create file under sub-folder
929+
FSDataOutputStream File2 = fs.create(new Path("/Folder1/File2"));
930+
File2.write("hi".getBytes());
931+
File2.close();
932+
// getUsed() should return total length of all the files in Filesystem
933+
assertEquals(4, fs.getUsed());
934+
} finally {
935+
if (cluster != null) {
936+
cluster.shutdown();
937+
cluster = null;
938+
}
939+
}
940+
}
941+
916942
private static void runCount(String path, long dirs, long files, FsShell shell
917943
) throws IOException {
918944
ByteArrayOutputStream bytes = new ByteArrayOutputStream();

0 commit comments

Comments
 (0)