public async Task testACLs() { ZooKeeper zk = await createClient(); try { await zk.createAsync("/acltest", new byte[0], ZooDefs.Ids.CREATOR_ALL_ACL, CreateMode.PERSISTENT); Assert.fail("Should have received an invalid acl error"); } catch (KeeperException.InvalidACLException e) { LOG.info("Test successful, invalid acl received : " + e.Message); } try { List <ACL> testACL = new List <ACL>(); testACL.Add(new ACL((int)(ZooDefs.Perms.ALL | ZooDefs.Perms.ADMIN), ZooDefs.Ids.AUTH_IDS)); testACL.Add(new ACL((int)(ZooDefs.Perms.ALL | ZooDefs.Perms.ADMIN), new Id("ip", "127.0.0.1/8"))); await zk.createAsync("/acltest", new byte[0], testACL, CreateMode.PERSISTENT); Assert.fail("Should have received an invalid acl error"); } catch (KeeperException.InvalidACLException e) { LOG.info("Test successful, invalid acl received : " + e.Message); } zk.addAuthInfo("digest", "ben:passwd".UTF8getBytes()); await zk.createAsync("/acltest", new byte[0], ZooDefs.Ids.CREATOR_ALL_ACL, CreateMode.PERSISTENT); await zk.closeAsync(); zk = await createClient(); zk.addAuthInfo("digest", "ben:passwd2".UTF8getBytes()); try { await zk.getDataAsync("/acltest", false); Assert.fail("Should have received a permission error"); } catch (KeeperException e) { Assert.assertEquals(KeeperException.Code.NOAUTH, e.getCode()); } zk.addAuthInfo("digest", "ben:passwd".UTF8getBytes()); await zk.getDataAsync("/acltest", false); await zk.setACLAsync("/acltest", ZooDefs.Ids.OPEN_ACL_UNSAFE, -1); await zk.closeAsync(); zk = await createClient(); await zk.getDataAsync("/acltest", false); IList <ACL> acls = (await zk.getACLAsync("/acltest")).Acls; Assert.assertEquals(1, acls.Count); Assert.assertEquals(ZooDefs.Ids.OPEN_ACL_UNSAFE, acls); // The stat parameter should be optional. acls = (await zk.getACLAsync("/acltest")).Acls; Assert.assertEquals(1, acls.Count); Assert.assertEquals(ZooDefs.Ids.OPEN_ACL_UNSAFE, acls); }
public async Task <byte[]> GetDataAsync([NotNull] string path) { ValidateState(); var dataResult = await _zk.getDataAsync(path, true); return(dataResult.Data); }
/// <summary> /// 监听节点变化 /// </summary> /// <param name="path"></param> /// <param name="watcher"></param> /// <returns></returns> public async Task <string> WatchNode(string path, Watcher watcher) { var result = await _zk.getDataAsync(path, watcher); if (result == null) { return(""); } return(Encoding.UTF8.GetString(result.Data)); }
public async Task <string> GetData(string path) { var dataResult = await RetryUntilConnected(async() => await _zk.getDataAsync(path, HasListeners(path))); if (dataResult.Data != null && dataResult.Data.Length > 0) { return(Encoding.UTF8.GetString(dataResult.Data)); } return(string.Empty); }
/** * Returns node's data */ public async Task <byte[]> getData(string path, Watcher watcher, Stat stat, bool retryOnConnLoss) { if (retryOnConnLoss) { return(await _zkCmdExecutor.RetryOperation(async() => (await keeper.getDataAsync(path, watcher)).Data)); } else { return((await keeper.getDataAsync(path, watcher)).Data); } }
public static async Task <byte[]> GetDataOrThrow_(this ZooKeeper client, string path, Watcher watcher = null) { __check_path__(path); var res = watcher == null ? await client.getDataAsync(path, watch : false) : await client.getDataAsync(path, watcher); if (res.Stat == null) { throw new Exception(res?.ToJson() ?? "数据不存在"); } return(res.Data); }
/// <summary> /// 设置服务命令。 /// </summary> /// <param name="routes">服务命令集合。</param> /// <returns>一个任务。</returns> public override async Task SetServiceCommandsAsync(IEnumerable <ServiceCommandDescriptor> serviceCommand) { if (_logger.IsEnabled(LogLevel.Information)) { _logger.LogInformation("准备添加服务命令。"); } await CreateSubdirectory(_configInfo.CommandPath); var path = _configInfo.CommandPath; if (!path.EndsWith("/")) { path += "/"; } serviceCommand = serviceCommand.ToArray(); foreach (var command in serviceCommand) { var nodePath = $"{path}{command.ServiceId}"; var nodeData = _serializer.Serialize(command); if (await _zooKeeper.existsAsync(nodePath) == null) { if (_logger.IsEnabled(LogLevel.Debug)) { _logger.LogDebug($"节点:{nodePath}不存在将进行创建。"); } await _zooKeeper.createAsync(nodePath, nodeData, ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); } else { if (_logger.IsEnabled(LogLevel.Debug)) { _logger.LogDebug($"将更新节点:{nodePath}的数据。"); } var onlineData = (await _zooKeeper.getDataAsync(nodePath)).Data; if (!DataEquals(nodeData, onlineData)) { await _zooKeeper.setDataAsync(nodePath, nodeData); } } NodeChange(command); } if (_logger.IsEnabled(LogLevel.Information)) { _logger.LogInformation("服务命令添加成功。"); } }
/// <summary> /// 设置服务路由。 /// </summary> /// <param name="routes">服务路由集合。</param> /// <returns>一个任务。</returns> protected override async Task SetRoutesAsync(IEnumerable <ServiceRouteDescriptor> routes) { if (_logger.IsEnabled(LogLevel.Information)) { _logger.LogInformation("准备添加服务路由。"); } await CreateSubdirectory(_configInfo.RoutePath); var path = _configInfo.RoutePath; if (!path.EndsWith("/")) { path += "/"; } routes = routes.ToArray(); foreach (var serviceRoute in routes) { var nodePath = $"{path}{serviceRoute.ServiceDescriptor.Id}"; var nodeData = _serializer.Serialize(serviceRoute); if (await _zooKeeper.existsAsync(nodePath) == null) { if (_logger.IsEnabled(LogLevel.Debug)) { _logger.LogDebug($"节点:{nodePath}不存在将进行创建。"); } await _zooKeeper.createAsync(nodePath, nodeData, ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); } else { if (_logger.IsEnabled(LogLevel.Debug)) { _logger.LogDebug($"将更新节点:{nodePath}的数据。"); } var onlineData = (await _zooKeeper.getDataAsync(nodePath)).Data; if (!DataEquals(nodeData, onlineData)) { await _zooKeeper.setDataAsync(nodePath, nodeData); } } } if (_logger.IsEnabled(LogLevel.Information)) { _logger.LogInformation("服务路由添加成功。"); } }
private static void ReadVnetThread(object o) { var input = o as Tuple <string, IEnumerable <string> >; var endpoints = input.Item1; var vnets = input.Item2; var zk = new ZooKeeper((string)endpoints, 10000, null); var acls = new List <ACL> { new ACL((int)ZooDefs.Perms.ALL, ZooDefs.Ids.ANYONE_ID_UNSAFE), }; foreach (var vnet in vnets) { ReadInternal(vnet).GetAwaiter().GetResult(); } Interlocked.Increment(ref threadFinished); async Task ReadInternal(string vnet) { var parent = string.Join("/", vnet, "mappings", "v4ca"); var children = (await zk.getChildrenAsync(parent)).Children; var tasks = children.Select(child => zk.getDataAsync(string.Join("/", parent, child))); await Task.WhenAll(tasks); Interlocked.Add(ref nodeCount, children.Count + 1); } }
internal async void Run() { var data = Encoding.UTF8.GetBytes("test"); ZooKeeper zk = new ZooKeeper("localhost:2181", 50000, NullWatcher.Instance); var zNodeRoot = zk.existsAsync("/").Result.getCversion(); var zNodeA = zk.existsAsync("/a").Result; if (zNodeA == null) { await zk.createAsync("/a", null, ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); } var node = zk.existsAsync("/a/1").Result; Task <string> nodetask = null; if (node == null) { nodetask = zk.createAsync("/a/1", data, ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL); } nodetask.Wait(); IList <string> children = zk.getChildrenAsync("/a", false).Result.Children; var test2 = zk.getDataAsync("/a", NodeWatcher.Instance).Result; var test3 = zk.setDataAsync("/a", data).Result; var closeEvent = zk.closeAsync(); closeEvent.Wait(); }
public async Task testWatcherCorrectness() { ZooKeeper zk = null; MyWatcher watcher = new MyWatcher(); zk = await createClient(watcher); string[] names = new string[10]; for (int i = 0; i < names.Length; i++) { string name = await zk.createAsync("/tc-", "initialvalue".UTF8getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT_SEQUENTIAL); names[i] = name; Stat stat = (await zk.getDataAsync(name, watcher)).Stat; await zk.setDataAsync(name, "new".UTF8getBytes(), stat.getVersion()); stat = await zk.existsAsync(name, watcher); await zk.deleteAsync(name, stat.getVersion()); } for (int i = 0; i < names.Length; i++) { string name = names[i]; WatchedEvent @event = watcher.events.poll(10 * 1000); Assert.assertEquals(name, @event.getPath()); Assert.assertEquals(Watcher.Event.EventType.NodeDataChanged, @event.get_Type()); Assert.assertEquals(Watcher.Event.KeeperState.SyncConnected, @event.getState()); @event = watcher.events.poll(10 * 1000); Assert.assertEquals(name, @event.getPath()); Assert.assertEquals(Watcher.Event.EventType.NodeDeleted, @event.get_Type()); Assert.assertEquals(Watcher.Event.KeeperState.SyncConnected, @event.getState()); } }
public async Task <TResult> LocateResource <TResult>(ZooKeeper zk, string resource, Func <byte[], TResult> getResultFunc) { var result = default(TResult); DataResult data = null; try { data = await zk.getDataAsync(resource); } catch (Exception e) { _logger.LogError(e.Message); return(result); } if (data?.Data?.Any() != true) { return(result); } var buf = data.Data; if (buf[0] != 0xff) { return(result); } var metadataLen = BinaryPrimitives.ReadInt32BigEndian(buf[1..]);
public static async Task <int> GetDateIntAsync(this ZooKeeper zkClient, string path) { var dataResult = await zkClient.getDataAsync(path); string data = dataResult?.Data == null ? null : Encoding.UTF8.GetString(dataResult.Data); return(int.Parse(data)); }
static async Task Test() { var output = string.Empty; ZooKeeper zk = null; try { //创建一个Zookeeper实例,第一个参数为目标服务器地址和端口,第二个参数为Session超时时间,第三个为节点变化时的回调方法 ZooKeeper.LogToFile = false; ZooKeeper.LogToTrace = true; zk = new ZooKeeper("127.0.0.1:2181", 10 * 1000, NullWatcher.Instance); var stat = await zk.existsAsync("/root", true); if (stat == null) { //创建一个节点root,数据是mydata,不进行ACL权限控制,节点为永久性的(即客户端shutdown了也不会消失) output = await zk.createAsync("/root", "mydata".GetBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); Trace.WriteLine($"output"); } stat = await zk.existsAsync("/root/childone", true); if (stat == null) { //在root下面创建一个childone znode,数据为childone,不进行ACL权限控制,节点为永久性的 output = await zk.createAsync("/root/childone", "childone".GetBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); Trace.WriteLine($"output"); } //取得/root节点下的子节点名称,返回List<String> var subNodes = await zk.getChildrenAsync("/root", true); Trace.WriteLine($"SubNodes: {(string.Join(",", subNodes.Children))}"); //取得/root/childone节点下的数据,返回byte[] var data = await zk.getDataAsync("/root/childone", true); Trace.WriteLine($"/root/childone Data: {Encoding.UTF8.GetString(data.Data)}"); //修改节点/root/childone下的数据,第三个参数为版本,如果是-1,那会无视被修改的数据版本,直接改掉 await zk.setDataAsync("/root/childone", "childonemodify".GetBytes(), -1); //删除/root/childone这个节点,第二个参数为版本,-1的话直接删除,无视版本 await zk.deleteAsync("/root/childone", -1); } catch (Exception ex) { Trace.WriteLine(ex.StackTrace); } finally { await zk.closeAsync(); } }
/// <summary> /// Reads the nodes /IP:Port@Gen and /IP:Port@Gen/IAmAlive (which together is one row) /// </summary> /// <param name="zk">The zookeeper instance used for the read</param> /// <param name="siloAddress">The silo address.</param> private static async Task <Tuple <MembershipEntry, string> > GetRow(ZooKeeper zk, SiloAddress siloAddress) { string rowPath = ConvertToRowPath(siloAddress); string rowIAmAlivePath = ConvertToRowIAmAlivePath(siloAddress); var rowDataTask = zk.getDataAsync(rowPath); var rowIAmAliveDataTask = zk.getDataAsync(rowIAmAlivePath); await Task.WhenAll(rowDataTask, rowIAmAliveDataTask); MembershipEntry me = Deserialize <MembershipEntry>((await rowDataTask).Data); me.IAmAliveTime = Deserialize <DateTime>((await rowIAmAliveDataTask).Data); int rowVersion = (await rowDataTask).Stat.getVersion(); return(new Tuple <MembershipEntry, string>(me, rowVersion.ToString(CultureInfo.InvariantCulture))); }
/// <summary> /// Visits the subtree with root as given path and calls the passed callback with each znode /// found during the search.It performs a depth-first, pre-order traversal of the tree. /// <para> /// <b>Important:</b> This is <i>not an atomic snapshot</i> of the tree ever, but the /// state as it exists across multiple RPCs from zkClient to the ensemble. /// For practical purposes, it is suggested to bring the clients to the ensemble /// down (i.e.prevent writes to pathRoot) to 'simulate' a snapshot behavior. /// </para> /// </summary> /// <param name="zk"></param> /// <param name="path"></param> /// <param name="watch"></param> /// <param name="visit"></param> /// <returns></returns> public static async Task visitSubTreeDFS(ZooKeeper zk, string path, bool watch, Func <string, Task> visit) { PathUtils.validatePath(path); await zk.getDataAsync(path, watch).ConfigureAwait(false); await visit(path).ConfigureAwait(false); await visitSubTreeDFSHelper(zk, path, watch, visit).ConfigureAwait(false); }
private async Task <string> GetDataAsync(string path, bool watch = false) { var result = await _zooKeeper.getDataAsync(path, watch); if (result == null) { return(null); } return(result.Data == null ? string.Empty : Encoding.UTF8.GetString(result.Data)); }
/// <summary> /// Returns parsed internal collections cloud state /// </summary> private async Task <SolrCloudState> GetInternalCollectionsStateAsync() { DataResult data; try { data = await zooKeeper.getDataAsync(ClusterState, true).ConfigureAwait(false); } catch (KeeperException ex) { return(new SolrCloudState(new Dictionary <string, SolrCloudCollection>())); } var collectionsState = data != null ? SolrCloudStateParser.Parse(Encoding.Default.GetString(data.Data)) : new SolrCloudState(new Dictionary <string, SolrCloudCollection>()); return(collectionsState); }
/// <summary> /// 获取指定路径的值 /// </summary> /// <param name="path"></param> /// <param name="watcher"></param> /// <returns></returns> public string GetData(string path, Watcher watcher = null) { try { if (watcher == null) { var data = _zk.getDataAsync(path).ConfigureAwait(false).GetAwaiter().GetResult(); return(Encoding.UTF8.GetString(data.Data)); } else { var data = _zk.getDataAsync(path, watcher).ConfigureAwait(false).GetAwaiter().GetResult(); return(Encoding.UTF8.GetString(data.Data)); } } catch (NoNodeException) { } return(null); }
/// <summary> /// 得到节点数据 /// </summary> /// <param name="path">不要使用path等关键字作为路径</param> /// <param name="watcher"></param> /// <returns></returns> public string GetData(string path, Watcher watcher = null) { Task <DataResult> dataResult = _zooKeeper.getDataAsync(path, watcher); dataResult.Wait(); if (dataResult.Result != null && dataResult.Status.ToString().ToLower() == "RanToCompletion".ToLower()) { return(Encoding.UTF8.GetString(dataResult.Result.Data)); } return(_fail); }
/// <summary> /// 获取path的数据 /// </summary> /// <param name="path">路径</param> /// <param name="watcher">监听path节点的watcher</param> /// <returns>返回数据</returns> public static string GetNodeData(string path, Watcher watcher = null) { int tryNum = 0; gotoLable: logger.LogInformation($"GetNodeData获取数据:path={path},watcher={watcher?.GetType()?.FullName}"); try { var result = watcher == null ? currentZookeeper.getDataAsync(path, true) : currentZookeeper.getDataAsync(path, watcher); result.Wait(); byte[] rst = result.Result.Data; if (rst == null || rst.Length < 1) { return(null); } return(Encoding.UTF8.GetString(rst)); } catch (KeeperException.NoNodeException nodeEx) // 这样的异常不用重连 { logger.LogError(nodeEx, $"GetNodeData 获取数据节点不存在 NoNodeException:{path}"); return(null); } catch (Exception e) { logger.LogError(e, $"GetNodeData获取数据异常:path={path},watcher={watcher?.GetType()?.FullName}"); tryNum += 1; if (tryNum < 3) { reConnection(e); Thread.Sleep(500); goto gotoLable; } } return(null); }
/// <summary> /// Return the head of the queue without modifying the queue. </summary> /// <returns> the data at the head of the queue. </returns> /// <exception cref="InvalidOperationException"> </exception> /// <exception cref="KeeperException"> </exception> public async Task <byte[]> element() { // element, take, and remove follow the same pattern. // We want to return the child node with the smallest sequence number. // Since other clients are remove()ing and take()ing nodes concurrently, // the child with the smallest sequence number in orderedChildren might be gone by the time we check. // We don't call getChildren again until we have tried the rest of the nodes in sequence order. while (true) { SortedDictionary <long, string> orderedChildren; try { orderedChildren = await getOrderedChildren(null).ConfigureAwait(false); } catch (KeeperException.NoNodeException) { throw new InvalidOperationException(); } if (orderedChildren.Count == 0) { throw new InvalidOperationException(); } foreach (string headNode in orderedChildren.Values) { if (headNode != null) { try { return((await zookeeper.getDataAsync(dir + "/" + headNode).ConfigureAwait(false)).Data); } catch (KeeperException.NoNodeException) { //Another client removed the node first, try next } } } } }
/// <summary> /// 获取指定节点上的数据,如果不存在,则创建并添加 /// </summary> /// <param name="keeper"></param> /// <param name="path"></param> /// <param name="data"></param> /// <returns></returns> public static async Task <byte[]> GetOrAddAsync(this ZooKeeper keeper, ZookeeperPathString path, byte[] data) { if (await keeper.ExistAsync(path)) { return((await keeper.getDataAsync(path, true)).Data); } else { await keeper.CreateAsync(path, data); return(data); } }
public async Task <TContent> GetAsync <TContent>(string nodeName) { if (await _zkClient.existsAsync(nodeName) != null) { var dataResult = await _zkClient.getDataAsync(nodeName); if (dataResult != null) { var content = Encoding.UTF8.GetString(dataResult.Data); return(JsonSerializer.Deserialize <TContent>(content, _jsonOptions)); } } return(default);
static async Task Main(string[] args) { var watcher = NullWatcher.Instance; ZooKeeper zk = new ZooKeeper(hostPort, CONNECTION_TIMEOUT, watcher); var root = await zk.createAsync("/root", Encoding.UTF8.GetBytes("123456"), null, CreateMode.EPHEMERAL); Console.WriteLine(zk.getDataAsync("/root")); await Task.CompletedTask; Console.ReadKey(); }
static async Task DeleteRecursive(ZooKeeper zk, string path = "", string key = "") { var correctedPath = path + "/" + key; var a = await zk.getChildrenAsync(correctedPath); var d = await zk.getDataAsync(correctedPath); foreach (var child in a.Children) { await DeleteRecursive(zk, correctedPath == "/"? "" : correctedPath, child); } await zk.deleteAsync(correctedPath); }
public async Task testSequentialNodeData() { const string queue_handle = "/queue"; ZooKeeper zk = await createClient(); await zk.createAsync(queue_handle, new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); await zk.createAsync(queue_handle + "/element", "0".UTF8getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT_SEQUENTIAL); await zk.createAsync(queue_handle + "/element", "1".UTF8getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT_SEQUENTIAL); IList <string> children = (await zk.getChildrenAsync(queue_handle, true)).Children; Assert.assertEquals(children.Count, 2); string child1 = children[0]; string child2 = children[1]; int compareResult = child1.CompareTo(child2); Assert.assertNotEquals(compareResult, 0); if (compareResult < 0) { } else { string temp = child1; child1 = child2; child2 = temp; } string child1data = (await zk.getDataAsync(queue_handle + "/" + child1, false)).Data.UTF8bytesToString(); string child2data = (await zk.getDataAsync(queue_handle + "/" + child2, false)).Data.UTF8bytesToString(); Assert.assertEquals(child1data, "0"); Assert.assertEquals(child2data, "1"); }
async static Task Main() { // Create a new ZK client. var zookeeper = new ZooKeeper("<zookeeper-url>", sessionTimeout: 3000, new MyWatcher()); // Use it to request a config setting. var connectionData = await zookeeper.getDataAsync("/common-settings/connectionString"); // Convert the received data from bytes to string. var connectionString = Encoding.UTF8.GetString(connectionData.Data); Console.WriteLine($"Got connectionString '{connectionString}'."); // Got connectionString '1.1.1.1:123' Console.Read(); }
public async Task TestMethod1() { //原来zk客户端不应该作为静态对象,每次new foreach (var i in Com.Range(100)) { var client = new ZooKeeper("localhost:32771", (int)TimeSpan.FromSeconds(5).TotalMilliseconds, this, canBeReadOnly: false); try { //var count = 0; //while (client.getState() != ZooKeeper.States.CONNECTED) //{ // if (++count > 100) { throw new Exception("loss patient to wait for connection"); } // await Task.Delay(10); //} if (await client.existsAsync("/home", false) == null) { var path = await client.createAsync("/home", "".GetBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); } var bs = new { id = 2, name = "fas", age = 44, time = DateTime.Now }.ToJson().GetBytes(); await client.setDataAsync("/home", bs); var data = await client.getDataAsync("/home", false); var children = await client.getChildrenAsync("/home"); var t = client.transaction(); //t.delete("/home"); t.setData("/home", $"{DateTime.Now.Ticks}".GetBytes()); var res = await t.commitAsync(); } catch (Exception e) { // } finally { await client.closeAsync(); this.connected = false; } } }
public async Task <string> Get(string nodeName) { var node = await _zookeeper.existsAsync(nodeName); if (node != null) { var dataResult = await _zookeeper.getDataAsync(nodeName); if (dataResult != null) { return(Encoding.ASCII.GetString(dataResult.Data)); } } return(default);