Skip to content

Commit

Permalink
HBASE-24904 Speed up some unit tests (#2276)
Browse files Browse the repository at this point in the history
Split TestAsyncTableAdminApi/TestAdminShell/TestLoadIncrementalHFiles

Reduce region numbers in TestSnapshotTemporaryDirectoryWithRegionReplicas/TestRegionReplicaFailover/TestSCP*

Signed-off-by: meiyi <myimeiyi@gmail.com>
Signed-off-by: stack <stack@apache.org>
  • Loading branch information
infraio authored Aug 21, 2020
1 parent a2264e9 commit a4368a7
Show file tree
Hide file tree
Showing 13 changed files with 932 additions and 736 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,21 +22,21 @@
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.CompletionException;

import org.apache.hadoop.hbase.AsyncMetaTableAccessor;
import org.apache.hadoop.hbase.HBaseClassTestRule;
import org.apache.hadoop.hbase.HConstants;
import org.apache.hadoop.hbase.HRegionLocation;
import org.apache.hadoop.hbase.ServerName;
import org.apache.hadoop.hbase.TableExistsException;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.TableNotFoundException;
import org.apache.hadoop.hbase.master.LoadBalancer;
import org.apache.hadoop.hbase.testclassification.ClientTests;
import org.apache.hadoop.hbase.testclassification.LargeTests;
Expand All @@ -52,6 +52,7 @@
* @see TestAsyncTableAdminApi2 This test and it used to be joined it was taking longer than our
* ten minute timeout so they were split.
* @see TestAsyncTableAdminApi3 Another split out from this class so each runs under ten minutes.
* @see TestAsyncTableAdminApi4 Another split out from this class so each runs under ten minutes.
*/
@RunWith(Parameterized.class)
@Category({ LargeTests.class, ClientTests.class })
Expand Down Expand Up @@ -361,96 +362,4 @@ private void testTruncateTable(final TableName tableName, boolean preserveSplits
assertEquals(1, TEST_UTIL.getHBaseCluster().getRegions(tableName).size());
}
}

@Test
public void testCloneTableSchema() throws Exception {
final TableName newTableName = TableName.valueOf(tableName.getNameAsString() + "_new");
testCloneTableSchema(tableName, newTableName, false);
}

@Test
public void testCloneTableSchemaPreservingSplits() throws Exception {
final TableName newTableName = TableName.valueOf(tableName.getNameAsString() + "_new");
testCloneTableSchema(tableName, newTableName, true);
}

private void testCloneTableSchema(final TableName tableName,
final TableName newTableName, boolean preserveSplits) throws Exception {
byte[][] splitKeys = new byte[2][];
splitKeys[0] = Bytes.toBytes(4);
splitKeys[1] = Bytes.toBytes(8);
int NUM_FAMILYS = 2;
int NUM_REGIONS = 3;
int BLOCK_SIZE = 1024;
int TTL = 86400;
boolean BLOCK_CACHE = false;

// Create the table
TableDescriptor tableDesc = TableDescriptorBuilder
.newBuilder(tableName)
.setColumnFamily(ColumnFamilyDescriptorBuilder.of(FAMILY_0))
.setColumnFamily(ColumnFamilyDescriptorBuilder
.newBuilder(FAMILY_1)
.setBlocksize(BLOCK_SIZE)
.setBlockCacheEnabled(BLOCK_CACHE)
.setTimeToLive(TTL)
.build()).build();
admin.createTable(tableDesc, splitKeys).join();

assertEquals(NUM_REGIONS, TEST_UTIL.getHBaseCluster().getRegions(tableName).size());
assertTrue("Table should be created with splitKyes + 1 rows in META",
admin.isTableAvailable(tableName, splitKeys).get());

// Clone & Verify
admin.cloneTableSchema(tableName, newTableName, preserveSplits).join();
TableDescriptor newTableDesc = admin.getDescriptor(newTableName).get();

assertEquals(NUM_FAMILYS, newTableDesc.getColumnFamilyCount());
assertEquals(BLOCK_SIZE, newTableDesc.getColumnFamily(FAMILY_1).getBlocksize());
assertEquals(BLOCK_CACHE, newTableDesc.getColumnFamily(FAMILY_1).isBlockCacheEnabled());
assertEquals(TTL, newTableDesc.getColumnFamily(FAMILY_1).getTimeToLive());
TEST_UTIL.verifyTableDescriptorIgnoreTableName(tableDesc, newTableDesc);

if (preserveSplits) {
assertEquals(NUM_REGIONS, TEST_UTIL.getHBaseCluster().getRegions(newTableName).size());
assertTrue("New table should be created with splitKyes + 1 rows in META",
admin.isTableAvailable(newTableName, splitKeys).get());
} else {
assertEquals(1, TEST_UTIL.getHBaseCluster().getRegions(newTableName).size());
}
}

