HBASE-22158 RawAsyncHBaseAdmin.getTableSplits should filter out none default replicas

Signed-off-by: Guanghao Zhang <zghao@apache.org>
Signed-off-by: Zheng Hu <openinx@gmail.com>
This commit is contained in:
Duo Zhang 2019-04-03 18:30:48 +08:00 committed by zhangduo
parent 02fea3e7a0
commit e35ff3acb7
2 changed files with 59 additions and 27 deletions

View File

@ -842,12 +842,13 @@ class RawAsyncHBaseAdmin implements AsyncAdmin {
@Override @Override
public CompletableFuture<List<RegionInfo>> getRegions(TableName tableName) { public CompletableFuture<List<RegionInfo>> getRegions(TableName tableName) {
if (tableName.equals(META_TABLE_NAME)) { if (tableName.equals(META_TABLE_NAME)) {
return connection.getLocator().getRegionLocation(tableName, null, null, operationTimeoutNs) return connection.registry.getMetaRegionLocation()
.thenApply(loc -> Collections.singletonList(loc.getRegion())); .thenApply(locs -> Stream.of(locs.getRegionLocations()).map(HRegionLocation::getRegion)
.collect(Collectors.toList()));
} else { } else {
return AsyncMetaTableAccessor.getTableHRegionLocations(metaTable, Optional.of(tableName)) return AsyncMetaTableAccessor.getTableHRegionLocations(metaTable, Optional.of(tableName))
.thenApply( .thenApply(
locs -> locs.stream().map(loc -> loc.getRegion()).collect(Collectors.toList())); locs -> locs.stream().map(HRegionLocation::getRegion).collect(Collectors.toList()));
} }
} }
@ -3412,21 +3413,24 @@ class RawAsyncHBaseAdmin implements AsyncAdmin {
private CompletableFuture<byte[][]> getTableSplits(TableName tableName) { private CompletableFuture<byte[][]> getTableSplits(TableName tableName) {
CompletableFuture<byte[][]> future = new CompletableFuture<>(); CompletableFuture<byte[][]> future = new CompletableFuture<>();
addListener(getRegions(tableName), (regions, err2) -> { addListener(
if (err2 != null) { getRegions(tableName).thenApply(regions -> regions.stream()
future.completeExceptionally(err2); .filter(RegionReplicaUtil::isDefaultReplica).collect(Collectors.toList())),
return; (regions, err2) -> {
} if (err2 != null) {
if (regions.size() == 1) { future.completeExceptionally(err2);
future.complete(null); return;
} else {
byte[][] splits = new byte[regions.size() - 1][];
for (int i = 1; i < regions.size(); i++) {
splits[i - 1] = regions.get(i).getStartKey();
} }
future.complete(splits); if (regions.size() == 1) {
} future.complete(null);
}); } else {
byte[][] splits = new byte[regions.size() - 1][];
for (int i = 1; i < regions.size(); i++) {
splits[i - 1] = regions.get(i).getStartKey();
}
future.complete(splits);
}
});
return future; return future;
} }
@ -3655,13 +3659,15 @@ class RawAsyncHBaseAdmin implements AsyncAdmin {
if (err3 != null) { if (err3 != null) {
future.completeExceptionally(err3); future.completeExceptionally(err3);
} else { } else {
addListener(createTable(newTableDesc, splits), (result, err4) -> { addListener(
if (err4 != null) { splits != null ? createTable(newTableDesc, splits) : createTable(newTableDesc),
future.completeExceptionally(err4); (result, err4) -> {
} else { if (err4 != null) {
future.complete(result); future.completeExceptionally(err4);
} } else {
}); future.complete(result);
}
});
} }
}); });
} else { } else {

View File

@ -43,11 +43,11 @@ import org.junit.runners.Parameterized;
@RunWith(Parameterized.class) @RunWith(Parameterized.class)
@Category({ LargeTests.class, ClientTests.class }) @Category({ LargeTests.class, ClientTests.class })
public class TestAsyncRegionAdminApiWithRegionReplicas extends TestAsyncAdminBase { public class TestAsyncAdminWithRegionReplicas extends TestAsyncAdminBase {
@ClassRule @ClassRule
public static final HBaseClassTestRule CLASS_RULE = public static final HBaseClassTestRule CLASS_RULE =
HBaseClassTestRule.forClass(TestAsyncRegionAdminApiWithRegionReplicas.class); HBaseClassTestRule.forClass(TestAsyncAdminWithRegionReplicas.class);
@BeforeClass @BeforeClass
public static void setUpBeforeClass() throws Exception { public static void setUpBeforeClass() throws Exception {
@ -125,4 +125,30 @@ public class TestAsyncRegionAdminApiWithRegionReplicas extends TestAsyncAdminBas
assertThat(e.getCause(), instanceOf(IllegalArgumentException.class)); assertThat(e.getCause(), instanceOf(IllegalArgumentException.class));
} }
} }
@Test
public void testCloneTableSchema() throws IOException, InterruptedException, ExecutionException {
createTableWithDefaultConf(tableName, 3);
admin.cloneTableSchema(tableName, TableName.valueOf(tableName.getNameAsString() + "_new"), true)
.get();
}
@Test
public void testGetTableRegions() throws InterruptedException, ExecutionException, IOException {
List<RegionInfo> metaRegions = admin.getRegions(TableName.META_TABLE_NAME).get();
assertEquals(3, metaRegions.size());
for (int i = 0; i < 3; i++) {
RegionInfo metaRegion = metaRegions.get(i);
assertEquals(TableName.META_TABLE_NAME, metaRegion.getTable());
assertEquals(i, metaRegion.getReplicaId());
}
createTableWithDefaultConf(tableName, 3);
List<RegionInfo> regions = admin.getRegions(tableName).get();
assertEquals(3, metaRegions.size());
for (int i = 0; i < 3; i++) {
RegionInfo region = regions.get(i);
assertEquals(tableName, region.getTable());
assertEquals(i, region.getReplicaId());
}
}
} }