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); } }
/// <summary>Update the path with prepending version number and hostname</summary> /// <param name="path">- to be updated in zookeeper</param> /// <exception cref="System.IO.IOException"/> internal virtual void Update(string path) { BKJournalProtos.CurrentInprogressProto.Builder builder = BKJournalProtos.CurrentInprogressProto .NewBuilder(); builder.SetPath(path).SetHostname(hostName); string content = TextFormat.PrintToString(((BKJournalProtos.CurrentInprogressProto )builder.Build())); try { zkc.SetData(this.currentInprogressNode, Sharpen.Runtime.GetBytesForString(content , Charsets.Utf8), this.versionNumberForPermission); } catch (KeeperException e) { throw new IOException("Exception when setting the data " + "[" + content + "] to CurrentInprogress. " , e); } catch (Exception e) { throw new IOException("Interrupted while setting the data " + "[" + content + "] to CurrentInprogress" , e); } if (Log.IsDebugEnabled()) { Log.Debug("Updated data[" + content + "] to CurrentInprogress"); } }
//static List<Org.Apache.Zookeeper.Data.ACL> CreateAllAccessACLList() //{ // var list = new List<Org.Apache.Zookeeper.Data.ACL>(); // var ZKid = new Org.Apache.Zookeeper.Data.ZKId("world", "anyone"); // var acl = new Org.Apache.Zookeeper.Data.ACL(ZooKeeperNet.Perms.ALL, ZKid); // list.Add(acl); // return list; //} static void Main(string[] args) { string path = "/zk-book"; zk = new ZooKeeper("127.0.0.1:2181", new TimeSpan(0, 0, 5), new ZooKeeper_SetData_API_Sync_Usage()); manualWaitHandler.WaitOne(); if (zk.Exists(path, false) == null) { zk.Create(path, "123".GetBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.Ephemeral); } Console.WriteLine(zk.GetData(path, true, null)); var stat = zk.SetData(path, "456".GetBytes(), -1); Console.WriteLine("{0}, {1}, {2}", stat.Czxid, stat.Mzxid, stat.Version); var stat2 = zk.SetData(path, "456".GetBytes(), stat.Version); Console.WriteLine("{0}, {1}, {2}", stat2.Czxid, stat2.Mzxid, stat2.Version); try { zk.SetData(path, "456".GetBytes(), stat.Version); } catch (Exception ex) { Console.WriteLine("Error: " + ex.ToString()); } Console.ReadLine(); }
/// <summary> /// 设置某个路径的值 /// </summary> /// <param name="path">路径</param> /// <param name="value">值</param> public void SetData(string path, string value) { ActionRetryHelper.Retry(() => { Stat stat = ZooKeeper.Exists(path, false); byte[] data = value == null ? null : value.GetBytes(); if (stat == null) { ZooKeeper.Create(path, data, Ids.OPEN_ACL_UNSAFE, CreateMode.Persistent); } else { ZooKeeper.SetData(path, data, stat.Version); } }, 3, new TimeSpan(10), () => { //exceptionAction }, (ex) => { //errorHandle LogManager.GetLogger().Error(string.Format("DisconfClient.ZooKeeperClient.SetData(path={0},value={1}),ex:{2}", path, value, ex)); }, () => { //retryExceptionAction LogManager.GetLogger().Error(string.Format("DisconfClient.ZooKeeperClient.SetData(path={0},value={1}),error", path, value)); }); }
static void Main(string[] args) { // 创建一个Zookeeper实例,第一个参数为目标服务器地址和端口,第二个参数为Session超时时间,第三个为节点变化时的回调方法 using (var 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 Stat SetData <T>(string nodePath, T data, int version) where T : new() { Stat stat = null; byte[] serializedData = ZNodesDataStructures.serialize <T>(data); int tries = retries; while ((tries--) > 0) { try { stat = zk.SetData(nodePath, serializedData, version); break; } catch (Exception ex) { if (tries == 0) { Console.WriteLine("SetData exception after #" + retries + " retries :\n" + ex.Message); Console.WriteLine("Last retry, throwing exception"); throw ex; } } } return(stat); }
public void UpdateNode(string path, byte[] data) { if (!string.IsNullOrEmpty(path)) { zk.SetData(path, data, zk.Exists(path, true).Version); } }
/// <exception cref="System.IO.IOException"/> internal virtual void Reset(long maxTxId) { lock (this) { try { BKJournalProtos.MaxTxIdProto.Builder builder = BKJournalProtos.MaxTxIdProto.NewBuilder ().SetTxId(maxTxId); byte[] data = Sharpen.Runtime.GetBytesForString(TextFormat.PrintToString(((BKJournalProtos.MaxTxIdProto )builder.Build())), Charsets.Utf8); if (currentStat != null) { currentStat = zkc.SetData(path, data, currentStat.GetVersion()); } else { zkc.Create(path, data, ZooDefs.Ids.OpenAclUnsafe, CreateMode.Persistent); } } catch (KeeperException e) { throw new IOException("Error writing max tx id", e); } catch (Exception e) { throw new IOException("Interrupted while writing max tx id", e); } } }
/// <summary> /// Writes data for a given path /// </summary> /// <param name="path"> /// The given path. /// </param> /// <param name="data"> /// The data to write. /// </param> /// <param name="version"> /// Expected version of data /// </param> public void WriteData(string path, byte[] data, int version) { Guard.NotNullNorEmpty(path, "path"); EnsuresNotDisposedAndNotNull(); _zkclient.SetData(path, data, version); }
public bool DataEdit(string Path, string data) { int version = 1; byte[] utf8Bytes = System.Text.Encoding.UTF8.GetBytes(data); zk.SetData(Path, utf8Bytes, version); return(true); }
public virtual void TestEmptyInprogressNode() { URI uri = BKJMUtil.CreateJournalURI("/hdfsjournal-emptyInprogress"); NamespaceInfo nsi = NewNSInfo(); BookKeeperJournalManager bkjm = new BookKeeperJournalManager(conf, uri, nsi); bkjm.Format(nsi); EditLogOutputStream @out = bkjm.StartLogSegment(1, NameNodeLayoutVersion.CurrentLayoutVersion ); for (long i = 1; i <= 100; i++) { FSEditLogOp op = FSEditLogTestUtil.GetNoOpInstance(); op.SetTransactionId(i); @out.Write(op); } @out.Close(); bkjm.FinalizeLogSegment(1, 100); @out = bkjm.StartLogSegment(101, NameNodeLayoutVersion.CurrentLayoutVersion); @out.Close(); bkjm.Close(); string inprogressZNode = bkjm.InprogressZNode(101); zkc.SetData(inprogressZNode, new byte[0], -1); bkjm = new BookKeeperJournalManager(conf, uri, nsi); try { bkjm.RecoverUnfinalizedSegments(); NUnit.Framework.Assert.Fail("Should have failed. There should be no way of creating" + " an empty inprogess znode"); } catch (IOException e) { // correct behaviour NUnit.Framework.Assert.IsTrue("Exception different than expected", e.Message.Contains ("Invalid/Incomplete data in znode")); } finally { bkjm.Close(); } }
public void SetData(string input) { try { zk.SetData("/configuration", Encoding.UTF8.GetBytes(input), -1); } catch (KeeperException.NoNodeException ex) { throw new Exception("节点不存在"); } }
public Stat SetZookeeperNodeData(string path, byte[] data, int version) { if (_zk != null) { return(_zk.SetData(path, data, version)); } else { return(null); } }
/// <summary> /// 创建永久节点 /// </summary> /// <param name="zk"></param> /// <param name="path"></param> /// <param name="data"></param> void CreatePersistentIfNot(ZooKeeper zk, string path, string data = "") { var stat = zk.Exists(path, false); if (stat == null) { zk.Create(path, data.GetBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.Persistent); } else { zk.SetData(path, data.GetBytes(), stat.Version); } }
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_Watch_Usage()); 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)); Console.WriteLine("begin to changed the data of path:{0}, which is watched.", path); zookeeper.SetData(path, "f**k you".GetBytes(), -1); Console.WriteLine("change data of path:{0} is finished, which is watched.", path); var newPath = "/ACID"; zookeeper.Create(newPath, "AAAA".GetBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.Ephemeral); var acidData = zookeeper.GetData(newPath, false, null); Console.WriteLine("The new created node's data(Unwatched):{0}", System.Text.UTF8Encoding.UTF8.GetString(acidData)); Console.WriteLine("begin to changed the data of path:{0}, which is UNwatched.", newPath); zookeeper.SetData(newPath, "laoHUYou".GetBytes(), -1); Console.WriteLine("change data of path:{0} is finished, which is Unwatched.", newPath); Console.ReadLine(); }
public bool UpdateNodeValue(string nodePath, string newNodeValue, ref string message) { try { byte[] newData = Encoding.UTF8.GetBytes(newNodeValue); using (ZooKeeper zk = new ZooKeeper(AppSettingsHelper.ZKServer, TimeSpan.FromSeconds(AppSettingsHelper.ZKSessionTimeOut), null)) { ZooKeeper.WaitUntilConnected(zk); Stat stat = zk.Exists(nodePath, false); if (stat == null) { message = string.Format("该节点【{0}】已被别人删除。", nodePath); return(false); } byte[] oldValue = zk.GetData(nodePath, false, stat); if (oldValue == null || oldValue.Length < 1) { if (string.IsNullOrWhiteSpace(newNodeValue)) { message = "和原来的值一样,所以无需更新。"; return(false); } } string oldValueStr = string.Empty; if (oldValue != null && oldValue.Length > 0) { oldValueStr = Encoding.UTF8.GetString(oldValue); } if (newNodeValue.Equals(oldValueStr)) { message = "和原来的值一样,所以无需更新。"; return(false); } Stat newStat = zk.SetData(nodePath, newData, stat.Version); return(true); } } catch (Exception ex) { message = string.Format("JinRi.Fx.Manage:{0}UpdateNodeValue()方法抛异常:{0}[{1}]{2}。", Environment.NewLine, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), ex.ToString()); log.Error(message); message = string.Format("UpdateNodeValue()方法抛异常:{0}[{1}]{2}。", Environment.NewLine, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), ex.ToString()); return(false); } }
private void setData(string value) { try { Console.WriteLine("[test] trying to create"); _zk.Create(Path, Encoding.UTF8.GetBytes(value), Ids.OPEN_ACL_UNSAFE, CreateMode.Ephemeral); Console.WriteLine("Created"); } catch (Exception) { Console.WriteLine("[test] already exists...setting data."); _zk.SetData(Path, Encoding.UTF8.GetBytes(value), -1); Console.WriteLine("data set"); } }
public void Start(NodeSetting setting) { var connectString = setting.ZkServer.Ip + ":" + setting.ZkServer.Port; zookeeper = new ZooKeeper(connectString, TimeSpan.MaxValue, null); var stat = zookeeper.Exists("/live_nodes/" + setting.Client.Ip, false); if (stat == null) { zookeeper.Create("/live_nodes/" + setting.Client.Ip, Encoding.UTF8.GetBytes(""), null, CreateMode.Ephemeral); } else { zookeeper.SetData("/live_nodes/" + setting.Client.Ip, Encoding.UTF8.GetBytes(""), -1); } }
/// <summary> /// 添加或者设置data /// </summary> /// <param name="zkPath"></param> /// <param name="data"></param> public void AddOrSetData(string zkPath, byte[] data) { _queue.Enqueue(() => { var stat = ZooKeeper.Exists(zkPath, false); if (stat != null) { //先删除子节点,再更新值保证不会出现客户端已经更新完并新增了节点,而服务端还没删完的情况 ZooKeeper.RemoveTmpChildNode(zkPath); ZooKeeper.SetData(zkPath, data, -1); } else { ZooKeeper.CreateWithPath(zkPath, data, Ids.OPEN_ACL_UNSAFE, CreateMode.Persistent); } }); }
static void Main(string[] args) { string path = "/zk-book"; zk = new ZooKeeper("127.0.0.1:2181", new TimeSpan(0, 0, 5), new ZooKeeper_GetData_API_Sync_Usage()); manualWaitHandler.WaitOne(); zk.Create(path, "123".GetBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.Ephemeral); Console.WriteLine(zk.GetData(path, true, stat)); Console.WriteLine("{0}, {1}, {2}", stat.Czxid, stat.Mzxid, stat.Version); zk.SetData(path, "123".GetBytes(), -1); Console.ReadLine(); }
//fixed by Yang Li: 必须根据实际将要被更新值的节点Version,进行更新值: public Stat SetZookeeperNodeData(string path, byte[] data) //public Stat SetZookeeperNodeData(string path, byte[] data, int version) { if (_zk != null) { //fixed by Yang Li: 必须根据实际将要被更新值的节点Version,进行更新值: Stat currentSat = _zk.Exists(path, false); if (currentSat != null) { return(_zk.SetData(path, data, currentSat.Version)); } return(null); //return _zk.SetData(path, data, version); } else { return(null); } }
/// <summary> /// 添加服务路由。 /// </summary> /// <param name="routes">服务路由集合。</param> /// <returns>一个任务。</returns> public Task AddRoutesAsync(IEnumerable <ServiceRoute> routes) { return(Task.Run(() => { if (_logger.IsEnabled(LogLevel.Information)) { _logger.Information("准备添加服务路由。"); } CreateSubdirectory(_configInfo.RoutePath); var path = _configInfo.RoutePath; if (!path.EndsWith("/")) { path += "/"; } foreach (var serviceRoute in routes) { var nodePath = $"{path}{serviceRoute.ServiceDescriptor.Id}"; var nodeData = _serializer.Serialize(serviceRoute); if (_zooKeeper.Exists(nodePath, false) == null) { if (_logger.IsEnabled(LogLevel.Debug)) { _logger.Debug($"节点:{nodePath}不存在将进行创建。"); } _zooKeeper.Create(nodePath, nodeData, ZooKeeperNet.Ids.OPEN_ACL_UNSAFE, CreateMode.Persistent); } else { if (_logger.IsEnabled(LogLevel.Debug)) { _logger.Debug($"将更新节点:{nodePath}的数据。"); } _zooKeeper.SetData(nodePath, nodeData, -1); } } if (_logger.IsEnabled(LogLevel.Information)) { _logger.Information("服务路由添加成功。"); } })); }
private void Run(object sender) { try { CancellationToken token = (CancellationToken)sender; using (ZooKeeper zooKeeper = CreateClient(this)) { Stat stat = new Stat(); if (zooKeeper.Exists("/unittests/recoverytests", false) == null) { zooKeeper.Create("/unittests", new byte[] { 0 }, Ids.OPEN_ACL_UNSAFE, CreateMode.Persistent); zooKeeper.Create("/unittests/recoverytests", new byte[] { 0 }, Ids.OPEN_ACL_UNSAFE, CreateMode.Persistent); } if (zooKeeper.Exists(testPath, false) == null) { zooKeeper.Create(testPath, new byte[] { 0 }, Ids.OPEN_ACL_UNSAFE, CreateMode.Persistent); } while (zooKeeper.State.IsAlive() && !token.IsCancellationRequested) { try { zooKeeper.GetData(testPath, true, stat); zooKeeper.SetData(testPath, Guid.NewGuid().ToByteArray(), -1); setDataCount++; } catch (KeeperException ke) { LOG.Error(ke); } } LOG.Error("Waiting for dispose."); } } catch (Exception ex) { LOG.Error(ex); } }
/// <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 void testDataSizeChange() { 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); zk.SetData(name, (name + name).GetBytes(), -1); stat = newStat(); zk.GetData(name, false, stat); Assert.AreNotSame(stat.Czxid, stat.Mzxid); Assert.AreEqual(stat.Czxid, stat.Pzxid); Assert.AreNotSame(stat.Ctime, stat.Mtime); Assert.AreEqual(0, stat.Cversion); Assert.AreEqual(1, stat.Version); Assert.AreEqual(0, stat.Aversion); Assert.AreEqual(0, stat.EphemeralOwner); Assert.AreEqual(name.Length * 2, stat.DataLength); Assert.AreEqual(0, stat.NumChildren); }
public void SetDataToNode(string path, string data) { zk.SetData(path, data.GetBytes(), -1); }
public static void SetData(string path, string data, int version) { var nodeData = data == null ? null : data.GetBytes(); Zk.SetData(path, nodeData, version); }
/// <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 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)); } }
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(); } } }