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.master.region.MasterRegion;
039import org.apache.hadoop.hbase.testclassification.MasterTests;
040import org.apache.hadoop.hbase.testclassification.MediumTests;
041import org.apache.hadoop.hbase.util.Bytes;
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, MasterRegion masterRegion) {
063      super(master, masterRegion);
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 {
078      super(conf);
079    }
080
081    @Override
082    protected AssignmentManager createAssignmentManager(MasterServices master,
083      MasterRegion masterRegion) {
084      return new AssignmentManagerForTest(master, masterRegion);
085    }
086  }
087
088  private static final HBaseTestingUtility UTIL = new HBaseTestingUtility();
089
090  private static TableName NAME = TableName.valueOf("Open");
091
092  private static byte[] CF = Bytes.toBytes("cf");
093
094  @BeforeClass
095  public static void setUp() throws Exception {
096    Configuration conf = UTIL.getConfiguration();
097    conf.setClass(HConstants.MASTER_IMPL, HMasterForTest.class, HMaster.class);
098    UTIL.startMiniCluster(1);
099    UTIL.waitFor(10000, () -> {
100      try (
101        RegionLocator locator = UTIL.getConnection().getRegionLocator(TableName.META_TABLE_NAME)) {
102        return locator.getRegionLocation(HConstants.EMPTY_START_ROW) != null;
103      } catch (Exception e) {
104        return false;
105      }
106    });
107  }
108
109  @AfterClass
110  public static void tearDown() throws Exception {
111    UTIL.shutdownMiniCluster();
112  }
113
114  private void assertBackoffIncrease() throws IOException, InterruptedException {
115    ProcedureTestUtil.waitUntilProcedureWaitingTimeout(UTIL, OpenRegionProcedure.class, 30000);
116    ProcedureTestUtil.waitUntilProcedureTimeoutIncrease(UTIL, OpenRegionProcedure.class, 2);
117  }
118
119  @Test
120  public void testBackoff() throws IOException, InterruptedException, ExecutionException {
121    FAIL = true;
122    try (AsyncConnection conn =
123      ConnectionFactory.createAsyncConnection(UTIL.getConfiguration()).get()) {
124      AsyncAdmin admin = conn.getAdminBuilder().setRpcTimeout(5, TimeUnit.MINUTES)
125        .setOperationTimeout(10, TimeUnit.MINUTES).build();
126      CompletableFuture<?> future = admin.createTable(TableDescriptorBuilder.newBuilder(NAME)
127        .setColumnFamily(ColumnFamilyDescriptorBuilder.of(CF)).build());
128      assertBackoffIncrease();
129      FAIL = false;
130      future.get();
131      UTIL.waitTableAvailable(NAME);
132    }
133  }
134}