Skip to content

Commit 61b48c2

Browse files
authored
adapt for disable/enable table test (#228)
* adapt for disable/enable table
1 parent 4164ef2 commit 61b48c2

File tree

4 files changed

+165
-42
lines changed

4 files changed

+165
-42
lines changed

src/main/java/com/alipay/oceanbase/hbase/util/OHAdmin.java

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import com.alipay.oceanbase.rpc.bolt.transport.TransportCodes;
55
import com.alipay.oceanbase.hbase.exception.FeatureNotSupportedException;
66
import com.alipay.oceanbase.rpc.exception.ObTableTransportException;
7+
import com.alipay.oceanbase.rpc.meta.ObTableRpcMetaType;
78
import org.apache.hadoop.conf.Configuration;
89
import org.apache.hadoop.hbase.*;
910
import org.apache.hadoop.hbase.client.*;
@@ -243,7 +244,19 @@ public Future<Void> truncateTableAsync(TableName tableName, boolean b) throws IO
243244

244245
@Override
245246
public void enableTable(TableName tableName) throws IOException {
246-
throw new FeatureNotSupportedException("does not support yet");
247+
OHConnectionConfiguration connectionConf = new OHConnectionConfiguration(conf);
248+
ObTableClient tableClient = ObTableClientManager.getOrCreateObTableClientByTableName(tableName, connectionConf);
249+
OHTableAccessControlExecutor executor = new OHTableAccessControlExecutor(tableClient, ObTableRpcMetaType.HTABLE_ENABLE_TABLE);
250+
try {
251+
executor.enableTable(tableName.getNameAsString());
252+
} catch (IOException e) {
253+
if (e.getCause() instanceof ObTableTransportException
254+
&& ((ObTableTransportException) e.getCause()).getErrorCode() == TransportCodes.BOLT_TIMEOUT) {
255+
throw new TimeoutIOException(e.getCause());
256+
} else {
257+
throw e;
258+
}
259+
}
247260
}
248261

249262
@Override
@@ -268,7 +281,19 @@ public Future<Void> disableTableAsync(TableName tableName) throws IOException {
268281

269282
@Override
270283
public void disableTable(TableName tableName) throws IOException {
271-
throw new FeatureNotSupportedException("does not support yet");
284+
OHConnectionConfiguration connectionConf = new OHConnectionConfiguration(conf);
285+
ObTableClient tableClient = ObTableClientManager.getOrCreateObTableClientByTableName(tableName, connectionConf);
286+
OHTableAccessControlExecutor executor = new OHTableAccessControlExecutor(tableClient, ObTableRpcMetaType.HTABLE_DISABLE_TABLE);
287+
try {
288+
executor.disableTable(tableName.getNameAsString());
289+
} catch (IOException e) {
290+
if (e.getCause() instanceof ObTableTransportException
291+
&& ((ObTableTransportException) e.getCause()).getErrorCode() == TransportCodes.BOLT_TIMEOUT) {
292+
throw new TimeoutIOException(e.getCause());
293+
} else {
294+
throw e;
295+
}
296+
}
272297
}
273298

274299
@Override
@@ -283,12 +308,19 @@ public HTableDescriptor[] disableTables(Pattern pattern) throws IOException {
283308

284309
@Override
285310
public boolean isTableEnabled(TableName tableName) throws IOException {
286-
throw new FeatureNotSupportedException("does not support yet");
311+
return isDisabled(tableName) == false;
287312
}
288313

289314
@Override
290315
public boolean isTableDisabled(TableName tableName) throws IOException {
291-
throw new FeatureNotSupportedException("does not support yet");
316+
return isDisabled(tableName) == true;
317+
}
318+
319+
private boolean isDisabled(TableName tableName) throws IOException {
320+
OHConnectionConfiguration connectionConf = new OHConnectionConfiguration(conf);
321+
ObTableClient tableClient = ObTableClientManager.getOrCreateObTableClientByTableName(tableName, connectionConf);
322+
OHTableDescriptorExecutor tableDescriptor = new OHTableDescriptorExecutor(tableName.getNameAsString(), tableClient);
323+
return tableDescriptor.isDisable();
292324
}
293325

294326
@Override

src/main/java/com/alipay/oceanbase/hbase/util/OHTableAccessControlExecutor.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package com.alipay.oceanbase.hbase.util;
22

33
import com.alibaba.fastjson.JSON;
4-
import com.alibaba.fastjson.JSONObject;
54
import com.alipay.oceanbase.hbase.execute.AbstractObTableMetaExecutor;
65
import com.alipay.oceanbase.rpc.ObTableClient;
76
import com.alipay.oceanbase.rpc.meta.ObTableMetaRequest;
@@ -38,7 +37,7 @@ public void enableTable(String tableName) throws IOException, TableNotFoundExcep
3837
ObTableMetaRequest request = new ObTableMetaRequest();
3938
request.setMetaType(getMetaType());
4039
Map<String, Object> requestData = new HashMap<>();
41-
requestData.put("name", tableName);
40+
requestData.put("table_name", tableName);
4241
String jsonData = JSON.toJSONString(requestData);
4342
request.setData(jsonData);
4443
execute(tableClient, request);
@@ -48,7 +47,7 @@ public void disableTable(String tableName) throws IOException, TableNotFoundExce
4847
ObTableMetaRequest request = new ObTableMetaRequest();
4948
request.setMetaType(getMetaType());
5049
Map<String, Object> requestData = new HashMap<>();
51-
requestData.put("name", tableName);
50+
requestData.put("table_name", tableName);
5251
String jsonData = JSON.toJSONString(requestData);
5352
request.setData(jsonData);
5453
execute(tableClient, request);

src/main/java/com/alipay/oceanbase/hbase/util/OHTableDescriptorExecutor.java

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import com.alipay.oceanbase.rpc.meta.ObTableMetaRequest;
88
import com.alipay.oceanbase.rpc.meta.ObTableMetaResponse;
99
import com.alipay.oceanbase.rpc.meta.ObTableRpcMetaType;
10+
import com.alipay.oceanbase.rpc.table.ObTable;
1011
import org.apache.hadoop.hbase.HColumnDescriptor;
1112
import org.apache.hadoop.hbase.HTableDescriptor;
1213
import org.apache.hadoop.hbase.TableName;
@@ -82,4 +83,51 @@ public HTableDescriptor getTableDescriptor() throws IOException {
8283

8384
return execute(client, request);
8485
}
86+
87+
public boolean isDisable() throws IOException {
88+
boolean isDisable = false;
89+
final ObTableMetaRequest request = new ObTableMetaRequest();
90+
request.setMetaType(getMetaType());
91+
final Map<String, String> requestData = new HashMap<>();
92+
requestData.put("table_name", tableName);
93+
94+
final String jsonData = JSON.toJSONString(requestData);
95+
request.setData(jsonData);
96+
try {
97+
ObTableMetaResponse response = innerExecute(client, request);
98+
final String responseData = response.getData();
99+
final JSONObject jsonMap = Optional.<JSONObject>ofNullable(JSON.parseObject(responseData))
100+
.orElseThrow(() -> new IOException("jsonMap is null"));
101+
JSONObject tbDesc = jsonMap.getJSONObject("tableDesc");
102+
if (tbDesc != null) {
103+
String state = tbDesc.getString("state");
104+
if (state.compareToIgnoreCase("disable") == 0) {
105+
isDisable = true;
106+
} else {
107+
isDisable = false;
108+
}
109+
}
110+
} catch (IOException e) {
111+
throw e;
112+
}
113+
return isDisable;
114+
}
115+
116+
private ObTableMetaResponse innerExecute(ObTableClient client, ObTableMetaRequest request) throws IOException {
117+
if (request.getMetaType() != getMetaType()) {
118+
throw new IOException("Invalid meta type, expected " + getMetaType());
119+
}
120+
ObTable table = client.getRandomTable();
121+
ObTableMetaResponse response;
122+
try {
123+
response = (ObTableMetaResponse) client.executeWithRetry(
124+
table,
125+
request,
126+
null /*tableName*/
127+
);
128+
} catch (Exception e) {
129+
throw new IOException("Failed to execute request", e);
130+
}
131+
return response;
132+
}
85133
}

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

Lines changed: 79 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -319,53 +319,97 @@ public void testAdminEnDisableTable() throws Exception {
319319
Configuration conf = ObHTableTestUtil.newConfiguration();
320320
Connection connection = ConnectionFactory.createConnection(conf);
321321
Admin admin = connection.getAdmin();
322-
admin.disableTable(TableName.valueOf("test_multi_cf"));
323322
assertTrue(admin.tableExists(TableName.valueOf("n1", "test")));
324323
assertTrue(admin.tableExists(TableName.valueOf("test_multi_cf")));
325-
// disable a non-existed table
326-
IOException thrown = assertThrows(IOException.class,
327-
() -> {
328-
admin.disableTable(TableName.valueOf("tablegroup_not_exists"));
329-
});
330-
assertTrue(thrown.getCause() instanceof ObTableException);
331-
Assert.assertEquals(ResultCodes.OB_TABLEGROUP_NOT_EXIST.errorCode, ((ObTableException) thrown.getCause()).getErrorCode());
324+
// 1. disable a non-existed table
325+
{
326+
IOException thrown = assertThrows(IOException.class,
327+
() -> {
328+
admin.disableTable(TableName.valueOf("tablegroup_not_exists"));
329+
});
330+
assertTrue(thrown.getCause() instanceof ObTableException);
331+
Assert.assertEquals(ResultCodes.OB_TABLEGROUP_NOT_EXIST.errorCode, ((ObTableException) thrown.getCause()).getErrorCode());
332+
}
333+
// 2. write an enabled table, should succeed
334+
{
335+
if (admin.isTableDisabled(TableName.valueOf("test_multi_cf"))) {
336+
admin.enableTable(TableName.valueOf("test_multi_cf"));
337+
}
338+
batchInsert(10, "test_multi_cf");
339+
batchGet(10, "test_multi_cf");
340+
}
332341

333-
// write an enabled table, should succeed
334-
batchInsert(10, "test_multi_ch");
335-
// disable a disabled table
336-
thrown = assertThrows(IOException.class,
337-
() -> {
338-
admin.disableTable(TableName.valueOf("test_multi_cf"));
339-
});
340-
assertTrue(thrown.getCause() instanceof ObTableException);
341-
Assert.assertEquals(ResultCodes.OB_KV_TABLE_NOT_DISABLED.errorCode, ((ObTableException) thrown.getCause()).getErrorCode());
342+
// 3. disable a enable table
343+
{
344+
if (admin.isTableEnabled(TableName.valueOf("test_multi_cf"))) {
345+
admin.disableTable(TableName.valueOf("test_multi_cf"));
346+
}
347+
// write and read disable table, should fail
348+
try {
349+
batchInsert(10, "test_multi_cf");
350+
Assert.fail();
351+
} catch (IOException ex) {
352+
Assert.assertTrue(ex.getCause() instanceof ObTableException);
353+
System.out.println(ex.getCause().getMessage());
354+
}
355+
try {
356+
batchGet(10, "test_multi_cf");
357+
Assert.fail();
358+
} catch (IOException ex) {
359+
Assert.assertTrue(ex.getCause() instanceof ObTableException);
360+
Assert.assertEquals(ResultCodes.OB_KV_TABLE_NOT_ENABLED.errorCode,
361+
((ObTableException) ex.getCause()).getErrorCode());
362+
}
342363

343-
// write an enabled table, should fail
344-
batchInsert(10, "test_multi_ch");
345-
enDisableRead(10, "test_multi_ch");
364+
}
346365

347-
// enable a disabled table
348-
admin.enableTable(TableName.valueOf("test_multi_cf"));
366+
// 4. enable a disabled table
367+
{
368+
if (admin.isTableDisabled(TableName.valueOf("test_multi_cf"))) {
369+
admin.enableTable(TableName.valueOf("test_multi_cf"));
370+
}
371+
// write an enabled table, should succeed
372+
batchInsert(10, "test_multi_cf");
373+
batchGet(10, "test_multi_cf");
374+
}
349375

350-
// write an enabled table, should succeed
351-
batchInsert(10, "test_multi_ch");
352-
enDisableRead(10, "test_multi_ch");
376+
// 5. enable an enabled table
377+
{
378+
if (admin.isTableDisabled(TableName.valueOf("n1", "test"))) {
379+
admin.enableTable(TableName.valueOf("n1", "test"));
380+
}
381+
try {
382+
admin.enableTable(TableName.valueOf("n1", "test"));
383+
Assert.fail();
384+
} catch (IOException ex) {
385+
Assert.assertTrue(ex.getCause() instanceof ObTableException);
386+
Assert.assertEquals(ResultCodes.OB_KV_TABLE_NOT_DISABLED.errorCode,
387+
((ObTableException) ex.getCause()).getErrorCode());
388+
}
389+
}
353390

354-
// enable an enabled table
355-
thrown = assertThrows(IOException.class,
356-
() -> {
357-
admin.disableTable(TableName.valueOf("n1", "test"));
358-
});
359-
assertTrue(thrown.getCause() instanceof ObTableException);
360-
Assert.assertEquals(ResultCodes.OB_KV_TABLE_NOT_ENABLED.errorCode, ((ObTableException) thrown.getCause()).getErrorCode());
391+
// 6. disable a disabled table
392+
{
393+
if (admin.isTableEnabled(TableName.valueOf("n1", "test"))) {
394+
admin.disableTable(TableName.valueOf("n1", "test"));
395+
}
396+
try {
397+
admin.disableTable(TableName.valueOf("n1", "test"));
398+
Assert.fail();
399+
} catch (IOException ex) {
400+
Assert.assertTrue(ex.getCause() instanceof ObTableException);
401+
Assert.assertEquals(ResultCodes.OB_KV_TABLE_NOT_ENABLED.errorCode,
402+
((ObTableException) ex.getCause()).getErrorCode());
403+
}
404+
}
361405

362-
admin.deleteTable(TableName.valueOf("n1", "test"));
363406
admin.deleteTable(TableName.valueOf("test_multi_cf"));
364-
assertFalse(admin.tableExists(TableName.valueOf("n1", "test")));
365407
assertFalse(admin.tableExists(TableName.valueOf("test_multi_cf")));
408+
admin.deleteTable(TableName.valueOf("n1", "test"));
409+
assertFalse(admin.tableExists(TableName.valueOf("n1", "test")));
366410
}
367411

368-
private void enDisableRead(int rows, String tablegroup) throws Exception {
412+
private void batchGet(int rows, String tablegroup) throws Exception {
369413
Configuration conf = ObHTableTestUtil.newConfiguration();
370414
Connection connection = ConnectionFactory.createConnection(conf);
371415
Table table = connection.getTable(TableName.valueOf(tablegroup));

0 commit comments

Comments
 (0)