Skip to content

Commit a075918

Browse files
committed
cherry pick 32534b6 hbase2.0 to master
1 parent 0064e2a commit a075918

File tree

2 files changed

+152
-48
lines changed

2 files changed

+152
-48
lines changed

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

Lines changed: 90 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,55 @@
3636
import java.util.regex.Pattern;
3737
import java.util.stream.Collectors;
3838

39+
import static com.alipay.oceanbase.rpc.protocol.payload.ResultCodes.*;
40+
3941
public class OHAdmin implements Admin {
4042
private boolean aborted = false;
4143
private final OHConnectionImpl connection;
4244
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+
}
4388
OHAdmin(OHConnectionImpl connection) {
4489
this.connection = connection;
4590
this.conf = connection.getConfiguration();
@@ -76,10 +121,7 @@ public boolean tableExists(TableName tableName) throws IOException {
76121
return executor.tableExists(tableName.getNameAsString());
77122
} catch (Exception e) {
78123
// 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);
83125
if (cause instanceof ObTableException) {
84126
int errCode = ((ObTableException) cause).getErrorCode();
85127
// 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 {
88130
return false;
89131
}
90132
}
91-
throw e;
133+
if (e instanceof IOException) {
134+
throw (IOException) e;
135+
} else {
136+
throw new IOException(e);
137+
}
92138
}
93139
}
94140

@@ -150,15 +196,15 @@ public HTableDescriptor getTableDescriptor(TableName tableName) throws TableNotF
150196
try {
151197
return executor.getTableDescriptor();
152198
} 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+
}
162208
}
163209

164210
@Override
@@ -169,14 +215,13 @@ public void createTable(HTableDescriptor tableDescriptor) throws IOException {
169215
try {
170216
executor.createTable(tableDescriptor, null);
171217
} 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+
});
180225
}
181226
}
182227

@@ -203,12 +248,15 @@ public void deleteTable(TableName tableName) throws IOException {
203248
try {
204249
executor.deleteTable(tableName.getNameAsString());
205250
} 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+
});
212260
}
213261
}
214262

@@ -235,16 +283,13 @@ public void enableTable(TableName tableName) throws IOException {
235283
try {
236284
executor.enableTable(tableName.getNameAsString());
237285
} 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+
});
248293
}
249294
}
250295

@@ -276,16 +321,13 @@ public void disableTable(TableName tableName) throws IOException {
276321
try {
277322
executor.disableTable(tableName.getNameAsString());
278323
} 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+
});
289331
}
290332
}
291333

