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
2 changes: 2 additions & 0 deletions pkg/sqlreplay/cmd/audit_log_plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,8 @@ func (decoder *AuditLogPluginDecoder) Decode(reader LineReader) (*Command, error
cmd.Success = true
cmd.ConnID = connID
cmd.StartTs = startTs
cmd.FileName = filename
cmd.Line = lineIdx
}
if len(cmds) > 1 {
decoder.pendingCmds = cmds[1:]
Expand Down
52 changes: 50 additions & 2 deletions pkg/sqlreplay/cmd/audit_log_plugin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -636,7 +636,7 @@ func TestDecodeSingleLine(t *testing.T) {
for i, test := range tests {
decoder := NewAuditLogPluginDecoder()
decoder.SetPSCloseStrategy(PSCloseStrategyAlways)
mr := mockReader{data: append([]byte(test.line), '\n')}
mr := mockReader{data: append([]byte(test.line), '\n'), filename: "my/file"}
cmds := make([]*Command, 0, 4)
var err error
for {
Expand All @@ -654,6 +654,10 @@ func TestDecodeSingleLine(t *testing.T) {
} else {
require.ErrorIs(t, err, io.EOF, "case %d", i)
}
for _, cmd := range test.cmds {
cmd.FileName = "my/file"
cmd.Line = 1
}
require.Equal(t, test.cmds, cmds, "case %d", i)
}
}
Expand All @@ -675,13 +679,15 @@ func TestDecodeMultiLines(t *testing.T) {
Type: pnet.ComQuery,
Payload: append([]byte{pnet.ComQuery.Byte()}, []byte("set sql_mode=''")...),
StmtType: "Set",
Line: 1,
Success: true,
},
{
Type: pnet.ComInitDB,
ConnID: 3695181836,
StartTs: time.Date(2025, 9, 6, 17, 3, 53, 718663917, time.FixedZone("", 8*3600+600)),
Payload: append([]byte{pnet.ComInitDB.Byte()}, []byte("test")...),
Line: 3,
Success: true,
},
{
Expand All @@ -690,6 +696,7 @@ func TestDecodeMultiLines(t *testing.T) {
Type: pnet.ComQuery,
Payload: append([]byte{pnet.ComQuery.Byte()}, []byte("select \"[=]\"")...),
StmtType: "Select",
Line: 3,
Success: true,
},
},
Expand All @@ -705,6 +712,7 @@ func TestDecodeMultiLines(t *testing.T) {
ConnID: 3695181836,
StartTs: time.Date(2025, 9, 6, 16, 16, 29, 583942167, time.FixedZone("", 8*3600+600)),
Payload: append([]byte{pnet.ComInitDB.Byte()}, []byte("test")...),
Line: 1,
Success: true,
},
{
Expand All @@ -713,6 +721,7 @@ func TestDecodeMultiLines(t *testing.T) {
Type: pnet.ComQuery,
Payload: append([]byte{pnet.ComQuery.Byte()}, []byte("set sql_mode=''")...),
StmtType: "Set",
Line: 1,
Success: true,
},
{
Expand All @@ -721,6 +730,7 @@ func TestDecodeMultiLines(t *testing.T) {
Type: pnet.ComQuery,
Payload: append([]byte{pnet.ComQuery.Byte()}, []byte("select \"[=]\"")...),
StmtType: "Select",
Line: 3,
Success: true,
},
},
Expand All @@ -735,6 +745,7 @@ func TestDecodeMultiLines(t *testing.T) {
ConnID: 3552575570,
Payload: []byte{pnet.ComQuit.Byte()},
Type: pnet.ComQuit,
Line: 2,
Success: true,
},
},
Expand All @@ -749,6 +760,7 @@ func TestDecodeMultiLines(t *testing.T) {
ConnID: 3695181836,
StartTs: time.Date(2025, 9, 6, 17, 3, 53, 718663917, time.FixedZone("", 8*3600+600)),
Payload: append([]byte{pnet.ComInitDB.Byte()}, []byte("test")...),
Line: 1,
Success: true,
},
{
Expand All @@ -758,6 +770,7 @@ func TestDecodeMultiLines(t *testing.T) {
CapturedPsID: 1,
Payload: append([]byte{pnet.ComStmtPrepare.Byte()}, []byte("select \"?\"")...),
StmtType: "Select",
Line: 1,
Success: true,
},
{
Expand All @@ -767,6 +780,7 @@ func TestDecodeMultiLines(t *testing.T) {
CapturedPsID: 1,
Payload: append([]byte{pnet.ComStmtExecute.Byte()}, []byte{1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 8, 0, 1, 0, 0, 0, 0, 0, 0, 0}...),
StmtType: "Select",
Line: 1,
Success: true,
},
{
Expand All @@ -776,6 +790,7 @@ func TestDecodeMultiLines(t *testing.T) {
CapturedPsID: 1,
Payload: append([]byte{pnet.ComStmtClose.Byte()}, []byte{1, 0, 0, 0}...),
StmtType: "Select",
Line: 1,
Success: true,
},
{
Expand All @@ -785,6 +800,7 @@ func TestDecodeMultiLines(t *testing.T) {
CapturedPsID: 2,
Payload: append([]byte{pnet.ComStmtPrepare.Byte()}, []byte("select \"?\"")...),
StmtType: "Select",
Line: 2,
Success: true,
},
{
Expand All @@ -794,6 +810,7 @@ func TestDecodeMultiLines(t *testing.T) {
CapturedPsID: 2,
Payload: append([]byte{pnet.ComStmtExecute.Byte()}, []byte{2, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 8, 0, 1, 0, 0, 0, 0, 0, 0, 0}...),
StmtType: "Select",
Line: 2,
Success: true,
},
{
Expand All @@ -803,6 +820,7 @@ func TestDecodeMultiLines(t *testing.T) {
CapturedPsID: 2,
Payload: append([]byte{pnet.ComStmtClose.Byte()}, []byte{2, 0, 0, 0}...),
StmtType: "Select",
Line: 2,
Success: true,
},
},
Expand All @@ -817,6 +835,7 @@ func TestDecodeMultiLines(t *testing.T) {
ConnID: 3695181836,
StartTs: time.Date(2025, 9, 6, 17, 3, 53, 718663917, time.FixedZone("", 8*3600+600)),
Payload: append([]byte{pnet.ComInitDB.Byte()}, []byte("test")...),
Line: 1,
Success: true,
},
{
Expand All @@ -825,13 +844,15 @@ func TestDecodeMultiLines(t *testing.T) {
StartTs: time.Date(2025, 9, 6, 17, 3, 53, 718663917, time.FixedZone("", 8*3600+600)),
Payload: append([]byte{pnet.ComQuery.Byte()}, []byte("select \"[=]\"")...),
StmtType: "Select",
Line: 1,
Success: true,
},
{
Type: pnet.ComInitDB,
ConnID: 3695181837,
StartTs: time.Date(2025, 9, 6, 17, 3, 53, 718663917, time.FixedZone("", 8*3600+600)),
Payload: append([]byte{pnet.ComInitDB.Byte()}, []byte("test")...),
Line: 2,
Success: true,
},
{
Expand All @@ -840,6 +861,7 @@ func TestDecodeMultiLines(t *testing.T) {
StartTs: time.Date(2025, 9, 6, 17, 3, 53, 718663917, time.FixedZone("", 8*3600+600)),
Payload: append([]byte{pnet.ComQuery.Byte()}, []byte("select \"[=]\"")...),
StmtType: "Select",
Line: 2,
Success: true,
},
},
Expand All @@ -849,7 +871,7 @@ func TestDecodeMultiLines(t *testing.T) {
for i, test := range tests {
decoder := NewAuditLogPluginDecoder()
decoder.SetPSCloseStrategy(PSCloseStrategyAlways)
mr := mockReader{data: append([]byte(test.lines), '\n')}
mr := mockReader{data: append([]byte(test.lines), '\n'), filename: "my/file"}
cmds := make([]*Command, 0, len(test.cmds))
for {
cmd, err := decoder.Decode(&mr)
Expand All @@ -859,6 +881,9 @@ func TestDecodeMultiLines(t *testing.T) {
}
cmds = append(cmds, cmd)
}
for _, cmd := range test.cmds {
cmd.FileName = "my/file"
}
require.Equal(t, test.cmds, cmds, "case %d", i)
}
}
Expand All @@ -879,6 +904,7 @@ func TestDecodeAuditLogWithCommandStartTime(t *testing.T) {
ConnID: 3695181836,
StartTs: time.Date(2025, 9, 14, 16, 16, 53, 718663917, time.FixedZone("", 8*3600+600)),
Payload: append([]byte{pnet.ComInitDB.Byte()}, []byte("test")...),
Line: 3,
Success: true,
},
{
Expand All @@ -887,6 +913,7 @@ func TestDecodeAuditLogWithCommandStartTime(t *testing.T) {
Type: pnet.ComQuery,
Payload: append([]byte{pnet.ComQuery.Byte()}, []byte("select \"[=]\"")...),
StmtType: "Select",
Line: 3,
Success: true,
},
},
Expand All @@ -902,6 +929,7 @@ func TestDecodeAuditLogWithCommandStartTime(t *testing.T) {
ConnID: 3695181836,
StartTs: time.Date(2025, 9, 14, 16, 16, 53, 718663917, time.FixedZone("", 8*3600+600)),
Payload: append([]byte{pnet.ComInitDB.Byte()}, []byte("b")...),
Line: 3,
Success: true,
},
{
Expand All @@ -910,6 +938,7 @@ func TestDecodeAuditLogWithCommandStartTime(t *testing.T) {
Type: pnet.ComQuery,
Payload: append([]byte{pnet.ComQuery.Byte()}, []byte("select \"[=]\"")...),
StmtType: "Select",
Line: 3,
Success: true,
},
},
Expand Down Expand Up @@ -965,6 +994,7 @@ func TestDecodeAuditLogInDirectedMode(t *testing.T) {
StartTs: time.Date(2025, 9, 18, 17, 48, 20, 613951140, time.FixedZone("", 8*3600+600)),
Payload: append([]byte{pnet.ComInitDB.Byte()}, []byte("test")...),
CapturedPsID: 0,
Line: 1,
Success: true,
},
{
Expand All @@ -974,6 +1004,7 @@ func TestDecodeAuditLogInDirectedMode(t *testing.T) {
CapturedPsID: 1,
Payload: append([]byte{pnet.ComStmtPrepare.Byte()}, []byte("SELECT c FROM sbtest1 WHERE id=?")...),
StmtType: "Select",
Line: 1,
Success: true,
},
{
Expand All @@ -983,6 +1014,7 @@ func TestDecodeAuditLogInDirectedMode(t *testing.T) {
CapturedPsID: 1,
Payload: append([]byte{pnet.ComStmtExecute.Byte()}, []byte{1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 8, 0, 0xe8, 0xaf, 0x07, 0, 0, 0, 0, 0}...),
StmtType: "Select",
Line: 1,
Success: true,
},
{
Expand All @@ -992,6 +1024,7 @@ func TestDecodeAuditLogInDirectedMode(t *testing.T) {
CapturedPsID: 1,
Payload: append([]byte{pnet.ComStmtClose.Byte()}, []byte{1, 0, 0, 0}...),
StmtType: "Select",
Line: 2,
Success: true,
},
},
Expand All @@ -1008,6 +1041,7 @@ func TestDecodeAuditLogInDirectedMode(t *testing.T) {
StartTs: time.Date(2025, 9, 18, 17, 48, 20, 613951140, time.FixedZone("", 8*3600+600)),
Payload: append([]byte{pnet.ComInitDB.Byte()}, []byte("test")...),
CapturedPsID: 0,
Line: 1,
Success: true,
},
{
Expand All @@ -1017,6 +1051,7 @@ func TestDecodeAuditLogInDirectedMode(t *testing.T) {
CapturedPsID: 1,
Payload: append([]byte{pnet.ComStmtPrepare.Byte()}, []byte("SELECT c FROM sbtest1 WHERE id=?")...),
StmtType: "Select",
Line: 1,
Success: true,
},
{
Expand All @@ -1026,6 +1061,7 @@ func TestDecodeAuditLogInDirectedMode(t *testing.T) {
CapturedPsID: 1,
Payload: append([]byte{pnet.ComStmtExecute.Byte()}, []byte{1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 8, 0, 0xe8, 0xaf, 0x07, 0, 0, 0, 0, 0}...),
StmtType: "Select",
Line: 1,
Success: true,
},
{
Expand All @@ -1035,6 +1071,7 @@ func TestDecodeAuditLogInDirectedMode(t *testing.T) {
CapturedPsID: 1,
Payload: append([]byte{pnet.ComStmtExecute.Byte()}, []byte{1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 8, 0, 0xf9, 0xe4, 0x01, 0, 0, 0, 0, 0}...),
StmtType: "Select",
Line: 2,
Success: true,
},
{
Expand All @@ -1044,6 +1081,7 @@ func TestDecodeAuditLogInDirectedMode(t *testing.T) {
CapturedPsID: 1,
Payload: append([]byte{pnet.ComStmtClose.Byte()}, []byte{1, 0, 0, 0}...),
StmtType: "Select",
Line: 3,
Success: true,
},
},
Expand All @@ -1060,6 +1098,7 @@ func TestDecodeAuditLogInDirectedMode(t *testing.T) {
StartTs: time.Date(2025, 9, 18, 17, 48, 20, 613951140, time.FixedZone("", 8*3600+600)),
Payload: append([]byte{pnet.ComInitDB.Byte()}, []byte("test")...),
CapturedPsID: 0,
Line: 1,
Success: true,
},
{
Expand All @@ -1069,6 +1108,7 @@ func TestDecodeAuditLogInDirectedMode(t *testing.T) {
CapturedPsID: 1,
Payload: append([]byte{pnet.ComStmtPrepare.Byte()}, []byte("SELECT c FROM sbtest1 WHERE id=?")...),
StmtType: "Select",
Line: 1,
Success: true,
},
{
Expand All @@ -1078,6 +1118,7 @@ func TestDecodeAuditLogInDirectedMode(t *testing.T) {
CapturedPsID: 1,
Payload: append([]byte{pnet.ComStmtExecute.Byte()}, []byte{1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 8, 0, 0xe8, 0xaf, 0x07, 0, 0, 0, 0, 0}...),
StmtType: "Select",
Line: 1,
Success: true,
},
{
Expand All @@ -1087,6 +1128,7 @@ func TestDecodeAuditLogInDirectedMode(t *testing.T) {
CapturedPsID: 1,
Payload: append([]byte{pnet.ComStmtClose.Byte()}, []byte{1, 0, 0, 0}...),
StmtType: "Select",
Line: 2,
Success: true,
},
{
Expand All @@ -1096,6 +1138,7 @@ func TestDecodeAuditLogInDirectedMode(t *testing.T) {
CapturedPsID: 1,
Payload: append([]byte{pnet.ComStmtPrepare.Byte()}, []byte("SELECT c FROM sbtest1 WHERE id=?")...),
StmtType: "Select",
Line: 3,
Success: true,
},
{
Expand All @@ -1105,6 +1148,7 @@ func TestDecodeAuditLogInDirectedMode(t *testing.T) {
CapturedPsID: 1,
Payload: append([]byte{pnet.ComStmtExecute.Byte()}, []byte{1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 8, 0, 0xf9, 0xe4, 0x01, 0, 0, 0, 0, 0}...),
StmtType: "Select",
Line: 3,
Success: true,
},
},
Expand Down Expand Up @@ -1144,6 +1188,7 @@ func TestDecodeAuditLogInNeverMode(t *testing.T) {
ConnID: 3807050215081378201,
StartTs: time.Date(2025, 9, 18, 17, 48, 20, 613951140, time.FixedZone("", 8*3600+600)),
Payload: append([]byte{pnet.ComInitDB.Byte()}, []byte("test")...),
Line: 1,
Success: true,
},
{
Expand All @@ -1153,6 +1198,7 @@ func TestDecodeAuditLogInNeverMode(t *testing.T) {
CapturedPsID: 1,
Payload: append([]byte{pnet.ComStmtPrepare.Byte()}, []byte("SELECT c FROM sbtest1 WHERE id=?")...),
StmtType: "Select",
Line: 1,
Success: true,
},
{
Expand All @@ -1162,6 +1208,7 @@ func TestDecodeAuditLogInNeverMode(t *testing.T) {
CapturedPsID: 1,
Payload: append([]byte{pnet.ComStmtExecute.Byte()}, []byte{1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 8, 0, 0xe8, 0xaf, 0x07, 0, 0, 0, 0, 0}...),
StmtType: "Select",
Line: 1,
Success: true,
},
{
Expand All @@ -1171,6 +1218,7 @@ func TestDecodeAuditLogInNeverMode(t *testing.T) {
CapturedPsID: 1,
Payload: append([]byte{pnet.ComStmtExecute.Byte()}, []byte{1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 8, 0, 0xe8, 0xaf, 0x07, 0, 0, 0, 0, 0}...),
StmtType: "Select",
Line: 2,
Success: true,
},
},
Expand Down
3 changes: 3 additions & 0 deletions pkg/sqlreplay/cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@ type Command struct {
StartTs time.Time
ConnID uint64
Type pnet.Command
// The place in the traffic file, used to report.
FileName string
Line int
// Logged only in audit log.
StmtType string
// Logged only in native log.
Expand Down
Loading