Skip to content

Commit d398946

Browse files
committed
add enable disable to OHTableAccessControlExecutor
1 parent 4b563a8 commit d398946

File tree

2 files changed

+185
-0
lines changed

2 files changed

+185
-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: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,30 @@
1818
package com.alipay.oceanbase.hbase;
1919

2020
import com.alipay.oceanbase.hbase.util.ObHTableTestUtil;
21+
import com.alipay.oceanbase.rpc.exception.ObTableException;
22+
import com.alipay.oceanbase.rpc.protocol.payload.ResultCodes;
2123
import org.apache.hadoop.conf.Configuration;
24+
import org.apache.hadoop.hbase.RegionMetrics;
25+
import org.apache.hadoop.hbase.ServerName;
26+
import org.apache.hadoop.hbase.TableName;
27+
import org.apache.hadoop.hbase.client.*;
28+
import org.apache.hadoop.hbase.util.Bytes;
2229
import org.apache.hadoop.hbase.util.Pair;
30+
2331
import org.junit.Assert;
32+
import org.junit.Assert.*;
2433
import org.junit.Test;
2534

35+
2636
import java.io.IOException;
37+
import java.sql.Statement;
38+
import java.util.LinkedList;
39+
import java.util.List;
2740
import java.util.concurrent.Executors;
2841

2942
import static com.alipay.oceanbase.hbase.constants.OHConstants.HBASE_HTABLE_TEST_LOAD_ENABLE;
43+
import static org.apache.hadoop.hbase.util.Bytes.toBytes;
44+
import static org.junit.Assert.*;
3045

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

0 commit comments

Comments
 (0)