|
@@ -18,10 +18,11 @@
|
|
package org.apache.hadoop.net;
|
|
package org.apache.hadoop.net;
|
|
|
|
|
|
import java.util.ArrayList;
|
|
import java.util.ArrayList;
|
|
-import java.util.List;
|
|
|
|
|
|
+import java.util.HashMap;
|
|
import java.util.Collection;
|
|
import java.util.Collection;
|
|
import java.util.Collections;
|
|
import java.util.Collections;
|
|
import java.util.List;
|
|
import java.util.List;
|
|
|
|
+import java.util.Map;
|
|
import java.util.Random;
|
|
import java.util.Random;
|
|
import java.util.TreeMap;
|
|
import java.util.TreeMap;
|
|
import java.util.concurrent.locks.ReadWriteLock;
|
|
import java.util.concurrent.locks.ReadWriteLock;
|
|
@@ -81,6 +82,7 @@ public class NetworkTopology {
|
|
*/
|
|
*/
|
|
static class InnerNode extends NodeBase {
|
|
static class InnerNode extends NodeBase {
|
|
protected List<Node> children=new ArrayList<Node>();
|
|
protected List<Node> children=new ArrayList<Node>();
|
|
|
|
+ private Map<String, Node> childrenMap = new HashMap<String, Node>();
|
|
private int numOfLeaves;
|
|
private int numOfLeaves;
|
|
|
|
|
|
/** Construct an InnerNode from a path-like string */
|
|
/** Construct an InnerNode from a path-like string */
|
|
@@ -172,10 +174,13 @@ public class NetworkTopology {
|
|
// this node is the parent of n; add n directly
|
|
// this node is the parent of n; add n directly
|
|
n.setParent(this);
|
|
n.setParent(this);
|
|
n.setLevel(this.level+1);
|
|
n.setLevel(this.level+1);
|
|
- for(int i=0; i<children.size(); i++) {
|
|
|
|
- if (children.get(i).getName().equals(n.getName())) {
|
|
|
|
- children.set(i, n);
|
|
|
|
- return false;
|
|
|
|
|
|
+ Node prev = childrenMap.put(n.getName(), n);
|
|
|
|
+ if (prev != null) {
|
|
|
|
+ for(int i=0; i<children.size(); i++) {
|
|
|
|
+ if (children.get(i).getName().equals(n.getName())) {
|
|
|
|
+ children.set(i, n);
|
|
|
|
+ return false;
|
|
|
|
+ }
|
|
}
|
|
}
|
|
}
|
|
}
|
|
children.add(n);
|
|
children.add(n);
|
|
@@ -184,17 +189,12 @@ public class NetworkTopology {
|
|
} else {
|
|
} else {
|
|
// find the next ancestor node
|
|
// find the next ancestor node
|
|
String parentName = getNextAncestorName(n);
|
|
String parentName = getNextAncestorName(n);
|
|
- InnerNode parentNode = null;
|
|
|
|
- for(int i=0; i<children.size(); i++) {
|
|
|
|
- if (children.get(i).getName().equals(parentName)) {
|
|
|
|
- parentNode = (InnerNode)children.get(i);
|
|
|
|
- break;
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
|
|
+ InnerNode parentNode = (InnerNode)childrenMap.get(parentName);
|
|
if (parentNode == null) {
|
|
if (parentNode == null) {
|
|
// create a new InnerNode
|
|
// create a new InnerNode
|
|
parentNode = createParentNode(parentName);
|
|
parentNode = createParentNode(parentName);
|
|
children.add(parentNode);
|
|
children.add(parentNode);
|
|
|
|
+ childrenMap.put(parentNode.getName(), parentNode);
|
|
}
|
|
}
|
|
// add n to the subtree of the next ancestor node
|
|
// add n to the subtree of the next ancestor node
|
|
if (parentNode.add(n)) {
|
|
if (parentNode.add(n)) {
|
|
@@ -235,12 +235,15 @@ public class NetworkTopology {
|
|
+parent+", is not a descendent of "+currentPath);
|
|
+parent+", is not a descendent of "+currentPath);
|
|
if (isParent(n)) {
|
|
if (isParent(n)) {
|
|
// this node is the parent of n; remove n directly
|
|
// this node is the parent of n; remove n directly
|
|
- for(int i=0; i<children.size(); i++) {
|
|
|
|
- if (children.get(i).getName().equals(n.getName())) {
|
|
|
|
- children.remove(i);
|
|
|
|
- numOfLeaves--;
|
|
|
|
- n.setParent(null);
|
|
|
|
- return true;
|
|
|
|
|
|
+ if (childrenMap.containsKey(n.getName())) {
|
|
|
|
+ for (int i=0; i<children.size(); i++) {
|
|
|
|
+ if (children.get(i).getName().equals(n.getName())) {
|
|
|
|
+ children.remove(i);
|
|
|
|
+ childrenMap.remove(n.getName());
|
|
|
|
+ numOfLeaves--;
|
|
|
|
+ n.setParent(null);
|
|
|
|
+ return true;
|
|
|
|
+ }
|
|
}
|
|
}
|
|
}
|
|
}
|
|
return false;
|
|
return false;
|
|
@@ -263,7 +266,8 @@ public class NetworkTopology {
|
|
// if the parent node has no children, remove the parent node too
|
|
// if the parent node has no children, remove the parent node too
|
|
if (isRemoved) {
|
|
if (isRemoved) {
|
|
if (parentNode.getNumOfChildren() == 0) {
|
|
if (parentNode.getNumOfChildren() == 0) {
|
|
- children.remove(i);
|
|
|
|
|
|
+ Node prev = children.remove(i);
|
|
|
|
+ childrenMap.remove(prev.getName());
|
|
}
|
|
}
|
|
numOfLeaves--;
|
|
numOfLeaves--;
|
|
}
|
|
}
|
|
@@ -280,12 +284,7 @@ public class NetworkTopology {
|
|
if (loc == null || loc.length() == 0) return this;
|
|
if (loc == null || loc.length() == 0) return this;
|
|
|
|
|
|
String[] path = loc.split(PATH_SEPARATOR_STR, 2);
|
|
String[] path = loc.split(PATH_SEPARATOR_STR, 2);
|
|
- Node childnode = null;
|
|
|
|
- for(int i=0; i<children.size(); i++) {
|
|
|
|
- if (children.get(i).getName().equals(path[0])) {
|
|
|
|
- childnode = children.get(i);
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
|
|
+ Node childnode = childrenMap.get(path[0]);
|
|
if (childnode == null) return null; // non-existing node
|
|
if (childnode == null) return null; // non-existing node
|
|
if (path.length == 1) return childnode;
|
|
if (path.length == 1) return childnode;
|
|
if (childnode instanceof InnerNode) {
|
|
if (childnode instanceof InnerNode) {
|
|
@@ -312,10 +311,13 @@ public class NetworkTopology {
|
|
isLeaf ? 1 : ((InnerNode)excludedNode).getNumOfLeaves();
|
|
isLeaf ? 1 : ((InnerNode)excludedNode).getNumOfLeaves();
|
|
if (isLeafParent()) { // children are leaves
|
|
if (isLeafParent()) { // children are leaves
|
|
if (isLeaf) { // excluded node is a leaf node
|
|
if (isLeaf) { // excluded node is a leaf node
|
|
- int excludedIndex = children.indexOf(excludedNode);
|
|
|
|
- if (excludedIndex != -1 && leafIndex >= 0) {
|
|
|
|
- // excluded node is one of the children so adjust the leaf index
|
|
|
|
- leafIndex = leafIndex>=excludedIndex ? leafIndex+1 : leafIndex;
|
|
|
|
|
|
+ if (excludedNode != null &&
|
|
|
|
+ childrenMap.containsKey(excludedNode.getName())) {
|
|
|
|
+ int excludedIndex = children.indexOf(excludedNode);
|
|
|
|
+ if (excludedIndex != -1 && leafIndex >= 0) {
|
|
|
|
+ // excluded node is one of the children so adjust the leaf index
|
|
|
|
+ leafIndex = leafIndex>=excludedIndex ? leafIndex+1 : leafIndex;
|
|
|
|
+ }
|
|
}
|
|
}
|
|
}
|
|
}
|
|
// range check
|
|
// range check
|