static void Main(string[] args) { //创建一个Zookeeper实例,第一个参数为目标服务器地址和端口,第二个参数为Session超时时间,第三个为节点变化时的回调方法 using (ZooKeeper zk = new ZooKeeper("121.199.25.195:2181", new TimeSpan(0, 0, 0, 50000), new Watcher())) { var stat = zk.Exists("/root", true); //创建一个节点root,数据是mydata,不进行ACL权限控制,节点为永久性的(即客户端shutdown了也不会消失) zk.Create("/root", "mydata".GetBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.Persistent); //在root下面创建一个childone znode,数据为childone,不进行ACL权限控制,节点为永久性的 zk.Create("/root/childone", "childone".GetBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.Persistent); //取得/root节点下的子节点名称,返回List<String> zk.GetChildren("/root", true); //取得/root/childone节点下的数据,返回byte[] zk.GetData("/root/childone", true, null); //修改节点/root/childone下的数据,第三个参数为版本,如果是-1,那会无视被修改的数据版本,直接改掉 zk.SetData("/root/childone", "childonemodify".GetBytes(), -1); //删除/root/childone这个节点,第二个参数为版本,-1的话直接删除,无视版本 zk.Delete("/root/childone", -1); } }
public ZkAdaptor() { watcher = new ZkWatcher() { DataChangedHandler = OnDataChanged, }; // zk handle // zkServer addr, session timeout, watcher handle = new ZooKeeper("192.168.0.103:2181,192.168.0.103:2182,192.168.0.103:2183,192.168.0.103:2184,192.168.0.103:2185", new TimeSpan(0, 0, 0, 50000), watcher); try { // create root node // no ACL // Persistent node handle.Create(RoutingRule.ZkRoutingRule.GetServiceRoot(), null, Ids.OPEN_ACL_UNSAFE, CreateMode.Persistent); } catch (KeeperException.NodeExistsException e) { // ignore } watcher.Handle = handle; }
/** * Barrier constructor * * @param address * @param root * @param size */ public Barrier(ZooKeeper zk, String root, int size, string nodeName) { _root = root; _size = size; _nodeName = nodeName; _zk = zk; // Create barrier node if (_zk == null) return; try { var s = _zk.Exists(root, false); if (s == null) _zk.Create(root, new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.Persistent); } catch (KeeperException e) { Console.WriteLine("Keeper exception when instantiating queue: " + e); } catch (Exception e) { //TODO } }
public virtual bool tryLock() { try { string splitStr = "_lock_"; if (lockName.Contains(splitStr)) { //throw new LockException("lockName can not contains \\u000B"); } //创建临时子节点 myZnode = zk.Create(root + "/" + lockName + splitStr, new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.EphemeralSequential); Console.WriteLine(myZnode + " is created "); //取出所有子节点 IList <string> subNodes = zk.GetChildren(root, false); //取出所有lockName的锁 IList <string> lockObjNodes = new List <string>(); foreach (string node in subNodes) { if (node.StartsWith(lockName)) { lockObjNodes.Add(node); } } Array alockObjNodes = lockObjNodes.ToArray(); Array.Sort(alockObjNodes); Console.WriteLine(myZnode + "==" + lockObjNodes[0]); if (myZnode.Equals(root + "/" + lockObjNodes[0])) { //如果是最小的节点,则表示取得锁 return(true); } //如果不是最小的节点,找到比自己小1的节点 string subMyZnode = myZnode.Substring(myZnode.LastIndexOf("/", StringComparison.Ordinal) + 1); waitNode = lockObjNodes[Array.BinarySearch(alockObjNodes, subMyZnode) - 1]; } catch (KeeperException e) { throw e; } return(false); }
public void testPing() { ZooKeeper zkIdle = null; ZooKeeper zkWatchCreator = null; try { CountdownWatcher watcher = new CountdownWatcher(); zkIdle = CreateClient(); zkWatchCreator = CreateClient(watcher); var node = Guid.NewGuid(); for (int i = 0; i < 10; i++) { zkWatchCreator.Create("/" + node + i, new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.Persistent); } for (int i = 0; i < 10; i++) { zkIdle.Exists("/" + node + i, true); } for (int i = 0; i < 10; i++) { Thread.Sleep(1000); zkWatchCreator.Delete("/" + node + i, -1); } // The bug will manifest itself here because zkIdle will expire zkIdle.Exists("/0", false); } finally { if (zkIdle != null) { zkIdle.Dispose(); } if (zkWatchCreator != null) { zkWatchCreator.Dispose(); } } }
static void Main(string[] args) { zookeeper = new ZooKeeper("192.168.183.134:2181,192.168.183.135:2181,192.168.183.136:2181", new TimeSpan(0, 0, 10), new Program()); manualWaitHandler.WaitOne(); //It's impossible to create path which the parent node does't exist //zookeeper.Create(@"/typhon", "123".GetBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.Persistent); //zookeeper.Create(@"/typhon/services", "123".GetBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.Persistent); // ASYNC_VerifyZookeeper(); string path = "/zk-book"; var testStat = zookeeper.Exists(path, false); if (testStat == null) { zookeeper.Create(path, "123".GetBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.Ephemeral); } Console.WriteLine(Encoding.UTF8.GetString(zookeeper.GetData(path, true, null))); Console.ReadLine(); }
/** * Join barrier * * @return * @throws KeeperException * @throws InterruptedException */ public bool Enter() { _zk.Create(_root + "/" + _nodeName, new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.EphemeralSequential); while (true) { lock (_mutex) { var children = _zk.GetChildren(_root, true); if (children.Count() < _size) { Monitor.Wait(_mutex); } else { return(true); } } } }
/// <summary> /// 创建分布式锁 /// </summary> /// <param name="lockName">竞争资源标志,lockName中不能包含单词lock</param> public ZooKeeprDistributedLock(string lockName) { this._lockName = lockName; // 创建一个与服务器的连接 try { _zk = new ZooKeeper(_connectString, _sessionTimeout, this); var sw = new Stopwatch(); sw.Start(); while (true) { if (Equals(_zk.State, States.CONNECTING)) { break; } if (Equals(_zk.State, States.CONNECTED)) { break; } } sw.Stop(); var ts2 = sw.Elapsed; Console.WriteLine($@"zoo连接总共花费{ts2.TotalMilliseconds}ms."); var stat = _zk.Exists(_root, false); if (stat == null) { // 创建根节点 _zk.Create(_root, new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.Persistent); } } catch (KeeperException e) { throw e; } }
static void Main(string[] args) { log4net.Config.XmlConfigurator.Configure(new FileInfo("zookeeperDemo.log4net")); zookeeper = new ZooKeeper("127.0.0.1:2181", new TimeSpan(0, 0, 10), new ZooKeeper_Auth_Usage()); zookeeper.AddAuthInfo("digest", "taokeeper:true".GetBytes()); manualWaitHandler.WaitOne(); //ASYNC_VerifyZookeeper(); string path = "/zk-book"; var testStat = zookeeper.Exists(path, false); if (testStat == null) { zookeeper.Create(path, "123".GetBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.Ephemeral); } var pathData = zookeeper.GetData(path, true, null); Console.WriteLine("The path's data(watched):{0}", System.Text.UTF8Encoding.UTF8.GetString(pathData)); // var errorzk = new ZooKeeper("127.0.0.1:2181", new TimeSpan(0, 0, 10), new ZooKeeper_Auth_Usage()); errorzk.AddAuthInfo("digest", "taokeeper:error".GetBytes()); manualWaitHandler.WaitOne(); var tempdata = errorzk.GetData(path, true, null); Console.WriteLine("The path's data(watched):{0}", System.Text.UTF8Encoding.UTF8.GetString(tempdata)); Console.ReadLine(); }
/// <summary> /// 创建分布式锁 /// </summary> /// <param name="lockName">竞争资源标志,lockName中不能包含单词lock</param> public ZooKeeprDistributedLock(string lockName) { this.lockName = lockName; // 创建一个与服务器的连接 try { zk = new ZooKeeper(connectString, sessionTimeout, this); Stopwatch sw = new Stopwatch(); sw.Start(); while (true) { if (zk.State == States.CONNECTING) { break; } if (zk.State == States.CONNECTED) { break; } } sw.Stop(); TimeSpan ts2 = sw.Elapsed; Console.WriteLine("zoo连接总共花费{0}ms.", ts2.TotalMilliseconds); var stat = zk.Exists(root, false); if (stat == null) { // 创建根节点 zk.Create(root, new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.Persistent); } } catch (KeeperException e) { throw e; } }
/// <summary> /// 创建一个路径节点 /// </summary> /// <param name="path">路径</param> /// <param name="value">值</param> /// <param name="createMode">创建模式</param> /// <returns></returns> public string CreateNode(string path, string value, CreateMode createMode) { return(ActionRetryHelper.Retry <string>(() => { Stat stat = ZooKeeper.Exists(path, false); byte[] data = value == null ? null : value.GetBytes(); if (stat == null) { return ZooKeeper.Create(path, data, Ids.OPEN_ACL_UNSAFE, createMode); } else { ZooKeeper.SetData(path, data, -1); } return path; }, 3, new TimeSpan(10), () => { }, (ex) => { //errorHandle LogManager.GetLogger() .Error(string.Format("DisconfClient.ZooKeeperClient.CreateNode(path={0},value={1},createMode={2}),ex:{3}", path, value, createMode, ex)); })); }
public String Create(string path, byte[] data, IEnumerable <Org.Apache.Zookeeper.Data.ACL> acl, CreateMode mode) { String name = null; int tries = retries; while ((tries--) > 0) { try { name = zk.Create(path, data, acl, mode); break; } catch (Exception ex) { if (tries == 0) { Console.WriteLine("Create exception after #" + retries + " retries :\n" + ex.Message); Console.WriteLine("Last retry, throwing exception"); throw ex; } } } return(name); }
public static void SetOtherACL() { var aclList = new List <ACL> { //world new ACL { Perms = Perms.ALL, Id = new ZKId { Id = "anyone", Scheme = "world" } }, //digest //密码必须是密文,先使用SHA1加密,然后base64编码 new ACL { Perms = Perms.READ, Id = new ZKId { Id = "user1:+owfoSBn/am19roBPzR1/MfCblE=", Scheme = "digest" } }, //ip new ACL { Perms = Perms.ALL, Id = new ZKId { Id = "172.18.23.56", Scheme = "ip" } }, //以172.18开头的ip地址 new ACL { Perms = Perms.ALL, Id = new ZKId { Id = "172.18.0.0/16", Scheme = "ip" } }, }; //创建节点时指定ACL zk.Create("/test/app", "hy".GetBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.Persistent); //单独设置ACL zk.SetACL("/test", aclList, -1); }
static void Main(string[] args) { //创建一个Zookeeper实例,第一个参数为目标服务器地址和端口,第二个参数为Session超时时间,第三个为节点变化时的回调方法 using (ZooKeeper zk = new ZooKeeper("127.0.0.1:2181", new TimeSpan(0, 0, 0, 50000), new Watcher())) { var stat = zk.Exists("/root", true); ////创建一个节点root,数据是mydata,不进行ACL权限控制,节点为永久性的(即客户端shutdown了也不会消失) //zk.Create("/root", "mydata".GetBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.Persistent); //在root下面创建一个childone znode,数据为childone,不进行ACL权限控制,节点为永久性的 zk.Create("/root/childone", "childone".GetBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.Persistent); //取得/root节点下的子节点名称,返回List<String> zk.GetChildren("/root", true); //取得/root/childone节点下的数据,返回byte[] zk.GetData("/root/childone", true, null); //修改节点/root/childone下的数据,第三个参数为版本,如果是-1,那会无视被修改的数据版本,直接改掉 zk.SetData("/root/childone", "childonemodify".GetBytes(), -1); //删除/root/childone这个节点,第二个参数为版本,-1的话直接删除,无视版本 zk.Delete("/root/childone", -1); Console.ReadKey(); } }
public void testBasic() { string name = "/" + Guid.NewGuid() + "foo"; zk.Create(name, name.GetBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.Persistent); Stat stat; stat = newStat(); zk.GetData(name, false, stat); Assert.AreEqual(stat.Czxid, stat.Mzxid); Assert.AreEqual(stat.Czxid, stat.Pzxid); Assert.AreEqual(stat.Ctime, stat.Mtime); Assert.AreEqual(0, stat.Cversion); Assert.AreEqual(0, stat.Version); Assert.AreEqual(0, stat.Aversion); Assert.AreEqual(0, stat.EphemeralOwner); Assert.AreEqual(name.Length, stat.DataLength); Assert.AreEqual(0, stat.NumChildren); }
private void verifyCreateFails(string path, ZooKeeper zk) { try { zk.Create(path, null, Ids.OPEN_ACL_UNSAFE, CreateMode.Persistent); } catch (InvalidOperationException e) { // this is good return; } Assert.Fail("bad path \"" + path + "\" not caught"); }
public void Publish2ZK() { string name = "/DHSFloodApexProgramPrototype"; zk.Create(name, name.GetBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.Persistent); string dataName = name + "/aPreparedness"; zk.Create(dataName, dataName.GetBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.Persistent); CreateFemaRegions(dataName); dataName = name + "/aResponse"; zk.Create(dataName, dataName.GetBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.Persistent); CreateFemaRegions(dataName); dataName = name + "/aRecovery"; zk.Create(dataName, dataName.GetBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.Persistent); CreateFemaRegions(dataName); /* * //https://earthobs1.arcgis.com/arcgis/rest/services/Live_Stream_Gauges/MapServer * var p = zk.GetChildren("/", false, null); * List<string> c_a = children_s; * var c_b = p; * c_a = c_a.OrderBy(e => e).ToList(); * c_b = c_b.OrderBy(e => e).ToList(); */ }
public void testPathValidation() { using (ZooKeeper zk = CreateClient()) { verifyCreateFails(null, zk); verifyCreateFails("", zk); verifyCreateFails("//", zk); verifyCreateFails("///", zk); verifyCreateFails("////", zk); verifyCreateFails("/.", zk); verifyCreateFails("/..", zk); verifyCreateFails("/./", zk); verifyCreateFails("/../", zk); verifyCreateFails("/foo/./", zk); verifyCreateFails("/foo/../", zk); verifyCreateFails("/foo/.", zk); verifyCreateFails("/foo/..", zk); verifyCreateFails("/./.", zk); verifyCreateFails("/../..", zk); verifyCreateFails("/\u0001foo", zk); verifyCreateFails("/foo/bar/", zk); verifyCreateFails("/foo//bar", zk); verifyCreateFails("/foo/bar//", zk); verifyCreateFails("foo", zk); verifyCreateFails("a", zk); string createseqpar = "/Createseqpar" + Guid.NewGuid(); zk.Create(createseqpar, null, Ids.OPEN_ACL_UNSAFE, CreateMode.Persistent); // next two steps - related to sequential processing // 1) verify that empty child name Assert.Fails if not sequential try { zk.Create(createseqpar, null, Ids.OPEN_ACL_UNSAFE, CreateMode.Persistent); Assert.True(false); } catch (Exception) { // catch this. } // 2) verify that empty child name success if sequential zk.Create(createseqpar, null, Ids.OPEN_ACL_UNSAFE, CreateMode.PersistentSequential); zk.Create(createseqpar + "/.", null, Ids.OPEN_ACL_UNSAFE, CreateMode.PersistentSequential); zk.Create(createseqpar + "/..", null, Ids.OPEN_ACL_UNSAFE, CreateMode.PersistentSequential); try { zk.Create(createseqpar + "//", null, Ids.OPEN_ACL_UNSAFE, CreateMode.PersistentSequential); Assert.True(false); } catch (InvalidOperationException) { // catch this. } try { zk.Create(createseqpar + "/./", null, Ids.OPEN_ACL_UNSAFE, CreateMode.PersistentSequential); Assert.True(false); } catch (Exception) { // catch this. } try { zk.Create(createseqpar + "/../", null, Ids.OPEN_ACL_UNSAFE, CreateMode.PersistentSequential); Assert.True(false); } catch (Exception) { // catch this. } zk.Create("/.foo" + Guid.NewGuid(), null, Ids.OPEN_ACL_UNSAFE, CreateMode.Persistent); zk.Create("/.f." + Guid.NewGuid(), null, Ids.OPEN_ACL_UNSAFE, CreateMode.Persistent); zk.Create("/..f" + Guid.NewGuid(), null, Ids.OPEN_ACL_UNSAFE, CreateMode.Persistent); zk.Create("/..f.." + Guid.NewGuid(), null, Ids.OPEN_ACL_UNSAFE, CreateMode.Persistent); zk.Create("/f.c" + Guid.NewGuid(), null, Ids.OPEN_ACL_UNSAFE, CreateMode.Persistent); zk.Create("/f\u0040f" + Guid.NewGuid(), null, Ids.OPEN_ACL_UNSAFE, CreateMode.Persistent); var f = "/f" + Guid.NewGuid(); zk.Create(f, null, Ids.OPEN_ACL_UNSAFE, CreateMode.Persistent); zk.Create(f + "/.f", null, Ids.OPEN_ACL_UNSAFE, CreateMode.Persistent); zk.Create(f + "/f.", null, Ids.OPEN_ACL_UNSAFE, CreateMode.Persistent); zk.Create(f + "/..f", null, Ids.OPEN_ACL_UNSAFE, CreateMode.Persistent); zk.Create(f + "/f..", null, Ids.OPEN_ACL_UNSAFE, CreateMode.Persistent); zk.Create(f + "/.f/f", null, Ids.OPEN_ACL_UNSAFE, CreateMode.Persistent); zk.Create(f + "/f./f", null, Ids.OPEN_ACL_UNSAFE, CreateMode.Persistent); } }
public void testMutipleWatcherObjs() { ZooKeeper zk = CreateClient(new CountdownWatcher()); try { MyWatcher[] watchers = new MyWatcher[100]; MyWatcher[] watchers2 = new MyWatcher[watchers.Length]; string name = "/" + Guid.NewGuid() + "foo-"; for (int i = 0; i < watchers.Length; i++) { watchers[i] = new MyWatcher(); watchers2[i] = new MyWatcher(); zk.Create(name + i, ("foodata" + i).GetBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.Persistent); } Stat stat = new Stat(); // // test get/Exists with single set of watchers // get all, then Exists all // for (int i = 0; i < watchers.Length; i++) { Assert.NotNull(zk.GetData(name + i, watchers[i], stat)); } for (int i = 0; i < watchers.Length; i++) { Assert.NotNull(zk.Exists(name + i, watchers[i])); } // trigger the watches for (int i = 0; i < watchers.Length; i++) { zk.SetData(name + i, ("foodata2-" + i).GetBytes(), -1); zk.SetData(name + i, ("foodata3-" + i).GetBytes(), -1); } for (int i = 0; i < watchers.Length; i++) { WatchedEvent @event; watchers[i].events.TryTake(out @event, TimeSpan.FromSeconds(3d)); Assert.Equal(name + i, @event.Path); Assert.Equal(EventType.NodeDataChanged, @event.Type); Assert.Equal(KeeperState.SyncConnected, @event.State); // small chance that an unexpected message was delivered // after this check, but we would catch that next time // we check events Assert.Equal(0, watchers[i].events.Count); } // // test get/Exists with single set of watchers // get/Exists together // for (int i = 0; i < watchers.Length; i++) { Assert.NotNull(zk.GetData(name + i, watchers[i], stat)); Assert.NotNull(zk.Exists(name + i, watchers[i])); } // trigger the watches for (int i = 0; i < watchers.Length; i++) { zk.SetData(name + i, ("foodata4-" + i).GetBytes(), -1); zk.SetData(name + i, ("foodata5-" + i).GetBytes(), -1); } for (int i = 0; i < watchers.Length; i++) { WatchedEvent @event; watchers[i].events.TryTake(out @event, TimeSpan.FromSeconds(10d)); Assert.Equal(name + i, @event.Path); Assert.Equal(EventType.NodeDataChanged, @event.Type); Assert.Equal(KeeperState.SyncConnected, @event.State); // small chance that an unexpected message was delivered // after this check, but we would catch that next time // we check events Assert.Equal(0, watchers[i].events.Count); } // // test get/Exists with two sets of watchers // for (int i = 0; i < watchers.Length; i++) { Assert.NotNull(zk.GetData(name + i, watchers[i], stat)); Assert.NotNull(zk.Exists(name + i, watchers2[i])); } // trigger the watches for (int i = 0; i < watchers.Length; i++) { zk.SetData(name + i, ("foodata6-" + i).GetBytes(), -1); zk.SetData(name + i, ("foodata7-" + i).GetBytes(), -1); } for (int i = 0; i < watchers.Length; i++) { WatchedEvent @event; watchers[i].events.TryTake(out @event, TimeSpan.FromSeconds(3000)); Assert.Equal(name + i, @event.Path); Assert.Equal(EventType.NodeDataChanged, @event.Type); Assert.Equal(KeeperState.SyncConnected, @event.State); // small chance that an unexpected message was delivered // after this check, but we would catch that next time // we check events Assert.Equal(0, watchers[i].events.Count); // watchers2 WatchedEvent event2; watchers2[i].events.TryTake(out @event2, TimeSpan.FromSeconds(3000)); Assert.Equal(name + i, event2.Path); Assert.Equal(EventType.NodeDataChanged, event2.Type); Assert.Equal(KeeperState.SyncConnected, event2.State); // small chance that an unexpected message was delivered // after this check, but we would catch that next time // we check events Assert.Equal(0, watchers2[i].events.Count); } } finally { if (zk != null) { zk.Dispose(); } } }
public void testChrootSynchronous() { string ch1 = "/" + Guid.NewGuid() + "ch1"; using (ZooKeeper zk1 = CreateClient()) { Assert.AreEqual(ch1, zk1.Create(ch1, null, Ids.OPEN_ACL_UNSAFE, CreateMode.Persistent)); } string ch2 = "/" + Guid.NewGuid() + "ch2"; using (ZooKeeper zk2 = CreateClient(ch1)) { Assert.AreEqual(ch2, zk2.Create(ch2, null, Ids.OPEN_ACL_UNSAFE, CreateMode.Persistent)); } using (ZooKeeper zk1 = CreateClient()) using (ZooKeeper zk2 = CreateClient(ch1)) { // check get MyWatcher w1 = new MyWatcher("w1", ch1); Assert.NotNull(zk1.Exists(ch1, w1)); string ch1Ch2 = string.Format("{0}{1}", ch1, ch2); MyWatcher w2 = new MyWatcher("w2", ch1Ch2); Assert.NotNull(zk1.Exists(ch1Ch2, w2)); MyWatcher w3 = new MyWatcher("w3", ch2); Assert.NotNull(zk2.Exists(ch2, w3)); // set watches on child MyWatcher w4 = new MyWatcher("w4", ch1); zk1.GetChildren(ch1, w4); MyWatcher w5 = new MyWatcher("w5", "/"); zk2.GetChildren("/", w5); // check set zk1.SetData(ch1, Encoding.UTF8.GetBytes("1"), -1); zk2.SetData(ch2, "2".GetBytes(), -1); // check watches Assert.True(w1.Matches()); Assert.True(w2.Matches()); Assert.True(w3.Matches()); // check exceptions string ch3 = "/" + Guid.NewGuid() + "ch3"; try { zk2.SetData(ch3, "3".GetBytes(), -1); } catch (KeeperException.NoNodeException e) { Assert.AreEqual(ch3, e.getPath()); } Assert.AreEqual("1".GetBytes(), zk1.GetData(ch1, false, null)); Assert.AreEqual("2".GetBytes(), zk1.GetData(ch1Ch2, false, null)); Assert.AreEqual("2".GetBytes(), zk2.GetData(ch2, false, null)); // check delete zk2.Delete(ch2, -1); Assert.True(w4.Matches()); Assert.True(w5.Matches()); zk1.Delete(ch1, -1); Assert.Null(zk1.Exists(ch1, false)); Assert.Null(zk1.Exists(ch1Ch2, false)); Assert.Null(zk2.Exists(ch2, false)); } }
private void FindPrefixInChildren(String prefix, ZooKeeper zookeeper, String dir) { List<String> names = Zookeeper.GetChildren(dir, false); foreach (string name in names) { if (name.StartsWith(prefix)) { id = name; if (LOG.IsDebugEnabled) { LOG.Debug("Found id created last time: " + id); } break; } } if (id == null) { id = zookeeper.Create(dir.Combine(prefix), data, Acl, CreateMode.EphemeralSequential); if (LOG.IsDebugEnabled) { LOG.Debug("Created id: " + id); } } }
public void testRootAcl() { ZooKeeper zk = CreateClient(); // set auth using digest zk.AddAuthInfo("digest", Encoding.UTF8.GetBytes("pat:test")); zk.SetACL("/", Ids.CREATOR_ALL_ACL, -1); zk.GetData("/", false, null); zk.Dispose(); // verify no access zk = CreateClient(); try { zk.GetData("/", false, null); Assert.Fail("validate auth"); } catch (KeeperException.NoAuthException e) { // expected } string path = "/" + Guid.NewGuid() + "apps"; try { zk.Create(path, null, Ids.CREATOR_ALL_ACL, CreateMode.Persistent); Assert.Fail("validate auth"); } catch (KeeperException.InvalidACLException e) { // expected } zk.AddAuthInfo("digest", Encoding.UTF8.GetBytes("world:anyone")); try { zk.Create(path, null, Ids.CREATOR_ALL_ACL, CreateMode.Persistent); Assert.Fail("validate auth"); } catch (KeeperException.NoAuthException e) { // expected } zk.Dispose(); // verify access using original auth zk = CreateClient(); zk.AddAuthInfo("digest", Encoding.UTF8.GetBytes("pat:test")); zk.GetData("/", false, null); zk.Create(path, null, Ids.CREATOR_ALL_ACL, CreateMode.Persistent); zk.Delete(path, -1); // reset acl (back to open) and verify accessible again zk.SetACL("/", Ids.OPEN_ACL_UNSAFE, -1); zk.Dispose(); zk = CreateClient(); zk.GetData("/", false, null); zk.Create(path, null, Ids.OPEN_ACL_UNSAFE, CreateMode.Persistent); try { zk.Create(path, null, Ids.CREATOR_ALL_ACL, CreateMode.Persistent); Assert.Fail("validate auth"); } catch (KeeperException.InvalidACLException e) { // expected } zk.Delete(path, -1); zk.AddAuthInfo("digest", Encoding.UTF8.GetBytes("world:anyone")); zk.Create(path, null, Ids.CREATOR_ALL_ACL, CreateMode.Persistent); zk.Dispose(); zk = CreateClient(); zk.Delete(path, -1); }
/// <summary> /// 统一配置服务器站点的初始化 /// </summary> public void InitServer() { var task = Task.Run(() => { this._handler.Execute(() => { //目前没考虑自定义配置 ,这里配置文件统一放置到zookeeper文件夹下 //同步zookeeper文件信息到zookeeper服务 if (!System.IO.Directory.Exists(RootPath)) { System.IO.Directory.CreateDirectory(RootPath); } //第一级目录,我设想存放的是站点名称,我就存放站点对应的Key好了,类似1、2、3、4,你也可以自行扩展 var dirs = System.IO.Directory.GetDirectories(RootPath); foreach (string dir in dirs) { string name = Path.GetFileName(dir); using (MaintainWatcher mk = new MaintainWatcher(config.Host, 1000)) { ZooKeeper zk = mk.ZooKeeper; var stat = zk.Exists($"/{name}", false); if (stat == null) { zk.Create($"/{name}", "".GetBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.Persistent); } var files = Directory.GetFiles(dir, "*.txt"); foreach (string file in files) { string key = Path.GetFileNameWithoutExtension(file); stat = zk.Exists($"/{name}/{key}", false); string text = FileHelper.ReadFile(file); ZNode znode = Deserialize(text); if (znode != null) { if (stat == null) { zk.Create($"/{name}/{key}", znode.ToString().GetBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.Persistent); } else { //文件的版本号比znode大,这样才去更新zookeeper服务器 //if (znode.Version > stat.Version) { //下面没有事物处理,可能会有问题 var tmpdata = zk.GetData($"/{name}/{key}", false, null); string tmpString = System.Text.Encoding.UTF8.GetString(tmpdata); stat = zk.SetData($"/{name}/{key}", znode.ToString().GetBytes(), -1); znode.Version = stat.Version; FileHelper.WriteFile(file, znode.ToString()); } } } } } } }, string.Empty); }); task.Wait(); }
public static void Create(string path, string data) { var nodeData = data == null ? null : data.GetBytes(); Zk.Create(path, nodeData, Ids.OPEN_ACL_UNSAFE, CreateMode.Persistent); }