コード例 #1
0
ファイル: RegistryUtils.cs プロジェクト: orf53975/hadoop.net
        /// <summary>
        /// List children of a directory and retrieve their
        /// <see cref="Org.Apache.Hadoop.Registry.Client.Types.RegistryPathStatus"/>
        /// values.
        /// <p>
        /// This is not an atomic operation; A child may be deleted
        /// during the iteration through the child entries. If this happens,
        /// the <code>PathNotFoundException</code> is caught and that child
        /// entry ommitted.
        /// </summary>
        /// <param name="path">path</param>
        /// <returns>
        /// a possibly empty map of child entries listed by
        /// their short name.
        /// </returns>
        /// <exception cref="Org.Apache.Hadoop.FS.PathNotFoundException">path is not in the registry.
        ///     </exception>
        /// <exception cref="Org.Apache.Hadoop.Registry.Client.Exceptions.InvalidPathnameException
        ///     ">the path is invalid.</exception>
        /// <exception cref="System.IO.IOException">Any other IO Exception</exception>
        public static IDictionary <string, RegistryPathStatus> StatChildren(RegistryOperations
                                                                            registryOperations, string path)
        {
            IList <string> childNames = registryOperations.List(path);
            IDictionary <string, RegistryPathStatus> results = new Dictionary <string, RegistryPathStatus
                                                                               >();

            foreach (string childName in childNames)
            {
                string child = RegistryPathUtils.Join(path, childName);
                try
                {
                    RegistryPathStatus stat = registryOperations.Stat(child);
                    results[childName] = stat;
                }
                catch (PathNotFoundException pnfe)
                {
                    if (Log.IsDebugEnabled())
                    {
                        Log.Debug("stat failed on {}: moved? {}", child, pnfe, pnfe);
                    }
                }
            }
            // and continue
            return(results);
        }
コード例 #2
0
        public virtual void TestStat()
        {
            PutExampleServiceEntry(EntryPath, 0);
            RegistryPathStatus stat = operations.Stat(EntryPath);

            NUnit.Framework.Assert.IsTrue(stat.size > 0);
            NUnit.Framework.Assert.IsTrue(stat.time > 0);
            NUnit.Framework.Assert.AreEqual(Name, stat.path);
        }
コード例 #3
0
        public virtual bool ShouldSelect(string path, RegistryPathStatus registryPathStatus
                                         , ServiceRecord serviceRecord)
        {
            string policy = serviceRecord.Get(YarnRegistryAttributes.YarnPersistence, string.Empty
                                              );

            return(id.Equals(serviceRecord.Get(YarnRegistryAttributes.YarnId, string.Empty)) &&
                   (targetPolicy.Equals(policy)));
        }
コード例 #4
0
        /// <exception cref="System.IO.IOException"/>
        public virtual RegistryPathStatus Stat(string path)
        {
            ValidatePath(path);
            Org.Apache.Zookeeper.Data.Stat stat = ZkStat(path);
            string             name             = RegistryPathUtils.LastPathEntry(path);
            RegistryPathStatus status           = new RegistryPathStatus(name, stat.GetCtime(), stat.GetDataLength
                                                                             (), stat.GetNumChildren());

            if (Log.IsDebugEnabled())
            {
                Log.Debug("Stat {} => {}", path, status);
            }
            return(status);
        }
コード例 #5
0
        public virtual void TestMkdirNoParent()
        {
            string path = EntryPath + "/missing";

            try
            {
                operations.Mknode(path, false);
                RegistryPathStatus stat = operations.Stat(path);
                NUnit.Framework.Assert.Fail("Got a status " + stat);
            }
            catch (PathNotFoundException)
            {
            }
        }
コード例 #6
0
        public virtual void TestPutNoParent()
        {
            ServiceRecord record = new ServiceRecord();

            record.Set(YarnRegistryAttributes.YarnId, "testPutNoParent");
            string path = "/path/without/parent";

            try
            {
                operations.Bind(path, record, 0);
                // didn't get a failure
                // trouble
                RegistryPathStatus stat = operations.Stat(path);
                NUnit.Framework.Assert.Fail("Got a status " + stat);
            }
            catch (PathNotFoundException)
            {
            }
        }
