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