Skip to content

Commit f260f09

Browse files
committed
HBASE-18480 The cost of BaseLoadBalancer.cluster is changed even if the rollback is done
1 parent ba5e870 commit f260f09

File tree

3 files changed

+30
-3
lines changed

3 files changed

+30
-3
lines changed

hbase-server/src/main/java/org/apache/hadoop/hbase/master/balancer/BaseLoadBalancer.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -805,6 +805,7 @@ void regionMoved(int region, int oldServer, int newServer) {
805805
} else if (oldServer >= 0 && (numRegionsPerServerPerTable[oldServer][tableIndex] + 1)
806806
== numMaxRegionsPerTable[tableIndex]) {
807807
//recompute maxRegionsPerTable since the previous value was coming from the old server
808+
numMaxRegionsPerTable[tableIndex] = 0;
808809
for (int serverIndex = 0 ; serverIndex < numRegionsPerServerPerTable.length; serverIndex++) {
809810
if (numRegionsPerServerPerTable[serverIndex][tableIndex] > numMaxRegionsPerTable[tableIndex]) {
810811
numMaxRegionsPerTable[tableIndex] = numRegionsPerServerPerTable[serverIndex][tableIndex];

hbase-server/src/main/java/org/apache/hadoop/hbase/master/balancer/StochasticLoadBalancer.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
*/
1818
package org.apache.hadoop.hbase.master.balancer;
1919

20+
import com.google.common.annotations.VisibleForTesting;
2021
import java.util.ArrayDeque;
2122
import java.util.ArrayList;
2223
import java.util.Arrays;
@@ -318,6 +319,12 @@ public synchronized List<RegionPlan> balanceCluster(TableName tableName, Map<Ser
318319
return balanceCluster(clusterState);
319320
}
320321

322+
@VisibleForTesting
323+
Cluster.Action nextAction(Cluster cluster) {
324+
return candidateGenerators.get(RANDOM.nextInt(candidateGenerators.size()))
325+
.generate(cluster);
326+
}
327+
321328
/**
322329
* Given the cluster state this will try and approach an optimal balance. This
323330
* should always approach the optimal state given enough steps.
@@ -384,9 +391,7 @@ public synchronized List<RegionPlan> balanceCluster(Map<ServerName,
384391
long step;
385392

386393
for (step = 0; step < computedMaxSteps; step++) {
387-
int generatorIdx = RANDOM.nextInt(candidateGenerators.size());
388-
CandidateGenerator p = candidateGenerators.get(generatorIdx);
389-
Cluster.Action action = p.generate(cluster);
394+
Cluster.Action action = nextAction(cluster);
390395

391396
if (action.type == Type.NULL) {
392397
continue;

hbase-server/src/test/java/org/apache/hadoop/hbase/master/balancer/TestStochasticLoadBalancer.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,27 @@ public void testSkewCost() {
280280
assertEquals(1, costFunction.cost(), 0.01);
281281
}
282282

283+
@Test
284+
public void testCostAfterUndoAction() {
285+
final int runs = 10;
286+
loadBalancer.setConf(conf);
287+
for (int[] mockCluster : clusterStateMocks) {
288+
BaseLoadBalancer.Cluster cluster = mockCluster(mockCluster);
289+
loadBalancer.initCosts(cluster);
290+
for (int i = 0; i != runs; ++i) {
291+
final double expectedCost = loadBalancer.computeCost(cluster, Double.MAX_VALUE);
292+
Cluster.Action action = loadBalancer.nextAction(cluster);
293+
cluster.doAction(action);
294+
loadBalancer.updateCostsWithAction(cluster, action);
295+
Cluster.Action undoAction = action.undoAction();
296+
cluster.doAction(undoAction);
297+
loadBalancer.updateCostsWithAction(cluster, undoAction);
298+
final double actualCost = loadBalancer.computeCost(cluster, Double.MAX_VALUE);
299+
assertEquals(expectedCost, actualCost, 0);
300+
}
301+
}
302+
}
303+
283304
@Test
284305
public void testTableSkewCost() {
285306
Configuration conf = HBaseConfiguration.create();

0 commit comments

Comments
 (0)