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.junit.jupiter.api.Assertions.assertNull; 021 022import java.time.Duration; 023import java.util.List; 024import java.util.Map; 025import org.apache.hadoop.hbase.ServerName; 026import org.apache.hadoop.hbase.TableName; 027import org.apache.hadoop.hbase.client.RegionInfo; 028import org.apache.hadoop.hbase.master.RegionPlan; 029import org.apache.hadoop.hbase.testclassification.LargeTests; 030import org.apache.hadoop.hbase.testclassification.MasterTests; 031import org.junit.jupiter.api.Tag; 032import org.junit.jupiter.api.Test; 033import org.slf4j.Logger; 034import org.slf4j.LoggerFactory; 035 036@Tag(MasterTests.TAG) 037@Tag(LargeTests.TAG) 038public class TestStochasticLoadBalancerBalanceCluster extends StochasticBalancerTestBase { 039 040 private static final Logger LOG = 041 LoggerFactory.getLogger(TestStochasticLoadBalancerBalanceCluster.class); 042 043 /** 044 * Test the load balancing algorithm. 045 * <p> 046 * Invariant is that all servers should be hosting either floor(average) or ceiling(average) 047 */ 048 @Test 049 public void testBalanceCluster() throws Exception { 050 setMaxRunTime(Duration.ofMillis(2500)); 051 loadBalancer.onConfigurationChange(conf); 052 for (int[] mockCluster : clusterStateMocks) { 053 Map<ServerName, List<RegionInfo>> servers = mockClusterServers(mockCluster); 054 List<ServerAndLoad> list = convertToList(servers); 055 LOG.info("Mock Cluster : {} {}", printMock(list), printStats(list)); 056 Map<TableName, Map<ServerName, List<RegionInfo>>> LoadOfAllTable = 057 (Map) mockClusterServersWithTables(servers); 058 List<RegionPlan> plans = loadBalancer.balanceCluster(LoadOfAllTable); 059 List<ServerAndLoad> balancedCluster = reconcile(list, plans, servers); 060 LOG.info("Mock Balance : {}", printMock(balancedCluster)); 061 assertClusterAsBalanced(balancedCluster); 062 LoadOfAllTable = (Map) mockClusterServersWithTables(servers); 063 List<RegionPlan> secondPlans = loadBalancer.balanceCluster(LoadOfAllTable); 064 assertNull(secondPlans); 065 for (Map.Entry<ServerName, List<RegionInfo>> entry : servers.entrySet()) { 066 returnRegions(entry.getValue()); 067 returnServer(entry.getKey()); 068 } 069 } 070 } 071}