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