Skip to content

Commit bf45f3b

Browse files
HDFS-14957. INodeReference Space Consumed was not same in QuotaUsage and ContentSummary. Contributed by hemanthboyina.
1 parent 8fe01db commit bf45f3b

File tree

2 files changed

+65
-3
lines changed

2 files changed

+65
-3
lines changed

hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/namenode/INodeFile.java

+40-2
Original file line numberDiff line numberDiff line change
@@ -873,8 +873,18 @@ public final ContentSummaryComputationContext computeContentSummary(
873873
counts.addContent(Content.FILE, 1);
874874
final long fileLen = computeFileSize(snapshotId);
875875
counts.addContent(Content.LENGTH, fileLen);
876-
counts.addContent(Content.DISKSPACE, storagespaceConsumed(null)
877-
.getStorageSpace());
876+
877+
FileWithSnapshotFeature sf = getFileWithSnapshotFeature();
878+
if (sf == null) {
879+
counts.addContent(Content.DISKSPACE,
880+
storagespaceConsumed(null).getStorageSpace());
881+
} else if (isStriped()) {
882+
counts.addContent(Content.DISKSPACE,
883+
storagespaceConsumedStriped().getStorageSpace());
884+
} else {
885+
long diskSpaceQuota = getDiskSpaceQuota(counts, sf, snapshotId);
886+
counts.addContent(Content.DISKSPACE, diskSpaceQuota);
887+
}
878888

879889
if (getStoragePolicyID() != BLOCK_STORAGE_POLICY_ID_UNSPECIFIED){
880890
BlockStoragePolicy bsp = summary.getBlockStoragePolicySuite().
@@ -890,6 +900,34 @@ public final ContentSummaryComputationContext computeContentSummary(
890900
return summary;
891901
}
892902

903+
/**
904+
* Compute disk space consumed by all the blocks in snapshots.
905+
*/
906+
private long getDiskSpaceQuota(ContentCounts counts,
907+
FileWithSnapshotFeature sf, int lastSnapshotId) {
908+
FileDiffList fileDiffList = sf.getDiffs();
909+
int last = fileDiffList.getLastSnapshotId();
910+
911+
if (lastSnapshotId == Snapshot.CURRENT_STATE_ID
912+
|| last == Snapshot.CURRENT_STATE_ID) {
913+
return storagespaceConsumed(null).getStorageSpace();
914+
}
915+
916+
final long ssDeltaNoReplication;
917+
short replication;
918+
919+
if (last < lastSnapshotId) {
920+
ssDeltaNoReplication = computeFileSize(true, false);
921+
replication = getFileReplication();
922+
} else {
923+
int sid = fileDiffList.getSnapshotById(lastSnapshotId);
924+
ssDeltaNoReplication = computeFileSize(sid);
925+
replication = getFileReplication(sid);
926+
}
927+
928+
return ssDeltaNoReplication * replication;
929+
}
930+
893931
/** The same as computeFileSize(null). */
894932
public final long computeFileSize() {
895933
return computeFileSize(CURRENT_STATE_ID);

hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/hdfs/server/namenode/snapshot/TestRenameWithSnapshots.java

+25-1
Original file line numberDiff line numberDiff line change
@@ -2476,4 +2476,28 @@ String printTree(String label) throws Exception {
24762476
output.println(b);
24772477
return b;
24782478
}
2479-
}
2479+
2480+
/**
2481+
* Test getContentsummary and getQuotausage for an INodeReference.
2482+
*/
2483+
@Test(timeout = 300000)
2484+
public void testQuotaForRenameFileInSnapshot() throws Exception {
2485+
final Path snapshotDir = new Path("/testRenameWithSnapshot");
2486+
hdfs.mkdirs(snapshotDir, new FsPermission((short) 0777));
2487+
final Path file = new Path(snapshotDir, "file");
2488+
DFSTestUtil.createFile(hdfs, file, BLOCKSIZE, REPL, SEED);
2489+
hdfs.allowSnapshot(snapshotDir);
2490+
hdfs.createSnapshot(snapshotDir, "s0");
2491+
hdfs.mkdirs(new Path("/dir1"));
2492+
2493+
// Truncate a file which exists in snapshot , that is an
2494+
// INodeReference
2495+
hdfs.truncate(file, 10);
2496+
hdfs.rename(file, new Path("/dir1"));
2497+
assertEquals(hdfs.getContentSummary(new Path("/")).getSpaceConsumed(),
2498+
hdfs.getQuotaUsage(new Path("/")).getSpaceConsumed());
2499+
assertEquals(
2500+
hdfs.getContentSummary(new Path("/")).getFileAndDirectoryCount(),
2501+
hdfs.getQuotaUsage(new Path("/")).getFileAndDirectoryCount());
2502+
}
2503+
}

0 commit comments

Comments
 (0)