From 136414dd72a80f379b80cd6f74b5b6ebd78f33ec Mon Sep 17 00:00:00 2001 From: wenbang <815204947@qq.com> Date: Fri, 29 May 2020 11:39:42 +0800 Subject: [PATCH] HBASE-24453 [BackPort-HBASE-20220] Check if table exists in the cluster before moving it to the specified regionserver group Co-authored-by: wenbang Signed-off-by: Reid Chan --- .../hbase/rsgroup/RSGroupAdminClient.java | 12 ++++++- .../hbase/rsgroup/TestRSGroupsAdmin1.java | 3 +- .../hbase/rsgroup/TestRSGroupsAdmin2.java | 35 ++++++++++++++++++- hbase-shell/src/main/ruby/shell/commands.rb | 2 ++ 4 files changed, 49 insertions(+), 3 deletions(-) diff --git a/hbase-rsgroup/src/main/java/org/apache/hadoop/hbase/rsgroup/RSGroupAdminClient.java b/hbase-rsgroup/src/main/java/org/apache/hadoop/hbase/rsgroup/RSGroupAdminClient.java index 32f8692414c..af1924c391a 100644 --- a/hbase-rsgroup/src/main/java/org/apache/hadoop/hbase/rsgroup/RSGroupAdminClient.java +++ b/hbase-rsgroup/src/main/java/org/apache/hadoop/hbase/rsgroup/RSGroupAdminClient.java @@ -23,7 +23,9 @@ import java.util.List; import java.util.Set; import org.apache.hadoop.hbase.TableName; +import org.apache.hadoop.hbase.TableNotFoundException; import org.apache.hadoop.hbase.classification.InterfaceAudience; +import org.apache.hadoop.hbase.client.Admin; import org.apache.hadoop.hbase.client.Connection; import org.apache.hadoop.hbase.net.Address; import org.apache.hadoop.hbase.protobuf.ProtobufUtil; @@ -55,9 +57,11 @@ import com.google.protobuf.ServiceException; @InterfaceAudience.Private public class RSGroupAdminClient implements RSGroupAdmin { private RSGroupAdminService.BlockingInterface stub; + private Admin admin; public RSGroupAdminClient(Connection conn) throws IOException { - stub = RSGroupAdminService.newBlockingStub(conn.getAdmin().coprocessorService()); + admin = conn.getAdmin(); + stub = RSGroupAdminService.newBlockingStub(admin.coprocessorService()); } @Override @@ -114,6 +118,9 @@ public class RSGroupAdminClient implements RSGroupAdmin { MoveTablesRequest.Builder builder = MoveTablesRequest.newBuilder().setTargetGroup(targetGroup); for(TableName tableName: tables) { builder.addTableName(ProtobufUtil.toProtoTableName(tableName)); + if (!admin.tableExists(tableName)) { + throw new TableNotFoundException(tableName); + } } try { stub.moveTables(null, builder.build()); @@ -200,6 +207,9 @@ public class RSGroupAdminClient implements RSGroupAdmin { } for(TableName tableName: tables) { builder.addTableName(ProtobufUtil.toProtoTableName(tableName)); + if (!admin.tableExists(tableName)) { + throw new TableNotFoundException(tableName); + } } try { stub.moveServersAndTables(null, builder.build()); diff --git a/hbase-rsgroup/src/test/java/org/apache/hadoop/hbase/rsgroup/TestRSGroupsAdmin1.java b/hbase-rsgroup/src/test/java/org/apache/hadoop/hbase/rsgroup/TestRSGroupsAdmin1.java index 2a6c1b33700..b95db7b23a6 100644 --- a/hbase-rsgroup/src/test/java/org/apache/hadoop/hbase/rsgroup/TestRSGroupsAdmin1.java +++ b/hbase-rsgroup/src/test/java/org/apache/hadoop/hbase/rsgroup/TestRSGroupsAdmin1.java @@ -40,6 +40,7 @@ import org.apache.hadoop.hbase.NamespaceDescriptor; import org.apache.hadoop.hbase.ServerName; import org.apache.hadoop.hbase.TableExistsException; import org.apache.hadoop.hbase.TableName; +import org.apache.hadoop.hbase.TableNotFoundException; import org.apache.hadoop.hbase.Waiter; import org.apache.hadoop.hbase.constraint.ConstraintException; import org.apache.hadoop.hbase.master.ServerManager; @@ -118,7 +119,7 @@ public class TestRSGroupsAdmin1 extends TestRSGroupsBase { try { rsGroupAdmin.moveTables(Sets.newHashSet(TableName.valueOf("bogustable")), "bogus"); fail("Expected move with bogus group to fail"); - } catch(ConstraintException ex) { + } catch(ConstraintException | TableNotFoundException ex) { //expected } diff --git a/hbase-rsgroup/src/test/java/org/apache/hadoop/hbase/rsgroup/TestRSGroupsAdmin2.java b/hbase-rsgroup/src/test/java/org/apache/hadoop/hbase/rsgroup/TestRSGroupsAdmin2.java index b026daf172d..aad5f6e185e 100644 --- a/hbase-rsgroup/src/test/java/org/apache/hadoop/hbase/rsgroup/TestRSGroupsAdmin2.java +++ b/hbase-rsgroup/src/test/java/org/apache/hadoop/hbase/rsgroup/TestRSGroupsAdmin2.java @@ -22,6 +22,7 @@ package org.apache.hadoop.hbase.rsgroup; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; @@ -37,6 +38,7 @@ import org.apache.commons.logging.LogFactory; import org.apache.hadoop.hbase.HRegionInfo; import org.apache.hadoop.hbase.ServerName; import org.apache.hadoop.hbase.TableName; +import org.apache.hadoop.hbase.TableNotFoundException; import org.apache.hadoop.hbase.Waiter; import org.apache.hadoop.hbase.client.ClusterConnection; import org.apache.hadoop.hbase.master.HMaster; @@ -298,6 +300,37 @@ public class TestRSGroupsAdmin2 extends TestRSGroupsBase { assertTrue(observer.preRemoveServersCalled); } + @Test + public void testNonExistentTableMove() throws Exception { + TableName tableName = TableName.valueOf(tablePrefix + rand.nextInt()); + + RSGroupInfo tableGrp = rsGroupAdmin.getRSGroupInfoOfTable(tableName); + assertNull(tableGrp); + + //test if table exists already. + boolean exist = admin.tableExists(tableName); + assertFalse(exist); + + LOG.info("Moving table "+ tableName + " to " + RSGroupInfo.DEFAULT_GROUP); + try { + rsGroupAdmin.moveTables(Sets.newHashSet(tableName), RSGroupInfo.DEFAULT_GROUP); + fail("Table " + tableName + " shouldn't have been successfully moved."); + } catch(IOException ex) { + assertTrue(ex instanceof TableNotFoundException); + } + + try { + rsGroupAdmin.moveServersAndTables( + Sets.newHashSet(Address.fromParts("bogus",123)), + Sets.newHashSet(tableName), RSGroupInfo.DEFAULT_GROUP); + fail("Table " + tableName + " shouldn't have been successfully moved."); + } catch(IOException ex) { + assertTrue(ex instanceof TableNotFoundException); + } + //verify group change + assertNull(rsGroupAdmin.getRSGroupInfoOfTable(tableName)); + } + @Test public void testMoveServersAndTables() throws Exception { final TableName tableName = TableName.valueOf(tablePrefix + "_testMoveServersAndTables"); @@ -414,4 +447,4 @@ public class TestRSGroupsAdmin2 extends TestRSGroupsBase { assertTrue(observer.postMoveServersAndTables); } -} \ No newline at end of file +} diff --git a/hbase-shell/src/main/ruby/shell/commands.rb b/hbase-shell/src/main/ruby/shell/commands.rb index 102a6e190fc..e769fd01c99 100644 --- a/hbase-shell/src/main/ruby/shell/commands.rb +++ b/hbase-shell/src/main/ruby/shell/commands.rb @@ -120,6 +120,8 @@ module Shell end # Global HBase exception handling below if not handled by respective command above if cause.kind_of?(org.apache.hadoop.hbase.TableNotFoundException) then + strs = cause.to_s.split(' ') + raise "Unknown table #{strs[0]}!" if strs.size == 1 raise "Unknown table #{args.first}!" end if cause.kind_of?(org.apache.hadoop.hbase.UnknownRegionException) then