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 static org.junit.Assert.assertTrue;
021
022import java.io.IOException;
023import org.apache.hadoop.hbase.HBaseClassTestRule;
024import org.apache.hadoop.hbase.HBaseTestingUtil;
025import org.apache.hadoop.hbase.TableName;
026import org.apache.hadoop.hbase.client.RegionInfo;
027import org.apache.hadoop.hbase.master.RegionState.State;
028import org.apache.hadoop.hbase.master.ServerManager;
029import org.apache.hadoop.hbase.master.assignment.AssignmentManager;
030import org.apache.hadoop.hbase.master.assignment.RegionStateNode;
031import org.apache.hadoop.hbase.master.assignment.TransitRegionStateProcedure;
032import org.apache.hadoop.hbase.procedure2.ProcedureExecutor;
033import org.apache.hadoop.hbase.testclassification.MasterTests;
034import org.apache.hadoop.hbase.testclassification.MediumTests;
035import org.apache.hadoop.hbase.util.Bytes;
036import org.junit.AfterClass;
037import org.junit.BeforeClass;
038import org.junit.ClassRule;
039import org.junit.Test;
040import org.junit.experimental.categories.Category;
041import org.slf4j.Logger;
042import org.slf4j.LoggerFactory;
043
044import org.apache.hadoop.hbase.shaded.protobuf.generated.ProcedureProtos.ProcedureState;
045
046/**
047 * Confirm that we will do backoff when retrying on reopening table regions, to avoid consuming all
048 * the CPUs.
049 */
050@Category({ MasterTests.class, MediumTests.class })
051public class TestReopenTableRegionsProcedureBackoff {
052
053  @ClassRule
054  public static final HBaseClassTestRule CLASS_RULE =
055    HBaseClassTestRule.forClass(TestReopenTableRegionsProcedureBackoff.class);
056
057  private static final Logger LOG =
058    LoggerFactory.getLogger(TestReopenTableRegionsProcedureBackoff.class);
059
060  private static final HBaseTestingUtil UTIL = new HBaseTestingUtil();
061
062  private static TableName TABLE_NAME = TableName.valueOf("Backoff");
063
064  private static byte[] CF = Bytes.toBytes("cf");
065
066  @BeforeClass
067  public static void setUp() throws Exception {
068    UTIL.getConfiguration().setInt(ServerManager.WAIT_ON_REGIONSERVERS_MINTOSTART, 1);
069    UTIL.startMiniCluster(1);
070    UTIL.createTable(TABLE_NAME, CF);
071  }
072
073  @AfterClass
074  public static void tearDown() throws Exception {
075    UTIL.shutdownMiniCluster();
076  }
077
078  @Test
079  public void testRetryBackoff() throws IOException, InterruptedException {
080    AssignmentManager am = UTIL.getMiniHBaseCluster().getMaster().getAssignmentManager();
081    ProcedureExecutor<MasterProcedureEnv> procExec =
082      UTIL.getMiniHBaseCluster().getMaster().getMasterProcedureExecutor();
083    RegionInfo regionInfo = UTIL.getAdmin().getRegions(TABLE_NAME).get(0);
084    RegionStateNode regionNode = am.getRegionStates().getRegionStateNode(regionInfo);
085    // just a dummy one
086    TransitRegionStateProcedure trsp =
087      TransitRegionStateProcedure.unassign(procExec.getEnvironment(), regionInfo);
088    long openSeqNum;
089    regionNode.lock();
090    try {
091      openSeqNum = regionNode.getOpenSeqNum();
092      // make a fake state to let the procedure wait.
093      regionNode.setState(State.OPENING);
094      regionNode.setOpenSeqNum(-1L);
095      regionNode.setProcedure(trsp);
096    } finally {
097      regionNode.unlock();
098    }
099    ReopenTableRegionsProcedure proc = new ReopenTableRegionsProcedure(TABLE_NAME);
100    procExec.submitProcedure(proc);
101    UTIL.waitFor(10000, () -> proc.getState() == ProcedureState.WAITING_TIMEOUT);
102    long oldTimeout = 0;
103    int timeoutIncrements = 0;
104    for (;;) {
105      long timeout = proc.getTimeout();
106      if (timeout > oldTimeout) {
107        LOG.info("Timeout incremented, was {}, now is {}, increments={}", timeout, oldTimeout,
108          timeoutIncrements);
109        oldTimeout = timeout;
110        timeoutIncrements++;
111        if (timeoutIncrements > 3) {
112          // If we incremented at least twice, break; the backoff is working.
113          break;
114        }
115      }
116      Thread.sleep(1000);
117    }
118    regionNode.lock();
119    try {
120      // reset to the correct state
121      regionNode.setState(State.OPEN);
122      regionNode.setOpenSeqNum(openSeqNum);
123      regionNode.unsetProcedure(trsp);
124    } finally {
125      regionNode.unlock();
126    }
127    ProcedureSyncWait.waitForProcedureToComplete(procExec, proc, 60000);
128    assertTrue(regionNode.getOpenSeqNum() > openSeqNum);
129  }
130}