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.List;
024import java.util.Map;
025import java.util.concurrent.atomic.AtomicInteger;
026import org.apache.hadoop.conf.Configuration;
027import org.apache.hadoop.fs.FileSystem;
028import org.apache.hadoop.fs.Path;
029import org.apache.hadoop.hbase.HBaseClassTestRule;
030import org.apache.hadoop.hbase.HBaseTestingUtil;
031import org.apache.hadoop.hbase.HConstants;
032import org.apache.hadoop.hbase.TableName;
033import org.apache.hadoop.hbase.client.RegionInfo;
034import org.apache.hadoop.hbase.client.TableDescriptor;
035import org.apache.hadoop.hbase.testclassification.MediumTests;
036import org.apache.hadoop.hbase.testclassification.RegionServerTests;
037import org.apache.hadoop.hbase.util.Bytes;
038import org.apache.hadoop.hbase.wal.WAL;
039import org.junit.AfterClass;
040import org.junit.BeforeClass;
041import org.junit.ClassRule;
042import org.junit.Test;
043import org.junit.experimental.categories.Category;
044
045/**
046 * Testcase for HBASE-20242
047 */
048@Category({ RegionServerTests.class, MediumTests.class })
049public class TestOpenSeqNumUnexpectedIncrease {
050
051  @ClassRule
052  public static final HBaseClassTestRule CLASS_RULE =
053    HBaseClassTestRule.forClass(TestOpenSeqNumUnexpectedIncrease.class);
054
055  private static final HBaseTestingUtil UTIL = new HBaseTestingUtil();
056
057  private static AtomicInteger FAILED_OPEN = new AtomicInteger(0);
058
059  private static TableName TABLE_NAME = TableName.valueOf("test");
060
061  private static byte[] CF = Bytes.toBytes("CF");
062
063  public static final class MockHRegion extends HRegion {
064
065    @SuppressWarnings("deprecation")
066    public MockHRegion(Path tableDir, WAL wal, FileSystem fs, Configuration confParam,
067      RegionInfo regionInfo, TableDescriptor htd, RegionServerServices rsServices) {
068      super(tableDir, wal, fs, confParam, regionInfo, htd, rsServices);
069    }
070
071    @Override
072    protected void writeRegionOpenMarker(WAL wal, long openSeqId) throws IOException {
073      if (getRegionInfo().getTable().equals(TABLE_NAME) && FAILED_OPEN.get() > 0) {
074        FAILED_OPEN.decrementAndGet();
075        rsServices.abort("for testing", new Exception("Inject error for testing"));
076        throw new IOException("Inject error for testing");
077      }
078    }
079
080    public Map<byte[], List<HStoreFile>> close() throws IOException {
081      // skip close
082      return null;
083    }
084  }
085
086  @BeforeClass
087  public static void setUp() throws Exception {
088    UTIL.getConfiguration().setInt(HConstants.HBASE_RPC_TIMEOUT_KEY, 600000);
089    UTIL.getConfiguration().setClass(HConstants.REGION_IMPL, MockHRegion.class, HRegion.class);
090    UTIL.startMiniCluster(3);
091    UTIL.createTable(TABLE_NAME, CF);
092    UTIL.getAdmin().balancerSwitch(false, true);
093  }
094
095  @AfterClass
096  public static void tearDown() throws Exception {
097    UTIL.shutdownMiniCluster();
098  }
099
100  @Test
101  public void test() throws IOException, InterruptedException {
102    HRegion region = UTIL.getMiniHBaseCluster().getRegions(TABLE_NAME).get(0);
103    long openSeqNum = region.getOpenSeqNum();
104    HRegionServer src = UTIL.getRSForFirstRegionInTable(TABLE_NAME);
105    HRegionServer dst = UTIL.getOtherRegionServer(src);
106
107    // will fail two times, and then verify that the open sequence number is still openSeqNum + 2
108    FAILED_OPEN.set(2);
109    UTIL.getAdmin().move(region.getRegionInfo().getEncodedNameAsBytes(), dst.getServerName());
110    UTIL.waitTableAvailable(TABLE_NAME);
111
112    HRegion region1 = UTIL.getMiniHBaseCluster().getRegions(TABLE_NAME).get(0);
113    long openSeqNum1 = region1.getOpenSeqNum();
114
115    assertEquals(openSeqNum + 2, openSeqNum1);
116  }
117}