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;
019
020import static org.junit.jupiter.api.Assertions.assertEquals;
021import static org.junit.jupiter.api.Assertions.assertFalse;
022import static org.junit.jupiter.api.Assertions.assertTrue;
023
024import java.io.IOException;
025import org.apache.hadoop.hbase.HBaseTestingUtil;
026import org.apache.hadoop.hbase.ServerName;
027import org.apache.hadoop.hbase.TableName;
028import org.apache.hadoop.hbase.Waiter;
029import org.apache.hadoop.hbase.client.BalanceRequest;
030import org.apache.hadoop.hbase.client.BalanceResponse;
031import org.apache.hadoop.hbase.client.RegionInfo;
032import org.apache.hadoop.hbase.regionserver.HRegionServer;
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.AfterEach;
037import org.junit.jupiter.api.Tag;
038import org.junit.jupiter.api.Test;
039import org.mockito.Mockito;
040
041@Tag(MasterTests.TAG)
042@Tag(MediumTests.TAG)
043public class TestMasterDryRunBalancer {
044
045  private static final HBaseTestingUtil TEST_UTIL = new HBaseTestingUtil();
046  private static final byte[] FAMILYNAME = Bytes.toBytes("fam");
047
048  @AfterEach
049  public void shutdown() throws Exception {
050    TEST_UTIL.shutdownMiniCluster();
051  }
052
053  @Test
054  public void testDryRunBalancer() throws Exception {
055    TEST_UTIL.startMiniCluster(2);
056
057    int numRegions = 100;
058    int regionsPerRs = numRegions / 2;
059    TableName tableName = createTable("testDryRunBalancer", numRegions);
060    HMaster master = Mockito.spy(TEST_UTIL.getHBaseCluster().getMaster());
061
062    // dry run should be possible with balancer disabled
063    // disabling it will ensure the chore does not mess with our forced unbalance below
064    master.balanceSwitch(false);
065    assertFalse(master.isBalancerOn());
066
067    HRegionServer biasedServer = unbalance(master, tableName);
068
069    BalanceResponse response = master.balance(BalanceRequest.newBuilder().setDryRun(true).build());
070    assertTrue(response.isBalancerRan());
071    // we don't know for sure that it will be exactly half the regions
072    assertTrue(response.getMovesCalculated() >= (regionsPerRs - 1)
073      && response.getMovesCalculated() <= (regionsPerRs + 1));
074    // but we expect no moves executed due to dry run
075    assertEquals(0, response.getMovesExecuted());
076
077    // sanity check that we truly don't try to execute any plans
078    Mockito.verify(master, Mockito.never()).executeRegionPlansWithThrottling(Mockito.anyList());
079
080    // should still be unbalanced post dry run
081    assertServerContainsAllRegions(biasedServer.getServerName(), tableName);
082
083    TEST_UTIL.deleteTable(tableName);
084  }
085
086  private TableName createTable(String table, int numRegions) throws IOException {
087    TableName tableName = TableName.valueOf(table);
088    TEST_UTIL.createMultiRegionTable(tableName, FAMILYNAME, numRegions);
089    return tableName;
090  }
091
092  private HRegionServer unbalance(HMaster master, TableName tableName) throws Exception {
093    waitForRegionsToSettle(master);
094
095    HRegionServer biasedServer = TEST_UTIL.getMiniHBaseCluster().getRegionServer(0);
096
097    for (RegionInfo regionInfo : TEST_UTIL.getAdmin().getRegions(tableName)) {
098      master.move(regionInfo.getEncodedNameAsBytes(),
099        Bytes.toBytes(biasedServer.getServerName().getServerName()));
100    }
101
102    waitForRegionsToSettle(master);
103
104    assertServerContainsAllRegions(biasedServer.getServerName(), tableName);
105
106    return biasedServer;
107  }
108
109  private void assertServerContainsAllRegions(ServerName serverName, TableName tableName)
110    throws IOException {
111    int numRegions = TEST_UTIL.getAdmin().getRegions(tableName).size();
112    assertEquals(numRegions,
113      TEST_UTIL.getMiniHBaseCluster().getRegionServer(serverName).getRegions(tableName).size());
114  }
115
116  private void waitForRegionsToSettle(HMaster master) {
117    Waiter.waitFor(TEST_UTIL.getConfiguration(), 60_000,
118      () -> master.getAssignmentManager().getRegionsInTransitionCount() <= 0);
119  }
120}