Skip to content

Commit 4d8df51

Browse files
GroundWuZhou-jw
authored andcommitted
adapt for disable/enable table test (#228)
* adapt for disable/enable table
1 parent 7aa7dbd commit 4d8df51

File tree

3 files changed

+86
-7
lines changed

3 files changed

+86
-7
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
}

0 commit comments

Comments
 (0)