From 102f0bf9c565f70afd272cd5f2cb39305c3783f8 Mon Sep 17 00:00:00 2001 From: Yechao Chen Date: Tue, 8 May 2018 14:04:59 +0800 Subject: [PATCH] HBASE-20500 [rsgroup] should keep at least one server in default group Signed-off-by: tedyu --- .../hbase/rsgroup/RSGroupAdminServer.java | 5 ++ .../hbase/rsgroup/TestRSGroupsBase.java | 47 +++++++++++++++++++ 2 files changed, 52 insertions(+) diff --git a/hbase-rsgroup/src/main/java/org/apache/hadoop/hbase/rsgroup/RSGroupAdminServer.java b/hbase-rsgroup/src/main/java/org/apache/hadoop/hbase/rsgroup/RSGroupAdminServer.java index 094fc1d948b..670e8aa62ba 100644 --- a/hbase-rsgroup/src/main/java/org/apache/hadoop/hbase/rsgroup/RSGroupAdminServer.java +++ b/hbase-rsgroup/src/main/java/org/apache/hadoop/hbase/rsgroup/RSGroupAdminServer.java @@ -56,6 +56,8 @@ import org.apache.hbase.thirdparty.com.google.common.collect.Maps; @InterfaceAudience.Private public class RSGroupAdminServer implements RSGroupAdmin { private static final Logger LOG = LoggerFactory.getLogger(RSGroupAdminServer.class); + public static final String KEEP_ONE_SERVER_IN_DEFAULT_ERROR_MESSAGE = "should keep at least " + + "one server in 'default' RSGroup."; private MasterServices master; private final RSGroupInfoManager rsGroupInfoManager; @@ -307,6 +309,9 @@ public class RSGroupAdminServer implements RSGroupAdmin { // Only move online servers (when moving from 'default') or servers from other // groups. This prevents bogus servers from entering groups if (RSGroupInfo.DEFAULT_GROUP.equals(srcGrp.getName())) { + if (srcGrp.getServers().size() <= servers.size()) { + throw new ConstraintException(KEEP_ONE_SERVER_IN_DEFAULT_ERROR_MESSAGE); + } checkOnlineServersOnly(servers); } // Ensure all servers are of same rsgroup. diff --git a/hbase-rsgroup/src/test/java/org/apache/hadoop/hbase/rsgroup/TestRSGroupsBase.java b/hbase-rsgroup/src/test/java/org/apache/hadoop/hbase/rsgroup/TestRSGroupsBase.java index 9e296370f63..199dd9864c5 100644 --- a/hbase-rsgroup/src/test/java/org/apache/hadoop/hbase/rsgroup/TestRSGroupsBase.java +++ b/hbase-rsgroup/src/test/java/org/apache/hadoop/hbase/rsgroup/TestRSGroupsBase.java @@ -339,6 +339,53 @@ public abstract class TestRSGroupsBase { Assert.assertEquals(null, rsGroupAdmin.getRSGroupInfo(fooGroup.getName())); } + @Test + public void testMoveServersFromDefaultGroup() throws Exception { + //create groups and assign servers + rsGroupAdmin.addRSGroup("foo"); + + RSGroupInfo fooGroup = rsGroupAdmin.getRSGroupInfo("foo"); + assertEquals(0, fooGroup.getServers().size()); + RSGroupInfo defaultGroup = rsGroupAdmin.getRSGroupInfo(RSGroupInfo.DEFAULT_GROUP); + + //test remove all servers from default + try { + rsGroupAdmin.moveServers(defaultGroup.getServers(), fooGroup.getName()); + fail(RSGroupAdminServer.KEEP_ONE_SERVER_IN_DEFAULT_ERROR_MESSAGE); + } catch (ConstraintException ex) { + assertTrue(ex.getMessage().contains(RSGroupAdminServer + .KEEP_ONE_SERVER_IN_DEFAULT_ERROR_MESSAGE)); + } + + //test success case, remove one server from default ,keep at least one server + if (defaultGroup.getServers().size() > 1) { + Address serverInDefaultGroup = defaultGroup.getServers().iterator().next(); + LOG.info("moving server " + serverInDefaultGroup + " from group default to group " + + fooGroup.getName()); + rsGroupAdmin.moveServers(Sets.newHashSet(serverInDefaultGroup), fooGroup.getName()); + } + + fooGroup = rsGroupAdmin.getRSGroupInfo("foo"); + LOG.info("moving servers " + fooGroup.getServers() + " to group default"); + rsGroupAdmin.moveServers(fooGroup.getServers(), RSGroupInfo.DEFAULT_GROUP); + + TEST_UTIL.waitFor(WAIT_TIMEOUT, new Waiter.Predicate() { + @Override + public boolean evaluate() throws Exception { + return getNumServers() == + rsGroupAdmin.getRSGroupInfo(RSGroupInfo.DEFAULT_GROUP).getServers().size(); + } + }); + + fooGroup = rsGroupAdmin.getRSGroupInfo("foo"); + assertEquals(0, fooGroup.getServers().size()); + + //test group removal + LOG.info("Remove group " + fooGroup.getName()); + rsGroupAdmin.removeRSGroup(fooGroup.getName()); + Assert.assertEquals(null, rsGroupAdmin.getRSGroupInfo(fooGroup.getName())); + } + @Test public void testTableMoveTruncateAndDrop() throws Exception { final byte[] familyNameBytes = Bytes.toBytes("f");