29
29
import org .apache .hadoop .hbase .util .Bytes ;
30
30
import org .apache .hadoop .hbase .util .Pair ;
31
31
32
+ import org .junit .AfterClass ;
32
33
import org .junit .Assert ;
34
+ import org .junit .BeforeClass ;
33
35
import org .junit .Test ;
34
36
35
-
36
37
import java .io .IOException ;
37
38
import java .sql .SQLException ;
38
39
import java .sql .Statement ;
45
46
import static org .apache .hadoop .hbase .util .Bytes .toBytes ;
46
47
import static org .junit .Assert .*;
47
48
import static org .junit .Assert .assertFalse ;
48
- import static com .alipay .oceanbase .hbase .util .ObHTableSecondaryPartUtil .*;
49
+ import static com .alipay .oceanbase .hbase .util .ObHTableSecondaryPartUtil .*;
49
50
50
51
public class OHTableAdminInterfaceTest {
51
52
public OHTablePool setUpLoadPool () throws IOException {
@@ -58,6 +59,28 @@ public OHTablePool setUpLoadPool() throws IOException {
58
59
return ohTablePool ;
59
60
}
60
61
62
+ public static void openHbaseAdminDDL () throws Exception {
63
+ java .sql .Connection conn = ObHTableTestUtil .getConnection ();
64
+ String stmt = "ALTER SYSTEM SET _enable_kv_hbase_admin_ddl = true;" ;
65
+ conn .createStatement ().execute (stmt );
66
+ }
67
+
68
+ public static void closeHbaseAdminDDL () throws Exception {
69
+ java .sql .Connection conn = ObHTableTestUtil .getConnection ();
70
+ String stmt = "ALTER SYSTEM SET _enable_kv_hbase_admin_ddl = false;" ;
71
+ conn .createStatement ().execute (stmt );
72
+ }
73
+
74
+ @ BeforeClass
75
+ public static void before () throws Exception {
76
+ openHbaseAdminDDL ();
77
+ }
78
+
79
+ @ AfterClass
80
+ public static void finish () throws Exception {
81
+ closeHbaseAdminDDL ();
82
+ }
83
+
61
84
public OHTablePool setUpPool () throws IOException {
62
85
Configuration c = ObHTableTestUtil .newConfiguration ();
63
86
OHTablePool ohTablePool = new OHTablePool (c , 10 );
@@ -321,7 +344,8 @@ public void testGetStartEndKeysOHTablePoolLoadNon() throws Exception {
321
344
Assert .assertEquals (0 , startEndKeys .getSecond ()[0 ].length );
322
345
}
323
346
324
- public static void createTable (Admin admin , TableName tableName , String ... columnFamilies ) throws IOException {
347
+ public static void createTable (Admin admin , TableName tableName , String ... columnFamilies )
348
+ throws IOException {
325
349
HTableDescriptor htd = new HTableDescriptor (tableName );
326
350
// Add column families
327
351
for (String cf : columnFamilies ) {
@@ -427,10 +451,8 @@ public void testAdminEnDisableTable() throws Exception {
427
451
try {
428
452
admin .enableTable (TableName .valueOf ("en_dis" , "test" ));
429
453
Assert .fail ();
430
- } catch (IOException ex ) {
431
- Assert .assertTrue (ex .getCause () instanceof ObTableException );
432
- Assert .assertEquals (ResultCodes .OB_KV_TABLE_NOT_ENABLED .errorCode ,
433
- ((ObTableException ) ex .getCause ()).getErrorCode ());
454
+ } catch (Exception ex ) {
455
+ Assert .assertEquals (TableNotDisabledException .class , ex .getClass ());
434
456
}
435
457
}
436
458
@@ -445,10 +467,8 @@ public void testAdminEnDisableTable() throws Exception {
445
467
try {
446
468
admin .disableTable (TableName .valueOf ("en_dis" , "test" ));
447
469
Assert .fail ();
448
- } catch (IOException ex ) {
449
- Assert .assertTrue (ex .getCause () instanceof ObTableException );
450
- Assert .assertEquals (ResultCodes .OB_KV_TABLE_NOT_DISABLED .errorCode ,
451
- ((ObTableException ) ex .getCause ()).getErrorCode ());
470
+ } catch (Exception ex ) {
471
+ Assert .assertEquals (TableNotEnabledException .class , ex .getClass ());
452
472
}
453
473
}
454
474
@@ -1255,7 +1275,9 @@ public void testHTableDDLDefense() throws Exception {
1255
1275
1256
1276
void checkKVAttributes (String tableName , String kvAttributes ) throws Exception {
1257
1277
java .sql .Connection conn = ObHTableTestUtil .getConnection ();
1258
- java .sql .ResultSet resultSet = conn .createStatement ().executeQuery ("select kv_attributes from oceanbase.__all_table where table_name = '" + tableName + "'" );
1278
+ java .sql .ResultSet resultSet = conn .createStatement ().executeQuery (
1279
+ "select kv_attributes from oceanbase.__all_table where table_name = '" + tableName
1280
+ + "'" );
1259
1281
resultSet .next ();
1260
1282
String value = resultSet .getString (1 );
1261
1283
Assert .assertEquals (kvAttributes , value );
@@ -1329,4 +1351,171 @@ public void testCreateTableInjectError() throws Exception {
1329
1351
assertFalse ("Table should not exist after normal delete" ,
1330
1352
admin .tableExists (TableName .valueOf (tableName )));
1331
1353
}
1354
+
1355
+ @ Test
1356
+ public void testHbaseAdminDDLKnob () throws Exception {
1357
+ Configuration conf = ObHTableTestUtil .newConfiguration ();
1358
+ Connection connection = ConnectionFactory .createConnection (conf );
1359
+ Admin admin = connection .getAdmin ();
1360
+ try {
1361
+ closeHbaseAdminDDL ();
1362
+ try {
1363
+ createTable (admin , TableName .valueOf ("t1" ), "cf1" , "cf2" , "cf3" );
1364
+ fail ();
1365
+ } catch (Exception e ) {
1366
+ Assert .assertEquals (-4007 , ((ObTableException ) e .getCause ()).getErrorCode ());
1367
+ }
1368
+
1369
+ try {
1370
+ admin .disableTable (TableName .valueOf ("t1" ));
1371
+ fail ();
1372
+ } catch (Exception e ) {
1373
+ Assert .assertEquals (-4007 , ((ObTableException ) e .getCause ()).getErrorCode ());
1374
+ }
1375
+
1376
+ try {
1377
+ admin .enableTable (TableName .valueOf ("t1" ));
1378
+ fail ();
1379
+ } catch (Exception e ) {
1380
+ Assert .assertEquals (-4007 , ((ObTableException ) e .getCause ()).getErrorCode ());
1381
+ }
1382
+
1383
+ try {
1384
+ admin .deleteTable (TableName .valueOf ("t1" ));
1385
+ fail ();
1386
+ } catch (Exception e ) {
1387
+ Assert .assertEquals (-4007 , ((ObTableException ) e .getCause ()).getErrorCode ());
1388
+ }
1389
+ } catch (Exception e ) {
1390
+ e .printStackTrace ();
1391
+ throw e ;
1392
+ } finally {
1393
+ openHbaseAdminDDL ();
1394
+ }
1395
+ }
1396
+
1397
+ @ Test
1398
+ public void testHbaseDDLException () throws Exception {
1399
+ Configuration conf = ObHTableTestUtil .newConfiguration ();
1400
+ Connection connection = ConnectionFactory .createConnection (conf );
1401
+ Admin admin = connection .getAdmin ();
1402
+
1403
+ // 1. create a created table
1404
+ try {
1405
+ createTable (admin , TableName .valueOf ("t1" ), "cf1" , "cf2" , "cf3" );
1406
+ createTable (admin , TableName .valueOf ("t1" ), "cf1" , "cf2" , "cf3" );
1407
+ fail ();
1408
+ } catch (Exception e ) {
1409
+ Assert .assertEquals (e .getClass (), TableExistsException .class );
1410
+ } finally {
1411
+ admin .deleteTable (TableName .valueOf ("t1" ));
1412
+ }
1413
+
1414
+ // 2. delete a non-exist table
1415
+ try {
1416
+ admin .deleteTable (TableName .valueOf ("t1" ));
1417
+ fail ();
1418
+ } catch (Exception e ) {
1419
+ Assert .assertEquals (e .getClass (), TableNotFoundException .class );
1420
+ }
1421
+
1422
+ // 3. enable a enabled table
1423
+ try {
1424
+ createTable (admin , TableName .valueOf ("t1" ), "cf1" , "cf2" , "cf3" );
1425
+ admin .enableTable (TableName .valueOf ("t1" ));
1426
+ fail ();
1427
+ } catch (Exception e ) {
1428
+ Assert .assertEquals (e .getClass (), TableNotDisabledException .class );
1429
+ } finally {
1430
+ admin .deleteTable (TableName .valueOf ("t1" ));
1431
+ }
1432
+
1433
+ // 4. disable a disabled table
1434
+ try {
1435
+ createTable (admin , TableName .valueOf ("t1" ), "cf1" , "cf2" , "cf3" );
1436
+ admin .disableTable (TableName .valueOf ("t1" ));
1437
+ admin .disableTable (TableName .valueOf ("t1" ));
1438
+ fail ();
1439
+ } catch (Exception e ) {
1440
+ Assert .assertEquals (e .getClass (), TableNotEnabledException .class );
1441
+ } finally {
1442
+ admin .deleteTable (TableName .valueOf ("t1" ));
1443
+ }
1444
+
1445
+ // 5. get htable descriptor from an uncreated table
1446
+ try {
1447
+ HTableDescriptor descriptor = admin .getTableDescriptor (TableName .valueOf ("t1" ));
1448
+ fail ();
1449
+ } catch (Exception e ) {
1450
+ Assert .assertEquals (e .getClass (), TableNotFoundException .class );
1451
+ }
1452
+
1453
+ // 6. get region metrics from an uncreated table
1454
+ try {
1455
+ admin .getRegionMetrics (null , TableName .valueOf ("t1" ));
1456
+ fail ();
1457
+ } catch (Exception e ) {
1458
+ e .printStackTrace ();
1459
+ Assert .assertEquals (e .getClass (), TableNotFoundException .class );
1460
+ }
1461
+
1462
+ // 6. create a table in an uncreated namespace
1463
+ try {
1464
+ createTable (admin , TableName .valueOf ("t1" ), "cf1" , "cf2" , "cf3" );
1465
+ createTable (admin , TableName .valueOf ("t1" ), "cf1" , "cf2" , "cf3" );
1466
+ fail ();
1467
+ } catch (Exception e ) {
1468
+ Assert .assertEquals (e .getClass (), TableExistsException .class );
1469
+ } finally {
1470
+ admin .deleteTable (TableName .valueOf ("t1" ));
1471
+ }
1472
+
1473
+ // 7. delete a non-exist table in an uncreated namespace
1474
+ try {
1475
+ admin .deleteTable (TableName .valueOf ("t1" ));
1476
+ fail ();
1477
+ } catch (Exception e ) {
1478
+ Assert .assertEquals (e .getClass (), TableNotFoundException .class );
1479
+ }
1480
+
1481
+ // 8. enable a enabled table in an uncreated namespace
1482
+ try {
1483
+ createTable (admin , TableName .valueOf ("t1" ), "cf1" , "cf2" , "cf3" );
1484
+ admin .enableTable (TableName .valueOf ("t1" ));
1485
+ fail ();
1486
+ } catch (Exception e ) {
1487
+ Assert .assertEquals (e .getClass (), TableNotDisabledException .class );
1488
+ } finally {
1489
+ admin .deleteTable (TableName .valueOf ("t1" ));
1490
+ }
1491
+
1492
+ // 9. disable a disabled table in an uncreated namespace
1493
+ try {
1494
+ createTable (admin , TableName .valueOf ("t1" ), "cf1" , "cf2" , "cf3" );
1495
+ admin .disableTable (TableName .valueOf ("t1" ));
1496
+ admin .disableTable (TableName .valueOf ("t1" ));
1497
+ fail ();
1498
+ } catch (Exception e ) {
1499
+ Assert .assertEquals (e .getClass (), TableNotEnabledException .class );
1500
+ } finally {
1501
+ admin .deleteTable (TableName .valueOf ("t1" ));
1502
+ }
1503
+
1504
+ // 10. get a table metrics from an uncreated namespace
1505
+ try {
1506
+ admin .getRegionMetrics (null , TableName .valueOf ("n1:t1" ));
1507
+ fail ();
1508
+ } catch (Exception e ) {
1509
+ Assert .assertEquals (e .getClass (), NamespaceNotFoundException .class );
1510
+ }
1511
+
1512
+ // 11. check table exists from an uncreated namespace
1513
+ try {
1514
+ admin .tableExists (TableName .valueOf ("n1:t1" ));
1515
+ fail ();
1516
+ } catch (Exception e ) {
1517
+ Assert .assertEquals (e .getClass (), NamespaceNotFoundException .class );
1518
+ }
1519
+
1520
+ }
1332
1521
}
0 commit comments