NetworkTopologyWithNodeGroup@LimitedPrivate({"HDFS","MapReduce"})
@Unstable
public class NetworkTopology
extends java.lang.Object
| Modifier and Type | Class | Description |
|---|---|---|
static class |
NetworkTopology.InvalidTopologyException |
| Modifier and Type | Field | Description |
|---|---|---|
static java.lang.String |
DEFAULT_RACK |
|
static org.slf4j.Logger |
LOG |
|
protected java.util.concurrent.locks.ReadWriteLock |
netlock |
the lock used to manage access
|
protected int |
numOfRacks |
rack counter
|
| Constructor | Description |
|---|---|
NetworkTopology() |
| Modifier and Type | Method | Description |
|---|---|---|
void |
add(Node node) |
Add a leaf node
Update node counter & rack counter if necessary
|
Node |
chooseRandom(java.lang.String scope) |
Randomly choose a node.
|
protected Node |
chooseRandom(java.lang.String scope,
java.lang.String excludedScope,
java.util.Collection<Node> excludedNodes) |
|
Node |
chooseRandom(java.lang.String scope,
java.util.Collection<Node> excludedNodes) |
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
if scope starts with ~, return the number of nodes that are not
in scope and excludedNodes;
|
void |
decommissionNode(Node node) |
Update empty rack number when remove a node like decommission.
|
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
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 int |
getDistanceByPath(Node node1,
Node node2) |
Return the distance between two nodes by comparing their network paths
without checking if they belong to the same ancestor node by reference.
|
static java.lang.String |
getFirstHalf(java.lang.String networkLocation) |
|
static NetworkTopology |
getInstance(Configuration conf) |
Get an instance of NetworkTopology based on the value of the configuration
parameter net.topology.impl.
|
static NetworkTopology |
getInstance(Configuration conf,
InnerNode.Factory factory) |
|
static java.lang.String |
getLastHalf(java.lang.String networkLocation) |
|
java.util.List<Node> |
getLeaves(java.lang.String scope) |
return leaves in scope
|
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 |
getNumOfNonEmptyRacks() |
|
int |
getNumOfRacks() |
|
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. |
protected int |
getWeight(Node reader,
Node node) |
Returns an integer weight which specifies how far away {node} is away from
{reader}.
|
protected static int |
getWeightUsingNetworkLocation(Node reader,
Node node) |
Returns an integer weight which specifies how far away node is
from reader.
|
boolean |
hasClusterEverBeenMultiRack() |
|
protected void |
incrementRacks() |
|
protected NetworkTopology |
init(InnerNode.Factory factory) |
|
protected static boolean |
isChildScope(java.lang.String parentScope,
java.lang.String childScope) |
Checks whether one scope is contained in the other scope.
|
boolean |
isNodeGroupAware() |
|
protected static boolean |
isNodeInScope(Node node,
java.lang.String scope) |
Checks whether a node belongs to the scope.
|
boolean |
isOnSameNodeGroup(Node node1,
Node node2) |
|
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 |
recommissionNode(Node node) |
Update empty rack number when add a node like recommission.
|
void |
remove(Node node) |
Remove a node
Update node counter and rack counter if necessary
|
void |
sortByDistance(Node reader,
Node[] nodes,
int activeLen) |
Sort nodes array by network distance to reader.
|
<T extends Node> |
sortByDistance(Node reader,
T[] nodes,
int activeLen,
java.util.function.Consumer<java.util.List<T>> secondarySort) |
Sort nodes array by network distance to reader with secondary sort.
|
void |
sortByDistanceUsingNetworkLocation(Node reader,
Node[] nodes,
int activeLen) |
Sort nodes array by network distance to reader with secondary sort.
|
<T extends Node> |
sortByDistanceUsingNetworkLocation(Node reader,
T[] nodes,
int activeLen,
java.util.function.Consumer<java.util.List<T>> secondarySort) |
Sort nodes array by network distance to reader.
|
java.lang.String |
toString() |
convert a network tree to a string.
|
public static final java.lang.String DEFAULT_RACK
public static final org.slf4j.Logger LOG
protected int numOfRacks
protected java.util.concurrent.locks.ReadWriteLock netlock
public static NetworkTopology getInstance(Configuration conf)
conf - the configuration to be usedpublic static NetworkTopology getInstance(Configuration conf, InnerNode.Factory factory)
protected NetworkTopology init(InnerNode.Factory factory)
public void add(Node node)
node - node to be added; can be nulljava.lang.IllegalArgumentException - if add a node to a leave
or node to be added is not a leafprotected void incrementRacks()
protected 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 java.util.List<Node> getDatanodesInRack(java.lang.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(java.lang.String loc)
loc - a path-like string representation of a nodepublic boolean hasClusterEverBeenMultiRack()
public java.lang.String getRack(java.lang.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 static int getDistanceByPath(Node node1, Node node2)
node1 - one nodenode2 - another nodepublic boolean isOnSameRack(Node node1, Node node2)
node1 - one node (can be null)node2 - another node (can be null)java.lang.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)
node1 - input node1.node2 - input 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(java.lang.String scope)
scope - range of nodes from which a node will be chosenchooseRandom(String, Collection)public Node chooseRandom(java.lang.String scope, java.util.Collection<Node> excludedNodes)
scope - range of nodes from which a node will be chosenexcludedNodes - nodes to be excluded fromprotected Node chooseRandom(java.lang.String scope, java.lang.String excludedScope, java.util.Collection<Node> excludedNodes)
public java.util.List<Node> getLeaves(java.lang.String scope)
scope - a path string@VisibleForTesting
public int countNumOfAvailableNodes(java.lang.String scope,
java.util.Collection<Node> excludedNodes)
scope - a path string that may start with ~excludedNodes - a list of nodespublic java.lang.String toString()
toString in class java.lang.Objectpublic static java.lang.String getFirstHalf(java.lang.String networkLocation)
networkLocation - input networkLocation.public static java.lang.String getLastHalf(java.lang.String networkLocation)
networkLocation - input networkLocation.@VisibleForTesting protected int getWeight(Node reader, Node node)
reader - Node where data will be readnode - Replica of data@VisibleForTesting protected static int getWeightUsingNetworkLocation(Node reader, Node node)
reader - Node where data will be readnode - Replica of datapublic void sortByDistance(Node reader, Node[] nodes, int activeLen)
In a three-level topology, a node can be either local, on the same rack, or on a different rack from the reader. Sorting the nodes based on network distance from the reader reduces network traffic and improves performance.
As an additional twist, we also randomize the nodes at each network distance. This helps with load balancing when there is data skew.
reader - Node where data will be readnodes - Available replicas with the requested dataactiveLen - Number of active nodes at the front of the arraypublic <T extends Node> void sortByDistance(Node reader, T[] nodes, int activeLen, java.util.function.Consumer<java.util.List<T>> secondarySort)
In a three-level topology, a node can be either local, on the same rack, or on a different rack from the reader. Sorting the nodes based on network distance from the reader reduces network traffic and improves performance.
As an additional twist, we also randomize the nodes at each network distance. This helps with load balancing when there is data skew.T - Generics Type Treader - Node where data will be readnodes - Available replicas with the requested dataactiveLen - Number of active nodes at the front of the arraysecondarySort - a secondary sorting strategy which can inject into
that point from outside to help sort the same distance.public void sortByDistanceUsingNetworkLocation(Node reader, Node[] nodes, int activeLen)
using network location. This is used when the reader is not a datanode. Sorting the nodes based on network distance from the reader reduces network traffic and improves performance.
reader - Node where data will be readnodes - Available replicas with the requested dataactiveLen - Number of active nodes at the front of the arraypublic <T extends Node> void sortByDistanceUsingNetworkLocation(Node reader, T[] nodes, int activeLen, java.util.function.Consumer<java.util.List<T>> secondarySort)
using network location. This is used when the reader is not a datanode. Sorting the nodes based on network distance from the reader reduces network traffic and improves performance.
T - Generics Type T.reader - Node where data will be readnodes - Available replicas with the requested dataactiveLen - Number of active nodes at the front of the arraysecondarySort - a secondary sorting strategy which can inject into
that point from outside to help sort the same distance.protected static boolean isChildScope(java.lang.String parentScope,
java.lang.String childScope)
parentScope - the parent scope to checkchildScope - the child scope which needs to be checked.protected static boolean isNodeInScope(Node node, java.lang.String scope)
node - the node to check.scope - scope to check.public int getNumOfNonEmptyRacks()
public void recommissionNode(Node node)
node - node to be added; can be nullpublic void decommissionNode(Node node)
node - node to be added; can be nullCopyright © 2008–2025 Apache Software Foundation. All rights reserved.