/// <exception cref="System.IO.IOException"/>
            public virtual FSEditLog MockEditLog(NNStorageRetentionManager.StoragePurger purger
                                                 )
            {
                IList <JournalManager> jms        = Lists.NewArrayList();
                JournalSet             journalSet = new JournalSet(0);

                foreach (TestNNStorageRetentionManager.TestCaseDescription.FakeRoot root in this.
                         dirRoots.Values)
                {
                    if (!root.type.IsOfType(NNStorage.NameNodeDirType.Edits))
                    {
                        continue;
                    }
                    // passing null NNStorage for unit test because it does not use it
                    FileJournalManager fjm = new FileJournalManager(this._enclosing.conf, root.MockStorageDir
                                                                        (), null);
                    fjm.purger = purger;
                    jms.AddItem(fjm);
                    journalSet.Add(fjm, false);
                }
                FSEditLog mockLog = Org.Mockito.Mockito.Mock <FSEditLog>();

                Org.Mockito.Mockito.DoAnswer(new _Answer_350(jms)).When(mockLog).PurgeLogsOlderThan
                    (Org.Mockito.Mockito.AnyLong());
                Org.Mockito.Mockito.DoAnswer(new _Answer_365(journalSet)).When(mockLog).SelectInputStreams
                    (Org.Mockito.Mockito.AnyCollection(), Org.Mockito.Mockito.AnyLong(), Org.Mockito.Mockito
                    .AnyBoolean());
                return(mockLog);
            }
        /// <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]);
        }
Пример #3
0
 public EditLogTailer(FSNamesystem namesystem, Configuration conf)
 {
     this.tailerThread = new EditLogTailer.EditLogTailerThread(this);
     this.conf         = conf;
     this.namesystem   = namesystem;
     this.editLog      = namesystem.GetEditLog();
     lastLoadTimeMs    = Time.MonotonicNow();
     logRollPeriodMs   = conf.GetInt(DFSConfigKeys.DfsHaLogrollPeriodKey, DFSConfigKeys.
                                     DfsHaLogrollPeriodDefault) * 1000;
     if (logRollPeriodMs >= 0)
     {
         this.activeAddr = GetActiveNodeAddress();
         Preconditions.CheckArgument(activeAddr.Port > 0, "Active NameNode must have an IPC port configured. "
                                     + "Got address '%s'", activeAddr);
         Log.Info("Will roll logs on active node at " + activeAddr + " every " + (logRollPeriodMs
                                                                                  / 1000) + " seconds.");
     }
     else
     {
         Log.Info("Not going to trigger log rolls on active node because " + DFSConfigKeys
                  .DfsHaLogrollPeriodKey + " is negative.");
     }
     sleepTimeMs = conf.GetInt(DFSConfigKeys.DfsHaTaileditsPeriodKey, DFSConfigKeys.DfsHaTaileditsPeriodDefault
                               ) * 1000;
     Log.Debug("logRollPeriodMs=" + logRollPeriodMs + " sleepTime=" + sleepTimeMs);
 }
Пример #4
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());
        }
Пример #5
0
        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();
                }
            }
        }
Пример #6
0
        public static JournalSet SpyOnJournalSet(NameNode nn)
        {
            FSEditLog  editLog = nn.GetFSImage().GetEditLog();
            JournalSet js      = Org.Mockito.Mockito.Spy(editLog.GetJournalSet());

            editLog.SetJournalSetForTesting(js);
            return(js);
        }
