|
@@ -27,6 +27,9 @@ import org.apache.hadoop.conf.Configuration;
|
|
|
import org.apache.hadoop.fs.FileStatus;
|
|
|
import org.apache.hadoop.fs.FileSystem;
|
|
|
import org.apache.hadoop.fs.Path;
|
|
|
+import org.apache.hadoop.fs.Options.Rename;
|
|
|
+import org.apache.hadoop.hdfs.protocol.LocatedBlocks;
|
|
|
+import org.apache.hadoop.hdfs.server.blockmanagement.BlockManager;
|
|
|
import org.apache.hadoop.hdfs.server.namenode.NameNodeAdapter;
|
|
|
import org.junit.Test;
|
|
|
|
|
@@ -125,4 +128,45 @@ public class TestDFSRename {
|
|
|
if (cluster != null) {cluster.shutdown();}
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Check the blocks of dst file are cleaned after rename with overwrite
|
|
|
+ */
|
|
|
+ @Test(timeout = 120000)
|
|
|
+ public void testRenameWithOverwrite() throws Exception {
|
|
|
+ final short replFactor = 2;
|
|
|
+ final long blockSize = 512;
|
|
|
+ Configuration conf = new Configuration();
|
|
|
+ MiniDFSCluster cluster = new MiniDFSCluster.Builder(conf).
|
|
|
+ numDataNodes(replFactor).build();
|
|
|
+ DistributedFileSystem dfs = cluster.getFileSystem();
|
|
|
+ try {
|
|
|
+
|
|
|
+ long fileLen = blockSize*3;
|
|
|
+ String src = "/foo/src";
|
|
|
+ String dst = "/foo/dst";
|
|
|
+ Path srcPath = new Path(src);
|
|
|
+ Path dstPath = new Path(dst);
|
|
|
+
|
|
|
+ DFSTestUtil.createFile(dfs, srcPath, fileLen, replFactor, 1);
|
|
|
+ DFSTestUtil.createFile(dfs, dstPath, fileLen, replFactor, 1);
|
|
|
+
|
|
|
+ LocatedBlocks lbs = NameNodeAdapter.getBlockLocations(
|
|
|
+ cluster.getNameNode(), dst, 0, fileLen);
|
|
|
+ BlockManager bm = NameNodeAdapter.getNamesystem(cluster.getNameNode()).
|
|
|
+ getBlockManager();
|
|
|
+ assertTrue(bm.getStoredBlock(lbs.getLocatedBlocks().get(0).getBlock().
|
|
|
+ getLocalBlock()) != null);
|
|
|
+ dfs.rename(srcPath, dstPath, Rename.OVERWRITE);
|
|
|
+ assertTrue(bm.getStoredBlock(lbs.getLocatedBlocks().get(0).getBlock().
|
|
|
+ getLocalBlock()) == null);
|
|
|
+ } finally {
|
|
|
+ if (dfs != null) {
|
|
|
+ dfs.close();
|
|
|
+ }
|
|
|
+ if (cluster != null) {
|
|
|
+ cluster.shutdown();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|