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