Пример #7
0
        internal static void AddFiles(FSEditLog editLog, int numFiles, short replication,
                                      int blocksPerFile, long startingBlockId, long blockSize, FileNameGenerator nameGenerator
                                      )
        {
            PermissionStatus p = new PermissionStatus("joeDoe", "people", new FsPermission((short
                                                                                            )0x1ff));
            INodeId        inodeId  = new INodeId();
            INodeDirectory dirInode = new INodeDirectory(inodeId.NextValue(), null, p, 0L);

            editLog.LogMkDir(BasePath, dirInode);
            BlockInfoContiguous[] blocks = new BlockInfoContiguous[blocksPerFile];
            for (int iB = 0; iB < blocksPerFile; ++iB)
            {
                blocks[iB] = new BlockInfoContiguous(new Block(0, blockSize, BlockGenerationStamp
                                                               ), replication);
            }
            long currentBlockId = startingBlockId;
            long bidAtSync      = startingBlockId;

            for (int iF = 0; iF < numFiles; iF++)
            {
                for (int iB_1 = 0; iB_1 < blocksPerFile; ++iB_1)
                {
                    blocks[iB_1].SetBlockId(currentBlockId++);
                }
                INodeFile inode = new INodeFile(inodeId.NextValue(), null, p, 0L, 0L, blocks, replication
                                                , blockSize, unchecked ((byte)0));
                inode.ToUnderConstruction(string.Empty, string.Empty);
                // Append path to filename with information about blockIDs
                string path = "_" + iF + "_B" + blocks[0].GetBlockId() + "_to_B" + blocks[blocksPerFile
                                                                                          - 1].GetBlockId() + "_";
                string filePath = nameGenerator.GetNextFileName(string.Empty);
                filePath = filePath + path;
                // Log the new sub directory in edits
                if ((iF % nameGenerator.GetFilesPerDirectory()) == 0)
                {
                    string currentDir = nameGenerator.GetCurrentDir();
                    dirInode = new INodeDirectory(inodeId.NextValue(), null, p, 0L);
                    editLog.LogMkDir(currentDir, dirInode);
                }
                INodeFile fileUc = new INodeFile(inodeId.NextValue(), null, p, 0L, 0L, BlockInfoContiguous
                                                 .EmptyArray, replication, blockSize);
                fileUc.ToUnderConstruction(string.Empty, string.Empty);
                editLog.LogOpenFile(filePath, fileUc, false, false);
                editLog.LogCloseFile(filePath, inode);
                if (currentBlockId - bidAtSync >= 2000)
                {
                    // sync every 2K blocks
                    editLog.LogSync();
                    bidAtSync = currentBlockId;
                }
            }
            System.Console.Out.WriteLine("Created edits log in directory " + edits_dir);
            System.Console.Out.WriteLine(" containing " + numFiles + " File-Creates, each file with "
                                         + blocksPerFile + " blocks");
            System.Console.Out.WriteLine(" blocks range: " + startingBlockId + " to " + (currentBlockId
                                                                                         - 1));
        }
Пример #8
0
        /// <exception cref="System.IO.IOException"/>
        public virtual void TestResolveReservedPath()
        {
            FSNamesystem fsn     = SetupFileSystem();
            FSEditLog    editlog = fsn.GetEditLog();

            fsn.GetBlockLocations("dummy", ReservedPath, 0, 1024);
            Org.Mockito.Mockito.Verify(editlog).LogTimes(Matchers.Eq(FilePath), Matchers.AnyLong
                                                             (), Matchers.AnyLong());
            fsn.Close();
        }
Пример #9
0
        /// <exception cref="System.IO.IOException"/>
        private static FSNamesystem GetMockNamesystem()
        {
            FSImage   fsImage = Org.Mockito.Mockito.Mock <FSImage>();
            FSEditLog editLog = Org.Mockito.Mockito.Mock <FSEditLog>();

            Org.Mockito.Mockito.DoReturn(editLog).When(fsImage).GetEditLog();
            FSNamesystem fsn = new FSNamesystem(conf, fsImage);

            fsn.SetImageLoaded(fsIsReady);
            return(fsn);
        }
Пример #10
0
        /// <exception cref="System.IO.IOException"/>
        private TestFailureToReadEdits.LimitedEditLogAnswer CauseFailureOnEditLogRead()
        {
            FSEditLog spyEditLog = NameNodeAdapter.SpyOnEditLog(nn1);

            TestFailureToReadEdits.LimitedEditLogAnswer answer = new TestFailureToReadEdits.LimitedEditLogAnswer
                                                                     ();
            Org.Mockito.Mockito.DoAnswer(answer).When(spyEditLog).SelectInputStreams(Matchers.AnyLong
                                                                                         (), Matchers.AnyLong(), (MetaRecoveryContext)Matchers.AnyObject(), Matchers.AnyBoolean
                                                                                         ());
            return(answer);
        }
Пример #11
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();
                }
            }
        }
