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 java.util.concurrent.CompletableFuture;
021import java.util.concurrent.TimeUnit;
022import org.apache.hadoop.hbase.HBaseClassTestRule;
023import org.apache.hadoop.hbase.HBaseTestingUtil;
024import org.apache.hadoop.hbase.TableName;
025import org.apache.hadoop.hbase.client.AsyncAdmin;
026import org.apache.hadoop.hbase.client.AsyncConnection;
027import org.apache.hadoop.hbase.client.ConnectionFactory;
028import org.apache.hadoop.hbase.client.RegionInfo;
029import org.apache.hadoop.hbase.master.assignment.TransitRegionStateProcedure;
030import org.apache.hadoop.hbase.master.procedure.MasterProcedureEnv;
031import org.apache.hadoop.hbase.procedure2.ProcedureExecutor;
032import org.apache.hadoop.hbase.regionserver.HRegionServer;
033import org.apache.hadoop.hbase.testclassification.MasterTests;
034import org.apache.hadoop.hbase.testclassification.MediumTests;
035import org.apache.hadoop.hbase.util.Bytes;
036import org.apache.hadoop.hbase.util.JVMClusterUtil.RegionServerThread;
037import org.junit.AfterClass;
038import org.junit.BeforeClass;
039import org.junit.ClassRule;
040import org.junit.Test;
041import org.junit.experimental.categories.Category;
042
043/**
044 * Testcase for HBASE-20634
045 */
046@Category({ MasterTests.class, MediumTests.class })
047public class TestServerCrashProcedureStuck {
048
049  @ClassRule
050  public static final HBaseClassTestRule CLASS_RULE =
051    HBaseClassTestRule.forClass(TestServerCrashProcedureStuck.class);
052
053  private static final HBaseTestingUtil UTIL = new HBaseTestingUtil();
054
055  private static TableName TABLE_NAME = TableName.valueOf("test");
056
057  private static byte[] CF = Bytes.toBytes("cf");
058
059  @BeforeClass
060  public static void setUp() throws Exception {
061    UTIL.startMiniCluster(3);
062    UTIL.getAdmin().balancerSwitch(false, true);
063    UTIL.createTable(TABLE_NAME, CF);
064    UTIL.waitTableAvailable(TABLE_NAME);
065  }
066
067  @AfterClass
068  public static void tearDown() throws Exception {
069    UTIL.shutdownMiniCluster();
070  }
071
072  @Test
073  public void test() throws Exception {
074    RegionServerThread rsThread = null;
075    for (RegionServerThread t : UTIL.getMiniHBaseCluster().getRegionServerThreads()) {
076      if (!t.getRegionServer().getRegions(TABLE_NAME).isEmpty()) {
077        rsThread = t;
078        break;
079      }
080    }
081    HRegionServer rs = rsThread.getRegionServer();
082    RegionInfo hri = rs.getRegions(TABLE_NAME).get(0).getRegionInfo();
083    HMaster master = UTIL.getMiniHBaseCluster().getMaster();
084    ProcedureExecutor<MasterProcedureEnv> executor = master.getMasterProcedureExecutor();
085    DummyRegionProcedure proc = new DummyRegionProcedure(executor.getEnvironment(), hri);
086    long procId = master.getMasterProcedureExecutor().submitProcedure(proc);
087    proc.waitUntilArrive();
088    try (AsyncConnection conn =
089      ConnectionFactory.createAsyncConnection(UTIL.getConfiguration()).get()) {
090      AsyncAdmin admin = conn.getAdmin();
091      CompletableFuture<Void> future = admin.move(hri.getRegionName());
092      rs.abort("For testing!");
093
094      UTIL.waitFor(30000,
095        () -> executor.getProcedures().stream()
096          .filter(p -> p instanceof TransitRegionStateProcedure)
097          .map(p -> (TransitRegionStateProcedure) p)
098          .anyMatch(p -> Bytes.equals(hri.getRegionName(), p.getRegion().getRegionName())));
099      proc.resume();
100      UTIL.waitFor(30000, () -> executor.isFinished(procId));
101      // see whether the move region procedure can finish properly
102      future.get(30, TimeUnit.SECONDS);
103    }
104  }
105}