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