/// <summary>
        /// Tests setting the rpc port to the same as the web port to test that
        /// an exception
        /// is thrown when trying to re-use the same port
        /// </summary>
        /// <exception cref="System.IO.IOException"/>
        public virtual void TestThatMatchingRPCandHttpPortsThrowException()
        {
            NameNode nameNode = null;

            try
            {
                Configuration conf    = new HdfsConfiguration();
                FilePath      nameDir = new FilePath(MiniDFSCluster.GetBaseDirectory(), "name");
                conf.Set(DFSConfigKeys.DfsNamenodeNameDirKey, nameDir.GetAbsolutePath());
                Random rand = new Random();
                int    port = 30000 + rand.Next(30000);
                // set both of these to the same port. It should fail.
                FileSystem.SetDefaultUri(conf, "hdfs://localhost:" + port);
                conf.Set(DFSConfigKeys.DfsNamenodeHttpAddressKey, "127.0.0.1:" + port);
                DFSTestUtil.FormatNameNode(conf);
                nameNode = new NameNode(conf);
            }
            finally
            {
                if (nameNode != null)
                {
                    nameNode.Stop();
                }
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Tests that an edits log created using CreateEditsLog is valid and can be
        /// loaded successfully by a namenode.
        /// </summary>
        /// <exception cref="System.Exception"/>
        public virtual void TestCanLoadCreatedEditsLog()
        {
            // Format namenode.
            HdfsConfiguration conf    = new HdfsConfiguration();
            FilePath          nameDir = new FilePath(HdfsDir, "name");

            conf.Set(DFSConfigKeys.DfsNamenodeNameDirKey, Util.FileAsURI(nameDir).ToString());
            DFSTestUtil.FormatNameNode(conf);
            // Call CreateEditsLog and move the resulting edits to the name dir.
            CreateEditsLog.Main(new string[] { "-f", "1000", "0", "1", "-d", TestDir.GetAbsolutePath
                                                   () });
            Path        editsWildcard = new Path(TestDir.GetAbsolutePath(), "*");
            FileContext localFc       = FileContext.GetLocalFSFileContext();

            foreach (FileStatus edits in localFc.Util().GlobStatus(editsWildcard))
            {
                Path src = edits.GetPath();
                Path dst = new Path(new FilePath(nameDir, "current").GetAbsolutePath(), src.GetName
                                        ());
                localFc.Rename(src, dst);
            }
            // Start a namenode to try to load the edits.
            cluster = new MiniDFSCluster.Builder(conf).Format(false).ManageNameDfsDirs(false)
                      .WaitSafeMode(false).Build();
            cluster.WaitClusterUp();
        }
        public virtual void SetUp()
        {
            FileSystem.SetDefaultUri(Conf, "hdfs://localhost:0");
            Conf.Set(DFSConfigKeys.DfsNamenodeHttpAddressKey, "0.0.0.0:0");
            // Set properties to make HDFS aware of NodeGroup.
            Conf.Set(DFSConfigKeys.DfsBlockReplicatorClassnameKey, typeof(BlockPlacementPolicyWithNodeGroup
                                                                          ).FullName);
            Conf.Set(CommonConfigurationKeysPublic.NetTopologyImplKey, typeof(NetworkTopologyWithNodeGroup
                                                                              ).FullName);
            Conf.SetBoolean(DFSConfigKeys.DfsNamenodeAvoidStaleDatanodeForWriteKey, true);
            FilePath baseDir = PathUtils.GetTestDir(typeof(TestReplicationPolicyWithNodeGroup
                                                           ));

            Conf.Set(DFSConfigKeys.DfsNamenodeNameDirKey, new FilePath(baseDir, "name").GetPath
                         ());
            DFSTestUtil.FormatNameNode(Conf);
            namenode = new NameNode(Conf);
            BlockManager bm = namenode.GetNamesystem().GetBlockManager();

            replicator = bm.GetBlockPlacementPolicy();
            cluster    = bm.GetDatanodeManager().GetNetworkTopology();
            // construct network topology
            for (int i = 0; i < NumOfDatanodes; i++)
            {
                cluster.Add(dataNodes[i]);
            }
            SetupDataNodeCapacity();
        }
        /// <summary>
        /// HDFS-3013: NameNode format command doesn't pick up
        /// dfs.namenode.name.dir.NameServiceId configuration.
        /// </summary>
        /// <exception cref="System.IO.IOException"/>
        public virtual void TestGenericKeysForNameNodeFormat()
        {
            Configuration conf = new HdfsConfiguration();

            // Set ephemeral ports
            conf.Set(DFSConfigKeys.DfsNamenodeRpcAddressKey, "127.0.0.1:0");
            conf.Set(DFSConfigKeys.DfsNamenodeHttpAddressKey, "127.0.0.1:0");
            conf.Set(DFSConfigKeys.DfsNameservices, "ns1");
            // Set a nameservice-specific configuration for name dir
            FilePath dir = new FilePath(MiniDFSCluster.GetBaseDirectory(), "testGenericKeysForNameNodeFormat"
                                        );

            if (dir.Exists())
            {
                FileUtil.FullyDelete(dir);
            }
            conf.Set(DFSConfigKeys.DfsNamenodeNameDirKey + ".ns1", dir.GetAbsolutePath());
            // Format and verify the right dir is formatted.
            DFSTestUtil.FormatNameNode(conf);
            GenericTestUtils.AssertExists(dir);
            // Ensure that the same dir is picked up by the running NN
            NameNode nameNode = new NameNode(conf);

            nameNode.Stop();
        }
Ejemplo n.º 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();
                }
            }
        }
