From e213f5fcf81cac3b1b171d18a58f277e456fc48b Mon Sep 17 00:00:00 2001 From: zhangduo Date: Fri, 3 Mar 2017 22:39:30 +0800 Subject: [PATCH] HBASE-17712 add new ut --- .../TestCompactionInDeadRegionServer.java | 148 +++++++++++++++++++++ 1 file changed, 148 insertions(+) create mode 100644 hbase-server/src/test/java/org/apache/hadoop/hbase/regionserver/TestCompactionInDeadRegionServer.java diff --git a/hbase-server/src/test/java/org/apache/hadoop/hbase/regionserver/TestCompactionInDeadRegionServer.java b/hbase-server/src/test/java/org/apache/hadoop/hbase/regionserver/TestCompactionInDeadRegionServer.java new file mode 100644 index 0000000..9bc3ab4 --- /dev/null +++ b/hbase-server/src/test/java/org/apache/hadoop/hbase/regionserver/TestCompactionInDeadRegionServer.java @@ -0,0 +1,148 @@ +/** + * 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.regionserver; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.fail; + +import java.io.IOException; + +import org.apache.hadoop.conf.Configuration; +import org.apache.hadoop.hbase.CoordinatedStateManager; +import org.apache.hadoop.hbase.HBaseTestingUtility; +import org.apache.hadoop.hbase.HConstants; +import org.apache.hadoop.hbase.TableName; +import org.apache.hadoop.hbase.Waiter.ExplainingPredicate; +import org.apache.hadoop.hbase.YouAreDeadException; +import org.apache.hadoop.hbase.client.Get; +import org.apache.hadoop.hbase.client.Put; +import org.apache.hadoop.hbase.client.Table; +import org.apache.hadoop.hbase.testclassification.LargeTests; +import org.apache.hadoop.hbase.testclassification.RegionServerTests; +import org.apache.hadoop.hbase.util.Bytes; +import org.apache.hadoop.hbase.util.JVMClusterUtil.RegionServerThread; +import org.apache.hadoop.hbase.zookeeper.ZKUtil; +import org.apache.hadoop.hbase.zookeeper.ZooKeeperWatcher; +import org.junit.AfterClass; +import org.junit.BeforeClass; +import org.junit.Test; +import org.junit.experimental.categories.Category; + +/** + * In the old time, when we hit a long STW GC when doing compaction, the RS will be marked as dead + * and region will be reassigned to another RS. But when the GC is finished, the RS may complete the + * compaction thus remove some store files and add a new one before crashing. This will cause + * FileNotFoundException in the new RS. This testcase is used to ensure that this will not happen + * now as we will write out a compaction marker to WAL before removing storefiles. And a dead RS can + * not write out any WAL edits and the compaction will fail. + */ +@Category({ RegionServerTests.class, LargeTests.class }) +public class TestCompactionInDeadRegionServer { + + private static final HBaseTestingUtility UTIL = new HBaseTestingUtility(); + + private static final TableName TABLE_NAME = TableName.valueOf("test"); + + private static final byte[] CF = Bytes.toBytes("cf"); + + private static final byte[] CQ = Bytes.toBytes("cq"); + + public static final class IgnoreYouAreDeadRS extends HRegionServer { + + public IgnoreYouAreDeadRS(Configuration conf) throws IOException, InterruptedException { + super(conf); + } + + public IgnoreYouAreDeadRS(Configuration conf, CoordinatedStateManager csm) throws IOException { + super(conf, csm); + } + + @Override + protected void tryRegionServerReport(long reportStartTime, long reportEndTime) + throws IOException { + try { + super.tryRegionServerReport(reportStartTime, reportEndTime); + } catch (YouAreDeadException e) { + // ignore, do not abort + } + } + + } + + @BeforeClass + public static void setUp() throws Exception { + UTIL.getConfiguration().setInt(HConstants.ZK_SESSION_TIMEOUT, 2000); + UTIL.getConfiguration().setClass(HConstants.REGION_SERVER_IMPL, IgnoreYouAreDeadRS.class, + HRegionServer.class); + UTIL.startMiniCluster(2); + Table table = UTIL.createTable(TABLE_NAME, CF); + for (int i = 0; i < 10; i++) { + table.put(new Put(Bytes.toBytes(i)).addColumn(CF, CQ, Bytes.toBytes(i))); + } + UTIL.getAdmin().flush(TABLE_NAME); + for (int i = 10; i < 20; i++) { + table.put(new Put(Bytes.toBytes(i)).addColumn(CF, CQ, Bytes.toBytes(i))); + } + UTIL.getAdmin().flush(TABLE_NAME); + } + + @AfterClass + public static void tearDown() throws Exception { + UTIL.shutdownMiniCluster(); + } + + @Test + public void test() throws Exception { + HRegionServer rsToSuspend = UTIL.getRSForFirstRegionInTable(TABLE_NAME); + HRegion region = (HRegion) rsToSuspend.getOnlineRegions(TABLE_NAME).get(0); + ZooKeeperWatcher watcher = UTIL.getZooKeeperWatcher(); + watcher.getRecoverableZooKeeper().delete( + ZKUtil.joinZNode(watcher.getZNodePaths().rsZNode, rsToSuspend.getServerName().toString()), + -1); + UTIL.waitFor(60000, 1000, new ExplainingPredicate() { + + @Override + public boolean evaluate() throws Exception { + for (RegionServerThread thread : UTIL.getHBaseCluster().getRegionServerThreads()) { + HRegionServer rs = thread.getRegionServer(); + if (rs != rsToSuspend) { + return !rs.getOnlineRegions(TABLE_NAME).isEmpty(); + } + } + return false; + } + + @Override + public String explainFailure() throws Exception { + return "The region for " + TABLE_NAME + " is still on " + rsToSuspend.getServerName(); + } + }); + try { + region.compact(true); + fail("Should fail as our wal file has already been closed, " + + "and walDir has also been renamed"); + } catch (Exception e) { + // expected + } + Table table = UTIL.getConnection().getTable(TABLE_NAME); + // should not hit FNFE + for (int i = 0; i < 20; i++) { + assertEquals(i, Bytes.toInt(table.get(new Get(Bytes.toBytes(i))).getValue(CF, CQ))); + } + } +} -- 1.9.1