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