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 TestLargeClusterBalancingMetaTableIsolation {
045
046  @ClassRule
047  public static final HBaseClassTestRule CLASS_RULE =
048    HBaseClassTestRule.forClass(TestLargeClusterBalancingMetaTableIsolation.class);
049
050  private static final Logger LOG =
051    LoggerFactory.getLogger(TestLargeClusterBalancingMetaTableIsolation.class);
052
053  private static final TableName NON_META_TABLE_NAME = TableName.valueOf("userTable");
054
055  private static final int NUM_SERVERS = 1000;
056  private static final int NUM_REGIONS = 20_000;
057
058  private static final ServerName[] servers = new ServerName[NUM_SERVERS];
059  private static final Map<ServerName, List<RegionInfo>> serverToRegions = new HashMap<>();
060
061  @BeforeClass
062  public static void setup() {
063    // Initialize servers
064    for (int i = 0; i < NUM_SERVERS; i++) {
065      servers[i] = ServerName.valueOf("server" + i, i, System.currentTimeMillis());
066    }
067
068    // Create regions
069    List<RegionInfo> allRegions = new ArrayList<>();
070    for (int i = 0; i < NUM_REGIONS; i++) {
071      TableName tableName = i < 3 ? TableName.META_TABLE_NAME : NON_META_TABLE_NAME;
072      byte[] startKey = new byte[1];
073      startKey[0] = (byte) i;
074      byte[] endKey = new byte[1];
075      endKey[0] = (byte) (i + 1);
076
077      RegionInfo regionInfo =
078        RegionInfoBuilder.newBuilder(tableName).setStartKey(startKey).setEndKey(endKey).build();
079      allRegions.add(regionInfo);
080    }
081
082    // Assign all regions to the first server
083    serverToRegions.put(servers[0], new ArrayList<>(allRegions));
084    for (int i = 1; i < NUM_SERVERS; i++) {
085      serverToRegions.put(servers[i], new ArrayList<>());
086    }
087  }
088
089  @Test
090  public void testMetaTableIsolation() {
091    Configuration conf = new Configuration(false);
092    conf.setBoolean(BalancerConditionals.ISOLATE_META_TABLE_KEY, true);
093    runBalancerToExhaustion(conf, serverToRegions, Set.of(this::isMetaTableIsolated), 10.0f);
094    LOG.info("Meta table regions are successfully isolated.");
095  }
096
097  private boolean isMetaTableIsolated(BalancerClusterState cluster) {
098    return isTableIsolated(cluster, TableName.META_TABLE_NAME, "Meta");
099  }
100
101}