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;
019
020import static org.junit.jupiter.api.Assertions.assertTrue;
021
022import java.io.IOException;
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.RegionInfo;
028import org.apache.hadoop.hbase.coprocessor.CoprocessorHost;
029import org.apache.hadoop.hbase.coprocessor.ObserverContext;
030import org.apache.hadoop.hbase.coprocessor.RegionCoprocessor;
031import org.apache.hadoop.hbase.coprocessor.RegionCoprocessorEnvironment;
032import org.apache.hadoop.hbase.coprocessor.RegionObserver;
033import org.apache.hadoop.hbase.master.assignment.RegionStateNode;
034import org.apache.hadoop.hbase.master.assignment.TransitRegionStateProcedure;
035import org.apache.hadoop.hbase.regionserver.HRegionServer;
036import org.apache.hadoop.hbase.testclassification.MasterTests;
037import org.apache.hadoop.hbase.testclassification.MediumTests;
038import org.apache.hadoop.hbase.util.Bytes;
039import org.apache.hadoop.hbase.util.JVMClusterUtil;
040import org.apache.hadoop.hbase.util.Threads;
041import org.junit.jupiter.api.AfterAll;
042import org.junit.jupiter.api.BeforeAll;
043import org.junit.jupiter.api.Tag;
044import org.junit.jupiter.api.Test;
045import org.slf4j.Logger;
046import org.slf4j.LoggerFactory;
047
048@Tag(MasterTests.TAG)
049@Tag(MediumTests.TAG)
050public class TestMasterAbortAndRSGotKilled {
051  private static Logger LOG =
052    LoggerFactory.getLogger(TestMasterAbortAndRSGotKilled.class.getName());
053
054  private static final HBaseTestingUtil UTIL = new HBaseTestingUtil();
055
056  private static TableName TABLE_NAME = TableName.valueOf("test");
057
058  private static CountDownLatch countDownLatch = new CountDownLatch(1);
059
060  private static byte[] CF = Bytes.toBytes("cf");
061
062  @BeforeAll
063  public static void setUp() throws Exception {
064    UTIL.getConfiguration().setStrings(CoprocessorHost.REGION_COPROCESSOR_CONF_KEY,
065      DelayCloseCP.class.getName());
066    UTIL.startMiniCluster(3);
067    UTIL.getAdmin().balancerSwitch(false, true);
068    UTIL.createTable(TABLE_NAME, CF);
069    UTIL.waitTableAvailable(TABLE_NAME);
070  }
071
072  @AfterAll
073  public static void tearDown() throws Exception {
074    UTIL.shutdownMiniCluster();
075  }
076
077  @Test
078  public void test() throws Exception {
079    JVMClusterUtil.RegionServerThread rsThread = null;
080    for (JVMClusterUtil.RegionServerThread t : UTIL.getMiniHBaseCluster()
081      .getRegionServerThreads()) {
082      if (!t.getRegionServer().getRegions(TABLE_NAME).isEmpty()) {
083        rsThread = t;
084        break;
085      }
086    }
087    // find the rs and hri of the table
088    HRegionServer rs = rsThread.getRegionServer();
089    RegionInfo hri = rs.getRegions(TABLE_NAME).get(0).getRegionInfo();
090    TransitRegionStateProcedure moveRegionProcedure = TransitRegionStateProcedure.reopen(
091      UTIL.getMiniHBaseCluster().getMaster().getMasterProcedureExecutor().getEnvironment(), hri);
092    RegionStateNode regionNode = UTIL.getMiniHBaseCluster().getMaster().getAssignmentManager()
093      .getRegionStates().getOrCreateRegionStateNode(hri);
094    regionNode.setProcedure(moveRegionProcedure);
095    UTIL.getMiniHBaseCluster().getMaster().getMasterProcedureExecutor()
096      .submitProcedure(moveRegionProcedure);
097    countDownLatch.await();
098    UTIL.getMiniHBaseCluster().stopMaster(0);
099    UTIL.getMiniHBaseCluster().startMaster();
100    // wait until master initialized
101    UTIL.waitFor(30000, () -> UTIL.getMiniHBaseCluster().getMaster() != null
102      && UTIL.getMiniHBaseCluster().getMaster().isInitialized());
103    assertTrue(UTIL.getMiniHBaseCluster().getLiveRegionServerThreads().size() == 3,
104      "Should be 3 RS after master restart");
105
106  }
107
108  public static class DelayCloseCP implements RegionCoprocessor, RegionObserver {
109
110    @Override
111    public void preClose(ObserverContext<? extends RegionCoprocessorEnvironment> c,
112      boolean abortRequested) throws IOException {
113      if (!c.getEnvironment().getRegion().getRegionInfo().getTable().isSystemTable()) {
114        LOG.info("begin to sleep");
115        countDownLatch.countDown();
116        // Sleep here so we can stuck the RPC call
117        Threads.sleep(10000);
118        LOG.info("finish sleep");
119      }
120    }
121
122    @Override
123    public Optional<RegionObserver> getRegionObserver() {
124      return Optional.of(this);
125    }
126  }
127}