src/test/java/com/alipay/oceanbase/hbase/OHTableAdminInterfaceTest.java

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -477,8 +477,14 @@ public void testAdminEnDisableTable() throws Exception {
477477
e.printStackTrace();
478478
throw e;
479479
} finally {
480+
if (admin.isTableEnabled(TableName.valueOf("test_en_dis_tb"))) {
481+
admin.disableTable(TableName.valueOf("test_en_dis_tb"));
482+
}
480483
admin.deleteTable(TableName.valueOf("test_en_dis_tb"));
481484
assertFalse(admin.tableExists(TableName.valueOf("test_en_dis_tb")));
485+
if (admin.isTableEnabled(TableName.valueOf("en_dis", "test"))) {
486+
admin.disableTable(TableName.valueOf("en_dis", "test"));
487+
}
482488
admin.deleteTable(TableName.valueOf("en_dis", "test"));
483489
assertFalse(admin.tableExists(TableName.valueOf("en_dis", "test")));
484490
}
@@ -532,11 +538,20 @@ public void testAdminDeleteTable() throws Exception {
532538
assertTrue(admin.tableExists(TableName.valueOf("test_del_tb")));
533539
IOException thrown = assertThrows(IOException.class,
534540
() -> {
541+
if (admin.isTableEnabled(TableName.valueOf("tablegroup_not_exists"))) {
542+
admin.disableTable(TableName.valueOf("tablegroup_not_exists"));
543+
}
535544
admin.deleteTable(TableName.valueOf("tablegroup_not_exists"));
536545
});
537546
Assert.assertTrue(thrown.getCause() instanceof ObTableException);
538547
Assert.assertEquals(ResultCodes.OB_KV_HBASE_TABLE_NOT_EXISTS.errorCode, ((ObTableException) thrown.getCause()).getErrorCode());
548+
if (admin.isTableEnabled(TableName.valueOf("del_tb", "test"))) {
549+
admin.disableTable(TableName.valueOf("del_tb", "test"));
550+
}
539551
admin.deleteTable(TableName.valueOf("del_tb", "test"));
552+
if (admin.isTableEnabled(TableName.valueOf("test_del_tb"))) {
553+
admin.disableTable(TableName.valueOf("test_del_tb"));
554+
}
540555
admin.deleteTable(TableName.valueOf("test_del_tb"));
541556
assertFalse(admin.tableExists(TableName.valueOf("del_tb", "test")));
542557
assertFalse(admin.tableExists(TableName.valueOf("test_del_tb")));
@@ -1263,6 +1278,9 @@ public void testCreateTableInjectError() throws Exception {
12631278
if (admin.tableExists(TableName.valueOf(tableName))) {
12641279
setErrSimPoint(ErrSimPoint.EN_DELETE_HTABLE_CF_FINISH_ERR, false);
12651280
setErrSimPoint(ErrSimPoint.EN_DELETE_HTABLE_SKIP_CF_ERR, false);
1281+
if (admin.isTableEnabled(TableName.valueOf(tableName))) {
1282+
admin.disableTable(TableName.valueOf(tableName));
1283+
}
12661284
admin.deleteTable(TableName.valueOf(tableName));
12671285
}
12681286
}
@@ -1326,6 +1344,9 @@ public void testHbaseDDLException() throws Exception {
13261344
} catch (Exception e) {
13271345
Assert.assertEquals(e.getClass(), TableExistsException.class);
13281346
} finally {
1347+
if (admin.isTableEnabled(TableName.valueOf("t1"))) {
1348+
admin.disableTable(TableName.valueOf("t1"));
1349+
}
13291350
admin.deleteTable(TableName.valueOf("t1"));
13301351
}
13311352

@@ -1345,6 +1366,9 @@ public void testHbaseDDLException() throws Exception {
13451366
} catch (Exception e) {
13461367
Assert.assertEquals(e.getClass(), TableNotDisabledException.class);
13471368
} finally {
1369+
if (admin.isTableEnabled(TableName.valueOf("t1"))) {
1370+
admin.disableTable(TableName.valueOf("t1"));
1371+
}
13481372
admin.deleteTable(TableName.valueOf("t1"));
13491373
}
13501374

@@ -1376,6 +1400,9 @@ public void testHbaseDDLException() throws Exception {
13761400
} catch (Exception e) {
13771401
Assert.assertEquals(e.getClass(), TableExistsException.class);
13781402
} finally {
1403+
if (admin.isTableEnabled(TableName.valueOf("t1"))) {
1404+
admin.disableTable(TableName.valueOf("t1"));
1405+
}
13791406
admin.deleteTable(TableName.valueOf("t1"));
13801407
}
13811408

@@ -1395,6 +1422,9 @@ public void testHbaseDDLException() throws Exception {
13951422
} catch (Exception e) {
13961423
Assert.assertEquals(e.getClass(), TableNotDisabledException.class);
13971424
} finally {
1425+
if (admin.isTableEnabled(TableName.valueOf("t1"))) {
1426+
admin.disableTable(TableName.valueOf("t1"));
1427+
}
13981428
admin.deleteTable(TableName.valueOf("t1"));
13991429
}
14001430

@@ -1407,6 +1437,9 @@ public void testHbaseDDLException() throws Exception {
14071437
} catch (Exception e) {
14081438
Assert.assertEquals(e.getClass(), TableNotEnabledException.class);
14091439
} finally {
1440+
if (admin.isTableEnabled(TableName.valueOf("t1"))) {
1441+
admin.disableTable(TableName.valueOf("t1"));
1442+
}
14101443
admin.deleteTable(TableName.valueOf("t1"));
14111444
}
14121445

@@ -1449,6 +1482,9 @@ public void testCreateDropTableGroup() throws Exception {
14491482

14501483
// 1. open err EN_DELETE_HTABLE_SKIP_CF_ERR, will skip delete cf table when delete hbase table
14511484
// and the subsequent delete htable operations will return OB_TABLEGROUP_NOT_EMPTY
1485+
if (admin.isTableEnabled(TableName.valueOf(tableName))) {
1486+
admin.disableTable(TableName.valueOf(tableName));
1487+
}
14521488
setErrSimPoint(ErrSimPoint.EN_DELETE_HTABLE_SKIP_CF_ERR, true);
14531489
ObHTableTestUtil.executeIgnoreExpectedErrors(() -> admin.deleteTable(TableName.valueOf(tableName)), "OB_TABLEGROUP_NOT_EMPTY");
14541490
assertTrue("Table should still exist after delete error injection",
@@ -1474,6 +1510,9 @@ public void testCreateDropTableGroup() throws Exception {
14741510
executeSQL(conn, "drop database if exists db_test_create_drop_tg_helper", true);
14751511
executeSQL(sysConn, String.format("alter tenant %s set default tablegroup = null", tenantName), true);
14761512
if (admin.tableExists(TableName.valueOf(tableName))) {
1513+
if (admin.isTableEnabled(TableName.valueOf(tableName))) {
1514+
admin.disableTable(TableName.valueOf(tableName));
1515+
}
14771516
setErrSimPoint(ErrSimPoint.EN_DELETE_HTABLE_SKIP_CF_ERR, false);
14781517
admin.deleteTable(TableName.valueOf(tableName));
14791518
}
@@ -1566,4 +1605,27 @@ public void testDDLStmtStr() throws Exception {
15661605
}
15671606
}
15681607
}
1608+
1609+
@Test
1610+
public void testDropEnabledTableFail() throws Exception {
1611+
Configuration conf = ObHTableTestUtil.newConfiguration();
1612+
Connection connection = ConnectionFactory.createConnection(conf);
1613+
Admin admin = connection.getAdmin();
1614+
1615+
byte[] tableName = Bytes.toBytes("test_drop_enabled_table_fail");
1616+
byte[] cf1 = Bytes.toBytes("cf1");
1617+
HColumnDescriptor hcd1 = new HColumnDescriptor(cf1);
1618+
HTableDescriptor htd = new HTableDescriptor(TableName.valueOf(tableName));
1619+
htd.addFamily(hcd1);
1620+
try {
1621+
admin.createTable(htd);
1622+
admin.deleteTable(TableName.valueOf(tableName));
1623+
fail();
1624+
} catch (Exception e) {
1625+
assertTrue(true);
1626+
}
1627+
admin.disableTable(TableName.valueOf(tableName));
1628+
admin.deleteTable(TableName.valueOf(tableName));
1629+
Assert.assertFalse(admin.tableExists(TableName.valueOf(tableName)));
1630+
}
15691631
}

0 commit comments

Comments
 (0)