/// <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())));
                        }
                    }
                }
            }
        }
Beispiel #2
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);
        }
        /// <summary>Test the computation and representation of diff between snapshots</summary>
        /// <exception cref="System.Exception"/>
        public virtual void TestDiffReport()
        {
            cluster.GetNamesystem().GetSnapshotManager().SetAllowNestedSnapshots(true);
            Path subsub1    = new Path(sub1, "subsub1");
            Path subsubsub1 = new Path(subsub1, "subsubsub1");

            hdfs.Mkdirs(subsubsub1);
            ModifyAndCreateSnapshot(sub1, new Path[] { sub1, subsubsub1 });
            ModifyAndCreateSnapshot(subsubsub1, new Path[] { sub1, subsubsub1 });
            try
            {
                hdfs.GetSnapshotDiffReport(subsub1, "s1", "s2");
                NUnit.Framework.Assert.Fail("Expect exception when getting snapshot diff report: "
                                            + subsub1 + " is not a snapshottable directory.");
            }
            catch (IOException e)
            {
                GenericTestUtils.AssertExceptionContains("Directory is not a snapshottable directory: "
                                                         + subsub1, e);
            }
            string invalidName = "invalid";

            try
            {
                hdfs.GetSnapshotDiffReport(sub1, invalidName, invalidName);
                NUnit.Framework.Assert.Fail("Expect exception when providing invalid snapshot name for diff report"
                                            );
            }
            catch (IOException e)
            {
                GenericTestUtils.AssertExceptionContains("Cannot find the snapshot of directory "
                                                         + sub1 + " with name " + invalidName, e);
            }
            // diff between the same snapshot
            SnapshotDiffReport report = hdfs.GetSnapshotDiffReport(sub1, "s0", "s0");

            System.Console.Out.WriteLine(report);
            NUnit.Framework.Assert.AreEqual(0, report.GetDiffList().Count);
            report = hdfs.GetSnapshotDiffReport(sub1, string.Empty, string.Empty);
            System.Console.Out.WriteLine(report);
            NUnit.Framework.Assert.AreEqual(0, report.GetDiffList().Count);
            report = hdfs.GetSnapshotDiffReport(subsubsub1, "s0", "s2");
            System.Console.Out.WriteLine(report);
            NUnit.Framework.Assert.AreEqual(0, report.GetDiffList().Count);
            // test path with scheme also works
            report = hdfs.GetSnapshotDiffReport(hdfs.MakeQualified(subsubsub1), "s0", "s2");
            System.Console.Out.WriteLine(report);
            NUnit.Framework.Assert.AreEqual(0, report.GetDiffList().Count);
            VerifyDiffReport(sub1, "s0", "s2", new SnapshotDiffReport.DiffReportEntry(SnapshotDiffReport.DiffType
                                                                                      .Modify, DFSUtil.String2Bytes(string.Empty)), new SnapshotDiffReport.DiffReportEntry
                                 (SnapshotDiffReport.DiffType.Create, DFSUtil.String2Bytes("file15")), new SnapshotDiffReport.DiffReportEntry
                                 (SnapshotDiffReport.DiffType.Delete, DFSUtil.String2Bytes("file12")), new SnapshotDiffReport.DiffReportEntry
                                 (SnapshotDiffReport.DiffType.Delete, DFSUtil.String2Bytes("file11")), new SnapshotDiffReport.DiffReportEntry
                                 (SnapshotDiffReport.DiffType.Create, DFSUtil.String2Bytes("file11")), new SnapshotDiffReport.DiffReportEntry
                                 (SnapshotDiffReport.DiffType.Modify, DFSUtil.String2Bytes("file13")), new SnapshotDiffReport.DiffReportEntry
                                 (SnapshotDiffReport.DiffType.Delete, DFSUtil.String2Bytes("link13")), new SnapshotDiffReport.DiffReportEntry
                                 (SnapshotDiffReport.DiffType.Create, DFSUtil.String2Bytes("link13")));
            VerifyDiffReport(sub1, "s0", "s5", new SnapshotDiffReport.DiffReportEntry(SnapshotDiffReport.DiffType
                                                                                      .Modify, DFSUtil.String2Bytes(string.Empty)), new SnapshotDiffReport.DiffReportEntry
                                 (SnapshotDiffReport.DiffType.Create, DFSUtil.String2Bytes("file15")), new SnapshotDiffReport.DiffReportEntry
                                 (SnapshotDiffReport.DiffType.Delete, DFSUtil.String2Bytes("file12")), new SnapshotDiffReport.DiffReportEntry
                                 (SnapshotDiffReport.DiffType.Modify, DFSUtil.String2Bytes("file10")), new SnapshotDiffReport.DiffReportEntry
                                 (SnapshotDiffReport.DiffType.Delete, DFSUtil.String2Bytes("file11")), new SnapshotDiffReport.DiffReportEntry
                                 (SnapshotDiffReport.DiffType.Create, DFSUtil.String2Bytes("file11")), new SnapshotDiffReport.DiffReportEntry
                                 (SnapshotDiffReport.DiffType.Modify, DFSUtil.String2Bytes("file13")), new SnapshotDiffReport.DiffReportEntry
                                 (SnapshotDiffReport.DiffType.Delete, DFSUtil.String2Bytes("link13")), new SnapshotDiffReport.DiffReportEntry
                                 (SnapshotDiffReport.DiffType.Create, DFSUtil.String2Bytes("link13")), new SnapshotDiffReport.DiffReportEntry
                                 (SnapshotDiffReport.DiffType.Modify, DFSUtil.String2Bytes("subsub1/subsubsub1"))
                             , new SnapshotDiffReport.DiffReportEntry(SnapshotDiffReport.DiffType.Create, DFSUtil
                                                                      .String2Bytes("subsub1/subsubsub1/file10")), new SnapshotDiffReport.DiffReportEntry
                                 (SnapshotDiffReport.DiffType.Create, DFSUtil.String2Bytes("subsub1/subsubsub1/file11"
                                                                                           )), new SnapshotDiffReport.DiffReportEntry(SnapshotDiffReport.DiffType.Create, DFSUtil
                                                                                                                                      .String2Bytes("subsub1/subsubsub1/file13")), new SnapshotDiffReport.DiffReportEntry
                                 (SnapshotDiffReport.DiffType.Create, DFSUtil.String2Bytes("subsub1/subsubsub1/link13"
                                                                                           )), new SnapshotDiffReport.DiffReportEntry(SnapshotDiffReport.DiffType.Create, DFSUtil
                                                                                                                                      .String2Bytes("subsub1/subsubsub1/file15")));
            VerifyDiffReport(sub1, "s2", "s5", new SnapshotDiffReport.DiffReportEntry(SnapshotDiffReport.DiffType
                                                                                      .Modify, DFSUtil.String2Bytes("file10")), new SnapshotDiffReport.DiffReportEntry
                                 (SnapshotDiffReport.DiffType.Modify, DFSUtil.String2Bytes("subsub1/subsubsub1"))
                             , new SnapshotDiffReport.DiffReportEntry(SnapshotDiffReport.DiffType.Create, DFSUtil
                                                                      .String2Bytes("subsub1/subsubsub1/file10")), new SnapshotDiffReport.DiffReportEntry
                                 (SnapshotDiffReport.DiffType.Create, DFSUtil.String2Bytes("subsub1/subsubsub1/file11"
                                                                                           )), new SnapshotDiffReport.DiffReportEntry(SnapshotDiffReport.DiffType.Create, DFSUtil
                                                                                                                                      .String2Bytes("subsub1/subsubsub1/file13")), new SnapshotDiffReport.DiffReportEntry
                                 (SnapshotDiffReport.DiffType.Create, DFSUtil.String2Bytes("subsub1/subsubsub1/link13"
                                                                                           )), new SnapshotDiffReport.DiffReportEntry(SnapshotDiffReport.DiffType.Create, DFSUtil
                                                                                                                                      .String2Bytes("subsub1/subsubsub1/file15")));
            VerifyDiffReport(sub1, "s3", string.Empty, new SnapshotDiffReport.DiffReportEntry
                                 (SnapshotDiffReport.DiffType.Modify, DFSUtil.String2Bytes("subsub1/subsubsub1"))
                             , new SnapshotDiffReport.DiffReportEntry(SnapshotDiffReport.DiffType.Create, DFSUtil
                                                                      .String2Bytes("subsub1/subsubsub1/file15")), new SnapshotDiffReport.DiffReportEntry
                                 (SnapshotDiffReport.DiffType.Delete, DFSUtil.String2Bytes("subsub1/subsubsub1/file12"
                                                                                           )), new SnapshotDiffReport.DiffReportEntry(SnapshotDiffReport.DiffType.Modify, DFSUtil
                                                                                                                                      .String2Bytes("subsub1/subsubsub1/file10")), new SnapshotDiffReport.DiffReportEntry
                                 (SnapshotDiffReport.DiffType.Delete, DFSUtil.String2Bytes("subsub1/subsubsub1/file11"
                                                                                           )), new SnapshotDiffReport.DiffReportEntry(SnapshotDiffReport.DiffType.Create, DFSUtil
                                                                                                                                      .String2Bytes("subsub1/subsubsub1/file11")), new SnapshotDiffReport.DiffReportEntry
                                 (SnapshotDiffReport.DiffType.Modify, DFSUtil.String2Bytes("subsub1/subsubsub1/file13"
                                                                                           )), new SnapshotDiffReport.DiffReportEntry(SnapshotDiffReport.DiffType.Create, DFSUtil
                                                                                                                                      .String2Bytes("subsub1/subsubsub1/link13")), new SnapshotDiffReport.DiffReportEntry
                                 (SnapshotDiffReport.DiffType.Delete, DFSUtil.String2Bytes("subsub1/subsubsub1/link13"
                                                                                           )));
        }