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.procedure;
019
020import java.io.IOException;
021import org.apache.hadoop.hbase.HBaseClassTestRule;
022import org.apache.hadoop.hbase.HBaseTestingUtil;
023import org.apache.hadoop.hbase.TableName;
024import org.apache.hadoop.hbase.client.RegionInfo;
025import org.apache.hadoop.hbase.master.HMaster;
026import org.apache.hadoop.hbase.master.ServerManager;
027import org.apache.hadoop.hbase.master.assignment.AssignmentManager;
028import org.apache.hadoop.hbase.master.assignment.RegionStateNode;
029import org.apache.hadoop.hbase.master.assignment.TransitRegionStateProcedure;
030import org.apache.hadoop.hbase.procedure2.ProcedureExecutor;
031import org.apache.hadoop.hbase.testclassification.MasterTests;
032import org.apache.hadoop.hbase.testclassification.MediumTests;
033import org.apache.hadoop.hbase.util.Bytes;
034import org.junit.AfterClass;
035import org.junit.BeforeClass;
036import org.junit.ClassRule;
037import org.junit.Test;
038import org.junit.experimental.categories.Category;
039
040/**
041 * Testcase for HBASE-21330.
042 */
043@Category({ MasterTests.class, MediumTests.class })
044public class TestReopenTableRegionsProcedureInfiniteLoop {
045
046  @ClassRule
047  public static final HBaseClassTestRule CLASS_RULE =
048    HBaseClassTestRule.forClass(TestReopenTableRegionsProcedureInfiniteLoop.class);
049
050  private static final HBaseTestingUtil UTIL = new HBaseTestingUtil();
051
052  private static TableName TABLE_NAME = TableName.valueOf("InfiniteLoop");
053
054  private static byte[] CF = Bytes.toBytes("cf");
055
056  @BeforeClass
057  public static void setUp() throws Exception {
058    UTIL.getConfiguration().setInt(ServerManager.WAIT_ON_REGIONSERVERS_MINTOSTART, 1);
059    UTIL.startMiniCluster(1);
060    UTIL.createTable(TABLE_NAME, CF);
061  }
062
063  @AfterClass
064  public static void tearDown() throws Exception {
065    UTIL.shutdownMiniCluster();
066  }
067
068  @Test
069  public void testInfiniteLoop() throws IOException {
070    HMaster master = UTIL.getMiniHBaseCluster().getMaster();
071    AssignmentManager am = master.getAssignmentManager();
072    ProcedureExecutor<MasterProcedureEnv> exec = master.getMasterProcedureExecutor();
073    RegionInfo regionInfo = UTIL.getAdmin().getRegions(TABLE_NAME).get(0);
074    RegionStateNode regionNode = am.getRegionStates().getRegionStateNode(regionInfo);
075    long procId;
076    ReopenTableRegionsProcedure proc = new ReopenTableRegionsProcedure(TABLE_NAME);
077    regionNode.lock();
078    try {
079      procId = exec.submitProcedure(proc);
080      UTIL.waitFor(30000, () -> proc.hasLock());
081      TransitRegionStateProcedure trsp =
082        TransitRegionStateProcedure.reopen(exec.getEnvironment(), regionInfo);
083      regionNode.setProcedure(trsp);
084      exec.submitProcedure(trsp);
085    } finally {
086      regionNode.unlock();
087    }
088    UTIL.waitFor(60000, () -> exec.isFinished(procId));
089  }
090}