in hadoop-hdds/common/src/main/java/org/apache/hadoop/hdds/scm/net/NetworkTopologyImpl.java [717:766]
public int getDistanceCost(Node node1, Node node2) {
if (Objects.equals(node1, node2)) {
return 0;
}
if (node1 == null || node2 == null) {
LOG.warn("One of the nodes is a null pointer");
return Integer.MAX_VALUE;
}
// verify levels are in range
int level1 = node1.getLevel();
int level2 = node2.getLevel();
if (level1 < NetConstants.ROOT_LEVEL || level2 < NetConstants.ROOT_LEVEL) {
return Integer.MAX_VALUE;
}
if (level1 > maxLevel || level2 > maxLevel) {
return Integer.MAX_VALUE;
}
int cost = 0;
netlock.readLock().lock();
try {
Node ancestor1 = node1.getAncestor(level1 - 1);
Node ancestor2 = node2.getAncestor(level2 - 1);
if (!Objects.equals(ancestor1, clusterTree) ||
!Objects.equals(ancestor2, clusterTree)) {
LOG.debug("One of the nodes is outside of network topology");
return Integer.MAX_VALUE;
}
while (level1 > level2 && node1 != null) {
node1 = node1.getParent();
level1--;
cost += node1 == null ? 0 : node1.getCost();
}
while (level2 > level1 && node2 != null) {
node2 = node2.getParent();
level2--;
cost += node2 == null ? 0 : node2.getCost();
}
while (node1 != null && node2 != null && !Objects.equals(node1, node2)) {
node1 = node1.getParent();
node2 = node2.getParent();
cost += node1 == null ? 0 : node1.getCost();
cost += node2 == null ? 0 : node2.getCost();
}
return cost;
} finally {
netlock.readLock().unlock();
}
}