/// <summary>
        /// Confirm that FSImage files in all StorageDirectory are the same,
        /// and non-empty, and there are the expected number of them.
        /// </summary>
        /// <param name="fsn">- the FSNamesystem being checked.</param>
        /// <param name="numImageDirs">- the configured number of StorageDirectory of type IMAGE.
        ///     </param>
        /// <returns>- the md5 hash of the most recent FSImage files, which must all be the same.
        ///     </returns>
        /// <exception cref="System.Exception">
        /// if image files are empty or different,
        /// if less than two StorageDirectory are provided, or if the
        /// actual number of StorageDirectory is less than configured.
        /// </exception>
        public static string CheckImages(FSNamesystem fsn, int numImageDirs)
        {
            NNStorage stg = fsn.GetFSImage().GetStorage();

            //any failed StorageDirectory is removed from the storageDirs list
            NUnit.Framework.Assert.AreEqual("Some StorageDirectories failed Upgrade", numImageDirs
                                            , stg.GetNumStorageDirs(NNStorage.NameNodeDirType.Image));
            NUnit.Framework.Assert.IsTrue("Not enough fsimage copies in MiniDFSCluster " + "to test parallel write"
                                          , numImageDirs > 1);
            // List of "current/" directory from each SD
            IList <FilePath> dirs = FSImageTestUtil.GetCurrentDirs(stg, NNStorage.NameNodeDirType
                                                                   .Image);

            // across directories, all files with same names should be identical hashes
            FSImageTestUtil.AssertParallelFilesAreIdentical(dirs, Sharpen.Collections.EmptySet
                                                            <string>());
            FSImageTestUtil.AssertSameNewestImage(dirs);
            // Return the hash of the newest image file
            Storage.StorageDirectory firstSd = stg.DirIterator(NNStorage.NameNodeDirType.Image
                                                               ).Next();
            FilePath latestImage = FSImageTestUtil.FindLatestImageFile(firstSd);
            string   md5         = FSImageTestUtil.GetImageFileMD5IgnoringTxId(latestImage);

            System.Console.Error.WriteLine("md5 of " + latestImage + ": " + md5);
            return(md5);
        }
        public virtual void TestExcludeInProgressStreams()
        {
            FilePath f = new FilePath(TestEditLog.TestDir + "/excludeinprogressstreams");
            // Don't close the edit log once the files have been set up.
            NNStorage storage = TestEditLog.SetupEdits(Sharpen.Collections.SingletonList <URI>
                                                           (f.ToURI()), 10, false);

            Storage.StorageDirectory sd = storage.DirIterator(NNStorage.NameNodeDirType.Edits
                                                              ).Next();
            FileJournalManager jm = new FileJournalManager(conf, sd, storage);

            // If we exclude the in-progess stream, we should only have 100 tx.
            NUnit.Framework.Assert.AreEqual(100, GetNumberOfTransactions(jm, 1, false, false)
                                            );
            EditLogInputStream elis = GetJournalInputStream(jm, 90, false);

            try
            {
                FSEditLogOp lastReadOp = null;
                while ((lastReadOp = elis.ReadOp()) != null)
                {
                    NUnit.Framework.Assert.IsTrue(lastReadOp.GetTransactionId() <= 100);
                }
            }
            finally
            {
                IOUtils.Cleanup(Log, elis);
            }
        }
        public virtual void TestDoPreUpgradeIOError()
        {
            FilePath    storageDir = new FilePath(TestEditLog.TestDir, "preupgradeioerror");
            IList <URI> editUris   = Sharpen.Collections.SingletonList(storageDir.ToURI());
            NNStorage   storage    = TestEditLog.SetupEdits(editUris, 5);

            Storage.StorageDirectory sd = storage.DirIterator(NNStorage.NameNodeDirType.Edits
                                                              ).Next();
            NUnit.Framework.Assert.IsNotNull(sd);
            // Change storage directory so that renaming current to previous.tmp fails.
            FileUtil.SetWritable(storageDir, false);
            FileJournalManager jm = null;

            try
            {
                jm = new FileJournalManager(conf, sd, storage);
                exception.Expect(typeof(IOException));
                if (NativeCodeLoader.IsNativeCodeLoaded())
                {
                    exception.ExpectMessage("failure in native rename");
                }
                jm.DoPreUpgrade();
            }
            finally
            {
                IOUtils.Cleanup(Log, jm);
                // Restore permissions on storage directory and make sure we can delete.
                FileUtil.SetWritable(storageDir, true);
                FileUtil.FullyDelete(storageDir);
            }
        }
        public virtual void TestInprogressRecoveryAll()
        {
            FilePath    f1       = new FilePath(TestEditLog.TestDir + "/failalltest0");
            FilePath    f2       = new FilePath(TestEditLog.TestDir + "/failalltest1");
            FilePath    f3       = new FilePath(TestEditLog.TestDir + "/failalltest2");
            IList <URI> editUris = ImmutableList.Of(f1.ToURI(), f2.ToURI(), f3.ToURI());
            // abort after the 5th roll
            NNStorage storage = TestEditLog.SetupEdits(editUris, 5, new TestEditLog.AbortSpec
                                                           (5, 0), new TestEditLog.AbortSpec(5, 1), new TestEditLog.AbortSpec(5, 2));
            IEnumerator <Storage.StorageDirectory> dirs = storage.DirIterator(NNStorage.NameNodeDirType
                                                                              .Edits);

            Storage.StorageDirectory sd = dirs.Next();
            FileJournalManager       jm = new FileJournalManager(conf, sd, storage);

            NUnit.Framework.Assert.AreEqual(5 * TestEditLog.TxnsPerRoll + TestEditLog.TxnsPerFail
                                            , GetNumberOfTransactions(jm, 1, true, false));
            sd = dirs.Next();
            jm = new FileJournalManager(conf, sd, storage);
            NUnit.Framework.Assert.AreEqual(5 * TestEditLog.TxnsPerRoll + TestEditLog.TxnsPerFail
                                            , GetNumberOfTransactions(jm, 1, true, false));
            sd = dirs.Next();
            jm = new FileJournalManager(conf, sd, storage);
            NUnit.Framework.Assert.AreEqual(5 * TestEditLog.TxnsPerRoll + TestEditLog.TxnsPerFail
                                            , GetNumberOfTransactions(jm, 1, true, false));
        }
        public virtual void TestManyLogsWithGaps()
        {
            FilePath  f       = new FilePath(TestEditLog.TestDir + "/manylogswithgaps");
            NNStorage storage = TestEditLog.SetupEdits(Sharpen.Collections.SingletonList <URI>
                                                           (f.ToURI()), 10);

            Storage.StorageDirectory sd = storage.DirIterator(NNStorage.NameNodeDirType.Edits
                                                              ).Next();
            long startGapTxId = 3 * TestEditLog.TxnsPerRoll + 1;
            long endGapTxId   = 4 * TestEditLog.TxnsPerRoll;

            FilePath[] files = new FilePath(f, "current").ListFiles(new _FilenameFilter_324(startGapTxId
                                                                                            , endGapTxId));
            NUnit.Framework.Assert.AreEqual(1, files.Length);
            NUnit.Framework.Assert.IsTrue(files[0].Delete());
            FileJournalManager jm = new FileJournalManager(conf, sd, storage);

            NUnit.Framework.Assert.AreEqual(startGapTxId - 1, GetNumberOfTransactions(jm, 1,
                                                                                      true, true));
            NUnit.Framework.Assert.AreEqual(0, GetNumberOfTransactions(jm, startGapTxId, true
                                                                       , true));
            // rolled 10 times so there should be 11 files.
            NUnit.Framework.Assert.AreEqual(11 * TestEditLog.TxnsPerRoll - endGapTxId, GetNumberOfTransactions
                                                (jm, endGapTxId + 1, true, true));
        }
