Skip to content

Commit 72a0772

Browse files
authored
Merge pull request #264 from oceanbase/add_test_case
add test case for disable-before-delete-table; fix cases
2 parents b444889 + 32534b6 commit 72a0772

File tree

2 files changed

+175
-50
lines changed

2 files changed

+175
-50
lines changed

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

Lines changed: 110 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,56 @@
3232
import java.util.concurrent.Future;
3333
import java.util.regex.Pattern;
3434

35+
import static com.alipay.oceanbase.rpc.protocol.payload.ResultCodes.*;
36+
3537
public class OHAdmin implements Admin {
3638
private boolean aborted = false;
3739
private final OHConnectionImpl connection;
3840
private final Configuration conf;
41+
42+
@FunctionalInterface
43+
private interface ExceptionHandler {
44+
void handle(int errorCode, TableName tableName) throws IOException;
45+
}
46+
47+
private Throwable getRootCause(Throwable e) {
48+
Throwable cause = e.getCause();
49+
while(cause != null && cause.getCause() != null) {
50+
cause = cause.getCause();
51+
}
52+
return cause;
53+
}
54+
55+
private void handleTimeoutException(Exception e) throws TimeoutIOException {
56+
if (e.getCause() instanceof ObTableTransportException
57+
&& ((ObTableTransportException) e.getCause()).getErrorCode() == TransportCodes.BOLT_TIMEOUT) {
58+
throw new TimeoutIOException(e.getCause());
59+
}
60+
}
61+
62+
private void handleObTableException(Exception e, TableName tableName, ExceptionHandler exceptionHandler) throws IOException {
63+
if (e instanceof IOException) {
64+
handleTimeoutException(e);
65+
}
66+
67+
Throwable cause = getRootCause(e);
68+
69+
if (cause instanceof ObTableException) {
70+
int errCode = ((ObTableException) cause).getErrorCode();
71+
try {
72+
exceptionHandler.handle(errCode, tableName);
73+
} catch (RuntimeException re) {
74+
throw re;
75+
}
76+
}
77+
78+
if (e instanceof IOException) {
79+
throw (IOException) e;
80+
} else {
81+
throw new IOException(e);
82+
}
83+
}
84+
3985
OHAdmin(OHConnectionImpl connection) {
4086
this.connection = connection;
4187
this.conf = connection.getConfiguration();
@@ -72,10 +118,7 @@ public boolean tableExists(TableName tableName) throws IOException {
72118
return executor.tableExists(tableName.getNameAsString());
73119
} catch (Exception e) {
74120
// try to get the original cause
75-
Throwable cause = e.getCause();
76-
while(cause != null && cause.getCause() != null) {
77-
cause = cause.getCause();
78-
}
121+
Throwable cause = getRootCause(e);
79122
if (cause instanceof ObTableException) {
80123
int errCode = ((ObTableException) cause).getErrorCode();
81124
// if the original cause is database_not_exist, means namespace in tableName does not exist
@@ -84,7 +127,11 @@ public boolean tableExists(TableName tableName) throws IOException {
84127
return false;
85128
}
86129
}
87-
throw e;
130+
if (e instanceof IOException) {
131+
throw (IOException) e;
132+
} else {
133+
throw new IOException(e);
134+
}
88135
}
89136
}
90137

@@ -161,14 +208,14 @@ public HTableDescriptor getTableDescriptor(TableName tableName) throws TableNotF
161208
try {
162209
return executor.getTableDescriptor();
163210
} catch (IOException e) {
164-
if (e.getCause() instanceof ObTableTransportException
165-
&& ((ObTableTransportException) e.getCause()).getErrorCode() == TransportCodes.BOLT_TIMEOUT) {
166-
throw new TimeoutIOException(e.getCause());
167-
} else if (e.getCause().getMessage().contains("OB_TABLEGROUP_NOT_EXIST")) {
168-
throw new TableNotFoundException(tableName);
169-
} else {
170-
throw e;
171-
}
211+
handleObTableException(e, tableName, (errCode, argTableName) -> {
212+
if (errCode == OB_KV_HBASE_TABLE_NOT_EXISTS.errorCode) {
213+
throw new TableNotFoundException(argTableName);
214+
} else if (errCode == OB_KV_HBASE_NAMESPACE_NOT_FOUND.errorCode) {
215+
throw new NamespaceNotFoundException(argTableName.getNamespaceAsString());
216+
}
217+
});
218+
throw e; // should never reach
172219
}
173220
}
174221

@@ -180,14 +227,14 @@ public TableDescriptor getDescriptor(TableName tableName) throws IOException {
180227
try {
181228
return executor.getTableDescriptor();
182229
} catch (IOException e) {
183-
if (e.getCause() instanceof ObTableTransportException
184-
&& ((ObTableTransportException) e.getCause()).getErrorCode() == TransportCodes.BOLT_TIMEOUT) {
185-
throw new TimeoutIOException(e.getCause());
186-
} else if (e.getCause().getMessage().contains("OB_TABLEGROUP_NOT_EXIST")) {
187-
throw new TableNotFoundException(tableName);
188-
} else {
189-
throw e;
190-
}
230+
handleObTableException(e, tableName, (errCode, argTableName) -> {
231+
if (errCode == OB_KV_HBASE_TABLE_NOT_EXISTS.errorCode) {
232+
throw new TableNotFoundException(argTableName);
233+
} else if (errCode == OB_KV_HBASE_NAMESPACE_NOT_FOUND.errorCode) {
234+
throw new NamespaceNotFoundException(argTableName.getNamespaceAsString());
235+
}
236+
});
237+
throw e; // should never reach
191238
}
192239
}
193240

