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.assignment; 019 020import static org.junit.Assert.assertEquals; 021 022import org.apache.hadoop.hbase.HBaseClassTestRule; 023import org.apache.hadoop.hbase.HBaseTestingUtility; 024import org.apache.hadoop.hbase.MiniHBaseCluster; 025import org.apache.hadoop.hbase.TableName; 026import org.apache.hadoop.hbase.Waiter; 027import org.apache.hadoop.hbase.client.Get; 028import org.apache.hadoop.hbase.client.RegionInfo; 029import org.apache.hadoop.hbase.client.Table; 030import org.apache.hadoop.hbase.regionserver.HRegionServer; 031import org.apache.hadoop.hbase.testclassification.LargeTests; 032import org.apache.hadoop.hbase.testclassification.MasterTests; 033import org.apache.hadoop.hbase.util.Bytes; 034import org.apache.hadoop.hbase.zookeeper.MiniZooKeeperCluster; 035import org.junit.After; 036import org.junit.Before; 037import org.junit.ClassRule; 038import org.junit.Rule; 039import org.junit.Test; 040import org.junit.experimental.categories.Category; 041import org.junit.rules.TestName; 042import org.slf4j.Logger; 043import org.slf4j.LoggerFactory; 044 045import org.apache.hbase.thirdparty.com.google.common.collect.Iterables; 046 047/** 048 * Testcase for HBASE-20792. 049 */ 050@Category({ LargeTests.class, MasterTests.class }) 051public class TestRegionMoveAndAbandon { 052 private static final Logger LOG = LoggerFactory.getLogger(TestRegionMoveAndAbandon.class); 053 054 @ClassRule 055 public static final HBaseClassTestRule CLASS_RULE = 056 HBaseClassTestRule.forClass(TestRegionMoveAndAbandon.class); 057 058 @Rule 059 public TestName name = new TestName(); 060 061 private HBaseTestingUtility UTIL; 062 private MiniHBaseCluster cluster; 063 private MiniZooKeeperCluster zkCluster; 064 private HRegionServer rs1; 065 private HRegionServer rs2; 066 private RegionInfo regionInfo; 067 068 @Before 069 public void setup() throws Exception { 070 UTIL = new HBaseTestingUtility(); 071 zkCluster = UTIL.startMiniZKCluster(); 072 cluster = UTIL.startMiniHBaseCluster(1, 2); 073 rs1 = cluster.getRegionServer(0); 074 rs2 = cluster.getRegionServer(1); 075 assertEquals(2, cluster.getRegionServerThreads().size()); 076 // We'll use hbase:namespace for our testing 077 UTIL.waitTableAvailable(TableName.NAMESPACE_TABLE_NAME, 30_000); 078 regionInfo = 079 Iterables.getOnlyElement(cluster.getRegions(TableName.NAMESPACE_TABLE_NAME)).getRegionInfo(); 080 } 081 082 @After 083 public void teardown() throws Exception { 084 if (cluster != null) { 085 cluster.shutdown(); 086 cluster = null; 087 } 088 if (zkCluster != null) { 089 zkCluster.shutdown(); 090 zkCluster = null; 091 } 092 } 093 094 @Test 095 public void test() throws Exception { 096 LOG.info("Moving {} to {}", regionInfo, rs2.getServerName()); 097 // Move to RS2 098 UTIL.moveRegionAndWait(regionInfo, rs2.getServerName()); 099 LOG.info("Moving {} to {}", regionInfo, rs1.getServerName()); 100 // Move to RS1 101 UTIL.moveRegionAndWait(regionInfo, rs1.getServerName()); 102 LOG.info("Killing RS {}", rs1.getServerName()); 103 // Stop RS1 104 cluster.killRegionServer(rs1.getServerName()); 105 // Region should get moved to RS2 106 UTIL.waitTableAvailable(TableName.NAMESPACE_TABLE_NAME, 30_000); 107 // Restart the master 108 LOG.info("Killing master {}", cluster.getMaster().getServerName()); 109 cluster.killMaster(cluster.getMaster().getServerName()); 110 // Stop RS2 111 LOG.info("Killing RS {}", rs2.getServerName()); 112 cluster.killRegionServer(rs2.getServerName()); 113 // Start up everything again 114 LOG.info("Starting cluster"); 115 UTIL.getMiniHBaseCluster().startMaster(); 116 UTIL.ensureSomeRegionServersAvailable(2); 117 118 UTIL.waitFor(30_000, new Waiter.Predicate<Exception>() { 119 @Override 120 public boolean evaluate() throws Exception { 121 try (Table nsTable = UTIL.getConnection().getTable(TableName.NAMESPACE_TABLE_NAME)) { 122 // Doesn't matter what we're getting. We just want to make sure we can access the region 123 nsTable.get(new Get(Bytes.toBytes("a"))); 124 return true; 125 } 126 } 127 }); 128 } 129}