Class NetworkTopologyImpl
- java.lang.Object
-
- org.apache.bookkeeper.net.NetworkTopologyImpl
-
- All Implemented Interfaces:
NetworkTopology
public class NetworkTopologyImpl extends java.lang.Object implements NetworkTopology
The class represents a cluster of computer with a tree hierarchical network topology. For example, a cluster may be consists of many data centers filled with racks of computers. In a network topology, leaves represent data nodes (computers) and inner nodes represent switches/routers that manage traffic in/out of data centers or racks.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description (package private) static class
NetworkTopologyImpl.InnerNode
InnerNode represents a switch/router of a data center or rack.static class
NetworkTopologyImpl.InvalidTopologyException
A marker for an InvalidTopology Exception.
-
Field Summary
Fields Modifier and Type Field Description (package private) NetworkTopologyImpl.InnerNode
clusterMap
The root cluster map.static int
DEFAULT_HOST_LEVEL
static java.lang.String
INVERSE
static org.slf4j.Logger
LOG
protected java.util.concurrent.locks.ReadWriteLock
netlock
The lock used to manage access.static java.lang.String
NODE_SEPARATOR
protected int
numOfRacks
Rack counter.-
Fields inherited from interface org.apache.bookkeeper.net.NetworkTopology
DEFAULT_RACK, DEFAULT_REGION, DEFAULT_REGION_AND_RACK, DEFAULT_UPGRADEDOMAIN, DEFAULT_ZONE, DEFAULT_ZONE_AND_UPGRADEDOMAIN
-
-
Constructor Summary
Constructors Constructor Description NetworkTopologyImpl()
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description void
add(Node node)
Add a leaf node.Node
chooseRandom(java.lang.String scope)
Randomly choose one node from scope.boolean
contains(Node node)
Check if the tree contains node node.int
countNumOfAvailableNodes(java.lang.String scope, java.util.Collection<Node> excludedNodes)
Return the number of leaves in scope but not in excludedNodes.java.util.List<Node>
getDatanodesInRack(java.lang.String loc)
Given a string representation of a rack, return its children.int
getDistance(Node node1, Node node2)
Return the distance between two nodes.static java.lang.String
getFirstHalf(java.lang.String networkLocation)
Divide networklocation string into two parts by last separator, and get the first part here.static java.lang.String
getLastHalf(java.lang.String networkLocation)
Divide networklocation string into two parts by last separator, and get the second part here.java.util.Set<Node>
getLeaves(java.lang.String scope)
Returns the nodes under a location.Node
getNode(java.lang.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()
Returns number of racks in the network topology.java.lang.String
getRack(java.lang.String loc)
Given a string representation of a rack for a specific network location.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.protected static void
swap(Node[] nodes, int i, int j)
Swap two array items.java.lang.String
toString()
Convert a network tree to a string.
-
-
-
Field Detail
-
DEFAULT_HOST_LEVEL
public static final int DEFAULT_HOST_LEVEL
- See Also:
- Constant Field Values
-
LOG
public static final org.slf4j.Logger LOG
-
NODE_SEPARATOR
public static final java.lang.String NODE_SEPARATOR
- See Also:
- Constant Field Values
-
INVERSE
public static final java.lang.String INVERSE
- See Also:
- Constant Field Values
-
clusterMap
NetworkTopologyImpl.InnerNode clusterMap
The root cluster map.
-
numOfRacks
protected int numOfRacks
Rack counter.
-
netlock
protected java.util.concurrent.locks.ReadWriteLock netlock
The lock used to manage access.
-
-
Method Detail
-
add
public void add(Node node)
Add a leaf node. Update node counter and rack counter if necessary- Specified by:
add
in interfaceNetworkTopology
- Parameters:
node
- node to be added; can be null- Throws:
java.lang.IllegalArgumentException
- if add a node to a leave or node to be added is not a leaf
-
getNodeForNetworkLocation
protected Node getNodeForNetworkLocation(Node node)
Return a reference to the node given its string representation. Default implementation delegates togetNode(String)
.To be overridden in subclasses for specific NetworkTopology implementations, as alternative to overriding the full
add(Node)
method.- Parameters:
node
- The string representation of this node's network location is used to retrieve a Node object.- Returns:
- a reference to the node; null if the node is not in the tree
- See Also:
add(Node)
,getNode(String)
-
getDatanodesInRack
public java.util.List<Node> getDatanodesInRack(java.lang.String loc)
Given a string representation of a rack, return its children.- Parameters:
loc
- a path-like string representation of a rack- Returns:
- a newly allocated list with all the node's children
-
remove
public void remove(Node node)
Remove a node. Update node counter and rack counter if necessary.- Specified by:
remove
in interfaceNetworkTopology
- Parameters:
node
- node to be removed; can be null
-
contains
public boolean contains(Node node)
Check if the tree contains node node.- Specified by:
contains
in interfaceNetworkTopology
- Parameters:
node
- a node- Returns:
- true if node is already in the tree; false otherwise
-
getNode
public Node getNode(java.lang.String loc)
Given a string representation of a node, return its reference.- Specified by:
getNode
in interfaceNetworkTopology
- Parameters:
loc
- a path-like string representation of a node- Returns:
- a reference to the node; null if the node is not in the tree
-
getRack
public java.lang.String getRack(java.lang.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.- Parameters:
loc
- a path-like string representation of a network location- Returns:
- a rack string
-
getNumOfRacks
public int getNumOfRacks()
Description copied from interface:NetworkTopology
Returns number of racks in the network topology.- Specified by:
getNumOfRacks
in interfaceNetworkTopology
- Returns:
- the total number of racks
-
getNumOfLeaves
public int getNumOfLeaves()
- Returns:
- the total number of leaf nodes
-
getDistance
public 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.
- Parameters:
node1
- one nodenode2
- another node- Returns:
- the distance between node1 and node2 which is zero if they are the same
or
Integer.MAX_VALUE
if node1 or node2 do not belong to the cluster
-
isOnSameRack
public boolean isOnSameRack(Node node1, Node node2)
Check if two nodes are on the same rack.- Parameters:
node1
- one node (can be null)node2
- another node (can be null)- Returns:
- true if node1 and node2 are on the same rack; false otherwise
- Throws:
java.lang.IllegalArgumentException
- when either node1 or node2 is null, or node1 or node2 do not belong to the cluster
-
isNodeGroupAware
public boolean isNodeGroupAware()
Check if network topology is aware of NodeGroup.
-
isOnSameNodeGroup
public boolean isOnSameNodeGroup(Node node1, Node node2)
Return false directly as not aware of NodeGroup, to be override in sub-class.
-
isSameParents
protected boolean isSameParents(Node node1, Node node2)
Compare the parents of each node for equality.To be overridden in subclasses for specific NetworkTopology implementations, as alternative to overriding the full
isOnSameRack(Node, Node)
method.- Parameters:
node1
- the first node to comparenode2
- the second node to compare- Returns:
- true if their parents are equal, false otherwise
- See Also:
isOnSameRack(Node, Node)
-
chooseRandom
public Node chooseRandom(java.lang.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.
- Parameters:
scope
- range of nodes from which a node will be chosen- Returns:
- the chosen node
-
getLeaves
public java.util.Set<Node> getLeaves(java.lang.String scope)
Description copied from interface:NetworkTopology
Returns the nodes under a location.- Specified by:
getLeaves
in interfaceNetworkTopology
- Parameters:
scope
- network location- Returns:
- nodes under a location
-
countNumOfAvailableNodes
public int countNumOfAvailableNodes(java.lang.String scope, java.util.Collection<Node> excludedNodes)
Description copied from interface:NetworkTopology
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;
- Specified by:
countNumOfAvailableNodes
in interfaceNetworkTopology
- Parameters:
scope
- a path string that may start with ~excludedNodes
- a list of nodes- Returns:
- number of available nodes
-
toString
public java.lang.String toString()
Convert a network tree to a string.- Overrides:
toString
in classjava.lang.Object
-
getFirstHalf
public static java.lang.String getFirstHalf(java.lang.String networkLocation)
Divide networklocation string into two parts by last separator, and get the first part here.- Parameters:
networkLocation
-- Returns:
-
getLastHalf
public static java.lang.String getLastHalf(java.lang.String networkLocation)
Divide networklocation string into two parts by last separator, and get the second part here.- Parameters:
networkLocation
-- Returns:
-
swap
protected static void swap(Node[] nodes, int i, int j)
Swap two array items.
-
pseudoSortByDistance
public 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. If a local rack node is found, swap it with the first element following the local node. If neither local node or local rack node is found, put a random replica location at position 0. It leaves the rest nodes untouched.- Parameters:
reader
- the node that wishes to read a block from one of the nodesnodes
- the list of nodes containing data for the reader
-
-