HADOOP-12185. NetworkTopology is not efficient adding/getting/removing nodes. Contributed by Inigo Goiri
(cherry picked from commit 47a69ec7a5
)
Conflicts:
hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/net/NetworkTopology.java
This commit is contained in:
parent
d01eaef40f
commit
360164e41e
|
@ -439,6 +439,9 @@ Release 2.8.0 - UNRELEASED
|
||||||
HADOOP-12186. ActiveStandbyElector shouldn't call monitorLockNodeAsync
|
HADOOP-12186. ActiveStandbyElector shouldn't call monitorLockNodeAsync
|
||||||
multiple times (zhihai xu via vinayakumarb)
|
multiple times (zhihai xu via vinayakumarb)
|
||||||
|
|
||||||
|
HADOOP-12185. NetworkTopology is not efficient adding/getting/removing
|
||||||
|
nodes. (Inigo Goiri via cdouglas)
|
||||||
|
|
||||||
Release 2.7.2 - UNRELEASED
|
Release 2.7.2 - UNRELEASED
|
||||||
|
|
||||||
INCOMPATIBLE CHANGES
|
INCOMPATIBLE CHANGES
|
||||||
|
|
|
@ -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,29 +174,27 @@ 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);
|
||||||
|
Node prev = childrenMap.put(n.getName(), n);
|
||||||
|
if (prev != null) {
|
||||||
for(int i=0; i<children.size(); i++) {
|
for(int i=0; i<children.size(); i++) {
|
||||||
if (children.get(i).getName().equals(n.getName())) {
|
if (children.get(i).getName().equals(n.getName())) {
|
||||||
children.set(i, n);
|
children.set(i, n);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
children.add(n);
|
children.add(n);
|
||||||
numOfLeaves++;
|
numOfLeaves++;
|
||||||
return true;
|
return true;
|
||||||
} else {
|
} else {
|
||||||
// find the next ancestor node
|
// find the next ancestor node
|
||||||
String parentName = getNextAncestorName(n);
|
String parentName = getNextAncestorName(n);
|
||||||
InnerNode parentNode = null;
|
InnerNode parentNode = (InnerNode)childrenMap.get(parentName);
|
||||||
for(int i=0; i<children.size(); i++) {
|
|
||||||
if (children.get(i).getName().equals(parentName)) {
|
|
||||||
parentNode = (InnerNode)children.get(i);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
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,14 +235,17 @@ 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 (childrenMap.containsKey(n.getName())) {
|
||||||
|
for (int i=0; i<children.size(); i++) {
|
||||||
if (children.get(i).getName().equals(n.getName())) {
|
if (children.get(i).getName().equals(n.getName())) {
|
||||||
children.remove(i);
|
children.remove(i);
|
||||||
|
childrenMap.remove(n.getName());
|
||||||
numOfLeaves--;
|
numOfLeaves--;
|
||||||
n.setParent(null);
|
n.setParent(null);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
return false;
|
return false;
|
||||||
} else {
|
} else {
|
||||||
// find the next ancestor node: the parent node
|
// find the next ancestor node: the parent node
|
||||||
|
@ -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;
|
Node childnode = childrenMap.get(path[0]);
|
||||||
for(int i=0; i<children.size(); i++) {
|
|
||||||
if (children.get(i).getName().equals(path[0])) {
|
|
||||||
childnode = children.get(i);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
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,12 +311,15 @@ 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
|
||||||
|
if (excludedNode != null &&
|
||||||
|
childrenMap.containsKey(excludedNode.getName())) {
|
||||||
int excludedIndex = children.indexOf(excludedNode);
|
int excludedIndex = children.indexOf(excludedNode);
|
||||||
if (excludedIndex != -1 && leafIndex >= 0) {
|
if (excludedIndex != -1 && leafIndex >= 0) {
|
||||||
// excluded node is one of the children so adjust the leaf index
|
// excluded node is one of the children so adjust the leaf index
|
||||||
leafIndex = leafIndex>=excludedIndex ? leafIndex+1 : leafIndex;
|
leafIndex = leafIndex>=excludedIndex ? leafIndex+1 : leafIndex;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
// range check
|
// range check
|
||||||
if (leafIndex<0 || leafIndex>=this.getNumOfChildren()) {
|
if (leafIndex<0 || leafIndex>=this.getNumOfChildren()) {
|
||||||
return null;
|
return null;
|
||||||
|
|
|
@ -18,8 +18,10 @@
|
||||||
package org.apache.hadoop.net;
|
package org.apache.hadoop.net;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
import java.util.HashMap;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
|
import org.apache.commons.math3.stat.inference.ChiSquareTest;
|
||||||
import org.junit.Assert;
|
import org.junit.Assert;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
|
@ -79,12 +81,14 @@ public class TestClusterTopology extends Assert {
|
||||||
public void testCountNumNodes() throws Exception {
|
public void testCountNumNodes() throws Exception {
|
||||||
// create the topology
|
// create the topology
|
||||||
NetworkTopology cluster = new NetworkTopology();
|
NetworkTopology cluster = new NetworkTopology();
|
||||||
cluster.add(getNewNode("node1", "/d1/r1"));
|
NodeElement node1 = getNewNode("node1", "/d1/r1");
|
||||||
|
cluster.add(node1);
|
||||||
NodeElement node2 = getNewNode("node2", "/d1/r2");
|
NodeElement node2 = getNewNode("node2", "/d1/r2");
|
||||||
cluster.add(node2);
|
cluster.add(node2);
|
||||||
cluster.add(getNewNode("node3", "/d1/r3"));
|
NodeElement node3 = getNewNode("node3", "/d1/r3");
|
||||||
NodeElement node3 = getNewNode("node4", "/d1/r4");
|
|
||||||
cluster.add(node3);
|
cluster.add(node3);
|
||||||
|
NodeElement node4 = getNewNode("node4", "/d1/r4");
|
||||||
|
cluster.add(node4);
|
||||||
// create exclude list
|
// create exclude list
|
||||||
List<Node> excludedNodes = new ArrayList<Node>();
|
List<Node> excludedNodes = new ArrayList<Node>();
|
||||||
|
|
||||||
|
@ -95,7 +99,7 @@ public class TestClusterTopology extends Assert {
|
||||||
assertEquals("4 nodes should be available with extra excluded Node", 4,
|
assertEquals("4 nodes should be available with extra excluded Node", 4,
|
||||||
cluster.countNumOfAvailableNodes(NodeBase.ROOT, excludedNodes));
|
cluster.countNumOfAvailableNodes(NodeBase.ROOT, excludedNodes));
|
||||||
// add one existing node to exclude list
|
// add one existing node to exclude list
|
||||||
excludedNodes.add(node3);
|
excludedNodes.add(node4);
|
||||||
assertEquals("excluded nodes with ROOT scope should be considered", 3,
|
assertEquals("excluded nodes with ROOT scope should be considered", 3,
|
||||||
cluster.countNumOfAvailableNodes(NodeBase.ROOT, excludedNodes));
|
cluster.countNumOfAvailableNodes(NodeBase.ROOT, excludedNodes));
|
||||||
assertEquals("excluded nodes without ~ scope should be considered", 2,
|
assertEquals("excluded nodes without ~ scope should be considered", 2,
|
||||||
|
@ -112,6 +116,69 @@ public class TestClusterTopology extends Assert {
|
||||||
// getting count with non-exist scope.
|
// getting count with non-exist scope.
|
||||||
assertEquals("No nodes should be considered for non-exist scope", 0,
|
assertEquals("No nodes should be considered for non-exist scope", 0,
|
||||||
cluster.countNumOfAvailableNodes("/non-exist", excludedNodes));
|
cluster.countNumOfAvailableNodes("/non-exist", excludedNodes));
|
||||||
|
// remove a node from the cluster
|
||||||
|
cluster.remove(node1);
|
||||||
|
assertEquals("1 node should be available", 1,
|
||||||
|
cluster.countNumOfAvailableNodes(NodeBase.ROOT, excludedNodes));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test how well we pick random nodes.
|
||||||
|
*/
|
||||||
|
@Test
|
||||||
|
public void testChooseRandom() {
|
||||||
|
// create the topology
|
||||||
|
NetworkTopology cluster = new NetworkTopology();
|
||||||
|
NodeElement node1 = getNewNode("node1", "/d1/r1");
|
||||||
|
cluster.add(node1);
|
||||||
|
NodeElement node2 = getNewNode("node2", "/d1/r2");
|
||||||
|
cluster.add(node2);
|
||||||
|
NodeElement node3 = getNewNode("node3", "/d1/r3");
|
||||||
|
cluster.add(node3);
|
||||||
|
NodeElement node4 = getNewNode("node4", "/d1/r3");
|
||||||
|
cluster.add(node4);
|
||||||
|
|
||||||
|
// Number of iterations to do the test
|
||||||
|
int numIterations = 100;
|
||||||
|
|
||||||
|
// Pick random nodes
|
||||||
|
HashMap<String,Integer> histogram = new HashMap<String,Integer>();
|
||||||
|
for (int i=0; i<numIterations; i++) {
|
||||||
|
String randomNode = cluster.chooseRandom(NodeBase.ROOT).getName();
|
||||||
|
if (!histogram.containsKey(randomNode)) {
|
||||||
|
histogram.put(randomNode, 0);
|
||||||
|
}
|
||||||
|
histogram.put(randomNode, histogram.get(randomNode) + 1);
|
||||||
|
}
|
||||||
|
assertEquals("Random is not selecting all nodes", 4, histogram.size());
|
||||||
|
|
||||||
|
// Check with 99% confidence (alpha=0.01 as confidence = (100 * (1 - alpha)
|
||||||
|
ChiSquareTest chiSquareTest = new ChiSquareTest();
|
||||||
|
double[] expected = new double[histogram.size()];
|
||||||
|
long[] observed = new long[histogram.size()];
|
||||||
|
int j=0;
|
||||||
|
for (Integer occurrence : histogram.values()) {
|
||||||
|
expected[j] = 1.0 * numIterations / histogram.size();
|
||||||
|
observed[j] = occurrence;
|
||||||
|
j++;
|
||||||
|
}
|
||||||
|
boolean chiSquareTestRejected =
|
||||||
|
chiSquareTest.chiSquareTest(expected, observed, 0.01);
|
||||||
|
|
||||||
|
// Check that they have the proper distribution
|
||||||
|
assertFalse("Not choosing nodes randomly", chiSquareTestRejected);
|
||||||
|
|
||||||
|
// Pick random nodes excluding the 2 nodes in /d1/r3
|
||||||
|
histogram = new HashMap<String,Integer>();
|
||||||
|
for (int i=0; i<numIterations; i++) {
|
||||||
|
String randomNode = cluster.chooseRandom("~/d1/r3").getName();
|
||||||
|
if (!histogram.containsKey(randomNode)) {
|
||||||
|
histogram.put(randomNode, 0);
|
||||||
|
}
|
||||||
|
histogram.put(randomNode, histogram.get(randomNode) + 1);
|
||||||
|
}
|
||||||
|
assertEquals("Random is not selecting the nodes it should",
|
||||||
|
2, histogram.size());
|
||||||
}
|
}
|
||||||
|
|
||||||
private NodeElement getNewNode(String name, String rackLocation) {
|
private NodeElement getNewNode(String name, String rackLocation) {
|
||||||
|
|
Loading…
Reference in New Issue