Example #1
0
        public async Task DeleteClientAsync(string clientPath)
        {
            string actionToPerform = "delete client znode";
            bool   succeeded       = false;

            while (!succeeded)
            {
                await BlockUntilConnected(actionToPerform);

                try
                {
                    await zookeeper.deleteAsync(clientPath);

                    succeeded = true;
                }
                catch (KeeperException.NoNodeException e)
                {
                    throw new ZkInvalidOperationException($"Could not {actionToPerform} as the node does not exist.",
                                                          e);
                }
                catch (KeeperException.ConnectionLossException)
                {
                    // do nothing, will try again in the next iteration
                }
                catch (KeeperException.SessionExpiredException e)
                {
                    throw new ZkSessionExpiredException($"Could not {actionToPerform} as the session has expired.", e);
                }
                catch (Exception e)
                {
                    throw new ZkInvalidOperationException($"Could not {actionToPerform} due to an unexpected error", e);
                }
            }
        }
        public async Task <IActionResult> Delete([FromRoute] string id)
        {
            await _zooKeeper.deleteAsync($"/connections/{id}/value");

            await _zooKeeper.deleteAsync($"/connections/{id}");

            return(Ok());
        }
        static void Main(string[] args)
        {
            var client = new org.apache.zookeeper.ZooKeeper(
                "10.217.9.184:2181,10.217.6.124:2181,10.217.6.140:2181,10.217.6.222:2181,10.217.9.47:2181",
                5000,
                null);

            Thread.Sleep(1000);
            var path         = "/LinuxMustDie";
            var data         = "LINUX IS ALIVE!!";
            var createResult = client.createAsync(path, new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);

            Console.WriteLine($"Create result = {createResult}");

            var setDataResult = client.setDataAsync(path, Encoding.UTF8.GetBytes(data)).GetAwaiter().GetResult();

            Console.WriteLine($"SetData result version = {setDataResult.getVersion()}");


            var getDataResult = client.getDataAsync(path).GetAwaiter().GetResult();

            Console.WriteLine($"GetData result = {Encoding.UTF8.GetString(getDataResult.Data)}");

            Console.ReadKey();
            client.deleteAsync(path).Wait();
        }
Example #4
0
        private async Task SafeDelete(string path)
        {
            while (true)
            {
                try
                {
                    await zookeeper.deleteAsync(path);

                    Console.Write($"Removed queue {path} in Apache ZooKeeper\r\n");
                    return;
                }
                catch (KeeperException.NoNodeException)
                {
                    return;
                }
                catch (KeeperException.NotEmptyException)
                {
                    return;
                }
                catch (KeeperException.ConnectionLossException)
                {
                }
                catch (KeeperException.SessionExpiredException)
                {
                    await EstablishSession();
                }
            }
        }
Example #5
0
        public Task Unlock(string lockNode)
        {
            var path = $"{this.nodePath}";

            Console.WriteLine($"Unlock {path}");
            return(zooKeeper.deleteAsync(path));
        }
Example #6
0
        public async Task <bool> ReleaseLock(string key)
        {
            if (string.IsNullOrEmpty(key))
            {
                throw new ArgumentNullException(nameof(key));
            }

            var sta = await _zooKeeper.existsAsync($"/PessimisticLock/{key}", new WatcherSample());

            if (sta == null)
            {
                return(true);
            }

            var lockData = await _zooKeeper.getDataAsync($"/PessimisticLock/{key}", new WatcherSample());

            var mark = Encoding.UTF8.GetString(lockData.Data) == key;

            if (mark)
            {
                await _zooKeeper.deleteAsync($"/PessimisticLock/{key}", sta.getVersion());
            }

            return(true);
        }
Example #7
0
        public async Task DeleteValue(string path)
        {
            var zk = new org.apache.zookeeper.ZooKeeper("127.0.0.1:2181", 60000, null);
            await zk.deleteAsync(path);

            await zk.closeAsync();
        }
Example #8
0
        private async Task SafeDelete(string path)
        {
            while (true)
            {
                try
                {
                    await zookeeper.deleteAsync(path);

                    return;
                }
                catch (KeeperException.NoNodeException)
                {
                    return;
                }
                catch (KeeperException.NotEmptyException)
                {
                    return;
                }
                catch (KeeperException.ConnectionLossException)
                {
                }
                catch (KeeperException.SessionExpiredException)
                {
                    await EstablishSession();
                }
            }
        }
Example #9
0
 /// <summary>
 /// 释放锁
 /// </summary>
 /// <returns></returns>
 public async Task UnLock()
 {
     try
     {
         myWatcher.AutoResetEvent.Dispose();
         await zooKeeper.deleteAsync(lockNode);
     }
     catch (KeeperException e)
     {
         throw e;
     }
 }
 protected static void DeleteNonexistentNode(string path, org.apache.zookeeper.ZooKeeper client)
 {
     try
     {
         client.deleteAsync(path).GetAwaiter().GetResult();
     }
     catch (Exception e)
     {
         e.Should().BeAssignableTo <KeeperException.NoNodeException>();
     }
     //deleteResult.Status.Should().Be(ZooKeeperStatus.NoNode);
     //deleteResult.Path.Should().Be(path);
 }
Example #11
0
        public static async Task DeleteRecursive(org.apache.zookeeper.ZooKeeper zk, string path = "", string key = "")
        {
            try
            {
                var correctedPath = path + "/" + key;
                var a             = await zk.getChildrenAsync(correctedPath);

                foreach (var child in a.Children)
                {
                    await DeleteRecursive(zk, correctedPath == "/"? "" : correctedPath, child);
                }
                await zk.deleteAsync(correctedPath);
            }
            catch (KeeperException.NoNodeException) { }
        }
Example #12
0
        /// <summary>
        /// //
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>

        public async Task <bool> GetLock(string key)
        {
            var node = await _zooKeeper.createAsync($"/PessimisticLockV2/LOCK", Encoding.UTF8.GetBytes("1"),
                                                    ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT_SEQUENTIAL);

            var children = await _zooKeeper.getChildrenAsync("/PessimisticLockV2");

            var minNode = children.Children.OrderBy(t => t).First();

            var res = ("/PessimisticLockV2/" + minNode).Equals(node);

            if (res)
            {
                await _zooKeeper.deleteAsync(node);//releaseLock

                return(true);
            }
            return(false);
        }
Example #13
0
 public async Task DeleteAsync(string path)
 {
     await _zooKeeper.deleteAsync(path, -1);
 }
 protected static void DeleteNode(string path, org.apache.zookeeper.ZooKeeper client)
 {
     client.deleteAsync(path).Wait();
 }