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.Set;
028import org.apache.hadoop.conf.Configuration;
029import org.apache.hadoop.hbase.HBaseClassTestRule;
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.testclassification.MasterTests;
035import org.apache.hadoop.hbase.testclassification.MediumTests;
036import org.junit.BeforeClass;
037import org.junit.ClassRule;
038import org.junit.Test;
039import org.junit.experimental.categories.Category;
040import org.slf4j.Logger;
041import org.slf4j.LoggerFactory;
042
043@Category({ MediumTests.class, MasterTests.class })
044public class TestLargeClusterBalancingTableIsolationAndReplicaDistribution {
045
046  @ClassRule
047  public static final HBaseClassTestRule CLASS_RULE = HBaseClassTestRule
048    .forClass(TestLargeClusterBalancingTableIsolationAndReplicaDistribution.class);
049
050  private static final Logger LOG =
051    LoggerFactory.getLogger(TestLargeClusterBalancingTableIsolationAndReplicaDistribution.class);
052  private static final TableName SYSTEM_TABLE_NAME = TableName.valueOf("hbase:system");
053  private static final TableName NON_ISOLATED_TABLE_NAME = TableName.valueOf("userTable");
054
055  private static final int NUM_SERVERS = 1000;
056  private static final int NUM_REGIONS = 10_000;
057  private static final int NUM_REPLICAS = 3;
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;
074      if (i < 1) {
075        tableName = TableName.META_TABLE_NAME;
076      } else if (i < 10) {
077        tableName = SYSTEM_TABLE_NAME;
078      } else {
079        tableName = NON_ISOLATED_TABLE_NAME;
080      }
081
082      // Define startKey and endKey for the region
083      byte[] startKey = new byte[1];
084      startKey[0] = (byte) i;
085      byte[] endKey = new byte[1];
086      endKey[0] = (byte) (i + 1);
087
088      // Create 3 replicas for each primary region
089      for (int replicaId = 0; replicaId < NUM_REPLICAS; replicaId++) {
090        RegionInfo regionInfo = RegionInfoBuilder.newBuilder(tableName).setStartKey(startKey)
091          .setEndKey(endKey).setReplicaId(replicaId).build();
092        allRegions.add(regionInfo);
093      }
094    }
095
096    // Assign all regions to one server
097    for (RegionInfo regionInfo : allRegions) {
098      serverToRegions.get(servers[0]).add(regionInfo);
099    }
100  }
101
102  @Test
103  public void testTableIsolationAndReplicaDistribution() {
104
105    Configuration conf = new Configuration(false);
106    conf.setBoolean(BalancerConditionals.ISOLATE_META_TABLE_KEY, true);
107    DistributeReplicasTestConditional.enableConditionalReplicaDistributionForTest(conf);
108
109    runBalancerToExhaustion(conf, serverToRegions,
110      Set.of(this::isMetaTableIsolated, CandidateGeneratorTestUtil::areAllReplicasDistributed),
111      10.0f);
112    LOG.info("Meta table regions are successfully isolated, "
113      + "and region replicas are appropriately distributed.");
114  }
115
116  /**
117   * Validates whether all meta table regions are isolated.
118   */
119  private boolean isMetaTableIsolated(BalancerClusterState cluster) {
120    return isTableIsolated(cluster, TableName.META_TABLE_NAME, "Meta");
121  }
122}