32
32
import java .util .concurrent .Future ;
33
33
import java .util .regex .Pattern ;
34
34
35
+ import static com .alipay .oceanbase .rpc .protocol .payload .ResultCodes .*;
36
+
35
37
public class OHAdmin implements Admin {
36
38
private boolean aborted = false ;
37
39
private final OHConnectionImpl connection ;
38
40
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
+
39
85
OHAdmin (OHConnectionImpl connection ) {
40
86
this .connection = connection ;
41
87
this .conf = connection .getConfiguration ();
@@ -72,10 +118,7 @@ public boolean tableExists(TableName tableName) throws IOException {
72
118
return executor .tableExists (tableName .getNameAsString ());
73
119
} catch (Exception e ) {
74
120
// 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 );
79
122
if (cause instanceof ObTableException ) {
80
123
int errCode = ((ObTableException ) cause ).getErrorCode ();
81
124
// 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 {
84
127
return false ;
85
128
}
86
129
}
87
- throw e ;
130
+ if (e instanceof IOException ) {
131
+ throw (IOException ) e ;
132
+ } else {
133
+ throw new IOException (e );
134
+ }
88
135
}
89
136
}
90
137
@@ -161,14 +208,14 @@ public HTableDescriptor getTableDescriptor(TableName tableName) throws TableNotF
161
208
try {
162
209
return executor .getTableDescriptor ();
163
210
} 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
172
219
}
173
220
}
174
221
@@ -180,14 +227,14 @@ public TableDescriptor getDescriptor(TableName tableName) throws IOException {
180
227
try {
181
228
return executor .getTableDescriptor ();
182
229
} 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
191
238
}
192
239
}
193
240
@@ -199,12 +246,13 @@ public void createTable(TableDescriptor tableDescriptor) throws IOException {
199
246
try {
200
247
executor .createTable (tableDescriptor , null );
201
248
} 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
+ });
208
256
}
209
257
}
210
258
@@ -231,12 +279,15 @@ public void deleteTable(TableName tableName) throws IOException {
231
279
try {
232
280
executor .deleteTable (tableName .getNameAsString ());
233
281
} 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
+ });
240
291
}
241
292
}
242
293
@@ -273,12 +324,13 @@ public void enableTable(TableName tableName) throws IOException {
273
324
try {
274
325
executor .enableTable (tableName .getNameAsString ());
275
326
} 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
+ });
282
334
}
283
335
}
284
336
@@ -310,12 +362,13 @@ public void disableTable(TableName tableName) throws IOException {
310
362
try {
311
363
executor .disableTable (tableName .getNameAsString ());
312
364
} 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
+ });
319
372
}
320
373
}
321
374
@@ -704,7 +757,18 @@ public List<RegionMetrics> getRegionMetrics(ServerName serverName, TableName tab
704
757
OHConnectionConfiguration connectionConf = new OHConnectionConfiguration (conf );
705
758
ObTableClient tableClient = ObTableClientManager .getOrCreateObTableClientByTableName (tableName , connectionConf );
706
759
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
+ }
708
772
}
709
773
710
774
@ Override
0 commit comments