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