private Node ChooseRandom(string scope, string excludedScope) { if (excludedScope != null) { if (scope.StartsWith(excludedScope)) { return(null); } if (!excludedScope.StartsWith(scope)) { excludedScope = null; } } Node node = GetNode(scope); if (!(node is NetworkTopology.InnerNode)) { return(node); } NetworkTopology.InnerNode innerNode = (NetworkTopology.InnerNode)node; int numOfDatanodes = innerNode.GetNumOfLeaves(); if (excludedScope == null) { node = null; } else { node = GetNode(excludedScope); if (!(node is NetworkTopology.InnerNode)) { numOfDatanodes -= 1; } else { numOfDatanodes -= ((NetworkTopology.InnerNode)node).GetNumOfLeaves(); } } if (numOfDatanodes == 0) { throw new NetworkTopology.InvalidTopologyException("Failed to find datanode (scope=\"" + scope.ToString() + "\" excludedScope=\"" + excludedScope.ToString() + "\")."); } int leaveIndex = r.Next(numOfDatanodes); return(innerNode.GetLeaf(leaveIndex, node)); }
/// <summary>return leaves in <i>scope</i></summary> /// <param name="scope">a path string</param> /// <returns>leaves nodes under specific scope</returns> public virtual IList <Node> GetLeaves(string scope) { Node node = GetNode(scope); IList <Node> leafNodes = new AList <Node>(); if (!(node is NetworkTopology.InnerNode)) { leafNodes.AddItem(node); } else { NetworkTopology.InnerNode innerNode = (NetworkTopology.InnerNode)node; for (int i = 0; i < innerNode.GetNumOfLeaves(); i++) { leafNodes.AddItem(innerNode.GetLeaf(i, null)); } } return(leafNodes); }
/// <summary>Sort nodes array by their distances to <i>reader</i>.</summary> /// <remarks> /// Sort nodes array by their distances to <i>reader</i>. /// <p/> /// This is the same as /// <see cref="NetworkTopology.SortByDistance(Node, Node[], int)"/> /// except with a four-level network topology which contains the /// additional network distance of a "node group" which is between local and /// same rack. /// </remarks> /// <param name="reader">Node where data will be read</param> /// <param name="nodes">Available replicas with the requested data</param> /// <param name="activeLen">Number of active nodes at the front of the array</param> public override void SortByDistance(Node reader, Node[] nodes, int activeLen) { // If reader is not a datanode (not in NetworkTopology tree), we need to // replace this reader with a sibling leaf node in tree. if (reader != null && !this.Contains(reader)) { Node nodeGroup = GetNode(reader.GetNetworkLocation()); if (nodeGroup != null && nodeGroup is NetworkTopology.InnerNode) { NetworkTopology.InnerNode parentNode = (NetworkTopology.InnerNode)nodeGroup; // replace reader with the first children of its parent in tree reader = parentNode.GetLeaf(0, null); } else { return; } } base.SortByDistance(reader, nodes, activeLen); }
/// <summary>convert a network tree to a string</summary> public override string ToString() { // print the number of racks StringBuilder tree = new StringBuilder(); tree.Append("Number of racks: "); tree.Append(numOfRacks); tree.Append("\n"); // print the number of leaves int numOfLeaves = GetNumOfLeaves(); tree.Append("Expected number of leaves:"); tree.Append(numOfLeaves); tree.Append("\n"); // print nodes for (int i = 0; i < numOfLeaves; i++) { tree.Append(NodeBase.GetPath(clusterMap.GetLeaf(i, null))); tree.Append("\n"); } return(tree.ToString()); }
/// <summary> /// get <i>leafIndex</i> leaf of this subtree /// if it is not in the <i>excludedNode</i> /// </summary> /// <param name="leafIndex">an indexed leaf of the node</param> /// <param name="excludedNode">an excluded node (can be null)</param> /// <returns/> internal virtual Node GetLeaf(int leafIndex, Node excludedNode) { int count = 0; // check if the excluded node a leaf bool isLeaf = excludedNode == null || !(excludedNode is NetworkTopology.InnerNode ); // calculate the total number of excluded leaf nodes int numOfExcludedLeaves = isLeaf ? 1 : ((NetworkTopology.InnerNode)excludedNode). GetNumOfLeaves(); if (IsLeafParent()) { // children are leaves if (isLeaf) { // excluded node is a leaf node int excludedIndex = children.IndexOf(excludedNode); if (excludedIndex != -1 && leafIndex >= 0) { // excluded node is one of the children so adjust the leaf index leafIndex = leafIndex >= excludedIndex ? leafIndex + 1 : leafIndex; } } // range check if (leafIndex < 0 || leafIndex >= this.GetNumOfChildren()) { return(null); } return(children[leafIndex]); } else { for (int i = 0; i < children.Count; i++) { NetworkTopology.InnerNode child = (NetworkTopology.InnerNode)children[i]; if (excludedNode == null || excludedNode != child) { // not the excludedNode int numOfLeaves = child.GetNumOfLeaves(); if (excludedNode != null && child.IsAncestor(excludedNode)) { numOfLeaves -= numOfExcludedLeaves; } if (count + numOfLeaves > leafIndex) { // the leaf is in the child subtree return(child.GetLeaf(leafIndex - count, excludedNode)); } else { // go to the next child count = count + numOfLeaves; } } else { // it is the excluededNode // skip it and set the excludedNode to be null excludedNode = null; } } return(null); } }