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.isTableIsolated;
021import static org.apache.hadoop.hbase.master.balancer.CandidateGeneratorTestUtil.runBalancerToExhaustion;
022
023import java.util.ArrayList;
024import java.util.HashMap;
025import java.util.List;
026import java.util.Map;
027import java.util.Random;
028import java.util.Set;
029import org.apache.hadoop.conf.Configuration;
030import org.apache.hadoop.hbase.ServerName;
031import org.apache.hadoop.hbase.TableName;
032import org.apache.hadoop.hbase.client.RegionInfo;
033import org.apache.hadoop.hbase.client.RegionInfoBuilder;
034import org.apache.hadoop.hbase.master.balancer.BalancerTestBase.MockMapping;
035import org.apache.hadoop.hbase.testclassification.LargeTests;
036import org.apache.hadoop.hbase.testclassification.MasterTests;
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(LargeTests.TAG)
045@Tag(MasterTests.TAG)
046public class TestLargeClusterBalancingTableIsolationAndReplicaDistribution {
047
048  private static final Logger LOG =
049    LoggerFactory.getLogger(TestLargeClusterBalancingTableIsolationAndReplicaDistribution.class);
050  private static final TableName SYSTEM_TABLE_NAME = TableName.valueOf("hbase:system");
051  private static final TableName NON_ISOLATED_TABLE_NAME = TableName.valueOf("userTable");
052
053  private static final int NUM_SERVERS = 500;
054  private static final int NUM_REGIONS = 2_500;
055  private static final int NUM_REPLICAS = 3;
056
057  private static final ServerName[] servers = new ServerName[NUM_SERVERS];
058  private static final Map<ServerName, List<RegionInfo>> serverToRegions = new HashMap<>();
059
060  @BeforeAll
061  public static void setup() {
062    // Initialize servers
063    for (int i = 0; i < NUM_SERVERS; i++) {
064      servers[i] = ServerName.valueOf("server" + i, i, System.currentTimeMillis());
065      serverToRegions.put(servers[i], new ArrayList<>());
066    }
067
068    // Create primary regions and their replicas
069    for (int i = 0; i < NUM_REGIONS; i++) {
070      TableName tableName;
071      if (i < 1) {
072        tableName = TableName.META_TABLE_NAME;
073      } else if (i < 10) {
074        tableName = SYSTEM_TABLE_NAME;
075      } else {
076        tableName = NON_ISOLATED_TABLE_NAME;
077      }
078
079      // Define startKey and endKey for the region
080      byte[] startKey = new byte[1];
081      startKey[0] = (byte) i;
082      byte[] endKey = new byte[1];
083      endKey[0] = (byte) (i + 1);
084
085      Random random = new Random();
086      // Create 3 replicas for each primary region
087      for (int replicaId = 0; replicaId < NUM_REPLICAS; replicaId++) {
088        RegionInfo regionInfo = RegionInfoBuilder.newBuilder(tableName).setStartKey(startKey)
089          .setEndKey(endKey).setReplicaId(replicaId).build();
090        // Assign region to random server
091        int randomServer = random.nextInt(servers.length);
092        serverToRegions.get(servers[randomServer]).add(regionInfo);
093      }
094    }
095  }
096
097  @Test
098  public void testTableIsolationAndReplicaDistribution() {
099    Configuration conf = new Configuration(false);
100    conf.setClass("hbase.util.ip.to.rack.determiner", MockMapping.class, DNSToSwitchMapping.class);
101    conf.setBoolean(BalancerConditionals.ISOLATE_META_TABLE_KEY, true);
102    conf.setBoolean(BalancerConditionals.ISOLATE_SYSTEM_TABLES_KEY, true);
103    DistributeReplicasTestConditional.enableConditionalReplicaDistributionForTest(conf);
104
105    runBalancerToExhaustion(conf, serverToRegions,
106      Set.of(this::isMetaTableIsolated, this::isSystemTableIsolated,
107        CandidateGeneratorTestUtil::areAllReplicasDistributed),
108      10.0f, 60_000, CandidateGeneratorTestUtil.ExhaustionType.COST_GOAL_ACHIEVED);
109    LOG.info("Meta table regions are successfully isolated, "
110      + "and region replicas are appropriately distributed.");
111  }
112
113  /**
114   * Validates whether all meta table regions are isolated.
115   */
116  private boolean isMetaTableIsolated(BalancerClusterState cluster) {
117    return isTableIsolated(cluster, TableName.META_TABLE_NAME, "Meta");
118  }
119
120  /**
121   * Validates whether all meta table regions are isolated.
122   */
123  private boolean isSystemTableIsolated(BalancerClusterState cluster) {
124    return isTableIsolated(cluster, SYSTEM_TABLE_NAME, "System");
125  }
126}