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