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) (#12747)
  • Loading branch information
jackysp authored and sre-bot committed Oct 16, 2019
1 parent c1c80d6 commit a55ead0
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 8 deletions.
5 changes: 3 additions & 2 deletions executor/adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,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()
return errors.Trace(err)
}
Expand Down Expand Up @@ -443,7 +444,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 @@ -1461,12 +1461,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 @@ -156,7 +156,8 @@ func Compile(ctx context.Context, sctx sessionctx.Context, stmtNode ast.StmtNode
return stmt, errors.Trace(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.Logger(context.Background()).Info("rollbackTxn for ddl/autocommit error.")
Expand All @@ -166,7 +167,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, sessVars)
Expand Down Expand Up @@ -197,7 +204,7 @@ func runStmt(ctx context.Context, sctx sessionctx.Context, s sqlexec.Statement)
// then it could include the transaction commit time.
if rs == nil {
s.(*executor.ExecStmt).LogSlowQuery(origTxnCtx.StartTS, err == nil)
sessVars.PrevStmt = s.OriginText()
sessVars.PrevStmt = executor.FormatSQL(s.OriginText(), sessVars)
}
}()

Expand Down Expand Up @@ -225,7 +232,7 @@ func runStmt(ctx context.Context, sctx sessionctx.Context, s sqlexec.Statement)
}
}

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
// statement never need a Txn(), such as:
Expand Down

0 comments on commit a55ead0

Please sign in to comment.