Beispiel #1
0
        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();
                }
            }
        }
Beispiel #2
0
        public static FSEditLog SpyOnEditLog(NameNode nn)
        {
            FSEditLog spyEditLog = Org.Mockito.Mockito.Spy(nn.GetNamesystem().GetFSImage().GetEditLog
                                                               ());

            DFSTestUtil.SetEditLogForTesting(nn.GetNamesystem(), spyEditLog);
            EditLogTailer tailer = nn.GetNamesystem().GetEditLogTailer();

            if (tailer != null)
            {
                tailer.SetEditLog(spyEditLog);
            }
            return(spyEditLog);
        }
Beispiel #3
0
        /// <exception cref="System.IO.IOException"/>
        internal static void TestNameNodeRecoveryImpl(TestNameNodeRecovery.Corruptor corruptor
                                                      , bool finalize)
        {
            string TestPath     = "/test/path/dir";
            string TestPath2    = "/second/dir";
            bool   needRecovery = corruptor.NeedRecovery(finalize);
            // start a cluster
            Configuration conf = new HdfsConfiguration();

            SetupRecoveryTestConf(conf);
            MiniDFSCluster cluster = null;
            FileSystem     fileSys = null;

            Storage.StorageDirectory sd = null;
            try
            {
                cluster = new MiniDFSCluster.Builder(conf).NumDataNodes(0).ManageNameDfsDirs(false
                                                                                             ).Build();
                cluster.WaitActive();
                if (!finalize)
                {
                    // Normally, the in-progress edit log would be finalized by
                    // FSEditLog#endCurrentLogSegment.  For testing purposes, we
                    // disable that here.
                    FSEditLog spyLog = Org.Mockito.Mockito.Spy(cluster.GetNameNode().GetFSImage().GetEditLog
                                                                   ());
                    Org.Mockito.Mockito.DoNothing().When(spyLog).EndCurrentLogSegment(true);
                    DFSTestUtil.SetEditLogForTesting(cluster.GetNamesystem(), spyLog);
                }
                fileSys = cluster.GetFileSystem();
                FSNamesystem namesystem = cluster.GetNamesystem();
                FSImage      fsimage    = namesystem.GetFSImage();
                fileSys.Mkdirs(new Path(TestPath));
                fileSys.Mkdirs(new Path(TestPath2));
                sd = fsimage.GetStorage().DirIterator(NNStorage.NameNodeDirType.Edits).Next();
            }
            finally
            {
                if (cluster != null)
                {
                    cluster.Shutdown();
                }
            }
            FilePath editFile = FSImageTestUtil.FindLatestEditsLog(sd).GetFile();

            NUnit.Framework.Assert.IsTrue("Should exist: " + editFile, editFile.Exists());
            // Corrupt the edit log
            Log.Info("corrupting edit log file '" + editFile + "'");
            corruptor.Corrupt(editFile);
            // If needRecovery == true, make sure that we can't start the
            // cluster normally before recovery
            cluster = null;
            try
            {
                Log.Debug("trying to start normally (this should fail)...");
                cluster = new MiniDFSCluster.Builder(conf).NumDataNodes(0).EnableManagedDfsDirsRedundancy
                              (false).Format(false).Build();
                cluster.WaitActive();
                cluster.Shutdown();
                if (needRecovery)
                {
                    NUnit.Framework.Assert.Fail("expected the corrupted edit log to prevent normal startup"
                                                );
                }
            }
            catch (IOException e)
            {
                if (!needRecovery)
                {
                    Log.Error("Got unexpected failure with " + corruptor.GetName() + corruptor, e);
                    NUnit.Framework.Assert.Fail("got unexpected exception " + e.Message);
                }
            }
            finally
            {
                if (cluster != null)
                {
                    cluster.Shutdown();
                }
            }
            // Perform NameNode recovery.
            // Even if there was nothing wrong previously (needRecovery == false),
            // this should still work fine.
            cluster = null;
            try
            {
                Log.Debug("running recovery...");
                cluster = new MiniDFSCluster.Builder(conf).NumDataNodes(0).EnableManagedDfsDirsRedundancy
                              (false).Format(false).StartupOption(recoverStartOpt).Build();
            }
            catch (IOException e)
            {
                NUnit.Framework.Assert.Fail("caught IOException while trying to recover. " + "message was "
                                            + e.Message + "\nstack trace\n" + StringUtils.StringifyException(e));
            }
            finally
            {
                if (cluster != null)
                {
                    cluster.Shutdown();
                }
            }
            // Make sure that we can start the cluster normally after recovery
            cluster = null;
            try
            {
                Log.Debug("starting cluster normally after recovery...");
                cluster = new MiniDFSCluster.Builder(conf).NumDataNodes(0).EnableManagedDfsDirsRedundancy
                              (false).Format(false).Build();
                Log.Debug("successfully recovered the " + corruptor.GetName() + " corrupted edit log"
                          );
                cluster.WaitActive();
                NUnit.Framework.Assert.IsTrue(cluster.GetFileSystem().Exists(new Path(TestPath)));
            }
            catch (IOException e)
            {
                NUnit.Framework.Assert.Fail("failed to recover.  Error message: " + e.Message);
            }
            finally
            {
                if (cluster != null)
                {
                    cluster.Shutdown();
                }
            }
        }
        public virtual void TestWhileOpenRenameParent()
        {
            Configuration conf        = new HdfsConfiguration();
            int           MaxIdleTime = 2000;

            // 2s
            conf.SetInt("ipc.client.connection.maxidletime", MaxIdleTime);
            conf.SetInt(DFSConfigKeys.DfsNamenodeHeartbeatRecheckIntervalKey, 1000);
            conf.SetInt(DFSConfigKeys.DfsHeartbeatIntervalKey, 1);
            conf.SetInt(DFSConfigKeys.DfsNamenodeSafemodeThresholdPctKey, 1);
            conf.SetInt(DFSConfigKeys.DfsBlockSizeKey, TestFileCreation.blockSize);
            // create cluster
            System.Console.Out.WriteLine("Test 1*****************************");
            MiniDFSCluster cluster = new MiniDFSCluster.Builder(conf).Build();
            FileSystem     fs      = null;

            try
            {
                cluster.WaitActive();
                fs = cluster.GetFileSystem();
                // Normally, the in-progress edit log would be finalized by
                // FSEditLog#endCurrentLogSegment.  For testing purposes, we
                // disable that here.
                FSEditLog spyLog = Org.Mockito.Mockito.Spy(cluster.GetNameNode().GetFSImage().GetEditLog
                                                               ());
                Org.Mockito.Mockito.DoNothing().When(spyLog).EndCurrentLogSegment(Org.Mockito.Mockito
                                                                                  .AnyBoolean());
                DFSTestUtil.SetEditLogForTesting(cluster.GetNamesystem(), spyLog);
                int nnport = cluster.GetNameNodePort();
                // create file1.
                Path dir1  = new Path("/user/a+b/dir1");
                Path file1 = new Path(dir1, "file1");
                FSDataOutputStream stm1 = TestFileCreation.CreateFile(fs, file1, 1);
                System.Console.Out.WriteLine("testFileCreationDeleteParent: " + "Created file " +
                                             file1);
                TestFileCreation.WriteFile(stm1);
                stm1.Hflush();
                // create file2.
                Path dir2  = new Path("/user/dir2");
                Path file2 = new Path(dir2, "file2");
                FSDataOutputStream stm2 = TestFileCreation.CreateFile(fs, file2, 1);
                System.Console.Out.WriteLine("testFileCreationDeleteParent: " + "Created file " +
                                             file2);
                TestFileCreation.WriteFile(stm2);
                stm2.Hflush();
                // move dir1 while file1 is open
                Path dir3 = new Path("/user/dir3");
                fs.Mkdirs(dir3);
                fs.Rename(dir1, dir3);
                // create file3
                Path file3 = new Path(dir3, "file3");
                FSDataOutputStream stm3 = fs.Create(file3);
                fs.Rename(file3, new Path(dir3, "bozo"));
                // Get a new block for the file.
                TestFileCreation.WriteFile(stm3, TestFileCreation.blockSize + 1);
                stm3.Hflush();
                // Stop the NameNode before closing the files.
                // This will ensure that the write leases are still active and present
                // in the edit log.  Simiarly, there should be a pending ADD_BLOCK_OP
                // for file3, since we just added a block to that file.
                cluster.GetNameNode().Stop();
                // Restart cluster with the same namenode port as before.
                cluster.Shutdown();
                try
                {
                    Sharpen.Thread.Sleep(2 * MaxIdleTime);
                }
                catch (Exception)
                {
                }
                cluster = new MiniDFSCluster.Builder(conf).NameNodePort(nnport).Format(false).Build
                              ();
                cluster.WaitActive();
                // restart cluster yet again. This triggers the code to read in
                // persistent leases from the edit log.
                cluster.Shutdown();
                try
                {
                    Sharpen.Thread.Sleep(5000);
                }
                catch (Exception)
                {
                }
                cluster = new MiniDFSCluster.Builder(conf).NameNodePort(nnport).Format(false).Build
                              ();
                cluster.WaitActive();
                fs = cluster.GetFileSystem();
                Path newfile = new Path("/user/dir3/dir1", "file1");
                NUnit.Framework.Assert.IsTrue(!fs.Exists(file1));
                NUnit.Framework.Assert.IsTrue(fs.Exists(file2));
                NUnit.Framework.Assert.IsTrue(fs.Exists(newfile));
                CheckFullFile(fs, newfile);
            }
            finally
            {
                fs.Close();
                cluster.Shutdown();
            }
        }