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:
Zheng Wang 2020-09-01 18:41:36 +05:30 committed by Viraj Jasani
parent fb654a9125
commit 19e2878d2f
No known key found for this signature in database
GPG Key ID: B3D6C0B41C8ADFD5
2 changed files with 34 additions and 7 deletions

View File

@ -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;
}

View File

@ -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);
}
}