/** * Recursively deletes children of a node. * * @param zookeeper the client * @param path path of the node to delete * @param deleteSelf flag that indicates that the node should also get deleted * @throws InterruptedException * @throws KeeperException */ public static async void deleteChildren(ZooKeeper zookeeper, String path, bool deleteSelf) { PathUtils.validatePath(path); ChildrenResult children = await zookeeper.getChildrenAsync(path, null); foreach (String child in children.Children) { String fullPath = makePath(path, child); deleteChildren(zookeeper, fullPath, true); } if (deleteSelf) { try { await zookeeper.deleteAsync(path, -1); } catch (KeeperException.NotEmptyException) { //someone has created a new child since we checked ... delete again. deleteChildren(zookeeper, path, true); } catch (KeeperException.NoNodeException) { // ignore... someone else has deleted the node it since we checked } } }
public async Task DeleteResourceAsync(string group, string resourceName) { while (true) { try { ChildrenResult childrenRes = await zookeeper.getChildrenAsync($"{zkRootPath}/{group}/resources/{resourceName}"); foreach (string child in childrenRes.Children) { await SafeDelete($"{zkRootPath}/{group}/resources/{resourceName}/{child}"); } await SafeDelete($"{zkRootPath}/{group}/resources/{resourceName}"); return; } catch (KeeperException.NoNodeException) { return; } catch (KeeperException.ConnectionLossException) { } catch (KeeperException.SessionExpiredException) { await EstablishSession(); } } }
public async Task <List <string> > GetStoppedAsync() { string actionToPerform = "get the list of stopped clients"; while (true) { await BlockUntilConnected(actionToPerform); try { ChildrenResult childrenResult = await zookeeper.getChildrenAsync(stoppedPath); return(childrenResult.Children); } catch (KeeperException.NoNodeException e) { throw new ZkInvalidOperationException( $"Could not {actionToPerform} as the stopped 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); } } }
private async Task <IEnumerable <(string key, string value)> > GetData(ZooKeeper zk) { ChildrenResult childrenResult = await zk.getChildrenAsync(_options.Path); IEnumerable <Task <IEnumerable <(string key, string value)> > > getAllChildrenTask = childrenResult.Children .Select(child => GetData(zk, _options.Path + '/' + child)); IEnumerable <(string key, string value)>[] result = await Task.WhenAll(getAllChildrenTask);
/** * Return the children of the given path sorted by sequence number * * @param zookeeper the client * @param path the path * @return sorted list of children * @throws InterruptedException thread interruption * @throws org.apache.zookeeper.KeeperException zookeeper errors */ public static async Task <List <string> > getSortedChildren(ZooKeeper zookeeper, String path) { ChildrenResult children = await zookeeper.getChildrenAsync(path, false); List <string> sortedList = new List <string>(children.Children); sortedList.Sort(); return(sortedList); }
public async Task <ClientsZnode> GetActiveClientsAsync() { string actionToPerform = "get the list of active clients"; while (true) { await BlockUntilConnected(actionToPerform); try { ChildrenResult childrenResult = await zookeeper.getChildrenAsync(clientsPath); List <string> childrenPaths = childrenResult.Children.Select(x => $"{clientsPath}/{x}").ToList(); return(new ClientsZnode { Version = childrenResult.Stat.getVersion(), ClientPaths = childrenPaths }); } catch (KeeperException.NoNodeException e) { throw new ZkInvalidOperationException( $"Could not {actionToPerform} as the clients 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); } } }
public async Task <ResourcesZnode> GetResourcesAsync(Watcher childWatcher, Watcher dataWatcher) { var actionToPerform = "get the list of resources"; while (true) { await BlockUntilConnected(actionToPerform); try { DataResult dataResult = null; if (dataWatcher != null) { dataResult = await this.zookeeper.getDataAsync(this.resourcesPath, dataWatcher); } else { dataResult = await this.zookeeper.getDataAsync(this.resourcesPath); } ChildrenResult childrenResult = null; if (childWatcher != null) { childrenResult = await this.zookeeper.getChildrenAsync(this.resourcesPath, childWatcher); } else { childrenResult = await this.zookeeper.getChildrenAsync(this.resourcesPath); } var resourcesZnodeData = JSONSerializer <ResourcesZnodeData> .DeSerialize( System.Text.Encoding.UTF8.GetString(dataResult.Data)); if (resourcesZnodeData == null) { resourcesZnodeData = new ResourcesZnodeData(); } return(new ResourcesZnode() { ResourceAssignments = resourcesZnodeData, Resources = childrenResult.Children, Version = dataResult.Stat.getVersion() }); } catch (KeeperException.NoNodeException e) { throw new ZkInvalidOperationException( $"Could not {actionToPerform} as the resources 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); } } }