Пример #12
0
        /// <exception cref="System.IO.IOException"/>
        public virtual void TestGetBlockLocationsRacingWithDelete()
        {
            FSNamesystem fsn     = Org.Mockito.Mockito.Spy(SetupFileSystem());
            FSDirectory  fsd     = fsn.GetFSDirectory();
            FSEditLog    editlog = fsn.GetEditLog();

            Org.Mockito.Mockito.DoAnswer(new _Answer_67(fsd)).When(fsn).WriteLock();
            fsn.GetBlockLocations("dummy", ReservedPath, 0, 1024);
            Org.Mockito.Mockito.Verify(editlog, Org.Mockito.Mockito.Never()).LogTimes(Matchers.AnyString
                                                                                          (), Matchers.AnyLong(), Matchers.AnyLong());
            fsn.Close();
        }
Пример #13
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);
        }
Пример #14
0
        /// <exception cref="System.IO.IOException"/>
        public virtual void TestGetBlockLocationsRacingWithRename()
        {
            FSNamesystem fsn     = Org.Mockito.Mockito.Spy(SetupFileSystem());
            FSDirectory  fsd     = fsn.GetFSDirectory();
            FSEditLog    editlog = fsn.GetEditLog();
            string       DstPath = "/bar";

            bool[] renamed = new bool[1];
            Org.Mockito.Mockito.DoAnswer(new _Answer_92(renamed, fsd, DstPath)).When(fsn).WriteLock
                ();
            fsn.GetBlockLocations("dummy", ReservedPath, 0, 1024);
            Org.Mockito.Mockito.Verify(editlog).LogTimes(Matchers.Eq(DstPath), Matchers.AnyLong
                                                             (), Matchers.AnyLong());
            fsn.Close();
        }
Пример #15
0
        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());
        }
Пример #16
0
        /// <summary>Create an unfinalized edit log for testing purposes</summary>
        /// <param name="testDir">Directory to create the edit log in</param>
        /// <param name="numTx">Number of transactions to add to the new edit log</param>
        /// <param name="offsetToTxId">
        /// A map from transaction IDs to offsets in the
        /// edit log file.
        /// </param>
        /// <returns>The new edit log file name.</returns>
        /// <exception cref="System.IO.IOException"/>
        private static FilePath PrepareUnfinalizedTestEditLog(FilePath testDir, int numTx
                                                              , SortedDictionary <long, long> offsetToTxId)
        {
            FilePath inProgressFile = new FilePath(testDir, NNStorage.GetInProgressEditsFileName
                                                       (1));
            FSEditLog fsel   = null;
            FSEditLog spyLog = null;

            try
            {
                fsel   = FSImageTestUtil.CreateStandaloneEditLog(testDir);
                spyLog = Org.Mockito.Mockito.Spy(fsel);
                // Normally, the in-progress edit log would be finalized by
                // FSEditLog#endCurrentLogSegment.  For testing purposes, we
                // disable that here.
                Org.Mockito.Mockito.DoNothing().When(spyLog).EndCurrentLogSegment(true);
                spyLog.OpenForWrite();
                NUnit.Framework.Assert.IsTrue("should exist: " + inProgressFile, inProgressFile.Exists
                                                  ());
                for (int i = 0; i < numTx; i++)
                {
                    long trueOffset = GetNonTrailerLength(inProgressFile);
                    long thisTxId   = spyLog.GetLastWrittenTxId() + 1;
                    offsetToTxId[trueOffset] = thisTxId;
                    System.Console.Error.WriteLine("txid " + thisTxId + " at offset " + trueOffset);
                    spyLog.LogDelete("path" + i, i, false);
                    spyLog.LogSync();
                }
            }
            finally
            {
                if (spyLog != null)
                {
                    spyLog.Close();
                }
                else
                {
                    if (fsel != null)
                    {
                        fsel.Close();
                    }
                }
            }
            return(inProgressFile);
        }
Пример #17
0
        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());
        }
Пример #18
0
        /// <summary>
        /// Create an aborted in-progress log in the given directory, containing
        /// only a specified number of "mkdirs" operations.
        /// </summary>
        /// <exception cref="System.IO.IOException"/>
        public static void CreateAbortedLogWithMkdirs(FilePath editsLogDir, int numDirs,
                                                      long firstTxId, long newInodeId)
        {
            FSEditLog editLog = FSImageTestUtil.CreateStandaloneEditLog(editsLogDir);

            editLog.SetNextTxId(firstTxId);
            editLog.OpenForWrite();
            PermissionStatus perms = PermissionStatus.CreateImmutable("fakeuser", "fakegroup"
                                                                      , FsPermission.CreateImmutable((short)0x1ed));

            for (int i = 1; i <= numDirs; i++)
            {
                string         dirName = "dir" + i;
                INodeDirectory dir     = new INodeDirectory(newInodeId + i - 1, DFSUtil.String2Bytes(
                                                                dirName), perms, 0L);
                editLog.LogMkDir("/" + dirName, dir);
            }
            editLog.LogSync();
            editLog.AbortCurrentLogSegment();
        }
