Skip to content

Commit 7b52fee

Browse files
authored
Merge pull request pingcap#5 from ti2sky/add-replay
fix some bug
2 parents 48af694 + 901e5ca commit 7b52fee

File tree

3 files changed

+37
-21
lines changed

3 files changed

+37
-21
lines changed

replay_file

100644100755
Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,2 @@
1-
5 3 test begin
2-
3 4 test begin
3-
5 5 test insert into t values(5)
4-
3 6 test insert into t values(6)
5-
5 7 test commit
6-
5 8 test select * from t
7-
3 8 test commit
8-
5 8 test commit
1+
3 1 test use `test`
2+
3 2 test select * from t where a BETWEEN ? AND ? [arguments: (1, 55)]

session/session.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2866,8 +2866,7 @@ func logGeneralQuery(execStmt *executor.ExecStmt, s *session, isPrepared bool) {
28662866
cfg := config.GetGlobalConfig()
28672867
vars := s.GetSessionVars()
28682868
if !s.isInternal() && cfg.EnableReplaySQL.Load() {
2869-
fmt.Println("print sql")
2870-
go func(sql, id string) {
2869+
go func(sql string) {
28712870
//TODO: We need to add a client col also.
28722871
var builder strings.Builder
28732872
builder.WriteString(fmt.Sprintf("%v", vars.ConnectionID))
@@ -2876,11 +2875,16 @@ func logGeneralQuery(execStmt *executor.ExecStmt, s *session, isPrepared bool) {
28762875
ts := strconv.FormatInt(s.sessionVars.StartTime.Unix()-cfg.ReplayMetaTS, 10)
28772876
builder.WriteString(ts)
28782877
builder.WriteString(" ")
2879-
text := strings.ReplaceAll(sql, "\n", " ")
2878+
builder.WriteString(vars.CurrentDB)
2879+
text := executor.QueryReplacer.Replace(sql)
28802880
builder.WriteString(text)
2881+
if vars.PreparedParams != nil {
2882+
builder.WriteString(" ")
2883+
builder.WriteString(vars.PreparedParams.String())
2884+
}
28812885
builder.WriteString("\n")
28822886
logutil.PutRecordOrDrop(builder.String())
2883-
}(vars.SQL, vars.TxnCtx.ID.String())
2887+
}(vars.SQL)
28842888
}
28852889

28862890
if variable.ProcessGeneralLog.Load() && !vars.InRestrictedSQL {

util/replayutil/record_replayer.go

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,14 @@ import (
99
"strings"
1010
"time"
1111

12-
"github.com/pingcap/errors"
1312
"github.com/pingcap/log"
1413
"github.com/pingcap/tidb/kv"
1514
"github.com/pingcap/tidb/session"
1615
)
1716

1817
// RecordReplayer is used to replay sql
1918
var RecordReplayer *recordReplayer
19+
2020
// Sessions is a map
2121
var Sessions map[string]session.Session
2222

@@ -72,7 +72,6 @@ func (r *recordReplayer) start() {
7272
}
7373
ts, _ := strconv.ParseFloat(record[1], 10)
7474
if sleepTime := ts - time.Since(start).Seconds(); sleepTime > 0 {
75-
fmt.Printf("sleep time:%v\n", sleepTime)
7675
time.Sleep(time.Duration(sleepTime) * time.Second)
7776
}
7877
if s, exist := Sessions[record[0]]; !exist {
@@ -83,24 +82,43 @@ func (r *recordReplayer) start() {
8382
return
8483
}
8584
Sessions[record[0]] = se
86-
go replayExecuteSQL(record[3], se, record[0])
85+
go replayExecuteSQL(record[3], se)
8786
} else {
88-
go replayExecuteSQL(record[3], s, record[0])
87+
go replayExecuteSQL(record[3], s)
8988
}
9089
}
9190
}
9291

93-
func replayExecuteSQL(sql string, s session.Session, connection string) error {
92+
func replayExecuteSQL(sql string, s session.Session) error {
9493
ctx := context.Background()
94+
fmt.Println(sql)
95+
args := strings.Split(sql, "[arguments: (")
96+
fmt.Println(args)
97+
if len(args) > 1{
98+
argument := strings.Split(args[1][:len(args[1])-2], ", ")
99+
sql = helper(args[0], argument)
100+
}
101+
fmt.Println(sql)
95102
stmts, err := s.Parse(ctx, sql)
96103
if err != nil {
97104
return err
98105
}
99106
for _, stmt := range stmts {
100-
_, err := s.ExecuteStmt(ctx, stmt)
101-
if err != nil {
102-
return errors.Trace(err)
103-
}
107+
s.ExecuteStmt(ctx, stmt)
104108
}
105109
return nil
106110
}
111+
112+
func helper(sql string, args []string) string {
113+
newsql := ""
114+
i := 0
115+
for _, b := range []byte(sql) {
116+
if b == byte('?'){
117+
newsql += args[i]
118+
i++
119+
}else{
120+
newsql += string(b)
121+
}
122+
}
123+
return newsql
124+
}

0 commit comments

Comments
 (0)