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

*: enable global memory control by default #38899

Merged
merged 3 commits into from
Nov 4, 2022
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
6 changes: 4 additions & 2 deletions executor/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,8 @@ var (
_ Executor = &TopNExec{}
_ Executor = &UnionExec{}

// GlobalMemoryUsageTracker is the ancestor of all the Executors' memory tracker and GlobalMemory Tracker
GlobalMemoryUsageTracker *memory.Tracker
// GlobalDiskUsageTracker is the ancestor of all the Executors' disk tracker
GlobalDiskUsageTracker *disk.Tracker
// GlobalAnalyzeMemoryTracker is the ancestor of all the Analyze jobs' memory tracker and child of global Tracker
Expand Down Expand Up @@ -149,8 +151,8 @@ type globalPanicOnExceed struct {

func init() {
action := &globalPanicOnExceed{}
memory.GlobalMemoryUsageTracker = memory.NewGlobalTracker(memory.LabelForGlobalMemory, -1)
memory.GlobalMemoryUsageTracker.SetActionOnExceed(action)
GlobalMemoryUsageTracker = memory.NewGlobalTracker(memory.LabelForGlobalMemory, -1)
GlobalMemoryUsageTracker.SetActionOnExceed(action)
GlobalDiskUsageTracker = disk.NewGlobalTrcaker(memory.LabelForGlobalStorage, -1)
GlobalDiskUsageTracker.SetActionOnExceed(action)
GlobalAnalyzeMemoryTracker = memory.NewTracker(memory.LabelForGlobalAnalyzeMemory, -1)
Expand Down
18 changes: 0 additions & 18 deletions executor/executor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6226,21 +6226,3 @@ func TestSessionRootTrackerDetach(t *testing.T) {
require.NoError(t, err)
require.Nil(t, tk.Session().GetSessionVars().MemTracker.GetFallbackForTest(false))
}

func TestServerMemoryQuota(t *testing.T) {
config.UpdateGlobal(func(conf *config.Config) {
conf.Performance.ServerMemoryQuota = 123456789000
})
defer config.RestoreFunc()()
store := testkit.CreateMockStore(t)
tk := testkit.NewTestKit(t, store)

require.Equal(t, memory.GlobalMemoryUsageTracker.GetBytesLimit(), int64(123456789000))
tk.MustExec("set global tidb_server_memory_limit = 3 << 30")
require.Equal(t, memory.GlobalMemoryUsageTracker.GetBytesLimit(), int64(-1))
tk.MustExec("set global tidb_server_memory_limit = 0")
require.Equal(t, memory.GlobalMemoryUsageTracker.GetBytesLimit(), int64(123456789000))
require.Equal(t, tk.Session().GetSessionVars().MemTracker.GetParentForTest(), memory.GlobalMemoryUsageTracker)
tk.Session().Close()
require.Nil(t, tk.Session().GetSessionVars().MemTracker.GetParentForTest())
}
14 changes: 13 additions & 1 deletion session/bootstrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -641,11 +641,13 @@ const (
version97 = 97
// version98 add a column `Token_issuer` to `mysql.user`
version98 = 98
// version99 converts server-memory-quota to a sysvar
version99 = 99
)

// 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 = version98
var currentBootstrapVersion int64 = version99

// DDL owner key's expired time is ManagerSessionTTL seconds, we should wait the time and give more time to have a chance to finish it.
var internalSQLTimeout = owner.ManagerSessionTTL + 15
Expand Down Expand Up @@ -746,8 +748,10 @@ var (
upgradeToVer93,
upgradeToVer94,
upgradeToVer95,
// We will redo upgradeToVer96 in upgradeToVer99, it is skipped here.
upgradeToVer97,
upgradeToVer98,
upgradeToVer99,
}
)

Expand Down Expand Up @@ -1978,6 +1982,14 @@ func upgradeToVer98(s Session, ver int64) {
doReentrantDDL(s, "ALTER TABLE mysql.user ADD COLUMN IF NOT EXISTS `Token_issuer` varchar(255)")
}

func upgradeToVer99(s Session, ver int64) {
if ver >= version99 {
return
}
valStr := strconv.Itoa(int(config.GetGlobalConfig().Performance.ServerMemoryQuota))
importConfigOption(s, "performance.server-memory-quota", variable.TiDBServerMemoryLimit, 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
3 changes: 0 additions & 3 deletions session/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -2572,9 +2572,6 @@ func (s *session) Close() {
s.RollbackTxn(ctx)
if s.sessionVars != nil {
s.sessionVars.WithdrawAllPreparedStmt()
if s.sessionVars.MemTracker != nil {
s.sessionVars.MemTracker.Detach()
}
}
if s.stmtStats != nil {
s.stmtStats.SetFinished()
Expand Down
1 change: 0 additions & 1 deletion sessionctx/variable/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -1698,7 +1698,6 @@ func NewSessionVars(hctx HookContext) *SessionVars {
vars.DiskTracker = disk.NewTracker(memory.LabelForSession, -1)
vars.MemTracker = memory.NewTracker(memory.LabelForSession, vars.MemQuotaQuery)
vars.MemTracker.IsRootTrackerOfSess = true
vars.MemTracker.AttachToGlobalTracker(memory.GlobalMemoryUsageTracker)

for _, engine := range config.GetGlobalConfig().IsolationRead.Engines {
switch engine {
Expand Down
10 changes: 0 additions & 10 deletions sessionctx/variable/sysvar.go
Original file line number Diff line number Diff line change
Expand Up @@ -787,16 +787,6 @@ var defaultSysVars = []*SysVar{
memory.ServerMemoryLimitOriginText.Store(str)
memory.ServerMemoryLimit.Store(bt)
gctuner.GlobalMemoryLimitTuner.UpdateMemoryLimit()

if bt == 0 {
if config.GetGlobalConfig().Performance.ServerMemoryQuota < 1 {
memory.GlobalMemoryUsageTracker.SetBytesLimit(-1)
} else {
memory.GlobalMemoryUsageTracker.SetBytesLimit(int64(config.GetGlobalConfig().Performance.ServerMemoryQuota))
}
} else {
memory.GlobalMemoryUsageTracker.SetBytesLimit(-1)
}
return nil
},
},
Expand Down
2 changes: 1 addition & 1 deletion sessionctx/variable/tidb_vars.go
Original file line number Diff line number Diff line change
Expand Up @@ -1146,7 +1146,7 @@ var (

// DefTiDBServerMemoryLimit indicates the default value of TiDBServerMemoryLimit(TotalMem * 80%).
// It should be a const and shouldn't be modified after tidb is started.
DefTiDBServerMemoryLimit = "0"
DefTiDBServerMemoryLimit = serverMemoryLimitDefaultValue()
GOGCTunerThreshold = atomic.NewFloat64(DefTiDBGOGCTunerThreshold)
)

Expand Down
6 changes: 3 additions & 3 deletions tidb-server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -700,11 +700,11 @@ func setGlobalVars() {
executor.GlobalDiskUsageTracker.SetBytesLimit(cfg.TempStorageQuota)
if cfg.Performance.ServerMemoryQuota < 1 {
// If MaxMemory equals 0, it means unlimited
memory.GlobalMemoryUsageTracker.SetBytesLimit(-1)
executor.GlobalMemoryUsageTracker.SetBytesLimit(-1)
} else {
memory.GlobalMemoryUsageTracker.SetBytesLimit(int64(cfg.Performance.ServerMemoryQuota))
executor.GlobalMemoryUsageTracker.SetBytesLimit(int64(cfg.Performance.ServerMemoryQuota))
}
kvcache.GlobalLRUMemUsageTracker.AttachToGlobalTracker(memory.GlobalMemoryUsageTracker)
kvcache.GlobalLRUMemUsageTracker.AttachToGlobalTracker(executor.GlobalMemoryUsageTracker)

t, err := time.ParseDuration(cfg.TiKVClient.StoreLivenessTimeout)
if err != nil || t < 0 {
Expand Down
8 changes: 0 additions & 8 deletions util/memory/tracker.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,6 @@ var (
TriggerMemoryLimitGC = atomicutil.NewBool(false)
MemoryLimitGCLast = atomicutil.NewTime(time.Time{})
MemoryLimitGCTotal = atomicutil.NewInt64(0)

// GlobalMemoryUsageTracker is the ancestor of all the Executors' memory tracker and GlobalMemory Tracker
GlobalMemoryUsageTracker *Tracker
)

// Tracker is used to track the memory usage during query execution.
Expand Down Expand Up @@ -762,11 +759,6 @@ func (t *Tracker) setParent(parent *Tracker) {
t.parMu.parent = parent
}

// GetParentForTest return the parent of the Tracker. Only used by test.
func (t *Tracker) GetParentForTest() *Tracker {
return t.getParent()
}

// CountAllChildrenMemUse return memory used tree for the tracker
func (t *Tracker) CountAllChildrenMemUse() map[string]int64 {
trackerMemUseMap := make(map[string]int64, 1024)
Expand Down