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 BalancerTestBase {
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    conf.setLong(StochasticLoadBalancer.MAX_STEPS_KEY, 2000000L);
055    conf.setLong("hbase.master.balancer.stochastic.maxRunningTime", 90 * 1000); // 90 sec
056    conf.setFloat("hbase.master.balancer.stochastic.maxMovePercent", 1.0f);
057    loadBalancer.setConf(conf);
058    for (int[] mockCluster : clusterStateMocks) {
059      Map<ServerName, List<RegionInfo>> servers = mockClusterServers(mockCluster);
060      List<ServerAndLoad> list = convertToList(servers);
061      LOG.info("Mock Cluster : " + printMock(list) + " " + printStats(list));
062
063      Map<TableName, Map<ServerName, List<RegionInfo>>> LoadOfAllTable =
064          (Map) mockClusterServersWithTables(servers);
065      List<RegionPlan> plans = loadBalancer.balanceCluster(LoadOfAllTable);
066      List<ServerAndLoad> balancedCluster = reconcile(list, plans, servers);
067      LOG.info("Mock Balance : " + printMock(balancedCluster));
068      assertClusterAsBalanced(balancedCluster);
069      LoadOfAllTable = (Map) mockClusterServersWithTables(servers);
070      List<RegionPlan> secondPlans = loadBalancer.balanceCluster(LoadOfAllTable);
071      assertNull(secondPlans);
072      for (Map.Entry<ServerName, List<RegionInfo>> entry : servers.entrySet()) {
073        returnRegions(entry.getValue());
074        returnServer(entry.getKey());
075      }
076    }
077  }
078}