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