HDFS-10715. NPE when applying AvailableSpaceBlockPlacementPolicy. Contributed by Guangbin Zhu.

This commit is contained in:
Akira Ajisaka 2016-08-05 17:31:11 +09:00
parent 18d9e6ec0b
commit ef432579a7
2 changed files with 28 additions and 6 deletions

View File

@ -76,6 +76,7 @@ protected DatanodeDescriptor chooseDataNode(final String scope,
(DatanodeDescriptor) clusterMap.chooseRandom(scope, excludedNode);
DatanodeDescriptor b =
(DatanodeDescriptor) clusterMap.chooseRandom(scope, excludedNode);
if (a != null && b != null){
int ret = compareDataNode(a, b);
if (ret == 0) {
return a;
@ -84,6 +85,9 @@ protected DatanodeDescriptor chooseDataNode(final String scope,
} else {
return (RAND.nextInt(100) < balancedPreference) ? b : a;
}
} else {
return a == null ? b : a;
}
}
/**

View File

@ -20,6 +20,8 @@
import java.io.File;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
@ -30,6 +32,7 @@
import org.apache.hadoop.hdfs.server.common.HdfsServerConstants;
import org.apache.hadoop.hdfs.server.namenode.NameNode;
import org.apache.hadoop.net.NetworkTopology;
import org.apache.hadoop.net.Node;
import org.apache.hadoop.test.PathUtils;
import org.junit.AfterClass;
import org.junit.Assert;
@ -158,6 +161,21 @@ public void testChooseTarget() {
Assert.assertTrue(possibility < 0.55);
}
@Test
public void testChooseDataNode() {
try {
Collection<Node> allNodes = new ArrayList<>(dataNodes.length);
Collections.addAll(allNodes, dataNodes);
if (placementPolicy instanceof AvailableSpaceBlockPlacementPolicy){
// exclude all datanodes when chooseDataNode, no NPE should be thrown
((AvailableSpaceBlockPlacementPolicy)placementPolicy)
.chooseDataNode("~", allNodes);
}
}catch (NullPointerException npe){
Assert.fail("NPE should not be thrown");
}
}
@AfterClass
public static void teardownCluster() {
if (namenode != null) {