Skip to content

Commit

Permalink
*: add an option to enable pessimistic mode for auto-commit transacti…
Browse files Browse the repository at this point in the history
…ons (#32056)

close #31135
  • Loading branch information
jackysp committed Feb 15, 2022
1 parent 2b9093f commit eabd78c
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 4 deletions.
3 changes: 3 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -569,6 +569,8 @@ type PessimisticTxn struct {
DeadlockHistoryCapacity uint `toml:"deadlock-history-capacity" json:"deadlock-history-capacity"`
// Whether retryable deadlocks (in-statement deadlocks) are collected to the information_schema.deadlocks table.
DeadlockHistoryCollectRetryable bool `toml:"deadlock-history-collect-retryable" json:"deadlock-history-collect-retryable"`
// PessimisticAutoCommit represents if true it means the auto-commit transactions will be in pessimistic mode.
PessimisticAutoCommit AtomicBool `toml:"pessimistic-auto-commit" json:"pessimistic-auto-commit"`
}

// DefaultPessimisticTxn returns the default configuration for PessimisticTxn
Expand All @@ -577,6 +579,7 @@ func DefaultPessimisticTxn() PessimisticTxn {
MaxRetryCount: 256,
DeadlockHistoryCapacity: 10,
DeadlockHistoryCollectRetryable: false,
PessimisticAutoCommit: *NewAtomicBool(false),
}
}

Expand Down
3 changes: 3 additions & 0 deletions config/config.toml.example
Original file line number Diff line number Diff line change
Expand Up @@ -460,6 +460,9 @@ deadlock-history-capacity = 10
# Whether retryable deadlocks (in-statement deadlocks) are collected to the information_schema.deadlocks table.
deadlock-history-collect-retryable = false

# If true it means the auto-commit transactions will be in pessimistic mode.
pessimistic-auto-commit = false

# experimental section controls the features that are still experimental: their semantics,
# interfaces are subject to change, using these features in the production environment is not recommended.
[experimental]
Expand Down
4 changes: 3 additions & 1 deletion config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,7 @@ spilled-file-encryption-method = "plaintext"
[pessimistic-txn]
deadlock-history-capacity = 123
deadlock-history-collect-retryable = true
pessimistic-auto-commit = true
[top-sql]
receiver-address = "127.0.0.1:10100"
[status]
Expand Down Expand Up @@ -308,6 +309,7 @@ grpc-max-send-msg-size = 40960
require.Equal(t, uint64(30), conf.StoresRefreshInterval)
require.Equal(t, uint(123), conf.PessimisticTxn.DeadlockHistoryCapacity)
require.True(t, conf.PessimisticTxn.DeadlockHistoryCollectRetryable)
require.True(t, conf.PessimisticTxn.PessimisticAutoCommit.Load())
require.Equal(t, "127.0.0.1:10100", conf.TopSQL.ReceiverAddress)
require.True(t, conf.Experimental.AllowsExpressionIndex)
require.Equal(t, uint(20), conf.Status.GRPCKeepAliveTime)
Expand Down Expand Up @@ -643,7 +645,7 @@ func TestSecurityValid(t *testing.T) {

func TestTcpNoDelay(t *testing.T) {
c1 := NewConfig()
//check default value
// check default value
require.True(t, c1.Performance.TCPNoDelay)
}

Expand Down
30 changes: 28 additions & 2 deletions session/pessimistic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2360,7 +2360,7 @@ func (s *testPessimisticSuite) TestIssue21498(c *C) {
tk.MustExec("set tidb_enable_amend_pessimistic_txn = 1")

for _, partition := range []bool{false, true} {
//RC test
// RC test
tk.MustExec("drop table if exists t, t1")
createTable := "create table t (id int primary key, v int, index iv (v))"
if partition {
Expand Down Expand Up @@ -2529,7 +2529,7 @@ func (s *testPessimisticSuite) TestPlanCacheSchemaChange(c *C) {
tk.MustExec("set tidb_enable_amend_pessimistic_txn = 1")
tk2.MustExec("set tidb_enable_amend_pessimistic_txn = 1")

//generate plan cache
// generate plan cache
tk.MustExec("prepare update_stmt from 'update t set vv = vv + 1 where v = ?'")
tk.MustExec("set @v = 1")
tk.MustExec("execute update_stmt using @v")
Expand Down Expand Up @@ -2836,3 +2836,29 @@ func (s *testPessimisticSuite) TestAmendForColumnChange(c *C) {

tk2.MustExec("drop database test_db")
}

func (s *testPessimisticSuite) TestPessimisticAutoCommitTxn(c *C) {
tk := testkit.NewTestKitWithInit(c, s.store)
tk.MustExec("set tidb_txn_mode = 'pessimistic'")
tk.MustExec("drop database if exists test_db")
tk.MustExec("create database test_db")
tk.MustExec("use test_db")
tk.MustExec("drop table if exists t")
tk.MustExec("create table t (i int)")
tk.MustExec("insert into t values (1)")
tk.MustExec("set autocommit = on")

rows := tk.MustQuery("explain update t set i = -i").Rows()
explain := fmt.Sprintf("%v", rows[1])
c.Assert(explain, Not(Matches), ".*SelectLock.*")

originCfg := config.GetGlobalConfig()
defer config.StoreGlobalConfig(originCfg)
newCfg := *originCfg
newCfg.PessimisticTxn.PessimisticAutoCommit.Store(true)
config.StoreGlobalConfig(&newCfg)

rows = tk.MustQuery("explain update t set i = -i").Rows()
explain = fmt.Sprintf("%v", rows[1])
c.Assert(explain, Matches, ".*SelectLock.*")
}
3 changes: 2 additions & 1 deletion session/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -3085,7 +3085,8 @@ func (s *session) PrepareTxnCtx(ctx context.Context) error {
ShardStep: int(s.sessionVars.ShardAllocateStep),
TxnScope: s.GetSessionVars().CheckAndGetTxnScope(),
}
if !s.sessionVars.IsAutocommit() || s.sessionVars.RetryInfo.Retrying {
if !s.sessionVars.IsAutocommit() || s.sessionVars.RetryInfo.Retrying ||
config.GetGlobalConfig().PessimisticTxn.PessimisticAutoCommit.Load() {
if s.sessionVars.TxnMode == ast.Pessimistic {
s.sessionVars.TxnCtx.IsPessimistic = true
}
Expand Down

0 comments on commit eabd78c

Please sign in to comment.