/// <summary> /// Load the nodes that will be used for this test. /// </summary> /// <param name="ringMaster">RingMaster client</param> /// <param name="rootPath">Root path to the nodes</param> /// <param name="maxNodes">Maximum number of nodes to load</param> /// <param name="maxGetChildrenEnumerationCount">Maximum number of children to enumerate per get children request</param> /// <returns>A <see cref="Task"/> that tracks execution of this method</returns> public async Task LoadNodes(IRingMasterRequestHandler ringMaster, string rootPath, int maxNodes, int maxGetChildrenEnumerationCount = 1000) { Trace.TraceInformation($"LoadNodes rootPath={rootPath}, maxNodes={maxNodes}, maxGetChildrenEnumerationCount={maxGetChildrenEnumerationCount}"); var nodes = new Queue <string>(); nodes.Enqueue(rootPath); while (!this.cancellationToken.IsCancellationRequested && (nodes.Count > 0) && (this.nodeList.Count < maxNodes)) { string currentNode = nodes.Dequeue(); this.nodeList.Add(currentNode); this.instrumentation?.NodeLoaded(this.nodeList.Count); try { await ringMaster.ForEachChild(currentNode, maxGetChildrenEnumerationCount, child => { string childFullPath = (currentNode == "/") ? $"/{child}" : $"{currentNode}/{child}"; nodes.Enqueue(childFullPath); }); } catch (RingMasterException ex) { Trace.TraceError($"Failed to get children of node {currentNode}. Exception={ex}"); } } Trace.TraceInformation($"LoadNodes Completed: {this.nodeList.Count} nodes loaded"); }
/// <summary> /// Enumerates the descendants of the node at the given path without blocking. /// </summary> /// <param name="ringMaster">Interface to ringmaster</param> /// <param name="path">Node path</param> /// <param name="maxChildrenPerRequest">Maximum number of children to retrieve with each GetChildren request</param> /// <param name="action">Action to execute for each descendant</param> /// <returns>A <see cref="Task"/> that tracks execution of this method</returns> public static async Task ForEachDescendant(this IRingMasterRequestHandler ringMaster, string path, int maxChildrenPerRequest, Action <string> action) { var queue = new Queue <string>(); queue.Enqueue(path); while (queue.Count > 0) { string parentPath = queue.Dequeue(); await ringMaster.ForEachChild( parentPath, maxChildrenPerRequest, child => { string childPath = (parentPath == "/") ? ("/" + child) : (parentPath + "/" + child); queue.Enqueue(childPath); }); action(parentPath); } }