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