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