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;
019
020import static org.junit.jupiter.api.Assertions.assertEquals;
021
022import java.util.List;
023import org.apache.hadoop.hbase.HBaseTestingUtil;
024import org.apache.hadoop.hbase.TableName;
025import org.apache.hadoop.hbase.client.Admin;
026import org.apache.hadoop.hbase.client.Put;
027import org.apache.hadoop.hbase.client.RegionInfo;
028import org.apache.hadoop.hbase.client.ResultScanner;
029import org.apache.hadoop.hbase.client.Scan;
030import org.apache.hadoop.hbase.client.Table;
031import org.apache.hadoop.hbase.master.assignment.SplitTableRegionProcedure;
032import org.apache.hadoop.hbase.master.assignment.TransitRegionStateProcedure;
033import org.apache.hadoop.hbase.master.procedure.MasterProcedureEnv;
034import org.apache.hadoop.hbase.procedure2.ProcedureExecutor;
035import org.apache.hadoop.hbase.testclassification.MasterTests;
036import org.apache.hadoop.hbase.testclassification.MediumTests;
037import org.apache.hadoop.hbase.util.Bytes;
038import org.junit.jupiter.api.AfterAll;
039import org.junit.jupiter.api.BeforeAll;
040import org.junit.jupiter.api.Tag;
041import org.junit.jupiter.api.Test;
042import org.slf4j.Logger;
043import org.slf4j.LoggerFactory;
044
045import org.apache.hbase.thirdparty.com.google.common.io.Closeables;
046
047@Tag(MasterTests.TAG)
048@Tag(MediumTests.TAG)
049public class TestSplitRegionWhileRSCrash {
050
051  private static final Logger LOG = LoggerFactory.getLogger(TestSplitRegionWhileRSCrash.class);
052
053  protected static final HBaseTestingUtil UTIL = new HBaseTestingUtil();
054  private static TableName TABLE_NAME = TableName.valueOf("test");
055  private static Admin ADMIN;
056  private static byte[] CF = Bytes.toBytes("cf");
057  private static Table TABLE;
058
059  @BeforeAll
060  public static void setupCluster() throws Exception {
061    UTIL.startMiniCluster(1);
062    ADMIN = UTIL.getAdmin();
063    TABLE = UTIL.createTable(TABLE_NAME, CF);
064    UTIL.waitTableAvailable(TABLE_NAME);
065  }
066
067  @AfterAll
068  public static void cleanupTest() throws Exception {
069    Closeables.close(TABLE, true);
070    UTIL.shutdownMiniCluster();
071  }
072
073  @Test
074  public void test() throws Exception {
075    MasterProcedureEnv env =
076      UTIL.getMiniHBaseCluster().getMaster().getMasterProcedureExecutor().getEnvironment();
077    final ProcedureExecutor<MasterProcedureEnv> executor =
078      UTIL.getMiniHBaseCluster().getMaster().getMasterProcedureExecutor();
079    List<RegionInfo> regionInfos = ADMIN.getRegions(TABLE_NAME);
080    // Since a flush request will be sent while initializing SplitTableRegionProcedure
081    // Create SplitTableRegionProcedure first before put data
082    SplitTableRegionProcedure splitProcedure =
083      new SplitTableRegionProcedure(env, regionInfos.get(0), Bytes.toBytes("row5"));
084    // write some rows to the table
085    LOG.info("Begin to put data");
086    for (int i = 0; i < 10; i++) {
087      byte[] row = Bytes.toBytes("row" + i);
088      Put put = new Put(row);
089      put.addColumn(CF, CF, CF);
090      TABLE.put(put);
091    }
092    executor.submitProcedure(splitProcedure);
093    LOG.info("SplitProcedure submitted");
094    UTIL.waitFor(30000,
095      () -> executor.getProcedures().stream().filter(p -> p instanceof TransitRegionStateProcedure)
096        .map(p -> (TransitRegionStateProcedure) p)
097        .anyMatch(p -> TABLE_NAME.equals(p.getTableName())));
098    UTIL.getMiniHBaseCluster()
099      .killRegionServer(UTIL.getMiniHBaseCluster().getRegionServer(0).getServerName());
100    UTIL.getMiniHBaseCluster().startRegionServer();
101    UTIL.waitUntilNoRegionsInTransition();
102    Scan scan = new Scan();
103    ResultScanner results = TABLE.getScanner(scan);
104    int count = 0;
105    while (results.next() != null) {
106      count++;
107    }
108    assertEquals(10, count, "There should be 10 rows!");
109  }
110}