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.HBaseTestingUtility;
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@Category({ MasterTests.class, MediumTests.class })
044public class TestServerCrashProcedureCarryingMetaStuck {
045
046  @ClassRule
047  public static final HBaseClassTestRule CLASS_RULE =
048    HBaseClassTestRule.forClass(TestServerCrashProcedureCarryingMetaStuck.class);
049
050  private static final HBaseTestingUtility UTIL = new HBaseTestingUtility();
051
052  @BeforeClass
053  public static void setUp() throws Exception {
054    UTIL.startMiniCluster(3);
055    UTIL.getAdmin().balancerSwitch(false, true);
056  }
057
058  @AfterClass
059  public static void tearDown() throws Exception {
060    UTIL.shutdownMiniCluster();
061  }
062
063  @Test
064  public void test() throws Exception {
065    RegionServerThread rsThread = null;
066    for (RegionServerThread t : UTIL.getMiniHBaseCluster().getRegionServerThreads()) {
067      if (!t.getRegionServer().getRegions(TableName.META_TABLE_NAME).isEmpty()) {
068        rsThread = t;
069        break;
070      }
071    }
072    HRegionServer rs = rsThread.getRegionServer();
073    RegionInfo hri = rs.getRegions(TableName.META_TABLE_NAME).get(0).getRegionInfo();
074    HMaster master = UTIL.getMiniHBaseCluster().getMaster();
075    ProcedureExecutor<MasterProcedureEnv> executor = master.getMasterProcedureExecutor();
076    DummyRegionProcedure proc = new DummyRegionProcedure(executor.getEnvironment(), hri);
077    long procId = master.getMasterProcedureExecutor().submitProcedure(proc);
078    proc.waitUntilArrive();
079    try (AsyncConnection conn =
080      ConnectionFactory.createAsyncConnection(UTIL.getConfiguration()).get()) {
081      AsyncAdmin admin = conn.getAdmin();
082      CompletableFuture<Void> future = admin.move(hri.getRegionName());
083      rs.abort("For testing!");
084
085      UTIL.waitFor(30000,
086        () -> executor.getProcedures().stream()
087          .filter(p -> p instanceof TransitRegionStateProcedure)
088          .map(p -> (TransitRegionStateProcedure) p)
089          .anyMatch(p -> Bytes.equals(hri.getRegionName(), p.getRegion().getRegionName())));
090      proc.resume();
091      UTIL.waitFor(30000, () -> executor.isFinished(procId));
092      // see whether the move region procedure can finish properly
093      future.get(30, TimeUnit.SECONDS);
094    }
095  }
096}