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.client;
019
020import static org.junit.Assert.assertNotEquals;
021import static org.junit.Assert.assertNotNull;
022import static org.junit.Assert.assertTrue;
023
024import java.util.HashSet;
025import java.util.Set;
026import org.apache.hadoop.hbase.HBaseTestingUtil;
027import org.apache.hadoop.hbase.ServerName;
028import org.apache.hadoop.hbase.StartTestingClusterOption;
029import org.apache.hadoop.hbase.TableName;
030import org.apache.hadoop.hbase.TableNameTestRule;
031import org.apache.hadoop.hbase.master.assignment.AssignmentManager;
032import org.apache.hadoop.hbase.master.assignment.AssignmentTestingUtil;
033import org.apache.hadoop.hbase.regionserver.StorefileRefresherChore;
034import org.junit.AfterClass;
035import org.junit.Rule;
036import org.slf4j.Logger;
037import org.slf4j.LoggerFactory;
038
039/**
040 * Base class for testing the scenarios where replicas are enabled for the meta table.
041 */
042public class MetaWithReplicasTestBase {
043
044  private static final Logger LOG = LoggerFactory.getLogger(MetaWithReplicasTestBase.class);
045
046  protected static final HBaseTestingUtil TEST_UTIL = new HBaseTestingUtil();
047
048  protected static final int REGIONSERVERS_COUNT = 3;
049
050  @Rule
051  public TableNameTestRule name = new TableNameTestRule();
052
053  protected static void startCluster() throws Exception {
054    TEST_UTIL.getConfiguration().setInt("zookeeper.session.timeout", 30000);
055    TEST_UTIL.getConfiguration()
056      .setInt(StorefileRefresherChore.REGIONSERVER_STOREFILE_REFRESH_PERIOD, 1000);
057    StartTestingClusterOption option = StartTestingClusterOption.builder()
058      .numAlwaysStandByMasters(1).numMasters(1).numRegionServers(REGIONSERVERS_COUNT).build();
059    TEST_UTIL.startMiniCluster(option);
060    Admin admin = TEST_UTIL.getAdmin();
061    HBaseTestingUtil.setReplicas(admin, TableName.META_TABLE_NAME, 3);
062    AssignmentManager am = TEST_UTIL.getMiniHBaseCluster().getMaster().getAssignmentManager();
063    Set<ServerName> sns = new HashSet<ServerName>();
064    ServerName hbaseMetaServerName = am.getRegionStates()
065      .getRegionStateNode(RegionInfoBuilder.FIRST_META_REGIONINFO).getRegionLocation();
066    LOG.info("HBASE:META DEPLOY: on " + hbaseMetaServerName);
067    sns.add(hbaseMetaServerName);
068    for (int replicaId = 1; replicaId < 3; replicaId++) {
069      RegionInfo h = RegionReplicaUtil
070        .getRegionInfoForReplica(RegionInfoBuilder.FIRST_META_REGIONINFO, replicaId);
071      AssignmentTestingUtil.waitForAssignment(am, h);
072      ServerName sn = am.getRegionStates().getRegionServerOfRegion(h);
073      assertNotNull(sn);
074      LOG.info("HBASE:META DEPLOY: " + h.getRegionNameAsString() + " on " + sn);
075      sns.add(sn);
076    }
077    // Fun. All meta region replicas have ended up on the one server. This will cause this test
078    // to fail ... sometimes.
079    if (sns.size() == 1) {
080      int count = TEST_UTIL.getMiniHBaseCluster().getLiveRegionServerThreads().size();
081      assertTrue("count=" + count, count == REGIONSERVERS_COUNT);
082      LOG.warn("All hbase:meta replicas are on the one server; moving hbase:meta: " + sns);
083      int metaServerIndex = TEST_UTIL.getHBaseCluster().getServerWithMeta();
084      int newServerIndex = metaServerIndex;
085      while (newServerIndex == metaServerIndex) {
086        newServerIndex = (newServerIndex + 1) % REGIONSERVERS_COUNT;
087      }
088      assertNotEquals(metaServerIndex, newServerIndex);
089      ServerName destinationServerName =
090        TEST_UTIL.getHBaseCluster().getRegionServer(newServerIndex).getServerName();
091      ServerName metaServerName =
092        TEST_UTIL.getHBaseCluster().getRegionServer(metaServerIndex).getServerName();
093      assertNotEquals(destinationServerName, metaServerName);
094      TEST_UTIL.getAdmin().move(RegionInfoBuilder.FIRST_META_REGIONINFO.getEncodedNameAsBytes(),
095        destinationServerName);
096    }
097    // Disable the balancer
098    TEST_UTIL.getAdmin().balancerSwitch(false, true);
099    LOG.debug("All meta replicas assigned");
100  }
101
102  @AfterClass
103  public static void tearDown() throws Exception {
104    TEST_UTIL.shutdownMiniCluster();
105  }
106}