Ejemplo n.º 6
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();
                }
            }
        }
        public virtual void TestNNThroughput()
        {
            Configuration conf    = new HdfsConfiguration();
            FilePath      nameDir = new FilePath(MiniDFSCluster.GetBaseDirectory(), "name");

            conf.Set(DFSConfigKeys.DfsNamenodeNameDirKey, nameDir.GetAbsolutePath());
            FileSystem.SetDefaultUri(conf, "hdfs://localhost:" + 0);
            conf.Set(DFSConfigKeys.DfsNamenodeHttpAddressKey, "0.0.0.0:0");
            DFSTestUtil.FormatNameNode(conf);
            string[] args = new string[] { "-op", "all" };
            NNThroughputBenchmark.RunBenchmark(conf, Arrays.AsList(args));
        }
Ejemplo n.º 8
0
        public virtual void TestFSNamespaceClearLeases()
        {
            Configuration conf    = new HdfsConfiguration();
            FilePath      nameDir = new FilePath(MiniDFSCluster.GetBaseDirectory(), "name");

            conf.Set(DFSConfigKeys.DfsNamenodeNameDirKey, nameDir.GetAbsolutePath());
            NameNode.InitMetrics(conf, HdfsServerConstants.NamenodeRole.Namenode);
            DFSTestUtil.FormatNameNode(conf);
            FSNamesystem fsn      = FSNamesystem.LoadFromDisk(conf);
            LeaseManager leaseMan = fsn.GetLeaseManager();

            leaseMan.AddLease("client1", "importantFile");
            NUnit.Framework.Assert.AreEqual(1, leaseMan.CountLease());
            fsn.Clear();
            leaseMan = fsn.GetLeaseManager();
            NUnit.Framework.Assert.AreEqual(0, leaseMan.CountLease());
        }
