Skip to content

Commit b893a15

Browse files
committed
add enable disable to OHTableAccessControlExecutor
1 parent 5e09cc3 commit b893a15

File tree

2 files changed

+170
-0
lines changed

2 files changed

+170
-0
lines changed
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package com.alipay.oceanbase.hbase.util;
2+
3+
import com.alibaba.fastjson.JSON;
4+
import com.alibaba.fastjson.JSONObject;
5+
import com.alipay.oceanbase.hbase.execute.AbstractObTableMetaExecutor;
6+
import com.alipay.oceanbase.rpc.ObTableClient;
7+
import com.alipay.oceanbase.rpc.meta.ObTableMetaRequest;
8+
import com.alipay.oceanbase.rpc.meta.ObTableMetaResponse;
9+
import com.alipay.oceanbase.rpc.meta.ObTableRpcMetaType;
10+
import org.apache.hadoop.hbase.TableNotDisabledException;
11+
import org.apache.hadoop.hbase.TableNotEnabledException;
12+
import org.apache.hadoop.hbase.TableNotFoundException;
13+
14+
import java.io.IOException;
15+
import java.util.HashMap;
16+
import java.util.Map;
17+
18+
public class OHTableAccessControlExecutor extends AbstractObTableMetaExecutor<Void> {
19+
private final ObTableClient tableClient;
20+
private final ObTableRpcMetaType type;
21+
22+
OHTableAccessControlExecutor(ObTableClient tableClient, ObTableRpcMetaType type) {
23+
this.tableClient = tableClient;
24+
this.type = type;
25+
}
26+
27+
@Override
28+
public ObTableRpcMetaType getMetaType() throws IOException {
29+
return this.type;
30+
}
31+
32+
@Override
33+
public Boolean parse(ObTableMetaResponse response) throws IOException {
34+
}
35+
36+
public void enableTable(String tableName) throws IOException, TableNotFoundException, TableNotEnabledException {
37+
ObTableMetaRequest request = new ObTableMetaRequest();
38+
request.setMetaType(getMetaType());
39+
Map<String, Object> requestData = new HashMap<>();
40+
requestData.put("name", tableName);
41+
String jsonData = JSON.toJSONString(requestData);
42+
request.setData(jsonData);
43+
execute(tableClient, request);
44+
}
45+
46+
public void disableTable(String tableName) throws IOException, TableNotFoundException, TableNotDisabledException {
47+
ObTableMetaRequest request = new ObTableMetaRequest();
48+
request.setMetaType(getMetaType());
49+
Map<String, Object> requestData = new HashMap<>();
50+
requestData.put("name", tableName);
51+
String jsonData = JSON.toJSONString(requestData);
52+
request.setData(jsonData);
53+
execute(tableClient, request);
54+
}
55+
}

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

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -572,4 +572,119 @@ private void batchInsert(int rows, String tablegroup) throws Exception {
572572
Object[] results = new Object[batchLsit.size()];
573573
table.batch(batchLsit, results);
574574
}
575+
576+
@Test
577+
public void testAdminEnDisableTable() throws Exception {
578+
java.sql.Connection conn = ObHTableTestUtil.getConnection();
579+
Statement st = conn.createStatement();
580+
st.execute("CREATE TABLEGROUP IF NOT EXISTS test_multi_cf SHARDING = 'ADAPTIVE';\n" +
581+
"\n" +
582+
"CREATE TABLE IF NOT EXISTS `test_multi_cf$family_with_group1` (\n" +
583+
" `K` varbinary(1024) NOT NULL,\n" +
584+
" `Q` varbinary(256) NOT NULL,\n" +
585+
" `T` bigint(20) NOT NULL,\n" +
586+
" `V` varbinary(1024) DEFAULT NULL,\n" +
587+
" PRIMARY KEY (`K`, `Q`, `T`)\n" +
588+
") TABLEGROUP = test_multi_cf PARTITION BY KEY(`K`) PARTITIONS 3;\n" +
589+
"\n" +
590+
"CREATE TABLE IF NOT EXISTS `test_multi_cf$family_with_group2` (\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 = test_multi_cf PARTITION BY KEY(`K`) PARTITIONS 3;\n" +
597+
"\n" +
598+
"CREATE TABLE IF NOT EXISTS `test_multi_cf$family_with_group3` (\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 = test_multi_cf PARTITION BY KEY(`K`) PARTITIONS 3;\n" +
605+
"\n" +
606+
"CREATE DATABASE IF NOT EXISTS `n1`;\n" +
607+
"use `n1`;\n" +
608+
"CREATE TABLEGROUP IF NOT EXISTS `n1:test` SHARDING = 'ADAPTIVE';\n" +
609+
"CREATE TABLE IF NOT EXISTS `n1:test$family_group` (\n" +
610+
" `K` varbinary(1024) NOT NULL,\n" +
611+
" `Q` varbinary(256) NOT NULL,\n" +
612+
" `T` bigint(20) NOT NULL,\n" +
613+
" `V` varbinary(1024) DEFAULT NULL,\n" +
614+
" PRIMARY KEY (`K`, `Q`, `T`)\n" +
615+
") TABLEGROUP = `n1:test`;" +
616+
"\n" +
617+
"CREATE TABLE IF NOT EXISTS `n1:test$family1` (\n" +
618+
" `K` varbinary(1024) NOT NULL,\n" +
619+
" `Q` varbinary(256) NOT NULL,\n" +
620+
" `T` bigint(20) NOT NULL,\n" +
621+
" `V` varbinary(1024) DEFAULT NULL,\n" +
622+
" PRIMARY KEY (`K`, `Q`, `T`)\n" +
623+
") TABLEGROUP = `n1:test`;");
624+
Configuration conf = ObHTableTestUtil.newConfiguration();
625+
Connection connection = ConnectionFactory.createConnection(conf);
626+
Admin admin = connection.getAdmin();
627+
admin.disableTable(TableName.valueOf("test_multi_cf"));
628+
assertTrue(admin.tableExists(TableName.valueOf("n1", "test")));
629+
assertTrue(admin.tableExists(TableName.valueOf("test_multi_cf")));
630+
// disable a non-existed table
631+
IOException thrown = assertThrows(IOException.class,
632+
() -> {
633+
admin.disableTable(TableName.valueOf("tablegroup_not_exists"));
634+
});
635+
assertTrue(thrown.getCause() instanceof ObTableException);
636+
Assert.assertEquals(ResultCodes.OB_TABLEGROUP_NOT_EXIST.errorCode, ((ObTableException) thrown.getCause()).getErrorCode());
637+
638+
// write an enabled table, should succeed
639+
batchInsert(10, "test_multi_ch");
640+
// disable a disabled table
641+
thrown = assertThrows(IOException.class,
642+
() -> {
643+
admin.disableTable(TableName.valueOf("test_multi_cf"));
644+
});
645+
assertTrue(thrown.getCause() instanceof ObTableException);
646+
Assert.assertEquals(ResultCodes.OB_KV_TABLE_NOT_DISABLED.errorCode, ((ObTableException) thrown.getCause()).getErrorCode());
647+
648+
// write an enabled table, should fail
649+
batchInsert(10, "test_multi_ch");
650+
enDisableRead(10, "test_multi_ch");
651+
652+
// enable a disabled table
653+
admin.enableTable(TableName.valueOf("test_multi_cf"));
654+
655+
// write an enabled table, should succeed
656+
batchInsert(10, "test_multi_ch");
657+
enDisableRead(10, "test_multi_ch");
658+
659+
// enable an enabled table
660+
thrown = assertThrows(IOException.class,
661+
() -> {
662+
admin.disableTable(TableName.valueOf("n1", "test");
663+
});
664+
assertTrue(thrown.getCause() instanceof ObTableException);
665+
Assert.assertEquals(ResultCodes.OB_KV_TABLE_NOT_ENABLED.errorCode, ((ObTableException) thrown.getCause()).getErrorCode());
666+
667+
admin.deleteTable(TableName.valueOf("n1", "test"));
668+
admin.deleteTable(TableName.valueOf("test_multi_cf"));
669+
assertFalse(admin.tableExists(TableName.valueOf("n1", "test")));
670+
assertFalse(admin.tableExists(TableName.valueOf("test_multi_cf")));
671+
}
672+
673+
private void enDisableRead(int rows, String tablegroup) throws Exception {
674+
Configuration conf = ObHTableTestUtil.newConfiguration();
675+
Connection connection = ConnectionFactory.createConnection(conf);
676+
Table table = connection.getTable(TableName.valueOf(tablegroup));
677+
List<Row> batchLsit = new LinkedList<>();
678+
for (int i = 0; i < rows; ++i) {
679+
Get get = new Get(toBytes("Key" + i));
680+
batchLsit.add(get);
681+
if (i % 100 == 0) { // 100 rows one batch to avoid OB_TIMEOUT
682+
Object[] results = new Object[batchLsit.size()];
683+
table.batch(batchLsit, results);
684+
batchLsit.clear();
685+
}
686+
}
687+
Object[] results = new Object[batchLsit.size()];
688+
table.batch(batchLsit, results);
689+
}
575690
}

0 commit comments

Comments
 (0)