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