Ejemplo n.º 9
0
        public virtual void TestCompression()
        {
            Log.Info("Test compressing image.");
            Configuration conf = new Configuration();

            FileSystem.SetDefaultUri(conf, "hdfs://localhost:0");
            conf.Set(DFSConfigKeys.DfsNamenodeHttpAddressKey, "127.0.0.1:0");
            FilePath base_dir = new FilePath(PathUtils.GetTestDir(GetType()), "dfs/");

            conf.Set(DFSConfigKeys.DfsNamenodeNameDirKey, new FilePath(base_dir, "name").GetPath
                         ());
            conf.SetBoolean(DFSConfigKeys.DfsPermissionsEnabledKey, false);
            DFSTestUtil.FormatNameNode(conf);
            // create an uncompressed image
            Log.Info("Create an uncompressed fsimage");
            NameNode namenode = new NameNode(conf);

            namenode.GetNamesystem().Mkdirs("/test", new PermissionStatus("hairong", null, FsPermission
                                                                          .GetDefault()), true);
            NamenodeProtocols nnRpc = namenode.GetRpcServer();

            NUnit.Framework.Assert.IsTrue(nnRpc.GetFileInfo("/test").IsDir());
            nnRpc.SetSafeMode(HdfsConstants.SafeModeAction.SafemodeEnter, false);
            nnRpc.SaveNamespace();
            namenode.Stop();
            namenode.Join();
            // compress image using default codec
            Log.Info("Read an uncomressed image and store it compressed using default codec."
                     );
            conf.SetBoolean(DFSConfigKeys.DfsImageCompressKey, true);
            CheckNameSpace(conf);
            // read image compressed using the default and compress it using Gzip codec
            Log.Info("Read a compressed image and store it using a different codec.");
            conf.Set(DFSConfigKeys.DfsImageCompressionCodecKey, "org.apache.hadoop.io.compress.GzipCodec"
                     );
            CheckNameSpace(conf);
            // read an image compressed in Gzip and store it uncompressed
            Log.Info("Read a compressed image and store it as uncompressed.");
            conf.SetBoolean(DFSConfigKeys.DfsImageCompressKey, false);
            CheckNameSpace(conf);
            // read an uncomrpessed image and store it uncompressed
            Log.Info("Read an uncompressed image and store it as uncompressed.");
            CheckNameSpace(conf);
        }
        public virtual void TestDNWithInvalidStorageWithHA()
        {
            MiniDFSNNTopology top = new MiniDFSNNTopology().AddNameservice(new MiniDFSNNTopology.NSConf
                                                                               ("ns1").AddNN(new MiniDFSNNTopology.NNConf("nn0").SetClusterId("cluster-1")).AddNN
                                                                               (new MiniDFSNNTopology.NNConf("nn1").SetClusterId("cluster-1")));

            top.SetFederation(true);
            MiniDFSCluster cluster = new MiniDFSCluster.Builder(conf).NnTopology(top).NumDataNodes
                                         (0).Build();

            try
            {
                cluster.StartDataNodes(conf, 1, true, null, null);
                // let the initialization be complete
                Sharpen.Thread.Sleep(10000);
                DataNode dn = cluster.GetDataNodes()[0];
                NUnit.Framework.Assert.IsTrue("Datanode should be running", dn.IsDatanodeUp());
                NUnit.Framework.Assert.AreEqual("BPOfferService should be running", 1, dn.GetAllBpOs
                                                    ().Length);
                MiniDFSCluster.DataNodeProperties dnProp = cluster.StopDataNode(0);
                cluster.GetNameNode(0).Stop();
                cluster.GetNameNode(1).Stop();
                Configuration nn1 = cluster.GetConfiguration(0);
                Configuration nn2 = cluster.GetConfiguration(1);
                // setting up invalid cluster
                HdfsServerConstants.StartupOption.Format.SetClusterId("cluster-2");
                DFSTestUtil.FormatNameNode(nn1);
                MiniDFSCluster.CopyNameDirs(FSNamesystem.GetNamespaceDirs(nn1), FSNamesystem.GetNamespaceDirs
                                                (nn2), nn2);
                cluster.RestartNameNode(0, false);
                cluster.RestartNameNode(1, false);
                cluster.RestartDataNode(dnProp);
                // let the initialization be complete
                Sharpen.Thread.Sleep(10000);
                dn = cluster.GetDataNodes()[0];
                NUnit.Framework.Assert.IsFalse("Datanode should have shutdown as only service failed"
                                               , dn.IsDatanodeUp());
            }
            finally
            {
                cluster.Shutdown();
            }
        }
Ejemplo n.º 11
0
        /// <summary>Verify namenode port usage.</summary>
        /// <exception cref="System.Exception"/>
        public virtual void RunTestNameNodePorts(bool withService)
        {
            NameNode nn = null;

            try
            {
                nn = StartNameNode(withService);
                // start another namenode on the same port
                Configuration conf2 = new HdfsConfiguration(config);
                conf2.Set(DFSConfigKeys.DfsNamenodeNameDirKey, Util.FileAsURI(new FilePath(hdfsDir
                                                                                           , "name2")).ToString());
                DFSTestUtil.FormatNameNode(conf2);
                bool started = CanStartNameNode(conf2);
                NUnit.Framework.Assert.IsFalse(started);
                // should fail
                // start on a different main port
                FileSystem.SetDefaultUri(conf2, "hdfs://" + ThisHost);
                started = CanStartNameNode(conf2);
                NUnit.Framework.Assert.IsFalse(started);
                // should fail again
                // reset conf2 since NameNode modifies it
                FileSystem.SetDefaultUri(conf2, "hdfs://" + ThisHost);
                // different http port
                conf2.Set(DFSConfigKeys.DfsNamenodeHttpAddressKey, ThisHost);
                started = CanStartNameNode(conf2);
                if (withService)
                {
                    NUnit.Framework.Assert.IsFalse("Should've failed on service port", started);
                    // reset conf2 since NameNode modifies it
                    FileSystem.SetDefaultUri(conf2, "hdfs://" + ThisHost);
                    conf2.Set(DFSConfigKeys.DfsNamenodeHttpAddressKey, ThisHost);
                    // Set Service address
                    conf2.Set(DFSConfigKeys.DfsNamenodeServiceRpcAddressKey, ThisHost);
                    started = CanStartNameNode(conf2);
                }
                NUnit.Framework.Assert.IsTrue(started);
            }
            finally
            {
                StopNameNode(nn);
            }
        }
