Skip to content

Commit

Permalink
[inner-2215] in execute protocol, prepare sql need to be recorded in …
Browse files Browse the repository at this point in the history
…sql_log (cherry pick)
  • Loading branch information
wenyh1 committed May 31, 2023
1 parent a300a70 commit 341ed10
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
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 org.jetbrains.annotations.Nullable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -163,6 +164,7 @@ private RWSplitHandler getRwSplitHandler(byte[] originPacket, Callback callback)
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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,11 @@ private void handleComStmtPrepare(byte[] data) {
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);
if (blacklistCheck(sql, userConfig.getBlacklist())) return;
int sqlType = rs & 0xff;
Expand All @@ -302,15 +307,15 @@ private void handleComStmtPrepare(byte[] data) {
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 {
session.execute(null, data, (isSuccess, resp, rwSplitService) -> {
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);
}
Expand All @@ -319,7 +324,7 @@ private void handleComStmtPrepare(byte[] data) {
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));
}
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -52,4 +54,8 @@ public boolean isNeedAddFieldType() {
public int getParamsCount() {
return paramsCount;
}

public String getPrepareSql() {
return prepareSql;
}
}

0 comments on commit 341ed10

Please sign in to comment.