View Javadoc

1   /**
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  package org.apache.hadoop.hbase.master.balancer;
19  
20  import java.util.List;
21  import java.util.Map;
22  
23  import org.apache.commons.logging.Log;
24  import org.apache.commons.logging.LogFactory;
25  import org.apache.hadoop.conf.Configuration;
26  import org.apache.hadoop.hbase.HBaseConfiguration;
27  import org.apache.hadoop.hbase.HRegionInfo;
28  import org.apache.hadoop.hbase.testclassification.MediumTests;
29  import org.apache.hadoop.hbase.ServerName;
30  import org.apache.hadoop.hbase.master.LoadBalancer;
31  import org.apache.hadoop.hbase.master.RegionPlan;
32  import org.junit.BeforeClass;
33  import org.junit.Test;
34  import org.junit.experimental.categories.Category;
35  
36  /**
37   * Test the load balancer that is created by default.
38   */
39  @Category(MediumTests.class)
40  public class TestDefaultLoadBalancer extends BalancerTestBase {
41    private static final Log LOG = LogFactory.getLog(TestDefaultLoadBalancer.class);
42  
43    private static LoadBalancer loadBalancer;
44  
45    @BeforeClass
46    public static void beforeAllTests() throws Exception {
47      Configuration conf = HBaseConfiguration.create();
48      conf.set("hbase.regions.slop", "0");
49      loadBalancer = new SimpleLoadBalancer();
50      loadBalancer.setConf(conf);
51    }
52  
53    // int[testnum][servernumber] -> numregions
54    int[][] clusterStateMocks = new int[][] {
55        // 1 node
56        new int[] { 0 },
57        new int[] { 1 },
58        new int[] { 10 },
59        // 2 node
60        new int[] { 0, 0 },
61        new int[] { 2, 0 },
62        new int[] { 2, 1 },
63        new int[] { 2, 2 },
64        new int[] { 2, 3 },
65        new int[] { 2, 4 },
66        new int[] { 1, 1 },
67        new int[] { 0, 1 },
68        new int[] { 10, 1 },
69        new int[] { 14, 1432 },
70        new int[] { 47, 53 },
71        // 3 node
72        new int[] { 0, 1, 2 },
73        new int[] { 1, 2, 3 },
74        new int[] { 0, 2, 2 },
75        new int[] { 0, 3, 0 },
76        new int[] { 0, 4, 0 },
77        new int[] { 20, 20, 0 },
78        // 4 node
79        new int[] { 0, 1, 2, 3 },
80        new int[] { 4, 0, 0, 0 },
81        new int[] { 5, 0, 0, 0 },
82        new int[] { 6, 6, 0, 0 },
83        new int[] { 6, 2, 0, 0 },
84        new int[] { 6, 1, 0, 0 },
85        new int[] { 6, 0, 0, 0 },
86        new int[] { 4, 4, 4, 7 },
87        new int[] { 4, 4, 4, 8 },
88        new int[] { 0, 0, 0, 7 },
89        // 5 node
90        new int[] { 1, 1, 1, 1, 4 },
91        // more nodes
92        new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 },
93        new int[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 10 }, new int[] { 6, 6, 5, 6, 6, 6, 6, 6, 6, 1 },
94        new int[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 54 }, new int[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 55 },
95        new int[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 56 }, new int[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 16 },
96        new int[] { 1, 1, 1, 1, 1, 1, 1, 1, 1, 8 }, new int[] { 1, 1, 1, 1, 1, 1, 1, 1, 1, 9 },
97        new int[] { 1, 1, 1, 1, 1, 1, 1, 1, 1, 10 }, new int[] { 1, 1, 1, 1, 1, 1, 1, 1, 1, 123 },
98        new int[] { 1, 1, 1, 1, 1, 1, 1, 1, 1, 155 },
99        new int[] { 0, 0, 144, 1, 1, 1, 1, 1123, 133, 138, 12, 1444 },
100       new int[] { 0, 0, 144, 1, 0, 4, 1, 1123, 133, 138, 12, 1444 },
101       new int[] { 1538, 1392, 1561, 1557, 1535, 1553, 1385, 1542, 1619 } };
102 
103   /**
104    * Test the load balancing algorithm.
105    *
106    * Invariant is that all servers should be hosting either floor(average) or
107    * ceiling(average)
108    *
109    * @throws Exception
110    */
111   @Test
112   public void testBalanceCluster() throws Exception {
113 
114     for (int[] mockCluster : clusterStateMocks) {
115       Map<ServerName, List<HRegionInfo>> servers = mockClusterServers(mockCluster);
116       List<ServerAndLoad> list = convertToList(servers);
117       LOG.info("Mock Cluster : " + printMock(list) + " " + printStats(list));
118       List<RegionPlan> plans = loadBalancer.balanceCluster(servers);
119       List<ServerAndLoad> balancedCluster = reconcile(list, plans, servers);
120       LOG.info("Mock Balance : " + printMock(balancedCluster));
121       assertClusterAsBalanced(balancedCluster);
122       for (Map.Entry<ServerName, List<HRegionInfo>> entry : servers.entrySet()) {
123         returnRegions(entry.getValue());
124         returnServer(entry.getKey());
125       }
126     }
127 
128   }
129 
130 }