diff --git src/test/java/org/apache/hadoop/hbase/TestDrainingServer.java src/test/java/org/apache/hadoop/hbase/TestDrainingServer.java new file mode 100644 index 0000000..622af03 --- /dev/null +++ src/test/java/org/apache/hadoop/hbase/TestDrainingServer.java @@ -0,0 +1,113 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.hadoop.hbase; + + +import java.io.IOException; + +import junit.framework.Assert; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.apache.hadoop.fs.FileSystem; +import org.apache.hadoop.hbase.client.HBaseAdmin; +import org.apache.hadoop.hbase.master.HMaster; +import org.apache.hadoop.hbase.regionserver.HRegionServer; +import org.apache.hadoop.hbase.util.Bytes; +import org.apache.hadoop.hbase.util.FSUtils; +import org.apache.hadoop.hbase.util.Threads; +import org.apache.hadoop.hbase.zookeeper.ZKUtil; +import org.apache.hadoop.hbase.zookeeper.ZooKeeperWatcher; +import org.apache.zookeeper.KeeperException; +import org.junit.AfterClass; +import org.junit.BeforeClass; +import org.junit.Test; + +public class TestDrainingServer { + private final Log LOG = LogFactory.getLog(this.getClass().getName()); + private static final HBaseTestingUtility TEST_UTIL = + new HBaseTestingUtility(); + private static final byte [] TABLENAME = Bytes.toBytes("t"); + private static final byte [] FAMILY = Bytes.toBytes("f"); + private static final int COUNT_OF_REGIONS = HBaseTestingUtility.KEYS.length; + + @BeforeClass + public static void setUpBeforeClass() throws Exception { + TEST_UTIL.startMiniCluster(5); + HTableDescriptor htd = new HTableDescriptor(TABLENAME); + htd.addFamily(new HColumnDescriptor(FAMILY)); + TEST_UTIL.createMultiRegionsInMeta(TEST_UTIL.getConfiguration(), htd, + HBaseTestingUtility.KEYS); + // Make a mark for the table in the filesystem. + FileSystem fs = FileSystem.get(TEST_UTIL.getConfiguration()); + FSUtils.createTableDescriptor(fs, FSUtils.getRootDir(TEST_UTIL.getConfiguration()), htd); + // Assign out the regions we just created. + HBaseAdmin admin = new HBaseAdmin(TEST_UTIL.getConfiguration()); + admin.disableTable(TABLENAME); + admin.enableTable(TABLENAME); + // Assert that every regionserver has some regions on it. + MiniHBaseCluster cluster = TEST_UTIL.getMiniHBaseCluster(); + for (int i = 0; i < cluster.getRegionServerThreads().size(); i++) { + HRegionServer hrs = cluster.getRegionServer(i); + Assert.assertFalse(hrs.getOnlineRegions().isEmpty()); + } + } + + @AfterClass + public static void tearDownAfterClass() throws Exception { + TEST_UTIL.shutdownMiniCluster(); + } + + /** + * Test that draining server works by killing a regionserver and verifying that + * the draining server does not pick up any of the dead servers regions. + * @throws KeeperException + * @throws IOException + */ + @Test + public void testDrainingServer() throws KeeperException, IOException { + // Add first server to draining servers up in zk. + HRegionServer drainingServer = + TEST_UTIL.getMiniHBaseCluster().getRegionServer(0); + final int regionsOnDrainingServer = + drainingServer.getOnlineRegions().size(); + LOG.info("Making " + drainingServer.getServerName() + " the draining server; it has " + + regionsOnDrainingServer + " regions"); + HMaster master = TEST_UTIL.getMiniHBaseCluster().getMaster(); + ZooKeeperWatcher zkw = master.getZooKeeper(); + String hrsDrainingZnode = + ZKUtil.joinZNode(zkw.drainingZNode, drainingServer.getServerName().toString()); + ZKUtil.createWithParents(zkw, hrsDrainingZnode); + // Kill a few regionservers. + int aborted = 0; + final int numberToAbort = 2; + for (int i = 1; i < TEST_UTIL.getMiniHBaseCluster().countServedRegions(); i++) { + HRegionServer hrs = TEST_UTIL.getMiniHBaseCluster().getRegionServer(i); + if (hrs.getServerName().equals(drainingServer.getServerName())) continue; + hrs.abort("Aborting"); + aborted++; + if (aborted >= numberToAbort) break; + } + // Wait for regions to come back on line again. + while (TEST_UTIL.getMiniHBaseCluster().countServedRegions() < COUNT_OF_REGIONS) { + Threads.sleep(100); + } + Assert.assertEquals(regionsOnDrainingServer, + drainingServer.getOnlineRegions().size()); + } +} \ No newline at end of file