From f0fd22aaa21af5957276ac8432b8be56d08b196d Mon Sep 17 00:00:00 2001 From: tangenta Date: Tue, 22 Sep 2020 10:21:41 +0800 Subject: [PATCH] cherry pick #20123 to release-4.0 Signed-off-by: ti-srebot --- executor/adapter.go | 1 + executor/seqtest/seq_executor_test.go | 32 +++++++++++++++++++++++++++ 2 files changed, 33 insertions(+) diff --git a/executor/adapter.go b/executor/adapter.go index 92c857b79f111..226527b5373ab 100644 --- a/executor/adapter.go +++ b/executor/adapter.go @@ -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 diff --git a/executor/seqtest/seq_executor_test.go b/executor/seqtest/seq_executor_test.go index cf1883aa4a323..0d36daf7d95bd 100644 --- a/executor/seqtest/seq_executor_test.go +++ b/executor/seqtest/seq_executor_test.go @@ -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)