Skip to content

Commit 7d03024

Browse files
committed
add snapshot ut
1 parent 8f570c9 commit 7d03024

File tree

2 files changed

+44
-5
lines changed

2 files changed

+44
-5
lines changed

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -673,7 +673,7 @@ private static class RenameOperation {
673673
private INode oldDstChild;
674674

675675
RenameOperation(FSDirectory fsd, INodesInPath srcIIP, INodesInPath dstIIP,
676-
Triple<Boolean, Optional<QuotaCounts>, Optional<QuotaCounts>> quotaPair) {
676+
Triple<Boolean, Optional<QuotaCounts>, Optional<QuotaCounts>> countTriple) {
677677
this.fsd = fsd;
678678
this.srcIIP = srcIIP;
679679
this.dstIIP = dstIIP;
@@ -723,10 +723,10 @@ private static class RenameOperation {
723723
withCount = null;
724724
}
725725
// Set quota for src and dst, ignore src is in Snapshot or is Reference
726-
this.updateQuota = quotaPair.getLeft() || isSrcInSnapshot || srcChildIsReference;
726+
this.updateQuota = countTriple.getLeft() || withCount != null;
727727
this.srcSubTreeCount = withCount == null ?
728-
quotaPair.getMiddle() : Optional.empty();
729-
this.dstSubTreeCount = quotaPair.getRight();
728+
countTriple.getMiddle() : Optional.empty();
729+
this.dstSubTreeCount = countTriple.getRight();
730730
}
731731

732732
boolean isSameStoragePolicy() {

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

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,6 @@ public void testRenameWithoutValidFeature() throws Exception {
219219
long expectedCount =
220220
dfs.getQuotaUsage(new Path("/")).getFileAndDirectoryCount() - originDstDirUsage;
221221
ContentSummary contentSummary3 = dfs.getContentSummary(testParentDir2);
222-
223222
// Src and dst must be same
224223
// dstDir2=/testRename/testDir2/dst-dir
225224
// dstDir3=/testRename/testDir3/dst-dir
@@ -303,10 +302,12 @@ public void testRenameUndoWithoutValidFeature() throws Exception {
303302
Mockito.doReturn(false).when(mockDir4).addChild(Mockito.eq(srcInode3), anyBoolean(), anyInt());
304303
rootDir.replaceChild(dir4, mockDir4, fsDirectory.getINodeMap());
305304
mockDir4.setParent(rootDir);
305+
306306
// srcDir=/testRenameUndo/testDir3/src-dir
307307
// dstDir=/testRenameUndo/testDir4/src-dir dstDir exist
308308
assertThrows(RemoteException.class,
309309
() -> dfs.rename(srcDir3, dstDir4, Options.Rename.OVERWRITE));
310+
310311
ContentSummary rootContentSummary4 = dfs.getContentSummary(new Path("/"));
311312
QuotaUsage rootQuotaUsage4 = dfs.getQuotaUsage(new Path("/"));
312313
ContentSummary contentSummary4 = dfs.getContentSummary(testParentDir3);
@@ -316,4 +317,42 @@ public void testRenameUndoWithoutValidFeature() throws Exception {
316317
assertEquals(rootContentSummary3.getFileAndDirectoryCount(),
317318
rootQuotaUsage4.getFileAndDirectoryCount());
318319
}
320+
321+
@Test
322+
public void testRenameFileInSnapshotDirWithoutValidFeature() throws Exception {
323+
final int fileLen = 1024;
324+
final short replication = 3;
325+
final Path root = new Path("/testRenameFileInSnapshotDir");
326+
assertTrue(dfs.mkdirs(root));
327+
328+
Path testParentDir1 = new Path(root, "testDir1");
329+
assertTrue(dfs.mkdirs(testParentDir1));
330+
Path file = new Path(testParentDir1, "file1");
331+
DFSTestUtil.createFile(dfs, file, fileLen, replication, 0);
332+
dfs.allowSnapshot(testParentDir1);
333+
dfs.createSnapshot(testParentDir1, "snapshot1");
334+
335+
Path testParentDir2 = new Path(root, "testDir2");
336+
assertTrue(dfs.mkdirs(testParentDir2));
337+
338+
ContentSummary contentSummary1 = dfs.getContentSummary(new Path("/"));
339+
QuotaUsage quotaUsage1 = dfs.getQuotaUsage(new Path("/"));
340+
assertEquals(contentSummary1.getSpaceConsumed(), quotaUsage1.getSpaceConsumed());
341+
assertEquals(contentSummary1.getFileAndDirectoryCount(),
342+
quotaUsage1.getFileAndDirectoryCount());
343+
344+
// The snapshot of file1 not be cleaned up, rename2 interface similar
345+
assertTrue(dfs.rename(new Path(testParentDir1, "file1"), testParentDir2));
346+
347+
ContentSummary contentSummary2 = dfs.getContentSummary(new Path("/"));
348+
QuotaUsage quotaUsage2 = dfs.getQuotaUsage(new Path("/"));
349+
assertEquals(quotaUsage1.getFileAndDirectoryCount() + 1,
350+
quotaUsage2.getFileAndDirectoryCount());
351+
assertEquals(quotaUsage1.getSpaceConsumed() + fileLen * replication,
352+
quotaUsage2.getSpaceConsumed());
353+
// Root directory's actual capacity must match quota usage
354+
assertEquals(contentSummary2.getFileAndDirectoryCount(),
355+
quotaUsage2.getFileAndDirectoryCount());
356+
assertEquals(contentSummary2.getSpaceConsumed(), quotaUsage2.getSpaceConsumed());
357+
}
319358
}

0 commit comments

Comments
 (0)