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