Skip to content

Commit 170570b

Browse files
committed
HBASE-26872 Load rate calculator for cost functions should be more precise (#4253)
Signed-off-by: Bryan Beaudreault <[email protected]> Signed-off-by: Viraj Jasani<[email protected]>
1 parent 89e7050 commit 170570b

File tree

2 files changed

+21
-4
lines changed

2 files changed

+21
-4
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,9 @@ protected double getRegionLoadCost(Collection<BalancerRegionLoad> regionLoadList
4242
double cost = 0;
4343
do {
4444
double current = getCostFromRl(iter.next());
45-
cost += current - previous;
45+
cost += current >= previous ? current - previous : current;
4646
previous = current;
4747
} while (iter.hasNext());
4848
return Math.max(0, cost / (regionLoadList.size() - 1));
4949
}
50-
}
50+
}

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

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
import static org.junit.Assert.assertTrue;
2424
import static org.mockito.Mockito.mock;
2525
import static org.mockito.Mockito.when;
26-
2726
import java.util.ArrayList;
2827
import java.util.Arrays;
2928
import java.util.List;
@@ -50,7 +49,6 @@
5049
import org.junit.ClassRule;
5150
import org.junit.Test;
5251
import org.junit.experimental.categories.Category;
53-
5452
import org.apache.hbase.thirdparty.org.apache.commons.collections4.CollectionUtils;
5553

5654
@Category({ MasterTests.class, MediumTests.class })
@@ -449,6 +447,25 @@ public void testRegionLoadCost() {
449447
assertEquals(2.5, result, 0.01);
450448
}
451449

450+
@Test
451+
public void testRegionLoadCostWhenDecrease() {
452+
List<BalancerRegionLoad> regionLoads = new ArrayList<>();
453+
// test region loads of [1,2,1,4]
454+
for (int i = 1; i < 5; i++) {
455+
int load = i == 3 ? 1 : i;
456+
BalancerRegionLoad regionLoad = mock(BalancerRegionLoad.class);
457+
when(regionLoad.getReadRequestsCount()).thenReturn((long)load);
458+
regionLoads.add(regionLoad);
459+
}
460+
461+
Configuration conf = HBaseConfiguration.create();
462+
ReadRequestCostFunction readCostFunction =
463+
new ReadRequestCostFunction(conf);
464+
double rateResult = readCostFunction.getRegionLoadCost(regionLoads);
465+
// read requests are treated as a rate so the average rate here is simply 1
466+
assertEquals(1.67, rateResult, 0.01);
467+
}
468+
452469
@Test
453470
public void testLosingRs() throws Exception {
454471
int numNodes = 3;

0 commit comments

Comments
 (0)