HBASE-18581 Removed dead code and some tidy up work in BaseLoadBalancer

* calls to methods getLowestLocalityRegionServer() & getLeastLoadedTopServerForRegion() got removed in HBASE-18164
  * call to calculateRegionServerLocalities() got removed in HBASE-15486
  * Some other minor improvements

Change-Id: Ib149530d8d20c019b0891c026e23180e260f59db
Signed-off-by: Apekshit Sharma <appy@apache.org>
This commit is contained in:
Umesh Agashe 2017-08-11 11:18:13 -07:00 committed by Apekshit Sharma
parent 4bda49c840
commit c298ab65ec
1 changed files with 32 additions and 158 deletions

View File

@ -1,4 +1,4 @@
/** /*
* Licensed to the Apache Software Foundation (ASF) under one * Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file * or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information * distributed with this work for additional information
@ -34,6 +34,7 @@ import java.util.Random;
import java.util.Set; import java.util.Set;
import java.util.TreeMap; import java.util.TreeMap;
import java.util.function.Predicate; import java.util.function.Predicate;
import java.util.stream.Collectors;
import org.apache.commons.lang.NotImplementedException; import org.apache.commons.lang.NotImplementedException;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
@ -360,10 +361,10 @@ public abstract class BaseLoadBalancer implements LoadBalancer {
} }
numMaxRegionsPerTable = new int[numTables]; numMaxRegionsPerTable = new int[numTables];
for (int serverIndex = 0 ; serverIndex < numRegionsPerServerPerTable.length; serverIndex++) { for (int[] aNumRegionsPerServerPerTable : numRegionsPerServerPerTable) {
for (tableIndex = 0 ; tableIndex < numRegionsPerServerPerTable[serverIndex].length; tableIndex++) { for (tableIndex = 0; tableIndex < aNumRegionsPerServerPerTable.length; tableIndex++) {
if (numRegionsPerServerPerTable[serverIndex][tableIndex] > numMaxRegionsPerTable[tableIndex]) { if (aNumRegionsPerServerPerTable[tableIndex] > numMaxRegionsPerTable[tableIndex]) {
numMaxRegionsPerTable[tableIndex] = numRegionsPerServerPerTable[serverIndex][tableIndex]; numMaxRegionsPerTable[tableIndex] = aNumRegionsPerServerPerTable[tableIndex];
} }
} }
} }
@ -375,10 +376,7 @@ public abstract class BaseLoadBalancer implements LoadBalancer {
} else { } else {
hasRegionReplicas = true; hasRegionReplicas = true;
HRegionInfo primaryInfo = RegionReplicaUtil.getRegionInfoForDefaultReplica(info); HRegionInfo primaryInfo = RegionReplicaUtil.getRegionInfoForDefaultReplica(info);
regionIndexToPrimaryIndex[i] = regionIndexToPrimaryIndex[i] = regionsToIndex.getOrDefault(primaryInfo, -1);
regionsToIndex.containsKey(primaryInfo) ?
regionsToIndex.get(primaryInfo):
-1;
} }
} }
@ -608,7 +606,7 @@ public abstract class BaseLoadBalancer implements LoadBalancer {
/** An action to move or swap a region */ /** An action to move or swap a region */
public static class Action { public static class Action {
public static enum Type { public enum Type {
ASSIGN_REGION, ASSIGN_REGION,
MOVE_REGION, MOVE_REGION,
SWAP_REGIONS, SWAP_REGIONS,
@ -806,9 +804,9 @@ public abstract class BaseLoadBalancer implements LoadBalancer {
== numMaxRegionsPerTable[tableIndex]) { == numMaxRegionsPerTable[tableIndex]) {
//recompute maxRegionsPerTable since the previous value was coming from the old server //recompute maxRegionsPerTable since the previous value was coming from the old server
numMaxRegionsPerTable[tableIndex] = 0; numMaxRegionsPerTable[tableIndex] = 0;
for (int serverIndex = 0 ; serverIndex < numRegionsPerServerPerTable.length; serverIndex++) { for (int[] aNumRegionsPerServerPerTable : numRegionsPerServerPerTable) {
if (numRegionsPerServerPerTable[serverIndex][tableIndex] > numMaxRegionsPerTable[tableIndex]) { if (aNumRegionsPerServerPerTable[tableIndex] > numMaxRegionsPerTable[tableIndex]) {
numMaxRegionsPerTable[tableIndex] = numRegionsPerServerPerTable[serverIndex][tableIndex]; numMaxRegionsPerTable[tableIndex] = aNumRegionsPerServerPerTable[tableIndex];
} }
} }
} }
@ -912,49 +910,7 @@ public abstract class BaseLoadBalancer implements LoadBalancer {
return Arrays.binarySearch(arr, val) >= 0; return Arrays.binarySearch(arr, val) >= 0;
} }
private Comparator<Integer> numRegionsComparator = new Comparator<Integer>() { private Comparator<Integer> numRegionsComparator = Comparator.comparingInt(this::getNumRegions);
@Override
public int compare(Integer integer, Integer integer2) {
return Integer.compare(getNumRegions(integer), getNumRegions(integer2));
}
};
void sortServersByLocality() {
Arrays.sort(serverIndicesSortedByLocality, localityComparator);
}
float getLocality(int server) {
return localityPerServer[server];
}
private Comparator<Integer> localityComparator = new Comparator<Integer>() {
@Override
public int compare(Integer integer, Integer integer2) {
return Float.compare(getLocality(integer), getLocality(integer2));
}
};
int getLowestLocalityRegionServer() {
if (regionFinder == null) {
return -1;
} else {
sortServersByLocality();
// We want to find server with non zero regions having lowest locality.
int i = 0;
int lowestLocalityServerIndex = serverIndicesSortedByLocality[i];
while (localityPerServer[lowestLocalityServerIndex] == 0
&& (regionsPerServer[lowestLocalityServerIndex].length == 0)) {
i++;
lowestLocalityServerIndex = serverIndicesSortedByLocality[i];
}
if (LOG.isTraceEnabled()) {
LOG.trace("Lowest locality region server with non zero regions is "
+ servers[lowestLocalityServerIndex].getHostname() + " with locality "
+ localityPerServer[lowestLocalityServerIndex]);
}
return lowestLocalityServerIndex;
}
}
int getLowestLocalityRegionOnServer(int serverIndex) { int getLowestLocalityRegionOnServer(int serverIndex) {
if (regionFinder != null) { if (regionFinder != null) {
@ -1003,62 +959,6 @@ public abstract class BaseLoadBalancer implements LoadBalancer {
} }
} }
/**
* Returns a least loaded server which has better locality for this region
* than the current server.
*/
int getLeastLoadedTopServerForRegion(int region, int currentServer) {
if (regionFinder != null) {
List<ServerName> topLocalServers = regionFinder.getTopBlockLocations(regions[region],
servers[currentServer].getHostname());
int leastLoadedServerIndex = -1;
int load = Integer.MAX_VALUE;
for (ServerName sn : topLocalServers) {
if (!serversToIndex.containsKey(sn.getHostAndPort())) {
continue;
}
int index = serversToIndex.get(sn.getHostAndPort());
if (regionsPerServer[index] == null) {
continue;
}
int tempLoad = regionsPerServer[index].length;
if (tempLoad <= load) {
leastLoadedServerIndex = index;
load = tempLoad;
}
}
if (leastLoadedServerIndex != -1) {
if (LOG.isTraceEnabled()) {
LOG.trace("Pick the least loaded server " +
servers[leastLoadedServerIndex].getHostname() +
" with better locality for region " + regions[region].getShortNameToLog());
}
}
return leastLoadedServerIndex;
} else {
return -1;
}
}
void calculateRegionServerLocalities() {
if (regionFinder == null) {
LOG.warn("Region location finder found null, skipping locality calculations.");
return;
}
for (int i = 0; i < regionsPerServer.length; i++) {
HDFSBlocksDistribution distribution = new HDFSBlocksDistribution();
if (regionsPerServer[i].length > 0) {
for (int j = 0; j < regionsPerServer[i].length; j++) {
int regionIndex = regionsPerServer[i][j];
distribution.add(regionFinder.getBlockDistribution(regions[regionIndex]));
}
} else {
LOG.debug("Server " + servers[i].getHostname() + " had 0 regions.");
}
localityPerServer[i] = distribution.getBlockLocalityIndex(servers[i].getHostname());
}
}
@VisibleForTesting @VisibleForTesting
protected void setNumRegions(int numRegions) { protected void setNumRegions(int numRegions) {
this.numRegions = numRegions; this.numRegions = numRegions;
@ -1073,32 +973,19 @@ public abstract class BaseLoadBalancer implements LoadBalancer {
justification="Not important but should be fixed") justification="Not important but should be fixed")
@Override @Override
public String toString() { public String toString() {
String desc = "Cluster{" + StringBuilder desc = new StringBuilder("Cluster={servers=[");
"servers=["; for(ServerName sn:servers) {
for(ServerName sn:servers) { desc.append(sn.getHostAndPort()).append(", ");
desc += sn.getHostAndPort() + ", "; }
} desc.append("], serverIndicesSortedByRegionCount=")
desc += .append(Arrays.toString(serverIndicesSortedByRegionCount))
", serverIndicesSortedByRegionCount="+ .append(", regionsPerServer=").append(Arrays.deepToString(regionsPerServer));
Arrays.toString(serverIndicesSortedByRegionCount) +
", regionsPerServer=[";
for (int[]r:regionsPerServer) { desc.append(", numMaxRegionsPerTable=").append(Arrays.toString(numMaxRegionsPerTable))
desc += Arrays.toString(r); .append(", numRegions=").append(numRegions).append(", numServers=").append(numServers)
} .append(", numTables=").append(numTables).append(", numMovedRegions=")
desc += "]" + .append(numMovedRegions).append('}');
", numMaxRegionsPerTable=" + return desc.toString();
Arrays.toString(numMaxRegionsPerTable) +
", numRegions=" +
numRegions +
", numServers=" +
numServers +
", numTables=" +
numTables +
", numMovedRegions=" +
numMovedRegions +
'}';
return desc;
} }
} }
@ -1364,9 +1251,7 @@ public abstract class BaseLoadBalancer implements LoadBalancer {
List<HRegionInfo> masterRegions = assignments.get(masterServerName); List<HRegionInfo> masterRegions = assignments.get(masterServerName);
if (!masterRegions.isEmpty()) { if (!masterRegions.isEmpty()) {
regions = new ArrayList<>(regions); regions = new ArrayList<>(regions);
for (HRegionInfo region: masterRegions) { regions.removeAll(masterRegions);
regions.remove(region);
}
} }
} }
if (regions == null || regions.isEmpty()) { if (regions == null || regions.isEmpty()) {
@ -1404,11 +1289,8 @@ public abstract class BaseLoadBalancer implements LoadBalancer {
for (int j = 0; j < numServers; j++) { // try all servers one by one for (int j = 0; j < numServers; j++) { // try all servers one by one
ServerName serverName = servers.get((j + serverIdx) % numServers); ServerName serverName = servers.get((j + serverIdx) % numServers);
if (!cluster.wouldLowerAvailability(region, serverName)) { if (!cluster.wouldLowerAvailability(region, serverName)) {
List<HRegionInfo> serverRegions = assignments.get(serverName); List<HRegionInfo> serverRegions =
if (serverRegions == null) { assignments.computeIfAbsent(serverName, k -> new ArrayList<>());
serverRegions = new ArrayList<>();
assignments.put(serverName, serverRegions);
}
serverRegions.add(region); serverRegions.add(region);
cluster.doAssignRegion(region, serverName); cluster.doAssignRegion(region, serverName);
serverIdx = (j + serverIdx + 1) % numServers; //remain from next server serverIdx = (j + serverIdx + 1) % numServers; //remain from next server
@ -1425,11 +1307,7 @@ public abstract class BaseLoadBalancer implements LoadBalancer {
for (HRegionInfo region : lastFewRegions) { for (HRegionInfo region : lastFewRegions) {
int i = RANDOM.nextInt(numServers); int i = RANDOM.nextInt(numServers);
ServerName server = servers.get(i); ServerName server = servers.get(i);
List<HRegionInfo> serverRegions = assignments.get(server); List<HRegionInfo> serverRegions = assignments.computeIfAbsent(server, k -> new ArrayList<>());
if (serverRegions == null) {
serverRegions = new ArrayList<>();
assignments.put(server, serverRegions);
}
serverRegions.add(region); serverRegions.add(region);
cluster.doAssignRegion(region, server); cluster.doAssignRegion(region, server);
} }
@ -1438,7 +1316,7 @@ public abstract class BaseLoadBalancer implements LoadBalancer {
protected Cluster createCluster(List<ServerName> servers, protected Cluster createCluster(List<ServerName> servers,
Collection<HRegionInfo> regions, boolean forceRefresh) { Collection<HRegionInfo> regions, boolean forceRefresh) {
if (forceRefresh == true) { if (forceRefresh) {
regionFinder.refreshAndWait(regions); regionFinder.refreshAndWait(regions);
} }
// Get the snapshot of the current assignments for the regions in question, and then create // Get the snapshot of the current assignments for the regions in question, and then create
@ -1525,14 +1403,10 @@ public abstract class BaseLoadBalancer implements LoadBalancer {
// Guarantee not to put other regions on master // Guarantee not to put other regions on master
servers.remove(masterServerName); servers.remove(masterServerName);
List<HRegionInfo> masterRegions = assignments.get(masterServerName); List<HRegionInfo> masterRegions = assignments.get(masterServerName);
if (!masterRegions.isEmpty()) { regions = regions.entrySet().stream().filter(e -> !masterRegions.contains(e.getKey()))
regions = new HashMap<>(regions); .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
for (HRegionInfo region: masterRegions) {
regions.remove(region);
}
}
} }
if (regions == null || regions.isEmpty()) { if (regions.isEmpty()) {
return assignments; return assignments;
} }