Пример #19
0
        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());
        }
Пример #20
0
        /// <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);
        }
Пример #21
0
            // add a bunch of transactions.
            public virtual void Run()
            {
                FSEditLog editLog = namesystem.GetEditLog();

                for (int i = 0; i < numTransactions; i++)
                {
                    try
                    {
                        string renewer = UserGroupInformation.GetLoginUser().GetUserName();
                        Org.Apache.Hadoop.Security.Token.Token <DelegationTokenIdentifier> token = namesystem
                                                                                                   .GetDelegationToken(new Text(renewer));
                        namesystem.RenewDelegationToken(token);
                        namesystem.CancelDelegationToken(token);
                        editLog.LogSync();
                    }
                    catch (IOException e)
                    {
                        System.Console.Out.WriteLine("Transaction " + i + " encountered exception " + e);
                    }
                }
            }
Пример #22
0
        /// <summary>
        /// Return a standalone instance of FSEditLog that will log into the given
        /// log directory.
        /// </summary>
        /// <remarks>
        /// Return a standalone instance of FSEditLog that will log into the given
        /// log directory. The returned instance is not yet opened.
        /// </remarks>
        /// <exception cref="System.IO.IOException"/>
        public static FSEditLog CreateStandaloneEditLog(FilePath logDir)
        {
            NUnit.Framework.Assert.IsTrue(logDir.Mkdirs() || logDir.Exists());
            if (!FileUtil.FullyDeleteContents(logDir))
            {
                throw new IOException("Unable to delete contents of " + logDir);
            }
            NNStorage storage = Org.Mockito.Mockito.Mock <NNStorage>();

            Storage.StorageDirectory sd = FSImageTestUtil.MockStorageDirectory(logDir, NNStorage.NameNodeDirType
                                                                               .Edits);
            IList <Storage.StorageDirectory> sds = Lists.NewArrayList(sd);

            Org.Mockito.Mockito.DoReturn(sds).When(storage).DirIterable(NNStorage.NameNodeDirType
                                                                        .Edits);
            Org.Mockito.Mockito.DoReturn(sd).When(storage).GetStorageDirectory(Matchers.AnyObject
                                                                               <URI>());
            FSEditLog editLog = new FSEditLog(new Configuration(), storage, ImmutableList.Of(
                                                  logDir.ToURI()));

            editLog.InitJournalsForWrite();
            return(editLog);
        }
Пример #23
0
        public virtual void SetUp()
        {
            conf = new Configuration();
            // Stall the standby checkpointer in two ways
            conf.SetLong(DFSConfigKeys.DfsNamenodeCheckpointPeriodKey, long.MaxValue);
            conf.SetLong(DFSConfigKeys.DfsNamenodeCheckpointTxnsKey, 20);
            // Make it autoroll after 10 edits
            conf.SetFloat(DFSConfigKeys.DfsNamenodeEditLogAutorollMultiplierThreshold, 0.5f);
            conf.SetInt(DFSConfigKeys.DfsNamenodeEditLogAutorollCheckIntervalMs, 100);
            int retryCount = 0;

            while (true)
            {
                try
                {
                    int basePort = 10060 + random.Next(100) * 2;
                    MiniDFSNNTopology topology = new MiniDFSNNTopology().AddNameservice(new MiniDFSNNTopology.NSConf
                                                                                            ("ns1").AddNN(new MiniDFSNNTopology.NNConf("nn1").SetHttpPort(basePort)).AddNN(new
                                                                                                                                                                           MiniDFSNNTopology.NNConf("nn2").SetHttpPort(basePort + 1)));
                    cluster = new MiniDFSCluster.Builder(conf).NnTopology(topology).NumDataNodes(0).Build
                                  ();
                    cluster.WaitActive();
                    nn0 = cluster.GetNameNode(0);
                    fs  = HATestUtil.ConfigureFailoverFs(cluster, conf);
                    cluster.TransitionToActive(0);
                    fs      = cluster.GetFileSystem(0);
                    editLog = nn0.GetNamesystem().GetEditLog();
                    ++retryCount;
                    break;
                }
                catch (BindException)
                {
                    Log.Info("Set up MiniDFSCluster failed due to port conflicts, retry " + retryCount
                             + " times");
                }
            }
        }
