internal CheckpointSignature(FSImage fsImage) : base(fsImage.GetStorage()) { blockpoolID = fsImage.GetBlockPoolID(); mostRecentCheckpointTxId = fsImage.GetStorage().GetMostRecentCheckpointTxId(); curSegmentTxId = fsImage.GetEditLog().GetCurSegmentTxId(); }
/// <exception cref="System.IO.IOException"/> private int DownloadImage(NNStorage storage, NamenodeProtocol proxy) { // Load the newly formatted image, using all of the directories // (including shared edits) long imageTxId = proxy.GetMostRecentCheckpointTxId(); long curTxId = proxy.GetTransactionID(); FSImage image = new FSImage(conf); try { image.GetStorage().SetStorageInfo(storage); image.InitEditLog(HdfsServerConstants.StartupOption.Regular); System.Diagnostics.Debug.Assert(image.GetEditLog().IsOpenForRead(), "Expected edit log to be open for read" ); // Ensure that we have enough edits already in the shared directory to // start up from the last checkpoint on the active. if (!skipSharedEditsCheck && !CheckLogsAvailableForRead(image, imageTxId, curTxId )) { return(ErrCodeLogsUnavailable); } image.GetStorage().WriteTransactionIdFileToStorage(curTxId); // Download that checkpoint into our storage directories. MD5Hash hash = TransferFsImage.DownloadImageToStorage(otherHttpAddr, imageTxId, storage , true); image.SaveDigestAndRenameCheckpointImage(NNStorage.NameNodeFile.Image, imageTxId, hash); } catch (IOException ioe) { image.Close(); throw; } return(0); }
public virtual void TestReplQueuesActiveAfterStartupSafemode() { Configuration conf = new Configuration(); FSEditLog fsEditLog = Org.Mockito.Mockito.Mock <FSEditLog>(); FSImage fsImage = Org.Mockito.Mockito.Mock <FSImage>(); Org.Mockito.Mockito.When(fsImage.GetEditLog()).ThenReturn(fsEditLog); FSNamesystem fsNamesystem = new FSNamesystem(conf, fsImage); FSNamesystem fsn = Org.Mockito.Mockito.Spy(fsNamesystem); //Make shouldPopulaeReplQueues return true HAContext haContext = Org.Mockito.Mockito.Mock <HAContext>(); HAState haState = Org.Mockito.Mockito.Mock <HAState>(); Org.Mockito.Mockito.When(haContext.GetState()).ThenReturn(haState); Org.Mockito.Mockito.When(haState.ShouldPopulateReplQueues()).ThenReturn(true); Whitebox.SetInternalState(fsn, "haContext", haContext); //Make NameNode.getNameNodeMetrics() not return null NameNode.InitMetrics(conf, HdfsServerConstants.NamenodeRole.Namenode); fsn.EnterSafeMode(false); NUnit.Framework.Assert.IsTrue("FSNamesystem didn't enter safemode", fsn.IsInSafeMode ()); NUnit.Framework.Assert.IsTrue("Replication queues were being populated during very first " + "safemode", !fsn.IsPopulatingReplQueues()); fsn.LeaveSafeMode(); NUnit.Framework.Assert.IsTrue("FSNamesystem didn't leave safemode", !fsn.IsInSafeMode ()); NUnit.Framework.Assert.IsTrue("Replication queues weren't being populated even after leaving " + "safemode", fsn.IsPopulatingReplQueues()); fsn.EnterSafeMode(false); NUnit.Framework.Assert.IsTrue("FSNamesystem didn't enter safemode", fsn.IsInSafeMode ()); NUnit.Framework.Assert.IsTrue("Replication queues weren't being populated after entering " + "safemode 2nd time", fsn.IsPopulatingReplQueues()); }
/// <summary>invalidate storage by removing the second and third storage directories</summary> /// <exception cref="System.IO.IOException"/> public virtual void InvalidateStorage(FSImage fi, ICollection <FilePath> filesToInvalidate ) { AList <Storage.StorageDirectory> al = new AList <Storage.StorageDirectory>(2); IEnumerator <Storage.StorageDirectory> it = fi.GetStorage().DirIterator(); while (it.HasNext()) { Storage.StorageDirectory sd = it.Next(); if (filesToInvalidate.Contains(sd.GetRoot())) { Log.Info("causing IO error on " + sd.GetRoot()); al.AddItem(sd); } } // simulate an error fi.GetStorage().ReportErrorsOnDirectories(al); foreach (JournalSet.JournalAndStream j in fi.GetEditLog().GetJournals()) { if (j.GetManager() is FileJournalManager) { FileJournalManager fm = (FileJournalManager)j.GetManager(); if (fm.GetStorageDirectory().GetRoot().Equals(path2) || fm.GetStorageDirectory(). GetRoot().Equals(path3)) { EditLogOutputStream mockStream = Org.Mockito.Mockito.Spy(j.GetCurrentStream()); j.SetCurrentStreamForTests(mockStream); Org.Mockito.Mockito.DoThrow(new IOException("Injected fault: write")).When(mockStream ).Write(Org.Mockito.Mockito.AnyObject <FSEditLogOp>()); } } } }
/// <summary>Pull out one of the JournalAndStream objects from the edit log.</summary> private JournalSet.JournalAndStream GetJournalAndStream(int index) { FSImage fsimage = cluster.GetNamesystem().GetFSImage(); FSEditLog editLog = fsimage.GetEditLog(); return(editLog.GetJournals()[index]); }
public virtual void TestSaveImageWhileSyncInProgress() { Configuration conf = GetConf(); NameNode.InitMetrics(conf, HdfsServerConstants.NamenodeRole.Namenode); DFSTestUtil.FormatNameNode(conf); FSNamesystem namesystem = FSNamesystem.LoadFromDisk(conf); try { FSImage fsimage = namesystem.GetFSImage(); FSEditLog editLog = fsimage.GetEditLog(); JournalSet.JournalAndStream jas = editLog.GetJournals()[0]; EditLogFileOutputStream spyElos = Org.Mockito.Mockito.Spy((EditLogFileOutputStream )jas.GetCurrentStream()); jas.SetCurrentStreamForTests(spyElos); AtomicReference <Exception> deferredException = new AtomicReference <Exception>(); CountDownLatch waitToEnterFlush = new CountDownLatch(1); Sharpen.Thread doAnEditThread = new _Thread_371(namesystem, deferredException, waitToEnterFlush ); Answer <Void> blockingFlush = new _Answer_388(doAnEditThread, waitToEnterFlush); // Signal to main thread that the edit thread is in the racy section Org.Mockito.Mockito.DoAnswer(blockingFlush).When(spyElos).Flush(); doAnEditThread.Start(); // Wait for the edit thread to get to the logsync unsynchronized section Log.Info("Main thread: waiting to enter flush..."); waitToEnterFlush.Await(); NUnit.Framework.Assert.IsNull(deferredException.Get()); Log.Info("Main thread: detected that logSync is in unsynchronized section."); Log.Info("Trying to enter safe mode."); Log.Info("This should block for " + BlockTime + "sec, since flush will sleep that long" ); long st = Time.Now(); namesystem.SetSafeMode(HdfsConstants.SafeModeAction.SafemodeEnter); long et = Time.Now(); Log.Info("Entered safe mode"); // Make sure we really waited for the flush to complete! NUnit.Framework.Assert.IsTrue(et - st > (BlockTime - 1) * 1000); // Once we're in safe mode, save namespace. namesystem.SaveNamespace(); Log.Info("Joining on edit thread..."); doAnEditThread.Join(); NUnit.Framework.Assert.IsNull(deferredException.Get()); // We did 3 edits: begin, txn, and end NUnit.Framework.Assert.AreEqual(3, VerifyEditLogs(namesystem, fsimage, NNStorage. GetFinalizedEditsFileName(1, 3), 1)); // after the save, just the one "begin" NUnit.Framework.Assert.AreEqual(1, VerifyEditLogs(namesystem, fsimage, NNStorage. GetInProgressEditsFileName(4), 4)); } finally { Log.Info("Closing nn"); if (namesystem != null) { namesystem.Close(); } } }
public virtual void TestSaveRightBeforeSync() { Configuration conf = GetConf(); NameNode.InitMetrics(conf, HdfsServerConstants.NamenodeRole.Namenode); DFSTestUtil.FormatNameNode(conf); FSNamesystem namesystem = FSNamesystem.LoadFromDisk(conf); try { FSImage fsimage = namesystem.GetFSImage(); FSEditLog editLog = Org.Mockito.Mockito.Spy(fsimage.GetEditLog()); DFSTestUtil.SetEditLogForTesting(namesystem, editLog); AtomicReference <Exception> deferredException = new AtomicReference <Exception>(); CountDownLatch waitToEnterSync = new CountDownLatch(1); Sharpen.Thread doAnEditThread = new _Thread_467(namesystem, deferredException, waitToEnterSync ); Answer <Void> blockingSync = new _Answer_484(doAnEditThread, waitToEnterSync); Org.Mockito.Mockito.DoAnswer(blockingSync).When(editLog).LogSync(); doAnEditThread.Start(); Log.Info("Main thread: waiting to just before logSync..."); waitToEnterSync.Await(); NUnit.Framework.Assert.IsNull(deferredException.Get()); Log.Info("Main thread: detected that logSync about to be called."); Log.Info("Trying to enter safe mode."); Log.Info("This should block for " + BlockTime + "sec, since we have pending edits" ); long st = Time.Now(); namesystem.SetSafeMode(HdfsConstants.SafeModeAction.SafemodeEnter); long et = Time.Now(); Log.Info("Entered safe mode"); // Make sure we really waited for the flush to complete! NUnit.Framework.Assert.IsTrue(et - st > (BlockTime - 1) * 1000); // Once we're in safe mode, save namespace. namesystem.SaveNamespace(); Log.Info("Joining on edit thread..."); doAnEditThread.Join(); NUnit.Framework.Assert.IsNull(deferredException.Get()); // We did 3 edits: begin, txn, and end NUnit.Framework.Assert.AreEqual(3, VerifyEditLogs(namesystem, fsimage, NNStorage. GetFinalizedEditsFileName(1, 3), 1)); // after the save, just the one "begin" NUnit.Framework.Assert.AreEqual(1, VerifyEditLogs(namesystem, fsimage, NNStorage. GetInProgressEditsFileName(4), 4)); } finally { Log.Info("Closing nn"); if (namesystem != null) { namesystem.Close(); } } }
public virtual void TestFsLockFairness() { Configuration conf = new Configuration(); FSEditLog fsEditLog = Org.Mockito.Mockito.Mock <FSEditLog>(); FSImage fsImage = Org.Mockito.Mockito.Mock <FSImage>(); Org.Mockito.Mockito.When(fsImage.GetEditLog()).ThenReturn(fsEditLog); conf.SetBoolean("dfs.namenode.fslock.fair", true); FSNamesystem fsNamesystem = new FSNamesystem(conf, fsImage); NUnit.Framework.Assert.IsTrue(fsNamesystem.GetFsLockForTests().IsFair()); conf.SetBoolean("dfs.namenode.fslock.fair", false); fsNamesystem = new FSNamesystem(conf, fsImage); NUnit.Framework.Assert.IsFalse(fsNamesystem.GetFsLockForTests().IsFair()); }
public virtual void TestStartupSafemode() { Configuration conf = new Configuration(); FSImage fsImage = Org.Mockito.Mockito.Mock <FSImage>(); FSEditLog fsEditLog = Org.Mockito.Mockito.Mock <FSEditLog>(); Org.Mockito.Mockito.When(fsImage.GetEditLog()).ThenReturn(fsEditLog); FSNamesystem fsn = new FSNamesystem(conf, fsImage); fsn.LeaveSafeMode(); NUnit.Framework.Assert.IsTrue("After leaving safemode FSNamesystem.isInStartupSafeMode still " + "returned true", !fsn.IsInStartupSafeMode()); NUnit.Framework.Assert.IsTrue("After leaving safemode FSNamesystem.isInSafeMode still returned" + " true", !fsn.IsInSafeMode()); fsn.EnterSafeMode(true); NUnit.Framework.Assert.IsTrue("After entering safemode due to low resources FSNamesystem." + "isInStartupSafeMode still returned true", !fsn.IsInStartupSafeMode()); NUnit.Framework.Assert.IsTrue("After entering safemode due to low resources FSNamesystem." + "isInSafeMode still returned false", fsn.IsInSafeMode()); }
public virtual void TestReset() { Configuration conf = new Configuration(); FSEditLog fsEditLog = Org.Mockito.Mockito.Mock <FSEditLog>(); FSImage fsImage = Org.Mockito.Mockito.Mock <FSImage>(); Org.Mockito.Mockito.When(fsImage.GetEditLog()).ThenReturn(fsEditLog); FSNamesystem fsn = new FSNamesystem(conf, fsImage); fsn.ImageLoadComplete(); NUnit.Framework.Assert.IsTrue(fsn.IsImageLoaded()); fsn.Clear(); NUnit.Framework.Assert.IsFalse(fsn.IsImageLoaded()); INodeDirectory root = (INodeDirectory)fsn.GetFSDirectory().GetINode("/"); NUnit.Framework.Assert.IsTrue(root.GetChildrenList(Org.Apache.Hadoop.Hdfs.Server.Namenode.Snapshot.Snapshot .CurrentStateId).IsEmpty()); fsn.ImageLoadComplete(); NUnit.Framework.Assert.IsTrue(fsn.IsImageLoaded()); }
private bool CheckLogsAvailableForRead(FSImage image, long imageTxId, long curTxIdOnOtherNode ) { if (imageTxId == curTxIdOnOtherNode) { // The other node hasn't written any logs since the last checkpoint. // This can be the case if the NN was freshly formatted as HA, and // then started in standby mode, so it has no edit logs at all. return(true); } long firstTxIdInLogs = imageTxId + 1; System.Diagnostics.Debug.Assert(curTxIdOnOtherNode >= firstTxIdInLogs, "first=" + firstTxIdInLogs + " onOtherNode=" + curTxIdOnOtherNode); try { ICollection <EditLogInputStream> streams = image.GetEditLog().SelectInputStreams(firstTxIdInLogs , curTxIdOnOtherNode, null, true); foreach (EditLogInputStream stream in streams) { IOUtils.CloseStream(stream); } return(true); } catch (IOException e) { string msg = "Unable to read transaction ids " + firstTxIdInLogs + "-" + curTxIdOnOtherNode + " from the configured shared edits storage " + Joiner.On(",").Join(sharedEditsUris ) + ". " + "Please copy these logs into the shared edits storage " + "or call saveNamespace on the active node.\n" + "Error: " + e.GetLocalizedMessage(); if (Log.IsDebugEnabled()) { Log.Fatal(msg, e); } else { Log.Fatal(msg); } return(false); } }
/// <exception cref="System.IO.IOException"/> private static FSNamesystem SetupFileSystem() { Configuration conf = new Configuration(); conf.SetLong(DFSConfigKeys.DfsNamenodeAccesstimePrecisionKey, 1L); FSEditLog editlog = Org.Mockito.Mockito.Mock <FSEditLog>(); FSImage image = Org.Mockito.Mockito.Mock <FSImage>(); Org.Mockito.Mockito.When(image.GetEditLog()).ThenReturn(editlog); FSNamesystem fsn = new FSNamesystem(conf, image, true); FSDirectory fsd = fsn.GetFSDirectory(); INodesInPath iip = fsd.GetINodesInPath("/", true); PermissionStatus perm = new PermissionStatus("hdfs", "supergroup", FsPermission.CreateImmutable ((short)unchecked ((int)(0x1ff)))); INodeFile file = new INodeFile(MockInodeId, Sharpen.Runtime.GetBytesForString(FileName , Charsets.Utf8), perm, 1, 1, new BlockInfoContiguous[] { }, (short)1, DFSConfigKeys .DfsBlockSizeDefault); fsn.GetFSDirectory().AddINode(iip, file); return(fsn); }
public virtual void TestSaveNamespace() { // start a cluster Configuration conf = new HdfsConfiguration(); MiniDFSCluster cluster = null; FileSystem fileSys = null; AtomicReference <Exception> caughtErr = new AtomicReference <Exception>(); try { cluster = new MiniDFSCluster.Builder(conf).NumDataNodes(NumDataNodes).Build(); cluster.WaitActive(); fileSys = cluster.GetFileSystem(); FSNamesystem namesystem = cluster.GetNamesystem(); NamenodeProtocols nn = cluster.GetNameNodeRpc(); FSImage fsimage = namesystem.GetFSImage(); FSEditLog editLog = fsimage.GetEditLog(); StartTransactionWorkers(nn, caughtErr); for (int i = 0; i < NumSaveImage && caughtErr.Get() == null; i++) { try { Sharpen.Thread.Sleep(20); } catch (Exception) { } Log.Info("Save " + i + ": entering safe mode"); namesystem.EnterSafeMode(false); // Verify edit logs before the save // They should start with the first edit after the checkpoint long logStartTxId = fsimage.GetStorage().GetMostRecentCheckpointTxId() + 1; VerifyEditLogs(namesystem, fsimage, NNStorage.GetInProgressEditsFileName(logStartTxId ), logStartTxId); Log.Info("Save " + i + ": saving namespace"); namesystem.SaveNamespace(); Log.Info("Save " + i + ": leaving safemode"); long savedImageTxId = fsimage.GetStorage().GetMostRecentCheckpointTxId(); // Verify that edit logs post save got finalized and aren't corrupt VerifyEditLogs(namesystem, fsimage, NNStorage.GetFinalizedEditsFileName(logStartTxId , savedImageTxId), logStartTxId); // The checkpoint id should be 1 less than the last written ID, since // the log roll writes the "BEGIN" transaction to the new log. NUnit.Framework.Assert.AreEqual(fsimage.GetStorage().GetMostRecentCheckpointTxId( ), editLog.GetLastWrittenTxId() - 1); namesystem.LeaveSafeMode(); Log.Info("Save " + i + ": complete"); } } finally { StopTransactionWorkers(); if (caughtErr.Get() != null) { throw new RuntimeException(caughtErr.Get()); } if (fileSys != null) { fileSys.Close(); } if (cluster != null) { cluster.Shutdown(); } } }
public virtual void TestEditLog() { // start a cluster Configuration conf = new HdfsConfiguration(); MiniDFSCluster cluster = null; FileSystem fileSys = null; try { conf.SetBoolean(DFSConfigKeys.DfsNamenodeDelegationTokenAlwaysUseKey, true); cluster = new MiniDFSCluster.Builder(conf).NumDataNodes(NumDataNodes).Build(); cluster.WaitActive(); fileSys = cluster.GetFileSystem(); FSNamesystem namesystem = cluster.GetNamesystem(); for (IEnumerator <URI> it = cluster.GetNameDirs(0).GetEnumerator(); it.HasNext();) { FilePath dir = new FilePath(it.Next().GetPath()); System.Console.Out.WriteLine(dir); } FSImage fsimage = namesystem.GetFSImage(); FSEditLog editLog = fsimage.GetEditLog(); // set small size of flush buffer editLog.SetOutputBufferCapacity(2048); // Create threads and make them run transactions concurrently. Sharpen.Thread[] threadId = new Sharpen.Thread[NumThreads]; for (int i = 0; i < NumThreads; i++) { TestSecurityTokenEditLog.Transactions trans = new TestSecurityTokenEditLog.Transactions (namesystem, NumTransactions); threadId[i] = new Sharpen.Thread(trans, "TransactionThread-" + i); threadId[i].Start(); } // wait for all transactions to get over for (int i_1 = 0; i_1 < NumThreads; i_1++) { try { threadId[i_1].Join(); } catch (Exception) { i_1--; } } // retry editLog.Close(); // Verify that we can read in all the transactions that we have written. // If there were any corruptions, it is likely that the reading in // of these transactions will throw an exception. // namesystem.GetDelegationTokenSecretManager().StopThreads(); int numKeys = namesystem.GetDelegationTokenSecretManager().GetNumberOfKeys(); int expectedTransactions = NumThreads * opsPerTrans * NumTransactions + numKeys + 2; // + 2 for BEGIN and END txns foreach (Storage.StorageDirectory sd in fsimage.GetStorage().DirIterable(NNStorage.NameNodeDirType .Edits)) { FilePath editFile = NNStorage.GetFinalizedEditsFile(sd, 1, 1 + expectedTransactions - 1); System.Console.Out.WriteLine("Verifying file: " + editFile); FSEditLogLoader loader = new FSEditLogLoader(namesystem, 0); long numEdits = loader.LoadFSEdits(new EditLogFileInputStream(editFile), 1); NUnit.Framework.Assert.AreEqual("Verification for " + editFile, expectedTransactions , numEdits); } } finally { if (fileSys != null) { fileSys.Close(); } if (cluster != null) { cluster.Shutdown(); } } }