public class NetworkTopology extends Object
Modifier and Type | Field and Description |
---|---|
static int |
DEFAULT_HOST_LEVEL |
static String |
DEFAULT_RACK |
static org.apache.commons.logging.Log |
LOG |
Constructor and Description |
---|
NetworkTopology() |
Modifier and Type | Method and Description |
---|---|
void |
add(Node node)
Add a leaf node
Update node counter & rack counter if neccessary
|
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,
List<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;
|
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.
|
Node |
getNode(String loc)
Given a string representation of a node, return its reference
|
int |
getNumOfLeaves()
Return the total number of nodes
|
int |
getNumOfRacks()
Return the total number of racks
|
boolean |
isOnSameRack(Node node1,
Node node2)
Check if two nodes are on the same rack
|
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 & rack counter if neccessary
|
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
public void add(Node node)
node
- node to be addedIllegalArgumentException
- if add a node to a leave
or node to be added is not a leafpublic void remove(Node node)
node
- node to be removedpublic boolean contains(Node node)
node
- a nodepublic Node getNode(String loc)
loc
- a path-like string representation of a nodepublic int getNumOfRacks()
public int getNumOfLeaves()
public int getDistance(Node node1, Node node2)
node1
- one nodenode2
- another nodepublic boolean isOnSameRack(Node node1, Node node2)
node1
- one nodenode2
- another nodeIllegalArgumentException
- when either node1 or node2 is null, or
node1 or node2 do not belong to the clusterpublic Node chooseRandom(String scope)
scope
- range of nodes from which a node will be choosenpublic int countNumOfAvailableNodes(String scope, List<Node> excludedNodes)
scope
- a path string that may start with ~excludedNodes
- a list of nodespublic String toString()
public void pseudoSortByDistance(Node reader, Node[] nodes)
Copyright © 2010 The Apache Software Foundation