public void TestAuthorized() { _zkClient.AddAuthInfo("digest", Encoding.Default.GetBytes("pat:pass")); TestUtil.ReSetPathUnCreate(_zkClient, "/path1"); _zkClient.Create("/path1", null, Ids.CREATOR_ALL_ACL, CreateMode.Persistent); _zkClient.ReadData <object>("/path1"); }
public void TestAuthorized() { List <ACL> acl = new List <ACL>(); acl.Add(new ACL(Perms.ALL, new ZKId("digest", "pat:pass"))); _zkClient.AddAuthInfo("digest", Encoding.Default.GetBytes("pat:pass")); _zkClient.Create("/path1", null, acl, CreateMode.Persistent); _zkClient.ReadData <object>("/path1"); }
public void TestGetChildren() { string path1 = "/a"; string path2 = "/a/a"; string path3 = "/a/a/a"; _zkClient.Create(path1, null, CreateMode.Persistent); _zkClient.Create(path2, null, CreateMode.Persistent); _zkClient.Create(path3, null, CreateMode.Persistent); Assert.True(1 == _zkClient.GetChildren(path1).Count); Assert.True(1 == _zkClient.GetChildren(path2).Count); Assert.True(0 == _zkClient.GetChildren(path3).Count); }
public bool Lock(int timeout, int delayTimeMillis) { this.delayTimeMillis = delayTimeMillis; long startTime = DateTime.Now.ToUnixTime(); while (true) { try { semaphore = new Semaphore(1, 1); client.Create(lockPath + "/lock", lockNodeData, CreateMode.Ephemeral); hasLock = true; return(true); } catch (ZKNodeExistsException e) { try { semaphore.WaitOne(); } catch (ThreadInterruptedException interruptedException) { return(false); } } //超时处理 if (timeout > 0 && (DateTime.Now.ToUnixTime() - startTime) >= timeout) { return(false); } } }
public bool Lock() { semaphore = new Semaphore(1, 1); string newPath = client.Create(lockPath + "/1", null, CreateMode.EphemeralSequential); string[] paths = newPath.Split(new string[] { "/" }, StringSplitOptions.RemoveEmptyEntries); currentSeq = paths[paths.Length - 1]; bool getLock = false; try { semaphore.WaitOne(); getLock = true; } catch (ThreadInterruptedException e) { throw new ZKInterruptedException(e); } if (getLock) { LOG.Debug("get halock successful."); } else { LOG.Debug("failed to get halock."); } return(getLock); }
private ZKHALock(ZKClient client, string lockPach) { this.client = client; this.lockPath = lockPach; countListener = new ZKChildListener().ChildChange( (parentPath, currentChilds) => { if (Check(currentSeq, currentChilds)) { semaphore.Release(); } }); stateListener = new ZKStateListener().StateChanged( (state) => { if (state == KeeperState.SyncConnected) { //如果重新连接 //如果重连后之前的节点已删除,并且lock处于等待状态,则重新创建节点,等待获得lock if (!client.Exists(lockPach + "/" + currentSeq) && !semaphore.WaitOne(1000)) { string newPath = client.Create(lockPath + "/1", null, CreateMode.EphemeralSequential); string[] paths = newPath.Split(new string[] { "/" }, StringSplitOptions.RemoveEmptyEntries); currentSeq = paths[paths.Length - 1]; } } }); }
public static ZKDistributedDelayLock NewInstance(ZKClient client, string lockPach) { if (!client.Exists(lockPach)) { throw new ZKNoNodeException("The lockPath is not exists!,please create the node.[path:" + lockPach + "]"); } ZKDistributedDelayLock zkDistributedDelayLock = new ZKDistributedDelayLock(client, lockPach); client.SubscribeDataChanges(lockPach + "/lock", zkDistributedDelayLock.nodeListener); client.SubscribeStateChanges(zkDistributedDelayLock.stateListener); try { client.Create(lockPach + "/nodes", null, CreateMode.Persistent); } catch (ZKNodeExistsException e) { //已被其他线程创建,这里忽略就可以 } return(zkDistributedDelayLock); }
public void CreateNode() { ZKClient zkClient = ZKClientBuilder.NewZKClient(TestUtil.zkServers) .SessionTimeout(10000) .ConnectionTimeout(10000) .Serializer(new SerializableSerializer()) .Build(); Console.WriteLine("conneted ok!"); User user = new User(); user.Id = 1; user.Name = "testUser"; string path = zkClient.Create("/testUserNode", user, CreateMode.Persistent); //输出创建节点的路径 Console.WriteLine("created path:" + path); zkClient.Close(); zkClient = null; }
public ZKDistributedDelayLock(ZKClient client, string lockPach) { this.client = client; this.lockPath = lockPach; cancellationTokenSource = new CancellationTokenSource(); factory = new TaskFactory( cancellationTokenSource.Token, TaskCreationOptions.None, TaskContinuationOptions.None, new LimitedConcurrencyLevelTaskScheduler(1)); nodeListener = new ZKDataListener().DataDeleted( (path) => { factory.StartNew(() => { if (!factory.CancellationToken.IsCancellationRequested) { if (!hasLock) { //如果当前没有持有锁 //为了解决网络闪断问题,先等待一段时间,再重新竞争锁 Thread.Sleep(delayTimeMillis); //如果之前获得锁的线程解除了锁定,则所有等待的线程都重新尝试,这里使得信号量加1 semaphore.Release(); } } }, factory.CancellationToken); }); stateListener = new ZKStateListener().StateChanged((state) => { if (state == KeeperState.SyncConnected) { //如果重新连接 factory.StartNew(() => { if (!factory.CancellationToken.IsCancellationRequested) { if (hasLock) { //现在持有锁 //重新创建节点 try { client.Create(lockPath + "/lock", lockNodeData, CreateMode.Ephemeral); } catch (ZKNodeExistsException e) { try { if (lockNodeData != client.ReadData <string>(lockPath + "/lock")) { hasLock = false; } } catch (ZKNoNodeException e2) { //ignore } } } } }, factory.CancellationToken); } }); }
private void AddParticipantNode() { string path = client.Create(leaderPath + "/nodes/1", id, CreateMode.EphemeralSequential); curentNodePath = path; }
public static void Create(string path, string data) { var nodeData = data == null ? null : data.GetBytes(); zkClient.Create(path, nodeData, Ids.OPEN_ACL_UNSAFE, CreateMode.Persistent); }