Ejemplo n.º 12
0
 /// <summary>Start the namenode.</summary>
 /// <exception cref="System.IO.IOException"/>
 public virtual NameNode StartNameNode(bool withService)
 {
     hdfsDir = new FilePath(TestDataDir, "dfs");
     if (hdfsDir.Exists() && !FileUtil.FullyDelete(hdfsDir))
     {
         throw new IOException("Could not delete hdfs directory '" + hdfsDir + "'");
     }
     config = new HdfsConfiguration();
     config.Set(DFSConfigKeys.DfsNamenodeNameDirKey, Util.FileAsURI(new FilePath(hdfsDir
                                                                                 , "name1")).ToString());
     FileSystem.SetDefaultUri(config, "hdfs://" + ThisHost);
     if (withService)
     {
         NameNode.SetServiceAddress(config, ThisHost);
     }
     config.Set(DFSConfigKeys.DfsNamenodeHttpAddressKey, ThisHost);
     DFSTestUtil.FormatNameNode(config);
     string[] args = new string[] {  };
     // NameNode will modify config with the ports it bound to
     return(NameNode.CreateNameNode(args, config));
 }
Ejemplo n.º 13
0
        public static void SetupCluster()
        {
            Configuration conf = new HdfsConfiguration();

            string[] racks = new string[] { "/rack1", "/rack1", "/rack1", "/rack2", "/rack2",
                                            "/rack2" };
            storages  = DFSTestUtil.CreateDatanodeStorageInfos(racks);
            dataNodes = DFSTestUtil.ToDatanodeDescriptor(storages);
            FileSystem.SetDefaultUri(conf, "hdfs://localhost:0");
            conf.Set(DFSConfigKeys.DfsNamenodeHttpAddressKey, "0.0.0.0:0");
            FilePath baseDir = PathUtils.GetTestDir(typeof(TestReplicationPolicy));

            conf.Set(DFSConfigKeys.DfsNamenodeNameDirKey, new FilePath(baseDir, "name").GetPath
                         ());
            conf.SetBoolean(DFSConfigKeys.DfsNamenodeAvoidStaleDatanodeForReadKey, true);
            conf.SetBoolean(DFSConfigKeys.DfsNamenodeAvoidStaleDatanodeForWriteKey, true);
            conf.SetBoolean(DFSConfigKeys.DfsNamenodeReplicationConsiderloadKey, true);
            DFSTestUtil.FormatNameNode(conf);
            namenode = new NameNode(conf);
            int blockSize = 1024;

            dnrList   = new AList <DatanodeRegistration>();
            dnManager = namenode.GetNamesystem().GetBlockManager().GetDatanodeManager();
            // Register DNs
            for (int i = 0; i < 6; i++)
            {
                DatanodeRegistration dnr = new DatanodeRegistration(dataNodes[i], new StorageInfo
                                                                        (HdfsServerConstants.NodeType.DataNode), new ExportedBlockKeys(), VersionInfo.GetVersion
                                                                        ());
                dnrList.AddItem(dnr);
                dnManager.RegisterDatanode(dnr);
                dataNodes[i].GetStorageInfos()[0].SetUtilizationForTesting(2 * HdfsConstants.MinBlocksForWrite
                                                                           * blockSize, 0L, 2 * HdfsConstants.MinBlocksForWrite * blockSize, 0L);
                dataNodes[i].UpdateHeartbeat(BlockManagerTestUtil.GetStorageReportsForDatanode(dataNodes
                                                                                               [i]), 0L, 0L, 0, 0, null);
            }
        }
        /// <summary>
        /// Tests setting the rpc port to a different as the web port that an
        /// exception is NOT thrown
        /// </summary>
        /// <exception cref="System.IO.IOException"/>
        public virtual void TestThatDifferentRPCandHttpPortsAreOK()
        {
            Configuration conf    = new HdfsConfiguration();
            FilePath      nameDir = new FilePath(MiniDFSCluster.GetBaseDirectory(), "name");

            conf.Set(DFSConfigKeys.DfsNamenodeNameDirKey, nameDir.GetAbsolutePath());
            Random rand = new Random();

            // A few retries in case the ports we choose are in use.
            for (int i = 0; i < 5; ++i)
            {
                int port1 = 30000 + rand.Next(10000);
                int port2 = port1 + 1 + rand.Next(10000);
                FileSystem.SetDefaultUri(conf, "hdfs://localhost:" + port1);
                conf.Set(DFSConfigKeys.DfsNamenodeHttpAddressKey, "127.0.0.1:" + port2);
                DFSTestUtil.FormatNameNode(conf);
                NameNode nameNode = null;
                try
                {
                    nameNode = new NameNode(conf);
                    // should be OK!
                    break;
                }
                catch (BindException)
                {
                    continue;
                }
                finally
                {
                    // Port in use? Try another.
                    if (nameNode != null)
                    {
                        nameNode.Stop();
                    }
                }
            }
        }
