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/** 044 * If your minCostNeedsBalance is set too low, then the balancer should still eventually stop making 045 * moves as further cost improvements become impossible, and balancer plan calculation becomes 046 * wasteful. This test ensures that the balancer will not get stuck in a loop of continuously moving 047 * regions. 048 */ 049@Category({ MasterTests.class, MediumTests.class }) 050public class TestUnattainableBalancerCostGoal { 051 052 @ClassRule 053 public static final HBaseClassTestRule CLASS_RULE = 054 HBaseClassTestRule.forClass(TestUnattainableBalancerCostGoal.class); 055 056 private static final Logger LOG = LoggerFactory.getLogger(TestUnattainableBalancerCostGoal.class); 057 058 private static final TableName SYSTEM_TABLE_NAME = TableName.valueOf("hbase:system"); 059 private static final TableName NON_SYSTEM_TABLE_NAME = TableName.valueOf("userTable"); 060 061 private static final int NUM_SERVERS = 10; 062 private static final int NUM_REGIONS = 1000; 063 private static final float UNACHIEVABLE_COST_GOAL = 0.01f; 064 065 private static final ServerName[] servers = new ServerName[NUM_SERVERS]; 066 private static final Map<ServerName, List<RegionInfo>> serverToRegions = new HashMap<>(); 067 068 @BeforeClass 069 public static void setup() { 070 // Initialize servers 071 for (int i = 0; i < NUM_SERVERS; i++) { 072 servers[i] = ServerName.valueOf("server" + i, i, System.currentTimeMillis()); 073 } 074 075 // Create regions 076 List<RegionInfo> allRegions = new ArrayList<>(); 077 for (int i = 0; i < NUM_REGIONS; i++) { 078 TableName tableName = i < 3 ? SYSTEM_TABLE_NAME : NON_SYSTEM_TABLE_NAME; 079 byte[] startKey = new byte[1]; 080 startKey[0] = (byte) i; 081 byte[] endKey = new byte[1]; 082 endKey[0] = (byte) (i + 1); 083 084 RegionInfo regionInfo = 085 RegionInfoBuilder.newBuilder(tableName).setStartKey(startKey).setEndKey(endKey).build(); 086 allRegions.add(regionInfo); 087 } 088 089 // Assign all regions to the first server 090 serverToRegions.put(servers[0], new ArrayList<>(allRegions)); 091 for (int i = 1; i < NUM_SERVERS; i++) { 092 serverToRegions.put(servers[i], new ArrayList<>()); 093 } 094 } 095 096 @Test 097 public void testSystemTableIsolation() { 098 Configuration conf = new Configuration(false); 099 conf.setBoolean(BalancerConditionals.ISOLATE_SYSTEM_TABLES_KEY, true); 100 runBalancerToExhaustion(conf, serverToRegions, Set.of(this::isSystemTableIsolated), 101 UNACHIEVABLE_COST_GOAL, 10_000, CandidateGeneratorTestUtil.ExhaustionType.NO_MORE_MOVES); 102 LOG.info("Meta table regions are successfully isolated."); 103 } 104 105 private boolean isSystemTableIsolated(BalancerClusterState cluster) { 106 return isTableIsolated(cluster, SYSTEM_TABLE_NAME, "System"); 107 } 108}