2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
18 package org
.apache
.hadoop
.hbase
.client
;
20 import static org
.apache
.hadoop
.hbase
.client
.AsyncProcess
.START_LOG_ERRORS_AFTER_COUNT_KEY
;
22 import java
.util
.Arrays
;
23 import java
.util
.List
;
24 import java
.util
.concurrent
.CompletableFuture
;
25 import java
.util
.concurrent
.ForkJoinPool
;
26 import java
.util
.function
.Supplier
;
27 import java
.util
.regex
.Pattern
;
29 import org
.apache
.commons
.io
.IOUtils
;
30 import org
.apache
.hadoop
.hbase
.HBaseTestingUtility
;
31 import org
.apache
.hadoop
.hbase
.HConstants
;
32 import org
.apache
.hadoop
.hbase
.TableName
;
33 import org
.apache
.hadoop
.hbase
.util
.Bytes
;
34 import org
.junit
.After
;
35 import org
.junit
.AfterClass
;
36 import org
.junit
.Before
;
37 import org
.junit
.BeforeClass
;
38 import org
.junit
.Rule
;
39 import org
.junit
.rules
.TestName
;
40 import org
.junit
.runners
.Parameterized
.Parameter
;
41 import org
.junit
.runners
.Parameterized
.Parameters
;
42 import org
.slf4j
.Logger
;
43 import org
.slf4j
.LoggerFactory
;
46 * Class to test AsyncAdmin.
48 public abstract class TestAsyncAdminBase
{
50 protected static final Logger LOG
= LoggerFactory
.getLogger(TestAsyncAdminBase
.class);
51 protected final static HBaseTestingUtility TEST_UTIL
= new HBaseTestingUtility();
52 protected static final byte[] FAMILY
= Bytes
.toBytes("testFamily");
53 protected static final byte[] FAMILY_0
= Bytes
.toBytes("cf0");
54 protected static final byte[] FAMILY_1
= Bytes
.toBytes("cf1");
56 protected static AsyncConnection ASYNC_CONN
;
57 protected AsyncAdmin admin
;
60 public Supplier
<AsyncAdmin
> getAdmin
;
62 private static AsyncAdmin
getRawAsyncAdmin() {
63 return ASYNC_CONN
.getAdmin();
66 private static AsyncAdmin
getAsyncAdmin() {
67 return ASYNC_CONN
.getAdmin(ForkJoinPool
.commonPool());
71 public static List
<Object
[]> params() {
72 return Arrays
.asList(new Supplier
<?
>[] { TestAsyncAdminBase
::getRawAsyncAdmin
},
73 new Supplier
<?
>[] { TestAsyncAdminBase
::getAsyncAdmin
});
77 public TestName testName
= new TestName();
78 protected TableName tableName
;
81 public static void setUpBeforeClass() throws Exception
{
82 TEST_UTIL
.getConfiguration().setInt(HConstants
.HBASE_RPC_TIMEOUT_KEY
, 60000);
83 TEST_UTIL
.getConfiguration().setInt(HConstants
.HBASE_CLIENT_OPERATION_TIMEOUT
, 120000);
84 TEST_UTIL
.getConfiguration().setInt(HConstants
.HBASE_CLIENT_RETRIES_NUMBER
, 2);
85 TEST_UTIL
.getConfiguration().setInt(START_LOG_ERRORS_AFTER_COUNT_KEY
, 0);
86 TEST_UTIL
.startMiniCluster(2);
87 ASYNC_CONN
= ConnectionFactory
.createAsyncConnection(TEST_UTIL
.getConfiguration()).get();
91 public static void tearDownAfterClass() throws Exception
{
92 IOUtils
.closeQuietly(ASYNC_CONN
);
93 TEST_UTIL
.shutdownMiniCluster();
97 public void setUp() throws Exception
{
98 admin
= getAdmin
.get();
99 String methodName
= testName
.getMethodName();
100 tableName
= TableName
.valueOf(methodName
.substring(0, methodName
.length() - 3));
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
-> {
110 admin
.disableTable(table
).join();
111 } catch (Exception e
) {
112 LOG
.debug("Table: " + tableName
+ " already disabled, so just deleting it.");
114 admin
.deleteTable(table
).join();
117 }, ForkJoinPool
.commonPool()).join();
120 protected void createTableWithDefaultConf(TableName tableName
) {
121 createTableWithDefaultConf(tableName
, null);
124 protected void createTableWithDefaultConf(TableName tableName
, byte[][] splitKeys
) {
125 createTableWithDefaultConf(tableName
, splitKeys
, FAMILY
);
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
));
134 CompletableFuture
<Void
> future
= splitKeys
== null ? admin
.createTable(builder
.build())
135 : admin
.createTable(builder
.build(), splitKeys
);