Ejemplo n.º 1
0
        /// <summary>
        /// Verify that a saveNamespace command brings faulty directories
        /// in fs.name.dir and fs.edit.dir back online.
        /// </summary>
        /// <exception cref="System.Exception"/>
        public virtual void TestReinsertnamedirsInSavenamespace()
        {
            // create a configuration with the key to restore error
            // directories in fs.name.dir
            Configuration conf = GetConf();

            conf.SetBoolean(DFSConfigKeys.DfsNamenodeNameDirRestoreKey, true);
            NameNode.InitMetrics(conf, HdfsServerConstants.NamenodeRole.Namenode);
            DFSTestUtil.FormatNameNode(conf);
            FSNamesystem fsn = FSNamesystem.LoadFromDisk(conf);
            // Replace the FSImage with a spy
            FSImage   originalImage = fsn.GetFSImage();
            NNStorage storage       = originalImage.GetStorage();
            FSImage   spyImage      = Org.Mockito.Mockito.Spy(originalImage);

            Whitebox.SetInternalState(fsn, "fsImage", spyImage);
            FileSystem   fs             = FileSystem.GetLocal(conf);
            FilePath     rootDir        = storage.GetStorageDir(0).GetRoot();
            Path         rootPath       = new Path(rootDir.GetPath(), "current");
            FsPermission permissionNone = new FsPermission((short)0);
            FsPermission permissionAll  = new FsPermission(FsAction.All, FsAction.ReadExecute,
                                                           FsAction.ReadExecute);

            fs.SetPermission(rootPath, permissionNone);
            try
            {
                DoAnEdit(fsn, 1);
                fsn.SetSafeMode(HdfsConstants.SafeModeAction.SafemodeEnter);
                // Save namespace - should mark the first storage dir as faulty
                // since it's not traversable.
                Log.Info("Doing the first savenamespace.");
                fsn.SaveNamespace();
                Log.Info("First savenamespace sucessful.");
                NUnit.Framework.Assert.IsTrue("Savenamespace should have marked one directory as bad."
                                              + " But found " + storage.GetRemovedStorageDirs().Count + " bad directories.",
                                              storage.GetRemovedStorageDirs().Count == 1);
                fs.SetPermission(rootPath, permissionAll);
                // The next call to savenamespace should try inserting the
                // erroneous directory back to fs.name.dir. This command should
                // be successful.
                Log.Info("Doing the second savenamespace.");
                fsn.SaveNamespace();
                Log.Warn("Second savenamespace sucessful.");
                NUnit.Framework.Assert.IsTrue("Savenamespace should have been successful in removing "
                                              + " bad directories from Image." + " But found " + storage.GetRemovedStorageDirs
                                                  ().Count + " bad directories.", storage.GetRemovedStorageDirs().Count == 0);
                // Now shut down and restart the namesystem
                Log.Info("Shutting down fsimage.");
                originalImage.Close();
                fsn.Close();
                fsn = null;
                // Start a new namesystem, which should be able to recover
                // the namespace from the previous incarnation.
                Log.Info("Loading new FSmage from disk.");
                fsn = FSNamesystem.LoadFromDisk(conf);
                // Make sure the image loaded including our edit.
                Log.Info("Checking reloaded image.");
                CheckEditExists(fsn, 1);
                Log.Info("Reloaded image is good.");
            }
            finally
            {
                if (rootDir.Exists())
                {
                    fs.SetPermission(rootPath, permissionAll);
                }
                if (fsn != null)
                {
                    try
                    {
                        fsn.Close();
                    }
                    catch (Exception t)
                    {
                        Log.Fatal("Failed to shut down", t);
                    }
                }
            }
        }