Esempio n. 6
0
 /// <exception cref="System.IO.IOException"/>
 private void DoUpgrade(NNStorage storage)
 {
     for (IEnumerator <Storage.StorageDirectory> it = storage.DirIterator(false); it.HasNext
              ();)
     {
         Storage.StorageDirectory sd = it.Next();
         NNUpgradeUtil.DoUpgrade(sd, storage);
     }
 }
Esempio n. 7
0
        /// <summary>This is called when using bootstrapStandby for HA upgrade.</summary>
        /// <remarks>
        /// This is called when using bootstrapStandby for HA upgrade. The SBN should
        /// also create previous directory so that later when it starts, it understands
        /// that the cluster is in the upgrade state. This function renames the old
        /// current directory to previous.tmp.
        /// </remarks>
        /// <exception cref="System.IO.IOException"/>
        private bool DoPreUpgrade(NNStorage storage, NamespaceInfo nsInfo)
        {
            bool isFormatted = false;
            IDictionary <Storage.StorageDirectory, Storage.StorageState> dataDirStates = new Dictionary
                                                                                         <Storage.StorageDirectory, Storage.StorageState>();

            try
            {
                isFormatted = FSImage.RecoverStorageDirs(HdfsServerConstants.StartupOption.Upgrade
                                                         , storage, dataDirStates);
                if (dataDirStates.Values.Contains(Storage.StorageState.NotFormatted))
                {
                    // recoverStorageDirs returns true if there is a formatted directory
                    isFormatted = false;
                    System.Console.Error.WriteLine("The original storage directory is not formatted."
                                                   );
                }
            }
            catch (InconsistentFSStateException e)
            {
                // if the storage is in a bad state,
                Log.Warn("The storage directory is in an inconsistent state", e);
            }
            finally
            {
                storage.UnlockAll();
            }
            // if there is InconsistentFSStateException or the storage is not formatted,
            // format the storage. Although this format is done through the new
            // software, since in HA setup the SBN is rolled back through
            // "-bootstrapStandby", we should still be fine.
            if (!isFormatted && !Format(storage, nsInfo))
            {
                return(false);
            }
            // make sure there is no previous directory
            FSImage.CheckUpgrade(storage);
            // Do preUpgrade for each directory
            for (IEnumerator <Storage.StorageDirectory> it = storage.DirIterator(false); it.HasNext
                     ();)
            {
                Storage.StorageDirectory sd = it.Next();
                try
                {
                    NNUpgradeUtil.RenameCurToTmp(sd);
                }
                catch (IOException e)
                {
                    Log.Error("Failed to move aside pre-upgrade storage " + "in image directory " + sd
                              .GetRoot(), e);
                    throw;
                }
            }
            storage.SetStorageInfo(nsInfo);
            storage.SetBlockPoolID(nsInfo.GetBlockPoolID());
            return(true);
        }
