diff --git a/hbase-server/src/test/java/org/apache/hadoop/hbase/HBaseTestingUtility.java b/hbase-server/src/test/java/org/apache/hadoop/hbase/HBaseTestingUtility.java index eec7892..54a9d42 100644 --- a/hbase-server/src/test/java/org/apache/hadoop/hbase/HBaseTestingUtility.java +++ b/hbase-server/src/test/java/org/apache/hadoop/hbase/HBaseTestingUtility.java @@ -2508,6 +2508,21 @@ public class HBaseTestingUtility extends HBaseCommonTestingUtility { return rows; } + /* + * Find any other region server which is different from the one identified by parameter + * @param rs + * @return another region server + */ + public HRegionServer getOtherRegionServer(HRegionServer rs) { + for (JVMClusterUtil.RegionServerThread rst : + getMiniHBaseCluster().getRegionServerThreads()) { + if (!(rst.getRegionServer() == rs)) { + return rst.getRegionServer(); + } + } + return null; + } + /** * Tool to get the reference to the region server object that holds the * region of the specified user table. 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 deleted file mode 100644 index 9bd74d0..0000000 --- a/hbase-server/src/test/java/org/apache/hadoop/hbase/regionserver/TestCompactionInDeadRegionServer.java +++ /dev/null @@ -1,159 +0,0 @@ -/** - * 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 java.util.Arrays; -import java.util.List; - -import org.apache.hadoop.conf.Configuration; -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.wal.AsyncFSWALProvider; -import org.apache.hadoop.hbase.wal.FSHLogProvider; -import org.apache.hadoop.hbase.wal.WALFactory; -import org.apache.hadoop.hbase.wal.WALProvider; -import org.apache.hadoop.hbase.zookeeper.ZKWatcher; -import org.apache.hadoop.hbase.zookeeper.ZNodePaths; -import org.junit.After; -import org.junit.Before; -import org.junit.Test; -import org.junit.experimental.categories.Category; -import org.junit.runner.RunWith; -import org.junit.runners.Parameterized; -import org.junit.runners.Parameterized.Parameter; -import org.junit.runners.Parameterized.Parameters; - -/** - * This testcase is used to ensure that the compaction marker will fail a compaction if the RS is - * already dead. It can not eliminate FNFE when scanning but it does reduce the possibility a lot. - */ -@RunWith(Parameterized.class) -@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); - } - - @Override - protected void tryRegionServerReport(long reportStartTime, long reportEndTime) - throws IOException { - try { - super.tryRegionServerReport(reportStartTime, reportEndTime); - } catch (YouAreDeadException e) { - // ignore, do not abort - } - } - } - - @Parameter - public Class walProvider; - - @Parameters(name = "{index}: wal={0}") - public static List params() { - return Arrays.asList(new Object[] { FSHLogProvider.class }, - new Object[] { AsyncFSWALProvider.class }); - } - - @Before - public void setUp() throws Exception { - UTIL.getConfiguration().setClass(WALFactory.WAL_PROVIDER, walProvider, WALProvider.class); - 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); - } - - @After - public void tearDown() throws Exception { - UTIL.shutdownMiniCluster(); - } - - @Test - public void test() throws Exception { - HRegionServer rsToSuspend = UTIL.getRSForFirstRegionInTable(TABLE_NAME); - HRegion region = (HRegion) rsToSuspend.getRegions(TABLE_NAME).get(0); - ZKWatcher watcher = UTIL.getZooKeeperWatcher(); - watcher.getRecoverableZooKeeper().delete( - ZNodePaths.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.getRegions(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))); - } - } -} diff --git a/hbase-server/src/test/java/org/apache/hadoop/hbase/regionserver/TestCompactionInDeadRegionServerWithAsyncWal.java b/hbase-server/src/test/java/org/apache/hadoop/hbase/regionserver/TestCompactionInDeadRegionServerWithAsyncWal.java new file mode 100644 index 0000000..75a1d80 --- /dev/null +++ b/hbase-server/src/test/java/org/apache/hadoop/hbase/regionserver/TestCompactionInDeadRegionServerWithAsyncWal.java @@ -0,0 +1,39 @@ +/** + * 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 org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.apache.hadoop.hbase.testclassification.MediumTests; +import org.apache.hadoop.hbase.testclassification.RegionServerTests; +import org.apache.hadoop.hbase.wal.AsyncFSWALProvider; +import org.apache.hadoop.hbase.wal.WALProvider; +import org.junit.experimental.categories.Category; + +/** + * This testcase is used to ensure that the compaction marker will fail a compaction if the RS is + * already dead. It can not eliminate FNFE when scanning but it does reduce the possibility a lot. + */ +@Category({ RegionServerTests.class, MediumTests.class }) +public class TestCompactionInDeadRegionServerWithAsyncWal extends TestCompactionInDeadRegionServerWithSyncWal { + private static final Log LOG = LogFactory.getLog(TestCompactionInDeadRegionServerWithAsyncWal.class); + + protected Class getWALProvider() { + return AsyncFSWALProvider.class; + } +} diff --git a/hbase-server/src/test/java/org/apache/hadoop/hbase/regionserver/TestCompactionInDeadRegionServerWithSyncWal.java b/hbase-server/src/test/java/org/apache/hadoop/hbase/regionserver/TestCompactionInDeadRegionServerWithSyncWal.java new file mode 100644 index 0000000..10f297c --- /dev/null +++ b/hbase-server/src/test/java/org/apache/hadoop/hbase/regionserver/TestCompactionInDeadRegionServerWithSyncWal.java @@ -0,0 +1,173 @@ +/** + * 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 java.util.Arrays; +import java.util.List; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.apache.hadoop.conf.Configuration; +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.regionserver.wal.DamagedWALException; +import org.apache.hadoop.hbase.testclassification.MediumTests; +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.wal.AsyncFSWALProvider; +import org.apache.hadoop.hbase.wal.FSHLogProvider; +import org.apache.hadoop.hbase.wal.WALFactory; +import org.apache.hadoop.hbase.wal.WALProvider; +import org.apache.hadoop.hbase.zookeeper.ZKWatcher; +import org.apache.hadoop.hbase.zookeeper.ZNodePaths; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.junit.experimental.categories.Category; +import org.junit.runner.RunWith; +import org.junit.runners.Parameterized; +import org.junit.runners.Parameterized.Parameter; +import org.junit.runners.Parameterized.Parameters; + +/** + * This testcase is used to ensure that the compaction marker will fail a compaction if the RS is + * already dead. It can not eliminate FNFE when scanning but it does reduce the possibility a lot. + */ +@Category({ RegionServerTests.class, MediumTests.class }) +public class TestCompactionInDeadRegionServerWithSyncWal { + private static final Log LOG = LogFactory.getLog(TestCompactionInDeadRegionServerWithSyncWal.class); + + 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); + } + + @Override + protected void tryRegionServerReport(long reportStartTime, long reportEndTime) + throws IOException { + try { + super.tryRegionServerReport(reportStartTime, reportEndTime); + } catch (YouAreDeadException e) { + // ignore, do not abort + } + } + } + + protected Class getWALProvider() { + return FSHLogProvider.class; + } + public Class walProvider = getWALProvider(); + + @Before + public void setUp() throws Exception { + UTIL.getConfiguration().setClass(WALFactory.WAL_PROVIDER, walProvider, WALProvider.class); + 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); + } + + @After + public void tearDown() throws Exception { + UTIL.shutdownMiniCluster(); + } + + @Test + public void test() throws Exception { + HRegionServer regionSvr = UTIL.getRSForFirstRegionInTable(TABLE_NAME); + HRegion region = (HRegion) regionSvr.getRegions(TABLE_NAME).get(0); + String regName = region.getRegionInfo().getEncodedName(); + List metaRegs = regionSvr.getRegions(TableName.META_TABLE_NAME); + if (metaRegs != null && !metaRegs.isEmpty()) { + LOG.info("meta is on the same server: " + regionSvr); + // when region is on same server as hbase:meta, writes would be disabled when + // it is opened on new server. + // so it is moved to a different server + HRegionServer otherRs = UTIL.getOtherRegionServer(regionSvr); + UTIL.moveRegionAndWait(region.getRegionInfo(), otherRs.getServerName()); + LOG.info("Moved region: " + regName + " to " + otherRs.getServerName()); + } + HRegionServer rsToSuspend = UTIL.getRSForFirstRegionInTable(TABLE_NAME); + region = (HRegion) rsToSuspend.getRegions(TABLE_NAME).get(0); + + ZKWatcher watcher = UTIL.getZooKeeperWatcher(); + watcher.getRecoverableZooKeeper().delete( + ZNodePaths.joinZNode(watcher.getZNodePaths().rsZNode, rsToSuspend.getServerName().toString()), + -1); + LOG.info("suspending " + rsToSuspend); + 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.getRegions(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) { + LOG.debug("expected exception: ", e); + } + 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))); + } + } +}