Skip to content

Commit f45a552

Browse files
authored
Merge pull request #232 from oceanbase/optimize_region_metrics_case
Optimize region metrics case
2 parents cc25a35 + 0319f35 commit f45a552

File tree

1 file changed

+103
-87
lines changed

1 file changed

+103
-87
lines changed

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

Lines changed: 103 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import com.alipay.oceanbase.hbase.exception.FeatureNotSupportedException;
2323
import com.alipay.oceanbase.hbase.util.ResultSetPrinter;
2424
import com.alipay.oceanbase.rpc.exception.ObTableException;
25+
import com.alipay.oceanbase.rpc.exception.ObTableGetException;
2526
import com.alipay.oceanbase.rpc.protocol.payload.ResultCodes;
2627
import org.apache.hadoop.conf.Configuration;
2728
import org.apache.hadoop.hbase.*;
@@ -432,31 +433,49 @@ public void testAdminGetRegionMetrics() throws Exception {
432433
java.sql.Connection conn = ObHTableTestUtil.getConnection();
433434
Statement st = conn.createStatement();
434435
st.execute("CREATE TABLEGROUP IF NOT EXISTS test_multi_cf SHARDING = 'ADAPTIVE';\n" +
435-
"\n" +
436436
"CREATE TABLE IF NOT EXISTS `test_multi_cf$family_with_group1` (\n" +
437437
" `K` varbinary(1024) NOT NULL,\n" +
438438
" `Q` varbinary(256) NOT NULL,\n" +
439439
" `T` bigint(20) NOT NULL,\n" +
440440
" `V` varbinary(1024) DEFAULT NULL,\n" +
441441
" PRIMARY KEY (`K`, `Q`, `T`)\n" +
442-
") TABLEGROUP = test_multi_cf PARTITION BY KEY(`K`) PARTITIONS 3;\n" +
443-
"\n" +
442+
") TABLEGROUP = test_multi_cf PARTITION BY KEY(`K`) PARTITIONS 10;\n" +
444443
"CREATE TABLE IF NOT EXISTS `test_multi_cf$family_with_group2` (\n" +
445444
" `K` varbinary(1024) NOT NULL,\n" +
446445
" `Q` varbinary(256) NOT NULL,\n" +
447446
" `T` bigint(20) NOT NULL,\n" +
448447
" `V` varbinary(1024) DEFAULT NULL,\n" +
449448
" PRIMARY KEY (`K`, `Q`, `T`)\n" +
450-
") TABLEGROUP = test_multi_cf PARTITION BY KEY(`K`) PARTITIONS 3;\n" +
451-
"\n" +
449+
") TABLEGROUP = test_multi_cf PARTITION BY KEY(`K`) PARTITIONS 10;\n" +
452450
"CREATE TABLE IF NOT EXISTS `test_multi_cf$family_with_group3` (\n" +
453451
" `K` varbinary(1024) NOT NULL,\n" +
454452
" `Q` varbinary(256) NOT NULL,\n" +
455453
" `T` bigint(20) NOT NULL,\n" +
456454
" `V` varbinary(1024) DEFAULT NULL,\n" +
457455
" PRIMARY KEY (`K`, `Q`, `T`)\n" +
458-
") TABLEGROUP = test_multi_cf PARTITION BY KEY(`K`) PARTITIONS 3;\n" +
459-
"\n" +
456+
") TABLEGROUP = test_multi_cf PARTITION BY KEY(`K`) PARTITIONS 10;\n" +
457+
"CREATE TABLEGROUP IF NOT EXISTS test_no_part SHARDING = 'ADAPTIVE';\n"+
458+
"CREATE TABLE IF NOT EXISTS `test_no_part$family_with_group1` (\n" +
459+
" `K` varbinary(1024) NOT NULL,\n" +
460+
" `Q` varbinary(256) NOT NULL,\n" +
461+
" `T` bigint(20) NOT NULL,\n" +
462+
" `V` varbinary(1024) DEFAULT NULL,\n" +
463+
" PRIMARY KEY (`K`, `Q`, `T`)\n" +
464+
") TABLEGROUP = test_no_part;\n" +
465+
"CREATE TABLE IF NOT EXISTS `test_no_part$family_with_group2` (\n" +
466+
" `K` varbinary(1024) NOT NULL,\n" +
467+
" `Q` varbinary(256) NOT NULL,\n" +
468+
" `T` bigint(20) NOT NULL,\n" +
469+
" `V` varbinary(1024) DEFAULT NULL,\n" +
470+
" PRIMARY KEY (`K`, `Q`, `T`)\n" +
471+
") TABLEGROUP = test_no_part;\n" +
472+
"CREATE TABLE IF NOT EXISTS `test_no_part$family_with_group3` (\n" +
473+
" `K` varbinary(1024) NOT NULL,\n" +
474+
" `Q` varbinary(256) NOT NULL,\n" +
475+
" `T` bigint(20) NOT NULL,\n" +
476+
" `V` varbinary(1024) DEFAULT NULL,\n" +
477+
" PRIMARY KEY (`K`, `Q`, `T`)\n" +
478+
") TABLEGROUP = test_no_part;\n" +
460479
"CREATE DATABASE IF NOT EXISTS `n1`;\n" +
461480
"use `n1`;\n" +
462481
"CREATE TABLEGROUP IF NOT EXISTS `n1:test_multi_cf` SHARDING = 'ADAPTIVE';\n" +
@@ -488,25 +507,30 @@ public void testAdminGetRegionMetrics() throws Exception {
488507
Configuration conf = ObHTableTestUtil.newConfiguration();
489508
Connection connection = ConnectionFactory.createConnection(conf);
490509
Admin admin = connection.getAdmin();
510+
// test tablegroup not existed
491511
IOException thrown = assertThrows(IOException.class,
492512
() -> {
493513
admin.getRegionMetrics(null, TableName.valueOf("tablegroup_not_exists"));
494514
});
495515
Assert.assertTrue(thrown.getCause() instanceof ObTableException);
496516
Assert.assertEquals(ResultCodes.OB_TABLEGROUP_NOT_EXIST.errorCode, ((ObTableException) thrown.getCause()).getErrorCode());
517+
518+
// test use serverName without tableName to get region metrics
497519
assertThrows(FeatureNotSupportedException.class,
498520
() -> {
499521
admin.getRegionMetrics(ServerName.valueOf("localhost,1,1"));
500522
});
501-
// insert 300 thousand of rows in each table under tablegroup test_multi_cf
523+
524+
// test single-thread getRegionMetrics after writing
502525
batchInsert(100000, tablegroup1);
503-
List<RegionMetrics> metrics = admin.getRegionMetrics(null, TableName.valueOf(tablegroup1));
504-
for (RegionMetrics regionMetrics : metrics) {
505-
System.out.println("region name: " + regionMetrics.getNameAsString()
506-
+ ", storeFileSize: " + regionMetrics.getStoreFileSize()
507-
+ ", memFileSize: " + regionMetrics.getMemStoreSize());
508-
}
509-
// concurrently read while writing 150 thousand of rows to 2 tablegroups
526+
// test ServerName is any string
527+
long start = System.currentTimeMillis();
528+
List<RegionMetrics> metrics = admin.getRegionMetrics(ServerName.valueOf("localhost,1,1"), TableName.valueOf(tablegroup1));
529+
long cost = System.currentTimeMillis() - start;
530+
System.out.println("get region metrics time usage: " + cost + "ms, tablegroup: " + tablegroup1);
531+
assertEquals(30, metrics.size());
532+
533+
// test getRegionMetrics concurrently reading while writing
510534
ExecutorService executorService = Executors.newFixedThreadPool(10);
511535
CountDownLatch latch = new CountDownLatch(100);
512536
List<Exception> exceptionCatcher = new ArrayList<>();
@@ -518,21 +542,42 @@ public void testAdminGetRegionMetrics() throws Exception {
518542
List<RegionMetrics> regionMetrics = null;
519543
// test get regionMetrics from different namespaces
520544
if (taskId % 3 != 0) {
545+
long thrStart = System.currentTimeMillis();
521546
regionMetrics = admin.getRegionMetrics(null, TableName.valueOf(tablegroup1));
547+
long thrCost = System.currentTimeMillis() - thrStart;
548+
System.out.println("task: " + taskId + ", get region metrics time usage: " + thrCost + "ms, tablegroup: " + tablegroup1);
549+
if (regionMetrics.size() != 30) {
550+
throw new ObTableGetException(
551+
"the number of region metrics does not match the number of tablets, the number of region metrics: " + regionMetrics.size());
552+
}
522553
} else {
554+
long thrStart = System.currentTimeMillis();
523555
regionMetrics = admin.getRegionMetrics(null, TableName.valueOf(tablegroup2));
524-
}
525-
for (RegionMetrics m : regionMetrics) {
526-
System.out.println("task: " + taskId + ", tablegroup: " + ((OHRegionMetrics) m).getTablegroup()
527-
+ ", region name: " + m.getNameAsString()
528-
+ ", storeFileSize: " + m.getStoreFileSize()
529-
+ ", memFileSize: " + m.getMemStoreSize());
556+
long thrCost = System.currentTimeMillis() - thrStart;
557+
System.out.println("task: " + taskId + ", get region metrics time usage: " + thrCost + "ms, tablegroup: " + tablegroup1);
558+
if (regionMetrics.size() != 9) {
559+
throw new ObTableGetException(
560+
"the number of region metrics does not match the number of tablets, the number of region metrics: " + regionMetrics.size());
561+
}
530562
}
531563
} else {
532-
if (taskId % 8 == 0) {
533-
batchInsert(1000, tablegroup2);
534-
} else {
535-
batchInsert(1000, tablegroup1);
564+
try {
565+
if (taskId % 8 == 0) {
566+
batchInsert(1000, tablegroup2);
567+
} else {
568+
batchInsert(1000, tablegroup1);
569+
}
570+
} catch (Exception e) {
571+
Exception originalCause = e;
572+
while (originalCause.getCause() != null && originalCause.getCause() instanceof ObTableException) {
573+
originalCause = (Exception) originalCause.getCause();
574+
}
575+
if (originalCause instanceof ObTableException && ((ObTableException) originalCause).getErrorCode() == ResultCodes.OB_TIMEOUT.errorCode) {
576+
// ignore
577+
System.out.println("taskId: " + taskId + " OB_TIMEOUT");
578+
} else {
579+
throw e;
580+
}
536581
}
537582
System.out.println("task: " + taskId + ", batchInsert");
538583
}
@@ -552,72 +597,76 @@ public void testAdminGetRegionMetrics() throws Exception {
552597
}
553598
executorService.shutdownNow();
554599
Assert.assertTrue(exceptionCatcher.isEmpty());
600+
601+
// test getRegionMetrics from non-partitioned table
602+
String non_part_tablegroup = "test_no_part";
603+
batchInsert(10000, non_part_tablegroup);
604+
start = System.currentTimeMillis();
605+
metrics = admin.getRegionMetrics(null, TableName.valueOf(non_part_tablegroup));
606+
cost = System.currentTimeMillis() - start;
607+
System.out.println("get region metrics time usage: " + cost + "ms, tablegroup: " + non_part_tablegroup);
608+
assertEquals(3, metrics.size());
555609
}
556610

557611
@Test
558612
public void testAdminDeleteTable() throws Exception {
559613
java.sql.Connection conn = ObHTableTestUtil.getConnection();
560614
Statement st = conn.createStatement();
561615
st.execute("CREATE TABLEGROUP IF NOT EXISTS test_multi_cf SHARDING = 'ADAPTIVE';\n" +
562-
"\n" +
563616
"CREATE TABLE IF NOT EXISTS `test_multi_cf$family_with_group1` (\n" +
564617
" `K` varbinary(1024) NOT NULL,\n" +
565618
" `Q` varbinary(256) NOT NULL,\n" +
566619
" `T` bigint(20) NOT NULL,\n" +
567620
" `V` varbinary(1024) DEFAULT NULL,\n" +
568621
" PRIMARY KEY (`K`, `Q`, `T`)\n" +
569-
") TABLEGROUP = test_multi_cf PARTITION BY KEY(`K`) PARTITIONS 3;\n" +
570-
"\n" +
622+
") TABLEGROUP = test_multi_cf PARTITION BY KEY(`K`) PARTITIONS 10;\n" +
571623
"CREATE TABLE IF NOT EXISTS `test_multi_cf$family_with_group2` (\n" +
572624
" `K` varbinary(1024) NOT NULL,\n" +
573625
" `Q` varbinary(256) NOT NULL,\n" +
574626
" `T` bigint(20) NOT NULL,\n" +
575627
" `V` varbinary(1024) DEFAULT NULL,\n" +
576628
" PRIMARY KEY (`K`, `Q`, `T`)\n" +
577-
") TABLEGROUP = test_multi_cf PARTITION BY KEY(`K`) PARTITIONS 3;\n" +
578-
"\n" +
629+
") TABLEGROUP = test_multi_cf PARTITION BY KEY(`K`) PARTITIONS 10;\n" +
579630
"CREATE TABLE IF NOT EXISTS `test_multi_cf$family_with_group3` (\n" +
580631
" `K` varbinary(1024) NOT NULL,\n" +
581632
" `Q` varbinary(256) NOT NULL,\n" +
582633
" `T` bigint(20) NOT NULL,\n" +
583634
" `V` varbinary(1024) DEFAULT NULL,\n" +
584635
" PRIMARY KEY (`K`, `Q`, `T`)\n" +
585-
") TABLEGROUP = test_multi_cf PARTITION BY KEY(`K`) PARTITIONS 3;\n" +
586-
"\n" +
636+
") TABLEGROUP = test_multi_cf PARTITION BY KEY(`K`) PARTITIONS 10;\n" +
587637
"CREATE DATABASE IF NOT EXISTS `n1`;\n" +
588638
"use `n1`;\n" +
589-
"CREATE TABLEGROUP IF NOT EXISTS `n1:test` SHARDING = 'ADAPTIVE';\n" +
590-
"CREATE TABLE IF NOT EXISTS `n1:test$family_group` (\n" +
591-
" `K` varbinary(1024) NOT NULL,\n" +
592-
" `Q` varbinary(256) NOT NULL,\n" +
593-
" `T` bigint(20) NOT NULL,\n" +
594-
" `V` varbinary(1024) DEFAULT NULL,\n" +
595-
" PRIMARY KEY (`K`, `Q`, `T`)\n" +
596-
") TABLEGROUP = `n1:test`;" +
597-
"\n" +
598-
"CREATE TABLE IF NOT EXISTS `n1:test$family1` (\n" +
599-
" `K` varbinary(1024) NOT NULL,\n" +
600-
" `Q` varbinary(256) NOT NULL,\n" +
601-
" `T` bigint(20) NOT NULL,\n" +
602-
" `V` varbinary(1024) DEFAULT NULL,\n" +
603-
" PRIMARY KEY (`K`, `Q`, `T`)\n" +
604-
") TABLEGROUP = `n1:test`;");
639+
"CREATE TABLEGROUP IF NOT EXISTS `n1:test_multi_cf` SHARDING = 'ADAPTIVE';\n" +
640+
"CREATE TABLE IF NOT EXISTS `n1:test_multi_cf$family_with_group1` (\n" +
641+
" `K` varbinary(1024) NOT NULL,\n" +
642+
" `Q` varbinary(256) NOT NULL,\n" +
643+
" `T` bigint(20) NOT NULL,\n" +
644+
" `V` varbinary(1024) DEFAULT NULL,\n" +
645+
" PRIMARY KEY (`K`, `Q`, `T`)\n" +
646+
") TABLEGROUP = `n1:test_multi_cf` PARTITION BY KEY(`K`) PARTITIONS 3;\n" +
647+
"CREATE TABLE IF NOT EXISTS `n1:test_multi_cf$family_with_group2` (\n" +
648+
" `K` varbinary(1024) NOT NULL,\n" +
649+
" `Q` varbinary(256) NOT NULL,\n" +
650+
" `T` bigint(20) NOT NULL,\n" +
651+
" `V` varbinary(1024) DEFAULT NULL,\n" +
652+
" PRIMARY KEY (`K`, `Q`, `T`)\n" +
653+
") TABLEGROUP = `n1:test_multi_cf` PARTITION BY KEY(`K`) PARTITIONS 3;");
605654
st.close();
606655
conn.close();
607656
Configuration conf = ObHTableTestUtil.newConfiguration();
608657
Connection connection = ConnectionFactory.createConnection(conf);
609658
Admin admin = connection.getAdmin();
610-
assertTrue(admin.tableExists(TableName.valueOf("n1", "test")));
659+
assertTrue(admin.tableExists(TableName.valueOf("n1", "test_multi_cf")));
611660
assertTrue(admin.tableExists(TableName.valueOf("test_multi_cf")));
612661
IOException thrown = assertThrows(IOException.class,
613662
() -> {
614663
admin.deleteTable(TableName.valueOf("tablegroup_not_exists"));
615664
});
616665
Assert.assertTrue(thrown.getCause() instanceof ObTableException);
617666
Assert.assertEquals(ResultCodes.OB_TABLEGROUP_NOT_EXIST.errorCode, ((ObTableException) thrown.getCause()).getErrorCode());
618-
admin.deleteTable(TableName.valueOf("n1", "test"));
667+
admin.deleteTable(TableName.valueOf("n1", "test_multi_cf"));
619668
admin.deleteTable(TableName.valueOf("test_multi_cf"));
620-
assertFalse(admin.tableExists(TableName.valueOf("n1", "test")));
669+
assertFalse(admin.tableExists(TableName.valueOf("n1", "test_multi_cf")));
621670
assertFalse(admin.tableExists(TableName.valueOf("test_multi_cf")));
622671
}
623672

@@ -626,49 +675,16 @@ public void testAdminTableExists() throws Exception {
626675
java.sql.Connection conn = ObHTableTestUtil.getConnection();
627676
Statement st = conn.createStatement();
628677
st.execute("CREATE TABLEGROUP IF NOT EXISTS test_multi_cf SHARDING = 'ADAPTIVE';\n" +
629-
"\n" +
630678
"CREATE TABLE IF NOT EXISTS `test_multi_cf$family_with_group1` (\n" +
631679
" `K` varbinary(1024) NOT NULL,\n" +
632680
" `Q` varbinary(256) NOT NULL,\n" +
633681
" `T` bigint(20) NOT NULL,\n" +
634682
" `V` varbinary(1024) DEFAULT NULL,\n" +
635683
" PRIMARY KEY (`K`, `Q`, `T`)\n" +
636-
") TABLEGROUP = test_multi_cf PARTITION BY KEY(`K`) PARTITIONS 3;\n" +
637-
"\n" +
638-
"CREATE TABLE IF NOT EXISTS `test_multi_cf$family_with_group2` (\n" +
639-
" `K` varbinary(1024) NOT NULL,\n" +
640-
" `Q` varbinary(256) NOT NULL,\n" +
641-
" `T` bigint(20) NOT NULL,\n" +
642-
" `V` varbinary(1024) DEFAULT NULL,\n" +
643-
" PRIMARY KEY (`K`, `Q`, `T`)\n" +
644-
") TABLEGROUP = test_multi_cf PARTITION BY KEY(`K`) PARTITIONS 3;\n" +
645-
"\n" +
646-
"CREATE TABLE IF NOT EXISTS `test_multi_cf$family_with_group3` (\n" +
647-
" `K` varbinary(1024) NOT NULL,\n" +
648-
" `Q` varbinary(256) NOT NULL,\n" +
649-
" `T` bigint(20) NOT NULL,\n" +
650-
" `V` varbinary(1024) DEFAULT NULL,\n" +
651-
" PRIMARY KEY (`K`, `Q`, `T`)\n" +
652-
") TABLEGROUP = test_multi_cf PARTITION BY KEY(`K`) PARTITIONS 3;\n" +
653-
"\n" +
684+
") TABLEGROUP = test_multi_cf PARTITION BY KEY(`K`) PARTITIONS 10;\n" +
654685
"CREATE DATABASE IF NOT EXISTS `n1`;\n" +
655686
"use `n1`;\n" +
656-
"CREATE TABLEGROUP IF NOT EXISTS `n1:test` SHARDING = 'ADAPTIVE';\n" +
657-
"CREATE TABLE IF NOT EXISTS `n1:test$family_group` (\n" +
658-
" `K` varbinary(1024) NOT NULL,\n" +
659-
" `Q` varbinary(256) NOT NULL,\n" +
660-
" `T` bigint(20) NOT NULL,\n" +
661-
" `V` varbinary(1024) DEFAULT NULL,\n" +
662-
" PRIMARY KEY (`K`, `Q`, `T`)\n" +
663-
") TABLEGROUP = `n1:test`;" +
664-
"\n" +
665-
"CREATE TABLE IF NOT EXISTS `n1:test$family1` (\n" +
666-
" `K` varbinary(1024) NOT NULL,\n" +
667-
" `Q` varbinary(256) NOT NULL,\n" +
668-
" `T` bigint(20) NOT NULL,\n" +
669-
" `V` varbinary(1024) DEFAULT NULL,\n" +
670-
" PRIMARY KEY (`K`, `Q`, `T`)\n" +
671-
") TABLEGROUP = `n1:test`;");
687+
"CREATE TABLEGROUP IF NOT EXISTS `n1:test_multi_cf` SHARDING = 'ADAPTIVE';");
672688
st.close();
673689
conn.close();
674690
Configuration conf = ObHTableTestUtil.newConfiguration();
@@ -680,8 +696,8 @@ public void testAdminTableExists() throws Exception {
680696
TableName.valueOf("random_string$");
681697
});
682698
Assert.assertFalse(admin.tableExists(TableName.valueOf("tablegroup_not_exists")));
699+
Assert.assertTrue(admin.tableExists(TableName.valueOf("n1", "test_multi_cf")));
683700
Assert.assertTrue(admin.tableExists(TableName.valueOf("test_multi_cf")));
684-
Assert.assertTrue(admin.tableExists(TableName.valueOf("n1", "test")));
685701
}
686702

687703
private void batchInsert(int rows, String tablegroup) throws Exception {

0 commit comments

Comments
 (0)