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.MergeTableRegionsProcedure;
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
050
051@Category({MasterTests.class, MediumTests.class})
052public class TestMergeTableRegionsWhileRSCrash {
053
054  @ClassRule
055  public static final HBaseClassTestRule CLASS_RULE =
056      HBaseClassTestRule.forClass(TestMergeTableRegionsWhileRSCrash.class);
057
058  private static final Logger LOG = LoggerFactory
059      .getLogger(TestMergeTableRegionsWhileRSCrash.class);
060
061  protected static final HBaseTestingUtility UTIL = new HBaseTestingUtility();
062  private static TableName TABLE_NAME = TableName.valueOf("test");
063  private static Admin admin;
064  private static byte[] CF = Bytes.toBytes("cf");
065  private static byte[] SPLITKEY = Bytes.toBytes("row5");
066  private static CountDownLatch mergeCommitArrive = new CountDownLatch(1);
067  private static Table TABLE;
068
069
070  @BeforeClass
071  public static void setupCluster() throws Exception {
072    UTIL.startMiniCluster(1);
073    admin = UTIL.getHBaseAdmin();
074    byte[][] splitKeys = new byte[1][];
075    splitKeys[0] = SPLITKEY;
076    TABLE = UTIL.createTable(TABLE_NAME, CF, splitKeys);
077    UTIL.waitTableAvailable(TABLE_NAME);
078  }
079
080  @AfterClass
081  public static void cleanupTest() throws Exception {
082    try {
083      UTIL.shutdownMiniCluster();
084    } catch (Exception e) {
085      LOG.warn("failure shutting down cluster", e);
086    }
087  }
088
089  @Test
090  public void test() throws Exception {
091    //write some rows to the table
092    for (int i = 0; i < 10; i++) {
093      byte[] row = Bytes.toBytes("row" + i);
094      Put put = new Put(row);
095      put.addColumn(CF, CF, CF);
096      TABLE.put(put);
097    }
098    MasterProcedureEnv env = UTIL.getMiniHBaseCluster().getMaster()
099        .getMasterProcedureExecutor().getEnvironment();
100    final ProcedureExecutor<MasterProcedureEnv> executor = UTIL.getMiniHBaseCluster()
101        .getMaster().getMasterProcedureExecutor();
102    List<RegionInfo> regionInfos = admin.getRegions(TABLE_NAME);
103    MergeTableRegionsProcedure mergeTableRegionsProcedure = new MergeTableRegionsProcedure(
104        env, new RegionInfo [] {regionInfos.get(0), regionInfos.get(1)}, false);
105    executor.submitProcedure(mergeTableRegionsProcedure);
106    UTIL.waitFor(30000, () -> executor.getProcedures().stream()
107        .filter(p -> p instanceof UnassignProcedure)
108        .map(p -> (UnassignProcedure) p)
109        .anyMatch(p -> TABLE_NAME.equals(p.getTableName())));
110    UTIL.getMiniHBaseCluster().killRegionServer(
111        UTIL.getMiniHBaseCluster().getRegionServer(0).getServerName());
112    UTIL.getMiniHBaseCluster().startRegionServer();
113    UTIL.waitUntilNoRegionsInTransition();
114    Scan scan = new Scan();
115    ResultScanner results = TABLE.getScanner(scan);
116    int count = 0;
117    Result result = null;
118    while ((result = results.next()) != null) {
119      count++;
120    }
121    Assert.assertEquals("There should be 10 rows!", 10, count);
122  }
123}