/// <exception cref="System.IO.IOException"/> internal static void FlushAndCheckLength(EditLogFileOutputStream elos, long expectedLength ) { elos.SetReadyToFlush(); elos.FlushAndSync(true); NUnit.Framework.Assert.AreEqual(expectedLength, elos.GetFile().Length()); }
/// <exception cref="System.IO.IOException"/> internal static void RunEditLogTest(TestNameNodeRecovery.EditLogTestSetup elts) { FilePath TestLogName = new FilePath(TestDir, "test_edit_log"); FSEditLogOp.OpInstanceCache cache = new FSEditLogOp.OpInstanceCache(); EditLogFileOutputStream elfos = null; EditLogFileInputStream elfis = null; try { elfos = new EditLogFileOutputStream(new Configuration(), TestLogName, 0); elfos.Create(NameNodeLayoutVersion.CurrentLayoutVersion); elts.AddTransactionsToLog(elfos, cache); elfos.SetReadyToFlush(); elfos.FlushAndSync(true); elfos.Close(); elfos = null; elfis = new EditLogFileInputStream(TestLogName); elfis.SetMaxOpSize(elts.GetMaxOpSize()); // reading through normally will get you an exception ICollection <long> validTxIds = elts.GetValidTxIds(); FSEditLogOp op = null; long prevTxId = 0; try { while (true) { op = elfis.NextOp(); if (op == null) { break; } Log.Debug("read txid " + op.txid); if (!validTxIds.Contains(op.GetTransactionId())) { NUnit.Framework.Assert.Fail("read txid " + op.GetTransactionId() + ", which we did not expect to find." ); } validTxIds.Remove(op.GetTransactionId()); prevTxId = op.GetTransactionId(); } if (elts.GetLastValidTxId() != -1) { NUnit.Framework.Assert.Fail("failed to throw IoException as expected"); } } catch (IOException) { if (elts.GetLastValidTxId() == -1) { NUnit.Framework.Assert.Fail("expected all transactions to be valid, but got exception " + "on txid " + prevTxId); } else { NUnit.Framework.Assert.AreEqual(prevTxId, elts.GetLastValidTxId()); } } if (elts.GetLastValidTxId() != -1) { // let's skip over the bad transaction op = null; prevTxId = 0; try { while (true) { op = elfis.NextValidOp(); if (op == null) { break; } prevTxId = op.GetTransactionId(); NUnit.Framework.Assert.IsTrue(validTxIds.Remove(op.GetTransactionId())); } } catch (Exception e) { NUnit.Framework.Assert.Fail("caught IOException while trying to skip over bad " + "transaction. message was " + e.Message + "\nstack trace\n" + StringUtils.StringifyException (e)); } } // We should have read every valid transaction. NUnit.Framework.Assert.IsTrue(validTxIds.IsEmpty()); } finally { IOUtils.Cleanup(Log, elfos, elfis); } }