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.TableName;
028import org.apache.hadoop.hbase.client.ColumnFamilyDescriptorBuilder;
029import org.apache.hadoop.hbase.client.TableDescriptor;
030import org.apache.hadoop.hbase.client.TableDescriptorBuilder;
031import org.apache.hadoop.hbase.master.assignment.AssignmentTestingUtil;
032import org.apache.hadoop.hbase.net.Address;
033import org.apache.hadoop.hbase.testclassification.MediumTests;
034import org.apache.hadoop.hbase.util.Bytes;
035import org.apache.hadoop.hbase.util.JVMClusterUtil;
036import org.apache.hadoop.hbase.util.Threads;
037import org.junit.After;
038import org.junit.AfterClass;
039import org.junit.Before;
040import org.junit.BeforeClass;
041import org.junit.ClassRule;
042import org.junit.Test;
043import org.junit.experimental.categories.Category;
044import org.slf4j.Logger;
045import org.slf4j.LoggerFactory;
046
047@Category(MediumTests.class)
048public class TestRSGroupsFallback extends TestRSGroupsBase {
049
050  @ClassRule
051  public static final HBaseClassTestRule CLASS_RULE =
052      HBaseClassTestRule.forClass(TestRSGroupsFallback.class);
053
054  protected static final Logger LOG = LoggerFactory.getLogger(TestRSGroupsFallback.class);
055
056  private static final String FALLBACK_GROUP = "fallback";
057
058  @BeforeClass
059  public static void setUp() throws Exception {
060    Configuration conf = TEST_UTIL.getConfiguration();
061    conf.setBoolean(RSGroupBasedLoadBalancer.FALLBACK_GROUP_ENABLE_KEY, true);
062    setUpTestBeforeClass();
063    master.balanceSwitch(true);
064  }
065
066  @AfterClass
067  public static void tearDown() throws Exception {
068    tearDownAfterClass();
069  }
070
071  @Before
072  public void beforeMethod() throws Exception {
073    setUpBeforeMethod();
074  }
075
076  @After
077  public void afterMethod() throws Exception {
078    tearDownAfterMethod();
079  }
080
081  @Test
082  public void testFallback() throws Exception {
083    // add fallback group
084    addGroup(FALLBACK_GROUP, 1);
085    // add test group
086    String groupName = getGroupName(name.getMethodName());
087    addGroup(groupName, 1);
088    TableDescriptor desc = TableDescriptorBuilder.newBuilder(tableName)
089        .setColumnFamily(ColumnFamilyDescriptorBuilder.newBuilder(Bytes.toBytes("f")).build())
090        .build();
091    admin.createTable(desc);
092    rsGroupAdmin.moveTables(Collections.singleton(tableName), groupName);
093    TEST_UTIL.waitUntilAllRegionsAssigned(tableName);
094    // server of test group crash, regions move to default group
095    crashRsInGroup(groupName);
096    assertRegionsInGroup(tableName, RSGroupInfo.DEFAULT_GROUP);
097
098    // server of default group crash, regions move to any other group
099    crashRsInGroup(RSGroupInfo.DEFAULT_GROUP);
100    assertRegionsInGroup(tableName, FALLBACK_GROUP);
101
102    // add a new server to default group, regions move to default group
103    TEST_UTIL.getMiniHBaseCluster().startRegionServerAndWait(60000);
104    master.balance();
105    assertRegionsInGroup(tableName, RSGroupInfo.DEFAULT_GROUP);
106
107    // add a new server to test group, regions move back
108    JVMClusterUtil.RegionServerThread t =
109      TEST_UTIL.getMiniHBaseCluster().startRegionServerAndWait(60000);
110    rsGroupAdmin.moveServers(
111      Collections.singleton(t.getRegionServer().getServerName().getAddress()), groupName);
112    master.balance();
113    assertRegionsInGroup(tableName, groupName);
114
115    TEST_UTIL.deleteTable(tableName);
116  }
117
118  private void assertRegionsInGroup(TableName tableName, String group) throws IOException {
119    RSGroupInfo fallbackGroup = rsGroupAdmin.getRSGroupInfo(group);
120    master.getAssignmentManager().getRegionStates().getRegionsOfTable(tableName).forEach(region -> {
121      Address regionOnServer = master.getAssignmentManager().getRegionStates()
122          .getRegionAssignments().get(region).getAddress();
123      assertTrue(fallbackGroup.getServers().contains(regionOnServer));
124    });
125  }
126
127  private void crashRsInGroup(String groupName) throws Exception {
128    for (Address server : rsGroupAdmin.getRSGroupInfo(groupName).getServers()) {
129      AssignmentTestingUtil.crashRs(TEST_UTIL, getServerName(server), true);
130    }
131    Threads.sleep(1000);
132    TEST_UTIL.waitUntilNoRegionsInTransition(60000);
133  }
134}