Skip to content

Commit

Permalink
optimize: optimize derivative product check base on mysql (#6044)
Browse files Browse the repository at this point in the history
  • Loading branch information
slievrly authored Nov 17, 2023
1 parent c829e73 commit c0c8725
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 31 deletions.
2 changes: 1 addition & 1 deletion changes/en-us/develop.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ Add changes here for all PR submitted to the develop branch.
- [[#6026](https://github.com/seata/seata/pull/6026)] fix incorrect metric report

### optimize:
- [[#PR_NO](https://github.com/seata/seata/pull/PR_NO)] A brief and accurate description of PR
- [[#6044](https://github.com/seata/seata/pull/6044)] optimize derivative product check base on mysql

### security:
- [[#PR_NO](https://github.com/seata/seata/pull/PR_NO)] A brief and accurate description of PR
Expand Down
2 changes: 1 addition & 1 deletion changes/zh-cn/develop.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
- [[#6026](https://github.com/seata/seata/pull/6026)] 修复异常的打点

### optimize:
- [[#PR_NO](https://github.com/seata/seata/pull/PR_NO)] 准确简要的PR描述
- [[#6044](https://github.com/seata/seata/pull/6044)] 优化MySQL衍生数据库判断逻辑

### security:
- [[#PR_NO](https://github.com/seata/seata/pull/PR_NO)] 准确简要的PR描述
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.sql.DataSource;

Expand All @@ -31,6 +30,7 @@
import io.seata.rm.datasource.sql.struct.TableMetaCacheFactory;
import io.seata.rm.datasource.util.JdbcUtils;
import io.seata.sqlparser.util.JdbcConstants;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -55,7 +55,16 @@ public class DataSourceProxy extends AbstractDataSourceProxy implements Resource

private String userName;

private String version;
private String kernelVersion;

private String productVersion;

/**
* POLARDB-X 1.X -> TDDL
* POLARDB-X 2.X & MySQL 5.6 -> PXC
* POLARDB-X 2.X & MySQL 5.7 -> AliSQL-X
*/
private static final String[] POLARDB_X_PRODUCT_KEYWORD = {"TDDL","AliSQL-X","PXC"};

/**
* Instantiates a new Data source proxy.
Expand Down Expand Up @@ -89,9 +98,9 @@ private void init(DataSource dataSource, String resourceGroupId) {
if (JdbcConstants.ORACLE.equals(dbType)) {
userName = connection.getMetaData().getUserName();
} else if (JdbcConstants.MYSQL.equals(dbType)) {
getMySQLAdaptiveType(connection);
validMySQLVersion(connection);
checkDerivativeProduct();
}
version = selectDbVersion(connection);
} catch (SQLException e) {
throw new IllegalStateException("can not init dataSource", e);
}
Expand All @@ -103,17 +112,31 @@ private void init(DataSource dataSource, String resourceGroupId) {
}

/**
* get mysql adaptive type for PolarDB-X
* Define derivative product version for MySQL Kernel
*
* @param connection db connection
*/
private void getMySQLAdaptiveType(Connection connection) {
try (Statement statement = connection.createStatement()) {
statement.executeQuery("show rule");
private void checkDerivativeProduct() {
if (!JdbcConstants.MYSQL.equals(dbType)) {
return;
}
// check for polardb-x
if (isPolardbXProduct()) {
dbType = JdbcConstants.POLARDBX;
} catch (SQLException e) {
dbType = JdbcConstants.MYSQL;
return;
}
// check for other products base on mysql kernel
}

private boolean isPolardbXProduct() {
if (StringUtils.isBlank(productVersion)) {
return false;
}
for (String keyword : POLARDB_X_PRODUCT_KEYWORD) {
if (productVersion.contains(keyword)) {
return true;
}
}
return false;
}

/**
Expand Down Expand Up @@ -296,27 +319,33 @@ public BranchType getBranchType() {
return BranchType.AT;
}

public String getVersion() {
return version;
public String getKernelVersion() {
return kernelVersion;
}

private String selectDbVersion(Connection connection) {
if (JdbcConstants.MYSQL.equals(dbType) || JdbcConstants.POLARDBX.equals(dbType)) {
try (PreparedStatement preparedStatement = connection.prepareStatement("SELECT VERSION()");
ResultSet versionResult = preparedStatement.executeQuery()) {
if (versionResult.next()) {
String version = versionResult.getString("VERSION()");
if (version == null) {
return null;
}
int dashIdx = version.indexOf('-');
// in mysql: 5.6.45, in polardb-x: 5.6.45-TDDL-xxx
return dashIdx > 0 ? version.substring(0, dashIdx) : version;
private void validMySQLVersion(Connection connection) {
if (!JdbcConstants.MYSQL.equals(dbType)) {
return;
}
try (PreparedStatement preparedStatement = connection.prepareStatement("SELECT VERSION()");
ResultSet versionResult = preparedStatement.executeQuery()) {
if (versionResult.next()) {
String version = versionResult.getString("VERSION()");
if (StringUtils.isBlank(version)) {
return;
}
int dashIdx = version.indexOf('-');
// in mysql: 5.6.45, in polardb-x: 5.6.45-TDDL-xxx
if (dashIdx > 0) {
kernelVersion = version.substring(0, dashIdx);
productVersion = version.substring(dashIdx + 1);
} else {
kernelVersion = version;
productVersion = version;
}
} catch (Exception e) {
LOGGER.error("get mysql version fail error: {}", e.getMessage());
}
} catch (Exception e) {
LOGGER.error("check mysql version fail error: {}", e.getMessage());
}
return "";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,6 @@ private String buildGroupBy(List<String> pkColumns,List<String> allSelectColumns
}

private String getDbVersion() {
return statementProxy.getConnectionProxy().getDataSourceProxy().getVersion();
return statementProxy.getConnectionProxy().getDataSourceProxy().getKernelVersion();
}
}

0 comments on commit c0c8725

Please sign in to comment.