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

txn: fix retry with non autocommit #22875

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
9 changes: 9 additions & 0 deletions session/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -670,6 +670,15 @@ func (s *session) retry(ctx context.Context, maxCnt uint) (err error) {
s.sessionVars.TxnCtx.IsPessimistic = true
}
}
// Complete the missing settings using `NewTxn` compared with lazy activation `txn.Txn(true)`.
// More refactors are needed in the future to make the inner logic of transaction activation
// interfaces the same.
if s.sessionVars.TxnCtx.IsPessimistic {
jackysp marked this conversation as resolved.
Show resolved Hide resolved
s.txn.SetOption(kv.Pessimistic, true)
}
if !s.sessionVars.IsAutocommit() {
s.sessionVars.SetStatusFlag(mysql.ServerStatusInTrans, true)
jackysp marked this conversation as resolved.
Show resolved Hide resolved
}
} else {
s.PrepareTxnCtx(ctx)
}
Expand Down
61 changes: 61 additions & 0 deletions session/session_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3486,3 +3486,64 @@ func (s *testSessionSerialSuite) TestProcessInfoIssue22068(c *C) {
c.Assert(pi.Plan, IsNil)
wg.Wait()
}

func (s *testSessionSuite2) TestNonAutocommitTxnRetry(c *C) {
tk1 := testkit.NewTestKitWithInit(c, s.store)
tk1.MustExec("drop table if exists t")
tk1.MustExec("create table t(pk varchar(50), c1 varchar(50), c2 varchar(50), key k1(c1, c2), primary key(pk))")
tk1.MustExec("insert into t values ('1', '10', '100')")

tk1.MustExec("set tidb_disable_txn_auto_retry = 0")
tk1.MustExec("set autocommit = 0")
tk1.MustExec("set tidb_txn_mode = ''")
tk2 := testkit.NewTestKitWithInit(c, s.store)
tk1.MustExec("update t set c1 = '11' where pk = '1'")
tk1.MustExec("update t set c2 = '101' where pk = '1'")
tk2.MustExec("begin optimistic")
tk2.MustQuery("select * from t for update").Check(testkit.Rows("1 10 100"))
tk2.MustExec("commit")
tk1.MustExec("commit")
tk2.MustExec("admin check table t")
tk2.MustQuery("select * from t use index(k1)").Check(testkit.Rows("1 11 101"))
tk2.MustQuery("select * from t where pk = '1'").Check(testkit.Rows("1 11 101"))
}

func (s *testSessionSuite2) TestAutocommitTxnRetry(c *C) {
tk1 := testkit.NewTestKitWithInit(c, s.store)
tk1.MustExec("drop table if exists t")
tk1.MustExec("create table t(pk varchar(50), c1 varchar(50), c2 varchar(50), key k1(c1, c2), primary key(pk))")
tk1.MustExec("insert into t values ('1', '10', '100')")

tk1.MustExec("set tidb_disable_txn_auto_retry = 0")
tk1.MustExec("set autocommit = 0")
tk1.MustExec("begin optimistic")
tk2 := testkit.NewTestKitWithInit(c, s.store)
tk1.MustExec("update t set c1 = '11' where pk = '1'")
tk1.MustExec("update t set c2 = '101' where pk = '1'")
tk2.MustExec("begin optimistic")
tk2.MustQuery("select * from t for update").Check(testkit.Rows("1 10 100"))
tk2.MustExec("commit")
tk1.MustExec("commit")
tk2.MustExec("admin check table t")
tk2.MustQuery("select * from t use index(k1)").Check(testkit.Rows("1 11 101"))
tk2.MustQuery("select * from t where pk = '1'").Check(testkit.Rows("1 11 101"))
}

func (s *testSessionSuite2) TestRetryWithSet(c *C) {
tk1 := testkit.NewTestKitWithInit(c, s.store)
tk1.MustExec("drop table if exists t")
tk1.MustExec("create table t(pk varchar(50), c1 bigint, c2 varchar(50), key k1(c1, c2), primary key(pk))")
tk1.MustExec("insert into t values ('1', '10', '100')")

tk1.MustExec("set tidb_disable_txn_auto_retry = 0")
tk1.MustExec("set autocommit = 0")
tk1.MustQuery("select * from t").Check(testkit.Rows("1 10 100"))
tk1.MustExec("set @a=@@tidb_current_ts")
tk1.MustExec("update t set c1 = @a where pk = '1'")
tk2 := testkit.NewTestKitWithInit(c, s.store)
tk2.MustExec("begin optimistic")
tk2.MustQuery("select * from t for update").Check(testkit.Rows("1 10 100"))
tk2.MustExec("commit")
tk1.MustExec("commit")
tk1.MustQuery("select c1 > 0 from t").Check(testkit.Rows("1"))
}