TestEnableTableHandler.testDeleteForSureClearsAllTableRowsFromMeta - ADDENDUM

This commit is contained in:
Esteban Gutierrez 2015-03-18 18:02:35 -07:00 committed by Matteo Bertozzi
parent 6a95dd3551
commit b728e66708
1 changed files with 92 additions and 3 deletions

View File

@ -21,6 +21,7 @@ package org.apache.hadoop.hbase.master.handler;
import java.util.ArrayList;
import java.util.List;
import java.io.IOException;
import java.util.concurrent.CountDownLatch;
import com.google.common.base.Predicate;
import com.google.common.collect.Iterables;
@ -43,6 +44,10 @@ import org.apache.hadoop.hbase.client.ResultScanner;
import org.apache.hadoop.hbase.client.Scan;
import org.apache.hadoop.hbase.client.Table;
import org.apache.hadoop.hbase.master.HMaster;
import org.apache.hadoop.hbase.coprocessor.BaseMasterObserver;
import org.apache.hadoop.hbase.coprocessor.CoprocessorHost;
import org.apache.hadoop.hbase.coprocessor.MasterCoprocessorEnvironment;
import org.apache.hadoop.hbase.coprocessor.ObserverContext;
import org.apache.hadoop.hbase.testclassification.MasterTests;
import org.apache.hadoop.hbase.testclassification.MediumTests;
import org.apache.hadoop.hbase.util.Bytes;
@ -54,6 +59,7 @@ import org.junit.experimental.categories.Category;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
@Category({ MasterTests.class, MediumTests.class })
public class TestEnableTableHandler {
@ -63,6 +69,8 @@ public class TestEnableTableHandler {
@Before
public void setUp() throws Exception {
TEST_UTIL.getConfiguration().set(CoprocessorHost.MASTER_COPROCESSOR_CONF_KEY,
MasterSyncObserver.class.getName());
TEST_UTIL.startMiniCluster(1);
}
@ -140,7 +148,12 @@ public class TestEnableTableHandler {
final HBaseAdmin admin = TEST_UTIL.getHBaseAdmin();
final HTableDescriptor desc = new HTableDescriptor(tableName);
desc.addFamily(new HColumnDescriptor(FAMILYNAME));
admin.createTable(desc, HBaseTestingUtility.KEYS_FOR_HBA_CREATE_TABLE);
try {
createTable(TEST_UTIL, desc, HBaseTestingUtility.KEYS_FOR_HBA_CREATE_TABLE);
} catch (Exception e) {
e.printStackTrace();
fail("Got an exception while creating " + tableName);
}
// Now I have a nice table, mangle it by removing the HConstants.REGIONINFO_QUALIFIER_STR
// content from a few of the rows.
try (Table metaTable = TEST_UTIL.getConnection().getTable(TableName.META_TABLE_NAME)) {
@ -157,8 +170,13 @@ public class TestEnableTableHandler {
}
admin.disableTable(tableName);
TEST_UTIL.waitTableDisabled(tableName.getName());
// Presume this synchronous all is.
admin.deleteTable(tableName);
// Rely on the coprocessor based latch to make the operation synchronous.
try {
deleteTable(TEST_UTIL, tableName);
} catch (Exception e) {
e.printStackTrace();
fail("Got an exception while deleting " + tableName);
}
int rowCount = 0;
try (ResultScanner scanner =
metaTable.getScanner(MetaTableAccessor.getScanForTableName(TEST_UTIL.getConnection(), tableName))) {
@ -170,4 +188,75 @@ public class TestEnableTableHandler {
assertEquals(0, rowCount);
}
}
public static class MasterSyncObserver extends BaseMasterObserver {
volatile CountDownLatch tableCreationLatch = null;
volatile CountDownLatch tableDeletionLatch = null;
@Override
public void postCreateTableHandler(final ObserverContext<MasterCoprocessorEnvironment> ctx,
HTableDescriptor desc, HRegionInfo[] regions) throws IOException {
// the AccessController test, some times calls only and directly the postCreateTableHandler()
if (tableCreationLatch != null) {
tableCreationLatch.countDown();
}
}
@Override
public void postDeleteTableHandler(final ObserverContext<MasterCoprocessorEnvironment> ctx,
TableName tableName)
throws IOException {
// the AccessController test, some times calls only and directly the postDeleteTableHandler()
if (tableDeletionLatch != null) {
tableDeletionLatch.countDown();
}
}
}
public static void createTable(HBaseTestingUtility testUtil, HTableDescriptor htd,
byte [][] splitKeys)
throws Exception {
createTable(testUtil, testUtil.getHBaseAdmin(), htd, splitKeys);
}
public static void createTable(HBaseTestingUtility testUtil, HBaseAdmin admin,
HTableDescriptor htd, byte [][] splitKeys)
throws Exception {
// NOTE: We need a latch because admin is not sync,
// so the postOp coprocessor method may be called after the admin operation returned.
MasterSyncObserver observer = (MasterSyncObserver)testUtil.getHBaseCluster().getMaster()
.getMasterCoprocessorHost().findCoprocessor(MasterSyncObserver.class.getName());
observer.tableCreationLatch = new CountDownLatch(1);
if (splitKeys != null) {
admin.createTable(htd, splitKeys);
} else {
admin.createTable(htd);
}
observer.tableCreationLatch.await();
observer.tableCreationLatch = null;
testUtil.waitUntilAllRegionsAssigned(htd.getTableName());
}
public static void deleteTable(HBaseTestingUtility testUtil, TableName tableName)
throws Exception {
deleteTable(testUtil, testUtil.getHBaseAdmin(), tableName);
}
public static void deleteTable(HBaseTestingUtility testUtil, HBaseAdmin admin,
TableName tableName)
throws Exception {
// NOTE: We need a latch because admin is not sync,
// so the postOp coprocessor method may be called after the admin operation returned.
MasterSyncObserver observer = (MasterSyncObserver)testUtil.getHBaseCluster().getMaster()
.getMasterCoprocessorHost().findCoprocessor(MasterSyncObserver.class.getName());
observer.tableDeletionLatch = new CountDownLatch(1);
try {
admin.disableTable(tableName);
} catch (Exception e) {
LOG.debug("Table: " + tableName + " already disabled, so just deleting it.");
}
admin.deleteTable(tableName);
observer.tableDeletionLatch.await();
observer.tableDeletionLatch = null;
}
}