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 static org.junit.jupiter.api.Assertions.assertEquals;
021
022import java.util.List;
023import java.util.concurrent.CountDownLatch;
024import org.apache.hadoop.hbase.HBaseTestingUtil;
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.TransitRegionStateProcedure;
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.jupiter.api.AfterAll;
041import org.junit.jupiter.api.BeforeAll;
042import org.junit.jupiter.api.Tag;
043import org.junit.jupiter.api.Test;
044import org.slf4j.Logger;
045import org.slf4j.LoggerFactory;
046
047@Tag(MasterTests.TAG)
048@Tag(MediumTests.TAG)
049public class TestMergeTableRegionsWhileRSCrash {
050
051  private static final Logger LOG =
052    LoggerFactory.getLogger(TestMergeTableRegionsWhileRSCrash.class);
053
054  protected static final HBaseTestingUtil UTIL = new HBaseTestingUtil();
055  private static TableName TABLE_NAME = TableName.valueOf("test");
056  private static Admin admin;
057  private static byte[] CF = Bytes.toBytes("cf");
058  private static byte[] SPLITKEY = Bytes.toBytes("row5");
059  private static CountDownLatch mergeCommitArrive = new CountDownLatch(1);
060  private static Table TABLE;
061
062  @BeforeAll
063  public static void setupCluster() throws Exception {
064    UTIL.startMiniCluster(1);
065    admin = UTIL.getAdmin();
066    byte[][] splitKeys = new byte[1][];
067    splitKeys[0] = SPLITKEY;
068    TABLE = UTIL.createTable(TABLE_NAME, CF, splitKeys);
069    UTIL.waitTableAvailable(TABLE_NAME);
070  }
071
072  @AfterAll
073  public static void cleanupTest() throws Exception {
074    try {
075      UTIL.shutdownMiniCluster();
076    } catch (Exception e) {
077      LOG.warn("failure shutting down cluster", e);
078    }
079  }
080
081  @Test
082  public void test() throws Exception {
083    // write some rows to the table
084    for (int i = 0; i < 10; i++) {
085      byte[] row = Bytes.toBytes("row" + i);
086      Put put = new Put(row);
087      put.addColumn(CF, CF, CF);
088      TABLE.put(put);
089    }
090    MasterProcedureEnv env =
091      UTIL.getMiniHBaseCluster().getMaster().getMasterProcedureExecutor().getEnvironment();
092    final ProcedureExecutor<MasterProcedureEnv> executor =
093      UTIL.getMiniHBaseCluster().getMaster().getMasterProcedureExecutor();
094    List<RegionInfo> regionInfos = admin.getRegions(TABLE_NAME);
095    MergeTableRegionsProcedure mergeTableRegionsProcedure = new MergeTableRegionsProcedure(env,
096      new RegionInfo[] { regionInfos.get(0), regionInfos.get(1) }, false);
097    executor.submitProcedure(mergeTableRegionsProcedure);
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    Result result = null;
110    while ((result = results.next()) != null) {
111      count++;
112    }
113    assertEquals(10, count, "There should be 10 rows!");
114  }
115}