From 04937baf7b7f072c0f3c2544ef9183416b8dd00b Mon Sep 17 00:00:00 2001 From: Duo Zhang Date: Wed, 3 Apr 2019 18:30:48 +0800 Subject: [PATCH] HBASE-22158 RawAsyncHBaseAdmin.getTableSplits should filter out none default replicas Signed-off-by: Guanghao Zhang Signed-off-by: Zheng Hu --- .../hbase/client/RawAsyncHBaseAdmin.java | 56 ++++++++++--------- ... => TestAsyncAdminWithRegionReplicas.java} | 30 +++++++++- 2 files changed, 59 insertions(+), 27 deletions(-) rename hbase-server/src/test/java/org/apache/hadoop/hbase/client/{TestAsyncRegionAdminApiWithRegionReplicas.java => TestAsyncAdminWithRegionReplicas.java} (81%) diff --git a/hbase-client/src/main/java/org/apache/hadoop/hbase/client/RawAsyncHBaseAdmin.java b/hbase-client/src/main/java/org/apache/hadoop/hbase/client/RawAsyncHBaseAdmin.java index 6c89fc88b0b1..1eaaa88470bd 100644 --- a/hbase-client/src/main/java/org/apache/hadoop/hbase/client/RawAsyncHBaseAdmin.java +++ b/hbase-client/src/main/java/org/apache/hadoop/hbase/client/RawAsyncHBaseAdmin.java @@ -838,12 +838,13 @@ public CompletableFuture> getRegions(ServerName serverName) { @Override public CompletableFuture> getRegions(TableName tableName) { if (tableName.equals(META_TABLE_NAME)) { - return connection.getLocator().getRegionLocation(tableName, null, null, operationTimeoutNs) - .thenApply(loc -> Collections.singletonList(loc.getRegion())); + return connection.registry.getMetaRegionLocation() + .thenApply(locs -> Stream.of(locs.getRegionLocations()).map(HRegionLocation::getRegion) + .collect(Collectors.toList())); } else { return AsyncMetaTableAccessor.getTableHRegionLocations(metaTable, Optional.of(tableName)) - .thenApply( - locs -> locs.stream().map(loc -> loc.getRegion()).collect(Collectors.toList())); + .thenApply( + locs -> locs.stream().map(HRegionLocation::getRegion).collect(Collectors.toList())); } } @@ -3418,21 +3419,24 @@ public CompletableFuture disableTableReplication(TableName tableName) { private CompletableFuture getTableSplits(TableName tableName) { CompletableFuture future = new CompletableFuture<>(); - addListener(getRegions(tableName), (regions, err2) -> { - if (err2 != null) { - future.completeExceptionally(err2); - return; - } - 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(); + addListener( + getRegions(tableName).thenApply(regions -> regions.stream() + .filter(RegionReplicaUtil::isDefaultReplica).collect(Collectors.toList())), + (regions, err2) -> { + if (err2 != null) { + future.completeExceptionally(err2); + return; } - 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; } @@ -3661,13 +3665,15 @@ public CompletableFuture cloneTableSchema(TableName tableName, TableName n if (err3 != null) { future.completeExceptionally(err3); } else { - addListener(createTable(newTableDesc, splits), (result, err4) -> { - if (err4 != null) { - future.completeExceptionally(err4); - } else { - future.complete(result); - } - }); + addListener( + splits != null ? createTable(newTableDesc, splits) : createTable(newTableDesc), + (result, err4) -> { + if (err4 != null) { + future.completeExceptionally(err4); + } else { + future.complete(result); + } + }); } }); } else { diff --git a/hbase-server/src/test/java/org/apache/hadoop/hbase/client/TestAsyncRegionAdminApiWithRegionReplicas.java b/hbase-server/src/test/java/org/apache/hadoop/hbase/client/TestAsyncAdminWithRegionReplicas.java similarity index 81% rename from hbase-server/src/test/java/org/apache/hadoop/hbase/client/TestAsyncRegionAdminApiWithRegionReplicas.java rename to hbase-server/src/test/java/org/apache/hadoop/hbase/client/TestAsyncAdminWithRegionReplicas.java index 13e5f4524cdd..17bd6713f4f1 100644 --- a/hbase-server/src/test/java/org/apache/hadoop/hbase/client/TestAsyncRegionAdminApiWithRegionReplicas.java +++ b/hbase-server/src/test/java/org/apache/hadoop/hbase/client/TestAsyncAdminWithRegionReplicas.java @@ -43,11 +43,11 @@ @RunWith(Parameterized.class) @Category({ LargeTests.class, ClientTests.class }) -public class TestAsyncRegionAdminApiWithRegionReplicas extends TestAsyncAdminBase { +public class TestAsyncAdminWithRegionReplicas extends TestAsyncAdminBase { @ClassRule public static final HBaseClassTestRule CLASS_RULE = - HBaseClassTestRule.forClass(TestAsyncRegionAdminApiWithRegionReplicas.class); + HBaseClassTestRule.forClass(TestAsyncAdminWithRegionReplicas.class); @BeforeClass public static void setUpBeforeClass() throws Exception { @@ -125,4 +125,30 @@ public void testMergeNonDefaultReplicas() 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 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 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()); + } + } }