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