HBASE-24569 Get hostAndWeights in addition using localhost if it is null in local mode (#2339)
Closes #1909 Signed-off-by: Duo Zhang <zhangduo@apache.org> Signed-off-by: Viraj Jasani <vjasani@apache.org>
This commit is contained in:
parent
fb654a9125
commit
19e2878d2f
@ -25,9 +25,8 @@ import java.util.Map;
|
||||
import java.util.NavigableSet;
|
||||
import java.util.TreeMap;
|
||||
import java.util.TreeSet;
|
||||
|
||||
import org.apache.hadoop.hbase.classification.InterfaceAudience;
|
||||
|
||||
import org.apache.hadoop.hbase.util.DNS;
|
||||
|
||||
/**
|
||||
* Data structure to describe the distribution of HDFS blocks among hosts.
|
||||
@ -202,6 +201,18 @@ public class HDFSBlocksDistribution {
|
||||
public float getBlockLocalityIndex(String host) {
|
||||
float localityIndex = 0;
|
||||
HostAndWeight hostAndWeight = this.hostAndWeights.get(host);
|
||||
// Compatible with local mode, see HBASE-24569
|
||||
if (hostAndWeight == null) {
|
||||
String currentHost = "";
|
||||
try {
|
||||
currentHost = DNS.getDefaultHost("default", "default");
|
||||
} catch (Exception e) {
|
||||
// Just ignore, it's ok, avoid too many log info
|
||||
}
|
||||
if (host.equals(currentHost)) {
|
||||
hostAndWeight = this.hostAndWeights.get(HConstants.LOCALHOST);
|
||||
}
|
||||
}
|
||||
if (hostAndWeight != null && uniqueBlocksTotalWeight != 0) {
|
||||
localityIndex=(float)hostAndWeight.weight/(float)uniqueBlocksTotalWeight;
|
||||
}
|
||||
|
@ -18,14 +18,15 @@
|
||||
*/
|
||||
package org.apache.hadoop.hbase;
|
||||
|
||||
import org.apache.hadoop.hbase.testclassification.SmallTests;
|
||||
import org.junit.Test;
|
||||
import org.junit.experimental.categories.Category;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotEquals;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import static junit.framework.Assert.assertEquals;
|
||||
import org.apache.hadoop.hbase.testclassification.SmallTests;
|
||||
import org.apache.hadoop.hbase.util.DNS;
|
||||
import org.junit.Test;
|
||||
import org.junit.experimental.categories.Category;
|
||||
|
||||
@Category(SmallTests.class)
|
||||
public class TestHDFSBlocksDistribution {
|
||||
@ -67,4 +68,19 @@ public class TestHDFSBlocksDistribution {
|
||||
assertEquals("Should be one host", 1, distribution.getHostAndWeights().size());
|
||||
assertEquals("Total weight should be 10", 10, distribution.getUniqueBlocksTotalWeight());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testLocalHostCompatibility() throws Exception {
|
||||
String currentHost = DNS.getDefaultHost("default", "default");
|
||||
HDFSBlocksDistribution distribution = new HDFSBlocksDistribution();
|
||||
assertEquals("Locality should be 0.0", 0.0,
|
||||
distribution.getBlockLocalityIndex(currentHost), 0.01);
|
||||
distribution.addHostsAndBlockWeight(new String[] { "localhost" }, 10);
|
||||
assertEquals("Should be one host", 1, distribution.getHostAndWeights().size());
|
||||
assertEquals("Locality should be 0.0", 0.0,
|
||||
distribution.getBlockLocalityIndex("test"), 0.01);
|
||||
assertNotEquals("Locality should be 0.0", 0.0,
|
||||
distribution.getBlockLocalityIndex(currentHost), 0.01);
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user