@Test
public void testCloneTableSchemaWithNonExistentSourceTable() throws Exception {
final TableName newTableName = TableName.valueOf(tableName.getNameAsString() + "_new");
// test for non-existent source table
try {
admin.cloneTableSchema(tableName, newTableName, false).join();
fail("Should have failed when source table doesn't exist.");
} catch (CompletionException e) {
assertTrue(e.getCause() instanceof TableNotFoundException);
}
}

@Test
public void testCloneTableSchemaWithExistentDestinationTable() throws Exception {
final TableName newTableName = TableName.valueOf(tableName.getNameAsString() + "_new");
byte[] FAMILY_0 = Bytes.toBytes("cf0");
TEST_UTIL.createTable(tableName, FAMILY_0);
TEST_UTIL.createTable(newTableName, FAMILY_0);
// test for existent destination table
try {
admin.cloneTableSchema(tableName, newTableName, false).join();
fail("Should have failed when destination table exists.");
} catch (CompletionException e) {
assertTrue(e.getCause() instanceof TableExistsException);
}
}

@Test
public void testIsTableAvailableWithInexistantTable() throws Exception {
final TableName newTableName = TableName.valueOf(tableName.getNameAsString() + "_new");
// test for inexistant table
assertFalse(admin.isTableAvailable(newTableName).get());
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.hadoop.hbase.client;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

import java.util.concurrent.CompletionException;

import org.apache.hadoop.hbase.HBaseClassTestRule;
import org.apache.hadoop.hbase.TableExistsException;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.TableNotFoundException;
import org.apache.hadoop.hbase.testclassification.ClientTests;
import org.apache.hadoop.hbase.testclassification.LargeTests;
import org.apache.hadoop.hbase.util.Bytes;
import org.junit.ClassRule;
import org.junit.Test;
import org.junit.experimental.categories.Category;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;

/**
* Class to test asynchronous table admin operations
* @see TestAsyncTableAdminApi Split from it so each runs under ten minutes.
*/
@RunWith(Parameterized.class)
@Category({ LargeTests.class, ClientTests.class })
public class TestAsyncTableAdminApi4 extends TestAsyncAdminBase {

@ClassRule
public static final HBaseClassTestRule CLASS_RULE =
HBaseClassTestRule.forClass(TestAsyncTableAdminApi4.class);

@Test
public void testCloneTableSchema() throws Exception {
final TableName newTableName = TableName.valueOf(tableName.getNameAsString() + "_new");
testCloneTableSchema(tableName, newTableName, false);
}

@Test
public void testCloneTableSchemaPreservingSplits() throws Exception {
final TableName newTableName = TableName.valueOf(tableName.getNameAsString() + "_new");
testCloneTableSchema(tableName, newTableName, true);
}

private void testCloneTableSchema(final TableName tableName,
final TableName newTableName, boolean preserveSplits) throws Exception {
byte[][] splitKeys = new byte[2][];
splitKeys[0] = Bytes.toBytes(4);
splitKeys[1] = Bytes.toBytes(8);
int NUM_FAMILYS = 2;
int NUM_REGIONS = 3;
int BLOCK_SIZE = 1024;
int TTL = 86400;
boolean BLOCK_CACHE = false;

// Create the table
TableDescriptor tableDesc = TableDescriptorBuilder
.newBuilder(tableName)
.setColumnFamily(ColumnFamilyDescriptorBuilder.of(FAMILY_0))
.setColumnFamily(ColumnFamilyDescriptorBuilder
.newBuilder(FAMILY_1)
.setBlocksize(BLOCK_SIZE)
.setBlockCacheEnabled(BLOCK_CACHE)
.setTimeToLive(TTL)
.build()).build();
admin.createTable(tableDesc, splitKeys).join();

assertEquals(NUM_REGIONS, TEST_UTIL.getHBaseCluster().getRegions(tableName).size());
assertTrue("Table should be created with splitKyes + 1 rows in META",
admin.isTableAvailable(tableName, splitKeys).get());

// Clone & Verify
admin.cloneTableSchema(tableName, newTableName, preserveSplits).join();
TableDescriptor newTableDesc = admin.getDescriptor(newTableName).get();

assertEquals(NUM_FAMILYS, newTableDesc.getColumnFamilyCount());
assertEquals(BLOCK_SIZE, newTableDesc.getColumnFamily(FAMILY_1).getBlocksize());
assertEquals(BLOCK_CACHE, newTableDesc.getColumnFamily(FAMILY_1).isBlockCacheEnabled());
assertEquals(TTL, newTableDesc.getColumnFamily(FAMILY_1).getTimeToLive());
TEST_UTIL.verifyTableDescriptorIgnoreTableName(tableDesc, newTableDesc);

if (preserveSplits) {
assertEquals(NUM_REGIONS, TEST_UTIL.getHBaseCluster().getRegions(newTableName).size());
assertTrue("New table should be created with splitKyes + 1 rows in META",
admin.isTableAvailable(newTableName, splitKeys).get());
} else {
assertEquals(1, TEST_UTIL.getHBaseCluster().getRegions(newTableName).size());
}
}

@Test
public void testCloneTableSchemaWithNonExistentSourceTable() throws Exception {
final TableName newTableName = TableName.valueOf(tableName.getNameAsString() + "_new");
// test for non-existent source table
try {
admin.cloneTableSchema(tableName, newTableName, false).join();
fail("Should have failed when source table doesn't exist.");
} catch (CompletionException e) {
assertTrue(e.getCause() instanceof TableNotFoundException);
}
}

@Test
public void testCloneTableSchemaWithExistentDestinationTable() throws Exception {
final TableName newTableName = TableName.valueOf(tableName.getNameAsString() + "_new");
byte[] FAMILY_0 = Bytes.toBytes("cf0");
TEST_UTIL.createTable(tableName, FAMILY_0);
TEST_UTIL.createTable(newTableName, FAMILY_0);
// test for existent destination table
try {
admin.cloneTableSchema(tableName, newTableName, false).join();
fail("Should have failed when destination table exists.");
} catch (CompletionException e) {
assertTrue(e.getCause() instanceof TableExistsException);
}
}

@Test
public void testIsTableAvailableWithInexistantTable() throws Exception {
final TableName newTableName = TableName.valueOf(tableName.getNameAsString() + "_new");
// test for inexistant table
assertFalse(admin.isTableAvailable(newTableName).get());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -159,9 +159,7 @@ public void testRestoreDisabledSnapshot()
TableName tableName = TableName.valueOf("testtb-" + tid);
byte[] emptySnapshot = Bytes.toBytes("emptySnaptb-" + tid);
byte[] snapshotName0 = Bytes.toBytes("snaptb0-" + tid);
byte[] snapshotName1 = Bytes.toBytes("snaptb1-" + tid);
int snapshot0Rows;
int snapshot1Rows;

// create Table and disable it
SnapshotTestingUtils.createTable(UTIL, tableName, getNumReplicas(), TEST_FAM);
Expand All @@ -177,21 +175,9 @@ public void testRestoreDisabledSnapshot()
snapshot0Rows = UTIL.countRows(table);
}
admin.disableTable(tableName);

// take a snapshot
takeSnapshot(tableName, Bytes.toString(snapshotName0), true);

// enable table and insert more data
admin.enableTable(tableName);
SnapshotTestingUtils.loadData(UTIL, tableName, 500, TEST_FAM);
try (Table table = UTIL.getConnection().getTable(tableName)) {
snapshot1Rows = UTIL.countRows(table);
}

SnapshotTestingUtils.verifyRowCount(UTIL, tableName, snapshot1Rows);
admin.disableTable(tableName);
takeSnapshot(tableName, Bytes.toString(snapshotName1), true);

// Restore from snapshot-0
admin.restoreSnapshot(snapshotName0);
admin.enableTable(tableName);
Expand All @@ -204,19 +190,6 @@ public void testRestoreDisabledSnapshot()
admin.enableTable(tableName);
SnapshotTestingUtils.verifyRowCount(UTIL, tableName, 0);
SnapshotTestingUtils.verifyReplicasCameOnline(tableName, admin, getNumReplicas());

// Restore from snapshot-1
admin.disableTable(tableName);
admin.restoreSnapshot(snapshotName1);
admin.enableTable(tableName);
SnapshotTestingUtils.verifyRowCount(UTIL, tableName, snapshot1Rows);
SnapshotTestingUtils.verifyReplicasCameOnline(tableName, admin, getNumReplicas());

// Restore from snapshot-1
UTIL.deleteTable(tableName);
admin.restoreSnapshot(snapshotName1);
SnapshotTestingUtils.verifyRowCount(UTIL, tableName, snapshot1Rows);
SnapshotTestingUtils.verifyReplicasCameOnline(tableName, admin, getNumReplicas());
}

@Test
Expand All @@ -226,9 +199,7 @@ public void testRestoreEnabledSnapshot()
TableName tableName = TableName.valueOf("testtb-" + tid);
byte[] emptySnapshot = Bytes.toBytes("emptySnaptb-" + tid);
byte[] snapshotName0 = Bytes.toBytes("snaptb0-" + tid);
byte[] snapshotName1 = Bytes.toBytes("snaptb1-" + tid);
int snapshot0Rows;
int snapshot1Rows;

// create Table
SnapshotTestingUtils.createTable(UTIL, tableName, getNumReplicas(), TEST_FAM);
Expand All @@ -241,19 +212,9 @@ public void testRestoreEnabledSnapshot()
try (Table table = UTIL.getConnection().getTable(tableName)) {
snapshot0Rows = UTIL.countRows(table);
}

// take a snapshot
takeSnapshot(tableName, Bytes.toString(snapshotName0), false);

// Insert more data
SnapshotTestingUtils.loadData(UTIL, tableName, 500, TEST_FAM);
try (Table table = UTIL.getConnection().getTable(tableName)) {
snapshot1Rows = UTIL.countRows(table);
}

SnapshotTestingUtils.verifyRowCount(UTIL, tableName, snapshot1Rows);
takeSnapshot(tableName, Bytes.toString(snapshotName1), false);

// Restore from snapshot-0
admin.disableTable(tableName);
admin.restoreSnapshot(snapshotName0);
Expand All @@ -267,19 +228,6 @@ public void testRestoreEnabledSnapshot()
admin.enableTable(tableName);
SnapshotTestingUtils.verifyRowCount(UTIL, tableName, 0);
SnapshotTestingUtils.verifyReplicasCameOnline(tableName, admin, getNumReplicas());

// Restore from snapshot-1
admin.disableTable(tableName);
admin.restoreSnapshot(snapshotName1);
admin.enableTable(tableName);
SnapshotTestingUtils.verifyRowCount(UTIL, tableName, snapshot1Rows);
SnapshotTestingUtils.verifyReplicasCameOnline(tableName, admin, getNumReplicas());

// Restore from snapshot-1
UTIL.deleteTable(tableName);
admin.restoreSnapshot(snapshotName1);
SnapshotTestingUtils.verifyRowCount(UTIL, tableName, snapshot1Rows);
SnapshotTestingUtils.verifyReplicasCameOnline(tableName, admin, getNumReplicas());
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import org.apache.hadoop.hbase.master.assignment.AssignmentTestingUtil;
import org.apache.hadoop.hbase.procedure2.ProcedureExecutor;
import org.apache.hadoop.hbase.procedure2.ProcedureTestingUtility;
import org.apache.hadoop.hbase.util.Bytes;
import org.junit.After;
import org.junit.Before;
import org.slf4j.Logger;
Expand Down Expand Up @@ -138,9 +139,10 @@ protected long getSCPProcId(ProcedureExecutor<?> procExec) {
}

protected Table createTable(final TableName tableName) throws IOException {
final Table t = this.util.createTable(tableName, HBaseTestingUtility.COLUMNS,
HBaseTestingUtility.KEYS_FOR_HBA_CREATE_TABLE, getRegionReplication());
return t;
int numRegions = 10;
byte[][] splitKeys = Bytes.split(Bytes.toBytes("aaa"), Bytes.toBytes("zzz"), numRegions - 3);
return util
.createTable(tableName, HBaseTestingUtility.COLUMNS, splitKeys, getRegionReplication());
}

protected int getRegionReplication() {
Expand Down
Loading

0 comments on commit a4368a7

Please sign in to comment.