Ejemplo n.º 15
0
        /// <exception cref="System.Exception"/>
        public virtual void TestSaveWhileEditsRolled()
        {
            Configuration conf = GetConf();

            NameNode.InitMetrics(conf, HdfsServerConstants.NamenodeRole.Namenode);
            DFSTestUtil.FormatNameNode(conf);
            FSNamesystem fsn = FSNamesystem.LoadFromDisk(conf);

            try
            {
                DoAnEdit(fsn, 1);
                CheckpointSignature sig = fsn.RollEditLog();
                Log.Warn("Checkpoint signature: " + sig);
                // Do another edit
                DoAnEdit(fsn, 2);
                // Save namespace
                fsn.SetSafeMode(HdfsConstants.SafeModeAction.SafemodeEnter);
                fsn.SaveNamespace();
                // Now shut down and restart the NN
                fsn.Close();
                fsn = null;
                // Start a new namesystem, which should be able to recover
                // the namespace from the previous incarnation.
                fsn = FSNamesystem.LoadFromDisk(conf);
                // Make sure the image loaded including our edits.
                CheckEditExists(fsn, 1);
                CheckEditExists(fsn, 2);
            }
            finally
            {
                if (fsn != null)
                {
                    fsn.Close();
                }
            }
        }
Ejemplo n.º 16
0
        /// <exception cref="System.Exception"/>
        public virtual void TestTxIdPersistence()
        {
            Configuration conf = GetConf();

            NameNode.InitMetrics(conf, HdfsServerConstants.NamenodeRole.Namenode);
            DFSTestUtil.FormatNameNode(conf);
            FSNamesystem fsn = FSNamesystem.LoadFromDisk(conf);

            try
            {
                // We have a BEGIN_LOG_SEGMENT txn to start
                NUnit.Framework.Assert.AreEqual(1, fsn.GetEditLog().GetLastWrittenTxId());
                DoAnEdit(fsn, 1);
                NUnit.Framework.Assert.AreEqual(2, fsn.GetEditLog().GetLastWrittenTxId());
                fsn.SetSafeMode(HdfsConstants.SafeModeAction.SafemodeEnter);
                fsn.SaveNamespace();
                // 2 more txns: END the first segment, BEGIN a new one
                NUnit.Framework.Assert.AreEqual(4, fsn.GetEditLog().GetLastWrittenTxId());
                // Shut down and restart
                fsn.GetFSImage().Close();
                fsn.Close();
                // 1 more txn to END that segment
                NUnit.Framework.Assert.AreEqual(5, fsn.GetEditLog().GetLastWrittenTxId());
                fsn = null;
                fsn = FSNamesystem.LoadFromDisk(conf);
                // 1 more txn to start new segment on restart
                NUnit.Framework.Assert.AreEqual(6, fsn.GetEditLog().GetLastWrittenTxId());
            }
            finally
            {
                if (fsn != null)
                {
                    fsn.Close();
                }
            }
        }
Ejemplo n.º 17
0
        /// <summary>Injects a failure on all storage directories while saving namespace.</summary>
        /// <param name="restoreStorageAfterFailure">
        /// if true, will try to save again after
        /// clearing the failure injection
        /// </param>
        /// <exception cref="System.Exception"/>
        public virtual void DoTestFailedSaveNamespace(bool restoreStorageAfterFailure)
        {
            Configuration conf = GetConf();

            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();

            storage.Close();
            // unlock any directories that FSNamesystem's initialization may have locked
            NNStorage spyStorage = Org.Mockito.Mockito.Spy(storage);

            originalImage.storage = spyStorage;
            FSImage spyImage = Org.Mockito.Mockito.Spy(originalImage);

            Whitebox.SetInternalState(fsn, "fsImage", spyImage);
            spyImage.storage.SetStorageDirectories(FSNamesystem.GetNamespaceDirs(conf), FSNamesystem
                                                   .GetNamespaceEditsDirs(conf));
            Org.Mockito.Mockito.DoThrow(new IOException("Injected fault: saveFSImage")).When(
                spyImage).SaveFSImage((SaveNamespaceContext)Matchers.AnyObject(), (Storage.StorageDirectory
                                                                                   )Matchers.AnyObject(), (NNStorage.NameNodeFile)Matchers.AnyObject());
            try
            {
                DoAnEdit(fsn, 1);
                // Save namespace
                fsn.SetSafeMode(HdfsConstants.SafeModeAction.SafemodeEnter);
                try
                {
                    fsn.SaveNamespace();
                    NUnit.Framework.Assert.Fail("saveNamespace did not fail even when all directories failed!"
                                                );
                }
                catch (IOException ioe)
                {
                    Log.Info("Got expected exception", ioe);
                }
                // Ensure that, if storage dirs come back online, things work again.
                if (restoreStorageAfterFailure)
                {
                    Org.Mockito.Mockito.Reset(spyImage);
                    spyStorage.SetRestoreFailedStorage(true);
                    fsn.SaveNamespace();
                    CheckEditExists(fsn, 1);
                }
                // Now shut down and restart the NN
                originalImage.Close();
                fsn.Close();
                fsn = null;
                // Start a new namesystem, which should be able to recover
                // the namespace from the previous incarnation.
                fsn = FSNamesystem.LoadFromDisk(conf);
                // Make sure the image loaded including our edits.
                CheckEditExists(fsn, 1);
            }
            finally
            {
                if (fsn != null)
                {
                    fsn.Close();
                }
            }
        }
