HBASE-26942 cache region locations when getAllRegionLocations (#4357)

Co-authored-by: huiruan <huiruan@tencent.com>
Signed-off-by: Duo Zhang <zhangduo@apache.org>
Signed-off-by: Bryan Beaudreault <bbeaudreault@apache.org>
This commit is contained in:
Ruanhui 2022-04-22 19:53:01 +08:00 committed by GitHub
parent d9f4c519ca
commit 7715e37d37
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 22 additions and 3 deletions

View File

@ -649,7 +649,7 @@ class AsyncNonMetaRegionLocator {
}
}
private void addLocationToCache(HRegionLocation loc) {
void addLocationToCache(HRegionLocation loc) {
addToCache(getTableCache(loc.getRegion().getTable()), createRegionLocations(loc));
}

View File

@ -17,6 +17,8 @@
*/
package org.apache.hadoop.hbase.client;
import static org.apache.hadoop.hbase.util.FutureUtils.addListener;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.CompletableFuture;
@ -58,8 +60,11 @@ class AsyncTableRegionLocatorImpl implements AsyncTableRegionLocator {
return conn.registry.getMetaRegionLocations()
.thenApply(locs -> Arrays.asList(locs.getRegionLocations()));
}
return AsyncMetaTableAccessor.getTableHRegionLocations(conn.getTable(TableName.META_TABLE_NAME),
tableName);
CompletableFuture<List<HRegionLocation>> future = AsyncMetaTableAccessor
.getTableHRegionLocations(conn.getTable(TableName.META_TABLE_NAME), tableName);
addListener(future, (locs, error) -> locs.forEach(loc -> conn.getLocator()
.getNonMetaRegionLocator().addLocationToCache(loc)));
return future;
}
@Override

View File

@ -25,6 +25,7 @@ import static org.hamcrest.CoreMatchers.instanceOf;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertSame;
import java.io.IOException;
@ -467,4 +468,17 @@ public class TestAsyncNonMetaRegionLocator {
IntStream.range(0, 100).parallel()
.forEach(i -> LOCATOR.updateCachedLocationOnError(loc, new NotServingRegionException()));
}
@Test
public void testCacheLocationWhenGetAllLocations() throws Exception {
createMultiRegionTable();
AsyncConnectionImpl conn = (AsyncConnectionImpl)
ConnectionFactory.createAsyncConnection(TEST_UTIL.getConfiguration()).get();
conn.getRegionLocator(TABLE_NAME).getAllRegionLocations().get();
List<RegionInfo> regions = TEST_UTIL.getAdmin().getRegions(TABLE_NAME);
for (RegionInfo region : regions) {
assertNotNull(conn.getLocator().getNonMetaRegionLocator()
.getRegionLocationInCache(TABLE_NAME, region.getStartKey()));
}
}
}