Esempio n. 8
0
                /// <exception cref="System.IO.IOException"/>
                public virtual void PurgeLogsOlderThan(long minTxIdToKeep)
                {
                    IEnumerator <Storage.StorageDirectory> iter = storage.DirIterator();

                    while (iter.HasNext())
                    {
                        Storage.StorageDirectory dir = iter.Next();
                        IList <FileJournalManager.EditLogFile> editFiles = FileJournalManager.MatchEditLogs
                                                                               (dir.GetCurrentDir());
                        foreach (FileJournalManager.EditLogFile f in editFiles)
                        {
                            if (f.GetLastTxId() < minTxIdToKeep)
                            {
                                purger.PurgeLog(f);
                            }
                        }
                    }
                }
        public virtual void TestInprogressRecovery()
        {
            FilePath f = new FilePath(TestEditLog.TestDir + "/inprogressrecovery");
            // abort after the 5th roll
            NNStorage storage = TestEditLog.SetupEdits(Sharpen.Collections.SingletonList <URI>
                                                           (f.ToURI()), 5, new TestEditLog.AbortSpec(5, 0));

            Storage.StorageDirectory sd = storage.DirIterator(NNStorage.NameNodeDirType.Edits
                                                              ).Next();
            FileJournalManager jm = new FileJournalManager(conf, sd, storage);

            NUnit.Framework.Assert.AreEqual(5 * TestEditLog.TxnsPerRoll + TestEditLog.TxnsPerFail
                                            , GetNumberOfTransactions(jm, 1, true, false));
        }
        public virtual void TestManyLogsWithCorruptInprogress()
        {
            FilePath  f       = new FilePath(TestEditLog.TestDir + "/manylogswithcorruptinprogress");
            NNStorage storage = TestEditLog.SetupEdits(Sharpen.Collections.SingletonList <URI>
                                                           (f.ToURI()), 10, new TestEditLog.AbortSpec(10, 0));

            Storage.StorageDirectory sd = storage.DirIterator(NNStorage.NameNodeDirType.Edits
                                                              ).Next();
            FilePath[] files = new FilePath(f, "current").ListFiles(new _FilenameFilter_356()
                                                                    );
            NUnit.Framework.Assert.AreEqual(files.Length, 1);
            CorruptAfterStartSegment(files[0]);
            FileJournalManager jm = new FileJournalManager(conf, sd, storage);

            NUnit.Framework.Assert.AreEqual(10 * TestEditLog.TxnsPerRoll + 1, GetNumberOfTransactions
                                                (jm, 1, true, false));
        }
        public virtual void TestAskForTransactionsMidfile()
        {
            FilePath  f       = new FilePath(TestEditLog.TestDir + "/askfortransactionsmidfile");
            NNStorage storage = TestEditLog.SetupEdits(Sharpen.Collections.SingletonList <URI>
                                                           (f.ToURI()), 10);

            Storage.StorageDirectory sd = storage.DirIterator(NNStorage.NameNodeDirType.Edits
                                                              ).Next();
            FileJournalManager jm = new FileJournalManager(conf, sd, storage);
            // 10 rolls, so 11 rolled files, 110 txids total.
            int TotalTxids = 10 * 11;

            for (int txid = 1; txid <= TotalTxids; txid++)
            {
                NUnit.Framework.Assert.AreEqual((TotalTxids - txid) + 1, GetNumberOfTransactions(
                                                    jm, txid, true, false));
            }
        }
        public virtual void TestReadFromMiddleOfEditLog()
        {
            FilePath  f       = new FilePath(TestEditLog.TestDir + "/readfrommiddleofeditlog");
            NNStorage storage = TestEditLog.SetupEdits(Sharpen.Collections.SingletonList <URI>
                                                           (f.ToURI()), 10);

            Storage.StorageDirectory sd = storage.DirIterator(NNStorage.NameNodeDirType.Edits
                                                              ).Next();
            FileJournalManager jm   = new FileJournalManager(conf, sd, storage);
            EditLogInputStream elis = GetJournalInputStream(jm, 5, true);

            try
            {
                FSEditLogOp op = elis.ReadOp();
                NUnit.Framework.Assert.AreEqual("read unexpected op", op.GetTransactionId(), 5);
            }
            finally
            {
                IOUtils.Cleanup(Log, elis);
            }
        }
        public virtual void TestReadFromStream()
        {
            FilePath f = new FilePath(TestEditLog.TestDir + "/readfromstream");
            // abort after 10th roll
            NNStorage storage = TestEditLog.SetupEdits(Sharpen.Collections.SingletonList <URI>
                                                           (f.ToURI()), 10, new TestEditLog.AbortSpec(10, 0));

            Storage.StorageDirectory sd = storage.DirIterator(NNStorage.NameNodeDirType.Edits
                                                              ).Next();
            FileJournalManager jm      = new FileJournalManager(conf, sd, storage);
            long expectedTotalTxnCount = TestEditLog.TxnsPerRoll * 10 + TestEditLog.TxnsPerFail;

            NUnit.Framework.Assert.AreEqual(expectedTotalTxnCount, GetNumberOfTransactions(jm
                                                                                           , 1, true, false));
            long skippedTxns = (3 * TestEditLog.TxnsPerRoll);
            // skip first 3 files
            long startingTxId = skippedTxns + 1;
            long numLoadable  = GetNumberOfTransactions(jm, startingTxId, true, false);

            NUnit.Framework.Assert.AreEqual(expectedTotalTxnCount - skippedTxns, numLoadable);
        }
        /// <exception cref="System.IO.IOException"/>
        /// <exception cref="System.Exception"/>
        public virtual void TestFinalizeErrorReportedToNNStorage()
        {
            FilePath f = new FilePath(TestEditLog.TestDir + "/filejournaltestError");
            // abort after 10th roll
            NNStorage storage = TestEditLog.SetupEdits(Sharpen.Collections.SingletonList <URI>
                                                           (f.ToURI()), 10, new TestEditLog.AbortSpec(10, 0));

            Storage.StorageDirectory sd = storage.DirIterator(NNStorage.NameNodeDirType.Edits
                                                              ).Next();
            FileJournalManager jm         = new FileJournalManager(conf, sd, storage);
            string             sdRootPath = sd.GetRoot().GetAbsolutePath();

            FileUtil.Chmod(sdRootPath, "-w", true);
            try
            {
                jm.FinalizeLogSegment(0, 1);
            }
            finally
            {
                FileUtil.Chmod(sdRootPath, "+w", true);
                NUnit.Framework.Assert.IsTrue(storage.GetRemovedStorageDirs().Contains(sd));
            }
        }