[inner-2215] in execute protocol, prepare sql need to be recorded in sql_log (cherry pick)

This commit is contained in:
wenyh1
2023-05-30 18:21:41 +08:00
parent 109d8b47b3
commit 6bd14dfbad
3 changed files with 17 additions and 4 deletions
@@ -17,6 +17,7 @@ import com.actiontech.dble.services.rwsplit.RWSplitService;
import com.actiontech.dble.services.rwsplit.handle.PSHandler;
import com.actiontech.dble.services.rwsplit.handle.PreparedStatementHolder;
import com.actiontech.dble.singleton.RouteService;
import com.actiontech.dble.statistic.sql.StatisticListener;
import com.actiontech.dble.util.StringUtil;
import org.jetbrains.annotations.Nullable;
import org.slf4j.Logger;
@@ -151,6 +152,7 @@ public class RWSplitNonBlockingSession extends Session {
if ((originPacket != null && originPacket.length > 4 && originPacket[4] == MySQLPacket.COM_STMT_EXECUTE)) {
long statementId = ByteUtil.readUB4(originPacket, 5);
PreparedStatementHolder holder = rwSplitService.getPrepareStatement(statementId);
StatisticListener.getInstance().record(rwSplitService, r -> r.onFrontendSetSql(getService().getSchema(), holder.getPrepareSql()));
if (holder.isMustMaster() && conn.getInstance().isReadInstance()) {
holder.setExecuteOrigin(originPacket);
PSHandler handler = new PSHandler(rwSplitService, holder);
@@ -239,6 +239,11 @@ public class RWSplitService extends BusinessService<RwSplitUserConfig> {
try {
RwSplitServerParse serverParse = ServerParseFactory.getRwSplitParser();
String sql = mm.readString(getCharset().getClient());
if (sql.endsWith(";")) {
sql = sql.substring(0, sql.length() - 1).trim();
}
sql = sql.trim();
final String finalSql = sql;
int rs = serverParse.parse(sql);
int sqlType = rs & 0xff;
if (sqlType == ServerParse.SELECT) {
@@ -248,7 +253,7 @@ public class RWSplitService extends BusinessService<RwSplitUserConfig> {
if (isSuccess) {
long statementId = ByteUtil.readUB4(resp, 5);
int paramCount = ByteUtil.readUB2(resp, 11);
psHolder.put(statementId, new PreparedStatementHolder(data, paramCount, true));
psHolder.put(statementId, new PreparedStatementHolder(data, paramCount, true, finalSql));
}
}, false);
} else {
@@ -256,7 +261,7 @@ public class RWSplitService extends BusinessService<RwSplitUserConfig> {
if (isSuccess) {
long statementId = ByteUtil.readUB4(resp, 5);
int paramCount = ByteUtil.readUB2(resp, 11);
psHolder.put(statementId, new PreparedStatementHolder(data, paramCount, false));
psHolder.put(statementId, new PreparedStatementHolder(data, paramCount, false, finalSql));
}
}, false);
}
@@ -265,7 +270,7 @@ public class RWSplitService extends BusinessService<RwSplitUserConfig> {
if (isSuccess) {
long statementId = ByteUtil.readUB4(resp, 5);
int paramCount = ByteUtil.readUB2(resp, 11);
psHolder.put(statementId, new PreparedStatementHolder(data, paramCount, true));
psHolder.put(statementId, new PreparedStatementHolder(data, paramCount, true, finalSql));
}
});
}
@@ -8,11 +8,13 @@ public class PreparedStatementHolder {
private final boolean mustMaster;
private byte[] fieldType;
private boolean needAddFieldType;
private String prepareSql;
public PreparedStatementHolder(byte[] prepareOrigin, int paramsCount, boolean mustMaster) {
public PreparedStatementHolder(byte[] prepareOrigin, int paramsCount, boolean mustMaster, String sql) {
this.prepareOrigin = prepareOrigin;
this.paramsCount = paramsCount;
this.mustMaster = mustMaster;
this.prepareSql = sql;
}
public boolean isMustMaster() {
@@ -52,4 +54,8 @@ public class PreparedStatementHolder {
public int getParamsCount() {
return paramsCount;
}
public String getPrepareSql() {
return prepareSql;
}
}