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