Esempio n. 1
0
        public virtual void TestSnapshots()
        {
            cluster.GetNamesystem().GetSnapshotManager().SetAllowNestedSnapshots(true);
            MetricsAsserts.AssertGauge("Snapshots", 0, MetricsAsserts.GetMetrics(NsMetrics));
            MetricsAsserts.AssertCounter("CreateSnapshotOps", 0L, MetricsAsserts.GetMetrics(NnMetrics
                                                                                            ));
            // Create a snapshot for a non-snapshottable directory, thus should not
            // change the metrics
            try
            {
                hdfs.CreateSnapshot(sub1, "s1");
            }
            catch (Exception)
            {
            }
            MetricsAsserts.AssertGauge("Snapshots", 0, MetricsAsserts.GetMetrics(NsMetrics));
            MetricsAsserts.AssertCounter("CreateSnapshotOps", 1L, MetricsAsserts.GetMetrics(NnMetrics
                                                                                            ));
            // Create snapshot for sub1
            hdfs.AllowSnapshot(sub1);
            hdfs.CreateSnapshot(sub1, "s1");
            MetricsAsserts.AssertGauge("Snapshots", 1, MetricsAsserts.GetMetrics(NsMetrics));
            MetricsAsserts.AssertCounter("CreateSnapshotOps", 2L, MetricsAsserts.GetMetrics(NnMetrics
                                                                                            ));
            hdfs.CreateSnapshot(sub1, "s2");
            MetricsAsserts.AssertGauge("Snapshots", 2, MetricsAsserts.GetMetrics(NsMetrics));
            MetricsAsserts.AssertCounter("CreateSnapshotOps", 3L, MetricsAsserts.GetMetrics(NnMetrics
                                                                                            ));
            hdfs.GetSnapshotDiffReport(sub1, "s1", "s2");
            MetricsAsserts.AssertCounter("SnapshotDiffReportOps", 1L, MetricsAsserts.GetMetrics
                                             (NnMetrics));
            // Create snapshot for a directory under sub1
            Path subsub1 = new Path(sub1, "sub1sub1");
            Path subfile = new Path(subsub1, "file");

            DFSTestUtil.CreateFile(hdfs, subfile, 1024, Replication, seed);
            hdfs.AllowSnapshot(subsub1);
            hdfs.CreateSnapshot(subsub1, "s11");
            MetricsAsserts.AssertGauge("Snapshots", 3, MetricsAsserts.GetMetrics(NsMetrics));
            MetricsAsserts.AssertCounter("CreateSnapshotOps", 4L, MetricsAsserts.GetMetrics(NnMetrics
                                                                                            ));
            // delete snapshot
            hdfs.DeleteSnapshot(sub1, "s2");
            MetricsAsserts.AssertGauge("Snapshots", 2, MetricsAsserts.GetMetrics(NsMetrics));
            MetricsAsserts.AssertCounter("DeleteSnapshotOps", 1L, MetricsAsserts.GetMetrics(NnMetrics
                                                                                            ));
            // rename snapshot
            hdfs.RenameSnapshot(sub1, "s1", "NewS1");
            MetricsAsserts.AssertGauge("Snapshots", 2, MetricsAsserts.GetMetrics(NsMetrics));
            MetricsAsserts.AssertCounter("RenameSnapshotOps", 1L, MetricsAsserts.GetMetrics(NnMetrics
                                                                                            ));
        }
        /// <summary>check the correctness of the diff reports</summary>
        /// <exception cref="System.IO.IOException"/>
        private void VerifyDiffReport(Path dir, string from, string to, params SnapshotDiffReport.DiffReportEntry
                                      [] entries)
        {
            SnapshotDiffReport report = hdfs.GetSnapshotDiffReport(dir, from, to);
            // reverse the order of from and to
            SnapshotDiffReport inverseReport = hdfs.GetSnapshotDiffReport(dir, to, from);

            System.Console.Out.WriteLine(report.ToString());
            System.Console.Out.WriteLine(inverseReport.ToString() + "\n");
            NUnit.Framework.Assert.AreEqual(entries.Length, report.GetDiffList().Count);
            NUnit.Framework.Assert.AreEqual(entries.Length, inverseReport.GetDiffList().Count
                                            );
            foreach (SnapshotDiffReport.DiffReportEntry entry in entries)
            {
                if (entry.GetType() == SnapshotDiffReport.DiffType.Modify)
                {
                    NUnit.Framework.Assert.IsTrue(report.GetDiffList().Contains(entry));
                    NUnit.Framework.Assert.IsTrue(inverseReport.GetDiffList().Contains(entry));
                }
                else
                {
                    if (entry.GetType() == SnapshotDiffReport.DiffType.Delete)
                    {
                        NUnit.Framework.Assert.IsTrue(report.GetDiffList().Contains(entry));
                        NUnit.Framework.Assert.IsTrue(inverseReport.GetDiffList().Contains(new SnapshotDiffReport.DiffReportEntry
                                                                                               (SnapshotDiffReport.DiffType.Create, entry.GetSourcePath())));
                    }
                    else
                    {
                        if (entry.GetType() == SnapshotDiffReport.DiffType.Create)
                        {
                            NUnit.Framework.Assert.IsTrue(report.GetDiffList().Contains(entry));
                            NUnit.Framework.Assert.IsTrue(inverseReport.GetDiffList().Contains(new SnapshotDiffReport.DiffReportEntry
                                                                                                   (SnapshotDiffReport.DiffType.Delete, entry.GetSourcePath())));
                        }
                    }
                }
            }
        }
Esempio n. 3
0
        /// <exception cref="System.Exception"/>
        public virtual int Run(string[] argv)
        {
            string description = "hdfs snapshotDiff <snapshotDir> <from> <to>:\n" + "\tGet the difference between two snapshots, \n"
                                 + "\tor between a snapshot and the current tree of a directory.\n" + "\tFor <from>/<to>, users can use \".\" to present the current status,\n"
                                 + "\tand use \".snapshot/snapshot_name\" to present a snapshot,\n" + "\twhere \".snapshot/\" can be omitted\n";

            if (argv.Length != 3)
            {
                System.Console.Error.WriteLine("Usage: \n" + description);
                return(1);
            }
            FileSystem fs = FileSystem.Get(GetConf());

            if (!(fs is DistributedFileSystem))
            {
                System.Console.Error.WriteLine("SnapshotDiff can only be used in DistributedFileSystem"
                                               );
                return(1);
            }
            DistributedFileSystem dfs = (DistributedFileSystem)fs;
            Path   snapshotRoot       = new Path(argv[0]);
            string fromSnapshot       = GetSnapshotName(argv[1]);
            string toSnapshot         = GetSnapshotName(argv[2]);

            try
            {
                SnapshotDiffReport diffReport = dfs.GetSnapshotDiffReport(snapshotRoot, fromSnapshot
                                                                          , toSnapshot);
                System.Console.Out.WriteLine(diffReport.ToString());
            }
            catch (IOException e)
            {
                string[] content = e.GetLocalizedMessage().Split("\n");
                System.Console.Error.WriteLine("snapshotDiff: " + content[0]);
                return(1);
            }
            return(0);
        }