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.assertTrue;
021
022import java.util.List;
023import java.util.Optional;
024import java.util.concurrent.CountDownLatch;
025import org.apache.hadoop.hbase.HBaseTestingUtil;
026import org.apache.hadoop.hbase.TableName;
027import org.apache.hadoop.hbase.client.Admin;
028import org.apache.hadoop.hbase.client.Mutation;
029import org.apache.hadoop.hbase.client.RegionInfo;
030import org.apache.hadoop.hbase.coprocessor.CoprocessorHost;
031import org.apache.hadoop.hbase.coprocessor.MasterCoprocessor;
032import org.apache.hadoop.hbase.coprocessor.MasterCoprocessorEnvironment;
033import org.apache.hadoop.hbase.coprocessor.MasterObserver;
034import org.apache.hadoop.hbase.coprocessor.ObserverContext;
035import org.apache.hadoop.hbase.testclassification.MasterTests;
036import org.apache.hadoop.hbase.testclassification.MediumTests;
037import org.apache.hadoop.hbase.util.Bytes;
038import org.junit.jupiter.api.AfterAll;
039import org.junit.jupiter.api.BeforeAll;
040import org.junit.jupiter.api.Tag;
041import org.junit.jupiter.api.Test;
042import org.slf4j.Logger;
043import org.slf4j.LoggerFactory;
044
045@Tag(MasterTests.TAG)
046@Tag(MediumTests.TAG)
047public class TestMasterAbortWhileMergingTable {
048
049  private static final Logger LOG = LoggerFactory.getLogger(TestMasterAbortWhileMergingTable.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 byte[] CF = Bytes.toBytes("cf");
055  private static byte[] SPLITKEY = Bytes.toBytes("bbbbbbb");
056  private static CountDownLatch mergeCommitArrive = new CountDownLatch(1);
057
058  @BeforeAll
059  public static void setupCluster() throws Exception {
060    UTIL.getConfiguration().set(CoprocessorHost.MASTER_COPROCESSOR_CONF_KEY,
061      MergeRegionObserver.class.getName());
062    UTIL.startMiniCluster(3);
063    admin = UTIL.getAdmin();
064    byte[][] splitKeys = new byte[1][];
065    splitKeys[0] = SPLITKEY;
066    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    List<RegionInfo> regionInfos = admin.getRegions(TABLE_NAME);
082    MergeTableRegionsProcedure mergeTableRegionsProcedure = new MergeTableRegionsProcedure(
083      UTIL.getMiniHBaseCluster().getMaster().getMasterProcedureExecutor().getEnvironment(),
084      new RegionInfo[] { regionInfos.get(0), regionInfos.get(1) }, false);
085    long procID = UTIL.getMiniHBaseCluster().getMaster().getMasterProcedureExecutor()
086      .submitProcedure(mergeTableRegionsProcedure);
087    mergeCommitArrive.await();
088    UTIL.getMiniHBaseCluster().stopMaster(0);
089    UTIL.getMiniHBaseCluster().startMaster();
090    // wait until master initialized
091    UTIL.waitFor(30000, () -> UTIL.getMiniHBaseCluster().getMaster() != null
092      && UTIL.getMiniHBaseCluster().getMaster().isInitialized());
093    UTIL.waitFor(30000,
094      () -> UTIL.getMiniHBaseCluster().getMaster().getMasterProcedureExecutor().isFinished(procID));
095    assertTrue(
096      UTIL.getMiniHBaseCluster().getMaster().getAssignmentManager().getRegionsInTransition().size()
097          == 0,
098      "Found region RIT, that's impossible! "
099        + UTIL.getMiniHBaseCluster().getMaster().getAssignmentManager().getRegionsInTransition());
100  }
101
102  public static class MergeRegionObserver implements MasterCoprocessor, MasterObserver {
103
104    @Override
105    public Optional<MasterObserver> getMasterObserver() {
106      return Optional.of(this);
107    }
108
109    @Override
110    public void preMergeRegionsCommitAction(ObserverContext<MasterCoprocessorEnvironment> ctx,
111      RegionInfo[] regionsToMerge, List<Mutation> metaEntries) {
112      mergeCommitArrive.countDown();
113      LOG.error("mergeCommitArrive countdown");
114    }
115  }
116
117}