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

session: add a global/session variable 'tidb_disable_txn_auto_retry' #6872

Merged
merged 3 commits into from
Jun 21, 2018
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
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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we return here.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, there are extra works to do below.

}
}
// 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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it could be true as default.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be false by default for now. Because many users rely on the auto-retry.

DefTiDBHashJoinConcurrency = 5
DefTiDBProjectionConcurrency = 4
DefTiDBOptimizerSelectivityLevel = 0
Expand Down