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 java.time.Duration;
024import java.time.Instant;
025import java.util.List;
026import org.apache.hadoop.conf.Configuration;
027import org.apache.hadoop.hbase.HBaseClassTestRule;
028import org.apache.hadoop.hbase.HBaseTestingUtility;
029import org.apache.hadoop.hbase.TableName;
030import org.apache.hadoop.hbase.client.RegionInfo;
031import org.apache.hadoop.hbase.master.ServerManager;
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;
041
042/**
043 * Confirm that we will rate limit reopen batches when reopening all table regions. This can avoid
044 * the pain associated with reopening too many regions at once.
045 */
046@Category({ MasterTests.class, MediumTests.class })
047public class TestReopenTableRegionsProcedureBatchBackoff {
048
049  @ClassRule
050  public static final HBaseClassTestRule CLASS_RULE =
051    HBaseClassTestRule.forClass(TestReopenTableRegionsProcedureBatchBackoff.class);
052
053  private static final HBaseTestingUtility UTIL = new HBaseTestingUtility();
054
055  private static TableName TABLE_NAME = TableName.valueOf("BatchBackoff");
056  private static final int BACKOFF_MILLIS_PER_RS = 3_000;
057  private static final int REOPEN_BATCH_SIZE = 1;
058
059  private static byte[] CF = Bytes.toBytes("cf");
060
061  @BeforeClass
062  public static void setUp() throws Exception {
063    Configuration conf = UTIL.getConfiguration();
064    conf.setInt(ServerManager.WAIT_ON_REGIONSERVERS_MINTOSTART, 1);
065    UTIL.startMiniCluster(1);
066    UTIL.createMultiRegionTable(TABLE_NAME, CF, 10);
067  }
068
069  @AfterClass
070  public static void tearDown() throws Exception {
071    UTIL.shutdownMiniCluster();
072  }
073
074  @Test
075  public void testRegionBatchBackoff() throws IOException {
076    ProcedureExecutor<MasterProcedureEnv> procExec =
077      UTIL.getMiniHBaseCluster().getMaster().getMasterProcedureExecutor();
078    List<RegionInfo> regions = UTIL.getAdmin().getRegions(TABLE_NAME);
079    assertTrue(10 <= regions.size());
080    ReopenTableRegionsProcedure proc =
081      new ReopenTableRegionsProcedure(TABLE_NAME, BACKOFF_MILLIS_PER_RS, REOPEN_BATCH_SIZE);
082    procExec.submitProcedure(proc);
083    Instant startedAt = Instant.now();
084    ProcedureSyncWait.waitForProcedureToComplete(procExec, proc, 60_000);
085    Instant stoppedAt = Instant.now();
086    assertTrue(Duration.between(startedAt, stoppedAt).toMillis()
087        > (long) regions.size() * BACKOFF_MILLIS_PER_RS);
088  }
089
090  @Test
091  public void testRegionBatchNoBackoff() throws IOException {
092    ProcedureExecutor<MasterProcedureEnv> procExec =
093      UTIL.getMiniHBaseCluster().getMaster().getMasterProcedureExecutor();
094    List<RegionInfo> regions = UTIL.getAdmin().getRegions(TABLE_NAME);
095    assertTrue(10 <= regions.size());
096    int noBackoffMillis = 0;
097    ReopenTableRegionsProcedure proc =
098      new ReopenTableRegionsProcedure(TABLE_NAME, noBackoffMillis, REOPEN_BATCH_SIZE);
099    procExec.submitProcedure(proc);
100    ProcedureSyncWait.waitForProcedureToComplete(procExec, proc,
101      (long) regions.size() * BACKOFF_MILLIS_PER_RS);
102  }
103}