コード例 #7
0
        public virtual void TestListListFully()
        {
            ServiceRecord r1   = new ServiceRecord();
            ServiceRecord r2   = CreateRecord("i", PersistencePolicies.Permanent, "r2");
            string        path = Userpath + ScHadoop + "/listing";

            operations.Mknode(path, true);
            string r1path = path + "/r1";

            operations.Bind(r1path, r1, 0);
            string r2path = path + "/r2";

            operations.Bind(r2path, r2, 0);
            RegistryPathStatus r1stat = operations.Stat(r1path);

            NUnit.Framework.Assert.AreEqual("r1", r1stat.path);
            RegistryPathStatus r2stat = operations.Stat(r2path);

            NUnit.Framework.Assert.AreEqual("r2", r2stat.path);
            AssertNotEquals(r1stat, r2stat);
            // listings now
            IList <string> list = operations.List(path);

            NUnit.Framework.Assert.AreEqual("Wrong no. of children", 2, list.Count);
            // there's no order here, so create one
            IDictionary <string, string> names = new Dictionary <string, string>();
            string entries = string.Empty;

            foreach (string child in list)
            {
                names[child] = child;
                entries     += child + " ";
            }
            NUnit.Framework.Assert.IsTrue("No 'r1' in " + entries, names.Contains("r1"));
            NUnit.Framework.Assert.IsTrue("No 'r2' in " + entries, names.Contains("r2"));
            IDictionary <string, RegistryPathStatus> stats = RegistryUtils.StatChildren(operations
                                                                                        , path);

            NUnit.Framework.Assert.AreEqual("Wrong no. of children", 2, stats.Count);
            NUnit.Framework.Assert.AreEqual(r1stat, stats["r1"]);
            NUnit.Framework.Assert.AreEqual(r2stat, stats["r2"]);
        }
コード例 #8
0
        public virtual void TestLsParent()
        {
            ServiceRecord      written  = PutExampleServiceEntry(EntryPath, 0);
            RegistryPathStatus stat     = operations.Stat(EntryPath);
            IList <string>     children = operations.List(ParentPath);

            NUnit.Framework.Assert.AreEqual(1, children.Count);
            NUnit.Framework.Assert.AreEqual(Name, children[0]);
            IDictionary <string, RegistryPathStatus> childStats = RegistryUtils.StatChildren(operations
                                                                                             , ParentPath);

            NUnit.Framework.Assert.AreEqual(1, childStats.Count);
            NUnit.Framework.Assert.AreEqual(stat, childStats[Name]);
            IDictionary <string, ServiceRecord> records = RegistryUtils.ExtractServiceRecords(
                operations, ParentPath, childStats.Values);

            NUnit.Framework.Assert.AreEqual(1, records.Count);
            ServiceRecord record = records[EntryPath];

            RegistryTypeUtils.ValidateServiceRecord(EntryPath, record);
            AssertMatches(written, record);
        }
コード例 #9
0
        public virtual int Purge(string path, RegistryAdminService.NodeSelector selector,
                                 RegistryAdminService.PurgePolicy purgePolicy, BackgroundCallback callback)
        {
            bool toDelete = false;
            // look at self to see if it has a service record
            IDictionary <string, RegistryPathStatus> childEntries;
            ICollection <RegistryPathStatus>         entries;

            try
            {
                // list this path's children
                childEntries = RegistryUtils.StatChildren(this, path);
                entries      = childEntries.Values;
            }
            catch (PathNotFoundException)
            {
                // there's no record here, it may have been deleted already.
                // exit
                return(0);
            }
            try
            {
                RegistryPathStatus registryPathStatus = Stat(path);
                ServiceRecord      serviceRecord      = Resolve(path);
                // there is now an entry here.
                toDelete = selector.ShouldSelect(path, registryPathStatus, serviceRecord);
            }
            catch (EOFException)
            {
            }
            catch (InvalidRecordException)
            {
            }
            catch (NoRecordException)
            {
            }
            catch (PathNotFoundException)
            {
                // ignore
                // ignore
                // ignore
                // there's no record here, it may have been deleted already.
                // exit
                return(0);
            }
            if (toDelete && !entries.IsEmpty())
            {
                if (Log.IsDebugEnabled())
                {
                    Log.Debug("Match on record @ {} with children ", path);
                }
                switch (purgePolicy)
                {
                case RegistryAdminService.PurgePolicy.SkipOnChildren:
                {
                    // there's children
                    // don't do the deletion... continue to next record
                    if (Log.IsDebugEnabled())
                    {
                        Log.Debug("Skipping deletion");
                    }
                    toDelete = false;
                    break;
                }

                case RegistryAdminService.PurgePolicy.PurgeAll:
                {
                    // mark for deletion
                    if (Log.IsDebugEnabled())
                    {
                        Log.Debug("Scheduling for deletion with children");
                    }
                    toDelete = true;
                    entries  = new AList <RegistryPathStatus>(0);
                    break;
                }

                case RegistryAdminService.PurgePolicy.FailOnChildren:
                {
                    if (Log.IsDebugEnabled())
                    {
                        Log.Debug("Failing deletion operation");
                    }
                    throw new PathIsNotEmptyDirectoryException(path);
                }
                }
            }
            int deleteOps = 0;

            if (toDelete)
            {
                try
                {
                    ZkDelete(path, true, callback);
                }
                catch (PathNotFoundException)
                {
                    // sign that the path was deleted during the operation.
                    // this is a no-op, and all children can be skipped
                    return(deleteOps);
                }
                deleteOps++;
            }
            // now go through the children
            foreach (RegistryPathStatus status in entries)
            {
                string childname = status.path;
                string childpath = RegistryPathUtils.Join(path, childname);
                deleteOps += Purge(childpath, selector, purgePolicy, callback);
            }
            return(deleteOps);
        }