HBASE-26921 Rewrite the counting cells part in TestMultiVersions (#4316)
[hbase.git] / hbase-server / src / test / java / org / apache / hadoop / hbase / master / TestMasterBalanceThrottling.java
blob7bc8e5b56ea7fec0773c015cd0c354250f6a1292
1 /**
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
18 package org.apache.hadoop.hbase.master;
20 import static org.junit.Assert.assertTrue;
22 import java.io.IOException;
23 import java.util.concurrent.atomic.AtomicBoolean;
24 import java.util.concurrent.atomic.AtomicInteger;
25 import org.apache.hadoop.hbase.HBaseClassTestRule;
26 import org.apache.hadoop.hbase.HBaseTestingUtil;
27 import org.apache.hadoop.hbase.HConstants;
28 import org.apache.hadoop.hbase.TableName;
29 import org.apache.hadoop.hbase.client.RegionInfo;
30 import org.apache.hadoop.hbase.regionserver.HRegionServer;
31 import org.apache.hadoop.hbase.testclassification.MasterTests;
32 import org.apache.hadoop.hbase.testclassification.MediumTests;
33 import org.apache.hadoop.hbase.util.Bytes;
34 import org.junit.After;
35 import org.junit.Before;
36 import org.junit.ClassRule;
37 import org.junit.Ignore;
38 import org.junit.Test;
39 import org.junit.experimental.categories.Category;
41 @Ignore // SimpleLoadBalancer seems borked whether AMv2 or not. Disabling till gets attention.
42 @Category({MasterTests.class, MediumTests.class})
43 public class TestMasterBalanceThrottling {
45 @ClassRule
46 public static final HBaseClassTestRule CLASS_RULE =
47 HBaseClassTestRule.forClass(TestMasterBalanceThrottling.class);
49 private static final HBaseTestingUtil TEST_UTIL = new HBaseTestingUtil();
50 private static final byte[] FAMILYNAME = Bytes.toBytes("fam");
52 @Before
53 public void setupConfiguration() {
54 TEST_UTIL.getConfiguration().set(HConstants.HBASE_MASTER_LOADBALANCER_CLASS,
55 "org.apache.hadoop.hbase.master.balancer.SimpleLoadBalancer");
58 @After
59 public void shutdown() throws Exception {
60 TEST_UTIL.getConfiguration().setInt(HConstants.HBASE_BALANCER_MAX_BALANCING,
61 HConstants.DEFAULT_HBASE_BALANCER_PERIOD);
62 TEST_UTIL.getConfiguration().setDouble(HConstants.HBASE_MASTER_BALANCER_MAX_RIT_PERCENT,
63 HConstants.DEFAULT_HBASE_MASTER_BALANCER_MAX_RIT_PERCENT);
64 TEST_UTIL.shutdownMiniCluster();
67 @Test
68 public void testThrottlingByBalanceInterval() throws Exception {
69 // Use default config and start a cluster of two regionservers.
70 TEST_UTIL.startMiniCluster(2);
72 TableName tableName = createTable("testNoThrottling");
73 final HMaster master = TEST_UTIL.getHBaseCluster().getMaster();
75 // Default max balancing time is 300000 ms and there are 50 regions to balance
76 // The balance interval is 6000 ms, much longger than the normal region in transition duration
77 // So the master can balance the region one by one
78 unbalance(master, tableName);
79 AtomicInteger maxCount = new AtomicInteger(0);
80 AtomicBoolean stop = new AtomicBoolean(false);
81 Thread checker = startBalancerChecker(master, maxCount, stop);
82 master.balance();
83 stop.set(true);
84 checker.interrupt();
85 checker.join();
86 assertTrue("max regions in transition: " + maxCount.get(), maxCount.get() == 1);
88 TEST_UTIL.deleteTable(tableName);
91 @Test
92 public void testThrottlingByMaxRitPercent() throws Exception {
93 // Set max balancing time to 500 ms and max percent of regions in transition to 0.05
94 TEST_UTIL.getConfiguration().setInt(HConstants.HBASE_BALANCER_MAX_BALANCING, 500);
95 TEST_UTIL.getConfiguration().setDouble(HConstants.HBASE_MASTER_BALANCER_MAX_RIT_PERCENT, 0.05);
96 TEST_UTIL.startMiniCluster(2);
98 TableName tableName = createTable("testThrottlingByMaxRitPercent");
99 final HMaster master = TEST_UTIL.getHBaseCluster().getMaster();
101 unbalance(master, tableName);
102 AtomicInteger maxCount = new AtomicInteger(0);
103 AtomicBoolean stop = new AtomicBoolean(false);
104 Thread checker = startBalancerChecker(master, maxCount, stop);
105 master.balance();
106 stop.set(true);
107 checker.interrupt();
108 checker.join();
109 // The max number of regions in transition is 100 * 0.05 = 5
110 assertTrue("max regions in transition: " + maxCount.get(), maxCount.get() == 5);
112 TEST_UTIL.deleteTable(tableName);
115 private TableName createTable(String table) throws IOException {
116 TableName tableName = TableName.valueOf(table);
117 byte[] startKey = new byte[] { 0x00 };
118 byte[] stopKey = new byte[] { 0x7f };
119 TEST_UTIL.createTable(tableName, new byte[][] { FAMILYNAME }, 1, startKey, stopKey,
120 100);
121 return tableName;
124 private Thread startBalancerChecker(final HMaster master, final AtomicInteger maxCount,
125 final AtomicBoolean stop) {
126 Runnable checker = new Runnable() {
127 @Override
128 public void run() {
129 while (!stop.get()) {
130 maxCount.set(Math.max(maxCount.get(),
131 master.getAssignmentManager().getRegionStates().getRegionsInTransitionCount()));
132 try {
133 Thread.sleep(10);
134 } catch (InterruptedException e) {
135 e.printStackTrace();
140 Thread thread = new Thread(checker);
141 thread.start();
142 return thread;
145 private void unbalance(HMaster master, TableName tableName) throws Exception {
146 while (master.getAssignmentManager().getRegionStates().getRegionsInTransitionCount() > 0) {
147 Thread.sleep(100);
149 HRegionServer biasedServer = TEST_UTIL.getMiniHBaseCluster().getRegionServer(0);
150 for (RegionInfo regionInfo : TEST_UTIL.getAdmin().getRegions(tableName)) {
151 master.move(regionInfo.getEncodedNameAsBytes(),
152 Bytes.toBytes(biasedServer.getServerName().getServerName()));
154 while (master.getAssignmentManager().getRegionStates().getRegionsInTransitionCount() > 0) {
155 Thread.sleep(100);