// // writes to file // /// <exception cref="System.IO.IOException"/> private static void WriteFile(FSDataOutputStream stm, long seed) { byte[] buffer = AppendTestUtil.RandomBytes(seed, fileSize); int mid = fileSize / 2; stm.Write(buffer, 0, mid); stm.Write(buffer, mid, fileSize - mid); }
// // verify that the data written are sane // /// <exception cref="System.IO.IOException"/> private static void CheckFile(FileSystem fileSys, Path name, int repl, int numblocks , int filesize, long seed) { bool done = false; int attempt = 0; long len = fileSys.GetFileStatus(name).GetLen(); NUnit.Framework.Assert.IsTrue(name + " should be of size " + filesize + " but found to be of size " + len, len == filesize); // wait till all full blocks are confirmed by the datanodes. while (!done) { attempt++; try { Sharpen.Thread.Sleep(1000); } catch (Exception) { } done = true; BlockLocation[] locations = fileSys.GetFileBlockLocations(fileSys.GetFileStatus(name ), 0, filesize); if (locations.Length < numblocks) { if (attempt > 100) { System.Console.Out.WriteLine("File " + name + " has only " + locations.Length + " blocks, " + " but is expected to have " + numblocks + " blocks."); } done = false; continue; } for (int idx = 0; idx < locations.Length; idx++) { if (locations[idx].GetHosts().Length < repl) { if (attempt > 100) { System.Console.Out.WriteLine("File " + name + " has " + locations.Length + " blocks: " + " The " + idx + " block has only " + locations[idx].GetHosts().Length + " replicas but is expected to have " + repl + " replicas."); } done = false; break; } } } FSDataInputStream stm = fileSys.Open(name); byte[] expected = AppendTestUtil.RandomBytes(seed, fileSize); // do a sanity check. Read the file byte[] actual = new byte[filesize]; stm.ReadFully(0, actual); CheckData(actual, 0, expected, "Read 1"); }
private void InitBuffer(int size) { long seed = AppendTestUtil.NextLong(); toWrite = AppendTestUtil.RandomBytes(seed, size); }
/// <summary> /// Write to one file, then kill one datanode in the pipeline and then /// close the file. /// </summary> /// <exception cref="System.IO.IOException"/> private void SimpleTest(int datanodeToKill) { Configuration conf = new HdfsConfiguration(); conf.SetInt(DFSConfigKeys.DfsNamenodeHeartbeatRecheckIntervalKey, 2000); conf.SetInt(DFSConfigKeys.DfsHeartbeatIntervalKey, 1); conf.SetInt(DFSConfigKeys.DfsNamenodeReplicationPendingTimeoutSecKey, 2); conf.SetInt(DFSConfigKeys.DfsClientSocketTimeoutKey, 5000); int myMaxNodes = 5; System.Console.Out.WriteLine("SimpleTest starting with DataNode to Kill " + datanodeToKill ); MiniDFSCluster cluster = new MiniDFSCluster.Builder(conf).NumDataNodes(myMaxNodes ).Build(); cluster.WaitActive(); FileSystem fs = cluster.GetFileSystem(); short repl = 3; Path filename = new Path("simpletest.dat"); try { // create a file and write one block of data System.Console.Out.WriteLine("SimpleTest creating file " + filename); FSDataOutputStream stm = CreateFile(fs, filename, repl); DFSOutputStream dfstream = (DFSOutputStream)(stm.GetWrappedStream()); // these are test settings dfstream.SetChunksPerPacket(5); dfstream.SetArtificialSlowdown(3000); long myseed = AppendTestUtil.NextLong(); byte[] buffer = AppendTestUtil.RandomBytes(myseed, fileSize); int mid = fileSize / 4; stm.Write(buffer, 0, mid); DatanodeInfo[] targets = dfstream.GetPipeline(); int count = 5; while (count-- > 0 && targets == null) { try { System.Console.Out.WriteLine("SimpleTest: Waiting for pipeline to be created."); Sharpen.Thread.Sleep(1000); } catch (Exception) { } targets = dfstream.GetPipeline(); } if (targets == null) { int victim = AppendTestUtil.NextInt(myMaxNodes); System.Console.Out.WriteLine("SimpleTest stopping datanode random " + victim); cluster.StopDataNode(victim); } else { int victim = datanodeToKill; System.Console.Out.WriteLine("SimpleTest stopping datanode " + targets[victim]); cluster.StopDataNode(targets[victim].GetXferAddr()); } System.Console.Out.WriteLine("SimpleTest stopping datanode complete"); // write some more data to file, close and verify stm.Write(buffer, mid, fileSize - mid); stm.Close(); CheckFile(fs, filename, repl, numBlocks, fileSize, myseed); } catch (Exception e) { System.Console.Out.WriteLine("Simple Workload exception " + e); Sharpen.Runtime.PrintStackTrace(e); NUnit.Framework.Assert.IsTrue(e.ToString(), false); } finally { fs.Close(); cluster.Shutdown(); } }