001/**
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package org.apache.hadoop.hbase.rsgroup;
019
020import static org.junit.Assert.assertTrue;
021
022import java.io.IOException;
023import java.util.Collections;
024
025import org.apache.hadoop.conf.Configuration;
026import org.apache.hadoop.hbase.HBaseClassTestRule;
027import org.apache.hadoop.hbase.HBaseTestingUtil;
028import org.apache.hadoop.hbase.HConstants;
029import org.apache.hadoop.hbase.TableName;
030import org.apache.hadoop.hbase.client.ColumnFamilyDescriptorBuilder;
031import org.apache.hadoop.hbase.client.TableDescriptor;
032import org.apache.hadoop.hbase.client.TableDescriptorBuilder;
033import org.apache.hadoop.hbase.master.assignment.AssignmentTestingUtil;
034import org.apache.hadoop.hbase.net.Address;
035import org.apache.hadoop.hbase.procedure2.ProcedureTestingUtility;
036import org.apache.hadoop.hbase.testclassification.MediumTests;
037import org.apache.hadoop.hbase.testclassification.RSGroupTests;
038import org.apache.hadoop.hbase.util.Bytes;
039import org.apache.hadoop.hbase.util.JVMClusterUtil;
040import org.apache.hadoop.hbase.util.Threads;
041import org.junit.After;
042import org.junit.AfterClass;
043import org.junit.Before;
044import org.junit.BeforeClass;
045import org.junit.ClassRule;
046import org.junit.Test;
047import org.junit.experimental.categories.Category;
048import org.slf4j.Logger;
049import org.slf4j.LoggerFactory;
050
051@Category({ RSGroupTests.class, MediumTests.class })
052public class TestRSGroupsFallback extends TestRSGroupsBase {
053
054  @ClassRule
055  public static final HBaseClassTestRule CLASS_RULE =
056      HBaseClassTestRule.forClass(TestRSGroupsFallback.class);
057
058  protected static final Logger LOG = LoggerFactory.getLogger(TestRSGroupsFallback.class);
059
060  private static final String FALLBACK_GROUP = "fallback";
061
062  @BeforeClass
063  public static void setUp() throws Exception {
064    Configuration conf = TEST_UTIL.getConfiguration();
065    conf.setBoolean(RSGroupBasedLoadBalancer.FALLBACK_GROUP_ENABLE_KEY, true);
066    conf.setInt(HConstants.HBASE_BALANCER_MAX_BALANCING, 0);
067    setUpTestBeforeClass();
068    MASTER.balanceSwitch(true);
069  }
070
071  @AfterClass
072  public static void tearDown() throws Exception {
073    tearDownAfterClass();
074  }
075
076  @Before
077  public void beforeMethod() throws Exception {
078    setUpBeforeMethod();
079  }
080
081  @After
082  public void afterMethod() throws Exception {
083    tearDownAfterMethod();
084  }
085
086  @Test
087  public void testFallback() throws Exception {
088    // add fallback group
089    addGroup(FALLBACK_GROUP, 1);
090    // add test group
091    String groupName = getGroupName(name.getMethodName());
092    addGroup(groupName, 1);
093    TableDescriptor desc = TableDescriptorBuilder.newBuilder(tableName)
094      .setColumnFamily(ColumnFamilyDescriptorBuilder.newBuilder(Bytes.toBytes("f")).build())
095      .setRegionServerGroup(groupName)
096      .build();
097    ADMIN.createTable(desc, HBaseTestingUtil.KEYS_FOR_HBA_CREATE_TABLE);
098    TEST_UTIL.waitUntilAllRegionsAssigned(tableName);
099    // server of test group crash, regions move to default group
100    crashRsInGroup(groupName);
101    assertRegionsInGroup(tableName, RSGroupInfo.DEFAULT_GROUP);
102
103    // server of default group crash, regions move to any other group
104    crashRsInGroup(RSGroupInfo.DEFAULT_GROUP);
105    assertRegionsInGroup(tableName, FALLBACK_GROUP);
106
107    // add a new server to default group, regions move to default group
108    TEST_UTIL.getMiniHBaseCluster().startRegionServerAndWait(60000);
109    assertTrue(MASTER.balance().isBalancerRan());
110    assertRegionsInGroup(tableName, RSGroupInfo.DEFAULT_GROUP);
111
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().isBalancerRan());
118    assertRegionsInGroup(tableName, groupName);
119
120    TEST_UTIL.deleteTable(tableName);
121  }
122
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));
131    });
132  }
133
134  private void crashRsInGroup(String groupName) throws Exception {
135    for (Address server : ADMIN.getRSGroup(groupName).getServers()) {
136      AssignmentTestingUtil.crashRs(TEST_UTIL, getServerName(server), true);
137    }
138    Threads.sleep(1000);
139    TEST_UTIL.waitUntilNoRegionsInTransition(60000);
140  }
141}