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