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