Skip to content

Commit

Permalink
session: add a global/session variable 'tidb_disable_txn_auto_retry' (p…
Browse files Browse the repository at this point in the history
  • Loading branch information
coocood authored and tiancaiamao committed Jun 21, 2018
1 parent 5a6e739 commit 8bec188
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 2 deletions.
12 changes: 11 additions & 1 deletion session/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,15 @@ func (s *session) doCommitWithRetry(ctx context.Context) error {
err := s.doCommit(ctx)
if err != nil {
commitRetryLimit := s.sessionVars.RetryLimit
if s.sessionVars.DisableTxnAutoRetry && !s.sessionVars.InRestrictedSQL {
// Do not retry non-autocommit transactions.
// For autocommit single statement transactions, the history count is always 1.
// For explicit transactions, the statement count is more than 1.
history := GetHistory(s)
if history.Count() > 1 {
commitRetryLimit = 0
}
}
// Don't retry in BatchInsert mode. As a counter-example, insert into t1 select * from t2,
// BatchInsert already commit the first batch 1000 rows, then it commit 1000-2000 and retry the statement,
// Finally t1 will have more data than t2, with no errors return to user!
Expand Down Expand Up @@ -1295,7 +1304,8 @@ const loadCommonGlobalVarsSQL = "select HIGH_PRIORITY * from mysql.global_variab
variable.TiDBOptInSubqUnFolding + quoteCommaQuote +
variable.TiDBDistSQLScanConcurrency + quoteCommaQuote +
variable.TiDBMaxChunkSize + quoteCommaQuote +
variable.TiDBRetryLimit + "')"
variable.TiDBRetryLimit + quoteCommaQuote +
variable.TiDBDisableTxnAutoRetry + "')"

// loadCommonGlobalVariablesIfNeeded loads and applies commonly used global variables for the session.
func (s *session) loadCommonGlobalVariablesIfNeeded() error {
Expand Down
37 changes: 37 additions & 0 deletions session/session_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2056,3 +2056,40 @@ func (s *testSessionSuite) TestCommitRetryCount(c *C) {
_, err := tk1.Se.Execute(context.Background(), "commit")
c.Assert(err, NotNil)
}

func (s *testSessionSuite) TestDisableTxnAutoRetry(c *C) {
tk1 := testkit.NewTestKitWithInit(c, s.store)
tk2 := testkit.NewTestKitWithInit(c, s.store)
tk1.MustExec("create table no_retry (id int)")
tk1.MustExec("insert into no_retry values (1)")
tk1.MustExec("set @@tidb_disable_txn_auto_retry = 1")

tk1.MustExec("begin")
tk1.MustExec("update no_retry set id = 2")

tk2.MustExec("begin")
tk2.MustExec("update no_retry set id = 3")
tk2.MustExec("commit")

// No auto retry because tidb_disable_txn_auto_retry is set to 1.
_, err := tk1.Se.Execute(context.Background(), "commit")
c.Assert(err, NotNil)

// session 1 starts a transaction early.
// execute a select statement to clear retry history.
tk1.MustExec("select 1")
tk1.Se.NewTxn()
// session 2 update the value.
tk2.MustExec("update no_retry set id = 4")
// Autocommit update will retry, so it would not fail.
tk1.MustExec("update no_retry set id = 5")

// RestrictedSQL should retry.
tk1.Se.GetSessionVars().InRestrictedSQL = true
tk1.MustExec("begin")

tk2.MustExec("update no_retry set id = 6")

tk1.MustExec("update no_retry set id = 7")
tk1.MustExec("commit")
}
6 changes: 5 additions & 1 deletion sessionctx/variable/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,8 @@ type SessionVars struct {
Concurrency
MemQuota
BatchSize
RetryLimit int64
RetryLimit int64
DisableTxnAutoRetry bool
// UsersLock is a lock for user defined variables.
UsersLock sync.RWMutex
// Users are user defined variables.
Expand Down Expand Up @@ -303,6 +304,7 @@ func NewSessionVars() *SessionVars {
AllowAggPushDown: false,
OptimizerSelectivityLevel: DefTiDBOptimizerSelectivityLevel,
RetryLimit: DefTiDBRetryLimit,
DisableTxnAutoRetry: DefTiDBDisableTxnAutoRetry,
}
vars.Concurrency = Concurrency{
IndexLookupConcurrency: DefIndexLookupConcurrency,
Expand Down Expand Up @@ -535,6 +537,8 @@ func (s *SessionVars) SetSystemVar(name string, val string) error {
atomic.StoreUint32(&ProcessGeneralLog, uint32(tidbOptPositiveInt32(val, DefTiDBGeneralLog)))
case TiDBRetryLimit:
s.RetryLimit = tidbOptInt64(val, DefTiDBRetryLimit)
case TiDBDisableTxnAutoRetry:
s.DisableTxnAutoRetry = TiDBOptOn(val)
case TiDBEnableStreaming:
s.EnableStreaming = TiDBOptOn(val)
case TiDBOptimizerSelectivityLevel:
Expand Down
1 change: 1 addition & 0 deletions sessionctx/variable/sysvar.go
Original file line number Diff line number Diff line change
Expand Up @@ -642,6 +642,7 @@ var defaultSysVars = []*SysVar{
{ScopeGlobal | ScopeSession, TiDBHashAggFinalConcurrency, strconv.Itoa(DefTiDBHashAggFinalConcurrency)},
{ScopeGlobal | ScopeSession, TiDBBackoffLockFast, strconv.Itoa(kv.DefBackoffLockFast)},
{ScopeGlobal | ScopeSession, TiDBRetryLimit, strconv.Itoa(DefTiDBRetryLimit)},
{ScopeGlobal | ScopeSession, TiDBDisableTxnAutoRetry, boolToIntStr(DefTiDBDisableTxnAutoRetry)},
{ScopeSession, TiDBOptimizerSelectivityLevel, strconv.Itoa(DefTiDBOptimizerSelectivityLevel)},
/* The following variable is defined as session scope but is actually server scope. */
{ScopeSession, TiDBGeneralLog, strconv.Itoa(DefTiDBGeneralLog)},
Expand Down
4 changes: 4 additions & 0 deletions sessionctx/variable/tidb_vars.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,9 @@ const (
// tidb_retry_limit is the maximun number of retries when committing a transaction.
TiDBRetryLimit = "tidb_retry_limit"

// tidb_disable_txn_auto_retry disables transaction auto retry.
TiDBDisableTxnAutoRetry = "tidb_disable_txn_auto_retry"

// tidb_enable_streaming enables TiDB to use streaming API for coprocessor requests.
TiDBEnableStreaming = "tidb_enable_streaming"

Expand Down Expand Up @@ -198,6 +201,7 @@ const (
DefTiDBMemQuotaNestedLoopApply = 32 << 30 // 32GB.
DefTiDBGeneralLog = 0
DefTiDBRetryLimit = 10
DefTiDBDisableTxnAutoRetry = false
DefTiDBHashJoinConcurrency = 5
DefTiDBProjectionConcurrency = 4
DefTiDBOptimizerSelectivityLevel = 0
Expand Down

0 comments on commit 8bec188

Please sign in to comment.