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.AsyncProcess.START_LOG_ERRORS_AFTER_COUNT_KEY; 021 022import java.util.Arrays; 023import java.util.List; 024import java.util.concurrent.CompletableFuture; 025import java.util.concurrent.ForkJoinPool; 026import java.util.function.Supplier; 027import java.util.regex.Pattern; 028 029import org.apache.commons.io.IOUtils; 030import org.apache.hadoop.hbase.HBaseTestingUtility; 031import org.apache.hadoop.hbase.HConstants; 032import org.apache.hadoop.hbase.TableName; 033import org.apache.hadoop.hbase.util.Bytes; 034import org.junit.After; 035import org.junit.AfterClass; 036import org.junit.Before; 037import org.junit.BeforeClass; 038import org.junit.Rule; 039import org.junit.rules.TestName; 040import org.junit.runners.Parameterized.Parameter; 041import org.junit.runners.Parameterized.Parameters; 042import org.slf4j.Logger; 043import org.slf4j.LoggerFactory; 044 045/** 046 * Class to test AsyncAdmin. 047 */ 048public abstract class TestAsyncAdminBase { 049 050 protected static final Logger LOG = LoggerFactory.getLogger(TestAsyncAdminBase.class); 051 protected final static HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility(); 052 protected static final byte[] FAMILY = Bytes.toBytes("testFamily"); 053 protected static final byte[] FAMILY_0 = Bytes.toBytes("cf0"); 054 protected static final byte[] FAMILY_1 = Bytes.toBytes("cf1"); 055 056 protected static AsyncConnection ASYNC_CONN; 057 protected AsyncAdmin admin; 058 059 @Parameter 060 public Supplier<AsyncAdmin> getAdmin; 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 @Parameters 071 public static List<Object[]> params() { 072 return Arrays.asList(new Supplier<?>[] { TestAsyncAdminBase::getRawAsyncAdmin }, 073 new Supplier<?>[] { TestAsyncAdminBase::getAsyncAdmin }); 074 } 075 076 @Rule 077 public TestName testName = new TestName(); 078 protected TableName tableName; 079 080 @BeforeClass 081 public static void setUpBeforeClass() throws Exception { 082 TEST_UTIL.getConfiguration().setInt(HConstants.HBASE_RPC_TIMEOUT_KEY, 60000); 083 TEST_UTIL.getConfiguration().setInt(HConstants.HBASE_CLIENT_OPERATION_TIMEOUT, 120000); 084 TEST_UTIL.getConfiguration().setInt(HConstants.HBASE_CLIENT_RETRIES_NUMBER, 2); 085 TEST_UTIL.getConfiguration().setInt(START_LOG_ERRORS_AFTER_COUNT_KEY, 0); 086 TEST_UTIL.startMiniCluster(2); 087 ASYNC_CONN = ConnectionFactory.createAsyncConnection(TEST_UTIL.getConfiguration()).get(); 088 } 089 090 @AfterClass 091 public static void tearDownAfterClass() throws Exception { 092 IOUtils.closeQuietly(ASYNC_CONN); 093 TEST_UTIL.shutdownMiniCluster(); 094 } 095 096 @Before 097 public void setUp() throws Exception { 098 admin = getAdmin.get(); 099 String methodName = testName.getMethodName(); 100 tableName = TableName.valueOf(methodName.substring(0, methodName.length() - 3)); 101 } 102 103 @After 104 public void tearDown() throws Exception { 105 admin.listTableNames(Pattern.compile(tableName.getNameAsString() + ".*"), false) 106 .whenCompleteAsync((tables, err) -> { 107 if (tables != null) { 108 tables.forEach(table -> { 109 try { 110 admin.disableTable(table).join(); 111 } catch (Exception e) { 112 LOG.debug("Table: " + tableName + " already disabled, so just deleting it."); 113 } 114 admin.deleteTable(table).join(); 115 }); 116 } 117 }, ForkJoinPool.commonPool()).join(); 118 } 119 120 protected void createTableWithDefaultConf(TableName tableName) { 121 createTableWithDefaultConf(tableName, null); 122 } 123 124 protected void createTableWithDefaultConf(TableName tableName, byte[][] splitKeys) { 125 createTableWithDefaultConf(tableName, splitKeys, FAMILY); 126 } 127 128 protected void createTableWithDefaultConf(TableName tableName, byte[][] splitKeys, 129 byte[]... families) { 130 TableDescriptorBuilder builder = TableDescriptorBuilder.newBuilder(tableName); 131 for (byte[] family : families) { 132 builder.setColumnFamily(ColumnFamilyDescriptorBuilder.of(family)); 133 } 134 CompletableFuture<Void> future = splitKeys == null ? admin.createTable(builder.build()) 135 : admin.createTable(builder.build(), splitKeys); 136 future.join(); 137 } 138}