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

executor: fix unexpected 'invalid auto-id' error in pessimistic txn retry (#20123) #20134

Merged
merged 2 commits into from
Sep 23, 2020
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
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