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