View Javadoc

1   /**
2    *
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *     http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   */
19  package org.apache.hadoop.hbase;
20  
21  import static org.junit.Assert.assertEquals;
22  import static org.junit.Assert.fail;
23  
24  import org.apache.commons.logging.Log;
25  import org.apache.commons.logging.LogFactory;
26  import org.apache.hadoop.hbase.client.Admin;
27  import org.apache.hadoop.hbase.client.Connection;
28  import org.apache.hadoop.hbase.client.ConnectionFactory;
29  import org.apache.hadoop.hbase.client.RegionLocator;
30  import org.apache.hadoop.hbase.master.RegionStates;
31  import org.apache.hadoop.hbase.protobuf.ProtobufUtil;
32  import org.apache.hadoop.hbase.regionserver.HRegionServer;
33  import org.apache.hadoop.hbase.testclassification.LargeTests;
34  import org.apache.hadoop.hbase.util.Bytes;
35  import org.apache.hadoop.hbase.util.JVMClusterUtil;
36  import org.apache.hadoop.hbase.util.Threads;
37  import org.junit.After;
38  import org.junit.Before;
39  import org.junit.Test;
40  import org.junit.experimental.categories.Category;
41  import org.junit.runner.RunWith;
42  import org.junit.runners.Parameterized;
43  import org.junit.runners.Parameterized.Parameters;
44  
45  import java.io.IOException;
46  import java.util.ArrayList;
47  import java.util.Arrays;
48  import java.util.Collection;
49  import java.util.List;
50  
51  /**
52   * Test whether region re-balancing works. (HBASE-71)
53   */
54  @Category(LargeTests.class)
55  @RunWith(value = Parameterized.class)
56  public class TestRegionRebalancing {
57  
58    @Parameters
59    public static Collection<Object[]> data() {
60      Object[][] balancers =
61          new String[][] { { "org.apache.hadoop.hbase.master.balancer.SimpleLoadBalancer" },
62              { "org.apache.hadoop.hbase.master.balancer.StochasticLoadBalancer" } };
63      return Arrays.asList(balancers);
64    }
65  
66    private static final byte[] FAMILY_NAME = Bytes.toBytes("col");
67    public static final Log LOG = LogFactory.getLog(TestRegionRebalancing.class);
68    private final HBaseTestingUtility UTIL = new HBaseTestingUtility();
69    private RegionLocator regionLocator;
70    private HTableDescriptor desc;
71    private String balancerName;
72  
73    public TestRegionRebalancing(String balancerName) {
74      this.balancerName = balancerName;
75  
76    }
77  
78    @After
79    public void after() throws Exception {
80      UTIL.shutdownMiniCluster();
81    }
82  
83    @Before
84    public void before() throws Exception {
85      UTIL.getConfiguration().set("hbase.master.loadbalancer.class", this.balancerName);
86      UTIL.startMiniCluster(1);
87      this.desc = new HTableDescriptor(TableName.valueOf("test"));
88      this.desc.addFamily(new HColumnDescriptor(FAMILY_NAME));
89    }
90  
91    /**
92     * For HBASE-71. Try a few different configurations of starting and stopping
93     * region servers to see if the assignment or regions is pretty balanced.
94     * @throws IOException
95     * @throws InterruptedException
96     */
97    @Test (timeout=300000)
98    @SuppressWarnings("deprecation")
99    public void testRebalanceOnRegionServerNumberChange()
100   throws IOException, InterruptedException {
101     try(Connection connection = ConnectionFactory.createConnection(UTIL.getConfiguration());
102         Admin admin = connection.getAdmin()) {
103       admin.createTable(this.desc, Arrays.copyOfRange(HBaseTestingUtility.KEYS,
104           1, HBaseTestingUtility.KEYS.length));
105       this.regionLocator = connection.getRegionLocator(this.desc.getTableName());
106   
107       MetaTableAccessor.fullScanMetaAndPrint(admin.getConnection());
108   
109       assertEquals("Test table should have right number of regions",
110         HBaseTestingUtility.KEYS.length,
111         this.regionLocator.getStartKeys().length);
112   
113       // verify that the region assignments are balanced to start out
114       assertRegionsAreBalanced();
115   
116       // add a region server - total of 2
117       LOG.info("Started second server=" +
118         UTIL.getHBaseCluster().startRegionServer().getRegionServer().getServerName());
119       UTIL.getHBaseCluster().getMaster().balance();
120       assertRegionsAreBalanced();
121   
122       // On a balanced cluster, calling balance() should return true
123       assert(UTIL.getHBaseCluster().getMaster().balance() == true);
124   
125       // if we add a server, then the balance() call should return true
126       // add a region server - total of 3
127       LOG.info("Started third server=" +
128           UTIL.getHBaseCluster().startRegionServer().getRegionServer().getServerName());
129       assert(UTIL.getHBaseCluster().getMaster().balance() == true);
130       assertRegionsAreBalanced();
131   
132       // kill a region server - total of 2
133       LOG.info("Stopped third server=" + UTIL.getHBaseCluster().stopRegionServer(2, false));
134       UTIL.getHBaseCluster().waitOnRegionServer(2);
135       UTIL.getHBaseCluster().getMaster().balance();
136       assertRegionsAreBalanced();
137   
138       // start two more region servers - total of 4
139       LOG.info("Readding third server=" +
140           UTIL.getHBaseCluster().startRegionServer().getRegionServer().getServerName());
141       LOG.info("Added fourth server=" +
142           UTIL.getHBaseCluster().startRegionServer().getRegionServer().getServerName());
143       assert(UTIL.getHBaseCluster().getMaster().balance() == true);
144       assertRegionsAreBalanced();
145   
146       for (int i = 0; i < 6; i++){
147         LOG.info("Adding " + (i + 5) + "th region server");
148         UTIL.getHBaseCluster().startRegionServer();
149       }
150       assert(UTIL.getHBaseCluster().getMaster().balance() == true);
151       assertRegionsAreBalanced();
152       regionLocator.close();
153     }
154   }
155 
156   /**
157    * Determine if regions are balanced. Figure out the total, divide by the
158    * number of online servers, then test if each server is +/- 1 of average
159    * rounded up.
160    */
161   private void assertRegionsAreBalanced() throws IOException {
162     // TODO: Fix this test.  Old balancer used to run with 'slop'.  New
163     // balancer does not.
164     boolean success = false;
165     float slop = (float)UTIL.getConfiguration().getFloat("hbase.regions.slop", 0.1f);
166     if (slop <= 0) slop = 1;
167 
168     for (int i = 0; i < 5; i++) {
169       success = true;
170       // make sure all the regions are reassigned before we test balance
171       waitForAllRegionsAssigned();
172 
173       long regionCount = UTIL.getMiniHBaseCluster().countServedRegions();
174       List<HRegionServer> servers = getOnlineRegionServers();
175       double avg = UTIL.getHBaseCluster().getMaster().getAverageLoad();
176       int avgLoadPlusSlop = (int)Math.ceil(avg * (1 + slop));
177       int avgLoadMinusSlop = (int)Math.floor(avg * (1 - slop)) - 1;
178       LOG.debug("There are " + servers.size() + " servers and " + regionCount
179         + " regions. Load Average: " + avg + " low border: " + avgLoadMinusSlop
180         + ", up border: " + avgLoadPlusSlop + "; attempt: " + i);
181 
182       for (HRegionServer server : servers) {
183         int serverLoad =
184           ProtobufUtil.getOnlineRegions(server.getRSRpcServices()).size();
185         LOG.debug(server.getServerName() + " Avg: " + avg + " actual: " + serverLoad);
186         if (!(avg > 2.0 && serverLoad <= avgLoadPlusSlop
187             && serverLoad >= avgLoadMinusSlop)) {
188           for (HRegionInfo hri :
189               ProtobufUtil.getOnlineRegions(server.getRSRpcServices())) {
190             if (hri.isMetaRegion()) serverLoad--;
191             // LOG.debug(hri.getRegionNameAsString());
192           }
193           if (!(serverLoad <= avgLoadPlusSlop && serverLoad >= avgLoadMinusSlop)) {
194             LOG.debug(server.getServerName() + " Isn't balanced!!! Avg: " + avg +
195                 " actual: " + serverLoad + " slop: " + slop);
196             success = false;
197             break;
198           }
199         }
200       }
201 
202       if (!success) {
203         // one or more servers are not balanced. sleep a little to give it a
204         // chance to catch up. then, go back to the retry loop.
205         try {
206           Thread.sleep(10000);
207         } catch (InterruptedException e) {}
208 
209         UTIL.getHBaseCluster().getMaster().balance();
210         continue;
211       }
212 
213       // if we get here, all servers were balanced, so we should just return.
214       return;
215     }
216     // if we get here, we tried 5 times and never got to short circuit out of
217     // the retry loop, so this is a failure.
218     fail("After 5 attempts, region assignments were not balanced.");
219   }
220 
221   private List<HRegionServer> getOnlineRegionServers() {
222     List<HRegionServer> list = new ArrayList<HRegionServer>();
223     for (JVMClusterUtil.RegionServerThread rst :
224         UTIL.getHBaseCluster().getRegionServerThreads()) {
225       if (rst.getRegionServer().isOnline()) {
226         list.add(rst.getRegionServer());
227       }
228     }
229     return list;
230   }
231 
232   /**
233    * Wait until all the regions are assigned.
234    */
235   private void waitForAllRegionsAssigned() throws IOException {
236     int totalRegions = HBaseTestingUtility.KEYS.length;
237     while (UTIL.getMiniHBaseCluster().countServedRegions() < totalRegions) {
238     // while (!cluster.getMaster().allRegionsAssigned()) {
239       LOG.debug("Waiting for there to be "+ totalRegions +" regions, but there are "
240         + UTIL.getMiniHBaseCluster().countServedRegions() + " right now.");
241       try {
242         Thread.sleep(200);
243       } catch (InterruptedException e) {}
244     }
245     RegionStates regionStates = UTIL.getHBaseCluster().getMaster().getAssignmentManager().getRegionStates();
246     while (!regionStates.getRegionsInTransition().isEmpty()) {
247       Threads.sleep(100);
248     }
249   }
250 
251 }
252