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