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 static org.apache.hadoop.hbase.master.balancer.CandidateGeneratorTestUtil.runBalancerToExhaustion;
021
022import java.util.ArrayList;
023import java.util.HashMap;
024import java.util.List;
025import java.util.Map;
026import java.util.Set;
027import org.apache.hadoop.conf.Configuration;
028import org.apache.hadoop.hbase.ServerName;
029import org.apache.hadoop.hbase.TableName;
030import org.apache.hadoop.hbase.client.RegionInfo;
031import org.apache.hadoop.hbase.client.RegionInfoBuilder;
032import org.apache.hadoop.hbase.master.balancer.BalancerTestBase.MockMapping;
033import org.apache.hadoop.hbase.master.balancer.replicas.ReplicaKeyCache;
034import org.apache.hadoop.hbase.testclassification.MasterTests;
035import org.apache.hadoop.hbase.testclassification.MediumTests;
036import org.apache.hadoop.hbase.util.Bytes;
037import org.apache.hadoop.net.DNSToSwitchMapping;
038import org.junit.jupiter.api.BeforeAll;
039import org.junit.jupiter.api.Tag;
040import org.junit.jupiter.api.Test;
041import org.slf4j.Logger;
042import org.slf4j.LoggerFactory;
043
044@Tag(MediumTests.TAG)
045@Tag(MasterTests.TAG)
046public class TestLargeClusterBalancingConditionalReplicaDistribution {
047
048  private static final Logger LOG =
049    LoggerFactory.getLogger(TestLargeClusterBalancingConditionalReplicaDistribution.class);
050
051  private static final int NUM_SERVERS = 1000;
052  private static final int NUM_REGIONS = 20_000;
053  private static final int NUM_REPLICAS = 3;
054  private static final int NUM_TABLES = 100;
055
056  private static final ServerName[] servers = new ServerName[NUM_SERVERS];
057  private static final Map<ServerName, List<RegionInfo>> serverToRegions = new HashMap<>();
058
059  @BeforeAll
060  public static void setup() {
061    // Initialize servers
062    for (int i = 0; i < NUM_SERVERS; i++) {
063      servers[i] = ServerName.valueOf("server" + i, i, System.currentTimeMillis());
064      serverToRegions.put(servers[i], new ArrayList<>());
065    }
066
067    // Create primary regions and their replicas
068    List<RegionInfo> allRegions = new ArrayList<>();
069    for (int i = 0; i < NUM_REGIONS; i++) {
070      TableName tableName = getTableName(i);
071      // Define startKey and endKey for the region
072      byte[] startKey = Bytes.toBytes(i);
073      byte[] endKey = Bytes.toBytes(i + 1);
074
075      // Create 3 replicas for each primary region
076      for (int replicaId = 0; replicaId < NUM_REPLICAS; replicaId++) {
077        RegionInfo regionInfo = RegionInfoBuilder.newBuilder(tableName).setStartKey(startKey)
078          .setEndKey(endKey).setReplicaId(replicaId).build();
079        allRegions.add(regionInfo);
080      }
081    }
082
083    // Assign all regions to one server
084    for (RegionInfo regionInfo : allRegions) {
085      serverToRegions.get(servers[0]).add(regionInfo);
086    }
087  }
088
089  private static TableName getTableName(int i) {
090    return TableName.valueOf("userTable" + i % NUM_TABLES);
091  }
092
093  @Test
094  public void testReplicaDistribution() {
095    Configuration conf = new Configuration();
096    conf.setClass("hbase.util.ip.to.rack.determiner", MockMapping.class, DNSToSwitchMapping.class);
097    DistributeReplicasTestConditional.enableConditionalReplicaDistributionForTest(conf);
098    conf.setBoolean(ReplicaKeyCache.CACHE_REPLICA_KEYS_KEY, true);
099    conf.setInt(ReplicaKeyCache.REPLICA_KEY_CACHE_SIZE_KEY, Integer.MAX_VALUE);
100    conf.setLong("hbase.master.balancer.stochastic.maxRunningTime", 30_000);
101
102    // turn off replica cost functions
103    conf.setLong("hbase.master.balancer.stochastic.regionReplicaRackCostKey", 0);
104    conf.setLong("hbase.master.balancer.stochastic.regionReplicaHostCostKey", 0);
105
106    runBalancerToExhaustion(conf, serverToRegions,
107      Set.of(CandidateGeneratorTestUtil::areAllReplicasDistributed), 10.0f);
108    LOG.info("Region replicas are appropriately distributed across RegionServers.");
109  }
110}