Skip to content
Merged
Show file tree
Hide file tree
Changes from 7 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,23 @@ 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));
} catch (Exception e) {
future.completeExceptionally(e);
} finally {
future.complete(locs);
Copy link
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
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,9 +25,13 @@
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.assertTrue;

import java.io.IOException;
import java.net.SocketAddress;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
Expand All @@ -36,6 +40,7 @@
import java.util.concurrent.ThreadLocalRandom;
import java.util.stream.IntStream;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.DoNotRetryIOException;
import org.apache.hadoop.hbase.HBaseClassTestRule;
import org.apache.hadoop.hbase.HBaseTestingUtil;
import org.apache.hadoop.hbase.HRegionLocation;
Expand Down Expand Up @@ -64,6 +69,8 @@

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

import org.apache.hadoop.hbase.shaded.protobuf.generated.ClientProtos.ClientService;

@Category({ MediumTests.class, ClientTests.class })
@RunWith(Parameterized.class)
public class TestAsyncNonMetaRegionLocator {
Expand Down Expand Up @@ -460,4 +467,58 @@ 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();
Configuration config = new Configuration(TEST_UTIL.getConfiguration());
config.setClass(ConnectionFactory.HBASE_CLIENT_ASYNC_CONNECTION_IMPL,
BadAsyncConnectionImpl.class, AsyncConnection.class);
AsyncConnectionImpl clientConn = (AsyncConnectionImpl)
ConnectionFactory.createAsyncConnection(config).get();

Exception ex = null;
try {
clientConn.getRegionLocator(TABLE_NAME).getAllRegionLocations().get();
} catch (Exception e) {
ex = e;
}

assertNotNull(ex);
assertTrue(ex instanceof ExecutionException);
assertTrue(ex.getCause() != null && ex.getCause() instanceof RegionLocationResultException);

List<RegionInfo> regions = TEST_UTIL.getAdmin().getRegions(TABLE_NAME);
for (RegionInfo region : regions) {
assertNull(clientConn.getLocator()
.getRegionLocationInCache(TABLE_NAME, region.getStartKey()));
}
}

private static final class RegionLocationResultException extends DoNotRetryIOException {}

private static final class BadAsyncConnectionImpl extends AsyncConnectionImpl {

public BadAsyncConnectionImpl(Configuration conf, ConnectionRegistry registry,
String clusterId, SocketAddress localAddress, User user) {
super(conf, registry, clusterId, localAddress, user);
}

@Override
ClientService.Interface getRegionServerStub(ServerName serverName) throws IOException {
throw new RegionLocationResultException();
}
}
}