Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
156 changes: 110 additions & 46 deletions src/main/java/com/alipay/oceanbase/hbase/util/OHAdmin.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,56 @@
import java.util.concurrent.Future;
import java.util.regex.Pattern;

import static com.alipay.oceanbase.rpc.protocol.payload.ResultCodes.*;

public class OHAdmin implements Admin {
private boolean aborted = false;
private final OHConnectionImpl connection;
private final Configuration conf;

@FunctionalInterface
private interface ExceptionHandler {
void handle(int errorCode, TableName tableName) throws IOException;
}

private Throwable getRootCause(Throwable e) {
Throwable cause = e.getCause();
while(cause != null && cause.getCause() != null) {
cause = cause.getCause();
}
return cause;
}

private void handleTimeoutException(Exception e) throws TimeoutIOException {
if (e.getCause() instanceof ObTableTransportException
&& ((ObTableTransportException) e.getCause()).getErrorCode() == TransportCodes.BOLT_TIMEOUT) {
throw new TimeoutIOException(e.getCause());
}
}

private void handleObTableException(Exception e, TableName tableName, ExceptionHandler exceptionHandler) throws IOException {
if (e instanceof IOException) {
handleTimeoutException(e);
}

Throwable cause = getRootCause(e);

if (cause instanceof ObTableException) {
int errCode = ((ObTableException) cause).getErrorCode();
try {
exceptionHandler.handle(errCode, tableName);
} catch (RuntimeException re) {
throw re;
}
}

if (e instanceof IOException) {
throw (IOException) e;
} else {
throw new IOException(e);
}
}

OHAdmin(OHConnectionImpl connection) {
this.connection = connection;
this.conf = connection.getConfiguration();
Expand Down Expand Up @@ -72,10 +118,7 @@ public boolean tableExists(TableName tableName) throws IOException {
return executor.tableExists(tableName.getNameAsString());
} catch (Exception e) {
// try to get the original cause
Throwable cause = e.getCause();
while(cause != null && cause.getCause() != null) {
cause = cause.getCause();
}
Throwable cause = getRootCause(e);
if (cause instanceof ObTableException) {
int errCode = ((ObTableException) cause).getErrorCode();
// if the original cause is database_not_exist, means namespace in tableName does not exist
Expand All @@ -84,7 +127,11 @@ public boolean tableExists(TableName tableName) throws IOException {
return false;
}
}
throw e;
if (e instanceof IOException) {
throw (IOException) e;
} else {
throw new IOException(e);
}
}
}

Expand Down Expand Up @@ -161,14 +208,14 @@ public HTableDescriptor getTableDescriptor(TableName tableName) throws TableNotF
try {
return executor.getTableDescriptor();
} catch (IOException e) {
if (e.getCause() instanceof ObTableTransportException
&& ((ObTableTransportException) e.getCause()).getErrorCode() == TransportCodes.BOLT_TIMEOUT) {
throw new TimeoutIOException(e.getCause());
} else if (e.getCause().getMessage().contains("OB_TABLEGROUP_NOT_EXIST")) {
throw new TableNotFoundException(tableName);
} else {
throw e;
}
handleObTableException(e, tableName, (errCode, argTableName) -> {
if (errCode == OB_KV_HBASE_TABLE_NOT_EXISTS.errorCode) {
throw new TableNotFoundException(argTableName);
} else if (errCode == OB_KV_HBASE_NAMESPACE_NOT_FOUND.errorCode) {
throw new NamespaceNotFoundException(argTableName.getNamespaceAsString());
}
});
throw e; // should never reach
}
}

Expand All @@ -180,14 +227,14 @@ public TableDescriptor getDescriptor(TableName tableName) throws IOException {
try {
return executor.getTableDescriptor();
} catch (IOException e) {
if (e.getCause() instanceof ObTableTransportException
&& ((ObTableTransportException) e.getCause()).getErrorCode() == TransportCodes.BOLT_TIMEOUT) {
throw new TimeoutIOException(e.getCause());
} else if (e.getCause().getMessage().contains("OB_TABLEGROUP_NOT_EXIST")) {
throw new TableNotFoundException(tableName);
} else {
throw e;
}
handleObTableException(e, tableName, (errCode, argTableName) -> {
if (errCode == OB_KV_HBASE_TABLE_NOT_EXISTS.errorCode) {
throw new TableNotFoundException(argTableName);
} else if (errCode == OB_KV_HBASE_NAMESPACE_NOT_FOUND.errorCode) {
throw new NamespaceNotFoundException(argTableName.getNamespaceAsString());
}
});
throw e; // should never reach
}
}

