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