|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, software |
| 13 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + */ |
| 18 | +package org.apache.hadoop.hbase.master.balancer; |
| 19 | + |
| 20 | +import static org.apache.hadoop.hbase.master.balancer.CandidateGeneratorTestUtil.isTableIsolated; |
| 21 | +import static org.apache.hadoop.hbase.master.balancer.CandidateGeneratorTestUtil.runBalancerToExhaustion; |
| 22 | + |
| 23 | +import java.util.ArrayList; |
| 24 | +import java.util.HashMap; |
| 25 | +import java.util.List; |
| 26 | +import java.util.Map; |
| 27 | +import org.apache.hadoop.conf.Configuration; |
| 28 | +import org.apache.hadoop.hbase.HBaseClassTestRule; |
| 29 | +import org.apache.hadoop.hbase.ServerName; |
| 30 | +import org.apache.hadoop.hbase.TableName; |
| 31 | +import org.apache.hadoop.hbase.client.RegionInfo; |
| 32 | +import org.apache.hadoop.hbase.client.RegionInfoBuilder; |
| 33 | +import org.apache.hadoop.hbase.testclassification.MasterTests; |
| 34 | +import org.apache.hadoop.hbase.testclassification.MediumTests; |
| 35 | +import org.junit.BeforeClass; |
| 36 | +import org.junit.ClassRule; |
| 37 | +import org.junit.Test; |
| 38 | +import org.junit.experimental.categories.Category; |
| 39 | +import org.slf4j.Logger; |
| 40 | +import org.slf4j.LoggerFactory; |
| 41 | + |
| 42 | +import org.apache.hbase.thirdparty.com.google.common.collect.ImmutableSet; |
| 43 | + |
| 44 | +/** |
| 45 | + * If your minCostNeedsBalance is set too low, then the balancer should still eventually stop making |
| 46 | + * moves as further cost improvements become impossible, and balancer plan calculation becomes |
| 47 | + * wasteful. This test ensures that the balancer will not get stuck in a loop of continuously moving |
| 48 | + * regions. |
| 49 | + */ |
| 50 | +@Category({ MasterTests.class, MediumTests.class }) |
| 51 | +public class TestUnattainableBalancerCostGoal { |
| 52 | + |
| 53 | + @ClassRule |
| 54 | + public static final HBaseClassTestRule CLASS_RULE = |
| 55 | + HBaseClassTestRule.forClass(TestUnattainableBalancerCostGoal.class); |
| 56 | + |
| 57 | + private static final Logger LOG = LoggerFactory.getLogger(TestUnattainableBalancerCostGoal.class); |
| 58 | + |
| 59 | + private static final TableName SYSTEM_TABLE_NAME = TableName.valueOf("hbase:system"); |
| 60 | + private static final TableName NON_SYSTEM_TABLE_NAME = TableName.valueOf("userTable"); |
| 61 | + |
| 62 | + private static final int NUM_SERVERS = 10; |
| 63 | + private static final int NUM_REGIONS = 1000; |
| 64 | + private static final float UNACHIEVABLE_COST_GOAL = 0.01f; |
| 65 | + |
| 66 | + private static final ServerName[] servers = new ServerName[NUM_SERVERS]; |
| 67 | + private static final Map<ServerName, List<RegionInfo>> serverToRegions = new HashMap<>(); |
| 68 | + |
| 69 | + @BeforeClass |
| 70 | + public static void setup() { |
| 71 | + // Initialize servers |
| 72 | + for (int i = 0; i < NUM_SERVERS; i++) { |
| 73 | + servers[i] = ServerName.valueOf("server" + i, i, System.currentTimeMillis()); |
| 74 | + } |
| 75 | + |
| 76 | + // Create regions |
| 77 | + List<RegionInfo> allRegions = new ArrayList<>(); |
| 78 | + for (int i = 0; i < NUM_REGIONS; i++) { |
| 79 | + TableName tableName = i < 3 ? SYSTEM_TABLE_NAME : NON_SYSTEM_TABLE_NAME; |
| 80 | + byte[] startKey = new byte[1]; |
| 81 | + startKey[0] = (byte) i; |
| 82 | + byte[] endKey = new byte[1]; |
| 83 | + endKey[0] = (byte) (i + 1); |
| 84 | + |
| 85 | + RegionInfo regionInfo = |
| 86 | + RegionInfoBuilder.newBuilder(tableName).setStartKey(startKey).setEndKey(endKey).build(); |
| 87 | + allRegions.add(regionInfo); |
| 88 | + } |
| 89 | + |
| 90 | + // Assign all regions to the first server |
| 91 | + serverToRegions.put(servers[0], new ArrayList<>(allRegions)); |
| 92 | + for (int i = 1; i < NUM_SERVERS; i++) { |
| 93 | + serverToRegions.put(servers[i], new ArrayList<>()); |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + @Test |
| 98 | + public void testSystemTableIsolation() { |
| 99 | + Configuration conf = new Configuration(false); |
| 100 | + conf.setBoolean(BalancerConditionals.ISOLATE_SYSTEM_TABLES_KEY, true); |
| 101 | + runBalancerToExhaustion(conf, serverToRegions, ImmutableSet.of(this::isSystemTableIsolated), |
| 102 | + UNACHIEVABLE_COST_GOAL, 10_000, CandidateGeneratorTestUtil.ExhaustionType.NO_MORE_MOVES); |
| 103 | + LOG.info("Meta table regions are successfully isolated."); |
| 104 | + } |
| 105 | + |
| 106 | + private boolean isSystemTableIsolated(BalancerClusterState cluster) { |
| 107 | + return isTableIsolated(cluster, SYSTEM_TABLE_NAME, "System"); |
| 108 | + } |
| 109 | +} |
0 commit comments