3
3
import com .alipay .oceanbase .hbase .exception .FeatureNotSupportedException ;
4
4
import com .alipay .oceanbase .rpc .ObTableClient ;
5
5
import com .alipay .oceanbase .rpc .bolt .transport .TransportCodes ;
6
+ import com .alipay .oceanbase .rpc .exception .ObTableException ;
6
7
import com .alipay .oceanbase .rpc .exception .ObTableTransportException ;
8
+ import com .alipay .oceanbase .rpc .meta .ObTableRpcMetaType ;
9
+ import com .alipay .oceanbase .rpc .protocol .payload .ResultCodes ;
7
10
import org .apache .hadoop .conf .Configuration ;
8
11
import org .apache .hadoop .hbase .*;
9
12
import org .apache .hadoop .hbase .client .Admin ;
25
28
import org .apache .hadoop .hbase .util .Pair ;
26
29
27
30
import java .io .IOException ;
31
+ import java .util .ArrayList ;
28
32
import java .util .Collections ;
29
33
import java .util .List ;
30
34
import java .util .Map ;
31
35
import java .util .concurrent .Future ;
32
36
import java .util .regex .Pattern ;
37
+ import java .util .stream .Collectors ;
33
38
34
39
public class OHAdmin implements Admin {
35
- private boolean aborted = false ;
36
- private final OHConnectionImpl connection ;
37
- private final Configuration conf ;
40
+ private boolean aborted = false ;
41
+ private final OHConnectionImpl connection ;
42
+ private final Configuration conf ;
38
43
OHAdmin (OHConnectionImpl connection ) {
39
44
this .connection = connection ;
40
45
this .conf = connection .getConfiguration ();
@@ -64,10 +69,27 @@ public Connection getConnection() {
64
69
65
70
@ Override
66
71
public boolean tableExists (TableName tableName ) throws IOException {
67
- OHConnectionConfiguration connectionConf = new OHConnectionConfiguration (conf );
68
- ObTableClient tableClient = ObTableClientManager .getOrCreateObTableClientByTableName (tableName , connectionConf );
69
- OHTableExistsExecutor executor = new OHTableExistsExecutor (tableClient );
70
- return executor .tableExists (tableName .getNameAsString ());
72
+ try {
73
+ OHConnectionConfiguration connectionConf = new OHConnectionConfiguration (conf );
74
+ ObTableClient tableClient = ObTableClientManager .getOrCreateObTableClientByTableName (tableName , connectionConf );
75
+ OHTableExistsExecutor executor = new OHTableExistsExecutor (tableClient );
76
+ return executor .tableExists (tableName .getNameAsString ());
77
+ } catch (Exception e ) {
78
+ // try to get the original cause
79
+ Throwable cause = e .getCause ();
80
+ while (cause != null && cause .getCause () != null ) {
81
+ cause = cause .getCause ();
82
+ }
83
+ if (cause instanceof ObTableException ) {
84
+ int errCode = ((ObTableException ) cause ).getErrorCode ();
85
+ // if the original cause is database_not_exist, means namespace in tableName does not exist
86
+ // for HBase, namespace not exist will not throw exceptions but will return false
87
+ if (errCode == ResultCodes .OB_ERR_BAD_DATABASE .errorCode ) {
88
+ return false ;
89
+ }
90
+ }
91
+ throw e ;
92
+ }
71
93
}
72
94
73
95
@ Override
@@ -122,16 +144,44 @@ public TableName[] listTableNames(String s, boolean b) throws IOException {
122
144
123
145
@ Override
124
146
public HTableDescriptor getTableDescriptor (TableName tableName ) throws TableNotFoundException , IOException {
125
- throw new FeatureNotSupportedException ("does not support yet" );
147
+ OHConnectionConfiguration connectionConf = new OHConnectionConfiguration (conf );
148
+ ObTableClient tableClient = ObTableClientManager .getOrCreateObTableClientByTableName (tableName , connectionConf );
149
+ OHTableDescriptorExecutor executor = new OHTableDescriptorExecutor (tableName .getNameAsString (), tableClient );
150
+ try {
151
+ return executor .getTableDescriptor ();
152
+ } 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
+ }
126
162
}
127
163
128
164
@ Override
129
- public void createTable (HTableDescriptor hTableDescriptor ) throws IOException {
130
- throw new FeatureNotSupportedException ("does not support yet" );
165
+ public void createTable (HTableDescriptor tableDescriptor ) throws IOException {
166
+ OHConnectionConfiguration connectionConf = new OHConnectionConfiguration (conf );
167
+ ObTableClient tableClient = ObTableClientManager .getOrCreateObTableClientByTableName (tableDescriptor .getTableName (), connectionConf );
168
+ OHCreateTableExecutor executor = new OHCreateTableExecutor (tableClient );
169
+ try {
170
+ executor .createTable (tableDescriptor , null );
171
+ } 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
+ }
180
+ }
131
181
}
132
182
133
183
@ Override
134
- public void createTable (HTableDescriptor hTableDescriptor , byte [] bytes , byte [] bytes1 , int i ) throws IOException {
184
+ public void createTable (HTableDescriptor tableDescriptor , byte [] bytes , byte [] bytes1 , int i ) throws IOException {
135
185
throw new FeatureNotSupportedException ("does not support yet" );
136
186
}
137
187
@@ -156,8 +206,8 @@ public void deleteTable(TableName tableName) throws IOException {
156
206
if (e .getCause () instanceof ObTableTransportException
157
207
&& ((ObTableTransportException ) e .getCause ()).getErrorCode () == TransportCodes .BOLT_TIMEOUT ) {
158
208
throw new TimeoutIOException (e .getCause ());
159
- } else {
160
- throw e ;
209
+ } else if ( e . getCause (). getMessage (). contains ( "not found" )) {
210
+ throw new TableNotFoundException ( tableName ) ;
161
211
}
162
212
}
163
213
}
@@ -179,7 +229,23 @@ public void truncateTable(TableName tableName, boolean b) throws IOException {
179
229
180
230
@ Override
181
231
public void enableTable (TableName tableName ) throws IOException {
182
- throw new FeatureNotSupportedException ("does not support yet" );
232
+ OHConnectionConfiguration connectionConf = new OHConnectionConfiguration (conf );
233
+ ObTableClient tableClient = ObTableClientManager .getOrCreateObTableClientByTableName (tableName , connectionConf );
234
+ OHTableAccessControlExecutor executor = new OHTableAccessControlExecutor (tableClient , ObTableRpcMetaType .HTABLE_ENABLE_TABLE );
235
+ try {
236
+ executor .enableTable (tableName .getNameAsString ());
237
+ } 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
+ }
248
+ }
183
249
}
184
250
185
251
@ Override
@@ -204,7 +270,23 @@ public void disableTableAsync(TableName tableName) throws IOException {
204
270
205
271
@ Override
206
272
public void disableTable (TableName tableName ) throws IOException {
207
- throw new FeatureNotSupportedException ("does not support yet" );
273
+ OHConnectionConfiguration connectionConf = new OHConnectionConfiguration (conf );
274
+ ObTableClient tableClient = ObTableClientManager .getOrCreateObTableClientByTableName (tableName , connectionConf );
275
+ OHTableAccessControlExecutor executor = new OHTableAccessControlExecutor (tableClient , ObTableRpcMetaType .HTABLE_DISABLE_TABLE );
276
+ try {
277
+ executor .disableTable (tableName .getNameAsString ());
278
+ } 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
+ }
289
+ }
208
290
}
209
291
210
292
@ Override
@@ -219,12 +301,19 @@ public HTableDescriptor[] disableTables(Pattern pattern) throws IOException {
219
301
220
302
@ Override
221
303
public boolean isTableEnabled (TableName tableName ) throws IOException {
222
- throw new FeatureNotSupportedException ( "does not support yet" );
304
+ return ! isDisabled ( tableName );
223
305
}
224
306
225
307
@ Override
226
308
public boolean isTableDisabled (TableName tableName ) throws IOException {
227
- throw new FeatureNotSupportedException ("does not support yet" );
309
+ return isDisabled (tableName );
310
+ }
311
+
312
+ private boolean isDisabled (TableName tableName ) throws IOException {
313
+ OHConnectionConfiguration connectionConf = new OHConnectionConfiguration (conf );
314
+ ObTableClient tableClient = ObTableClientManager .getOrCreateObTableClientByTableName (tableName , connectionConf );
315
+ OHTableDescriptorExecutor tableDescriptor = new OHTableDescriptorExecutor (tableName .getNameAsString (), tableClient );
316
+ return tableDescriptor .isDisable ();
228
317
}
229
318
230
319
@ Override
@@ -513,13 +602,35 @@ public synchronized void close() throws IOException {
513
602
}
514
603
515
604
@ Override
516
- public HTableDescriptor [] getTableDescriptorsByTableName (List <TableName > list ) throws IOException {
517
- throw new FeatureNotSupportedException ("does not support yet" );
605
+ public HTableDescriptor [] getTableDescriptorsByTableName (List <TableName > tableNames ) throws IOException {
606
+ OHConnectionConfiguration connectionConf = new OHConnectionConfiguration (conf );
607
+ List <HTableDescriptor > tableDescriptors = new ArrayList <>();
608
+ for (TableName tableName : tableNames ) {
609
+ ObTableClient tableClient = ObTableClientManager .getOrCreateObTableClientByTableName (tableName , connectionConf );
610
+ OHTableDescriptorExecutor executor = new OHTableDescriptorExecutor (tableName .getNameAsString (), tableClient );
611
+ try {
612
+ tableDescriptors .add (executor .getTableDescriptor ());
613
+ } catch (IOException e ) {
614
+ if (e .getCause () instanceof ObTableTransportException
615
+ && ((ObTableTransportException ) e .getCause ()).getErrorCode () == TransportCodes .BOLT_TIMEOUT ) {
616
+ throw new TimeoutIOException (e .getCause ());
617
+ } else if (e .getCause ().getMessage ().contains ("OB_TABLEGROUP_NOT_EXIST" )) {
618
+ throw new TableNotFoundException (tableName );
619
+ } else {
620
+ throw e ;
621
+ }
622
+ }
623
+ }
624
+ return tableDescriptors .toArray (new HTableDescriptor [0 ]);
518
625
}
519
626
520
627
@ Override
521
- public HTableDescriptor [] getTableDescriptors (List <String > list ) throws IOException {
522
- throw new FeatureNotSupportedException ("does not support yet" );
628
+ public HTableDescriptor [] getTableDescriptors (List <String > tableNames ) throws IOException {
629
+ List <TableName > tableNameList = new ArrayList <>();
630
+ for (String tableName : tableNames ) {
631
+ tableNameList .add (TableName .valueOf (tableName ));
632
+ }
633
+ return getTableDescriptorsByTableName (tableNameList );
523
634
}
524
635
525
636
@ Override
0 commit comments