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