From e2ac5a9338be0f1e3c853e11522746c722083027 Mon Sep 17 00:00:00 2001 From: Ruanhui <32773751+frostruan@users.noreply.github.com> Date: Sat, 23 Apr 2022 03:30:26 +0800 Subject: [PATCH] HBASE-26942 cache region locations when getAllRegionLocations (#4364) Co-authored-by: huiruan Signed-off-by: Duo Zhang Signed-off-by: Bryan Beaudreault --- .../hbase/client/AsyncNonMetaRegionLocator.java | 2 +- .../hbase/client/AsyncTableRegionLocatorImpl.java | 6 +++++- .../client/TestAsyncNonMetaRegionLocator.java | 14 ++++++++++++++ 3 files changed, 20 insertions(+), 2 deletions(-) diff --git a/hbase-client/src/main/java/org/apache/hadoop/hbase/client/AsyncNonMetaRegionLocator.java b/hbase-client/src/main/java/org/apache/hadoop/hbase/client/AsyncNonMetaRegionLocator.java index 5798ee5f1fb..6896a2970b0 100644 --- a/hbase-client/src/main/java/org/apache/hadoop/hbase/client/AsyncNonMetaRegionLocator.java +++ b/hbase-client/src/main/java/org/apache/hadoop/hbase/client/AsyncNonMetaRegionLocator.java @@ -649,7 +649,7 @@ class AsyncNonMetaRegionLocator { } } - private void addLocationToCache(HRegionLocation loc) { + void addLocationToCache(HRegionLocation loc) { addToCache(getTableCache(loc.getRegion().getTable()), createRegionLocations(loc)); } diff --git a/hbase-client/src/main/java/org/apache/hadoop/hbase/client/AsyncTableRegionLocatorImpl.java b/hbase-client/src/main/java/org/apache/hadoop/hbase/client/AsyncTableRegionLocatorImpl.java index d5b275d2a77..db7d78cab93 100644 --- a/hbase-client/src/main/java/org/apache/hadoop/hbase/client/AsyncTableRegionLocatorImpl.java +++ b/hbase-client/src/main/java/org/apache/hadoop/hbase/client/AsyncTableRegionLocatorImpl.java @@ -18,6 +18,7 @@ package org.apache.hadoop.hbase.client; import static org.apache.hadoop.hbase.trace.TraceUtil.tracedFuture; +import static org.apache.hadoop.hbase.util.FutureUtils.addListener; import java.util.Arrays; import java.util.List; @@ -61,8 +62,11 @@ class AsyncTableRegionLocatorImpl implements AsyncTableRegionLocator { return conn.registry.getMetaRegionLocations() .thenApply(locs -> Arrays.asList(locs.getRegionLocations())); } - return AsyncMetaTableAccessor + CompletableFuture> future = AsyncMetaTableAccessor .getTableHRegionLocations(conn.getTable(TableName.META_TABLE_NAME), tableName); + addListener(future, (locs, error) -> locs.forEach(loc -> conn.getLocator() + .getNonMetaRegionLocator().addLocationToCache(loc))); + return future; }, getClass().getSimpleName() + ".getAllRegionLocations"); } diff --git a/hbase-server/src/test/java/org/apache/hadoop/hbase/client/TestAsyncNonMetaRegionLocator.java b/hbase-server/src/test/java/org/apache/hadoop/hbase/client/TestAsyncNonMetaRegionLocator.java index 79ab476de7a..21110f7fd4d 100644 --- a/hbase-server/src/test/java/org/apache/hadoop/hbase/client/TestAsyncNonMetaRegionLocator.java +++ b/hbase-server/src/test/java/org/apache/hadoop/hbase/client/TestAsyncNonMetaRegionLocator.java @@ -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; @@ -459,4 +460,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 regions = TEST_UTIL.getAdmin().getRegions(TABLE_NAME); + for (RegionInfo region : regions) { + assertNotNull(conn.getLocator().getNonMetaRegionLocator() + .getRegionLocationInCache(TABLE_NAME, region.getStartKey())); + } + } }