HBASE-25334 TestRSGroupsFallback.testFallback is flaky (#2728)
[hbase.git] / hbase-server / src / test / java / org / apache / hadoop / hbase / rsgroup / TestRSGroupsFallback.java
blob478ffc654757af335a1107e1e053d7e47b58244c
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.rsgroup;
20 import static org.junit.Assert.assertTrue;
22 import java.io.IOException;
23 import java.util.Collections;
25 import org.apache.hadoop.conf.Configuration;
26 import org.apache.hadoop.hbase.HBaseClassTestRule;
27 import org.apache.hadoop.hbase.HBaseTestingUtility;
28 import org.apache.hadoop.hbase.HConstants;
29 import org.apache.hadoop.hbase.TableName;
30 import org.apache.hadoop.hbase.client.ColumnFamilyDescriptorBuilder;
31 import org.apache.hadoop.hbase.client.TableDescriptor;
32 import org.apache.hadoop.hbase.client.TableDescriptorBuilder;
33 import org.apache.hadoop.hbase.master.assignment.AssignmentTestingUtil;
34 import org.apache.hadoop.hbase.net.Address;
35 import org.apache.hadoop.hbase.procedure2.ProcedureTestingUtility;
36 import org.apache.hadoop.hbase.testclassification.MediumTests;
37 import org.apache.hadoop.hbase.testclassification.RSGroupTests;
38 import org.apache.hadoop.hbase.util.Bytes;
39 import org.apache.hadoop.hbase.util.JVMClusterUtil;
40 import org.apache.hadoop.hbase.util.Threads;
41 import org.junit.After;
42 import org.junit.AfterClass;
43 import org.junit.Before;
44 import org.junit.BeforeClass;
45 import org.junit.ClassRule;
46 import org.junit.Test;
47 import org.junit.experimental.categories.Category;
48 import org.slf4j.Logger;
49 import org.slf4j.LoggerFactory;
51 @Category({ RSGroupTests.class, MediumTests.class })
52 public class TestRSGroupsFallback extends TestRSGroupsBase {
54 @ClassRule
55 public static final HBaseClassTestRule CLASS_RULE =
56 HBaseClassTestRule.forClass(TestRSGroupsFallback.class);
58 protected static final Logger LOG = LoggerFactory.getLogger(TestRSGroupsFallback.class);
60 private static final String FALLBACK_GROUP = "fallback";
62 @BeforeClass
63 public static void setUp() throws Exception {
64 Configuration conf = TEST_UTIL.getConfiguration();
65 conf.setBoolean(RSGroupBasedLoadBalancer.FALLBACK_GROUP_ENABLE_KEY, true);
66 conf.setInt(HConstants.HBASE_BALANCER_MAX_BALANCING, 0);
67 setUpTestBeforeClass();
68 MASTER.balanceSwitch(true);
71 @AfterClass
72 public static void tearDown() throws Exception {
73 tearDownAfterClass();
76 @Before
77 public void beforeMethod() throws Exception {
78 setUpBeforeMethod();
81 @After
82 public void afterMethod() throws Exception {
83 tearDownAfterMethod();
86 @Test
87 public void testFallback() throws Exception {
88 // add fallback group
89 addGroup(FALLBACK_GROUP, 1);
90 // add test group
91 String groupName = getGroupName(name.getMethodName());
92 addGroup(groupName, 1);
93 TableDescriptor desc = TableDescriptorBuilder.newBuilder(tableName)
94 .setColumnFamily(ColumnFamilyDescriptorBuilder.newBuilder(Bytes.toBytes("f")).build())
95 .setRegionServerGroup(groupName)
96 .build();
97 ADMIN.createTable(desc, HBaseTestingUtility.KEYS_FOR_HBA_CREATE_TABLE);
98 TEST_UTIL.waitUntilAllRegionsAssigned(tableName);
99 // server of test group crash, regions move to default group
100 crashRsInGroup(groupName);
101 assertRegionsInGroup(tableName, RSGroupInfo.DEFAULT_GROUP);
103 // server of default group crash, regions move to any other group
104 crashRsInGroup(RSGroupInfo.DEFAULT_GROUP);
105 assertRegionsInGroup(tableName, FALLBACK_GROUP);
107 // add a new server to default group, regions move to default group
108 TEST_UTIL.getMiniHBaseCluster().startRegionServerAndWait(60000);
109 assertTrue(MASTER.balance());
110 assertRegionsInGroup(tableName, RSGroupInfo.DEFAULT_GROUP);
112 // add a new server to test group, regions move back
113 JVMClusterUtil.RegionServerThread t =
114 TEST_UTIL.getMiniHBaseCluster().startRegionServerAndWait(60000);
115 ADMIN.moveServersToRSGroup(
116 Collections.singleton(t.getRegionServer().getServerName().getAddress()), groupName);
117 assertTrue(MASTER.balance());
118 assertRegionsInGroup(tableName, groupName);
120 TEST_UTIL.deleteTable(tableName);
123 private void assertRegionsInGroup(TableName table, String group) throws IOException {
124 ProcedureTestingUtility.waitAllProcedures(
125 TEST_UTIL.getMiniHBaseCluster().getMaster().getMasterProcedureExecutor());
126 RSGroupInfo rsGroup = ADMIN.getRSGroup(group);
127 MASTER.getAssignmentManager().getRegionStates().getRegionsOfTable(table).forEach(region -> {
128 Address regionOnServer = MASTER.getAssignmentManager().getRegionStates()
129 .getRegionAssignments().get(region).getAddress();
130 assertTrue(rsGroup.getServers().contains(regionOnServer));
134 private void crashRsInGroup(String groupName) throws Exception {
135 for (Address server : ADMIN.getRSGroup(groupName).getServers()) {
136 AssignmentTestingUtil.crashRs(TEST_UTIL, getServerName(server), true);
138 Threads.sleep(1000);
139 TEST_UTIL.waitUntilNoRegionsInTransition(60000);