Skip to content

Commit

Permalink
session: annotate the previous statement to the error when transactio…
Browse files Browse the repository at this point in the history
…n commit failed (#12087)
  • Loading branch information
jackysp authored Sep 16, 2019
1 parent e2b1f7e commit 04292a0
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 9 deletions.
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

0 comments on commit 04292a0

Please sign in to comment.