Ejemplo n.º 18
0
        // Root scratch directory on local filesystem
        // The singleton master storage directory for Namenode
        // A checksum of the contents in namenodeStorage directory
        // The namespaceId of the namenodeStorage directory
        // The clusterId of the namenodeStorage directory
        // The blockpoolId of the namenodeStorage directory
        // The fsscTime of the namenodeStorage directory
        // The singleton master storage directory for Datanode
        // A checksum of the contents in datanodeStorage directory
        // A checksum of the contents in blockpool storage directory
        // A checksum of the contents in blockpool finalize storage directory
        // A checksum of the contents in blockpool rbw storage directory
        /// <summary>Initialize the data structures used by this class.</summary>
        /// <remarks>
        /// Initialize the data structures used by this class.
        /// IMPORTANT NOTE: This method must be called once before calling
        /// any other public method on this class.
        /// <p>
        /// Creates a singleton master populated storage
        /// directory for a Namenode (contains edits, fsimage,
        /// version, and time files) and a Datanode (contains version and
        /// block files).  This can be a lengthy operation.
        /// </remarks>
        /// <exception cref="System.Exception"/>
        public static void Initialize()
        {
            CreateEmptyDirs(new string[] { TestRootDir.ToString() });
            Configuration config = new HdfsConfiguration();

            config.Set(DFSConfigKeys.DfsNamenodeNameDirKey, namenodeStorage.ToString());
            config.Set(DFSConfigKeys.DfsNamenodeEditsDirKey, namenodeStorage.ToString());
            config.Set(DFSConfigKeys.DfsDatanodeDataDirKey, datanodeStorage.ToString());
            MiniDFSCluster cluster = null;
            string         bpid    = null;

            try
            {
                // format data-node
                CreateEmptyDirs(new string[] { datanodeStorage.ToString() });
                // format and start NameNode and start DataNode
                DFSTestUtil.FormatNameNode(config);
                cluster = new MiniDFSCluster.Builder(config).NumDataNodes(1).StartupOption(HdfsServerConstants.StartupOption
                                                                                           .Regular).Format(false).ManageDataDfsDirs(false).ManageNameDfsDirs(false).Build(
                    );
                NamenodeProtocols namenode = cluster.GetNameNodeRpc();
                namenodeStorageNamespaceID = namenode.VersionRequest().GetNamespaceID();
                namenodeStorageFsscTime    = namenode.VersionRequest().GetCTime();
                namenodeStorageClusterID   = namenode.VersionRequest().GetClusterID();
                namenodeStorageBlockPoolID = namenode.VersionRequest().GetBlockPoolID();
                FileSystem fs      = FileSystem.Get(config);
                Path       baseDir = new Path("/TestUpgrade");
                fs.Mkdirs(baseDir);
                // write some files
                int    bufferSize = 4096;
                byte[] buffer     = new byte[bufferSize];
                for (int i = 0; i < bufferSize; i++)
                {
                    buffer[i] = unchecked ((byte)((byte)('0') + i % 50));
                }
                WriteFile(fs, new Path(baseDir, "file1"), buffer, bufferSize);
                WriteFile(fs, new Path(baseDir, "file2"), buffer, bufferSize);
                // save image
                namenode.SetSafeMode(HdfsConstants.SafeModeAction.SafemodeEnter, false);
                namenode.SaveNamespace();
                namenode.SetSafeMode(HdfsConstants.SafeModeAction.SafemodeLeave, false);
                // write more files
                WriteFile(fs, new Path(baseDir, "file3"), buffer, bufferSize);
                WriteFile(fs, new Path(baseDir, "file4"), buffer, bufferSize);
                bpid = cluster.GetNamesystem(0).GetBlockPoolId();
            }
            finally
            {
                // shutdown
                if (cluster != null)
                {
                    cluster.Shutdown();
                }
                FileUtil.FullyDelete(new FilePath(namenodeStorage, "in_use.lock"));
                FileUtil.FullyDelete(new FilePath(datanodeStorage, "in_use.lock"));
            }
            namenodeStorageChecksum = ChecksumContents(HdfsServerConstants.NodeType.NameNode,
                                                       new FilePath(namenodeStorage, "current"), false);
            FilePath dnCurDir = new FilePath(datanodeStorage, "current");

            datanodeStorageChecksum = ChecksumContents(HdfsServerConstants.NodeType.DataNode,
                                                       dnCurDir, false);
            FilePath bpCurDir = new FilePath(BlockPoolSliceStorage.GetBpRoot(bpid, dnCurDir),
                                             "current");

            blockPoolStorageChecksum = ChecksumContents(HdfsServerConstants.NodeType.DataNode
                                                        , bpCurDir, false);
            FilePath bpCurFinalizeDir = new FilePath(BlockPoolSliceStorage.GetBpRoot(bpid, dnCurDir
                                                                                     ), "current/" + DataStorage.StorageDirFinalized);

            blockPoolFinalizedStorageChecksum = ChecksumContents(HdfsServerConstants.NodeType
                                                                 .DataNode, bpCurFinalizeDir, true);
            FilePath bpCurRbwDir = new FilePath(BlockPoolSliceStorage.GetBpRoot(bpid, dnCurDir
                                                                                ), "current/" + DataStorage.StorageDirRbw);

            blockPoolRbwStorageChecksum = ChecksumContents(HdfsServerConstants.NodeType.DataNode
                                                           , bpCurRbwDir, false);
        }
