Skip to content

Commit beb162e

Browse files
committed
[Test] add test for create/drp tablegroup and ddl stmt str
1 parent 667a16f commit beb162e

File tree

1 file changed

+241
-61
lines changed

1 file changed

+241
-61
lines changed

src/test/java/com/alipay/oceanbase/hbase/OHTableAdminInterfaceTest.java

Lines changed: 241 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,9 @@ enum ErrSimPoint {
9494
EN_CREATE_HTABLE_TG_FINISH_ERR(2621),
9595
EN_CREATE_HTABLE_CF_FINISH_ERR(2622),
9696
EN_DISABLE_HTABLE_CF_FINISH_ERR(2623),
97-
EN_DELETE_HTABLE_CF_FINISH_ERR(2624);
98-
97+
EN_DELETE_HTABLE_CF_FINISH_ERR(2624),
98+
EN_DELETE_HTABLE_SKIP_CF_ERR(2625);
99+
99100
private final int errCode;
100101

101102
ErrSimPoint(int errCode) {
@@ -568,7 +569,7 @@ public void testAdminGetRegionMetrics() throws Exception {
568569
admin.getRegionMetrics(null, TableName.valueOf("tablegroup_not_exists"));
569570
});
570571
Assert.assertTrue(thrown.getCause() instanceof ObTableException);
571-
Assert.assertEquals(ResultCodes.OB_TABLEGROUP_NOT_EXIST.errorCode, ((ObTableException) thrown.getCause()).getErrorCode());
572+
Assert.assertEquals(ResultCodes.OB_KV_HBASE_TABLE_NOT_EXISTS.errorCode, ((ObTableException) thrown.getCause()).getErrorCode());
572573

573574
// test use serverName without tableName to get region metrics
574575
assertThrows(FeatureNotSupportedException.class,
@@ -663,6 +664,19 @@ public void testAdminGetRegionMetrics() throws Exception {
663664
assertEquals(3, metrics.size());
664665
}
665666

667+
private void deleteTableIfExists(Admin admin, TableName tableName) throws Exception {
668+
if (admin.tableExists(tableName)) {
669+
if (admin.isTableEnabled(tableName)) {
670+
admin.disableTable(tableName);
671+
}
672+
admin.deleteTable(tableName);
673+
}
674+
}
675+
676+
private void deleteTableIfExists(Admin admin, String tableName) throws Exception {
677+
deleteTableIfExists(admin, TableName.valueOf(tableName));
678+
}
679+
666680
@Test
667681
public void testAdminDeleteTable() throws Exception {
668682
java.sql.Connection conn = ObHTableTestUtil.getConnection();
@@ -673,20 +687,28 @@ public void testAdminDeleteTable() throws Exception {
673687
Configuration conf = ObHTableTestUtil.newConfiguration();
674688
Connection connection = ConnectionFactory.createConnection(conf);
675689
Admin admin = connection.getAdmin();
676-
createTable(admin, TableName.valueOf("test_del_tb"), "cf1", "cf2", "cf3");
677-
createTable(admin, TableName.valueOf("del_tb", "test"), "cf1", "cf2", "cf3");
678-
assertTrue(admin.tableExists(TableName.valueOf("del_tb", "test")));
679-
assertTrue(admin.tableExists(TableName.valueOf("test_del_tb")));
680-
IOException thrown = assertThrows(IOException.class,
681-
() -> {
682-
admin.deleteTable(TableName.valueOf("tablegroup_not_exists"));
683-
});
684-
Assert.assertTrue(thrown.getCause() instanceof ObTableException);
685-
Assert.assertEquals(ResultCodes.OB_TABLEGROUP_NOT_EXIST.errorCode, ((ObTableException) thrown.getCause()).getErrorCode());
686-
admin.deleteTable(TableName.valueOf("del_tb", "test"));
687-
admin.deleteTable(TableName.valueOf("test_del_tb"));
688-
assertFalse(admin.tableExists(TableName.valueOf("del_tb", "test")));
689-
assertFalse(admin.tableExists(TableName.valueOf("test_del_tb")));
690+
try {
691+
createTable(admin, TableName.valueOf("test_del_tb"), "cf1", "cf2", "cf3");
692+
createTable(admin, TableName.valueOf("del_tb", "test"), "cf1", "cf2", "cf3");
693+
assertTrue(admin.tableExists(TableName.valueOf("del_tb", "test")));
694+
assertTrue(admin.tableExists(TableName.valueOf("test_del_tb")));
695+
IOException thrown = assertThrows(IOException.class,
696+
() -> {
697+
admin.deleteTable(TableName.valueOf("tablegroup_not_exists"));
698+
});
699+
Assert.assertTrue(thrown.getCause() instanceof ObTableException);
700+
Assert.assertEquals(ResultCodes.OB_TABLEGROUP_NOT_EXIST.errorCode, ((ObTableException) thrown.getCause()).getErrorCode());
701+
admin.deleteTable(TableName.valueOf("del_tb", "test"));
702+
admin.deleteTable(TableName.valueOf("test_del_tb"));
703+
assertFalse(admin.tableExists(TableName.valueOf("del_tb", "test")));
704+
assertFalse(admin.tableExists(TableName.valueOf("test_del_tb")));
705+
} catch (Exception e) {
706+
e.printStackTrace();
707+
fail();
708+
} finally {
709+
deleteTableIfExists(admin, "test_del_tb");
710+
deleteTableIfExists(admin, "del_tb");
711+
}
690712
}
691713

692714
@Test
@@ -1305,51 +1327,62 @@ public void testCreateTableInjectError() throws Exception {
13051327
htd.addFamily(hcd1);
13061328
htd.addFamily(hcd2);
13071329
htd.addFamily(hcd3);
1308-
1309-
// 1. open err EN_CREATE_HTABLE_TG_FINISH_ERR
1310-
setErrSimPoint(ErrSimPoint.EN_CREATE_HTABLE_TG_FINISH_ERR, true);
1311-
ObHTableTestUtil.executeIgnoreUnexpectedError(() -> admin.createTable(htd));
1312-
assertFalse("Table should not exist after TG error injection",
1313-
admin.tableExists(TableName.valueOf(tableName)));
1314-
setErrSimPoint(ErrSimPoint.EN_CREATE_HTABLE_TG_FINISH_ERR, false);
1315-
1316-
// 2. open err EN_CREATE_HTABLE_CF_FINISH_ERR
1317-
setErrSimPoint(ErrSimPoint.EN_CREATE_HTABLE_CF_FINISH_ERR, true);
1318-
ObHTableTestUtil.executeIgnoreUnexpectedError(() -> admin.createTable(htd));
1319-
assertFalse("Table should not exist after CF error injection",
1320-
admin.tableExists(TableName.valueOf(tableName)));
1321-
setErrSimPoint(ErrSimPoint.EN_CREATE_HTABLE_CF_FINISH_ERR, false);
1322-
1323-
// 3. create table without error
1324-
admin.createTable(htd);
1325-
assertTrue("Table should exist after normal creation",
1326-
admin.tableExists(TableName.valueOf(tableName)));
1327-
assertEquals("Table should have 3 column families", 3,
1330+
1331+
try {
1332+
// 1. open err EN_CREATE_HTABLE_TG_FINISH_ERR
1333+
setErrSimPoint(ErrSimPoint.EN_CREATE_HTABLE_TG_FINISH_ERR, true);
1334+
ObHTableTestUtil.executeIgnoreUnexpectedError(() -> admin.createTable(htd));
1335+
assertFalse("Table should not exist after TG error injection",
1336+
admin.tableExists(TableName.valueOf(tableName)));
1337+
setErrSimPoint(ErrSimPoint.EN_CREATE_HTABLE_TG_FINISH_ERR, false);
1338+
1339+
// 2. open err EN_CREATE_HTABLE_CF_FINISH_ERR
1340+
setErrSimPoint(ErrSimPoint.EN_CREATE_HTABLE_CF_FINISH_ERR, true);
1341+
ObHTableTestUtil.executeIgnoreUnexpectedError(() -> admin.createTable(htd));
1342+
assertFalse("Table should not exist after CF error injection",
1343+
admin.tableExists(TableName.valueOf(tableName)));
1344+
setErrSimPoint(ErrSimPoint.EN_CREATE_HTABLE_CF_FINISH_ERR, false);
1345+
1346+
// 3. create table without error
1347+
admin.createTable(htd);
1348+
assertTrue("Table should exist after normal creation",
1349+
admin.tableExists(TableName.valueOf(tableName)));
1350+
assertEquals("Table should have 3 column families", 3,
13281351
admin.getTableDescriptor(TableName.valueOf(tableName)).getFamilies().size());
1329-
1330-
// 4. open err EN_DISABLE_HTABLE_CF_FINISH_ERR
1331-
setErrSimPoint(ErrSimPoint.EN_DISABLE_HTABLE_CF_FINISH_ERR, true);
1332-
admin.disableTable(TableName.valueOf(tableName));
1333-
assertFalse("Table should not be disabled after disable error injection",
1334-
admin.isTableDisabled(TableName.valueOf(tableName)));
1335-
setErrSimPoint(ErrSimPoint.EN_DISABLE_HTABLE_CF_FINISH_ERR, false);
1336-
1337-
// 5. disable table without error
1338-
admin.disableTable(TableName.valueOf(tableName));
1339-
assertTrue("Table should be disabled after normal disable",
1340-
admin.isTableDisabled(TableName.valueOf(tableName)));
1341-
1342-
// 6. open err EN_DELETE_HTABLE_CF_FINISH_ERR
1343-
setErrSimPoint(ErrSimPoint.EN_DELETE_HTABLE_CF_FINISH_ERR, true);
1344-
admin.deleteTable(TableName.valueOf(tableName));
1345-
assertTrue("Table should still exist after delete error injection",
1346-
admin.tableExists(TableName.valueOf(tableName)));
1347-
setErrSimPoint(ErrSimPoint.EN_DELETE_HTABLE_CF_FINISH_ERR, false);
1348-
1349-
// 7. delete table without error
1350-
admin.deleteTable(TableName.valueOf(tableName));
1351-
assertFalse("Table should not exist after normal delete",
1352-
admin.tableExists(TableName.valueOf(tableName)));
1352+
1353+
// 4. open err EN_DISABLE_HTABLE_CF_FINISH_ERR
1354+
setErrSimPoint(ErrSimPoint.EN_DISABLE_HTABLE_CF_FINISH_ERR, true);
1355+
ObHTableTestUtil.executeIgnoreUnexpectedError(() -> admin.disableTable(TableName.valueOf(tableName)));
1356+
assertFalse("Table should not be disabled after disable error injection",
1357+
admin.isTableDisabled(TableName.valueOf(tableName)));
1358+
setErrSimPoint(ErrSimPoint.EN_DISABLE_HTABLE_CF_FINISH_ERR, false);
1359+
1360+
// 5. disable table without error
1361+
admin.disableTable(TableName.valueOf(tableName));
1362+
assertTrue("Table should be disabled after normal disable",
1363+
admin.isTableDisabled(TableName.valueOf(tableName)));
1364+
1365+
// 6. open err EN_DELETE_HTABLE_CF_FINISH_ERR
1366+
setErrSimPoint(ErrSimPoint.EN_DELETE_HTABLE_CF_FINISH_ERR, true);
1367+
ObHTableTestUtil.executeIgnoreUnexpectedError(() -> admin.deleteTable(TableName.valueOf(tableName)));
1368+
assertTrue("Table should still exist after delete error injection",
1369+
admin.tableExists(TableName.valueOf(tableName)));
1370+
setErrSimPoint(ErrSimPoint.EN_DELETE_HTABLE_CF_FINISH_ERR, false);
1371+
1372+
// 7. delete table without error
1373+
admin.deleteTable(TableName.valueOf(tableName));
1374+
assertFalse("Table should not exist after normal delete",
1375+
admin.tableExists(TableName.valueOf(tableName)));
1376+
} catch (Exception e) {
1377+
e.printStackTrace();
1378+
assertTrue(false);
1379+
} finally {
1380+
if (admin.tableExists(TableName.valueOf(tableName))) {
1381+
setErrSimPoint(ErrSimPoint.EN_DELETE_HTABLE_CF_FINISH_ERR, false);
1382+
setErrSimPoint(ErrSimPoint.EN_DELETE_HTABLE_SKIP_CF_ERR, false);
1383+
admin.deleteTable(TableName.valueOf(tableName));
1384+
}
1385+
}
13531386
}
13541387

13551388
@Test
@@ -1518,4 +1551,151 @@ public void testHbaseDDLException() throws Exception {
15181551
}
15191552

15201553
}
1554+
1555+
// Test cases for abnormal scene in CreateTableGroupHelper/DropTableGroupHelper
1556+
@Test
1557+
public void testCreateDropTableGroup() throws Exception {
1558+
Configuration conf = ObHTableTestUtil.newConfiguration();
1559+
Connection connection = ConnectionFactory.createConnection(conf);
1560+
Admin admin = connection.getAdmin();
1561+
java.sql.Connection conn = ObHTableTestUtil.getConnection();
1562+
java.sql.Connection sysConn = ObHTableTestUtil.getSysConnection();
1563+
String tenantName = "mysql";
1564+
1565+
byte[] tableName = Bytes.toBytes("test_create_drop_tg_helper");
1566+
byte[] cf1 = Bytes.toBytes("cf1");
1567+
byte[] cf2 = Bytes.toBytes("cf2");
1568+
byte[] cf3 = Bytes.toBytes("cf3");
1569+
1570+
HColumnDescriptor hcd1 = new HColumnDescriptor(cf1);
1571+
HColumnDescriptor hcd2 = new HColumnDescriptor(cf2);
1572+
HColumnDescriptor hcd3 = new HColumnDescriptor(cf3);
1573+
1574+
HTableDescriptor htd = new HTableDescriptor(TableName.valueOf(tableName));
1575+
htd.addFamily(hcd1);
1576+
htd.addFamily(hcd2);
1577+
htd.addFamily(hcd3);
1578+
1579+
try {
1580+
admin.createTable(htd);
1581+
1582+
// 1. open err EN_DELETE_HTABLE_SKIP_CF_ERR, will skip delete cf table when delete hbase table
1583+
// and the subsequent delete htable operations will return OB_TABLEGROUP_NOT_EMPTY
1584+
setErrSimPoint(ErrSimPoint.EN_DELETE_HTABLE_SKIP_CF_ERR, true);
1585+
ObHTableTestUtil.executeIgnoreExpectedErrors(() -> admin.deleteTable(TableName.valueOf(tableName)), "OB_TABLEGROUP_NOT_EMPTY");
1586+
assertTrue("Table should still exist after delete error injection",
1587+
admin.tableExists(TableName.valueOf(tableName)));
1588+
setErrSimPoint(ErrSimPoint.EN_DELETE_HTABLE_SKIP_CF_ERR, false);
1589+
1590+
// 2. create a database and set default tablegroup to test_create_drop_tg_helper,
1591+
// and the subsequent delete htable operation will return OB_TABLEGROUP_NOT_EMPTY
1592+
executeSQL(conn, "create database db_test_create_drop_tg_helper default tablegroup test_create_drop_tg_helper", true);
1593+
ObHTableTestUtil.executeIgnoreExpectedErrors(() -> admin.deleteTable(TableName.valueOf(tableName)), "OB_TABLEGROUP_NOT_EMPTY");
1594+
executeSQL(conn, "drop database db_test_create_drop_tg_helper", true);
1595+
1596+
// 3. set tenant's default tablegroup to test_create_drop_tg_helper,
1597+
// and the subsequent delete htable operation will return OB_TABLEGROUP_NOT_EMPTY
1598+
executeSQL(sysConn, String.format("alter tenant %s set default tablegroup = test_create_drop_tg_helper", tenantName), true);
1599+
ObHTableTestUtil.executeIgnoreExpectedErrors(() -> admin.deleteTable(TableName.valueOf(tableName)), "OB_TABLEGROUP_NOT_EMPTY");
1600+
executeSQL(sysConn, String.format("alter tenant %s set default tablegroup = null", tenantName), true);
1601+
1602+
} catch (Exception e) {
1603+
e.printStackTrace();
1604+
throw e;
1605+
} finally {
1606+
executeSQL(conn, "drop database if exists db_test_create_drop_tg_helper", true);
1607+
executeSQL(sysConn, String.format("alter tenant %s set default tablegroup = null", tenantName), true);
1608+
if (admin.tableExists(TableName.valueOf(tableName))) {
1609+
setErrSimPoint(ErrSimPoint.EN_DELETE_HTABLE_SKIP_CF_ERR, false);
1610+
admin.deleteTable(TableName.valueOf(tableName));
1611+
}
1612+
}
1613+
}
1614+
1615+
private void checkDDLStmtStr(List<Map.Entry<Integer, String>> ddlStmts) throws Exception {
1616+
java.sql.Connection conn = ObHTableTestUtil.getConnection();
1617+
java.sql.ResultSet resultSet = conn.createStatement().executeQuery(
1618+
String.format("select operation_type, ddl_stmt_str from oceanbase.__all_ddl_operation order by gmt_modified desc limit %d", ddlStmts.size() + 2));
1619+
Assert.assertTrue(resultSet.next());
1620+
int operationType = resultSet.getInt("operation_type");
1621+
Assert.assertEquals(1503, operationType);
1622+
String ddlStmtStr = resultSet.getString("ddl_stmt_str");
1623+
Assert.assertEquals("", ddlStmtStr);
1624+
1625+
for (int i = 0; i < ddlStmts.size(); i++) {
1626+
Assert.assertTrue(resultSet.next());
1627+
operationType = resultSet.getInt("operation_type");
1628+
Assert.assertEquals(ddlStmts.get(i).getKey().intValue(), operationType);
1629+
ddlStmtStr = resultSet.getString("ddl_stmt_str");
1630+
Assert.assertEquals(ddlStmts.get(i).getValue(), ddlStmtStr);
1631+
}
1632+
Assert.assertTrue(resultSet.next());
1633+
operationType = resultSet.getInt("operation_type");
1634+
Assert.assertEquals(1503, operationType);
1635+
ddlStmtStr = resultSet.getString("ddl_stmt_str");
1636+
Assert.assertEquals("", ddlStmtStr);
1637+
Assert.assertFalse(resultSet.next());
1638+
}
1639+
1640+
@Test
1641+
public void testDDLStmtStr() throws Exception {
1642+
Configuration conf = ObHTableTestUtil.newConfiguration();
1643+
Connection connection = ConnectionFactory.createConnection(conf);
1644+
Admin admin = connection.getAdmin();
1645+
1646+
byte[] tableName = Bytes.toBytes("test_ddl_stmt_str");
1647+
byte[] cf1 = Bytes.toBytes("cf1");
1648+
byte[] cf2 = Bytes.toBytes("cf2");
1649+
1650+
HColumnDescriptor hcd1 = new HColumnDescriptor(cf1);
1651+
HColumnDescriptor hcd2 = new HColumnDescriptor(cf2);
1652+
1653+
HTableDescriptor htd = new HTableDescriptor(TableName.valueOf(tableName));
1654+
htd.addFamily(hcd1);
1655+
htd.addFamily(hcd2);
1656+
1657+
try {
1658+
// 1. create hbase table
1659+
admin.createTable(htd);
1660+
List<Map.Entry<Integer, String>> expStmtStrs = new ArrayList<>();
1661+
expStmtStrs.add(new AbstractMap.SimpleEntry<>(4, "CREATE TABLE `test_ddl_stmt_str$cf1` (K varbinary(1024) NOT NULL, Q varbinary(256) NOT NULL, T bigint NOT NULL, V varbinary(1048576) NOT NULL, " +
1662+
"PRIMARY KEY (K, Q, T)) TABLEGROUP = `test_ddl_stmt_str` kv_attributes = '{\"Hbase\": {\"MaxVersions\": 1, \"CreatedBy\": \"Admin\"}}'"));
1663+
expStmtStrs.add(new AbstractMap.SimpleEntry<>(4, "CREATE TABLE `test_ddl_stmt_str$cf2` (K varbinary(1024) NOT NULL, Q varbinary(256) NOT NULL, T bigint NOT NULL, V varbinary(1048576) NOT NULL, " +
1664+
"PRIMARY KEY (K, Q, T)) TABLEGROUP = `test_ddl_stmt_str` kv_attributes = '{\"Hbase\": {\"MaxVersions\": 1, \"CreatedBy\": \"Admin\"}}'"));
1665+
expStmtStrs.add(new AbstractMap.SimpleEntry<>(302, "CREATE TABLEGROUP `test_ddl_stmt_str`"));
1666+
checkDDLStmtStr(expStmtStrs);
1667+
1668+
// 2. disable hbase table
1669+
admin.disableTable(TableName.valueOf(tableName));
1670+
expStmtStrs.clear();
1671+
expStmtStrs.add(new AbstractMap.SimpleEntry<>(3, "ALTER TABLE test_ddl_stmt_str$cf1 KV_ATTRIBUTES='{\"Hbase\": {\"MaxVersions\": 1, \"CreatedBy\": \"Admin\", \"State\": \"disable\"}}'"));
1672+
expStmtStrs.add(new AbstractMap.SimpleEntry<>(3, "ALTER TABLE test_ddl_stmt_str$cf2 KV_ATTRIBUTES='{\"Hbase\": {\"MaxVersions\": 1, \"CreatedBy\": \"Admin\", \"State\": \"disable\"}}'"));
1673+
checkDDLStmtStr(expStmtStrs);
1674+
1675+
// 3. enable hbase table
1676+
admin.enableTable(TableName.valueOf(tableName));
1677+
expStmtStrs.clear();
1678+
expStmtStrs.add(new AbstractMap.SimpleEntry<>(3, "ALTER TABLE test_ddl_stmt_str$cf1 KV_ATTRIBUTES='{\"Hbase\": {\"MaxVersions\": 1, \"CreatedBy\": \"Admin\", \"State\": \"enable\"}}'"));
1679+
expStmtStrs.add(new AbstractMap.SimpleEntry<>(3, "ALTER TABLE test_ddl_stmt_str$cf2 KV_ATTRIBUTES='{\"Hbase\": {\"MaxVersions\": 1, \"CreatedBy\": \"Admin\", \"State\": \"enable\"}}'"));
1680+
checkDDLStmtStr(expStmtStrs);
1681+
1682+
// 4. delete hbase table
1683+
admin.disableTable(TableName.valueOf(tableName));
1684+
admin.deleteTable(TableName.valueOf(tableName));
1685+
expStmtStrs.clear();
1686+
expStmtStrs.add(new AbstractMap.SimpleEntry<>(303, "DROP TABLEGROUP `test_ddl_stmt_str`"));
1687+
expStmtStrs.add(new AbstractMap.SimpleEntry<>(2, "DROP TABLE `test`.`test_ddl_stmt_str$cf1`"));
1688+
expStmtStrs.add(new AbstractMap.SimpleEntry<>(2, "DROP TABLE `test`.`test_ddl_stmt_str$cf2`"));
1689+
} catch (Exception e) {
1690+
e.printStackTrace();
1691+
fail();
1692+
} finally {
1693+
if (admin.tableExists(TableName.valueOf(tableName))) {
1694+
if (admin.isTableEnabled(TableName.valueOf(tableName))) {
1695+
admin.disableTable(TableName.valueOf(tableName));
1696+
}
1697+
admin.deleteTable(TableName.valueOf(tableName));
1698+
}
1699+
}
1700+
}
15211701
}

0 commit comments

Comments
 (0)