Skip to content

Commit 8f0752a

Browse files
authored
Merge pull request #221 from Zhou-jw/hbase_compat_3_2.0_enable_disable
add enable disable to OHTableAccessControlExecutor
2 parents 5e09cc3 + 22e61f9 commit 8f0752a

File tree

2 files changed

+172
-0
lines changed

2 files changed

+172
-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: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,11 @@
2929
import org.apache.hadoop.hbase.client.*;
3030
import org.apache.hadoop.hbase.util.Bytes;
3131
import org.apache.hadoop.hbase.util.Pair;
32+
3233
import org.junit.Assert;
3334
import org.junit.Test;
3435

36+
3537
import java.io.IOException;
3638
import java.sql.Statement;
3739
import java.util.ArrayList;
@@ -268,6 +270,121 @@ public void testGetStartEndKeysOHTablePoolLoadNon() throws Exception {
268270
Assert.assertEquals(0, startEndKeys.getSecond()[0].length);
269271
}
270272

273+
@Test
274+
public void testAdminEnDisableTable() throws Exception {
275+
java.sql.Connection conn = ObHTableTestUtil.getConnection();
276+
Statement st = conn.createStatement();
277+
st.execute("CREATE TABLEGROUP IF NOT EXISTS test_multi_cf SHARDING = 'ADAPTIVE';\n" +
278+
"\n" +
279+
"CREATE TABLE IF NOT EXISTS `test_multi_cf$family_with_group1` (\n" +
280+
" `K` varbinary(1024) NOT NULL,\n" +
281+
" `Q` varbinary(256) NOT NULL,\n" +
282+
" `T` bigint(20) NOT NULL,\n" +
283+
" `V` varbinary(1024) DEFAULT NULL,\n" +
284+
" PRIMARY KEY (`K`, `Q`, `T`)\n" +
285+
") TABLEGROUP = test_multi_cf PARTITION BY KEY(`K`) PARTITIONS 3;\n" +
286+
"\n" +
287+
"CREATE TABLE IF NOT EXISTS `test_multi_cf$family_with_group2` (\n" +
288+
" `K` varbinary(1024) NOT NULL,\n" +
289+
" `Q` varbinary(256) NOT NULL,\n" +
290+
" `T` bigint(20) NOT NULL,\n" +
291+
" `V` varbinary(1024) DEFAULT NULL,\n" +
292+
" PRIMARY KEY (`K`, `Q`, `T`)\n" +
293+
") TABLEGROUP = test_multi_cf PARTITION BY KEY(`K`) PARTITIONS 3;\n" +
294+
"\n" +
295+
"CREATE TABLE IF NOT EXISTS `test_multi_cf$family_with_group3` (\n" +
296+
" `K` varbinary(1024) NOT NULL,\n" +
297+
" `Q` varbinary(256) NOT NULL,\n" +
298+
" `T` bigint(20) NOT NULL,\n" +
299+
" `V` varbinary(1024) DEFAULT NULL,\n" +
300+
" PRIMARY KEY (`K`, `Q`, `T`)\n" +
301+
") TABLEGROUP = test_multi_cf PARTITION BY KEY(`K`) PARTITIONS 3;\n" +
302+
"\n" +
303+
"CREATE DATABASE IF NOT EXISTS `n1`;\n" +
304+
"use `n1`;\n" +
305+
"CREATE TABLEGROUP IF NOT EXISTS `n1:test` SHARDING = 'ADAPTIVE';\n" +
306+
"CREATE TABLE IF NOT EXISTS `n1:test$family_group` (\n" +
307+
" `K` varbinary(1024) NOT NULL,\n" +
308+
" `Q` varbinary(256) NOT NULL,\n" +
309+
" `T` bigint(20) NOT NULL,\n" +
310+
" `V` varbinary(1024) DEFAULT NULL,\n" +
311+
" PRIMARY KEY (`K`, `Q`, `T`)\n" +
312+
") TABLEGROUP = `n1:test`;" +
313+
"\n" +
314+
"CREATE TABLE IF NOT EXISTS `n1:test$family1` (\n" +
315+
" `K` varbinary(1024) NOT NULL,\n" +
316+
" `Q` varbinary(256) NOT NULL,\n" +
317+
" `T` bigint(20) NOT NULL,\n" +
318+
" `V` varbinary(1024) DEFAULT NULL,\n" +
319+
" PRIMARY KEY (`K`, `Q`, `T`)\n" +
320+
") TABLEGROUP = `n1:test`;");
321+
Configuration conf = ObHTableTestUtil.newConfiguration();
322+
Connection connection = ConnectionFactory.createConnection(conf);
323+
Admin admin = connection.getAdmin();
324+
admin.disableTable(TableName.valueOf("test_multi_cf"));
325+
assertTrue(admin.tableExists(TableName.valueOf("n1", "test")));
326+
assertTrue(admin.tableExists(TableName.valueOf("test_multi_cf")));
327+
// disable a non-existed table
328+
IOException thrown = assertThrows(IOException.class,
329+
() -> {
330+
admin.disableTable(TableName.valueOf("tablegroup_not_exists"));
331+
});
332+
assertTrue(thrown.getCause() instanceof ObTableException);
333+
Assert.assertEquals(ResultCodes.OB_TABLEGROUP_NOT_EXIST.errorCode, ((ObTableException) thrown.getCause()).getErrorCode());
334+
335+
// write an enabled table, should succeed
336+
batchInsert(10, "test_multi_ch");
337+
// disable a disabled table
338+
thrown = assertThrows(IOException.class,
339+
() -> {
340+
admin.disableTable(TableName.valueOf("test_multi_cf"));
341+
});
342+
assertTrue(thrown.getCause() instanceof ObTableException);
343+
Assert.assertEquals(ResultCodes.OB_KV_TABLE_NOT_DISABLED.errorCode, ((ObTableException) thrown.getCause()).getErrorCode());
344+
345+
// write an enabled table, should fail
346+
batchInsert(10, "test_multi_ch");
347+
enDisableRead(10, "test_multi_ch");
348+
349+
// enable a disabled table
350+
admin.enableTable(TableName.valueOf("test_multi_cf"));
351+
352+
// write an enabled table, should succeed
353+
batchInsert(10, "test_multi_ch");
354+
enDisableRead(10, "test_multi_ch");
355+
356+
// enable an enabled table
357+
thrown = assertThrows(IOException.class,
358+
() -> {
359+
admin.disableTable(TableName.valueOf("n1", "test");
360+
});
361+
assertTrue(thrown.getCause() instanceof ObTableException);
362+
Assert.assertEquals(ResultCodes.OB_KV_TABLE_NOT_ENABLED.errorCode, ((ObTableException) thrown.getCause()).getErrorCode());
363+
364+
admin.deleteTable(TableName.valueOf("n1", "test"));
365+
admin.deleteTable(TableName.valueOf("test_multi_cf"));
366+
assertFalse(admin.tableExists(TableName.valueOf("n1", "test")));
367+
assertFalse(admin.tableExists(TableName.valueOf("test_multi_cf")));
368+
}
369+
370+
private void enDisableRead(int rows, String tablegroup) throws Exception {
371+
Configuration conf = ObHTableTestUtil.newConfiguration();
372+
Connection connection = ConnectionFactory.createConnection(conf);
373+
Table table = connection.getTable(TableName.valueOf(tablegroup));
374+
List<Row> batchLsit = new LinkedList<>();
375+
for (int i = 0; i < rows; ++i) {
376+
Get get = new Get(toBytes("Key" + i));
377+
batchLsit.add(get);
378+
if (i % 100 == 0) { // 100 rows one batch to avoid OB_TIMEOUT
379+
Object[] results = new Object[batchLsit.size()];
380+
table.batch(batchLsit, results);
381+
batchLsit.clear();
382+
}
383+
}
384+
Object[] results = new Object[batchLsit.size()];
385+
table.batch(batchLsit, results);
386+
}
387+
271388
@Test
272389
public void testAdminGetRegionMetrics() throws Exception {
273390
java.sql.Connection conn = ObHTableTestUtil.getConnection();

0 commit comments

Comments
 (0)