Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

session: annotate the previous statement to the error when transaction commit failed #12087

Merged
merged 17 commits into from
Sep 16, 2019
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: 1 addition & 1 deletion ddl/db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3232,7 +3232,7 @@ func (s *testDBSuite2) TestLockTables(c *C) {
tk2.MustExec("lock tables t1 write")
_, err = tk.Exec("commit")
c.Assert(err, NotNil)
c.Assert(err.Error(), Equals, "[domain:2]Information schema is changed. [try again later]")
c.Assert(err.Error(), Equals, "previous statement: insert into t1 set a=1: [domain:2]Information schema is changed. [try again later]")

// Test lock table by other session in transaction and commit with retry.
tk.MustExec("unlock tables")
Expand Down
5 changes: 3 additions & 2 deletions executor/adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,8 @@ func (a *recordSet) NewChunk() *chunk.Chunk {
func (a *recordSet) Close() error {
err := a.executor.Close()
a.stmt.LogSlowQuery(a.txnStartTS, a.lastErr == nil)
a.stmt.Ctx.GetSessionVars().PrevStmt = a.stmt.OriginText()
sessVars := a.stmt.Ctx.GetSessionVars()
sessVars.PrevStmt = FormatSQL(a.stmt.OriginText(), sessVars)
a.stmt.logAudit()
a.stmt.SummaryStmt()
return err
Expand Down Expand Up @@ -750,7 +751,7 @@ func (a *ExecStmt) LogSlowQuery(txnTS uint64, succ bool) {
Succ: succ,
}
if _, ok := a.StmtNode.(*ast.CommitStmt); ok {
slowItems.PrevStmt = FormatSQL(sessVars.PrevStmt, sessVars)
slowItems.PrevStmt = sessVars.PrevStmt
}
if costTime < threshold {
logutil.SlowQueryLogger.Debug(sessVars.SlowLogFormat(slowItems))
Expand Down
4 changes: 2 additions & 2 deletions session/session_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1590,12 +1590,12 @@ func (s *testSessionSuite) TestUnique(c *C) {
c.Assert(err, NotNil)
// Check error type and error message
c.Assert(terror.ErrorEqual(err, kv.ErrKeyExists), IsTrue, Commentf("err %v", err))
c.Assert(err.Error(), Equals, "[kv:1062]Duplicate entry '1' for key 'PRIMARY'")
c.Assert(err.Error(), Equals, "previous statement: insert into test(id, val) values(1, 1);: [kv:1062]Duplicate entry '1' for key 'PRIMARY'")

_, err = tk1.Exec("commit")
c.Assert(err, NotNil)
c.Assert(terror.ErrorEqual(err, kv.ErrKeyExists), IsTrue, Commentf("err %v", err))
c.Assert(err.Error(), Equals, "[kv:1062]Duplicate entry '2' for key 'val'")
c.Assert(err.Error(), Equals, "previous statement: insert into test(id, val) values(2, 2);: [kv:1062]Duplicate entry '2' for key 'val'")

// Test for https://github.com/pingcap/tidb/issues/463
tk.MustExec("drop table test;")
Expand Down
15 changes: 11 additions & 4 deletions session/tidb.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,8 @@ func Compile(ctx context.Context, sctx sessionctx.Context, stmtNode ast.StmtNode
return stmt, err
}

func finishStmt(ctx context.Context, sctx sessionctx.Context, se *session, sessVars *variable.SessionVars, meetsErr error) error {
func finishStmt(ctx context.Context, sctx sessionctx.Context, se *session, sessVars *variable.SessionVars,
meetsErr error, sql sqlexec.Statement) error {
if meetsErr != nil {
if !sessVars.InTxn() {
logutil.BgLogger().Info("rollbackTxn for ddl/autocommit failed")
Expand All @@ -173,7 +174,13 @@ func finishStmt(ctx context.Context, sctx sessionctx.Context, se *session, sessV
}

if !sessVars.InTxn() {
return se.CommitTxn(ctx)
if err := se.CommitTxn(ctx); err != nil {
if _, ok := sql.(*executor.ExecStmt).StmtNode.(*ast.CommitStmt); ok {
err = errors.Annotatef(err, "previous statement: %s", se.GetSessionVars().PrevStmt)
}
return err
}
return nil
}

return checkStmtLimit(ctx, sctx, se)
Expand Down Expand Up @@ -219,7 +226,7 @@ func runStmt(ctx context.Context, sctx sessionctx.Context, s sqlexec.Statement)
if rs == nil {
s.(*executor.ExecStmt).LogSlowQuery(origTxnCtx.StartTS, err == nil)
s.(*executor.ExecStmt).SummaryStmt()
sessVars.PrevStmt = s.OriginText()
sessVars.PrevStmt = executor.FormatSQL(s.OriginText(), sessVars)
}
}()

Expand Down Expand Up @@ -248,7 +255,7 @@ func runStmt(ctx context.Context, sctx sessionctx.Context, s sqlexec.Statement)
logutil.BgLogger().Error("get txn failed", zap.Error(err1))
}
}
err = finishStmt(ctx, sctx, se, sessVars, err)
err = finishStmt(ctx, sctx, se, sessVars, err, s)

if se.txn.pending() {
// After run statement finish, txn state is still pending means the
Expand Down