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
.AsyncConnectionConfiguration
.START_LOG_ERRORS_AFTER_COUNT_KEY
;
22 import java
.io
.IOException
;
23 import java
.util
.Arrays
;
24 import java
.util
.List
;
25 import java
.util
.concurrent
.CompletableFuture
;
26 import java
.util
.concurrent
.ForkJoinPool
;
27 import java
.util
.function
.Supplier
;
28 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
.StartMiniClusterOption
;
33 import org
.apache
.hadoop
.hbase
.TableName
;
34 import org
.apache
.hadoop
.hbase
.util
.Bytes
;
35 import org
.junit
.After
;
36 import org
.junit
.AfterClass
;
37 import org
.junit
.Before
;
38 import org
.junit
.BeforeClass
;
39 import org
.junit
.Rule
;
40 import org
.junit
.rules
.TestName
;
41 import org
.junit
.runners
.Parameterized
.Parameter
;
42 import org
.junit
.runners
.Parameterized
.Parameters
;
43 import org
.slf4j
.Logger
;
44 import org
.slf4j
.LoggerFactory
;
47 * Class to test AsyncAdmin.
49 public abstract class TestAsyncAdminBase
{
51 protected static final Logger LOG
= LoggerFactory
.getLogger(TestAsyncAdminBase
.class);
52 protected final static HBaseTestingUtility TEST_UTIL
= new HBaseTestingUtility();
53 protected static final byte[] FAMILY
= Bytes
.toBytes("testFamily");
54 protected static final byte[] FAMILY_0
= Bytes
.toBytes("cf0");
55 protected static final byte[] FAMILY_1
= Bytes
.toBytes("cf1");
57 protected static AsyncConnection ASYNC_CONN
;
58 protected AsyncAdmin admin
;
61 public Supplier
<AsyncAdmin
> getAdmin
;
63 private static AsyncAdmin
getRawAsyncAdmin() {
64 return ASYNC_CONN
.getAdmin();
67 private static AsyncAdmin
getAsyncAdmin() {
68 return ASYNC_CONN
.getAdmin(ForkJoinPool
.commonPool());
72 public static List
<Object
[]> params() {
73 return Arrays
.asList(new Supplier
<?
>[] { TestAsyncAdminBase
::getRawAsyncAdmin
},
74 new Supplier
<?
>[] { TestAsyncAdminBase
::getAsyncAdmin
});
78 public TestName testName
= new TestName();
79 protected TableName tableName
;
82 public static void setUpBeforeClass() throws Exception
{
83 TEST_UTIL
.getConfiguration().setInt(HConstants
.HBASE_RPC_TIMEOUT_KEY
, 60000);
84 TEST_UTIL
.getConfiguration().setInt(HConstants
.HBASE_CLIENT_OPERATION_TIMEOUT
, 120000);
85 TEST_UTIL
.getConfiguration().setInt(HConstants
.HBASE_CLIENT_RETRIES_NUMBER
, 2);
86 TEST_UTIL
.getConfiguration().setInt(START_LOG_ERRORS_AFTER_COUNT_KEY
, 0);
87 StartMiniClusterOption option
= StartMiniClusterOption
.builder().numRegionServers(2).
88 numMasters(2).build();
89 TEST_UTIL
.startMiniCluster(option
);
90 ASYNC_CONN
= ConnectionFactory
.createAsyncConnection(TEST_UTIL
.getConfiguration()).get();
94 public static void tearDownAfterClass() throws Exception
{
95 IOUtils
.closeQuietly(ASYNC_CONN
);
96 TEST_UTIL
.shutdownMiniCluster();
100 public void setUp() throws Exception
{
101 admin
= getAdmin
.get();
102 String methodName
= testName
.getMethodName();
103 tableName
= TableName
.valueOf(methodName
.substring(0, methodName
.length() - 3));
107 public void tearDown() throws Exception
{
108 admin
.listTableNames(Pattern
.compile(tableName
.getNameAsString() + ".*"), false)
109 .whenCompleteAsync((tables
, err
) -> {
110 if (tables
!= null) {
111 tables
.forEach(table
-> {
113 admin
.disableTable(table
).join();
114 } catch (Exception e
) {
115 LOG
.debug("Table: " + tableName
+ " already disabled, so just deleting it.");
117 admin
.deleteTable(table
).join();
120 }, ForkJoinPool
.commonPool()).join();
121 if (!admin
.isBalancerEnabled().join()) {
122 admin
.balancerSwitch(true, true);
126 protected void createTableWithDefaultConf(TableName tableName
) throws IOException
{
127 createTableWithDefaultConf(tableName
, null);
130 protected void createTableWithDefaultConf(TableName tableName
, int regionReplication
)
132 createTableWithDefaultConf(tableName
, regionReplication
, null, FAMILY
);
135 protected void createTableWithDefaultConf(TableName tableName
, byte[][] splitKeys
)
137 createTableWithDefaultConf(tableName
, splitKeys
, FAMILY
);
140 protected void createTableWithDefaultConf(TableName tableName
, int regionReplication
,
141 byte[][] splitKeys
) throws IOException
{
142 createTableWithDefaultConf(tableName
, regionReplication
, splitKeys
, FAMILY
);
145 protected void createTableWithDefaultConf(TableName tableName
, byte[][] splitKeys
,
146 byte[]... families
) throws IOException
{
147 createTableWithDefaultConf(tableName
, 1, splitKeys
, families
);
150 protected void createTableWithDefaultConf(TableName tableName
, int regionReplication
,
151 byte[][] splitKeys
, byte[]... families
) throws IOException
{
152 TableDescriptorBuilder builder
=
153 TableDescriptorBuilder
.newBuilder(tableName
).setRegionReplication(regionReplication
);
154 for (byte[] family
: families
) {
155 builder
.setColumnFamily(ColumnFamilyDescriptorBuilder
.of(family
));
157 CompletableFuture
<Void
> future
= splitKeys
== null ? admin
.createTable(builder
.build())
158 : admin
.createTable(builder
.build(), splitKeys
);
160 TEST_UTIL
.waitUntilAllRegionsAssigned(tableName
);