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.client; 019 020import static org.apache.hadoop.hbase.client.AsyncConnectionConfiguration.START_LOG_ERRORS_AFTER_COUNT_KEY; 021 022import java.io.IOException; 023import java.util.concurrent.CompletableFuture; 024import java.util.concurrent.ForkJoinPool; 025import java.util.function.Supplier; 026import java.util.regex.Pattern; 027import java.util.stream.Stream; 028import org.apache.hadoop.hbase.HBaseTestingUtil; 029import org.apache.hadoop.hbase.HConstants; 030import org.apache.hadoop.hbase.StartTestingClusterOption; 031import org.apache.hadoop.hbase.TableName; 032import org.apache.hadoop.hbase.util.Bytes; 033import org.junit.jupiter.api.AfterEach; 034import org.junit.jupiter.api.BeforeEach; 035import org.junit.jupiter.api.TestInfo; 036import org.junit.jupiter.params.provider.Arguments; 037import org.slf4j.Logger; 038import org.slf4j.LoggerFactory; 039 040import org.apache.hbase.thirdparty.com.google.common.io.Closeables; 041 042/** 043 * Class to test AsyncAdmin. 044 */ 045public abstract class TestAsyncAdminBase extends AbstractTestUpdateConfiguration { 046 047 protected static final Logger LOG = LoggerFactory.getLogger(TestAsyncAdminBase.class); 048 protected final static HBaseTestingUtil TEST_UTIL = new HBaseTestingUtil(); 049 protected static final byte[] FAMILY = Bytes.toBytes("testFamily"); 050 protected static final byte[] FAMILY_0 = Bytes.toBytes("cf0"); 051 protected static final byte[] FAMILY_1 = Bytes.toBytes("cf1"); 052 053 protected static AsyncConnection ASYNC_CONN; 054 protected AsyncAdmin admin; 055 056 public Supplier<AsyncAdmin> getAdmin; 057 058 public TestAsyncAdminBase(Supplier<AsyncAdmin> admin) { 059 this.getAdmin = admin; 060 } 061 062 private static AsyncAdmin getRawAsyncAdmin() { 063 return ASYNC_CONN.getAdmin(); 064 } 065 066 private static AsyncAdmin getAsyncAdmin() { 067 return ASYNC_CONN.getAdmin(ForkJoinPool.commonPool()); 068 } 069 070 public static Stream<Arguments> parameters() { 071 return Stream.of(Arguments.of((Supplier<AsyncAdmin>) TestAsyncAdminBase::getRawAsyncAdmin), 072 Arguments.of((Supplier<AsyncAdmin>) TestAsyncAdminBase::getAsyncAdmin)); 073 } 074 075 protected TableName tableName; 076 077 public static void setUpBeforeClass() throws Exception { 078 TEST_UTIL.getConfiguration().setInt(HConstants.HBASE_RPC_TIMEOUT_KEY, 60000); 079 TEST_UTIL.getConfiguration().setInt(HConstants.HBASE_CLIENT_OPERATION_TIMEOUT, 120000); 080 TEST_UTIL.getConfiguration().setInt(HConstants.HBASE_CLIENT_RETRIES_NUMBER, 2); 081 TEST_UTIL.getConfiguration().setInt(START_LOG_ERRORS_AFTER_COUNT_KEY, 0); 082 StartTestingClusterOption option = 083 StartTestingClusterOption.builder().numRegionServers(2).numMasters(2).build(); 084 TEST_UTIL.startMiniCluster(option); 085 ASYNC_CONN = ConnectionFactory.createAsyncConnection(TEST_UTIL.getConfiguration()).get(); 086 } 087 088 public static void tearDownAfterClass() throws Exception { 089 Closeables.close(ASYNC_CONN, true); 090 TEST_UTIL.shutdownMiniCluster(); 091 } 092 093 @BeforeEach 094 public void setUp(TestInfo testInfo) throws Exception { 095 admin = getAdmin.get(); 096 String methodName = testInfo.getTestMethod().get().getName(); 097 tableName = TableName.valueOf(methodName.substring(0, methodName.length() - 3)); 098 } 099 100 @AfterEach 101 public void tearDown() throws Exception { 102 admin.listTableNames(Pattern.compile(tableName.getNameAsString() + ".*"), false) 103 .whenCompleteAsync((tables, err) -> { 104 if (tables != null) { 105 tables.forEach(table -> { 106 try { 107 admin.disableTable(table).join(); 108 } catch (Exception e) { 109 LOG.debug("Table: " + tableName + " already disabled, so just deleting it."); 110 } 111 admin.deleteTable(table).join(); 112 }); 113 } 114 }, ForkJoinPool.commonPool()).join(); 115 if (!admin.isBalancerEnabled().join()) { 116 admin.balancerSwitch(true, true); 117 } 118 } 119 120 protected void createTableWithDefaultConf(TableName tableName) throws IOException { 121 createTableWithDefaultConf(tableName, null); 122 } 123 124 protected void createTableWithDefaultConf(TableName tableName, int regionReplication) 125 throws IOException { 126 createTableWithDefaultConf(tableName, regionReplication, null, FAMILY); 127 } 128 129 protected void createTableWithDefaultConf(TableName tableName, byte[][] splitKeys) 130 throws IOException { 131 createTableWithDefaultConf(tableName, splitKeys, FAMILY); 132 } 133 134 protected void createTableWithDefaultConf(TableName tableName, int regionReplication, 135 byte[][] splitKeys) throws IOException { 136 createTableWithDefaultConf(tableName, regionReplication, splitKeys, FAMILY); 137 } 138 139 protected void createTableWithDefaultConf(TableName tableName, byte[][] splitKeys, 140 byte[]... families) throws IOException { 141 createTableWithDefaultConf(tableName, 1, splitKeys, families); 142 } 143 144 protected void createTableWithDefaultConf(TableName tableName, int regionReplication, 145 byte[][] splitKeys, byte[]... families) throws IOException { 146 TableDescriptorBuilder builder = 147 TableDescriptorBuilder.newBuilder(tableName).setRegionReplication(regionReplication); 148 for (byte[] family : families) { 149 builder.setColumnFamily(ColumnFamilyDescriptorBuilder.of(family)); 150 } 151 CompletableFuture<Void> future = splitKeys == null 152 ? admin.createTable(builder.build()) 153 : admin.createTable(builder.build(), splitKeys); 154 future.join(); 155 TEST_UTIL.waitUntilAllRegionsAssigned(tableName); 156 } 157}