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