Skip to content

Commit 23bdee7

Browse files
JackShi148Zhou-jw
authored andcommitted
optimize region metrics case
1 parent 0708a1e commit 23bdee7

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.*;
@@ -388,31 +389,49 @@ public void testAdminGetRegionMetrics() throws Exception {
388389
java.sql.Connection conn = ObHTableTestUtil.getConnection();
389390
Statement st = conn.createStatement();
390391
st.execute("CREATE TABLEGROUP IF NOT EXISTS test_multi_cf SHARDING = 'ADAPTIVE';\n" +
391-
"\n" +
392392
"CREATE TABLE IF NOT EXISTS `test_multi_cf$family_with_group1` (\n" +
393393
" `K` varbinary(1024) NOT NULL,\n" +
394394
" `Q` varbinary(256) NOT NULL,\n" +
395395
" `T` bigint(20) NOT NULL,\n" +
396396
" `V` varbinary(1024) DEFAULT NULL,\n" +
397397
" PRIMARY KEY (`K`, `Q`, `T`)\n" +
398-
") TABLEGROUP = test_multi_cf PARTITION BY KEY(`K`) PARTITIONS 3;\n" +
399-
"\n" +
398+
") TABLEGROUP = test_multi_cf PARTITION BY KEY(`K`) PARTITIONS 10;\n" +
400399
"CREATE TABLE IF NOT EXISTS `test_multi_cf$family_with_group2` (\n" +
401400
" `K` varbinary(1024) NOT NULL,\n" +
402401
" `Q` varbinary(256) NOT NULL,\n" +
403402
" `T` bigint(20) NOT NULL,\n" +
404403
" `V` varbinary(1024) DEFAULT NULL,\n" +
405404
" PRIMARY KEY (`K`, `Q`, `T`)\n" +
406-
") TABLEGROUP = test_multi_cf PARTITION BY KEY(`K`) PARTITIONS 3;\n" +
407-
"\n" +
405+
") TABLEGROUP = test_multi_cf PARTITION BY KEY(`K`) PARTITIONS 10;\n" +
408406
"CREATE TABLE IF NOT EXISTS `test_multi_cf$family_with_group3` (\n" +
409407
" `K` varbinary(1024) NOT NULL,\n" +
410408
" `Q` varbinary(256) NOT NULL,\n" +
411409
" `T` bigint(20) NOT NULL,\n" +
412410
" `V` varbinary(1024) DEFAULT NULL,\n" +
413411
" PRIMARY KEY (`K`, `Q`, `T`)\n" +
414-
") TABLEGROUP = test_multi_cf PARTITION BY KEY(`K`) PARTITIONS 3;\n" +
415-
"\n" +
412+
") TABLEGROUP = test_multi_cf PARTITION BY KEY(`K`) PARTITIONS 10;\n" +
413+
"CREATE TABLEGROUP IF NOT EXISTS test_no_part SHARDING = 'ADAPTIVE';\n"+
414+
"CREATE TABLE IF NOT EXISTS `test_no_part$family_with_group1` (\n" +
415+
" `K` varbinary(1024) NOT NULL,\n" +
416+
" `Q` varbinary(256) NOT NULL,\n" +
417+
" `T` bigint(20) NOT NULL,\n" +
418+
" `V` varbinary(1024) DEFAULT NULL,\n" +
419+
" PRIMARY KEY (`K`, `Q`, `T`)\n" +
420+
") TABLEGROUP = test_no_part;\n" +
421+
"CREATE TABLE IF NOT EXISTS `test_no_part$family_with_group2` (\n" +
422+
" `K` varbinary(1024) NOT NULL,\n" +
423+
" `Q` varbinary(256) NOT NULL,\n" +
424+
" `T` bigint(20) NOT NULL,\n" +
425+
" `V` varbinary(1024) DEFAULT NULL,\n" +
426+
" PRIMARY KEY (`K`, `Q`, `T`)\n" +
427+
") TABLEGROUP = test_no_part;\n" +
428+
"CREATE TABLE IF NOT EXISTS `test_no_part$family_with_group3` (\n" +
429+
" `K` varbinary(1024) NOT NULL,\n" +
430+
" `Q` varbinary(256) NOT NULL,\n" +
431+
" `T` bigint(20) NOT NULL,\n" +
432+
" `V` varbinary(1024) DEFAULT NULL,\n" +
433+
" PRIMARY KEY (`K`, `Q`, `T`)\n" +
434+
") TABLEGROUP = test_no_part;\n" +
416435
"CREATE DATABASE IF NOT EXISTS `n1`;\n" +
417436
"use `n1`;\n" +
418437
"CREATE TABLEGROUP IF NOT EXISTS `n1:test_multi_cf` SHARDING = 'ADAPTIVE';\n" +
@@ -444,25 +463,30 @@ public void testAdminGetRegionMetrics() throws Exception {
444463
Configuration conf = ObHTableTestUtil.newConfiguration();
445464
Connection connection = ConnectionFactory.createConnection(conf);
446465
Admin admin = connection.getAdmin();
466+
// test tablegroup not existed
447467
IOException thrown = assertThrows(IOException.class,
448468
() -> {
449469
admin.getRegionMetrics(null, TableName.valueOf("tablegroup_not_exists"));
450470
});
451471
Assert.assertTrue(thrown.getCause() instanceof ObTableException);
452472
Assert.assertEquals(ResultCodes.OB_TABLEGROUP_NOT_EXIST.errorCode, ((ObTableException) thrown.getCause()).getErrorCode());
473+
474+
// test use serverName without tableName to get region metrics
453475
assertThrows(FeatureNotSupportedException.class,
454476
() -> {
455477
admin.getRegionMetrics(ServerName.valueOf("localhost,1,1"));
456478
});
457-
// insert 300 thousand of rows in each table under tablegroup test_multi_cf
479+
480+
// test single-thread getRegionMetrics after writing
458481
batchInsert(100000, tablegroup1);
459-
List<RegionMetrics> metrics = admin.getRegionMetrics(null, TableName.valueOf(tablegroup1));
460-
for (RegionMetrics regionMetrics : metrics) {
461-
System.out.println("region name: " + regionMetrics.getNameAsString()
462-
+ ", storeFileSize: " + regionMetrics.getStoreFileSize()
463-
+ ", memFileSize: " + regionMetrics.getMemStoreSize());
464-
}
465-
// concurrently read while writing 150 thousand of rows to 2 tablegroups
482+
// test ServerName is any string
483+
long start = System.currentTimeMillis();
484+
List<RegionMetrics> metrics = admin.getRegionMetrics(ServerName.valueOf("localhost,1,1"), TableName.valueOf(tablegroup1));
485+
long cost = System.currentTimeMillis() - start;
486+
System.out.println("get region metrics time usage: " + cost + "ms, tablegroup: " + tablegroup1);
487+
assertEquals(30, metrics.size());
488+
489+
// test getRegionMetrics concurrently reading while writing
466490
ExecutorService executorService = Executors.newFixedThreadPool(10);
467491
CountDownLatch latch = new CountDownLatch(100);
468492
List<Exception> exceptionCatcher = new ArrayList<>();
@@ -474,21 +498,42 @@ public void testAdminGetRegionMetrics() throws Exception {
474498
List<RegionMetrics> regionMetrics = null;
475499
// test get regionMetrics from different namespaces
476500
if (taskId % 3 != 0) {
501+
long thrStart = System.currentTimeMillis();
477502
regionMetrics = admin.getRegionMetrics(null, TableName.valueOf(tablegroup1));
503+
long thrCost = System.currentTimeMillis() - thrStart;
504+
System.out.println("task: " + taskId + ", get region metrics time usage: " + thrCost + "ms, tablegroup: " + tablegroup1);
505+
if (regionMetrics.size() != 30) {
506+
throw new ObTableGetException(
507+
"the number of region metrics does not match the number of tablets, the number of region metrics: " + regionMetrics.size());
508+
}
478509
} else {
510+
long thrStart = System.currentTimeMillis();
479511
regionMetrics = admin.getRegionMetrics(null, TableName.valueOf(tablegroup2));
480-
}
481-
for (RegionMetrics m : regionMetrics) {
482-
System.out.println("task: " + taskId + ", tablegroup: " + ((OHRegionMetrics) m).getTablegroup()
483-
+ ", region name: " + m.getNameAsString()
484-
+ ", storeFileSize: " + m.getStoreFileSize()
485-
+ ", memFileSize: " + m.getMemStoreSize());
512+
long thrCost = System.currentTimeMillis() - thrStart;
513+
System.out.println("task: " + taskId + ", get region metrics time usage: " + thrCost + "ms, tablegroup: " + tablegroup1);
514+
if (regionMetrics.size() != 9) {
515+
throw new ObTableGetException(
516+
"the number of region metrics does not match the number of tablets, the number of region metrics: " + regionMetrics.size());
517+
}
486518
}
487519
} else {
488-
if (taskId % 8 == 0) {
489-
batchInsert(1000, tablegroup2);
490-
} else {
491-
batchInsert(1000, tablegroup1);
520+
try {
521+
if (taskId % 8 == 0) {
522+
batchInsert(1000, tablegroup2);
523+
} else {
524+
batchInsert(1000, tablegroup1);
525+
}
526+
} catch (Exception e) {
527+
Exception originalCause = e;
528+
while (originalCause.getCause() != null && originalCause.getCause() instanceof ObTableException) {
529+
originalCause = (Exception) originalCause.getCause();
530+
}
531+
if (originalCause instanceof ObTableException && ((ObTableException) originalCause).getErrorCode() == ResultCodes.OB_TIMEOUT.errorCode) {
532+
// ignore
533+
System.out.println("taskId: " + taskId + " OB_TIMEOUT");
534+
} else {
535+
throw e;
536+
}
492537
}
493538
System.out.println("task: " + taskId + ", batchInsert");
494539
}
@@ -508,72 +553,76 @@ public void testAdminGetRegionMetrics() throws Exception {
508553
}
509554
executorService.shutdownNow();
510555
Assert.assertTrue(exceptionCatcher.isEmpty());
556+
557+
// test getRegionMetrics from non-partitioned table
558+
String non_part_tablegroup = "test_no_part";
559+
batchInsert(10000, non_part_tablegroup);
560+
start = System.currentTimeMillis();
561+
metrics = admin.getRegionMetrics(null, TableName.valueOf(non_part_tablegroup));
562+
cost = System.currentTimeMillis() - start;
563+
System.out.println("get region metrics time usage: " + cost + "ms, tablegroup: " + non_part_tablegroup);
564+
assertEquals(3, metrics.size());
511565
}
512566

513567
@Test
514568
public void testAdminDeleteTable() throws Exception {
515569
java.sql.Connection conn = ObHTableTestUtil.getConnection();
516570
Statement st = conn.createStatement();
517571
st.execute("CREATE TABLEGROUP IF NOT EXISTS test_multi_cf SHARDING = 'ADAPTIVE';\n" +
518-
"\n" +
519572
"CREATE TABLE IF NOT EXISTS `test_multi_cf$family_with_group1` (\n" +
520573
" `K` varbinary(1024) NOT NULL,\n" +
521574
" `Q` varbinary(256) NOT NULL,\n" +
522575
" `T` bigint(20) NOT NULL,\n" +
523576
" `V` varbinary(1024) DEFAULT NULL,\n" +
524577
" PRIMARY KEY (`K`, `Q`, `T`)\n" +
525-
") TABLEGROUP = test_multi_cf PARTITION BY KEY(`K`) PARTITIONS 3;\n" +
526-
"\n" +
578+
") TABLEGROUP = test_multi_cf PARTITION BY KEY(`K`) PARTITIONS 10;\n" +
527579
"CREATE TABLE IF NOT EXISTS `test_multi_cf$family_with_group2` (\n" +
528580
" `K` varbinary(1024) NOT NULL,\n" +
529581
" `Q` varbinary(256) NOT NULL,\n" +
530582
" `T` bigint(20) NOT NULL,\n" +
531583
" `V` varbinary(1024) DEFAULT NULL,\n" +
532584
" PRIMARY KEY (`K`, `Q`, `T`)\n" +
533-
") TABLEGROUP = test_multi_cf PARTITION BY KEY(`K`) PARTITIONS 3;\n" +
534-
"\n" +
585+
") TABLEGROUP = test_multi_cf PARTITION BY KEY(`K`) PARTITIONS 10;\n" +
535586
"CREATE TABLE IF NOT EXISTS `test_multi_cf$family_with_group3` (\n" +
536587
" `K` varbinary(1024) NOT NULL,\n" +
537588
" `Q` varbinary(256) NOT NULL,\n" +
538589
" `T` bigint(20) NOT NULL,\n" +
539590
" `V` varbinary(1024) DEFAULT NULL,\n" +
540591
" PRIMARY KEY (`K`, `Q`, `T`)\n" +
541-
") TABLEGROUP = test_multi_cf PARTITION BY KEY(`K`) PARTITIONS 3;\n" +
542-
"\n" +
592+
") TABLEGROUP = test_multi_cf PARTITION BY KEY(`K`) PARTITIONS 10;\n" +
543593
"CREATE DATABASE IF NOT EXISTS `n1`;\n" +
544594
"use `n1`;\n" +
545-
"CREATE TABLEGROUP IF NOT EXISTS `n1:test` SHARDING = 'ADAPTIVE';\n" +
546-
"CREATE TABLE IF NOT EXISTS `n1:test$family_group` (\n" +
547-
" `K` varbinary(1024) NOT NULL,\n" +
548-
" `Q` varbinary(256) NOT NULL,\n" +
549-
" `T` bigint(20) NOT NULL,\n" +
550-
" `V` varbinary(1024) DEFAULT NULL,\n" +
551-
" PRIMARY KEY (`K`, `Q`, `T`)\n" +
552-
") TABLEGROUP = `n1:test`;" +
553-
"\n" +
554-
"CREATE TABLE IF NOT EXISTS `n1:test$family1` (\n" +
555-
" `K` varbinary(1024) NOT NULL,\n" +
556-
" `Q` varbinary(256) NOT NULL,\n" +
557-
" `T` bigint(20) NOT NULL,\n" +
558-
" `V` varbinary(1024) DEFAULT NULL,\n" +
559-
" PRIMARY KEY (`K`, `Q`, `T`)\n" +
560-
") TABLEGROUP = `n1:test`;");
595+
"CREATE TABLEGROUP IF NOT EXISTS `n1:test_multi_cf` SHARDING = 'ADAPTIVE';\n" +
596+
"CREATE TABLE IF NOT EXISTS `n1:test_multi_cf$family_with_group1` (\n" +
597+
" `K` varbinary(1024) NOT NULL,\n" +
598+
" `Q` varbinary(256) NOT NULL,\n" +
599+
" `T` bigint(20) NOT NULL,\n" +
600+
" `V` varbinary(1024) DEFAULT NULL,\n" +
601+
" PRIMARY KEY (`K`, `Q`, `T`)\n" +
602+
") TABLEGROUP = `n1:test_multi_cf` PARTITION BY KEY(`K`) PARTITIONS 3;\n" +
603+
"CREATE TABLE IF NOT EXISTS `n1:test_multi_cf$family_with_group2` (\n" +
604+
" `K` varbinary(1024) NOT NULL,\n" +
605+
" `Q` varbinary(256) NOT NULL,\n" +
606+
" `T` bigint(20) NOT NULL,\n" +
607+
" `V` varbinary(1024) DEFAULT NULL,\n" +
608+
" PRIMARY KEY (`K`, `Q`, `T`)\n" +
609+
") TABLEGROUP = `n1:test_multi_cf` PARTITION BY KEY(`K`) PARTITIONS 3;");
561610
st.close();
562611
conn.close();
563612
Configuration conf = ObHTableTestUtil.newConfiguration();
564613
Connection connection = ConnectionFactory.createConnection(conf);
565614
Admin admin = connection.getAdmin();
566-
assertTrue(admin.tableExists(TableName.valueOf("n1", "test")));
615+
assertTrue(admin.tableExists(TableName.valueOf("n1", "test_multi_cf")));
567616
assertTrue(admin.tableExists(TableName.valueOf("test_multi_cf")));
568617
IOException thrown = assertThrows(IOException.class,
569618
() -> {
570619
admin.deleteTable(TableName.valueOf("tablegroup_not_exists"));
571620
});
572621
Assert.assertTrue(thrown.getCause() instanceof ObTableException);
573622
Assert.assertEquals(ResultCodes.OB_TABLEGROUP_NOT_EXIST.errorCode, ((ObTableException) thrown.getCause()).getErrorCode());
574-
admin.deleteTable(TableName.valueOf("n1", "test"));
623+
admin.deleteTable(TableName.valueOf("n1", "test_multi_cf"));
575624
admin.deleteTable(TableName.valueOf("test_multi_cf"));
576-
assertFalse(admin.tableExists(TableName.valueOf("n1", "test")));
625+
assertFalse(admin.tableExists(TableName.valueOf("n1", "test_multi_cf")));
577626
assertFalse(admin.tableExists(TableName.valueOf("test_multi_cf")));
578627
}
579628

@@ -582,49 +631,16 @@ public void testAdminTableExists() throws Exception {
582631
java.sql.Connection conn = ObHTableTestUtil.getConnection();
583632
Statement st = conn.createStatement();
584633
st.execute("CREATE TABLEGROUP IF NOT EXISTS test_multi_cf SHARDING = 'ADAPTIVE';\n" +
585-
"\n" +
586634
"CREATE TABLE IF NOT EXISTS `test_multi_cf$family_with_group1` (\n" +
587635
" `K` varbinary(1024) NOT NULL,\n" +
588636
" `Q` varbinary(256) NOT NULL,\n" +
589637
" `T` bigint(20) NOT NULL,\n" +
590638
" `V` varbinary(1024) DEFAULT NULL,\n" +
591639
" PRIMARY KEY (`K`, `Q`, `T`)\n" +
592-
") TABLEGROUP = test_multi_cf PARTITION BY KEY(`K`) PARTITIONS 3;\n" +
593-
"\n" +
594-
"CREATE TABLE IF NOT EXISTS `test_multi_cf$family_with_group2` (\n" +
595-
" `K` varbinary(1024) NOT NULL,\n" +
596-
" `Q` varbinary(256) NOT NULL,\n" +
597-
" `T` bigint(20) NOT NULL,\n" +
598-
" `V` varbinary(1024) DEFAULT NULL,\n" +
599-
" PRIMARY KEY (`K`, `Q`, `T`)\n" +
600-
") TABLEGROUP = test_multi_cf PARTITION BY KEY(`K`) PARTITIONS 3;\n" +
601-
"\n" +
602-
"CREATE TABLE IF NOT EXISTS `test_multi_cf$family_with_group3` (\n" +
603-
" `K` varbinary(1024) NOT NULL,\n" +
604-
" `Q` varbinary(256) NOT NULL,\n" +
605-
" `T` bigint(20) NOT NULL,\n" +
606-
" `V` varbinary(1024) DEFAULT NULL,\n" +
607-
" PRIMARY KEY (`K`, `Q`, `T`)\n" +
608-
") TABLEGROUP = test_multi_cf PARTITION BY KEY(`K`) PARTITIONS 3;\n" +
609-
"\n" +
640+
") TABLEGROUP = test_multi_cf PARTITION BY KEY(`K`) PARTITIONS 10;\n" +
610641
"CREATE DATABASE IF NOT EXISTS `n1`;\n" +
611642
"use `n1`;\n" +
612-
"CREATE TABLEGROUP IF NOT EXISTS `n1:test` SHARDING = 'ADAPTIVE';\n" +
613-
"CREATE TABLE IF NOT EXISTS `n1:test$family_group` (\n" +
614-
" `K` varbinary(1024) NOT NULL,\n" +
615-
" `Q` varbinary(256) NOT NULL,\n" +
616-
" `T` bigint(20) NOT NULL,\n" +
617-
" `V` varbinary(1024) DEFAULT NULL,\n" +
618-
" PRIMARY KEY (`K`, `Q`, `T`)\n" +
619-
") TABLEGROUP = `n1:test`;" +
620-
"\n" +
621-
"CREATE TABLE IF NOT EXISTS `n1:test$family1` (\n" +
622-
" `K` varbinary(1024) NOT NULL,\n" +
623-
" `Q` varbinary(256) NOT NULL,\n" +
624-
" `T` bigint(20) NOT NULL,\n" +
625-
" `V` varbinary(1024) DEFAULT NULL,\n" +
626-
" PRIMARY KEY (`K`, `Q`, `T`)\n" +
627-
") TABLEGROUP = `n1:test`;");
643+
"CREATE TABLEGROUP IF NOT EXISTS `n1:test_multi_cf` SHARDING = 'ADAPTIVE';");
628644
st.close();
629645
conn.close();
630646
Configuration conf = ObHTableTestUtil.newConfiguration();
@@ -636,8 +652,8 @@ public void testAdminTableExists() throws Exception {
636652
TableName.valueOf("random_string$");
637653
});
638654
Assert.assertFalse(admin.tableExists(TableName.valueOf("tablegroup_not_exists")));
655+
Assert.assertTrue(admin.tableExists(TableName.valueOf("n1", "test_multi_cf")));
639656
Assert.assertTrue(admin.tableExists(TableName.valueOf("test_multi_cf")));
640-
Assert.assertTrue(admin.tableExists(TableName.valueOf("n1", "test")));
641657
}
642658

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

0 commit comments

Comments
 (0)