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.master;
019
020import java.io.IOException;
021import org.apache.hadoop.conf.Configuration;
022import org.apache.hadoop.fs.FileSystem;
023import org.apache.hadoop.fs.Path;
024import org.apache.hadoop.hbase.RegionTooBusyException;
025import org.apache.hadoop.hbase.client.Mutation;
026import org.apache.hadoop.hbase.client.RegionInfo;
027import org.apache.hadoop.hbase.client.TableDescriptor;
028import org.apache.hadoop.hbase.master.region.MasterRegionFactory;
029import org.apache.hadoop.hbase.regionserver.HRegion;
030import org.apache.hadoop.hbase.regionserver.HRegionFileSystem;
031import org.apache.hadoop.hbase.regionserver.OperationStatus;
032import org.apache.hadoop.hbase.regionserver.RegionServerServices;
033import org.apache.hadoop.hbase.testclassification.LargeTests;
034import org.apache.hadoop.hbase.testclassification.MasterTests;
035import org.apache.hadoop.hbase.wal.WAL;
036import org.junit.jupiter.api.AfterAll;
037import org.junit.jupiter.api.BeforeAll;
038import org.junit.jupiter.api.Tag;
039
040/**
041 * MasterRegion related test that ensures the operations continue even when Procedure state update
042 * encounters retriable IO errors.
043 */
044@Tag(MasterTests.TAG)
045@Tag(LargeTests.TAG)
046public class TestMasterRegionMutation1 extends AbstractTestMasterRegionMutation {
047
048  @BeforeAll
049  public static void setUpBeforeClass() throws Exception {
050    AbstractTestMasterRegionMutation.setUpBeforeClass(2, TestRegion.class);
051  }
052
053  @AfterAll
054  public static void tearDownAfterClass() throws Exception {
055    AbstractTestMasterRegionMutation.tearDownAfterClass();
056  }
057
058  public static class TestRegion extends HRegion {
059
060    public TestRegion(Path tableDir, WAL wal, FileSystem fs, Configuration confParam,
061      RegionInfo regionInfo, TableDescriptor htd, RegionServerServices rsServices) {
062      super(tableDir, wal, fs, confParam, regionInfo, htd, rsServices);
063    }
064
065    public TestRegion(HRegionFileSystem fs, WAL wal, Configuration confParam, TableDescriptor htd,
066      RegionServerServices rsServices) {
067      super(fs, wal, confParam, htd, rsServices);
068    }
069
070    @Override
071    public OperationStatus[] batchMutate(Mutation[] mutations, boolean atomic, long nonceGroup,
072      long nonce) throws IOException {
073      if (
074        MasterRegionFactory.TABLE_NAME.equals(getTableDescriptor().getTableName())
075          && ERROR_OUT.get()
076      ) {
077        // First time errors are recovered with enough retries
078        if (FIRST_TIME_ERROR.get() && ERROR_COUNTER.getAndIncrement() == 5) {
079          ERROR_OUT.set(false);
080          ERROR_COUNTER.set(0);
081          FIRST_TIME_ERROR.set(false);
082          return super.batchMutate(mutations, atomic, nonceGroup, nonce);
083        }
084        // Second time errors are not recovered with enough retries, leading to master abort
085        if (!FIRST_TIME_ERROR.get() && ERROR_COUNTER.getAndIncrement() == 8) {
086          ERROR_OUT.set(false);
087          ERROR_COUNTER.set(0);
088        }
089        throw new RegionTooBusyException("test error...");
090      }
091      return super.batchMutate(mutations, atomic, nonceGroup, nonce);
092    }
093  }
094
095}