Пример #24
0
        public static void AbortEditLogs(NameNode nn)
        {
            FSEditLog el = nn.GetFSImage().GetEditLog();

            el.AbortCurrentLogSegment();
        }
Пример #25
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();
                }
            }
        }
Пример #26
0
        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();
                }
            }
        }
Пример #27
0
        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();
                }
            }
        }
Пример #28
0
        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();
            }
        }
Пример #29
0
        /// <param name="args">arguments</param>
        /// <exception cref="System.IO.IOException"></exception>
        public static void Main(string[] args)
        {
            long  startingBlockId  = 1;
            int   numFiles         = 0;
            short replication      = 1;
            int   numBlocksPerFile = 0;
            long  blockSize        = 10;

            if (args.Length == 0)
            {
                PrintUsageExit();
            }
            for (int i = 0; i < args.Length; i++)
            {
                // parse command line
                if (args[i].Equals("-h"))
                {
                    PrintUsageExit();
                }
                if (args[i].Equals("-f"))
                {
                    if (i + 3 >= args.Length || args[i + 1].StartsWith("-") || args[i + 2].StartsWith
                            ("-") || args[i + 3].StartsWith("-"))
                    {
                        PrintUsageExit("Missing num files, starting block and/or number of blocks");
                    }
                    numFiles         = System.Convert.ToInt32(args[++i]);
                    startingBlockId  = System.Convert.ToInt32(args[++i]);
                    numBlocksPerFile = System.Convert.ToInt32(args[++i]);
                    if (numFiles <= 0 || numBlocksPerFile <= 0)
                    {
                        PrintUsageExit("numFiles and numBlocksPerFile most be greater than 0");
                    }
                }
                else
                {
                    if (args[i].Equals("-l"))
                    {
                        if (i + 1 >= args.Length)
                        {
                            PrintUsageExit("Missing block length");
                        }
                        blockSize = long.Parse(args[++i]);
                    }
                    else
                    {
                        if (args[i].Equals("-r") || args[i + 1].StartsWith("-"))
                        {
                            if (i + 1 >= args.Length)
                            {
                                PrintUsageExit("Missing replication factor");
                            }
                            replication = short.ParseShort(args[++i]);
                        }
                        else
                        {
                            if (args[i].Equals("-d"))
                            {
                                if (i + 1 >= args.Length || args[i + 1].StartsWith("-"))
                                {
                                    PrintUsageExit("Missing edits logs directory");
                                }
                                edits_dir = args[++i];
                            }
                            else
                            {
                                PrintUsageExit();
                            }
                        }
                    }
                }
            }
            FilePath editsLogDir     = new FilePath(edits_dir);
            FilePath subStructureDir = new FilePath(edits_dir + "/" + Storage.StorageDirCurrent
                                                    );

            if (!editsLogDir.Exists())
            {
                if (!editsLogDir.Mkdir())
                {
                    System.Console.Out.WriteLine("cannot create " + edits_dir);
                    System.Environment.Exit(-1);
                }
            }
            if (!subStructureDir.Exists())
            {
                if (!subStructureDir.Mkdir())
                {
                    System.Console.Out.WriteLine("cannot create subdirs of " + edits_dir);
                    System.Environment.Exit(-1);
                }
            }
            FileNameGenerator nameGenerator = new FileNameGenerator(BasePath, 100);
            FSEditLog         editLog       = FSImageTestUtil.CreateStandaloneEditLog(editsLogDir);

            editLog.OpenForWrite();
            AddFiles(editLog, numFiles, replication, numBlocksPerFile, startingBlockId, blockSize
                     , nameGenerator);
            editLog.LogSync();
            editLog.Close();
        }
Пример #30
0
 public virtual void SetEditLog(FSEditLog editLog)
 {
     this.editLog = editLog;
 }