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