Skip to content

Commit

Permalink
fix issue chaosblade-io#304
Browse files Browse the repository at this point in the history
Signed-off-by: hengyoush <hengyoush1@163.com>
  • Loading branch information
hengyoush committed May 12, 2023
1 parent 05f7a11 commit 9afe8ec
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@

import java.lang.reflect.Method;

import static com.alibaba.chaosblade.exec.plugin.mysql.MysqlConstant.MYSQL_IO_CLASS;
import static com.alibaba.chaosblade.exec.plugin.mysql.MysqlConstant.MYSQL_SERVER_PREPARED_STMT_METHOD;

/**
* @author yefei
* @create 2020-11-19 16:14
Expand All @@ -42,23 +45,36 @@ public EnhancerModel doBeforeAdvice(ClassLoader classLoader, String className, O
return null;
}

Object statement = methodArguments[0];
Object query = methodArguments[1];
boolean isMysqlIOClass = MYSQL_IO_CLASS.equals(className);
Object statement;
Object query;
Object catalog;
if (methodArguments.length == 10) {
// mysql 5.1.x
catalog = methodArguments[8];
} else if (methodArguments.length == 11) {
// mysql 5.0.x
catalog = methodArguments[9];
Object mysqlIO;
if (isMysqlIOClass) {
statement = methodArguments[0];
query = methodArguments[1];
if (methodArguments.length == 10) {
// mysql 5.1.x
catalog = methodArguments[8];
} else if (methodArguments.length == 11) {
// mysql 5.0.x
catalog = methodArguments[9];
} else {
logger.warn("The necessary parameters is null or length is not equal 10 or 11, {}",
methodArguments != null ? methodArguments.length : null);
return null;
}
mysqlIO = object;
} else {
logger.warn("The necessary parameters is null or length is not equal 10 or 11, {}",
methodArguments != null ? methodArguments.length : null);
return null;
statement = object;
query = ReflectUtil.getSuperclassFieldValue(object, "originalSql", false);
catalog = ReflectUtil.getSuperclassFieldValue(object, "currentCatalog", false);
Object connection = ReflectUtil.getSuperclassFieldValue(object, "connection", false);
mysqlIO = ReflectUtil.getSuperclassFieldValue(connection, "io", false);
}

String host = ReflectUtil.getFieldValue(object, "host", false);
Integer port = ReflectUtil.getFieldValue(object, "port", false);
String host = ReflectUtil.getFieldValue(mysqlIO, "host", false);
Integer port = ReflectUtil.getFieldValue(mysqlIO, "port", false);
String sql = (String) query;
String database = null;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,21 @@ public EnhancerModel doBeforeAdvice(ClassLoader classLoader, String className, O
return null;
}

Object callingQuery = methodArguments[0];
Object connection = ReflectUtil.getSuperclassFieldValue(callingQuery, "connection", false);
String sql = String.valueOf(methodArguments[1]);
Object callingQuery;
String sql;
if (MysqlConstant.MYSQL8_NATIVE_SESSION_CLASS.equals(className)) {
callingQuery = methodArguments[0];
sql = String.valueOf(methodArguments[1]);
} else {
callingQuery = object;
sql = null;
}

if (ReflectUtil.isAssignableFrom(classLoader, callingQuery.getClass(), "com.mysql.cj.jdbc.JdbcPreparedStatement")) {
sql = ReflectUtil.invokeMethod(callingQuery, "getPreparedSql", new Object[0], false);
}

Object connection = ReflectUtil.getSuperclassFieldValue(callingQuery, "connection", false);
String host = ReflectUtil.getFieldValue(connection, "origHostToConnectTo", false);
Integer port = ReflectUtil.getFieldValue(connection, "origPortToConnectTo", false);
String database = ReflectUtil.getFieldValue(connection, "database", false);;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,13 @@ public interface MysqlConstant {

String MYSQL_IO_CLASS = "com.mysql.jdbc.MysqlIO";
String INTERCEPTOR_PRE_METHOD = "sqlQueryDirect";
String MYSQL_SERVER_PREPARED_STMT_CLASS = "com.mysql.jdbc.ServerPreparedStatement";
String MYSQL_SERVER_PREPARED_STMT_METHOD = "serverExecute";

String MYSQL8_NATIVE_SESSION_CLASS = "com.mysql.cj.NativeSession";
String MYSQL8_NATIVE_SESSION_METHOD = "execSQL";
String MYSQL8_SERVER_PREPARED_STMT_CLASS = "com.mysql.cj.jdbc.ServerPreparedStatement";
String MYSQL8SERVER_PREPARED_STMT_METHOD = "serverExecute";


String IO_SHARDING_STATEMENT_EXECUTOR_CLASS = "io.shardingsphere.shardingjdbc.executor.AbstractStatementExecutor";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public EnhancerModel doBeforeAdvice(ClassLoader classLoader, String className, O
Method method, Object[] methodArguments)
throws Exception {

if (MYSQL_IO_CLASS.equals(className)) {
if (MYSQL_IO_CLASS.equals(className) || MYSQL_SERVER_PREPARED_STMT_CLASS.equals(className)) {
return mysql5Enhancer.doBeforeAdvice(classLoader, className, object, method, methodArguments);
}else if(IO_SHARDING_STATEMENT_EXECUTOR_CLASS.equals(className)) {
return ioShardingJdbcEnhancer.doBeforeAdvice(classLoader, className, object, method, methodArguments);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import com.alibaba.chaosblade.exec.common.aop.matcher.clazz.ClassMatcher;
import com.alibaba.chaosblade.exec.common.aop.matcher.clazz.NameClassMatcher;
import com.alibaba.chaosblade.exec.common.aop.matcher.clazz.OrClassMatcher;
import com.alibaba.chaosblade.exec.common.aop.matcher.clazz.SuperClassMatcher;
import com.alibaba.chaosblade.exec.common.aop.matcher.method.MethodMatcher;
import com.alibaba.chaosblade.exec.common.aop.matcher.method.NameMethodMatcher;
import com.alibaba.chaosblade.exec.common.aop.matcher.method.OrMethodMatcher;
Expand All @@ -36,8 +37,8 @@ public ClassMatcher getClassMatcher() {
OrClassMatcher orClassMatcher = new OrClassMatcher();

return orClassMatcher
.or(new NameClassMatcher(MYSQL_IO_CLASS))
.or(new NameClassMatcher(MYSQL8_NATIVE_SESSION_CLASS))
.or(new NameClassMatcher(MYSQL_IO_CLASS)).or(new NameClassMatcher(MYSQL_SERVER_PREPARED_STMT_CLASS))
.or(new NameClassMatcher(MYSQL8_NATIVE_SESSION_CLASS)).or(new NameClassMatcher(MYSQL8_SERVER_PREPARED_STMT_CLASS))
.or(new NameClassMatcher(IO_SHARDING_STATEMENT_EXECUTOR_CLASS))
.or(new NameClassMatcher(APACHE_SHARDING_EXECUTOR_ENGINE_CLASS));
}
Expand All @@ -46,8 +47,8 @@ public ClassMatcher getClassMatcher() {
public MethodMatcher getMethodMatcher() {
OrMethodMatcher orMethodMatcher = new OrMethodMatcher();
return orMethodMatcher
.or(new NameMethodMatcher(INTERCEPTOR_PRE_METHOD))
.or(new NameMethodMatcher(MYSQL8_NATIVE_SESSION_METHOD))
.or(new NameMethodMatcher(INTERCEPTOR_PRE_METHOD)).or(new NameMethodMatcher(MYSQL_SERVER_PREPARED_STMT_METHOD))
.or(new NameMethodMatcher(MYSQL8_NATIVE_SESSION_METHOD)).or(new NameMethodMatcher(MYSQL8SERVER_PREPARED_STMT_METHOD))
.or(new NameMethodMatcher(IO_SHARDING_STATEMENT_EXECUTOR_METHOD))
.or(new NameMethodMatcher(APACHE_SHARDING_EXECUTOR_ENGINE_METHOD));
}
Expand Down

0 comments on commit 9afe8ec

Please sign in to comment.