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