private async Task EnsureZnodeAsync(string path) { while (true) { try { await zookeeper.createAsync(path, Encoding.UTF8.GetBytes("0"), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); return; } catch (KeeperException.NodeExistsException) { return; } catch (KeeperException.ConnectionLossException) { } catch (KeeperException.SessionExpiredException) { await EstablishSession(); } } }
public async Task <IActionResult> Create([FromBody] Connection form) { await _zooKeeper.createAsync($"/connections/{form.Id}", null, ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); await _zooKeeper.createAsync($"/connections/{form.Id}/value", Encoding.UTF8.GetBytes(form.Value), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); return(Ok()); }
public async Task Lock(string lockNode) { var node = $"{config.LockPath}/${lockNode}"; await this.CreatNodeIfNotExist(node); var path = $"{config.LockPath}/${lockNode}/${config.Name}-"; this.nodePath = await zooKeeper.createAsync( path, this.config.Name.ToBytes(), this.acls, ZK.CreateMode.EPHEMERAL_SEQUENTIAL); var index = nodePath.GetIndex(); var children = await zooKeeper.getChildrenAsync(node); var childrenIndex = children.Children.Select(p => new { Node = p, Index = p.GetIndex() }).OrderBy(p => p.Index).ToList(); if (childrenIndex.First().Index == index) { Console.WriteLine($"{this.config.Name} Begin Lock"); return; } var targetNode = string.Empty; for (int i = 0; i < childrenIndex.Count; i++) { if (childrenIndex[i].Index == index) { targetNode = childrenIndex[i - 1].Node; break; } } if (string.IsNullOrEmpty(targetNode)) { throw new Exception("Node Get Error"); } var tcs = new TaskCompletionSource <bool>(); Console.WriteLine($"{this.config.Name} Wait for {node}/{targetNode}"); var waitNode = await zooKeeper.existsAsync( $"{node}/{targetNode}", new WaitWatcher($"{node}/{targetNode}", tcs, e => e.get_Type() == EventType.NodeDeleted || e.getState() == KeeperState.Disconnected)); if (waitNode != null) { await Task.WhenAny(Task.Delay(Timeout.Infinite), tcs.Task); } Console.WriteLine($"{this.config.Name} Begin Lock"); return; }
private async Task SetupConnectionAsync() { zooKeeper = new org.apache.zookeeper.ZooKeeper(connectionString, 100000, new ZookeeperWatcher()); org.apache.zookeeper.ZooKeeper.LogToFile = false; org.apache.zookeeper.ZooKeeper.LogToTrace = true; var root = await zooKeeper.existsAsync("/"); if (root == null) { if (!createStoreIfNeeded) { throw new InvalidOperationException("Store does not exist"); } await zooKeeper.createAsync("/", null, Z.ZooDefs.Ids.OPEN_ACL_UNSAFE, Z.CreateMode.PERSISTENT); } await CreateIfNotExist(zooKeeper, prefix); if (useWatchdog) { await CreateIfNotExist(zooKeeper, watchdogPrefix); } watcher = new FeatureWatcher(zooKeeper, OnZookeeperEntryChanged); if (useWatchdog) { await SetupWatchdog(); } localView.Clear(); }
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(); }
public async Task AddValue(string path, string value) { var zk = new org.apache.zookeeper.ZooKeeper("127.0.0.1:2181", 60000, null); await zk.createAsync(path, Encoding.UTF8.GetBytes(value), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); await zk.closeAsync(); }
private static async Task Main(string[] args) { org.apache.zookeeper.ZooKeeper zooKeeper = new org.apache.zookeeper.ZooKeeper("127.0.0.1:2181", 5000, new WatcherSample()); var res = await zooKeeper.createAsync("/node1", Encoding.UTF8.GetBytes("1"), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL); Console.WriteLine(res); res = await zooKeeper.createAsync("/node2", Encoding.UTF8.GetBytes("1"), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL); Console.WriteLine(res); PessimisticLock lLock = new PessimisticLock(zooKeeper); Console.WriteLine(await lLock.GetLock("4")); Console.WriteLine(await lLock.ReleaseLock("4")); List <Task> tasks = new List <Task>(5); for (int i = 0; i < 5; i++) { tasks.Add(new Task(() => { var lockRes = lLock.GetLock("4").Result; Console.WriteLine($"{Thread.CurrentThread.Name} get lock {lockRes}"); })); } foreach (var task in tasks) { task.Start(); } Task.WaitAll(tasks.ToArray()); PessimisticLockV2 lockV2 = new PessimisticLockV2(zooKeeper); var lockV2res = await lockV2.GetLock("111"); Console.WriteLine(lockV2res); await Task.CompletedTask; }
public PessimisticLockV2(org.apache.zookeeper.ZooKeeper zooKeeper) { _zooKeeper = zooKeeper; var node = _zooKeeper.existsAsync("/PessimisticLockV2").Result; if (node == null) { Task.WaitAll(_zooKeeper.createAsync("/PessimisticLockV2", null, ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT)); } }
public override async Task <CreateResult> Execute(ZooKeeperNetExClient client) { if (!NodeHelper.ValidateDataSize(Request.Data)) { return(CreateUnsuccessfulResult(ZooKeeperStatus.BadArguments, NodeHelper.DataSizeLimitExceededException(Request.Data))); } var newPath = await client.createAsync(Request.Path, Request.Data, ZooDefs.Ids.OPEN_ACL_UNSAFE, Request.CreateMode.ToInnerCreateMode()).ConfigureAwait(false); return(CreateResult.Successful(Request.Path, newPath)); }
protected static void CreateNode(string path, org.apache.zookeeper.CreateMode createMode, org.apache.zookeeper.ZooKeeper client) { var parts = path.Split(new[] { "/" }, StringSplitOptions.RemoveEmptyEntries); var currentPart = string.Empty; foreach (var part in parts) { currentPart = $"{currentPart}/{part}"; var createResult = client.createAsync(currentPart, new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, createMode).Result; createResult.Should().NotBeNull(); } }
public async Task <string> CreateClientAsync() { string actionToPerform = "create client znode"; while (true) { await BlockUntilConnected(actionToPerform); try { string clientPath = await zookeeper.createAsync( $"{clientsPath}/c_", Encoding.UTF8.GetBytes("0"), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL_SEQUENTIAL); clientId = clientPath.Substring(clientPath.LastIndexOf("/", StringComparison.Ordinal) + 1); return(clientPath); } catch (KeeperException.NoNodeException e) { throw new ZkInvalidOperationException($"Could not {actionToPerform} as parent node does not exist", e); } catch (KeeperException.ConnectionLossException) { // do nothing, the next iteration will try again } 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); } } }
/// <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); }
public static async Task <string> CreateIfNotExist(org.apache.zookeeper.ZooKeeper zk, string path, string data = null) { var p = path.Split(new[] { '/' }, StringSplitOptions.RemoveEmptyEntries); var s = ""; string ret = null; for (int i = 0; i < p.Length; i++) { s = s + "/" + p[i]; try { ret = await zk.createAsync(s, i == p.Length - 1?Encoding.UTF8.GetBytes(data) : null, ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); } catch (KeeperException.NodeExistsException) { } } return(ret); }
public async Task <bool> GetLock(string key) { if (string.IsNullOrEmpty(key)) { throw new ArgumentNullException(nameof(key)); } try { var result = await _zooKeeper.createAsync($"/PessimisticLock/{key}", Encoding.UTF8.GetBytes(key), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL);//临时node 不能有children node return(result == $"/PessimisticLock/{key}"); } catch (Exception e) { Console.WriteLine(e); } return(false); }
public async Task RefreshAsync() { var connDic = new Dictionary <string, string>(); var isExisted = await _zooKeeper.existsAsync("/connections"); if (isExisted == null) { await _zooKeeper.createAsync("/connections", null, ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); } var connResult = await _zooKeeper.getChildrenAsync("/connections", new ConnectionWatcher(this)); foreach (var conn in connResult.Children) { var connData = await _zooKeeper.getDataAsync($"/connections/{conn}/value"); var connStr = Encoding.UTF8.GetString(connData.Data); connDic[conn] = connStr; } _cache.Set("connections", connDic); }
/// <summary> /// 获取锁 /// </summary> /// <param name="millisecondsTimeout">等待时间</param> /// <returns></returns> public async Task <bool> TryLock(int millisecondsTimeout = 0) { try { zooKeeper = new org.apache.zookeeper.ZooKeeper("127.0.0.1", 50000, new MyWatcher()); //创建锁节点 if (await zooKeeper.existsAsync("/Locks") == null) { await zooKeeper.createAsync("/Locks", null, Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); } //新建一个临时锁节点 lockNode = await zooKeeper.createAsync("/Locks/Lock_", null, Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL_SEQUENTIAL); //获取锁下所有节点 var lockNodes = await zooKeeper.getChildrenAsync("/Locks"); lockNodes.Children.Sort(); //判断如果创建的节点就是最小节点 返回锁 if (lockNode.Split("/").Last() == lockNodes.Children[0]) { return(true); } else { //当前节点的位置 var location = lockNodes.Children.FindIndex(n => n == lockNode.Split("/").Last()); //获取当前节点 前面一个节点的路径 var frontNodePath = lockNodes.Children[location - 1]; //在前面一个节点上加上Watcher ,当前面那个节点删除时,会触发Process方法 await zooKeeper.getDataAsync("/Locks/" + frontNodePath, myWatcher); //如果时间为0 一直等待下去 if (millisecondsTimeout == 0) { myWatcher.AutoResetEvent.WaitOne(); } else //如果时间不为0 等待指定时间后,返回结果 { var result = myWatcher.AutoResetEvent.WaitOne(millisecondsTimeout); if (result)//如果返回True,说明在指定时间内,前面的节点释放了锁(但是) { //获取锁下所有节点 lockNodes = await zooKeeper.getChildrenAsync("/Locks"); //判断如果创建的节点就是最小节点 返回锁 if (lockNode.Split("/").Last() == lockNodes.Children[0]) { return(true); } else { return(false); } } else { return(false); } } } } catch (KeeperException e) { await UnLock(); throw e; } return(false); }
public async Task <string> CreateAsync(string path, byte[] data, CreateMode mode) { return(await _zooKeeper.createAsync(path, data, ZooDefs.Ids.OPEN_ACL_UNSAFE, mode)); }