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.client.RegionInfo; 027import org.apache.hadoop.hbase.master.RegionPlan; 028import org.apache.hadoop.hbase.testclassification.LargeTests; 029import org.apache.hadoop.hbase.testclassification.MasterTests; 030import org.junit.ClassRule; 031import org.junit.Test; 032import org.junit.experimental.categories.Category; 033import org.slf4j.Logger; 034import org.slf4j.LoggerFactory; 035 036@Category({ MasterTests.class, LargeTests.class }) 037public class TestStochasticLoadBalancerBalanceCluster extends BalancerTestBase { 038 039 @ClassRule 040 public static final HBaseClassTestRule CLASS_RULE = 041 HBaseClassTestRule.forClass(TestStochasticLoadBalancerBalanceCluster.class); 042 043 private static final Logger LOG = 044 LoggerFactory.getLogger(TestStochasticLoadBalancerBalanceCluster.class); 045 046 /** 047 * Test the load balancing algorithm. 048 * <p> 049 * Invariant is that all servers should be hosting either floor(average) or ceiling(average) 050 */ 051 @Test 052 public void testBalanceCluster() throws Exception { 053 conf.setLong(StochasticLoadBalancer.MAX_STEPS_KEY, 2000000L); 054 conf.setLong("hbase.master.balancer.stochastic.maxRunningTime", 90 * 1000); // 90 sec 055 conf.setFloat("hbase.master.balancer.stochastic.maxMovePercent", 1.0f); 056 loadBalancer.setConf(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 List<RegionPlan> plans = loadBalancer.balanceCluster(servers); 062 List<ServerAndLoad> balancedCluster = reconcile(list, plans, servers); 063 LOG.info("Mock Balance : " + printMock(balancedCluster)); 064 assertClusterAsBalanced(balancedCluster); 065 List<RegionPlan> secondPlans = loadBalancer.balanceCluster(servers); 066 assertNull(secondPlans); 067 for (Map.Entry<ServerName, List<RegionInfo>> entry : servers.entrySet()) { 068 returnRegions(entry.getValue()); 069 returnServer(entry.getKey()); 070 } 071 } 072 } 073}