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.assignment;
019
020import java.util.List;
021
022import org.apache.hadoop.hbase.HBaseClassTestRule;
023import org.apache.hadoop.hbase.HBaseTestingUtil;
024import org.apache.hadoop.hbase.TableName;
025import org.apache.hadoop.hbase.client.Admin;
026import org.apache.hadoop.hbase.client.RegionInfo;
027import org.apache.hadoop.hbase.client.Table;
028import org.apache.hadoop.hbase.client.TableDescriptor;
029import org.apache.hadoop.hbase.master.procedure.MasterProcedureConstants;
030import org.apache.hadoop.hbase.master.procedure.MasterProcedureEnv;
031import org.apache.hadoop.hbase.master.procedure.ModifyTableProcedure;
032import org.apache.hadoop.hbase.procedure2.ProcedureExecutor;
033import org.apache.hadoop.hbase.testclassification.MasterTests;
034import org.apache.hadoop.hbase.testclassification.MediumTests;
035import org.apache.hadoop.hbase.util.Bytes;
036import org.junit.AfterClass;
037import org.junit.Assert;
038import org.junit.BeforeClass;
039import org.junit.ClassRule;
040import org.junit.Test;
041import org.junit.experimental.categories.Category;
042import org.slf4j.Logger;
043import org.slf4j.LoggerFactory;
044
045import org.apache.hadoop.hbase.shaded.protobuf.generated.ProcedureProtos;
046
047
048@Category({MasterTests.class, MediumTests.class})
049public class TestModifyTableWhileMerging {
050
051  @ClassRule
052  public static final HBaseClassTestRule CLASS_RULE =
053      HBaseClassTestRule.forClass(TestModifyTableWhileMerging.class);
054
055  private static final Logger LOG = LoggerFactory
056      .getLogger(TestModifyTableWhileMerging.class);
057
058  protected static final HBaseTestingUtil UTIL = new HBaseTestingUtil();
059  private static TableName TABLE_NAME = TableName.valueOf("test");
060  private static Admin admin;
061  private static Table client;
062  private static byte[] CF = Bytes.toBytes("cf");
063  private static byte[] SPLITKEY = Bytes.toBytes("bbbbbbb");
064
065  @BeforeClass
066  public static void setupCluster() throws Exception {
067    //Set procedure executor thread to 1, making reproducing this issue of HBASE-20921 easier
068    UTIL.getConfiguration().setInt(MasterProcedureConstants.MASTER_PROCEDURE_THREADS, 1);
069    UTIL.startMiniCluster(1);
070    admin = UTIL.getAdmin();
071    byte[][] splitKeys = new byte[1][];
072    splitKeys[0] = SPLITKEY;
073    client = UTIL.createTable(TABLE_NAME, CF, splitKeys);
074    UTIL.waitTableAvailable(TABLE_NAME);
075  }
076
077  @AfterClass
078  public static void cleanupTest() throws Exception {
079    try {
080      UTIL.shutdownMiniCluster();
081    } catch (Exception e) {
082      LOG.warn("failure shutting down cluster", e);
083    }
084  }
085
086  @Test
087  public void test() throws Exception {
088    TableDescriptor tableDescriptor = client.getDescriptor();
089    ProcedureExecutor<MasterProcedureEnv> executor = UTIL.getMiniHBaseCluster().getMaster()
090        .getMasterProcedureExecutor();
091    MasterProcedureEnv env = executor.getEnvironment();
092    List<RegionInfo> regionInfos = admin.getRegions(TABLE_NAME);
093    MergeTableRegionsProcedure mergeTableRegionsProcedure = new MergeTableRegionsProcedure(
094      UTIL.getMiniHBaseCluster().getMaster().getMasterProcedureExecutor()
095        .getEnvironment(), new RegionInfo [] {regionInfos.get(0), regionInfos.get(1)}, false);
096    ModifyTableProcedure modifyTableProcedure = new ModifyTableProcedure(env, tableDescriptor);
097    long procModify = executor.submitProcedure(modifyTableProcedure);
098    UTIL.waitFor(30000, () -> executor.getProcedures().stream()
099      .filter(p -> p instanceof ModifyTableProcedure)
100      .map(p -> (ModifyTableProcedure) p)
101      .anyMatch(p -> TABLE_NAME.equals(p.getTableName())));
102    long proc = executor.submitProcedure(mergeTableRegionsProcedure);
103    UTIL.waitFor(3000000, () -> UTIL.getMiniHBaseCluster().getMaster()
104        .getMasterProcedureExecutor().isFinished(procModify));
105    Assert.assertEquals("Modify Table procedure should success!",
106        ProcedureProtos.ProcedureState.SUCCESS, modifyTableProcedure.getState());
107  }
108
109}