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.master;
019
020import static org.junit.Assert.assertEquals;
021import static org.junit.Assert.assertNotNull;
022import static org.junit.Assert.assertTrue;
023
024import java.util.List;
025
026import org.apache.hadoop.hbase.HBaseClassTestRule;
027import org.apache.hadoop.hbase.HConstants;
028import org.apache.hadoop.hbase.MiniHBaseCluster;
029import org.apache.hadoop.hbase.ServerName;
030import org.apache.hadoop.hbase.TableName;
031import org.apache.hadoop.hbase.client.RegionInfo;
032import org.apache.hadoop.hbase.testclassification.MasterTests;
033import org.apache.hadoop.hbase.testclassification.MediumTests;
034import org.apache.hadoop.hbase.util.JVMClusterUtil;
035import org.junit.ClassRule;
036import org.junit.Test;
037import org.junit.experimental.categories.Category;
038import org.slf4j.Logger;
039import org.slf4j.LoggerFactory;
040
041@Category({ MasterTests.class, MediumTests.class })
042public class TestRoundRobinAssignmentOnRestart extends AbstractTestRestartCluster {
043
044  @ClassRule
045  public static final HBaseClassTestRule CLASS_RULE =
046      HBaseClassTestRule.forClass(TestRoundRobinAssignmentOnRestart.class);
047
048  private static final Logger LOG =
049      LoggerFactory.getLogger(TestRoundRobinAssignmentOnRestart.class);
050
051  @Override
052  protected boolean splitWALCoordinatedByZk() {
053    return true;
054  }
055
056  private final int regionNum = 10;
057  private final int rsNum = 2;
058
059  /**
060   * This tests retaining assignments on a cluster restart
061   */
062  @Test
063  public void test() throws Exception {
064    UTIL.startMiniCluster(rsNum);
065    // Turn off balancer
066    UTIL.getMiniHBaseCluster().getMaster().getMasterRpcServices().synchronousBalanceSwitch(false);
067    LOG.info("\n\nCreating tables");
068    for (TableName TABLE : TABLES) {
069      UTIL.createMultiRegionTable(TABLE, FAMILY, regionNum);
070    }
071    // Wait until all regions are assigned
072    for (TableName TABLE : TABLES) {
073      UTIL.waitTableEnabled(TABLE);
074    }
075    UTIL.waitUntilNoRegionsInTransition(60000);
076
077    MiniHBaseCluster cluster = UTIL.getHBaseCluster();
078    List<JVMClusterUtil.RegionServerThread> threads = cluster.getLiveRegionServerThreads();
079    assertEquals(2, threads.size());
080
081    ServerName testServer = threads.get(0).getRegionServer().getServerName();
082    int port = testServer.getPort();
083    List<RegionInfo> regionInfos =
084        cluster.getMaster().getAssignmentManager().getRegionsOnServer(testServer);
085    LOG.debug("RegionServer {} has {} regions", testServer, regionInfos.size());
086    assertTrue(regionInfos.size() >= (TABLES.length * regionNum / rsNum));
087
088    // Restart 1 regionserver
089    cluster.stopRegionServer(testServer);
090    cluster.waitForRegionServerToStop(testServer, 60000);
091    cluster.getConf().setInt(HConstants.REGIONSERVER_PORT, port);
092    cluster.startRegionServer();
093
094    HMaster master = UTIL.getMiniHBaseCluster().getMaster();
095    List<ServerName> localServers = master.getServerManager().getOnlineServersList();
096    ServerName newTestServer = null;
097    for (ServerName serverName : localServers) {
098      if (serverName.getAddress().equals(testServer.getAddress())) {
099        newTestServer = serverName;
100        break;
101      }
102    }
103    assertNotNull(newTestServer);
104
105    // Wait until all regions are assigned
106    for (TableName TABLE : TABLES) {
107      UTIL.waitTableAvailable(TABLE);
108    }
109    UTIL.waitUntilNoRegionsInTransition(60000);
110
111    List<RegionInfo> newRegionInfos =
112        cluster.getMaster().getAssignmentManager().getRegionsOnServer(newTestServer);
113    LOG.debug("RegionServer {} has {} regions", newTestServer, newRegionInfos.size());
114    assertTrue("Should not retain all regions when restart",
115        newRegionInfos.size() < regionInfos.size());
116  }
117}