36
36
import java .util .regex .Pattern ;
37
37
import java .util .stream .Collectors ;
38
38
39
+ import static com .alipay .oceanbase .rpc .protocol .payload .ResultCodes .*;
40
+
39
41
public class OHAdmin implements Admin {
40
42
private boolean aborted = false ;
41
43
private final OHConnectionImpl connection ;
42
44
private final Configuration conf ;
45
+
46
+ @ FunctionalInterface
47
+ private interface ExceptionHandler {
48
+ void handle (int errorCode , TableName tableName ) throws IOException ;
49
+ }
50
+
51
+ private Throwable getRootCause (Throwable e ) {
52
+ Throwable cause = e .getCause ();
53
+ while (cause != null && cause .getCause () != null ) {
54
+ cause = cause .getCause ();
55
+ }
56
+ return cause ;
57
+ }
58
+
59
+ private void handleTimeoutException (Exception e ) throws TimeoutIOException {
60
+ if (e .getCause () instanceof ObTableTransportException
61
+ && ((ObTableTransportException ) e .getCause ()).getErrorCode () == TransportCodes .BOLT_TIMEOUT ) {
62
+ throw new TimeoutIOException (e .getCause ());
63
+ }
64
+ }
65
+
66
+ private void handleObTableException (Exception e , TableName tableName , ExceptionHandler exceptionHandler ) throws IOException {
67
+ if (e instanceof IOException ) {
68
+ handleTimeoutException (e );
69
+ }
70
+
71
+ Throwable cause = getRootCause (e );
72
+
73
+ if (cause instanceof ObTableException ) {
74
+ int errCode = ((ObTableException ) cause ).getErrorCode ();
75
+ try {
76
+ exceptionHandler .handle (errCode , tableName );
77
+ } catch (RuntimeException re ) {
78
+ throw re ;
79
+ }
80
+ }
81
+
82
+ if (e instanceof IOException ) {
83
+ throw (IOException ) e ;
84
+ } else {
85
+ throw new IOException (e );
86
+ }
87
+ }
43
88
OHAdmin (OHConnectionImpl connection ) {
44
89
this .connection = connection ;
45
90
this .conf = connection .getConfiguration ();
@@ -76,10 +121,7 @@ public boolean tableExists(TableName tableName) throws IOException {
76
121
return executor .tableExists (tableName .getNameAsString ());
77
122
} catch (Exception e ) {
78
123
// try to get the original cause
79
- Throwable cause = e .getCause ();
80
- while (cause != null && cause .getCause () != null ) {
81
- cause = cause .getCause ();
82
- }
124
+ Throwable cause = getRootCause (e );
83
125
if (cause instanceof ObTableException ) {
84
126
int errCode = ((ObTableException ) cause ).getErrorCode ();
85
127
// if the original cause is database_not_exist, means namespace in tableName does not exist
@@ -88,7 +130,11 @@ public boolean tableExists(TableName tableName) throws IOException {
88
130
return false ;
89
131
}
90
132
}
91
- throw e ;
133
+ if (e instanceof IOException ) {
134
+ throw (IOException ) e ;
135
+ } else {
136
+ throw new IOException (e );
137
+ }
92
138
}
93
139
}
94
140
@@ -150,15 +196,15 @@ public HTableDescriptor getTableDescriptor(TableName tableName) throws TableNotF
150
196
try {
151
197
return executor .getTableDescriptor ();
152
198
} catch (IOException e ) {
153
- if ( e . getCause () instanceof ObTableTransportException
154
- && (( ObTableTransportException ) e . getCause ()). getErrorCode () == TransportCodes . BOLT_TIMEOUT ) {
155
- throw new TimeoutIOException ( e . getCause () );
156
- } else if (e . getCause (). getMessage (). contains ( "OB_TABLEGROUP_NOT_EXIST" ) ) {
157
- throw new TableNotFoundException ( tableName );
158
- } else {
159
- throw e ;
160
- }
161
- }
199
+ handleObTableException ( e , tableName , ( errCode , argTableName ) -> {
200
+ if ( errCode == OB_KV_HBASE_TABLE_NOT_EXISTS . errorCode ) {
201
+ throw new TableNotFoundException ( argTableName );
202
+ } else if (errCode == OB_KV_HBASE_NAMESPACE_NOT_FOUND . errorCode ) {
203
+ throw new NamespaceNotFoundException ( argTableName . getNamespaceAsString () );
204
+ }
205
+ }) ;
206
+ throw e ; // should never reach
207
+ }
162
208
}
163
209
164
210
@ Override
@@ -169,14 +215,13 @@ public void createTable(HTableDescriptor tableDescriptor) throws IOException {
169
215
try {
170
216
executor .createTable (tableDescriptor , null );
171
217
} catch (IOException e ) {
172
- if (e .getCause () instanceof ObTableTransportException
173
- && ((ObTableTransportException ) e .getCause ()).getErrorCode () == TransportCodes .BOLT_TIMEOUT ) {
174
- throw new TimeoutIOException (e .getCause ());
175
- } else if (e .getCause ().getMessage ().contains ("already exist" )) {
176
- throw new TableExistsException (e .getCause ().getMessage ());
177
- } else {
178
- throw e ;
179
- }
218
+ handleObTableException (e , tableDescriptor .getTableName (), (errCode , tableName ) -> {
219
+ if (errCode == OB_KV_HBASE_TABLE_EXISTS .errorCode ) {
220
+ throw new TableExistsException (tableName .getNameAsString ());
221
+ } else if (errCode == OB_KV_HBASE_NAMESPACE_NOT_FOUND .errorCode ) {
222
+ throw new NamespaceNotFoundException (tableName .getNameAsString ());
223
+ }
224
+ });
180
225
}
181
226
}
182
227
@@ -203,12 +248,15 @@ public void deleteTable(TableName tableName) throws IOException {
203
248
try {
204
249
executor .deleteTable (tableName .getNameAsString ());
205
250
} catch (IOException e ) {
206
- if (e .getCause () instanceof ObTableTransportException
207
- && ((ObTableTransportException ) e .getCause ()).getErrorCode () == TransportCodes .BOLT_TIMEOUT ) {
208
- throw new TimeoutIOException (e .getCause ());
209
- } else if (e .getCause ().getMessage ().contains ("not found" )) {
210
- throw new TableNotFoundException (tableName );
211
- }
251
+ handleObTableException (e , tableName , (errCode , argTableName ) -> {
252
+ if (errCode == OB_KV_HBASE_TABLE_NOT_EXISTS .errorCode ) {
253
+ throw new TableNotFoundException (argTableName );
254
+ } else if (errCode == OB_KV_HBASE_NAMESPACE_NOT_FOUND .errorCode ) {
255
+ throw new NamespaceNotFoundException (argTableName .getNamespaceAsString ());
256
+ } else if (errCode == OB_KV_TABLE_NOT_DISABLED .errorCode ) {
257
+ throw new TableNotDisabledException (argTableName );
258
+ }
259
+ });
212
260
}
213
261
}
214
262
@@ -235,16 +283,13 @@ public void enableTable(TableName tableName) throws IOException {
235
283
try {
236
284
executor .enableTable (tableName .getNameAsString ());
237
285
} catch (IOException e ) {
238
- if (e .getCause () instanceof ObTableTransportException
239
- && ((ObTableTransportException ) e .getCause ()).getErrorCode () == TransportCodes .BOLT_TIMEOUT ) {
240
- throw new TimeoutIOException (e .getCause ());
241
- } else if (e .getCause ().getMessage ().contains ("not exist" )) {
242
- throw new TableNotFoundException (tableName );
243
- } else if (e .getCause ().getMessage ().contains ("not enabled" )) {
244
- throw new TableNotDisabledException (e .getCause ().getMessage ());
245
- } else {
246
- throw e ;
247
- }
286
+ handleObTableException (e , tableName , (errCode , argTableName ) -> {
287
+ if (errCode == OB_KV_HBASE_TABLE_NOT_EXISTS .errorCode ) {
288
+ throw new TableNotFoundException (argTableName );
289
+ } else if (errCode == OB_KV_HBASE_NAMESPACE_NOT_FOUND .errorCode ) {
290
+ throw new NamespaceNotFoundException (argTableName .getNamespaceAsString ());
291
+ }
292
+ });
248
293
}
249
294
}
250
295
@@ -276,16 +321,13 @@ public void disableTable(TableName tableName) throws IOException {
276
321
try {
277
322
executor .disableTable (tableName .getNameAsString ());
278
323
} catch (IOException e ) {
279
- if (e .getCause () instanceof ObTableTransportException
280
- && ((ObTableTransportException ) e .getCause ()).getErrorCode () == TransportCodes .BOLT_TIMEOUT ) {
281
- throw new TimeoutIOException (e .getCause ());
282
- } else if (e .getCause ().getMessage ().contains ("not exist" )) {
283
- throw new TableNotFoundException (tableName );
284
- } else if (e .getCause ().getMessage ().contains ("not disabled" )) {
285
- throw new TableNotEnabledException (e .getCause ().getMessage ());
286
- } else {
287
- throw e ;
288
- }
324
+ handleObTableException (e , tableName , (errCode , argTableName ) -> {
325
+ if (errCode == OB_KV_HBASE_TABLE_NOT_EXISTS .errorCode ) {
326
+ throw new TableNotFoundException (argTableName );
327
+ } else if (errCode == OB_KV_HBASE_NAMESPACE_NOT_FOUND .errorCode ) {
328
+ throw new NamespaceNotFoundException (argTableName .getNamespaceAsString ());
329
+ }
330
+ });
289
331
}
290
332
}
291
333
0 commit comments