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.balancer;
019
020import java.time.Duration;
021import java.util.HashMap;
022import java.util.List;
023import java.util.Map;
024import org.apache.hadoop.hbase.HBaseClassTestRule;
025import org.apache.hadoop.hbase.ServerName;
026import org.apache.hadoop.hbase.client.RegionInfo;
027import org.apache.hadoop.hbase.master.RackManager;
028import org.apache.hadoop.hbase.testclassification.LargeTests;
029import org.apache.hadoop.hbase.testclassification.MasterTests;
030import org.junit.ClassRule;
031import org.junit.Test;
032import org.junit.experimental.categories.Category;
033
034@Category({ MasterTests.class, LargeTests.class })
035public class TestStochasticLoadBalancerRegionReplicaWithRacks extends StochasticBalancerTestBase {
036
037  @ClassRule
038  public static final HBaseClassTestRule CLASS_RULE =
039    HBaseClassTestRule.forClass(TestStochasticLoadBalancerRegionReplicaWithRacks.class);
040
041  private static class ForTestRackManager extends RackManager {
042
043    int numRacks;
044    Map<String, Integer> serverIndexes = new HashMap<String, Integer>();
045    int numServers = 0;
046
047    public ForTestRackManager(int numRacks) {
048      this.numRacks = numRacks;
049    }
050
051    @Override
052    public String getRack(ServerName server) {
053      String key = server.getServerName();
054      if (!serverIndexes.containsKey(key)) {
055        serverIndexes.put(key, numServers++);
056      }
057      return "rack_" + serverIndexes.get(key) % numRacks;
058    }
059  }
060
061  @Test
062  public void testRegionReplicationOnMidClusterWithRacks() {
063    conf.setLong(StochasticLoadBalancer.MAX_STEPS_KEY, 100000000L);
064    conf.setBoolean("hbase.master.balancer.stochastic.runMaxSteps", true);
065    setMaxRunTime(Duration.ofSeconds(5));
066    loadBalancer.onConfigurationChange(conf);
067    int numNodes = 5;
068    int numRegions = numNodes * 1;
069    int replication = 3; // 3 replicas per region
070    int numRegionsPerServer = 1;
071    int numTables = 1;
072    int numRacks = 3; // all replicas should be on a different rack
073    Map<ServerName, List<RegionInfo>> serverMap =
074      createServerMap(numNodes, numRegions, numRegionsPerServer, replication, numTables);
075    RackManager rm = new ForTestRackManager(numRacks);
076    testWithClusterWithIteration(serverMap, rm, true, true);
077  }
078
079  @Test
080  public void testRegionReplicationOnLargeClusterWithRacks() {
081    conf.setBoolean("hbase.master.balancer.stochastic.runMaxSteps", true);
082    conf.setLong(StochasticLoadBalancer.MAX_STEPS_KEY, 100000000L);
083    setMaxRunTime(Duration.ofSeconds(5));
084    loadBalancer.onConfigurationChange(conf);
085    int numNodes = 100;
086    int numRegions = numNodes * 30;
087    int replication = 3; // 3 replicas per region
088    int numRegionsPerServer = 28;
089    int numTables = 1;
090    int numRacks = 4; // all replicas should be on a different rack
091    Map<ServerName, List<RegionInfo>> serverMap =
092      createServerMap(numNodes, numRegions, numRegionsPerServer, replication, numTables);
093    RackManager rm = new ForTestRackManager(numRacks);
094
095    testWithClusterWithIteration(serverMap, rm, true, true);
096  }
097}