clarax commented on a change in pull request #3575:
URL: https://github.com/apache/hbase/pull/3575#discussion_r686549918
##########
File path:
hbase-balancer/src/main/java/org/apache/hadoop/hbase/master/balancer/BalancerClusterState.java
##########
@@ -699,102 +684,57 @@ void regionMoved(int region, int oldServer, int
newServer) {
// update for servers
int primary = regionIndexToPrimaryIndex[region];
if (oldServer >= 0) {
- primariesOfRegionsPerServer[oldServer] =
- removeRegion(primariesOfRegionsPerServer[oldServer], primary);
+ removeElement(primariesOfRegionsPerServer.get(oldServer), primary,
region);
}
- primariesOfRegionsPerServer[newServer] =
- addRegionSorted(primariesOfRegionsPerServer[newServer], primary);
+ addElement(primariesOfRegionsPerServer.get(newServer), primary, region);
// update for hosts
if (multiServersPerHost) {
- int oldHost = oldServer >= 0 ? serverIndexToHostIndex[oldServer] : -1;
- int newHost = serverIndexToHostIndex[newServer];
- if (newHost != oldHost) {
- regionsPerHost[newHost] = addRegion(regionsPerHost[newHost], region);
- primariesOfRegionsPerHost[newHost] =
- addRegionSorted(primariesOfRegionsPerHost[newHost], primary);
- if (oldHost >= 0) {
- regionsPerHost[oldHost] = removeRegion(regionsPerHost[oldHost],
region);
- primariesOfRegionsPerHost[oldHost] =
- removeRegion(primariesOfRegionsPerHost[oldHost], primary); // will
still be sorted
- }
- }
+ updateForLocation(serverIndexToHostIndex, regionsPerHost,
primariesOfRegionsPerHost,
+ oldServer, newServer, primary, region);
}
// update for racks
if (numRacks > 1) {
- int oldRack = oldServer >= 0 ? serverIndexToRackIndex[oldServer] : -1;
- int newRack = serverIndexToRackIndex[newServer];
- if (newRack != oldRack) {
- regionsPerRack[newRack] = addRegion(regionsPerRack[newRack], region);
- primariesOfRegionsPerRack[newRack] =
- addRegionSorted(primariesOfRegionsPerRack[newRack], primary);
- if (oldRack >= 0) {
- regionsPerRack[oldRack] = removeRegion(regionsPerRack[oldRack],
region);
- primariesOfRegionsPerRack[oldRack] =
- removeRegion(primariesOfRegionsPerRack[oldRack], primary); // will
still be sorted
- }
- }
- }
- }
-
- int[] removeRegion(int[] regions, int regionIndex) {
- // TODO: this maybe costly. Consider using linked lists
- int[] newRegions = new int[regions.length - 1];
- int i = 0;
- for (i = 0; i < regions.length; i++) {
- if (regions[i] == regionIndex) {
- break;
- }
- newRegions[i] = regions[i];
+ updateForLocation(serverIndexToRackIndex, regionsPerRack,
primariesOfRegionsPerRack,
+ oldServer, newServer, primary, region);
}
- System.arraycopy(regions, i + 1, newRegions, i, newRegions.length - i);
- return newRegions;
- }
-
- int[] addRegion(int[] regions, int regionIndex) {
- int[] newRegions = new int[regions.length + 1];
- System.arraycopy(regions, 0, newRegions, 0, regions.length);
- newRegions[newRegions.length - 1] = regionIndex;
- return newRegions;
}
- int[] addRegionSorted(int[] regions, int regionIndex) {
- int[] newRegions = new int[regions.length + 1];
- int i = 0;
- for (i = 0; i < regions.length; i++) { // find the index to insert
- if (regions[i] > regionIndex) {
- break;
+ /**
+ * Common method for per host and per rack region index updates when a
region is moved.
+ * @param serverIndexToLocation serverIndexToHostIndex or
serverIndexToRackIndex
+ * @param regionsPerLocation regionsPerHost or regionsPerRack
+ * @param primariesOfRegionsPerLocation primariesOfRegionsPerHost or
primariesOfRegionsPerRack
+ */
+ private void updateForLocation(int[] serverIndexToLocation,
+ ArrayList<HashSet<Integer>> regionsPerLocation,
+ ArrayList<HashMap<Integer, ArrayList<Integer>>>
primariesOfRegionsPerLocation,
+ int oldServer, int newServer, int primary, int region) {
+ int oldLocation = oldServer >= 0 ? serverIndexToLocation[oldServer] : -1;
+ int newLocation = serverIndexToLocation[newServer];
+ if (newLocation != oldLocation) {
+ regionsPerLocation.get(newLocation).add(region);
+ addElement(primariesOfRegionsPerLocation.get(newLocation), primary,
region);
+ if (oldLocation >= 0) {
+ regionsPerLocation.get(oldLocation).remove(region);
+ removeElement(primariesOfRegionsPerLocation.get(oldLocation), primary,
region);
Review comment:
O(n)->O(1) for removing an element by its value. Please notice in the
hash map the key is the value of the element of the old int[].
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]