Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@
import java.util.TreeMap;
import java.util.TreeSet;
import org.apache.hadoop.fs.StorageType;
import org.apache.hadoop.hbase.util.DNS;
import org.apache.yetus.audience.InterfaceAudience;


/**
* Data structure to describe the distribution of HDFS blocks among hosts.
*
Expand Down Expand Up @@ -280,6 +280,18 @@ public long getBlocksLocalWithSsdWeight(String host) {
private long getBlocksLocalityWeightInternal(String host, Visitor visitor) {
long 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 = visitor.visit(hostAndWeight);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,16 @@
*/
package org.apache.hadoop.hbase;

import static junit.framework.Assert.assertEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotEquals;

import java.util.HashMap;
import java.util.Map;

import org.apache.hadoop.fs.StorageType;
import org.apache.hadoop.hbase.testclassification.MiscTests;
import org.apache.hadoop.hbase.testclassification.SmallTests;
import org.apache.hadoop.hbase.util.DNS;
import org.junit.ClassRule;
import org.junit.Test;
import org.junit.experimental.categories.Category;
Expand Down Expand Up @@ -81,4 +83,19 @@ public void testAdd() throws Exception {
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);
}

}