/// <summary> /// Remove a node /// Update node counter and rack counter if necessary /// </summary> /// <param name="node">node to be removed; can be null</param> public virtual void Remove(Node node) { if (node == null) { return; } if (node is NetworkTopology.InnerNode) { throw new ArgumentException("Not allow to remove an inner node: " + NodeBase.GetPath (node)); } Log.Info("Removing a node: " + NodeBase.GetPath(node)); netlock.WriteLock().Lock(); try { if (clusterMap.Remove(node)) { NetworkTopology.InnerNode rack = (NetworkTopology.InnerNode)GetNode(node.GetNetworkLocation ()); if (rack == null) { numOfRacks--; } } if (Log.IsDebugEnabled()) { Log.Debug("NetworkTopology became:\n" + this.ToString()); } } finally { netlock.WriteLock().Unlock(); } }
/// <summary>Remove node <i>n</i> from the subtree of this node</summary> /// <param name="n">node to be deleted</param> /// <returns>true if the node is deleted; false otherwise</returns> internal virtual bool Remove(Node n) { string parent = n.GetNetworkLocation(); string currentPath = GetPath(this); if (!IsAncestor(n)) { throw new ArgumentException(n.GetName() + ", which is located at " + parent + ", is not a descendent of " + currentPath); } if (IsParent(n)) { // this node is the parent of n; remove n directly for (int i = 0; i < children.Count; i++) { if (children[i].GetName().Equals(n.GetName())) { children.Remove(i); numOfLeaves--; n.SetParent(null); return(true); } } return(false); } else { // find the next ancestor node: the parent node string parentName = GetNextAncestorName(n); NetworkTopology.InnerNode parentNode = null; int i; for (i = 0; i < children.Count; i++) { if (children[i].GetName().Equals(parentName)) { parentNode = (NetworkTopology.InnerNode)children[i]; break; } } if (parentNode == null) { return(false); } // remove n from the parent node bool isRemoved = parentNode.Remove(n); // if the parent node has no children, remove the parent node too if (isRemoved) { if (parentNode.GetNumOfChildren() == 0) { children.Remove(i); } numOfLeaves--; } return(isRemoved); } }
/// <summary>Add node <i>n</i> to the subtree of this node</summary> /// <param name="n">node to be added</param> /// <returns>true if the node is added; false otherwise</returns> internal virtual bool Add(Node n) { if (!IsAncestor(n)) { throw new ArgumentException(n.GetName() + ", which is located at " + n.GetNetworkLocation () + ", is not a decendent of " + GetPath(this)); } if (IsParent(n)) { // this node is the parent of n; add n directly n.SetParent(this); n.SetLevel(this.level + 1); for (int i = 0; i < children.Count; i++) { if (children[i].GetName().Equals(n.GetName())) { children.Set(i, n); return(false); } } children.AddItem(n); numOfLeaves++; return(true); } else { // find the next ancestor node string parentName = GetNextAncestorName(n); NetworkTopology.InnerNode parentNode = null; for (int i = 0; i < children.Count; i++) { if (children[i].GetName().Equals(parentName)) { parentNode = (NetworkTopology.InnerNode)children[i]; break; } } if (parentNode == null) { // create a new InnerNode parentNode = CreateParentNode(parentName); children.AddItem(parentNode); } // add n to the subtree of the next ancestor node if (parentNode.Add(n)) { numOfLeaves++; return(true); } else { return(false); } } }
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>Given a string representation of a rack, return its children</summary> /// <param name="loc">a path-like string representation of a rack</param> /// <returns>a newly allocated list with all the node's children</returns> public virtual IList <Node> GetDatanodesInRack(string loc) { netlock.ReadLock().Lock(); try { loc = NodeBase.Normalize(loc); if (!NodeBase.Root.Equals(loc)) { loc = Runtime.Substring(loc, 1); } NetworkTopology.InnerNode rack = (NetworkTopology.InnerNode)clusterMap.GetLoc(loc ); if (rack == null) { return(null); } return(new AList <Node>(rack.GetChildren())); } finally { netlock.ReadLock().Unlock(); } }
public InnerNodeWithNodeGroup(string name, string location, NetworkTopology.InnerNode parent, int level) : base(name, location, parent, level) { }
/// <summary> /// Construct an InnerNode /// from its name, its network location, its parent, and its level /// </summary> internal InnerNode(string name, string location, NetworkTopology.InnerNode parent , int level) : base(name, location, parent, level) { }
public NetworkTopology() { // end of InnerNode clusterMap = new NetworkTopology.InnerNode(NetworkTopology.InnerNode.Root); }
/// <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); } }