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;
021import org.apache.hadoop.hbase.HBaseClassTestRule;
022import org.apache.hadoop.hbase.HBaseTestingUtility;
023import org.apache.hadoop.hbase.MiniHBaseCluster;
024import org.apache.hadoop.hbase.StartMiniClusterOption;
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.MasterTests;
032import org.apache.hadoop.hbase.testclassification.MediumTests;
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;
044import org.apache.hbase.thirdparty.com.google.common.collect.Iterables;
045
046/**
047 * Testcase for HBASE-20792.
048 */
049@Category({ MediumTests.class, MasterTests.class })
050public class TestRegionMoveAndAbandon {
051  private static final Logger LOG = LoggerFactory.getLogger(TestRegionMoveAndAbandon.class);
052
053  @ClassRule
054  public static final HBaseClassTestRule CLASS_RULE =
055    HBaseClassTestRule.forClass(TestRegionMoveAndAbandon.class);
056
057  @Rule
058  public TestName name = new TestName();
059
060  private HBaseTestingUtility UTIL;
061  private MiniHBaseCluster cluster;
062  private MiniZooKeeperCluster zkCluster;
063  private HRegionServer rs1;
064  private HRegionServer rs2;
065  private RegionInfo regionInfo;
066
067  @Before
068  public void setup() throws Exception {
069    UTIL = new HBaseTestingUtility();
070    zkCluster = UTIL.startMiniZKCluster();
071    StartMiniClusterOption option = StartMiniClusterOption.builder().numRegionServers(2).build();
072    cluster = UTIL.startMiniHBaseCluster(option);
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    UTIL.waitFor(30_000, () -> rs2.isStopped() && !rs2.isAlive());
114    UTIL.waitFor(30_000, () -> rs1.isStopped() && !rs1.isAlive());
115    // make sure none of regionserver threads is alive.
116    UTIL.waitFor(30_000, () ->
117      UTIL.getMiniHBaseCluster().getLiveRegionServerThreads().isEmpty());
118    // Start up everything again
119    LOG.info("Starting cluster");
120    UTIL.getMiniHBaseCluster().startMaster();
121    UTIL.invalidateConnection();
122    UTIL.ensureSomeRegionServersAvailable(2);
123
124    UTIL.waitFor(30_000, new Waiter.Predicate<Exception>() {
125      @Override
126      public boolean evaluate() throws Exception {
127        try (Table nsTable = UTIL.getConnection().getTable(TableName.NAMESPACE_TABLE_NAME)) {
128          // Doesn't matter what we're getting. We just want to make sure we can access the region
129          nsTable.get(new Get(Bytes.toBytes("a")));
130          return true;
131        }
132      }
133    });
134  }
135}