Expand All @@ -199,12 +246,13 @@ public void createTable(TableDescriptor tableDescriptor) throws IOException {
try {
executor.createTable(tableDescriptor, null);
} catch (IOException e) {
if (e.getCause() instanceof ObTableTransportException
&& ((ObTableTransportException) e.getCause()).getErrorCode() == TransportCodes.BOLT_TIMEOUT) {
throw new TimeoutIOException(e.getCause());
} else {
throw e;
}
handleObTableException(e, tableDescriptor.getTableName(), (errCode, tableName) -> {
if (errCode == OB_KV_HBASE_TABLE_EXISTS.errorCode) {
throw new TableExistsException(tableName.getNameAsString());
} else if (errCode == OB_KV_HBASE_NAMESPACE_NOT_FOUND.errorCode) {
throw new NamespaceNotFoundException(tableName.getNameAsString());
}
});
}
}

Expand All @@ -231,12 +279,15 @@ public void deleteTable(TableName tableName) throws IOException {
try {
executor.deleteTable(tableName.getNameAsString());
} catch (IOException e) {
if (e.getCause() instanceof ObTableTransportException
&& ((ObTableTransportException) e.getCause()).getErrorCode() == TransportCodes.BOLT_TIMEOUT) {
throw new TimeoutIOException(e.getCause());
} else {
throw e;
}
handleObTableException(e, tableName, (errCode, argTableName) -> {
if (errCode == OB_KV_HBASE_TABLE_NOT_EXISTS.errorCode) {
throw new TableNotFoundException(argTableName);
} else if (errCode == OB_KV_HBASE_NAMESPACE_NOT_FOUND.errorCode) {
throw new NamespaceNotFoundException(argTableName.getNamespaceAsString());
} else if (errCode == OB_KV_TABLE_NOT_DISABLED.errorCode) {
throw new TableNotDisabledException(argTableName);
}
});
}
}

Expand Down Expand Up @@ -273,12 +324,13 @@ public void enableTable(TableName tableName) throws IOException {
try {
executor.enableTable(tableName.getNameAsString());
} catch (IOException e) {
if (e.getCause() instanceof ObTableTransportException
&& ((ObTableTransportException) e.getCause()).getErrorCode() == TransportCodes.BOLT_TIMEOUT) {
throw new TimeoutIOException(e.getCause());
} else {
throw e;
}
handleObTableException(e, tableName, (errCode, argTableName) -> {
if (errCode == OB_KV_HBASE_TABLE_NOT_EXISTS.errorCode) {
throw new TableNotFoundException(argTableName);
} else if (errCode == OB_KV_HBASE_NAMESPACE_NOT_FOUND.errorCode) {
throw new NamespaceNotFoundException(argTableName.getNamespaceAsString());
}
});
}
}

Expand Down Expand Up @@ -310,12 +362,13 @@ public void disableTable(TableName tableName) throws IOException {
try {
executor.disableTable(tableName.getNameAsString());
} catch (IOException e) {
if (e.getCause() instanceof ObTableTransportException
&& ((ObTableTransportException) e.getCause()).getErrorCode() == TransportCodes.BOLT_TIMEOUT) {
throw new TimeoutIOException(e.getCause());
} else {
throw e;
}
handleObTableException(e, tableName, (errCode, argTableName) -> {
if (errCode == OB_KV_HBASE_TABLE_NOT_EXISTS.errorCode) {
throw new TableNotFoundException(argTableName);
} else if (errCode == OB_KV_HBASE_NAMESPACE_NOT_FOUND.errorCode) {
throw new NamespaceNotFoundException(argTableName.getNamespaceAsString());
}
});
}
}

Expand Down Expand Up @@ -704,7 +757,18 @@ public List<RegionMetrics> getRegionMetrics(ServerName serverName, TableName tab
OHConnectionConfiguration connectionConf = new OHConnectionConfiguration(conf);
ObTableClient tableClient = ObTableClientManager.getOrCreateObTableClientByTableName(tableName, connectionConf);
OHRegionMetricsExecutor executor = new OHRegionMetricsExecutor(tableClient);
return executor.getRegionMetrics(tableName.getNameAsString());
try {
return executor.getRegionMetrics(tableName.getNameAsString());
} catch (Exception e) {
handleObTableException(e, tableName, (errCode, argTableName) -> {
if (errCode == OB_KV_HBASE_TABLE_NOT_EXISTS.errorCode) {
throw new TableNotFoundException(argTableName.getNameAsString());
} else if (errCode == OB_KV_HBASE_NAMESPACE_NOT_FOUND.errorCode) {
throw new NamespaceNotFoundException(argTableName.getNamespaceAsString());
}
});
throw e; // should never reach
}
}

@Override
Expand Down
Loading