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.HConstants;
030import org.apache.hadoop.hbase.ServerName;
031import org.apache.hadoop.hbase.StartMiniClusterOption;
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.apache.hadoop.hbase.zookeeper.MetaTableLocator;
038import org.junit.AfterClass;
039import org.junit.Rule;
040import org.slf4j.Logger;
041import org.slf4j.LoggerFactory;
042
043/**
044 * Base class for testing the scenarios where replicas are enabled for the meta table.
045 */
046public class MetaWithReplicasTestBase {
047
048  private static final Logger LOG = LoggerFactory.getLogger(MetaWithReplicasTestBase.class);
049
050  protected static final HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility();
051
052  protected static final int REGIONSERVERS_COUNT = 3;
053
054  @Rule
055  public TableNameTestRule name = new TableNameTestRule();
056
057  protected static void startCluster() throws Exception {
058    TEST_UTIL.getConfiguration().setInt("zookeeper.session.timeout", 30000);
059    TEST_UTIL.getConfiguration().setInt(HConstants.META_REPLICAS_NUM, 3);
060    TEST_UTIL.getConfiguration()
061      .setInt(StorefileRefresherChore.REGIONSERVER_STOREFILE_REFRESH_PERIOD, 1000);
062    StartMiniClusterOption option = StartMiniClusterOption.builder().numAlwaysStandByMasters(1)
063      .numMasters(1).numRegionServers(REGIONSERVERS_COUNT).build();
064    TEST_UTIL.startMiniCluster(option);
065    AssignmentManager am = TEST_UTIL.getMiniHBaseCluster().getMaster().getAssignmentManager();
066    Set<ServerName> sns = new HashSet<ServerName>();
067    ServerName hbaseMetaServerName =
068      MetaTableLocator.getMetaRegionLocation(TEST_UTIL.getZooKeeperWatcher());
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}