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 static org.junit.Assert.assertEquals;
21  import static org.junit.Assert.assertTrue;
22  
23  import java.util.ArrayList;
24  import java.util.List;
25  
26  import org.apache.commons.logging.Log;
27  import org.apache.commons.logging.LogFactory;
28  import org.apache.hadoop.hbase.HBaseTestingUtility;
29  import org.apache.hadoop.hbase.HDFSBlocksDistribution;
30  import org.apache.hadoop.hbase.HRegionInfo;
31  import org.apache.hadoop.hbase.MiniHBaseCluster;
32  import org.apache.hadoop.hbase.ServerName;
33  import org.apache.hadoop.hbase.TableName;
34  import org.apache.hadoop.hbase.client.Table;
35  import org.apache.hadoop.hbase.regionserver.Region;
36  import org.apache.hadoop.hbase.regionserver.HRegionServer;
37  import org.apache.hadoop.hbase.util.Bytes;
38  import org.apache.hadoop.hbase.testclassification.SmallTests;
39  import org.junit.AfterClass;
40  import org.junit.BeforeClass;
41  import org.junit.Test;
42  import org.junit.experimental.categories.Category;
43  
44  @Category({SmallTests.class})
45  public class TestRegionLocationFinder {
46    private static final Log LOG = LogFactory.getLog(TestRegionLocationFinder.class);
47    private final static HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility();
48    private static MiniHBaseCluster cluster;
49  
50    private final static TableName tableName = TableName.valueOf("table");
51    private final static byte[] FAMILY = Bytes.toBytes("cf");
52    private static Table table;
53    private final static int ServerNum = 5;
54  
55    private static RegionLocationFinder finder = new RegionLocationFinder();
56  
57    @BeforeClass
58    public static void setUpBeforeClass() throws Exception {
59      cluster = TEST_UTIL.startMiniCluster(1, ServerNum);
60      table = TEST_UTIL.createTable(tableName, FAMILY, HBaseTestingUtility.KEYS_FOR_HBA_CREATE_TABLE);
61      TEST_UTIL.waitTableAvailable(tableName, 1000);
62      TEST_UTIL.loadTable(table, FAMILY);
63  
64      for (int i = 0; i < ServerNum; i++) {
65        HRegionServer server = cluster.getRegionServer(i);
66        for (Region region : server.getOnlineRegions(tableName)) {
67          region.flush(true);
68        }
69      }
70  
71      finder.setConf(TEST_UTIL.getConfiguration());
72      finder.setServices(cluster.getMaster());
73      finder.setClusterStatus(cluster.getMaster().getClusterStatus());
74    }
75  
76    @AfterClass
77    public static void tearDownAfterClass() throws Exception {
78      table.close();
79      TEST_UTIL.deleteTable(tableName);
80      TEST_UTIL.shutdownMiniCluster();
81    }
82  
83    @Test
84    public void testInternalGetTopBlockLocation() throws Exception {
85      for (int i = 0; i < ServerNum; i++) {
86        HRegionServer server = cluster.getRegionServer(i);
87        for (Region region : server.getOnlineRegions(tableName)) {
88          // get region's hdfs block distribution by region and RegionLocationFinder, 
89          // they should have same result
90          HDFSBlocksDistribution blocksDistribution1 = region.getHDFSBlocksDistribution();
91          HDFSBlocksDistribution blocksDistribution2 = finder.getBlockDistribution(region
92              .getRegionInfo());
93          assertEquals(blocksDistribution1.getUniqueBlocksTotalWeight(),
94            blocksDistribution2.getUniqueBlocksTotalWeight());
95          if (blocksDistribution1.getUniqueBlocksTotalWeight() != 0) {
96            assertEquals(blocksDistribution1.getTopHosts().get(0), blocksDistribution2.getTopHosts()
97                .get(0));
98          }
99        }
100     }
101   }
102 
103   @Test
104   public void testMapHostNameToServerName() throws Exception {
105     List<String> topHosts = new ArrayList<String>();
106     for (int i = 0; i < ServerNum; i++) {
107       HRegionServer server = cluster.getRegionServer(i);
108       String serverHost = server.getServerName().getHostname();
109       if (!topHosts.contains(serverHost)) {
110         topHosts.add(serverHost);
111       }
112     }
113     List<ServerName> servers = finder.mapHostNameToServerName(topHosts);
114     // mini cluster, all rs in one host
115     assertEquals(1, topHosts.size());
116     for (int i = 0; i < ServerNum; i++) {
117       ServerName server = cluster.getRegionServer(i).getServerName();
118       assertTrue(servers.contains(server));
119     }
120   }
121 
122   @Test
123   public void testGetTopBlockLocations() throws Exception {
124     for (int i = 0; i < ServerNum; i++) {
125       HRegionServer server = cluster.getRegionServer(i);
126       for (Region region : server.getOnlineRegions(tableName)) {
127         List<ServerName> servers = finder.getTopBlockLocations(region.getRegionInfo());
128         // test table may have empty region
129         if (region.getHDFSBlocksDistribution().getUniqueBlocksTotalWeight() == 0) {
130           continue;
131         }
132         List<String> topHosts = region.getHDFSBlocksDistribution().getTopHosts();
133         // rs and datanode may have different host in local machine test
134         if (!topHosts.contains(server.getServerName().getHostname())) {
135           continue;
136         }
137         for (int j = 0; j < ServerNum; j++) {
138           ServerName serverName = cluster.getRegionServer(j).getServerName();
139           assertTrue(servers.contains(serverName));
140         }
141       }
142     }
143   }
144 }