コード例 #1
0
        public string Watch(string path)
        {
            var stat = Zookeeper.Exists(path, new ConfigCenterWatcher(Zookeeper, ZkCache));

            if (stat != null)
            {
                var data = Zookeeper.GetData(path, false, null).GetString();
                ZkCache.SetValue(path, data);
                return(data);
            }

            return(null);
        }
コード例 #2
0
        public void SetNode(string path, string value, IEnumerable <ACL> acl)
        {
            var stat = Zookeeper.Exists(path, false);

            if (stat == null)
            {
                var createRet = Zookeeper.Create(path, value.GetBytes(), acl, CreateMode.Persistent);
            }
            else
            {
                Zookeeper.SetData(path, value.GetBytes(), stat.Version);
            }
        }
コード例 #3
0
ファイル: UtilsBase.cs プロジェクト: pzdn2009/zookeeper
        public void Init()
        {
            var ret = RetryOperation(() =>
            {
                return(Zookeeper.Exists("/", false) != null);
            });

            if (!ret)
            {
                if (ZkSerialization.Enable)
                {
                    var str = File.ReadAllText(LocalZkFilePath);
                    ZkSerialization.Deserilize(ZkCache, str);
                }
            }
        }
コード例 #4
0
        /**
         * Ensures that the given path exists with the given data, ACL and flags
         * @param path
         * @param acl
         * @param flags
         */
        protected void EnsureExists(string path, byte[] data, List <ACL> acl, CreateMode flags)
        {
            try
            {
                var components  = path.Split(PathUtils.PathSeparatorCharAsArray, StringSplitOptions.RemoveEmptyEntries);
                var pathBuilder = new StringBuilder(path.Length);

                for (var i = 0; i < components.Length; ++i)
                {
                    // Create a loop-scoped variable to avoid closure troubles
                    // inside RetryOperation.
                    var isLastComponent = (i == components.Length - 1);

                    var component = components[i];

                    pathBuilder.Append(PathUtils.PathSeparator);
                    pathBuilder.Append(component);
                    var currentPartialPath = pathBuilder.ToString();

                    RetryOperation(() =>
                    {
                        var stat = Zookeeper.Exists(currentPartialPath, false);
                        if (stat != null)
                        {
                            return(true);
                        }
                        Zookeeper.Create(currentPartialPath, isLastComponent ? data : null, acl, flags);
                        return(true);
                    });
                }
            }
            catch (KeeperException e)
            {
                LOG.WarnFormat("Caught: {0} {1}", e, e.StackTrace);
            }
            catch (ThreadInterruptedException e)
            {
                LOG.WarnFormat("Caught: {0} {1}", e, e.StackTrace);
            }
        }
コード例 #5
0
ファイル: ProtocolSupport.cs プロジェクト: chaelim/zookeeper
 /**
  * Ensures that the given path exists with the given data, ACL and flags
  * @param path
  * @param acl
  * @param flags
  */
 protected void EnsureExists(string path, byte[] data, List <ACL> acl, CreateMode flags)
 {
     try
     {
         RetryOperation(() =>
         {
             Stat stat = Zookeeper.Exists(path, false);
             if (stat != null)
             {
                 return(true);
             }
             Zookeeper.Create(path, data, acl, flags);
             return(true);
         });
     }
     catch (KeeperException e)
     {
         LOG.WarnFormat("Caught: {0} {1}", e, e.StackTrace);
     }
     catch (ThreadInterruptedException e)
     {
         LOG.WarnFormat("Caught: {0} {1}", e, e.StackTrace);
     }
 }
コード例 #6
0
        private object LockOperation()
        {
            do
            {
                if (id == null)
                {
                    long   sessionId = Zookeeper.SessionId;
                    string prefix    = "x-" + sessionId + "-";
                    FindPrefixInChildren(prefix, Zookeeper, dir);
                    idName = new ZNodeName(id);
                }

                if (id == null)
                {
                    continue;
                }

                List <string> names = Zookeeper.GetChildren(dir, false);
                if (names.IsEmpty())
                {
                    LOG.Warn("No children in: " + dir + " when we've just " +
                             "created one! Lets recreate it...");
                    // lets force the recreation of the id
                    id = null;
                }
                else
                {
                    // lets sort them explicitly (though they do seem to come back in order ususally :)
                    var sortedNames = new SortedSet <ZNodeName>();
                    foreach (string name in names)
                    {
                        sortedNames.Add(new ZNodeName(dir.Combine(name)));
                    }
                    ownerId = sortedNames.First().Name;
                    SortedSet <ZNodeName> lessThanMe = sortedNames.HeadSet(idName);
                    if (!lessThanMe.IsEmpty())
                    {
                        ZNodeName lastChildName = lessThanMe.Last();
                        lastChildId = lastChildName.Name;
                        if (LOG.IsDebugEnabled)
                        {
                            LOG.Debug("watching less than me node: " + lastChildId);
                        }
                        Stat stat = Zookeeper.Exists(lastChildId, new LockWatcher(this));
                        if (stat != null)
                        {
                            return(false);
                        }

                        LOG.Warn("Could not find the stats for less than me: " + lastChildName.Name);
                    }
                    else
                    {
                        if (Owner)
                        {
                            OnLockAcquired();
                            return(true);
                        }
                    }
                }
            } while (id == null);
            return(false);
        }
コード例 #7
0
        public bool RunForLeader()
        {
            long   sessionId = Zookeeper.SessionId;
            string prefix    = "election-" + sessionId + "-";
            var    names     = Zookeeper.GetChildren(path, false);

            // See whether we have already run for election in this process
            foreach (string name in names)
            {
                if (name.StartsWith(prefix))
                {
                    id = name;
                    if (LOG.IsDebugEnabled)
                    {
                        LOG.DebugFormat("Found id created last time: {0}", id);
                    }
                }
            }

            if (id == null)
            {
                id = Zookeeper.Create(path.Combine(prefix), data, Acl, CreateMode.EphemeralSequential);

                if (LOG.IsDebugEnabled)
                {
                    LOG.DebugFormat("Created id: {0}", id);
                }
            }

            idName = new ZNodeName(id);

            names = Zookeeper.GetChildren(path, false);
            var sortedNames = new SortedSet <ZNodeName>();

            foreach (var name in names)
            {
                sortedNames.Add(new ZNodeName(name));
            }

            var priors = sortedNames.HeadSet(idName);

            if (priors.Count == 0)
            {
                throw new InvalidOperationException("Count of priors is 0, but should at least include this node.");
            }

            if (priors.Count == 1)
            {
                IsOwner = true;
                watcher.TakeLeadership();
                return(true);
            }
            // only watch the node directly before us
            ZNodeName penultimate = null, last = null;

            foreach (var name in sortedNames)
            {
                penultimate = last;
                last        = name;
            }
            if (penultimate == null)
            {
                throw new InvalidOperationException("Penultimate value in priors is null, but count shoudl have been at least 2.");
            }
            var penultimatePath = path.Combine(penultimate.Name);

            if (Zookeeper.Exists(penultimatePath, new LeaderWatcher(Zookeeper, this, penultimatePath, watcher, path, id)) == null)
            {
                IsOwner = true;
                watcher.TakeLeadership();
                return(true);
            }
            return(false);
        }