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