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.StartMiniClusterOption;
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.LargeTests;
033import org.apache.hadoop.hbase.testclassification.MasterTests;
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({ LargeTests.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 HBaseTestingUtility UTIL;
063  private MiniHBaseCluster cluster;
064  private MiniZooKeeperCluster zkCluster;
065  private HRegionServer rs1;
066  private HRegionServer rs2;
067  private RegionInfo regionInfo;
068
069  @Before
070  public void setup() throws Exception {
071    UTIL = new HBaseTestingUtility();
072    zkCluster = UTIL.startMiniZKCluster();
073    StartMiniClusterOption option = StartMiniClusterOption.builder().numRegionServers(2).build();
074    cluster = UTIL.startMiniHBaseCluster(option);
075    rs1 = cluster.getRegionServer(0);
076    rs2 = cluster.getRegionServer(1);
077    assertEquals(2, cluster.getRegionServerThreads().size());
078    // We'll use hbase:namespace for our testing
079    UTIL.waitTableAvailable(TableName.NAMESPACE_TABLE_NAME, 30_000);
080    regionInfo =
081      Iterables.getOnlyElement(cluster.getRegions(TableName.NAMESPACE_TABLE_NAME)).getRegionInfo();
082  }
083
084  @After
085  public void teardown() throws Exception {
086    if (cluster != null) {
087      cluster.shutdown();
088      cluster = null;
089    }
090    if (zkCluster != null) {
091      zkCluster.shutdown();
092      zkCluster = null;
093    }
094  }
095
096  @Test
097  public void test() throws Exception {
098    LOG.info("Moving {} to {}", regionInfo, rs2.getServerName());
099    // Move to RS2
100    UTIL.moveRegionAndWait(regionInfo, rs2.getServerName());
101    LOG.info("Moving {} to {}", regionInfo, rs1.getServerName());
102    // Move to RS1
103    UTIL.moveRegionAndWait(regionInfo, rs1.getServerName());
104    LOG.info("Killing RS {}", rs1.getServerName());
105    // Stop RS1
106    cluster.killRegionServer(rs1.getServerName());
107    // Region should get moved to RS2
108    UTIL.waitTableAvailable(TableName.NAMESPACE_TABLE_NAME, 30_000);
109    // Restart the master
110    LOG.info("Killing master {}", cluster.getMaster().getServerName());
111    cluster.killMaster(cluster.getMaster().getServerName());
112    // Stop RS2
113    LOG.info("Killing RS {}", rs2.getServerName());
114    cluster.killRegionServer(rs2.getServerName());
115    // Start up everything again
116    LOG.info("Starting cluster");
117    UTIL.getMiniHBaseCluster().startMaster();
118    UTIL.ensureSomeRegionServersAvailable(2);
119
120    UTIL.waitFor(30_000, new Waiter.Predicate<Exception>() {
121      @Override
122      public boolean evaluate() throws Exception {
123        try (Table nsTable = UTIL.getConnection().getTable(TableName.NAMESPACE_TABLE_NAME)) {
124          // Doesn't matter what we're getting. We just want to make sure we can access the region
125          nsTable.get(new Get(Bytes.toBytes("a")));
126          return true;
127        }
128      }
129    });
130  }
131}