001/**
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package org.apache.hadoop.hbase.regionserver;
019
020import static org.junit.Assert.assertEquals;
021
022import java.io.IOException;
023import java.util.concurrent.atomic.AtomicInteger;
024import org.apache.hadoop.conf.Configuration;
025import org.apache.hadoop.fs.FileSystem;
026import org.apache.hadoop.fs.Path;
027import org.apache.hadoop.hbase.HBaseClassTestRule;
028import org.apache.hadoop.hbase.HBaseTestingUtility;
029import org.apache.hadoop.hbase.HConstants;
030import org.apache.hadoop.hbase.TableName;
031import org.apache.hadoop.hbase.client.RegionInfo;
032import org.apache.hadoop.hbase.client.TableDescriptor;
033import org.apache.hadoop.hbase.testclassification.MediumTests;
034import org.apache.hadoop.hbase.testclassification.RegionServerTests;
035import org.apache.hadoop.hbase.util.Bytes;
036import org.apache.hadoop.hbase.wal.WAL;
037import org.junit.AfterClass;
038import org.junit.BeforeClass;
039import org.junit.ClassRule;
040import org.junit.Test;
041import org.junit.experimental.categories.Category;
042
043/**
044 * Testcase for HBASE-20242
045 */
046@Category({ RegionServerTests.class, MediumTests.class })
047public class TestOpenSeqNumUnexpectedIncrease {
048
049  @ClassRule
050  public static final HBaseClassTestRule CLASS_RULE =
051    HBaseClassTestRule.forClass(TestOpenSeqNumUnexpectedIncrease.class);
052
053  private static final HBaseTestingUtility UTIL = new HBaseTestingUtility();
054
055  private static AtomicInteger FAILED_OPEN = new AtomicInteger(0);
056
057  private static TableName TABLE_NAME = TableName.valueOf("test");
058
059  private static byte[] CF = Bytes.toBytes("CF");
060
061  public static final class MockHRegion extends HRegion {
062
063    @SuppressWarnings("deprecation")
064    public MockHRegion(Path tableDir, WAL wal, FileSystem fs, Configuration confParam,
065        RegionInfo regionInfo, TableDescriptor htd, RegionServerServices rsServices) {
066      super(tableDir, wal, fs, confParam, regionInfo, htd, rsServices);
067    }
068
069    @Override
070    protected void writeRegionOpenMarker(WAL wal, long openSeqId) throws IOException {
071      if (getRegionInfo().getTable().equals(TABLE_NAME) && FAILED_OPEN.get() > 0) {
072        FAILED_OPEN.decrementAndGet();
073        rsServices.abort("for testing", new Exception("Inject error for testing"));
074        throw new IOException("Inject error for testing");
075      }
076    }
077  }
078
079  @BeforeClass
080  public static void setUp() throws Exception {
081    UTIL.getConfiguration().setInt(HConstants.HBASE_RPC_TIMEOUT_KEY, 600000);
082    UTIL.getConfiguration().setClass(HConstants.REGION_IMPL, MockHRegion.class, HRegion.class);
083    UTIL.startMiniCluster(3);
084    UTIL.createTable(TABLE_NAME, CF);
085    UTIL.getAdmin().balancerSwitch(false, true);
086  }
087
088  @AfterClass
089  public static void tearDown() throws Exception {
090    UTIL.shutdownMiniCluster();
091  }
092
093  @Test
094  public void test() throws IOException, InterruptedException {
095    HRegion region = UTIL.getMiniHBaseCluster().getRegions(TABLE_NAME).get(0);
096    long openSeqNum = region.getOpenSeqNum();
097    HRegionServer src = UTIL.getRSForFirstRegionInTable(TABLE_NAME);
098    HRegionServer dst = UTIL.getOtherRegionServer(src);
099
100    // will fail two times, and then verify that the open sequence number is still openSeqNum + 2
101    FAILED_OPEN.set(2);
102    UTIL.getAdmin().move(region.getRegionInfo().getEncodedNameAsBytes(),
103      Bytes.toBytes(dst.getServerName().getServerName()));
104    UTIL.waitTableAvailable(TABLE_NAME);
105
106    HRegion region1 = UTIL.getMiniHBaseCluster().getRegions(TABLE_NAME).get(0);
107    long openSeqNum1 = region1.getOpenSeqNum();
108
109    assertEquals(openSeqNum + 2, openSeqNum1);
110  }
111}