public virtual void TestCopyOnWrite() { Configuration conf = new HdfsConfiguration(); MiniDFSCluster cluster = new MiniDFSCluster.Builder(conf).Build(); FileSystem fs = cluster.GetFileSystem(); IPEndPoint addr = new IPEndPoint("localhost", cluster.GetNameNodePort()); DFSClient client = new DFSClient(addr, conf); try { // create a new file, write to it and close it. // Path file1 = new Path("/filestatus.dat"); FSDataOutputStream stm = AppendTestUtil.CreateFile(fs, file1, 1); WriteFile(stm); stm.Close(); // Get a handle to the datanode DataNode[] dn = cluster.ListDataNodes(); NUnit.Framework.Assert.IsTrue("There should be only one datanode but found " + dn .Length, dn.Length == 1); LocatedBlocks locations = client.GetNamenode().GetBlockLocations(file1.ToString() , 0, long.MaxValue); IList <LocatedBlock> blocks = locations.GetLocatedBlocks(); // // Create hard links for a few of the blocks // for (int i = 0; i < blocks.Count; i = i + 2) { ExtendedBlock b = blocks[i].GetBlock(); FilePath f = DataNodeTestUtils.GetFile(dn[0], b.GetBlockPoolId(), b.GetLocalBlock ().GetBlockId()); FilePath link = new FilePath(f.ToString() + ".link"); System.Console.Out.WriteLine("Creating hardlink for File " + f + " to " + link); HardLink.CreateHardLink(f, link); } // // Detach all blocks. This should remove hardlinks (if any) // for (int i_1 = 0; i_1 < blocks.Count; i_1++) { ExtendedBlock b = blocks[i_1].GetBlock(); System.Console.Out.WriteLine("testCopyOnWrite detaching block " + b); NUnit.Framework.Assert.IsTrue("Detaching block " + b + " should have returned true" , DataNodeTestUtils.UnlinkBlock(dn[0], b, 1)); } // Since the blocks were already detached earlier, these calls should // return false // for (int i_2 = 0; i_2 < blocks.Count; i_2++) { ExtendedBlock b = blocks[i_2].GetBlock(); System.Console.Out.WriteLine("testCopyOnWrite detaching block " + b); NUnit.Framework.Assert.IsTrue("Detaching block " + b + " should have returned false" , !DataNodeTestUtils.UnlinkBlock(dn[0], b, 1)); } } finally { client.Close(); fs.Close(); cluster.Shutdown(); } }