Skip to content

Commit

Permalink
txn: tidb_constraint_check_in_place_pessimistic should not be global …
Browse files Browse the repository at this point in the history
…scoped (#38767)

close #38766
  • Loading branch information
ekexium authored Nov 7, 2022
1 parent 490e4c4 commit 21d6190
Show file tree
Hide file tree
Showing 8 changed files with 35 additions and 22 deletions.
13 changes: 8 additions & 5 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -751,6 +751,8 @@ type PessimisticTxn struct {
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"`
// ConstraintCheckInPlacePessimistic is the default value for the session variable `tidb_constraint_check_in_place_pessimistic`
ConstraintCheckInPlacePessimistic bool `toml:"constraint-check-in-place-pessimistic" json:"constraint-check-in-place-pessimistic"`
}

// TrxSummary is the config for transaction summary collecting.
Expand All @@ -772,10 +774,11 @@ func (config *TrxSummary) Valid() error {
// DefaultPessimisticTxn returns the default configuration for PessimisticTxn
func DefaultPessimisticTxn() PessimisticTxn {
return PessimisticTxn{
MaxRetryCount: 256,
DeadlockHistoryCapacity: 10,
DeadlockHistoryCollectRetryable: false,
PessimisticAutoCommit: *NewAtomicBool(false),
MaxRetryCount: 256,
DeadlockHistoryCapacity: 10,
DeadlockHistoryCollectRetryable: false,
PessimisticAutoCommit: *NewAtomicBool(false),
ConstraintCheckInPlacePessimistic: true,
}
}

Expand Down Expand Up @@ -1040,7 +1043,7 @@ var removedConfig = map[string]struct{}{
"log.query-log-max-len": {},
"performance.committer-concurrency": {},
"experimental.enable-global-kill": {},
"performance.run-auto-analyze": {}, //use tidb_enable_auto_analyze
"performance.run-auto-analyze": {}, // use tidb_enable_auto_analyze
// use tidb_enable_prepared_plan_cache, tidb_prepared_plan_cache_size and tidb_prepared_plan_cache_memory_guard_ratio
"prepared-plan-cache.enabled": {},
"prepared-plan-cache.capacity": {},
Expand Down
2 changes: 1 addition & 1 deletion metrics/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ var (
Namespace: "tidb",
Subsystem: "session",
Name: "lazy_pessimistic_unique_check_set_count",
Help: "Counter of setting tidb_constraint_check_in_place to false",
Help: "Counter of setting tidb_constraint_check_in_place to false, note that it doesn't count the default value set by tidb config",
},
)
)
Expand Down
8 changes: 8 additions & 0 deletions session/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -1352,6 +1352,10 @@ func createSessionFunc(store kv.Storage) pools.Factory {
if err != nil {
return nil, errors.Trace(err)
}
err = se.sessionVars.SetSystemVar(variable.TiDBConstraintCheckInPlacePessimistic, variable.On)
if err != nil {
return nil, errors.Trace(err)
}
se.sessionVars.CommonGlobalLoaded = true
se.sessionVars.InRestrictedSQL = true
// Internal session uses default format to prevent memory leak problem.
Expand All @@ -1378,6 +1382,10 @@ func createSessionWithDomainFunc(store kv.Storage) func(*domain.Domain) (pools.R
if err != nil {
return nil, errors.Trace(err)
}
err = se.sessionVars.SetSystemVar(variable.TiDBConstraintCheckInPlacePessimistic, variable.On)
if err != nil {
return nil, errors.Trace(err)
}
se.sessionVars.CommonGlobalLoaded = true
se.sessionVars.InRestrictedSQL = true
// Internal session uses default format to prevent memory leak problem.
Expand Down
2 changes: 1 addition & 1 deletion sessionctx/variable/sysvar.go
Original file line number Diff line number Diff line change
Expand Up @@ -1929,7 +1929,7 @@ var defaultSysVars = []*SysVar{
DDLDiskQuota.Store(TidbOptUint64(val, DefTiDBDDLDiskQuota))
return nil
}},
{Scope: ScopeGlobal | ScopeSession, Name: TiDBConstraintCheckInPlacePessimistic, Value: BoolToOnOff(DefTiDBConstraintCheckInPlacePessimistic), Type: TypeBool,
{Scope: ScopeSession, Name: TiDBConstraintCheckInPlacePessimistic, Value: BoolToOnOff(config.GetGlobalConfig().PessimisticTxn.ConstraintCheckInPlacePessimistic), Type: TypeBool,
SetSession: func(s *SessionVars, val string) error {
s.ConstraintCheckInPlacePessimistic = TiDBOptOn(val)
if !s.ConstraintCheckInPlacePessimistic {
Expand Down
25 changes: 12 additions & 13 deletions sessionctx/variable/tidb_vars.go
Original file line number Diff line number Diff line change
Expand Up @@ -1070,19 +1070,18 @@ const (
DefTiDBGeneralPlanCacheSize = 100
DefTiDBEnableTiFlashReadForWriteStmt = false
// MaxDDLReorgBatchSize is exported for testing.
MaxDDLReorgBatchSize int32 = 10240
MinDDLReorgBatchSize int32 = 32
MinExpensiveQueryTimeThreshold uint64 = 10 // 10s
DefTiDBRcWriteCheckTs = false
DefTiDBConstraintCheckInPlacePessimistic = true
DefTiDBForeignKeyChecks = false
DefTiDBAnalyzePartitionConcurrency = 1
DefTiDBOptRangeMaxSize = 64 * int64(size.MB) // 64 MB
DefTiDBCostModelVer = 1
DefTiDBServerMemoryLimitSessMinSize = 128 << 20
DefTiDBMergePartitionStatsConcurrency = 1
DefTiDBServerMemoryLimitGCTrigger = 0.7
DefTiDBEnableGOGCTuner = true
MaxDDLReorgBatchSize int32 = 10240
MinDDLReorgBatchSize int32 = 32
MinExpensiveQueryTimeThreshold uint64 = 10 // 10s
DefTiDBRcWriteCheckTs = false
DefTiDBForeignKeyChecks = false
DefTiDBAnalyzePartitionConcurrency = 1
DefTiDBOptRangeMaxSize = 64 * int64(size.MB) // 64 MB
DefTiDBCostModelVer = 1
DefTiDBServerMemoryLimitSessMinSize = 128 << 20
DefTiDBMergePartitionStatsConcurrency = 1
DefTiDBServerMemoryLimitGCTrigger = 0.7
DefTiDBEnableGOGCTuner = true
// DefTiDBGOGCTunerThreshold is to limit TiDBGOGCTunerThreshold.
DefTiDBGOGCTunerThreshold float64 = 0.6
DefTiDBOptPrefixIndexSingleScan = true
Expand Down
3 changes: 2 additions & 1 deletion table/tables/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,8 @@ func (c *index) Create(sctx sessionctx.Context, txn kv.Transaction, indexedValue
}
if lazyCheck {
flags := []kv.FlagsOp{kv.SetPresumeKeyNotExists}
if !vars.ConstraintCheckInPlacePessimistic && vars.TxnCtx.IsPessimistic && vars.InTxn() {
if !vars.ConstraintCheckInPlacePessimistic && vars.TxnCtx.IsPessimistic && vars.InTxn() &&
!vars.InRestrictedSQL && vars.ConnectionID > 0 {
flags = append(flags, kv.SetNeedConstraintCheckInPrewrite)
}
err = txn.GetMemBuffer().SetWithFlags(key, idxVal, flags...)
Expand Down
3 changes: 2 additions & 1 deletion table/tables/tables.go
Original file line number Diff line number Diff line change
Expand Up @@ -854,7 +854,8 @@ func (t *TableCommon) AddRecord(sctx sessionctx.Context, r []types.Datum, opts .

if setPresume {
flags := []kv.FlagsOp{kv.SetPresumeKeyNotExists}
if !sessVars.ConstraintCheckInPlacePessimistic && sessVars.TxnCtx.IsPessimistic && sessVars.InTxn() {
if !sessVars.ConstraintCheckInPlacePessimistic && sessVars.TxnCtx.IsPessimistic && sessVars.InTxn() &&
!sessVars.InRestrictedSQL && sessVars.ConnectionID > 0 {
flags = append(flags, kv.SetNeedConstraintCheckInPrewrite)
}
err = memBuffer.SetWithFlags(key, value, flags...)
Expand Down
1 change: 1 addition & 0 deletions tidb-server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -671,6 +671,7 @@ func setGlobalVars() {
variable.SetSysVar(variable.TiDBIsolationReadEngines, strings.Join(cfg.IsolationRead.Engines, ","))
variable.SetSysVar(variable.TiDBEnforceMPPExecution, variable.BoolToOnOff(config.GetGlobalConfig().Performance.EnforceMPP))
variable.MemoryUsageAlarmRatio.Store(cfg.Instance.MemoryUsageAlarmRatio)
variable.SetSysVar(variable.TiDBConstraintCheckInPlacePessimistic, variable.BoolToOnOff(cfg.PessimisticTxn.ConstraintCheckInPlacePessimistic))
if hostname, err := os.Hostname(); err == nil {
variable.SetSysVar(variable.Hostname, hostname)
}
Expand Down

0 comments on commit 21d6190

Please sign in to comment.