Beispiel #1
0
 public LockInternals(ZooKeeper zkClient, ILockInternalsDriver driver, string path, string lockName, int maxLeases)
 {
     _driver    = driver;
     _lockName  = lockName;
     _maxLeases = maxLeases;
     _zkClient  = zkClient;
     _basePath  = ZKPaths.ValidatePath(path);
     _path      = ZKPaths.MakePath(path, lockName);
     _signal    = new SemaphoreSlim(0);
     _watcher   = new ReleaseLockWatcher(_signal);
 }
Beispiel #2
0
        /// <summary>
        /// zk信号量
        /// </summary>
        /// <param name="zkClient"></param>
        /// <param name="path"></param>
        /// <param name="maxLeases"></param>
        public ZookeeperSemaphore(ZooKeeper zkClient, string path, int maxLeases, int lockTimeout)
        {
            ZKPaths.ValidatePath(path);

            _zkClient       = zkClient;
            _maxLeases      = maxLeases;
            _lockTimeout    = lockTimeout;
            _lock           = new ZookeeperMutexLock(zkClient, ZKPaths.MakePath(path, LOCK_PARENT), lockTimeout);
            _leasesPath     = ZKPaths.MakePath(path, LEASE_PARENT);
            _acquiredLeases = new List <string>(maxLeases);

            _signal  = new SemaphoreSlim(0);
            _watcher = new ReleaseLockWatcher(_signal);
        }
        public static async Task <string> RecursionCreateAsync(this ZooKeeper zkClient, string path, byte[] data, List <ACL> acl, CreateMode createMode)
        {
            ZKPaths.ValidatePath(path);

            var paths = path.Split(new[] { '/' }, StringSplitOptions.RemoveEmptyEntries);

            string outPath  = null;
            var    currPath = string.Empty;

            for (int i = 0; i < paths.Length; i++)
            {
                currPath += "/" + paths[i];

                try
                {
                    var stat = await zkClient.existsAsync(currPath);

                    if (stat == null)              // 节点不存在
                    {
                        if (i == paths.Length - 1) // 终节点
                        {
                            outPath = await zkClient.createAsync(currPath, data, acl, createMode);
                        }
                        else
                        {
                            await zkClient.createAsync(currPath, null, ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
                        }
                    }
                }
                catch (KeeperException.NodeExistsException)
                {
                    continue;
                }
            }
            return(outPath);
        }
 public ZookeeperMutexLock(ZooKeeper zkClient, string path, string lockName, int maxLeases, ILockInternalsDriver driver, int lockTimeout)
 {
     _basePath    = ZKPaths.ValidatePath(path);
     _lockTimeout = lockTimeout;
     _internals   = new LockInternals(zkClient, driver, path, lockName, maxLeases);
 }