/// <summary>
 /// Add a leaf node
 /// Update node counter & rack counter if necessary
 /// </summary>
 /// <param name="node">node to be added; can be null</param>
 /// <exception>
 /// IllegalArgumentException
 /// if add a node to a leave
 /// or node to be added is not a leaf
 /// </exception>
 public override void Add(Node node)
 {
     if (node == null)
     {
         return;
     }
     if (node is NetworkTopology.InnerNode)
     {
         throw new ArgumentException("Not allow to add an inner node: " + NodeBase.GetPath
                                         (node));
     }
     netlock.WriteLock().Lock();
     try
     {
         Node rack = null;
         // if node only with default rack info, here we need to add default
         // nodegroup info
         if (NetworkTopology.DefaultRack.Equals(node.GetNetworkLocation()))
         {
             node.SetNetworkLocation(node.GetNetworkLocation() + Org.Apache.Hadoop.Net.NetworkTopologyWithNodeGroup
                                     .DefaultNodegroup);
         }
         Node nodeGroup = GetNode(node.GetNetworkLocation());
         if (nodeGroup == null)
         {
             nodeGroup = new NetworkTopologyWithNodeGroup.InnerNodeWithNodeGroup(node.GetNetworkLocation
                                                                                     ());
         }
         rack = GetNode(nodeGroup.GetNetworkLocation());
         // rack should be an innerNode and with parent.
         // note: rack's null parent case is: node's topology only has one layer,
         //       so rack is recognized as "/" and no parent.
         // This will be recognized as a node with fault topology.
         if (rack != null && (!(rack is NetworkTopology.InnerNode) || rack.GetParent() ==
                              null))
         {
             throw new ArgumentException("Unexpected data node " + node.ToString() + " at an illegal network location"
                                         );
         }
         if (clusterMap.Add(node))
         {
             Log.Info("Adding a new node: " + NodeBase.GetPath(node));
             if (rack == null)
             {
                 // We only track rack number here
                 numOfRacks++;
             }
         }
         if (Log.IsDebugEnabled())
         {
             Log.Debug("NetworkTopology became:\n" + this.ToString());
         }
     }
     finally
     {
         netlock.WriteLock().Unlock();
     }
 }
        protected internal override Node GetNodeForNetworkLocation(Node node)
        {
            // if node only with default rack info, here we need to add default
            // nodegroup info
            if (NetworkTopology.DefaultRack.Equals(node.GetNetworkLocation()))
            {
                node.SetNetworkLocation(node.GetNetworkLocation() + DefaultNodegroup);
            }
            Node nodeGroup = GetNode(node.GetNetworkLocation());

            if (nodeGroup == null)
            {
                nodeGroup = new NetworkTopologyWithNodeGroup.InnerNodeWithNodeGroup(node.GetNetworkLocation
                                                                                        ());
            }
            return(GetNode(nodeGroup.GetNetworkLocation()));
        }
 /// <summary>
 /// Given a string representation of a node group for a specific network
 /// location
 /// </summary>
 /// <param name="loc">a path-like string representation of a network location</param>
 /// <returns>a node group string</returns>
 public virtual string GetNodeGroup(string loc)
 {
     netlock.ReadLock().Lock();
     try
     {
         loc = NetworkTopology.InnerNode.Normalize(loc);
         Node locNode = GetNode(loc);
         if (locNode is NetworkTopologyWithNodeGroup.InnerNodeWithNodeGroup)
         {
             NetworkTopologyWithNodeGroup.InnerNodeWithNodeGroup node = (NetworkTopologyWithNodeGroup.InnerNodeWithNodeGroup
                                                                         )locNode;
             if (node.IsNodeGroup())
             {
                 return(loc);
             }
             else
             {
                 if (node.IsRack())
                 {
                     // not sure the node group for a rack
                     return(null);
                 }
                 else
                 {
                     // may be a leaf node
                     return(GetNodeGroup(node.GetNetworkLocation()));
                 }
             }
         }
         else
         {
             // not in cluster map, don't handle it
             return(loc);
         }
     }
     finally
     {
         netlock.ReadLock().Unlock();
     }
 }
 public override string GetRack(string loc)
 {
     netlock.ReadLock().Lock();
     try
     {
         loc = NetworkTopology.InnerNode.Normalize(loc);
         Node locNode = GetNode(loc);
         if (locNode is NetworkTopologyWithNodeGroup.InnerNodeWithNodeGroup)
         {
             NetworkTopologyWithNodeGroup.InnerNodeWithNodeGroup node = (NetworkTopologyWithNodeGroup.InnerNodeWithNodeGroup
                                                                         )locNode;
             if (node.IsRack())
             {
                 return(loc);
             }
             else
             {
                 if (node.IsNodeGroup())
                 {
                     return(node.GetNetworkLocation());
                 }
                 else
                 {
                     // may be a data center
                     return(null);
                 }
             }
         }
         else
         {
             // not in cluster map, don't handle it
             return(loc);
         }
     }
     finally
     {
         netlock.ReadLock().Unlock();
     }
 }
 public NetworkTopologyWithNodeGroup()
 {
     clusterMap = new NetworkTopologyWithNodeGroup.InnerNodeWithNodeGroup(NetworkTopology.InnerNode
                                                                          .Root);
 }