@@ -199,12 +246,13 @@ public void createTable(TableDescriptor tableDescriptor) throws IOException {
199246
try {
200247
executor.createTable(tableDescriptor, null);
201248
} catch (IOException e) {
202-
if (e.getCause() instanceof ObTableTransportException
203-
&& ((ObTableTransportException) e.getCause()).getErrorCode() == TransportCodes.BOLT_TIMEOUT) {
204-
throw new TimeoutIOException(e.getCause());
205-
} else {
206-
throw e;
207-
}
249+
handleObTableException(e, tableDescriptor.getTableName(), (errCode, tableName) -> {
250+
if (errCode == OB_KV_HBASE_TABLE_EXISTS.errorCode) {
251+
throw new TableExistsException(tableName.getNameAsString());
252+
} else if (errCode == OB_KV_HBASE_NAMESPACE_NOT_FOUND.errorCode) {
253+
throw new NamespaceNotFoundException(tableName.getNameAsString());
254+
}
255+
});
208256
}
209257
}
210258

@@ -231,12 +279,15 @@ public void deleteTable(TableName tableName) throws IOException {
231279
try {
232280
executor.deleteTable(tableName.getNameAsString());
233281
} catch (IOException e) {
234-
if (e.getCause() instanceof ObTableTransportException
235-
&& ((ObTableTransportException) e.getCause()).getErrorCode() == TransportCodes.BOLT_TIMEOUT) {
236-
throw new TimeoutIOException(e.getCause());
237-
} else {
238-
throw e;
239-
}
282+
handleObTableException(e, tableName, (errCode, argTableName) -> {
283+
if (errCode == OB_KV_HBASE_TABLE_NOT_EXISTS.errorCode) {
284+
throw new TableNotFoundException(argTableName);
285+
} else if (errCode == OB_KV_HBASE_NAMESPACE_NOT_FOUND.errorCode) {
286+
throw new NamespaceNotFoundException(argTableName.getNamespaceAsString());
287+
} else if (errCode == OB_KV_TABLE_NOT_DISABLED.errorCode) {
288+
throw new TableNotDisabledException(argTableName);
289+
}
290+
});
240291
}
241292
}
242293

@@ -273,12 +324,13 @@ public void enableTable(TableName tableName) throws IOException {
273324
try {
274325
executor.enableTable(tableName.getNameAsString());
275326
} catch (IOException e) {
276-
if (e.getCause() instanceof ObTableTransportException
277-
&& ((ObTableTransportException) e.getCause()).getErrorCode() == TransportCodes.BOLT_TIMEOUT) {
278-
throw new TimeoutIOException(e.getCause());
279-
} else {
280-
throw e;
281-
}
327+
handleObTableException(e, tableName, (errCode, argTableName) -> {
328+
if (errCode == OB_KV_HBASE_TABLE_NOT_EXISTS.errorCode) {
329+
throw new TableNotFoundException(argTableName);
330+
} else if (errCode == OB_KV_HBASE_NAMESPACE_NOT_FOUND.errorCode) {
331+
throw new NamespaceNotFoundException(argTableName.getNamespaceAsString());
332+
}
333+
});
282334
}
283335
}
284336

@@ -310,12 +362,13 @@ public void disableTable(TableName tableName) throws IOException {
310362
try {
311363
executor.disableTable(tableName.getNameAsString());
312364
} catch (IOException e) {
313-
if (e.getCause() instanceof ObTableTransportException
314-
&& ((ObTableTransportException) e.getCause()).getErrorCode() == TransportCodes.BOLT_TIMEOUT) {
315-
throw new TimeoutIOException(e.getCause());
316-
} else {
317-
throw e;
318-
}
365+
handleObTableException(e, tableName, (errCode, argTableName) -> {
366+
if (errCode == OB_KV_HBASE_TABLE_NOT_EXISTS.errorCode) {
367+
throw new TableNotFoundException(argTableName);
368+
} else if (errCode == OB_KV_HBASE_NAMESPACE_NOT_FOUND.errorCode) {
369+
throw new NamespaceNotFoundException(argTableName.getNamespaceAsString());
370+
}
371+
});
319372
}
320373
}
321374

@@ -704,7 +757,18 @@ public List<RegionMetrics> getRegionMetrics(ServerName serverName, TableName tab
704757
OHConnectionConfiguration connectionConf = new OHConnectionConfiguration(conf);
705758
ObTableClient tableClient = ObTableClientManager.getOrCreateObTableClientByTableName(tableName, connectionConf);
706759
OHRegionMetricsExecutor executor = new OHRegionMetricsExecutor(tableClient);
707-
return executor.getRegionMetrics(tableName.getNameAsString());
760+
try {
761+
return executor.getRegionMetrics(tableName.getNameAsString());
762+
} catch (Exception e) {
763+
handleObTableException(e, tableName, (errCode, argTableName) -> {
764+
if (errCode == OB_KV_HBASE_TABLE_NOT_EXISTS.errorCode) {
765+
throw new TableNotFoundException(argTableName.getNameAsString());
766+
} else if (errCode == OB_KV_HBASE_NAMESPACE_NOT_FOUND.errorCode) {
767+
throw new NamespaceNotFoundException(argTableName.getNamespaceAsString());
768+
}
769+
});
770+
throw e; // should never reach
771+
}
708772
}
709773

710774
@Override

0 commit comments

Comments
 (0)