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.apache.hadoop.net.DNSToSwitchMapping;
33  import org.junit.BeforeClass;
34  import org.junit.Test;
35  import org.junit.experimental.categories.Category;
36  
37  /**
38   * Test the load balancer that is created by default.
39   */
40  @Category(MediumTests.class)
41  public class TestDefaultLoadBalancer extends BalancerTestBase {
42    private static final Log LOG = LogFactory.getLog(TestDefaultLoadBalancer.class);
43  
44    private static LoadBalancer loadBalancer;
45  
46    @BeforeClass
47    public static void beforeAllTests() throws Exception {
48      Configuration conf = HBaseConfiguration.create();
49      conf.setClass("hbase.util.ip.to.rack.determiner", MockMapping.class, DNSToSwitchMapping.class);
50      conf.set("hbase.regions.slop", "0");
51      loadBalancer = new SimpleLoadBalancer();
52      loadBalancer.setConf(conf);
53    }
54  
55    // int[testnum][servernumber] -> numregions
56    int[][] clusterStateMocks = new int[][] {
57        // 1 node
58        new int[] { 0 },
59        new int[] { 1 },
60        new int[] { 10 },
61        // 2 node
62        new int[] { 0, 0 },
63        new int[] { 2, 0 },
64        new int[] { 2, 1 },
65        new int[] { 2, 2 },
66        new int[] { 2, 3 },
67        new int[] { 2, 4 },
68        new int[] { 1, 1 },
69        new int[] { 0, 1 },
70        new int[] { 10, 1 },
71        new int[] { 14, 1432 },
72        new int[] { 47, 53 },
73        // 3 node
74        new int[] { 0, 1, 2 },
75        new int[] { 1, 2, 3 },
76        new int[] { 0, 2, 2 },
77        new int[] { 0, 3, 0 },
78        new int[] { 0, 4, 0 },
79        new int[] { 20, 20, 0 },
80        // 4 node
81        new int[] { 0, 1, 2, 3 },
82        new int[] { 4, 0, 0, 0 },
83        new int[] { 5, 0, 0, 0 },
84        new int[] { 6, 6, 0, 0 },
85        new int[] { 6, 2, 0, 0 },
86        new int[] { 6, 1, 0, 0 },
87        new int[] { 6, 0, 0, 0 },
88        new int[] { 4, 4, 4, 7 },
89        new int[] { 4, 4, 4, 8 },
90        new int[] { 0, 0, 0, 7 },
91        // 5 node
92        new int[] { 1, 1, 1, 1, 4 },
93        // more nodes
94        new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 },
95        new int[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 10 }, new int[] { 6, 6, 5, 6, 6, 6, 6, 6, 6, 1 },
96        new int[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 54 }, new int[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 55 },
97        new int[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 56 }, new int[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 16 },
98        new int[] { 1, 1, 1, 1, 1, 1, 1, 1, 1, 8 }, new int[] { 1, 1, 1, 1, 1, 1, 1, 1, 1, 9 },
99        new int[] { 1, 1, 1, 1, 1, 1, 1, 1, 1, 10 }, new int[] { 1, 1, 1, 1, 1, 1, 1, 1, 1, 123 },
100       new int[] { 1, 1, 1, 1, 1, 1, 1, 1, 1, 155 },
101       new int[] { 0, 0, 144, 1, 1, 1, 1, 1123, 133, 138, 12, 1444 },
102       new int[] { 0, 0, 144, 1, 0, 4, 1, 1123, 133, 138, 12, 1444 },
103       new int[] { 1538, 1392, 1561, 1557, 1535, 1553, 1385, 1542, 1619 } };
104 
105   /**
106    * Test the load balancing algorithm.
107    *
108    * Invariant is that all servers should be hosting either floor(average) or
109    * ceiling(average)
110    *
111    * @throws Exception
112    */
113   @Test (timeout=60000)
114   public void testBalanceCluster() throws Exception {
115 
116     for (int[] mockCluster : clusterStateMocks) {
117       Map<ServerName, List<HRegionInfo>> servers = mockClusterServers(mockCluster);
118       List<ServerAndLoad> list = convertToList(servers);
119       LOG.info("Mock Cluster : " + printMock(list) + " " + printStats(list));
120       List<RegionPlan> plans = loadBalancer.balanceCluster(servers);
121       List<ServerAndLoad> balancedCluster = reconcile(list, plans, servers);
122       LOG.info("Mock Balance : " + printMock(balancedCluster));
123       assertClusterAsBalanced(balancedCluster);
124       for (Map.Entry<ServerName, List<HRegionInfo>> entry : servers.entrySet()) {
125         returnRegions(entry.getValue());
126         returnServer(entry.getKey());
127       }
128     }
129   }
130 }