Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -649,7 +649,7 @@ private void removeLocationFromCache(HRegionLocation loc) {
}
}

private void addLocationToCache(HRegionLocation loc) {
void addLocationToCache(HRegionLocation loc) {
addToCache(getTableCache(loc.getRegion().getTable()), createRegionLocations(loc));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -61,8 +62,21 @@ public CompletableFuture<List<HRegionLocation>> getAllRegionLocations() {
return conn.registry.getMetaRegionLocations()
.thenApply(locs -> Arrays.asList(locs.getRegionLocations()));
}
return ClientMetaTableAccessor
.getTableHRegionLocations(conn.getTable(TableName.META_TABLE_NAME), tableName);
CompletableFuture<List<HRegionLocation>> future = new CompletableFuture<>();
addListener(ClientMetaTableAccessor.getTableHRegionLocations(conn
.getTable(TableName.META_TABLE_NAME), tableName), (locs, error) -> {
if (error != null) {
future.completeExceptionally(error);
return;
}
try {
locs.forEach(loc -> conn.getLocator()
.getNonMetaRegionLocator().addLocationToCache(loc));
Comment thread
bbeaudreault marked this conversation as resolved.
Outdated
} finally {
future.complete(locs);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you move this to right after the locs.forEach?

As is, this will result in completing the future twice when an exception is raised. I think CompletableFuture will handle this correctly, but it's more correct and less confusing to future readers to not do that.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sorry, that's a stupid mistake..... Thanks for pointing out. I'll fix this.

}
});
return future;
}, getClass().getSimpleName() + ".getAllRegionLocations");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,10 @@
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.assertNull;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertThrows;

import java.io.IOException;
import java.util.Arrays;
Expand Down Expand Up @@ -61,6 +64,7 @@
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameter;
import org.mockito.Mockito;

import org.apache.hbase.thirdparty.com.google.common.io.Closeables;

Expand Down Expand Up @@ -460,4 +464,38 @@ public void testConcurrentUpdateCachedLocationOnError() throws Exception {
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().getRegionLocationInCache(TABLE_NAME, region.getStartKey()));
}
}

@Test
public void testCacheLocationExceptionallyWhenGetAllLocations() throws Exception {
createMultiRegionTable();
AsyncConnectionImpl conn = (AsyncConnectionImpl)
ConnectionFactory.createAsyncConnection(TEST_UTIL.getConfiguration()).get();
AsyncTable<AdvancedScanResultConsumer> metaTable = conn.getTable(TableName.META_TABLE_NAME);
AsyncTable<AdvancedScanResultConsumer> spyMetaTable = Mockito.spy(metaTable);
Mockito.doThrow(new MockedBadScanResultException()).when(spyMetaTable)
.scan(Mockito.any(Scan.class), Mockito.any(AdvancedScanResultConsumer.class));
AsyncConnectionImpl spyConn = Mockito.spy(conn);
Mockito.doReturn(spyMetaTable).when(spyConn).getTable(TableName.META_TABLE_NAME);
assertThrows(MockedBadScanResultException.class, () ->
spyConn.getRegionLocator(TABLE_NAME).getAllRegionLocations().get());
List<RegionInfo> regions = TEST_UTIL.getAdmin().getRegions(TABLE_NAME);
for (RegionInfo region : regions) {
assertNull(conn.getLocator().getRegionLocationInCache(TABLE_NAME, region.getStartKey()));
}
}

/** This is used to mock that getting all region locations completes exceptionally. */
private static final class MockedBadScanResultException extends RuntimeException {}
}