@InterfaceAudience.LimitedPrivate(value={"HDFS","MapReduce"}) @InterfaceStability.Unstable public class NetworkTopology extends Object
Modifier and Type | Class and Description |
---|---|
static class |
NetworkTopology.InvalidTopologyException |
Modifier and Type | Field and Description |
---|---|
static int |
DEFAULT_HOST_LEVEL |
static String |
DEFAULT_RACK |
static org.apache.commons.logging.Log |
LOG |
protected ReadWriteLock |
netlock
the lock used to manage access
|
protected int |
numOfRacks
rack counter
|
protected static Random |
r |
Constructor and Description |
---|
NetworkTopology() |
Modifier and Type | Method and Description |
---|---|
void |
add(Node node)
Add a leaf node
Update node counter & rack counter if necessary
|
Node |
chooseRandom(String scope)
randomly choose one node from scope
if scope starts with ~, choose one from the all nodes except for the
ones in scope; otherwise, choose one from scope
|
boolean |
contains(Node node)
Check if the tree contains node node
|
int |
countNumOfAvailableNodes(String scope,
Collection<Node> excludedNodes)
return the number of leaves in scope but not in excludedNodes
if scope starts with ~, return the number of nodes that are not
in scope and excludedNodes;
|
List<Node> |
getDatanodesInRack(String loc)
Given a string representation of a rack, return its children
|
int |
getDistance(Node node1,
Node node2)
Return the distance between two nodes
It is assumed that the distance from one node to its parent is 1
The distance between two nodes is calculated by summing up their distances
to their closest common ancestor.
|
static String |
getFirstHalf(String networkLocation)
Divide networklocation string into two parts by last separator, and get
the first part here.
|
static NetworkTopology |
getInstance(Configuration conf)
Get an instance of NetworkTopology based on the value of the configuration
parameter net.topology.impl.
|
static String |
getLastHalf(String networkLocation)
Divide networklocation string into two parts by last separator, and get
the second part here.
|
List<Node> |
getLeaves(String scope)
return leaves in scope
|
Node |
getNode(String loc)
Given a string representation of a node, return its reference
|
protected Node |
getNodeForNetworkLocation(Node node)
Return a reference to the node given its string representation.
|
int |
getNumOfLeaves() |
int |
getNumOfRacks() |
String |
getRack(String loc)
Given a string representation of a rack for a specific network
location
To be overridden in subclasses for specific NetworkTopology
implementations, as alternative to overriding the full
getRack(String) method. |
boolean |
isNodeGroupAware()
Check if network topology is aware of NodeGroup
|
boolean |
isOnSameNodeGroup(Node node1,
Node node2)
Return false directly as not aware of NodeGroup, to be override in sub-class
|
boolean |
isOnSameRack(Node node1,
Node node2)
Check if two nodes are on the same rack
|
protected boolean |
isSameParents(Node node1,
Node node2)
Compare the parents of each node for equality
|
void |
pseudoSortByDistance(Node reader,
Node[] nodes)
Sort nodes array by their distances to reader
It linearly scans the array, if a local node is found, swap it with
the first element of the array.
|
void |
remove(Node node)
Remove a node
Update node counter and rack counter if necessary
|
protected static void |
swap(Node[] nodes,
int i,
int j)
swap two array items
|
String |
toString()
convert a network tree to a string
|
public static final String DEFAULT_RACK
public static final int DEFAULT_HOST_LEVEL
public static final org.apache.commons.logging.Log LOG
protected int numOfRacks
protected ReadWriteLock netlock
protected static final Random r
public static NetworkTopology getInstance(Configuration conf)
conf
- the configuration to be usedpublic void add(Node node)
node
- node to be added; can be nullIllegalArgumentException
- if add a node to a leave
or node to be added is not a leafprotected Node getNodeForNetworkLocation(Node node)
getNode(String)
.
To be overridden in subclasses for specific NetworkTopology
implementations, as alternative to overriding the full add(Node)
method.
node
- The string representation of this node's network location is
used to retrieve a Node object.add(Node)
,
getNode(String)
public List<Node> getDatanodesInRack(String loc)
loc
- a path-like string representation of a rackpublic void remove(Node node)
node
- node to be removed; can be nullpublic boolean contains(Node node)
node
- a nodepublic Node getNode(String loc)
loc
- a path-like string representation of a nodepublic String getRack(String loc)
getRack(String)
method.loc
- a path-like string representation of a network locationpublic int getNumOfRacks()
public int getNumOfLeaves()
public int getDistance(Node node1, Node node2)
node1
- one nodenode2
- another nodeInteger.MAX_VALUE
if node1 or node2 do not belong to the clusterpublic boolean isOnSameRack(Node node1, Node node2)
node1
- one node (can be null)node2
- another node (can be null)IllegalArgumentException
- when either node1 or node2 is null, or
node1 or node2 do not belong to the clusterpublic boolean isNodeGroupAware()
public boolean isOnSameNodeGroup(Node node1, Node node2)
protected boolean isSameParents(Node node1, Node node2)
To be overridden in subclasses for specific NetworkTopology
implementations, as alternative to overriding the full
isOnSameRack(Node, Node)
method.
node1
- the first node to comparenode2
- the second node to compareisOnSameRack(Node, Node)
public Node chooseRandom(String scope)
scope
- range of nodes from which a node will be chosenpublic List<Node> getLeaves(String scope)
scope
- a path stringpublic int countNumOfAvailableNodes(String scope, Collection<Node> excludedNodes)
scope
- a path string that may start with ~excludedNodes
- a list of nodespublic String toString()
public static String getFirstHalf(String networkLocation)
networkLocation
- public static String getLastHalf(String networkLocation)
networkLocation
- protected static void swap(Node[] nodes, int i, int j)
public void pseudoSortByDistance(Node reader, Node[] nodes)
reader
- the node that wishes to read a block from one of the nodesnodes
- the list of nodes containing data for the readerCopyright © 2013 Apache Software Foundation. All rights reserved.