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