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.assignment;
019
020import java.io.IOException;
021import java.util.concurrent.CompletableFuture;
022import java.util.concurrent.ExecutionException;
023import java.util.concurrent.TimeUnit;
024import org.apache.hadoop.conf.Configuration;
025import org.apache.hadoop.hbase.HBaseTestingUtil;
026import org.apache.hadoop.hbase.HConstants;
027import org.apache.hadoop.hbase.ProcedureTestUtil;
028import org.apache.hadoop.hbase.TableName;
029import org.apache.hadoop.hbase.client.AsyncAdmin;
030import org.apache.hadoop.hbase.client.ColumnFamilyDescriptorBuilder;
031import org.apache.hadoop.hbase.client.TableDescriptorBuilder;
032import org.apache.hadoop.hbase.master.HMaster;
033import org.apache.hadoop.hbase.master.MasterServices;
034import org.apache.hadoop.hbase.master.region.MasterRegion;
035import org.apache.hadoop.hbase.testclassification.MasterTests;
036import org.apache.hadoop.hbase.testclassification.MediumTests;
037import org.apache.hadoop.hbase.util.Bytes;
038import org.apache.hadoop.hbase.util.FutureUtils;
039import org.junit.jupiter.api.AfterAll;
040import org.junit.jupiter.api.BeforeAll;
041import org.junit.jupiter.api.Tag;
042import org.junit.jupiter.api.Test;
043
044/**
045 * Testcase for HBASE-23079.
046 */
047@Tag(MasterTests.TAG)
048@Tag(MediumTests.TAG)
049public class TestOpenRegionProcedureBackoff {
050
051  private static volatile boolean FAIL = false;
052
053  private static final class AssignmentManagerForTest extends AssignmentManager {
054
055    public AssignmentManagerForTest(MasterServices master, MasterRegion masterRegion) {
056      super(master, masterRegion);
057    }
058
059    @Override
060    CompletableFuture<Void> persistToMeta(RegionStateNode regionNode) {
061      if (FAIL) {
062        return FutureUtils.failedFuture(new IOException("Inject Error!"));
063      }
064      return super.persistToMeta(regionNode);
065    }
066  }
067
068  public static final class HMasterForTest extends HMaster {
069
070    public HMasterForTest(Configuration conf) throws IOException {
071      super(conf);
072    }
073
074    @Override
075    protected AssignmentManager createAssignmentManager(MasterServices master,
076      MasterRegion masterRegion) {
077      return new AssignmentManagerForTest(master, masterRegion);
078    }
079  }
080
081  private static final HBaseTestingUtil UTIL = new HBaseTestingUtil();
082
083  private static TableName NAME = TableName.valueOf("Open");
084
085  private static byte[] CF = Bytes.toBytes("cf");
086
087  @BeforeAll
088  public static void setUp() throws Exception {
089    Configuration conf = UTIL.getConfiguration();
090    conf.setClass(HConstants.MASTER_IMPL, HMasterForTest.class, HMaster.class);
091    UTIL.startMiniCluster(1);
092    UTIL.waitTableAvailable(TableName.META_TABLE_NAME);
093  }
094
095  @AfterAll
096  public static void tearDown() throws Exception {
097    UTIL.shutdownMiniCluster();
098  }
099
100  private void assertBackoffIncrease() throws IOException, InterruptedException {
101    ProcedureTestUtil.waitUntilProcedureWaitingTimeout(UTIL, OpenRegionProcedure.class, 30000);
102    ProcedureTestUtil.waitUntilProcedureTimeoutIncrease(UTIL, OpenRegionProcedure.class, 2);
103  }
104
105  @Test
106  public void testBackoff() throws IOException, InterruptedException, ExecutionException {
107    FAIL = true;
108    AsyncAdmin admin = UTIL.getAsyncConnection().getAdminBuilder()
109      .setRpcTimeout(5, TimeUnit.MINUTES).setOperationTimeout(10, TimeUnit.MINUTES).build();
110    CompletableFuture<?> future = admin.createTable(TableDescriptorBuilder.newBuilder(NAME)
111      .setColumnFamily(ColumnFamilyDescriptorBuilder.of(CF)).build());
112    assertBackoffIncrease();
113    FAIL = false;
114    future.get();
115    UTIL.waitTableAvailable(NAME);
116  }
117}