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.assertEquals;
021import static org.junit.Assert.assertFalse;
022
023import org.apache.hadoop.hbase.HBaseClassTestRule;
024import org.apache.hadoop.hbase.HBaseCluster;
025import org.apache.hadoop.hbase.HBaseTestingUtility;
026import org.apache.hadoop.hbase.HConstants;
027import org.apache.hadoop.hbase.HRegionInfo;
028import org.apache.hadoop.hbase.MiniHBaseCluster;
029import org.apache.hadoop.hbase.TableName;
030import org.apache.hadoop.hbase.Waiter;
031import org.apache.hadoop.hbase.client.Admin;
032import org.apache.hadoop.hbase.coprocessor.CoprocessorHost;
033import org.apache.hadoop.hbase.master.HMaster;
034import org.apache.hadoop.hbase.master.ServerManager;
035import org.apache.hadoop.hbase.regionserver.HRegionServer;
036import org.apache.hadoop.hbase.testclassification.MediumTests;
037import org.apache.hadoop.hbase.util.Bytes;
038import org.junit.AfterClass;
039import org.junit.Assert;
040import org.junit.BeforeClass;
041import org.junit.ClassRule;
042import org.junit.Rule;
043import org.junit.Test;
044import org.junit.experimental.categories.Category;
045import org.junit.rules.TestName;
046import org.slf4j.Logger;
047import org.slf4j.LoggerFactory;
048
049import org.apache.hbase.thirdparty.com.google.common.collect.Sets;
050
051// This tests that GroupBasedBalancer will use data in zk to do balancing during master startup.
052// This does not test retain assignment.
053// The tests brings up 3 RS, creates a new RS group 'my_group', moves 1 RS to 'my_group', assigns
054// 'hbase:rsgroup' to 'my_group', and kill the only server in that group so that 'hbase:rsgroup'
055// table isn't available. It then kills the active master and waits for backup master to come
056// online. In new master, RSGroupInfoManagerImpl gets the data from zk and waits for the expected
057// assignment with a timeout.
058@Category(MediumTests.class)
059public class TestRSGroupsOfflineMode {
060
061  @ClassRule
062  public static final HBaseClassTestRule CLASS_RULE =
063      HBaseClassTestRule.forClass(TestRSGroupsOfflineMode.class);
064
065  private static final Logger LOG = LoggerFactory.getLogger(TestRSGroupsOfflineMode.class);
066  private static HMaster master;
067  private static Admin hbaseAdmin;
068  private static HBaseTestingUtility TEST_UTIL;
069  private static HBaseCluster cluster;
070  private final static long WAIT_TIMEOUT = 60000 * 5;
071
072  @Rule
073  public TestName name = new TestName();
074
075  @BeforeClass
076  public static void setUp() throws Exception {
077    TEST_UTIL = new HBaseTestingUtility();
078    TEST_UTIL.getConfiguration().set(
079        HConstants.HBASE_MASTER_LOADBALANCER_CLASS,
080        RSGroupBasedLoadBalancer.class.getName());
081    TEST_UTIL.getConfiguration().set(CoprocessorHost.MASTER_COPROCESSOR_CONF_KEY,
082        RSGroupAdminEndpoint.class.getName());
083    TEST_UTIL.getConfiguration().set(
084        ServerManager.WAIT_ON_REGIONSERVERS_MINTOSTART,
085        "1");
086    TEST_UTIL.startMiniCluster(2, 3);
087    cluster = TEST_UTIL.getHBaseCluster();
088    master = ((MiniHBaseCluster)cluster).getMaster();
089    master.balanceSwitch(false);
090    hbaseAdmin = TEST_UTIL.getAdmin();
091    //wait till the balancer is in online mode
092    TEST_UTIL.waitFor(WAIT_TIMEOUT, new Waiter.Predicate<Exception>() {
093      @Override
094      public boolean evaluate() throws Exception {
095        return master.isInitialized() &&
096            ((RSGroupBasedLoadBalancer) master.getLoadBalancer()).isOnline() &&
097            master.getServerManager().getOnlineServersList().size() >= 3;
098      }
099    });
100  }
101
102  @AfterClass
103  public static void tearDown() throws Exception {
104    TEST_UTIL.shutdownMiniCluster();
105  }
106
107  @Test
108  public void testOffline() throws Exception, InterruptedException {
109    // Table should be after group table name so it gets assigned later.
110    final TableName failoverTable = TableName.valueOf(name.getMethodName());
111    TEST_UTIL.createTable(failoverTable, Bytes.toBytes("f"));
112    final HRegionServer killRS = ((MiniHBaseCluster)cluster).getRegionServer(0);
113    final HRegionServer groupRS = ((MiniHBaseCluster)cluster).getRegionServer(1);
114    final HRegionServer failoverRS = ((MiniHBaseCluster)cluster).getRegionServer(2);
115    String newGroup =  "my_group";
116    RSGroupAdmin groupAdmin = new RSGroupAdminClient(TEST_UTIL.getConnection());
117    groupAdmin.addRSGroup(newGroup);
118    if(master.getAssignmentManager().getRegionStates().getRegionAssignments()
119          .containsValue(failoverRS.getServerName())) {
120      for (HRegionInfo regionInfo : hbaseAdmin.getOnlineRegions(failoverRS.getServerName())) {
121        hbaseAdmin.move(regionInfo.getEncodedNameAsBytes(),
122            Bytes.toBytes(failoverRS.getServerName().getServerName()));
123      }
124      LOG.info("Waiting for region unassignments on failover RS...");
125      TEST_UTIL.waitFor(WAIT_TIMEOUT, new Waiter.Predicate<Exception>() {
126        @Override public boolean evaluate() throws Exception {
127          return !master.getServerManager().getLoad(failoverRS.getServerName())
128              .getRegionMetrics().isEmpty();
129        }
130      });
131    }
132
133    // Move server to group and make sure all tables are assigned.
134    groupAdmin.moveServers(Sets.newHashSet(groupRS.getServerName().getAddress()), newGroup);
135    TEST_UTIL.waitFor(WAIT_TIMEOUT, new Waiter.Predicate<Exception>() {
136      @Override
137      public boolean evaluate() throws Exception {
138        return groupRS.getNumberOfOnlineRegions() < 1 &&
139            master.getAssignmentManager().getRegionStates().getRegionsInTransitionCount() < 1;
140      }
141    });
142    // Move table to group and wait.
143    groupAdmin.moveTables(Sets.newHashSet(RSGroupInfoManager.RSGROUP_TABLE_NAME), newGroup);
144    LOG.info("Waiting for move table...");
145    TEST_UTIL.waitFor(WAIT_TIMEOUT, new Waiter.Predicate<Exception>() {
146      @Override
147      public boolean evaluate() throws Exception {
148        return groupRS.getNumberOfOnlineRegions() == 1;
149      }
150    });
151
152    groupRS.stop("die");
153    // Race condition here.
154    TEST_UTIL.getHBaseCluster().getMaster().stopMaster();
155    LOG.info("Waiting for offline mode...");
156    TEST_UTIL.waitFor(WAIT_TIMEOUT, new Waiter.Predicate<Exception>() {
157      @Override
158      public boolean evaluate() throws Exception {
159        return TEST_UTIL.getHBaseCluster().getMaster() != null &&
160            TEST_UTIL.getHBaseCluster().getMaster().isActiveMaster() &&
161            TEST_UTIL.getHBaseCluster().getMaster().isInitialized() &&
162            TEST_UTIL.getHBaseCluster().getMaster().getServerManager().getOnlineServers().size()
163                <= 3;
164      }
165    });
166
167    // Get groupInfoManager from the new active master.
168    RSGroupInfoManager groupMgr = ((MiniHBaseCluster)cluster).getMaster().getMasterCoprocessorHost()
169            .findCoprocessor(RSGroupAdminEndpoint.class).getGroupInfoManager();
170    // Make sure balancer is in offline mode, since this is what we're testing.
171    assertFalse(groupMgr.isOnline());
172    // Verify the group affiliation that's loaded from ZK instead of tables.
173    assertEquals(newGroup,
174        groupMgr.getRSGroupOfTable(RSGroupInfoManager.RSGROUP_TABLE_NAME));
175    assertEquals(RSGroupInfo.DEFAULT_GROUP, groupMgr.getRSGroupOfTable(failoverTable));
176    // Kill final regionserver to see the failover happens for all tables except GROUP table since
177    // it's group does not have any online RS.
178    killRS.stop("die");
179    master = TEST_UTIL.getHBaseCluster().getMaster();
180    LOG.info("Waiting for new table assignment...");
181    TEST_UTIL.waitFor(WAIT_TIMEOUT, new Waiter.Predicate<Exception>() {
182      @Override
183      public boolean evaluate() throws Exception {
184        return failoverRS.getRegions(failoverTable).size() >= 1;
185      }
186    });
187    Assert.assertEquals(0, failoverRS.getRegions(RSGroupInfoManager.RSGROUP_TABLE_NAME).size());
188
189    // Need this for minicluster to shutdown cleanly.
190    master.stopMaster();
191  }
192}