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.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() 060 .setInt(StorefileRefresherChore.REGIONSERVER_STOREFILE_REFRESH_PERIOD, 1000); 061 StartMiniClusterOption option = StartMiniClusterOption.builder().numAlwaysStandByMasters(1) 062 .numMasters(1).numRegionServers(REGIONSERVERS_COUNT).build(); 063 TEST_UTIL.startMiniCluster(option); 064 Admin admin = TEST_UTIL.getAdmin(); 065 HBaseTestingUtility.setReplicas(admin, TableName.META_TABLE_NAME, 3); 066 AssignmentManager am = TEST_UTIL.getMiniHBaseCluster().getMaster().getAssignmentManager(); 067 Set<ServerName> sns = new HashSet<ServerName>(); 068 ServerName hbaseMetaServerName = 069 MetaTableLocator.getMetaRegionLocation(TEST_UTIL.getZooKeeperWatcher()); 070 LOG.info("HBASE:META DEPLOY: on " + hbaseMetaServerName); 071 sns.add(hbaseMetaServerName); 072 for (int replicaId = 1; replicaId < 3; replicaId++) { 073 RegionInfo h = RegionReplicaUtil 074 .getRegionInfoForReplica(RegionInfoBuilder.FIRST_META_REGIONINFO, replicaId); 075 AssignmentTestingUtil.waitForAssignment(am, h); 076 ServerName sn = am.getRegionStates().getRegionServerOfRegion(h); 077 assertNotNull(sn); 078 LOG.info("HBASE:META DEPLOY: " + h.getRegionNameAsString() + " on " + sn); 079 sns.add(sn); 080 } 081 // Fun. All meta region replicas have ended up on the one server. This will cause this test 082 // to fail ... sometimes. 083 if (sns.size() == 1) { 084 int count = TEST_UTIL.getMiniHBaseCluster().getLiveRegionServerThreads().size(); 085 assertTrue("count=" + count, count == REGIONSERVERS_COUNT); 086 LOG.warn("All hbase:meta replicas are on the one server; moving hbase:meta: " + sns); 087 int metaServerIndex = TEST_UTIL.getHBaseCluster().getServerWithMeta(); 088 int newServerIndex = metaServerIndex; 089 while (newServerIndex == metaServerIndex) { 090 newServerIndex = (newServerIndex + 1) % REGIONSERVERS_COUNT; 091 } 092 assertNotEquals(metaServerIndex, newServerIndex); 093 ServerName destinationServerName = 094 TEST_UTIL.getHBaseCluster().getRegionServer(newServerIndex).getServerName(); 095 ServerName metaServerName = 096 TEST_UTIL.getHBaseCluster().getRegionServer(metaServerIndex).getServerName(); 097 assertNotEquals(destinationServerName, metaServerName); 098 TEST_UTIL.getAdmin().move(RegionInfoBuilder.FIRST_META_REGIONINFO.getEncodedNameAsBytes(), 099 destinationServerName); 100 } 101 // Disable the balancer 102 LoadBalancerTracker l = 103 new LoadBalancerTracker(TEST_UTIL.getZooKeeperWatcher(), new Abortable() { 104 AtomicBoolean aborted = new AtomicBoolean(false); 105 106 @Override 107 public boolean isAborted() { 108 return aborted.get(); 109 } 110 111 @Override 112 public void abort(String why, Throwable e) { 113 aborted.set(true); 114 } 115 }); 116 l.setBalancerOn(false); 117 LOG.debug("All meta replicas assigned"); 118 } 119 120 @AfterClass 121 public static void tearDown() throws Exception { 122 TEST_UTIL.shutdownMiniCluster(); 123 } 124}