internal static IList <Mover.MLocation> ToLocations(LocatedBlock lb) { DatanodeInfo[] datanodeInfos = lb.GetLocations(); StorageType[] storageTypes = lb.GetStorageTypes(); long size = lb.GetBlockSize(); IList <Mover.MLocation> locations = new List <Mover.MLocation>(); for (int i = 0; i < datanodeInfos.Length; i++) { locations.AddItem(new Mover.MLocation(datanodeInfos[i], storageTypes[i], size)); } return(locations); }
/// <summary>TC11: Racing rename</summary> /// <exception cref="System.Exception"/> private void TestTC11(bool appendToNewBlock) { Path p = new Path("/TC11/foo" + (appendToNewBlock ? "0" : "1")); System.Console.Out.WriteLine("p=" + p); //a. Create file and write one block of data. Close file. int len1 = (int)BlockSize; { FSDataOutputStream @out = fs.Create(p, false, buffersize, Replication, BlockSize); AppendTestUtil.Write(@out, 0, len1); @out.Close(); } //b. Reopen file in "append" mode. Append half block of data. FSDataOutputStream out_1 = appendToNewBlock ? fs.Append(p, EnumSet.Of(CreateFlag. Append, CreateFlag.NewBlock), 4096, null) : fs.Append(p); int len2 = (int)BlockSize / 2; AppendTestUtil.Write(out_1, len1, len2); out_1.Hflush(); //c. Rename file to file.new. Path pnew = new Path(p + ".new"); NUnit.Framework.Assert.IsTrue(fs.Rename(p, pnew)); //d. Close file handle that was opened in (b). out_1.Close(); //check block sizes long len = fs.GetFileStatus(pnew).GetLen(); LocatedBlocks locatedblocks = fs.dfs.GetNamenode().GetBlockLocations(pnew.ToString (), 0L, len); int numblock = locatedblocks.LocatedBlockCount(); for (int i = 0; i < numblock; i++) { LocatedBlock lb = locatedblocks.Get(i); ExtendedBlock blk = lb.GetBlock(); long size = lb.GetBlockSize(); if (i < numblock - 1) { NUnit.Framework.Assert.AreEqual(BlockSize, size); } foreach (DatanodeInfo datanodeinfo in lb.GetLocations()) { DataNode dn = cluster.GetDataNode(datanodeinfo.GetIpcPort()); Block metainfo = DataNodeTestUtils.GetFSDataset(dn).GetStoredBlock(blk.GetBlockPoolId (), blk.GetBlockId()); NUnit.Framework.Assert.AreEqual(size, metainfo.GetNumBytes()); } } }
/// <summary>TC7: Corrupted replicas are present.</summary> /// <exception cref="System.IO.IOException">an exception might be thrown</exception> /// <exception cref="System.Exception"/> private void TestTC7(bool appendToNewBlock) { short repl = 2; Path p = new Path("/TC7/foo" + (appendToNewBlock ? "0" : "1")); System.Console.Out.WriteLine("p=" + p); //a. Create file with replication factor of 2. Write half block of data. Close file. int len1 = (int)(BlockSize / 2); { FSDataOutputStream @out = fs.Create(p, false, buffersize, repl, BlockSize); AppendTestUtil.Write(@out, 0, len1); @out.Close(); } DFSTestUtil.WaitReplication(fs, p, repl); //b. Log into one datanode that has one replica of this block. // Find the block file on this datanode and truncate it to zero size. LocatedBlocks locatedblocks = fs.dfs.GetNamenode().GetBlockLocations(p.ToString() , 0L, len1); NUnit.Framework.Assert.AreEqual(1, locatedblocks.LocatedBlockCount()); LocatedBlock lb = locatedblocks.Get(0); ExtendedBlock blk = lb.GetBlock(); NUnit.Framework.Assert.AreEqual(len1, lb.GetBlockSize()); DatanodeInfo[] datanodeinfos = lb.GetLocations(); NUnit.Framework.Assert.AreEqual(repl, datanodeinfos.Length); DataNode dn = cluster.GetDataNode(datanodeinfos[0].GetIpcPort()); FilePath f = DataNodeTestUtils.GetBlockFile(dn, blk.GetBlockPoolId(), blk.GetLocalBlock ()); RandomAccessFile raf = new RandomAccessFile(f, "rw"); AppendTestUtil.Log.Info("dn=" + dn + ", blk=" + blk + " (length=" + blk.GetNumBytes () + ")"); NUnit.Framework.Assert.AreEqual(len1, raf.Length()); raf.SetLength(0); raf.Close(); //c. Open file in "append mode". Append a new block worth of data. Close file. int len2 = (int)BlockSize; { FSDataOutputStream @out = appendToNewBlock ? fs.Append(p, EnumSet.Of(CreateFlag.Append , CreateFlag.NewBlock), 4096, null) : fs.Append(p); AppendTestUtil.Write(@out, len1, len2); @out.Close(); } //d. Reopen file and read two blocks worth of data. AppendTestUtil.Check(fs, p, len1 + len2); }
public virtual void TestGetBlockLocations() { Path root = new Path("/"); Path file = new Path("/file"); DFSTestUtil.CreateFile(hdfs, file, Blocksize, Replication, seed); // take a snapshot on root SnapshotTestHelper.CreateSnapshot(hdfs, root, "s1"); Path fileInSnapshot = SnapshotTestHelper.GetSnapshotPath(root, "s1", file.GetName ()); FileStatus status = hdfs.GetFileStatus(fileInSnapshot); // make sure we record the size for the file NUnit.Framework.Assert.AreEqual(Blocksize, status.GetLen()); // append data to file DFSTestUtil.AppendFile(hdfs, file, Blocksize - 1); status = hdfs.GetFileStatus(fileInSnapshot); // the size of snapshot file should still be BLOCKSIZE NUnit.Framework.Assert.AreEqual(Blocksize, status.GetLen()); // the size of the file should be (2 * BLOCKSIZE - 1) status = hdfs.GetFileStatus(file); NUnit.Framework.Assert.AreEqual(Blocksize * 2 - 1, status.GetLen()); // call DFSClient#callGetBlockLocations for the file in snapshot LocatedBlocks blocks = DFSClientAdapter.CallGetBlockLocations(cluster.GetNameNodeRpc (), fileInSnapshot.ToString(), 0, long.MaxValue); IList <LocatedBlock> blockList = blocks.GetLocatedBlocks(); // should be only one block NUnit.Framework.Assert.AreEqual(Blocksize, blocks.GetFileLength()); NUnit.Framework.Assert.AreEqual(1, blockList.Count); // check the last block LocatedBlock lastBlock = blocks.GetLastLocatedBlock(); NUnit.Framework.Assert.AreEqual(0, lastBlock.GetStartOffset()); NUnit.Framework.Assert.AreEqual(Blocksize, lastBlock.GetBlockSize()); // take another snapshot SnapshotTestHelper.CreateSnapshot(hdfs, root, "s2"); Path fileInSnapshot2 = SnapshotTestHelper.GetSnapshotPath(root, "s2", file.GetName ()); // append data to file without closing HdfsDataOutputStream @out = AppendFileWithoutClosing(file, Blocksize); @out.Hsync(EnumSet.Of(HdfsDataOutputStream.SyncFlag.UpdateLength)); status = hdfs.GetFileStatus(fileInSnapshot2); // the size of snapshot file should be BLOCKSIZE*2-1 NUnit.Framework.Assert.AreEqual(Blocksize * 2 - 1, status.GetLen()); // the size of the file should be (3 * BLOCKSIZE - 1) status = hdfs.GetFileStatus(file); NUnit.Framework.Assert.AreEqual(Blocksize * 3 - 1, status.GetLen()); blocks = DFSClientAdapter.CallGetBlockLocations(cluster.GetNameNodeRpc(), fileInSnapshot2 .ToString(), 0, long.MaxValue); NUnit.Framework.Assert.IsFalse(blocks.IsUnderConstruction()); NUnit.Framework.Assert.IsTrue(blocks.IsLastBlockComplete()); blockList = blocks.GetLocatedBlocks(); // should be 2 blocks NUnit.Framework.Assert.AreEqual(Blocksize * 2 - 1, blocks.GetFileLength()); NUnit.Framework.Assert.AreEqual(2, blockList.Count); // check the last block lastBlock = blocks.GetLastLocatedBlock(); NUnit.Framework.Assert.AreEqual(Blocksize, lastBlock.GetStartOffset()); NUnit.Framework.Assert.AreEqual(Blocksize, lastBlock.GetBlockSize()); blocks = DFSClientAdapter.CallGetBlockLocations(cluster.GetNameNodeRpc(), fileInSnapshot2 .ToString(), Blocksize, 0); blockList = blocks.GetLocatedBlocks(); NUnit.Framework.Assert.AreEqual(1, blockList.Count); // check blocks for file being written blocks = DFSClientAdapter.CallGetBlockLocations(cluster.GetNameNodeRpc(), file.ToString (), 0, long.MaxValue); blockList = blocks.GetLocatedBlocks(); NUnit.Framework.Assert.AreEqual(3, blockList.Count); NUnit.Framework.Assert.IsTrue(blocks.IsUnderConstruction()); NUnit.Framework.Assert.IsFalse(blocks.IsLastBlockComplete()); lastBlock = blocks.GetLastLocatedBlock(); NUnit.Framework.Assert.AreEqual(Blocksize * 2, lastBlock.GetStartOffset()); NUnit.Framework.Assert.AreEqual(Blocksize - 1, lastBlock.GetBlockSize()); @out.Close(); }
/// <summary>Test to verify the race between finalizeBlock and Lease recovery</summary> /// <exception cref="System.Exception"/> public virtual void TestRaceBetweenReplicaRecoveryAndFinalizeBlock() { TearDown(); // Stop the Mocked DN started in startup() Configuration conf = new HdfsConfiguration(); conf.Set(DFSConfigKeys.DfsDatanodeXceiverStopTimeoutMillisKey, "1000"); MiniDFSCluster cluster = new MiniDFSCluster.Builder(conf).NumDataNodes(1).Build(); try { cluster.WaitClusterUp(); DistributedFileSystem fs = cluster.GetFileSystem(); Path path = new Path("/test"); FSDataOutputStream @out = fs.Create(path); @out.WriteBytes("data"); @out.Hsync(); IList <LocatedBlock> blocks = DFSTestUtil.GetAllBlocks(fs.Open(path)); LocatedBlock block = blocks[0]; DataNode dataNode = cluster.GetDataNodes()[0]; AtomicBoolean recoveryInitResult = new AtomicBoolean(true); Sharpen.Thread recoveryThread = new _Thread_612(block, dataNode, recoveryInitResult ); recoveryThread.Start(); try { @out.Close(); } catch (IOException e) { NUnit.Framework.Assert.IsTrue("Writing should fail", e.Message.Contains("are bad. Aborting..." )); } finally { recoveryThread.Join(); } NUnit.Framework.Assert.IsTrue("Recovery should be initiated successfully", recoveryInitResult .Get()); dataNode.UpdateReplicaUnderRecovery(block.GetBlock(), block.GetBlock().GetGenerationStamp () + 1, block.GetBlock().GetBlockId(), block.GetBlockSize()); } finally { if (null != cluster) { cluster.Shutdown(); cluster = null; } } }