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