Ejemplo n.º 19
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);
                    }
                }
            }
        }
Ejemplo n.º 20
0
        /// <exception cref="System.Exception"/>
        public virtual void TestCancelSaveNamespace()
        {
            Configuration conf = GetConf();

            NameNode.InitMetrics(conf, HdfsServerConstants.NamenodeRole.Namenode);
            DFSTestUtil.FormatNameNode(conf);
            FSNamesystem fsn = FSNamesystem.LoadFromDisk(conf);
            // Replace the FSImage with a spy
            FSImage   image   = fsn.GetFSImage();
            NNStorage storage = image.GetStorage();

            storage.Close();
            // unlock any directories that FSNamesystem's initialization may have locked
            storage.SetStorageDirectories(FSNamesystem.GetNamespaceDirs(conf), FSNamesystem.GetNamespaceEditsDirs
                                              (conf));
            FSNamesystem spyFsn   = Org.Mockito.Mockito.Spy(fsn);
            FSNamesystem finalFsn = spyFsn;

            GenericTestUtils.DelayAnswer delayer = new GenericTestUtils.DelayAnswer(Log);
            BlockIdManager bid = Org.Mockito.Mockito.Spy(spyFsn.GetBlockIdManager());

            Whitebox.SetInternalState(finalFsn, "blockIdManager", bid);
            Org.Mockito.Mockito.DoAnswer(delayer).When(bid).GetGenerationStampV2();
            ExecutorService pool = Executors.NewFixedThreadPool(2);

            try
            {
                DoAnEdit(fsn, 1);
                Canceler canceler = new Canceler();
                // Save namespace
                fsn.SetSafeMode(HdfsConstants.SafeModeAction.SafemodeEnter);
                try
                {
                    Future <Void> saverFuture = pool.Submit(new _Callable_561(image, finalFsn, canceler
                                                                              ));
                    // Wait until saveNamespace calls getGenerationStamp
                    delayer.WaitForCall();
                    // then cancel the saveNamespace
                    Future <Void> cancelFuture = pool.Submit(new _Callable_572(canceler));
                    // give the cancel call time to run
                    Sharpen.Thread.Sleep(500);
                    // allow saveNamespace to proceed - it should check the cancel flag after
                    // this point and throw an exception
                    delayer.Proceed();
                    cancelFuture.Get();
                    saverFuture.Get();
                    NUnit.Framework.Assert.Fail("saveNamespace did not fail even though cancelled!");
                }
                catch (Exception t)
                {
                    GenericTestUtils.AssertExceptionContains("SaveNamespaceCancelledException", t);
                }
                Log.Info("Successfully cancelled a saveNamespace");
                // Check that we have only the original image and not any
                // cruft left over from half-finished images
                FSImageTestUtil.LogStorageContents(Log, storage);
                foreach (Storage.StorageDirectory sd in storage.DirIterable(null))
                {
                    FilePath curDir = sd.GetCurrentDir();
                    GenericTestUtils.AssertGlobEquals(curDir, "fsimage_.*", NNStorage.GetImageFileName
                                                          (0), NNStorage.GetImageFileName(0) + MD5FileUtils.Md5Suffix);
                }
            }
            finally
            {
                fsn.Close();
            }
        }
