Skip to content

Commit

Permalink
executor: fix unexpected 'invalid auto-id' error in pessimistic txn r…
Browse files Browse the repository at this point in the history
…etry (#20123) (#20134)

Signed-off-by: ti-srebot <ti-srebot@pingcap.com>
  • Loading branch information
ti-srebot authored Sep 23, 2020
1 parent 476c992 commit d1a152f
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
1 change: 1 addition & 0 deletions executor/adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -646,6 +646,7 @@ func (a *ExecStmt) handlePessimisticLockError(ctx context.Context, err error) (E
// Rollback the statement change before retry it.
a.Ctx.StmtRollback()
a.Ctx.GetSessionVars().StmtCtx.ResetForRetry()
a.Ctx.GetSessionVars().RetryInfo.ResetOffset()

if err = e.Open(ctx); err != nil {
return nil, err
Expand Down
32 changes: 32 additions & 0 deletions executor/seqtest/seq_executor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1283,6 +1283,38 @@ func (s *seqTestSuite) TestAutoIncIDInRetry(c *C) {
tk.MustQuery(`select * from t`).Check(testkit.Rows("1", "2", "3", "4", "5"))
}

func (s *seqTestSuite) TestPessimisticConflictRetryAutoID(c *C) {
tk := testkit.NewTestKitWithInit(c, s.store)
tk.MustExec("drop table if exists t;")
tk.MustExec("create table t (id int not null auto_increment unique key, idx int unique key, c int);")
concurrency := 2
var wg sync.WaitGroup
var err []error
wg.Add(concurrency)
err = make([]error, concurrency)
for i := 0; i < concurrency; i++ {
tk := testkit.NewTestKitWithInit(c, s.store)
tk.MustExec("set tidb_txn_mode = 'pessimistic'")
tk.MustExec("set autocommit = 1")
go func(idx int) {
for i := 0; i < 10; i++ {
sql := fmt.Sprintf("insert into t(idx, c) values (1, %[1]d) on duplicate key update c = %[1]d", i)
_, e := tk.Exec(sql)
if e != nil {
err[idx] = e
wg.Done()
return
}
}
wg.Done()
}(i)
}
wg.Wait()
for _, e := range err {
c.Assert(e, IsNil)
}
}

func (s *seqTestSuite) TestAutoRandIDRetry(c *C) {
tk := testkit.NewTestKitWithInit(c, s.store)

Expand Down

0 comments on commit d1a152f

Please sign in to comment.