Skip to content

Commit

Permalink
*: move config file option oom-use-tmp-storage to sysvar (#36679)
Browse files Browse the repository at this point in the history
ref #33769
  • Loading branch information
CbcWestwolf authored Aug 2, 2022
1 parent d18a8be commit c060208
Show file tree
Hide file tree
Showing 24 changed files with 56 additions and 55 deletions.
7 changes: 5 additions & 2 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,9 +170,8 @@ type Config struct {
RunDDL bool `toml:"run-ddl" json:"run-ddl"`
SplitTable bool `toml:"split-table" json:"split-table"`
TokenLimit uint `toml:"token-limit" json:"token-limit"`
OOMUseTmpStorage bool `toml:"oom-use-tmp-storage" json:"oom-use-tmp-storage"`
TempStoragePath string `toml:"tmp-storage-path" json:"tmp-storage-path"`
// TempStorageQuota describe the temporary storage Quota during query exector when OOMUseTmpStorage is enabled
// TempStorageQuota describe the temporary storage Quota during query exector when TiDBEnableTmpStorageOnOOM is enabled
// If the quota exceed the capacity of the TempStoragePath, the tidb-server would exit with fatal error
TempStorageQuota int64 `toml:"tmp-storage-quota" json:"tmp-storage-quota"` // Bytes
TxnLocalLatches tikvcfg.TxnLocalLatches `toml:"-" json:"-"`
Expand Down Expand Up @@ -269,6 +268,9 @@ type Config struct {
MemQuotaQuery int64 `toml:"mem-quota-query" json:"mem-quota-query"`
OOMAction string `toml:"oom-action" json:"oom-action"`

// OOMUseTmpStorage unused since bootstrap v93
OOMUseTmpStorage bool `toml:"oom-use-tmp-storage" json:"oom-use-tmp-storage"`

// CheckMb4ValueInUTF8, EnableCollectExecutionInfo, Plugin are deprecated.
CheckMb4ValueInUTF8 AtomicBool `toml:"check-mb4-value-in-utf8" json:"check-mb4-value-in-utf8"`
EnableCollectExecutionInfo bool `toml:"enable-collect-execution-info" json:"enable-collect-execution-info"`
Expand Down Expand Up @@ -1029,6 +1031,7 @@ var removedConfig = map[string]struct{}{
"plugin.dir": {}, // use plugin_dir
"performance.feedback-probability": {}, // This feature is deprecated
"performance.query-feedback-limit": {},
"oom-use-tmp-storage": {}, // use tidb_enable_tmp_storage_on_oom
}

// isAllRemovedConfigItems returns true if all the items that couldn't validate
Expand Down
7 changes: 2 additions & 5 deletions config/config.toml.example
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,12 @@ split-table = true
# The limit of concurrent executed sessions.
token-limit = 1000

# Controls whether to enable the temporary storage for some operators when a single SQL statement exceeds the memory quota specified by the memory quota.
oom-use-tmp-storage = true

# Specifies the temporary storage path for some operators when a single SQL statement exceeds the memory quota specified by the memory quota.
# It defaults to a generated directory in `<TMPDIR>/<os/user.Current().Uid>_tidb/` if it is unset.
# It only takes effect when `oom-use-tmp-storage` is `true`.
# It only takes effect when `tidb_enable_tmp_storage_on_oom` is `true`.
# tmp-storage-path = "/tmp/<os/user.Current().Uid>_tidb/MC4wLjAuMDo0MDAwLzAuMC4wLjA6MTAwODA=/tmp-storage"

# Specifies the maximum use of temporary storage (bytes) for all active queries when `oom-use-tmp-storage` is enabled.
# Specifies the maximum use of temporary storage (bytes) for all active queries when `tidb_enable_tmp_storage_on_oom` is enabled.
# If the `tmp-storage-quota` exceeds the capacity of the temporary storage directory, tidb-server would return an error and exit.
# The default value of tmp-storage-quota is under 0 which means tidb-server wouldn't check the capacity.
tmp-storage-quota = -1
Expand Down
5 changes: 1 addition & 4 deletions config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,14 +208,11 @@ token-limit = 1000
# The maximum memory available for a single SQL statement. Default: 1GB
mem-quota-query = 1073741824
# Controls whether to enable the temporary storage for some operators when a single SQL statement exceeds the memory quota specified by mem-quota-query.
oom-use-tmp-storage = true
# Specifies the temporary storage path for some operators when a single SQL statement exceeds the memory quota specified by mem-quota-query.
# <snip>
# tmp-storage-path = "/tmp/<os/user.Current().Uid>_tidb/MC4wLjAuMDo0MDAwLzAuMC4wLjA6MTAwODA=/tmp-storage"
# Specifies the maximum use of temporary storage (bytes) for all active queries when oom-use-tmp-storage is enabled.
# Specifies the maximum use of temporary storage (bytes) for all active queries when tidb_enable_tmp_storage_on_oom is enabled.
# If the tmp-storage-quota exceeds the capacity of the temporary storage directory, tidb-server would return an error and exit.
# The default value of tmp-storage-quota is under 0 which means tidb-server wouldn't check the capacity.
tmp-storage-quota = -1
Expand Down
1 change: 1 addition & 0 deletions ddl/schematracker/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ go_library(
"//sessionctx",
"//sessionctx/variable",
"//statistics/handle",
"//store/mockstore",
"//table",
"//table/tables",
"//tidb-binlog/pump_client",
Expand Down
4 changes: 2 additions & 2 deletions executor/aggregate.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,13 @@ import (

"github.com/pingcap/errors"
"github.com/pingcap/failpoint"
"github.com/pingcap/tidb/config"
"github.com/pingcap/tidb/executor/aggfuncs"
"github.com/pingcap/tidb/expression"
"github.com/pingcap/tidb/parser/mysql"
"github.com/pingcap/tidb/parser/terror"
"github.com/pingcap/tidb/sessionctx"
"github.com/pingcap/tidb/sessionctx/stmtctx"
"github.com/pingcap/tidb/sessionctx/variable"
"github.com/pingcap/tidb/types"
"github.com/pingcap/tidb/types/json"
"github.com/pingcap/tidb/util/chunk"
Expand Down Expand Up @@ -334,7 +334,7 @@ func (e *HashAggExec) initForUnparallelExec() {
e.executed, e.isChildDrained = false, false
e.listInDisk = chunk.NewListInDisk(retTypes(e.children[0]))
e.tmpChkForSpill = newFirstChunk(e.children[0])
if e.ctx.GetSessionVars().TrackAggregateMemoryUsage && config.GetGlobalConfig().OOMUseTmpStorage {
if e.ctx.GetSessionVars().TrackAggregateMemoryUsage && variable.EnableTmpStorageOnOOM.Load() {
e.diskTracker = disk.NewTracker(e.id, -1)
e.diskTracker.AttachTo(e.ctx.GetSessionVars().StmtCtx.DiskTracker)
e.listInDisk.GetDiskTracker().AttachTo(e.diskTracker)
Expand Down
6 changes: 3 additions & 3 deletions executor/cte.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ import (

"github.com/pingcap/errors"
"github.com/pingcap/failpoint"
"github.com/pingcap/tidb/config"
"github.com/pingcap/tidb/sessionctx"
"github.com/pingcap/tidb/sessionctx/variable"
"github.com/pingcap/tidb/util/chunk"
"github.com/pingcap/tidb/util/codec"
"github.com/pingcap/tidb/util/cteutil"
Expand Down Expand Up @@ -160,7 +160,7 @@ func (e *CTEExec) Next(ctx context.Context, req *chunk.Chunk) (err error) {
}

failpoint.Inject("testCTEStorageSpill", func(val failpoint.Value) {
if val.(bool) && config.GetGlobalConfig().OOMUseTmpStorage {
if val.(bool) && variable.EnableTmpStorageOnOOM.Load() {
defer resAction.WaitForTest()
defer iterInAction.WaitForTest()
if iterOutAction != nil {
Expand Down Expand Up @@ -429,7 +429,7 @@ func setupCTEStorageTracker(tbl cteutil.Storage, ctx sessionctx.Context, parentM
diskTracker.SetLabel(memory.LabelForCTEStorage)
diskTracker.AttachTo(parentDiskTracker)

if config.GetGlobalConfig().OOMUseTmpStorage {
if variable.EnableTmpStorageOnOOM.Load() {
actionSpill = tbl.ActionSpill()
failpoint.Inject("testCTEStorageSpill", func(val failpoint.Value) {
if val.(bool) {
Expand Down
8 changes: 2 additions & 6 deletions executor/cte_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (
"testing"

"github.com/pingcap/failpoint"
"github.com/pingcap/tidb/config"
"github.com/pingcap/tidb/parser/terror"
"github.com/pingcap/tidb/testkit"
"github.com/pingcap/tidb/types"
Expand Down Expand Up @@ -350,14 +349,11 @@ func TestCTEWithLimit(t *testing.T) {
}

func TestSpillToDisk(t *testing.T) {
defer config.RestoreFunc()()
config.UpdateGlobal(func(conf *config.Config) {
conf.OOMUseTmpStorage = true
})

store := testkit.CreateMockStore(t)

tk := testkit.NewTestKit(t, store)
tk.MustExec("SET GLOBAL tidb_enable_tmp_storage_on_oom = 1")
defer tk.MustExec("SET GLOBAL tidb_enable_tmp_storage_on_oom = 0")
tk.MustExec("use test;")

require.NoError(t, failpoint.Enable("github.com/pingcap/tidb/executor/testCTEStorageSpill", "return(true)"))
Expand Down
2 changes: 1 addition & 1 deletion executor/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -1932,7 +1932,7 @@ func ResetContextOfStmt(ctx sessionctx.Context, s ast.StmtNode) (err error) {

sc.InitDiskTracker(memory.LabelForSQLText, -1)
globalConfig := config.GetGlobalConfig()
if globalConfig.OOMUseTmpStorage && GlobalDiskUsageTracker != nil {
if variable.EnableTmpStorageOnOOM.Load() && GlobalDiskUsageTracker != nil {
sc.DiskTracker.AttachToGlobalTracker(GlobalDiskUsageTracker)
}
switch variable.OOMAction.Load() {
Expand Down
5 changes: 0 additions & 5 deletions executor/executor_pkg_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import (
"unsafe"

"github.com/pingcap/failpoint"
"github.com/pingcap/tidb/config"
"github.com/pingcap/tidb/executor/aggfuncs"
"github.com/pingcap/tidb/expression"
"github.com/pingcap/tidb/kv"
Expand Down Expand Up @@ -437,10 +436,6 @@ func TestLoadDataWithDifferentEscapeChar(t *testing.T) {
}

func TestSortSpillDisk(t *testing.T) {
defer config.RestoreFunc()()
config.UpdateGlobal(func(conf *config.Config) {
conf.OOMUseTmpStorage = true
})
require.NoError(t, failpoint.Enable("github.com/pingcap/tidb/executor/testSortedRowContainerSpill", "return(true)"))
defer func() {
require.NoError(t, failpoint.Disable("github.com/pingcap/tidb/executor/testSortedRowContainerSpill"))
Expand Down
4 changes: 2 additions & 2 deletions executor/join.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@ import (

"github.com/pingcap/errors"
"github.com/pingcap/failpoint"
"github.com/pingcap/tidb/config"
"github.com/pingcap/tidb/expression"
"github.com/pingcap/tidb/parser/terror"
plannercore "github.com/pingcap/tidb/planner/core"
"github.com/pingcap/tidb/sessionctx"
"github.com/pingcap/tidb/sessionctx/variable"
"github.com/pingcap/tidb/types"
"github.com/pingcap/tidb/util"
"github.com/pingcap/tidb/util/bitmap"
Expand Down Expand Up @@ -779,7 +779,7 @@ func (e *HashJoinExec) buildHashTableForList(buildSideResultCh <-chan *chunk.Chu
e.rowContainer.GetMemTracker().SetLabel(memory.LabelForBuildSideResult)
e.rowContainer.GetDiskTracker().AttachTo(e.diskTracker)
e.rowContainer.GetDiskTracker().SetLabel(memory.LabelForBuildSideResult)
if config.GetGlobalConfig().OOMUseTmpStorage {
if variable.EnableTmpStorageOnOOM.Load() {
actionSpill := e.rowContainer.ActionSpill()
failpoint.Inject("testRowContainerSpill", func(val failpoint.Value) {
if val.(bool) {
Expand Down
3 changes: 0 additions & 3 deletions executor/join_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,6 @@ func TestJoinPanic2(t *testing.T) {
func TestJoinInDisk(t *testing.T) {
origin := config.RestoreFunc()
defer origin()
config.UpdateGlobal(func(conf *config.Config) {
conf.OOMUseTmpStorage = true
})

store, dom := testkit.CreateMockStoreAndDomain(t)
tk := testkit.NewTestKit(t, store)
Expand Down
4 changes: 2 additions & 2 deletions executor/merge_join.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ import (
"context"

"github.com/pingcap/failpoint"
"github.com/pingcap/tidb/config"
"github.com/pingcap/tidb/expression"
"github.com/pingcap/tidb/sessionctx/stmtctx"
"github.com/pingcap/tidb/sessionctx/variable"
"github.com/pingcap/tidb/util/chunk"
"github.com/pingcap/tidb/util/disk"
"github.com/pingcap/tidb/util/memory"
Expand Down Expand Up @@ -93,7 +93,7 @@ func (t *mergeJoinTable) init(exec *MergeJoinExec) {
t.rowContainer.GetMemTracker().SetLabel(memory.LabelForInnerTable)
t.rowContainer.GetDiskTracker().AttachTo(exec.diskTracker)
t.rowContainer.GetDiskTracker().SetLabel(memory.LabelForInnerTable)
if config.GetGlobalConfig().OOMUseTmpStorage {
if variable.EnableTmpStorageOnOOM.Load() {
actionSpill := t.rowContainer.ActionSpill()
failpoint.Inject("testMergeJoinRowContainerSpill", func(val failpoint.Value) {
if val.(bool) {
Expand Down
6 changes: 0 additions & 6 deletions executor/merge_join_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -239,11 +239,6 @@ func checkPlanAndRun(tk *testkit.TestKit, t *testing.T, plan string, sql string)
}

func TestShuffleMergeJoinInDisk(t *testing.T) {
defer config.RestoreFunc()()
config.UpdateGlobal(func(conf *config.Config) {
conf.OOMUseTmpStorage = true
})

require.NoError(t, failpoint.Enable("github.com/pingcap/tidb/executor/testMergeJoinRowContainerSpill", "return(true)"))
defer func() {
require.NoError(t, failpoint.Disable("github.com/pingcap/tidb/executor/testMergeJoinRowContainerSpill"))
Expand Down Expand Up @@ -280,7 +275,6 @@ func TestMergeJoinInDisk(t *testing.T) {
restore := config.RestoreFunc()
defer restore()
config.UpdateGlobal(func(conf *config.Config) {
conf.OOMUseTmpStorage = true
conf.TempStoragePath = t.TempDir()
})

Expand Down
4 changes: 2 additions & 2 deletions executor/sort.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ import (
"errors"

"github.com/pingcap/failpoint"
"github.com/pingcap/tidb/config"
"github.com/pingcap/tidb/expression"
plannercore "github.com/pingcap/tidb/planner/core"
"github.com/pingcap/tidb/planner/util"
"github.com/pingcap/tidb/sessionctx/variable"
"github.com/pingcap/tidb/util/chunk"
"github.com/pingcap/tidb/util/disk"
"github.com/pingcap/tidb/util/mathutil"
Expand Down Expand Up @@ -181,7 +181,7 @@ func (e *SortExec) fetchRowChunks(ctx context.Context) error {
e.rowChunks = chunk.NewSortedRowContainer(fields, e.maxChunkSize, byItemsDesc, e.keyColumns, e.keyCmpFuncs)
e.rowChunks.GetMemTracker().AttachTo(e.memTracker)
e.rowChunks.GetMemTracker().SetLabel(memory.LabelForRowChunks)
if config.GetGlobalConfig().OOMUseTmpStorage {
if variable.EnableTmpStorageOnOOM.Load() {
e.spillAction = e.rowChunks.ActionSpill()
failpoint.Inject("testSortedRowContainerSpill", func(val failpoint.Value) {
if val.(bool) {
Expand Down
2 changes: 0 additions & 2 deletions executor/sort_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ func testSortInDisk(t *testing.T, removeDir bool) {
restore := config.RestoreFunc()
defer restore()
config.UpdateGlobal(func(conf *config.Config) {
conf.OOMUseTmpStorage = true
conf.TempStoragePath = t.TempDir()
})
require.NoError(t, failpoint.Enable("github.com/pingcap/tidb/executor/testSortedRowContainerSpill", "return(true)"))
Expand Down Expand Up @@ -97,7 +96,6 @@ func testSortInDisk(t *testing.T, removeDir bool) {
func TestIssue16696(t *testing.T) {
defer config.RestoreFunc()()
config.UpdateGlobal(func(conf *config.Config) {
conf.OOMUseTmpStorage = true
conf.TempStoragePath = t.TempDir()
})
alarmRatio := variable.MemoryUsageAlarmRatio.Load()
Expand Down
6 changes: 3 additions & 3 deletions planner/core/plan_cost.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ import (
"math"

"github.com/pingcap/errors"
"github.com/pingcap/tidb/config"
"github.com/pingcap/tidb/expression"
"github.com/pingcap/tidb/kv"
"github.com/pingcap/tidb/parser/model"
"github.com/pingcap/tidb/planner/property"
"github.com/pingcap/tidb/sessionctx/variable"
"github.com/pingcap/tidb/statistics"
"github.com/pingcap/tidb/util/paging"
)
Expand Down Expand Up @@ -890,7 +890,7 @@ func (p *PhysicalHashJoin) GetCost(lCnt, rCnt float64, isMPP bool, costFlag uint
build = p.children[1]
}
sessVars := p.ctx.GetSessionVars()
oomUseTmpStorage := config.GetGlobalConfig().OOMUseTmpStorage
oomUseTmpStorage := variable.EnableTmpStorageOnOOM.Load()
memQuota := sessVars.StmtCtx.MemTracker.GetBytesLimit() // sessVars.MemQuotaQuery && hint
rowSize := getAvgRowSize(build.statsInfo(), build.Schema())
spill := oomUseTmpStorage && memQuota > 0 && rowSize*buildCnt > float64(memQuota) && p.storeTp != kv.TiFlash
Expand Down Expand Up @@ -1087,7 +1087,7 @@ func (p *PhysicalSort) GetCost(count float64, schema *expression.Schema) float64
cpuCost := count * math.Log2(count) * sessVars.GetCPUFactor()
memoryCost := count * sessVars.GetMemoryFactor()

oomUseTmpStorage := config.GetGlobalConfig().OOMUseTmpStorage
oomUseTmpStorage := variable.EnableTmpStorageOnOOM.Load()
memQuota := sessVars.StmtCtx.MemTracker.GetBytesLimit() // sessVars.MemQuotaQuery && hint
rowSize := getAvgRowSize(p.statsInfo(), schema)
spill := oomUseTmpStorage && memQuota > 0 && rowSize*count > float64(memQuota)
Expand Down
1 change: 1 addition & 0 deletions session/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ go_library(
"//config",
"//ddl",
"//ddl/placement",
"//ddl/schematracker",
"//domain",
"//errno",
"//executor",
Expand Down
13 changes: 12 additions & 1 deletion session/bootstrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -623,11 +623,13 @@ const (
version91 = 91
// version92 for concurrent ddl.
version92 = 92
// version93 converts oom-use-tmp-storage to a sysvar
version93 = 93
)

// currentBootstrapVersion is defined as a variable, so we can modify its value for testing.
// please make sure this is the largest version
var currentBootstrapVersion int64 = version92
var currentBootstrapVersion int64 = version93

var (
bootstrapVersion = []func(Session, int64){
Expand Down Expand Up @@ -722,6 +724,7 @@ var (
upgradeToVer89,
upgradeToVer90,
upgradeToVer91,
upgradeToVer93,
}
)

Expand Down Expand Up @@ -1922,6 +1925,14 @@ func upgradeToVer91(s Session, ver int64) {
importConfigOption(s, "prepared-plan-cache.memory-guard-ratio", variable.TiDBPrepPlanCacheMemoryGuardRatio, valStr)
}

func upgradeToVer93(s Session, ver int64) {
if ver >= version93 {
return
}
valStr := variable.BoolToOnOff(config.GetGlobalConfig().OOMUseTmpStorage)
importConfigOption(s, "oom-use-tmp-storage", variable.TiDBEnableTmpStorageOnOOM, valStr)
}

func writeOOMAction(s Session) {
comment := "oom-action is `log` by default in v3.0.x, `cancel` by default in v4.0.11+"
mustExecute(s, `INSERT HIGH_PRIORITY INTO %n.%n VALUES (%?, %?, %?) ON DUPLICATE KEY UPDATE VARIABLE_VALUE= %?`,
Expand Down
6 changes: 6 additions & 0 deletions sessionctx/variable/sysvar.go
Original file line number Diff line number Diff line change
Expand Up @@ -842,6 +842,12 @@ var defaultSysVars = []*SysVar{
}, GetGlobal: func(s *SessionVars) (string, error) {
return BoolToOnOff(memory.EnableGCAwareMemoryTrack.Load()), nil
}},
{Scope: ScopeGlobal, Name: TiDBEnableTmpStorageOnOOM, Value: BoolToOnOff(DefTiDBEnableTmpStorageOnOOM), Type: TypeBool, SetGlobal: func(s *SessionVars, val string) error {
EnableTmpStorageOnOOM.Store(TiDBOptOn(val))
return nil
}, GetGlobal: func(s *SessionVars) (string, error) {
return BoolToOnOff(EnableTmpStorageOnOOM.Load()), nil
}},

/* The system variables below have GLOBAL and SESSION scope */
{Scope: ScopeGlobal | ScopeSession, Name: TiDBRowFormatVersion, Value: strconv.Itoa(DefTiDBRowFormatV1), Type: TypeUnsigned, MinValue: 1, MaxValue: 2, SetGlobal: func(s *SessionVars, val string) error {
Expand Down
Loading

0 comments on commit c060208

Please sign in to comment.