Ejemplo n.º 21
0
        /// <exception cref="System.Exception"/>
        private void SaveNamespaceWithInjectedFault(TestSaveNamespace.Fault fault)
        {
            Configuration conf = GetConf();

            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();
            NNStorage spyStorage    = Org.Mockito.Mockito.Spy(storage);

            originalImage.storage = spyStorage;
            FSImage spyImage = Org.Mockito.Mockito.Spy(originalImage);

            Whitebox.SetInternalState(fsn, "fsImage", spyImage);
            bool shouldFail = false;

            switch (fault)
            {
            case TestSaveNamespace.Fault.SaveSecondFsimageRte:
            {
                // should we expect the save operation to fail
                // inject fault
                // The spy throws a RuntimeException when writing to the second directory
                Org.Mockito.Mockito.DoAnswer(new TestSaveNamespace.FaultySaveImage(true)).When(spyImage
                                                                                               ).SaveFSImage((SaveNamespaceContext)Matchers.AnyObject(), (Storage.StorageDirectory
                                                                                                                                                          )Matchers.AnyObject(), (NNStorage.NameNodeFile)Matchers.AnyObject());
                shouldFail = false;
                break;
            }

            case TestSaveNamespace.Fault.SaveSecondFsimageIoe:
            {
                // The spy throws an IOException when writing to the second directory
                Org.Mockito.Mockito.DoAnswer(new TestSaveNamespace.FaultySaveImage(false)).When(spyImage
                                                                                                ).SaveFSImage((SaveNamespaceContext)Matchers.AnyObject(), (Storage.StorageDirectory
                                                                                                                                                           )Matchers.AnyObject(), (NNStorage.NameNodeFile)Matchers.AnyObject());
                shouldFail = false;
                break;
            }

            case TestSaveNamespace.Fault.SaveAllFsimages:
            {
                // The spy throws IOException in all directories
                Org.Mockito.Mockito.DoThrow(new RuntimeException("Injected")).When(spyImage).SaveFSImage
                    ((SaveNamespaceContext)Matchers.AnyObject(), (Storage.StorageDirectory)Matchers.AnyObject
                        (), (NNStorage.NameNodeFile)Matchers.AnyObject());
                shouldFail = true;
                break;
            }

            case TestSaveNamespace.Fault.WriteStorageAll:
            {
                // The spy throws an exception before writing any VERSION files
                Org.Mockito.Mockito.DoThrow(new RuntimeException("Injected")).When(spyStorage).WriteAll
                    ();
                shouldFail = true;
                break;
            }

            case TestSaveNamespace.Fault.WriteStorageOne:
            {
                // The spy throws on exception on one particular storage directory
                Org.Mockito.Mockito.DoAnswer(new TestSaveNamespace.FaultySaveImage(true)).When(spyStorage
                                                                                               ).WriteProperties((Storage.StorageDirectory)Matchers.AnyObject());
                // TODO: unfortunately this fails -- should be improved.
                // See HDFS-2173.
                shouldFail = true;
                break;
            }
            }
            try
            {
                DoAnEdit(fsn, 1);
                // Save namespace - this may fail, depending on fault injected
                fsn.SetSafeMode(HdfsConstants.SafeModeAction.SafemodeEnter);
                try
                {
                    fsn.SaveNamespace();
                    if (shouldFail)
                    {
                        NUnit.Framework.Assert.Fail("Did not fail!");
                    }
                }
                catch (Exception e)
                {
                    if (!shouldFail)
                    {
                        throw;
                    }
                    else
                    {
                        Log.Info("Test caught expected exception", e);
                    }
                }
                fsn.SetSafeMode(HdfsConstants.SafeModeAction.SafemodeLeave);
                // Should still be able to perform edits
                DoAnEdit(fsn, 2);
                // Now shut down and restart the namesystem
                originalImage.Close();
                fsn.Close();
                fsn = null;
                // Start a new namesystem, which should be able to recover
                // the namespace from the previous incarnation.
                fsn = FSNamesystem.LoadFromDisk(conf);
                // Make sure the image loaded including our edits.
                CheckEditExists(fsn, 1);
                CheckEditExists(fsn, 2);
            }
            finally
            {
                if (fsn != null)
                {
                    fsn.Close();
                }
            }
        }