Skip to content

Commit

Permalink
executor: make the format of the DB field in slow ... (#18389) (#18512)
Browse files Browse the repository at this point in the history
  • Loading branch information
ti-srebot authored Sep 4, 2020
1 parent a5922c2 commit 709b48a
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 8 deletions.
1 change: 1 addition & 0 deletions executor/simple.go
Original file line number Diff line number Diff line change
Expand Up @@ -523,6 +523,7 @@ func (e *SimpleExec) executeUse(s *ast.UseStmt) error {
if !exists {
return infoschema.ErrDatabaseNotExists.GenWithStackByArgs(dbname)
}
e.ctx.GetSessionVars().CurrentDBChanged = dbname.O != e.ctx.GetSessionVars().CurrentDB
e.ctx.GetSessionVars().CurrentDB = dbname.O
// character_set_database is the character set used by the default database.
// The server sets this variable whenever the default database changes.
Expand Down
7 changes: 7 additions & 0 deletions executor/slow_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,13 @@ func (e *slowQueryRetriever) parseSlowLog(ctx sessionctx.Context, reader *bufio.
}
}
} else if strings.HasSuffix(line, variable.SlowLogSQLSuffixStr) {
if strings.HasPrefix(line, "use") {
// `use DB` statements in the slow log is used to keep it be compatible with MySQL,
// since we already get the current DB from the `# DB` field, we can ignore it here,
// please see https://github.com/pingcap/tidb/issues/17846 for more details.
continue
}

// Get the sql string, and mark the start flag to false.
_, err = st.setFieldValue(tz, variable.SlowLogQuerySQLStr, string(hack.Slice(line)), e.fileLine, e.checker)
if err != nil {
Expand Down
1 change: 1 addition & 0 deletions executor/slow_query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ func (s *testExecSuite) TestParseSlowLogFile(c *C) {
# Succ: false
# Plan_digest: 60e9378c746d9a2be1c791047e008967cf252eb6de9167ad3aa6098fa2d523f4
# Prev_stmt: update t set i = 1;
use test;
select * from t;`
reader := bufio.NewReader(bytes.NewBufferString(slowLogStr))
loc, err := time.LoadLocation("Asia/Shanghai")
Expand Down
9 changes: 9 additions & 0 deletions sessionctx/variable/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,10 @@ type SessionVars struct {
// CurrentDB is the default database of this session.
CurrentDB string

// CurrentDBChanged indicates if the CurrentDB has been updated, and if it is we should print it into
// the slow log to make it be compatible with MySQL, https://github.com/pingcap/tidb/issues/17846.
CurrentDBChanged bool

// StrictSQLMode indicates if the session is in strict mode.
StrictSQLMode bool

Expand Down Expand Up @@ -1786,6 +1790,11 @@ func (s *SessionVars) SlowLogFormat(logItems *SlowQueryLogItems) string {
writeSlowLogItem(&buf, SlowLogPrevStmt, logItems.PrevStmt)
}

if s.CurrentDBChanged {
buf.WriteString(fmt.Sprintf("use %s;\n", s.CurrentDB))
s.CurrentDBChanged = false
}

buf.WriteString(logItems.SQL)
if len(logItems.SQL) == 0 || logItems.SQL[len(logItems.SQL)-1] != ';' {
buf.WriteString(";")
Expand Down
21 changes: 13 additions & 8 deletions sessionctx/variable/session_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ func (*testSessionSuite) TestSlowLogFormat(c *C) {

var memMax int64 = 2333
var diskMax int64 = 6666
resultString := `# Txn_start_ts: 406649736972468225
resultFields := `# Txn_start_ts: 406649736972468225
# User@Host: root[root] @ 192.168.0.1 [192.168.0.1]
# Conn_ID: 1
# Query_time: 1
Expand All @@ -186,7 +186,7 @@ func (*testSessionSuite) TestSlowLogFormat(c *C) {
# DB: test
# Index_names: [t1:a,t2:b]
# Is_internal: true
# Digest: 42a1c8aae6f133e934d4bf0147491709a8812ea05ff8819ec522780fe657b772
# Digest: f94c76d7fa8f60e438118752bfbfb71fe9e1934888ac415ddd8625b121af124c
# Stats: t1:pseudo
# Num_cop_tasks: 10
# Cop_proc_avg: 1 Cop_proc_p90: 2 Cop_proc_max: 3 Cop_proc_addr: 10.6.131.78
Expand All @@ -203,11 +203,10 @@ func (*testSessionSuite) TestSlowLogFormat(c *C) {
# PD_total: 11
# Backoff_total: 12
# Write_sql_response_total: 1
# Succ: true
select * from t;`
sql := "select * from t"
# Succ: true`
sql := "select * from t;"
_, digest := parser.NormalizeDigest(sql)
logString := seVar.SlowLogFormat(&variable.SlowQueryLogItems{
logItems := &variable.SlowQueryLogItems{
TxnTS: txnTS,
SQL: sql,
Digest: digest,
Expand All @@ -233,8 +232,14 @@ select * from t;`
DurationPreprocessSubQuery: 2,
PreprocessSubQueries: 2,
},
})
c.Assert(logString, Equals, resultString)
}
logString := seVar.SlowLogFormat(logItems)
c.Assert(logString, Equals, resultFields+"\n"+sql)

seVar.CurrentDBChanged = true
logString = seVar.SlowLogFormat(logItems)
c.Assert(logString, Equals, resultFields+"\n"+"use test;\n"+sql)
c.Assert(seVar.CurrentDBChanged, IsFalse)
}

func (*testSessionSuite) TestIsolationRead(c *C) {
Expand Down

0 comments on commit 709b48a

Please sign in to comment.