001/** 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, software 013 * distributed under the License is distributed on an "AS IS" BASIS, 014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 015 * See the License for the specific language governing permissions and 016 * limitations under the License. 017 */ 018package org.apache.hadoop.hbase.client; 019 020import static java.util.stream.Collectors.toList; 021import static org.apache.hadoop.hbase.HConstants.EMPTY_END_ROW; 022import static org.apache.hadoop.hbase.HConstants.EMPTY_START_ROW; 023import static org.hamcrest.CoreMatchers.instanceOf; 024import static org.junit.Assert.assertArrayEquals; 025import static org.junit.Assert.assertEquals; 026import static org.junit.Assert.assertSame; 027import static org.junit.Assert.assertThat; 028 029import java.io.IOException; 030import java.util.Arrays; 031import java.util.List; 032import java.util.concurrent.CompletableFuture; 033import java.util.concurrent.ExecutionException; 034import java.util.concurrent.ThreadLocalRandom; 035import java.util.stream.IntStream; 036import org.apache.commons.io.IOUtils; 037import org.apache.hadoop.hbase.HBaseClassTestRule; 038import org.apache.hadoop.hbase.HBaseTestingUtility; 039import org.apache.hadoop.hbase.HRegionInfo; 040import org.apache.hadoop.hbase.HRegionLocation; 041import org.apache.hadoop.hbase.NotServingRegionException; 042import org.apache.hadoop.hbase.ServerName; 043import org.apache.hadoop.hbase.TableName; 044import org.apache.hadoop.hbase.TableNotFoundException; 045import org.apache.hadoop.hbase.Waiter.ExplainingPredicate; 046import org.apache.hadoop.hbase.security.User; 047import org.apache.hadoop.hbase.testclassification.ClientTests; 048import org.apache.hadoop.hbase.testclassification.MediumTests; 049import org.apache.hadoop.hbase.util.Bytes; 050import org.junit.After; 051import org.junit.AfterClass; 052import org.junit.BeforeClass; 053import org.junit.ClassRule; 054import org.junit.Test; 055import org.junit.experimental.categories.Category; 056 057@Category({ MediumTests.class, ClientTests.class }) 058public class TestAsyncNonMetaRegionLocator { 059 060 @ClassRule 061 public static final HBaseClassTestRule CLASS_RULE = 062 HBaseClassTestRule.forClass(TestAsyncNonMetaRegionLocator.class); 063 064 private static final HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility(); 065 066 private static TableName TABLE_NAME = TableName.valueOf("async"); 067 068 private static byte[] FAMILY = Bytes.toBytes("cf"); 069 070 private static AsyncConnectionImpl CONN; 071 072 private static AsyncNonMetaRegionLocator LOCATOR; 073 074 private static byte[][] SPLIT_KEYS; 075 076 @BeforeClass 077 public static void setUp() throws Exception { 078 TEST_UTIL.startMiniCluster(3); 079 TEST_UTIL.getAdmin().setBalancerRunning(false, true); 080 AsyncRegistry registry = AsyncRegistryFactory.getRegistry(TEST_UTIL.getConfiguration()); 081 CONN = new AsyncConnectionImpl(TEST_UTIL.getConfiguration(), registry, 082 registry.getClusterId().get(), User.getCurrent()); 083 LOCATOR = new AsyncNonMetaRegionLocator(CONN); 084 SPLIT_KEYS = new byte[8][]; 085 for (int i = 111; i < 999; i += 111) { 086 SPLIT_KEYS[i / 111 - 1] = Bytes.toBytes(String.format("%03d", i)); 087 } 088 } 089 090 @AfterClass 091 public static void tearDown() throws Exception { 092 IOUtils.closeQuietly(CONN); 093 TEST_UTIL.shutdownMiniCluster(); 094 } 095 096 @After 097 public void tearDownAfterTest() throws IOException { 098 Admin admin = TEST_UTIL.getAdmin(); 099 if (admin.tableExists(TABLE_NAME)) { 100 if (admin.isTableEnabled(TABLE_NAME)) { 101 TEST_UTIL.getAdmin().disableTable(TABLE_NAME); 102 } 103 TEST_UTIL.getAdmin().deleteTable(TABLE_NAME); 104 } 105 LOCATOR.clearCache(TABLE_NAME); 106 } 107 108 private void createSingleRegionTable() throws IOException, InterruptedException { 109 TEST_UTIL.createTable(TABLE_NAME, FAMILY); 110 TEST_UTIL.waitTableAvailable(TABLE_NAME); 111 } 112 113 @Test 114 public void testNoTable() throws InterruptedException { 115 for (RegionLocateType locateType : RegionLocateType.values()) { 116 try { 117 LOCATOR.getRegionLocation(TABLE_NAME, EMPTY_START_ROW, locateType, false).get(); 118 } catch (ExecutionException e) { 119 assertThat(e.getCause(), instanceOf(TableNotFoundException.class)); 120 } 121 } 122 } 123 124 @Test 125 public void testDisableTable() throws IOException, InterruptedException { 126 createSingleRegionTable(); 127 TEST_UTIL.getAdmin().disableTable(TABLE_NAME); 128 for (RegionLocateType locateType : RegionLocateType.values()) { 129 try { 130 LOCATOR.getRegionLocation(TABLE_NAME, EMPTY_START_ROW, locateType, false).get(); 131 } catch (ExecutionException e) { 132 assertThat(e.getCause(), instanceOf(TableNotFoundException.class)); 133 } 134 } 135 } 136 137 private void assertLocEquals(byte[] startKey, byte[] endKey, ServerName serverName, 138 HRegionLocation loc) { 139 HRegionInfo info = loc.getRegionInfo(); 140 assertEquals(TABLE_NAME, info.getTable()); 141 assertArrayEquals(startKey, info.getStartKey()); 142 assertArrayEquals(endKey, info.getEndKey()); 143 assertEquals(serverName, loc.getServerName()); 144 } 145 146 @Test 147 public void testSingleRegionTable() throws IOException, InterruptedException, ExecutionException { 148 createSingleRegionTable(); 149 ServerName serverName = TEST_UTIL.getRSForFirstRegionInTable(TABLE_NAME).getServerName(); 150 for (RegionLocateType locateType : RegionLocateType.values()) { 151 assertLocEquals(EMPTY_START_ROW, EMPTY_END_ROW, serverName, 152 LOCATOR.getRegionLocation(TABLE_NAME, EMPTY_START_ROW, locateType, false).get()); 153 } 154 byte[] randKey = new byte[ThreadLocalRandom.current().nextInt(128)]; 155 ThreadLocalRandom.current().nextBytes(randKey); 156 for (RegionLocateType locateType : RegionLocateType.values()) { 157 assertLocEquals(EMPTY_START_ROW, EMPTY_END_ROW, serverName, 158 LOCATOR.getRegionLocation(TABLE_NAME, randKey, locateType, false).get()); 159 } 160 } 161 162 private void createMultiRegionTable() throws IOException, InterruptedException { 163 TEST_UTIL.createTable(TABLE_NAME, FAMILY, SPLIT_KEYS); 164 TEST_UTIL.waitTableAvailable(TABLE_NAME); 165 } 166 167 private static byte[][] getStartKeys() { 168 byte[][] startKeys = new byte[SPLIT_KEYS.length + 1][]; 169 startKeys[0] = EMPTY_START_ROW; 170 System.arraycopy(SPLIT_KEYS, 0, startKeys, 1, SPLIT_KEYS.length); 171 return startKeys; 172 } 173 174 private static byte[][] getEndKeys() { 175 byte[][] endKeys = Arrays.copyOf(SPLIT_KEYS, SPLIT_KEYS.length + 1); 176 endKeys[endKeys.length - 1] = EMPTY_START_ROW; 177 return endKeys; 178 } 179 180 private ServerName[] getLocations(byte[][] startKeys) { 181 ServerName[] serverNames = new ServerName[startKeys.length]; 182 TEST_UTIL.getHBaseCluster().getRegionServerThreads().stream().map(t -> t.getRegionServer()) 183 .forEach(rs -> { 184 rs.getRegions(TABLE_NAME).forEach(r -> { 185 serverNames[Arrays.binarySearch(startKeys, r.getRegionInfo().getStartKey(), 186 Bytes::compareTo)] = rs.getServerName(); 187 }); 188 }); 189 return serverNames; 190 } 191 192 @Test 193 public void testMultiRegionTable() throws IOException, InterruptedException { 194 createMultiRegionTable(); 195 byte[][] startKeys = getStartKeys(); 196 ServerName[] serverNames = getLocations(startKeys); 197 IntStream.range(0, 2).forEach(n -> IntStream.range(0, startKeys.length).forEach(i -> { 198 try { 199 assertLocEquals(startKeys[i], i == startKeys.length - 1 ? EMPTY_END_ROW : startKeys[i + 1], 200 serverNames[i], LOCATOR 201 .getRegionLocation(TABLE_NAME, startKeys[i], RegionLocateType.CURRENT, false).get()); 202 } catch (InterruptedException | ExecutionException e) { 203 throw new RuntimeException(e); 204 } 205 })); 206 207 LOCATOR.clearCache(TABLE_NAME); 208 IntStream.range(0, 2).forEach(n -> IntStream.range(0, startKeys.length).forEach(i -> { 209 try { 210 assertLocEquals(startKeys[i], i == startKeys.length - 1 ? EMPTY_END_ROW : startKeys[i + 1], 211 serverNames[i], 212 LOCATOR.getRegionLocation(TABLE_NAME, startKeys[i], RegionLocateType.AFTER, false).get()); 213 } catch (InterruptedException | ExecutionException e) { 214 throw new RuntimeException(e); 215 } 216 })); 217 218 LOCATOR.clearCache(TABLE_NAME); 219 byte[][] endKeys = getEndKeys(); 220 IntStream.range(0, 2).forEach( 221 n -> IntStream.range(0, endKeys.length).map(i -> endKeys.length - 1 - i).forEach(i -> { 222 try { 223 assertLocEquals(i == 0 ? EMPTY_START_ROW : endKeys[i - 1], endKeys[i], serverNames[i], 224 LOCATOR.getRegionLocation(TABLE_NAME, endKeys[i], RegionLocateType.BEFORE, false) 225 .get()); 226 } catch (InterruptedException | ExecutionException e) { 227 throw new RuntimeException(e); 228 } 229 })); 230 } 231 232 @Test 233 public void testRegionMove() throws IOException, InterruptedException, ExecutionException { 234 createSingleRegionTable(); 235 ServerName serverName = TEST_UTIL.getRSForFirstRegionInTable(TABLE_NAME).getServerName(); 236 HRegionLocation loc = LOCATOR 237 .getRegionLocation(TABLE_NAME, EMPTY_START_ROW, RegionLocateType.CURRENT, false).get(); 238 assertLocEquals(EMPTY_START_ROW, EMPTY_END_ROW, serverName, loc); 239 ServerName newServerName = TEST_UTIL.getHBaseCluster().getRegionServerThreads().stream() 240 .map(t -> t.getRegionServer().getServerName()).filter(sn -> !sn.equals(serverName)) 241 .findAny().get(); 242 243 TEST_UTIL.getAdmin().move(Bytes.toBytes(loc.getRegionInfo().getEncodedName()), 244 Bytes.toBytes(newServerName.getServerName())); 245 while (!TEST_UTIL.getRSForFirstRegionInTable(TABLE_NAME).getServerName() 246 .equals(newServerName)) { 247 Thread.sleep(100); 248 } 249 // Should be same as it is in cache 250 assertSame(loc, LOCATOR 251 .getRegionLocation(TABLE_NAME, EMPTY_START_ROW, RegionLocateType.CURRENT, false).get()); 252 LOCATOR.updateCachedLocation(loc, null); 253 // null error will not trigger a cache cleanup 254 assertSame(loc, LOCATOR 255 .getRegionLocation(TABLE_NAME, EMPTY_START_ROW, RegionLocateType.CURRENT, false).get()); 256 LOCATOR.updateCachedLocation(loc, new NotServingRegionException()); 257 assertLocEquals(EMPTY_START_ROW, EMPTY_END_ROW, newServerName, LOCATOR 258 .getRegionLocation(TABLE_NAME, EMPTY_START_ROW, RegionLocateType.CURRENT, false).get()); 259 } 260 261 // usually locate after will return the same result, so we add a test to make it return different 262 // result. 263 @Test 264 public void testLocateAfter() throws IOException, InterruptedException, ExecutionException { 265 byte[] row = Bytes.toBytes("1"); 266 byte[] splitKey = Arrays.copyOf(row, 2); 267 TEST_UTIL.createTable(TABLE_NAME, FAMILY, new byte[][] { splitKey }); 268 TEST_UTIL.waitTableAvailable(TABLE_NAME); 269 HRegionLocation currentLoc = 270 LOCATOR.getRegionLocation(TABLE_NAME, row, RegionLocateType.CURRENT, false).get(); 271 ServerName currentServerName = TEST_UTIL.getRSForFirstRegionInTable(TABLE_NAME).getServerName(); 272 assertLocEquals(EMPTY_START_ROW, splitKey, currentServerName, currentLoc); 273 274 HRegionLocation afterLoc = 275 LOCATOR.getRegionLocation(TABLE_NAME, row, RegionLocateType.AFTER, false).get(); 276 ServerName afterServerName = 277 TEST_UTIL.getHBaseCluster().getRegionServerThreads().stream().map(t -> t.getRegionServer()) 278 .filter(rs -> rs.getRegions(TABLE_NAME).stream() 279 .anyMatch(r -> Bytes.equals(splitKey, r.getRegionInfo().getStartKey()))) 280 .findAny().get().getServerName(); 281 assertLocEquals(splitKey, EMPTY_END_ROW, afterServerName, afterLoc); 282 283 assertSame(afterLoc, 284 LOCATOR.getRegionLocation(TABLE_NAME, row, RegionLocateType.AFTER, false).get()); 285 } 286 287 // For HBASE-17402 288 @Test 289 public void testConcurrentLocate() throws IOException, InterruptedException, ExecutionException { 290 createMultiRegionTable(); 291 byte[][] startKeys = getStartKeys(); 292 byte[][] endKeys = getEndKeys(); 293 ServerName[] serverNames = getLocations(startKeys); 294 for (int i = 0; i < 100; i++) { 295 LOCATOR.clearCache(TABLE_NAME); 296 List<CompletableFuture<HRegionLocation>> futures = IntStream.range(0, 1000) 297 .mapToObj(n -> String.format("%03d", n)).map(s -> Bytes.toBytes(s)) 298 .map(r -> LOCATOR.getRegionLocation(TABLE_NAME, r, RegionLocateType.CURRENT, false)) 299 .collect(toList()); 300 for (int j = 0; j < 1000; j++) { 301 int index = Math.min(8, j / 111); 302 assertLocEquals(startKeys[index], endKeys[index], serverNames[index], futures.get(j).get()); 303 } 304 } 305 } 306 307 @Test 308 public void testReload() throws Exception { 309 createSingleRegionTable(); 310 ServerName serverName = TEST_UTIL.getRSForFirstRegionInTable(TABLE_NAME).getServerName(); 311 for (RegionLocateType locateType : RegionLocateType.values()) { 312 assertLocEquals(EMPTY_START_ROW, EMPTY_END_ROW, serverName, 313 LOCATOR.getRegionLocation(TABLE_NAME, EMPTY_START_ROW, locateType, false).get()); 314 } 315 ServerName newServerName = TEST_UTIL.getHBaseCluster().getRegionServerThreads().stream() 316 .map(t -> t.getRegionServer().getServerName()).filter(sn -> !sn.equals(serverName)) 317 .findAny().get(); 318 Admin admin = TEST_UTIL.getAdmin(); 319 HRegionInfo region = admin.getTableRegions(TABLE_NAME).stream().findAny().get(); 320 admin.move(region.getEncodedNameAsBytes(), Bytes.toBytes(newServerName.getServerName())); 321 TEST_UTIL.waitFor(30000, new ExplainingPredicate<Exception>() { 322 323 @Override 324 public boolean evaluate() throws Exception { 325 ServerName newServerName = TEST_UTIL.getRSForFirstRegionInTable(TABLE_NAME).getServerName(); 326 return newServerName != null && !newServerName.equals(serverName); 327 } 328 329 @Override 330 public String explainFailure() throws Exception { 331 return region.getRegionNameAsString() + " is still on " + serverName; 332 } 333 334 }); 335 // The cached location will not change 336 for (RegionLocateType locateType : RegionLocateType.values()) { 337 assertLocEquals(EMPTY_START_ROW, EMPTY_END_ROW, serverName, 338 LOCATOR.getRegionLocation(TABLE_NAME, EMPTY_START_ROW, locateType, false).get()); 339 } 340 // should get the new location when reload = true 341 assertLocEquals(EMPTY_START_ROW, EMPTY_END_ROW, newServerName, 342 LOCATOR.getRegionLocation(TABLE_NAME, EMPTY_START_ROW, RegionLocateType.CURRENT, true).get()); 343 // the cached location should be replaced 344 for (RegionLocateType locateType : RegionLocateType.values()) { 345 assertLocEquals(EMPTY_START_ROW, EMPTY_END_ROW, newServerName, 346 LOCATOR.getRegionLocation(TABLE_NAME, EMPTY_START_ROW, locateType, false).get()); 347 } 348 } 349}