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