49
49
import java .util .concurrent .Future ;
50
50
import java .util .regex .Pattern ;
51
51
52
+ import static com .alipay .oceanbase .rpc .protocol .payload .ResultCodes .*;
53
+
52
54
public class OHAdmin implements Admin {
53
55
private boolean aborted = false ;
54
56
private final OHConnectionImpl connection ;
55
57
private final Configuration conf ;
56
58
59
+ @ FunctionalInterface
60
+ private interface ExceptionHandler {
61
+ void handle (int errorCode , TableName tableName ) throws IOException ;
62
+ }
63
+
64
+ private Throwable getRootCause (Throwable e ) {
65
+ Throwable cause = e .getCause ();
66
+ while (cause != null && cause .getCause () != null ) {
67
+ cause = cause .getCause ();
68
+ }
69
+ return cause ;
70
+ }
71
+
72
+ private void handleTimeoutException (Exception e ) throws TimeoutIOException {
73
+ if (e .getCause () instanceof ObTableTransportException
74
+ && ((ObTableTransportException ) e .getCause ()).getErrorCode () == TransportCodes .BOLT_TIMEOUT ) {
75
+ throw new TimeoutIOException (e .getCause ());
76
+ }
77
+ }
78
+
79
+ private void handleObTableException (Exception e , TableName tableName , ExceptionHandler exceptionHandler ) throws IOException {
80
+ if (e instanceof IOException ) {
81
+ handleTimeoutException (e );
82
+ }
83
+
84
+ Throwable cause = getRootCause (e );
85
+
86
+ if (cause instanceof ObTableException ) {
87
+ int errCode = ((ObTableException ) cause ).getErrorCode ();
88
+ try {
89
+ exceptionHandler .handle (errCode , tableName );
90
+ } catch (RuntimeException re ) {
91
+ throw re ;
92
+ }
93
+ }
94
+
95
+ if (e instanceof IOException ) {
96
+ throw (IOException ) e ;
97
+ } else {
98
+ throw new IOException (e );
99
+ }
100
+ }
101
+
57
102
OHAdmin (OHConnectionImpl connection ) {
58
103
this .connection = connection ;
59
104
this .conf = connection .getConfiguration ();
@@ -91,10 +136,7 @@ public boolean tableExists(TableName tableName) throws IOException {
91
136
return executor .tableExists (tableName .getNameAsString ());
92
137
} catch (Exception e ) {
93
138
// try to get the original cause
94
- Throwable cause = e .getCause ();
95
- while (cause != null && cause .getCause () != null ) {
96
- cause = cause .getCause ();
97
- }
139
+ Throwable cause = getRootCause (e );
98
140
if (cause instanceof ObTableException ) {
99
141
int errCode = ((ObTableException ) cause ).getErrorCode ();
100
142
// if the original cause is database_not_exist, means namespace in tableName does not exist
@@ -103,7 +145,11 @@ public boolean tableExists(TableName tableName) throws IOException {
103
145
return false ;
104
146
}
105
147
}
106
- throw e ;
148
+ if (e instanceof IOException ) {
149
+ throw (IOException ) e ;
150
+ } else {
151
+ throw new IOException (e );
152
+ }
107
153
}
108
154
}
109
155
@@ -184,14 +230,14 @@ public HTableDescriptor getTableDescriptor(TableName tableName) throws TableNotF
184
230
try {
185
231
return executor .getTableDescriptor ();
186
232
} catch (IOException e ) {
187
- if ( e . getCause () instanceof ObTableTransportException
188
- && (( ObTableTransportException ) e . getCause ()). getErrorCode () == TransportCodes . BOLT_TIMEOUT ) {
189
- throw new TimeoutIOException ( e . getCause () );
190
- } else if (e . getCause (). getMessage (). contains ( "OB_TABLEGROUP_NOT_EXIST" ) ) {
191
- throw new TableNotFoundException ( tableName );
192
- } else {
193
- throw e ;
194
- }
233
+ handleObTableException ( e , tableName , ( errCode , argTableName ) -> {
234
+ if ( errCode == OB_KV_HBASE_TABLE_NOT_EXISTS . errorCode ) {
235
+ throw new TableNotFoundException ( argTableName );
236
+ } else if (errCode == OB_KV_HBASE_NAMESPACE_NOT_FOUND . errorCode ) {
237
+ throw new NamespaceNotFoundException ( argTableName . getNamespaceAsString () );
238
+ }
239
+ }) ;
240
+ throw e ; // should never reach
195
241
}
196
242
}
197
243
@@ -205,14 +251,14 @@ public TableDescriptor getDescriptor(TableName tableName) throws IOException {
205
251
try {
206
252
return executor .getTableDescriptor ();
207
253
} catch (IOException e ) {
208
- if ( e . getCause () instanceof ObTableTransportException
209
- && (( ObTableTransportException ) e . getCause ()). getErrorCode () == TransportCodes . BOLT_TIMEOUT ) {
210
- throw new TimeoutIOException ( e . getCause () );
211
- } else if (e . getCause (). getMessage (). contains ( "OB_TABLEGROUP_NOT_EXIST" ) ) {
212
- throw new TableNotFoundException ( tableName );
213
- } else {
214
- throw e ;
215
- }
254
+ handleObTableException ( e , tableName , ( errCode , argTableName ) -> {
255
+ if ( errCode == OB_KV_HBASE_TABLE_NOT_EXISTS . errorCode ) {
256
+ throw new TableNotFoundException ( argTableName );
257
+ } else if (errCode == OB_KV_HBASE_NAMESPACE_NOT_FOUND . errorCode ) {
258
+ throw new NamespaceNotFoundException ( argTableName . getNamespaceAsString () );
259
+ }
260
+ }) ;
261
+ throw e ; // should never reach
216
262
}
217
263
}
218
264
@@ -225,12 +271,13 @@ public void createTable(TableDescriptor tableDescriptor) throws IOException {
225
271
try {
226
272
executor .createTable (tableDescriptor , null );
227
273
} catch (IOException e ) {
228
- if (e .getCause () instanceof ObTableTransportException
229
- && ((ObTableTransportException ) e .getCause ()).getErrorCode () == TransportCodes .BOLT_TIMEOUT ) {
230
- throw new TimeoutIOException (e .getCause ());
231
- } else {
232
- throw e ;
233
- }
274
+ handleObTableException (e , tableDescriptor .getTableName (), (errCode , tableName ) -> {
275
+ if (errCode == OB_KV_HBASE_TABLE_EXISTS .errorCode ) {
276
+ throw new TableExistsException (tableName .getNameAsString ());
277
+ } else if (errCode == OB_KV_HBASE_NAMESPACE_NOT_FOUND .errorCode ) {
278
+ throw new NamespaceNotFoundException (tableName .getNameAsString ());
279
+ }
280
+ });
234
281
}
235
282
}
236
283
@@ -260,12 +307,15 @@ public void deleteTable(TableName tableName) throws IOException {
260
307
try {
261
308
executor .deleteTable (tableName .getNameAsString ());
262
309
} catch (IOException e ) {
263
- if (e .getCause () instanceof ObTableTransportException
264
- && ((ObTableTransportException ) e .getCause ()).getErrorCode () == TransportCodes .BOLT_TIMEOUT ) {
265
- throw new TimeoutIOException (e .getCause ());
266
- } else {
267
- throw e ;
268
- }
310
+ handleObTableException (e , tableName , (errCode , argTableName ) -> {
311
+ if (errCode == OB_KV_HBASE_TABLE_NOT_EXISTS .errorCode ) {
312
+ throw new TableNotFoundException (argTableName );
313
+ } else if (errCode == OB_KV_HBASE_NAMESPACE_NOT_FOUND .errorCode ) {
314
+ throw new NamespaceNotFoundException (argTableName .getNamespaceAsString ());
315
+ } else if (errCode == OB_KV_TABLE_NOT_DISABLED .errorCode ) {
316
+ throw new TableNotDisabledException (argTableName );
317
+ }
318
+ });
269
319
}
270
320
}
271
321
@@ -304,12 +354,13 @@ public void enableTable(TableName tableName) throws IOException {
304
354
try {
305
355
executor .enableTable (tableName .getNameAsString ());
306
356
} catch (IOException e ) {
307
- if (e .getCause () instanceof ObTableTransportException
308
- && ((ObTableTransportException ) e .getCause ()).getErrorCode () == TransportCodes .BOLT_TIMEOUT ) {
309
- throw new TimeoutIOException (e .getCause ());
310
- } else {
311
- throw e ;
312
- }
357
+ handleObTableException (e , tableName , (errCode , argTableName ) -> {
358
+ if (errCode == OB_KV_HBASE_TABLE_NOT_EXISTS .errorCode ) {
359
+ throw new TableNotFoundException (argTableName );
360
+ } else if (errCode == OB_KV_HBASE_NAMESPACE_NOT_FOUND .errorCode ) {
361
+ throw new NamespaceNotFoundException (argTableName .getNamespaceAsString ());
362
+ }
363
+ });
313
364
}
314
365
}
315
366
@@ -343,12 +394,13 @@ public void disableTable(TableName tableName) throws IOException {
343
394
try {
344
395
executor .disableTable (tableName .getNameAsString ());
345
396
} catch (IOException e ) {
346
- if (e .getCause () instanceof ObTableTransportException
347
- && ((ObTableTransportException ) e .getCause ()).getErrorCode () == TransportCodes .BOLT_TIMEOUT ) {
348
- throw new TimeoutIOException (e .getCause ());
349
- } else {
350
- throw e ;
351
- }
397
+ handleObTableException (e , tableName , (errCode , argTableName ) -> {
398
+ if (errCode == OB_KV_HBASE_TABLE_NOT_EXISTS .errorCode ) {
399
+ throw new TableNotFoundException (argTableName );
400
+ } else if (errCode == OB_KV_HBASE_NAMESPACE_NOT_FOUND .errorCode ) {
401
+ throw new NamespaceNotFoundException (argTableName .getNamespaceAsString ());
402
+ }
403
+ });
352
404
}
353
405
}
354
406
@@ -747,7 +799,18 @@ public Map<byte[], RegionLoad> getRegionLoad(ServerName serverName, TableName ta
747
799
tableName , connectionConf );
748
800
OHRegionLoadExecutor executor = new OHRegionLoadExecutor (tableName .getNameAsString (),
749
801
tableClient );
750
- return executor .getRegionLoad ();
802
+ try {
803
+ return executor .getRegionLoad ();
804
+ } catch (Exception e ) {
805
+ handleObTableException (e , tableName , (errCode , argTableName ) -> {
806
+ if (errCode == OB_KV_HBASE_TABLE_NOT_EXISTS .errorCode ) {
807
+ throw new TableNotFoundException (argTableName .getNameAsString ());
808
+ } else if (errCode == OB_KV_HBASE_NAMESPACE_NOT_FOUND .errorCode ) {
809
+ throw new NamespaceNotFoundException (argTableName .getNamespaceAsString ());
810
+ }
811
+ });
812
+ throw e ; // should never reach
813
+ }
751
814
}
752
815
753
816
@ Override
0 commit comments