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

*: disable global memory control by default #38859

Merged
merged 5 commits into from
Nov 3, 2022
Merged
Show file tree
Hide file tree
Changes from 2 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: 2 additions & 4 deletions executor/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,6 @@ 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 @@ -151,8 +149,8 @@ type globalPanicOnExceed struct {

func init() {
action := &globalPanicOnExceed{}
GlobalMemoryUsageTracker = memory.NewGlobalTracker(memory.LabelForGlobalMemory, -1)
GlobalMemoryUsageTracker.SetActionOnExceed(action)
memory.GlobalMemoryUsageTracker = memory.NewGlobalTracker(memory.LabelForGlobalMemory, -1)
memory.GlobalMemoryUsageTracker.SetActionOnExceed(action)
GlobalDiskUsageTracker = disk.NewGlobalTrcaker(memory.LabelForGlobalStorage, -1)
GlobalDiskUsageTracker.SetActionOnExceed(action)
GlobalAnalyzeMemoryTracker = memory.NewTracker(memory.LabelForGlobalAnalyzeMemory, -1)
Expand Down
14 changes: 14 additions & 0 deletions executor/executor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6199,3 +6199,17 @@ 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) {
Copy link
Contributor

Choose a reason for hiding this comment

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

defer config.RestoreFunc()()

conf.Performance.ServerMemoryQuota = 123456789000
})
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))
}
3 changes: 3 additions & 0 deletions server/conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -1049,6 +1049,9 @@ func (cc *clientConn) initConnect(ctx context.Context) error {
// This function returns and the connection is closed if there is an IO error or there is a panic.
func (cc *clientConn) Run(ctx context.Context) {
defer func() {
if tracker := cc.ctx.GetSessionVars().MemTracker; tracker != nil {
tracker.Detach()
}
r := recover()
if r != nil {
logutil.Logger(ctx).Error("connection running loop panic",
Expand Down
11 changes: 0 additions & 11 deletions session/bootstrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -636,8 +636,6 @@ const (
version94 = 94
// version95 add a column `User_attributes` to `mysql.user`
version95 = 95
// version96 converts server-memory-quota to a sysvar
version96 = 96
// version97 sets tidb_opt_range_max_size to 0 when a cluster upgrades from some version lower than v6.4.0 to v6.4.0+.
// It promises the compatibility of building ranges behavior.
version97 = 97
Expand Down Expand Up @@ -748,7 +746,6 @@ var (
upgradeToVer93,
upgradeToVer94,
upgradeToVer95,
upgradeToVer96,
upgradeToVer97,
upgradeToVer98,
}
Expand Down Expand Up @@ -1953,14 +1950,6 @@ func upgradeToVer95(s Session, ver int64) {
doReentrantDDL(s, "ALTER TABLE mysql.user ADD COLUMN IF NOT EXISTS `User_attributes` JSON")
}

func upgradeToVer96(s Session, ver int64) {
if ver >= version96 {
return
}
valStr := strconv.Itoa(int(config.GetGlobalConfig().Performance.ServerMemoryQuota))
importConfigOption(s, "performance.server-memory-quota", variable.TiDBServerMemoryLimit, valStr)
}

func upgradeToVer97(s Session, ver int64) {
if ver >= version97 {
return
Expand Down
1 change: 1 addition & 0 deletions sessionctx/variable/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -1625,6 +1625,7 @@ 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: 10 additions & 0 deletions sessionctx/variable/sysvar.go
Original file line number Diff line number Diff line change
Expand Up @@ -776,6 +776,16 @@ 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 @@ -1138,7 +1138,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 = serverMemoryLimitDefaultValue()
DefTiDBServerMemoryLimit = "0"
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 @@ -699,11 +699,11 @@ func setGlobalVars() {
executor.GlobalDiskUsageTracker.SetBytesLimit(cfg.TempStorageQuota)
if cfg.Performance.ServerMemoryQuota < 1 {
// If MaxMemory equals 0, it means unlimited
executor.GlobalMemoryUsageTracker.SetBytesLimit(-1)
memory.GlobalMemoryUsageTracker.SetBytesLimit(-1)
} else {
executor.GlobalMemoryUsageTracker.SetBytesLimit(int64(cfg.Performance.ServerMemoryQuota))
memory.GlobalMemoryUsageTracker.SetBytesLimit(int64(cfg.Performance.ServerMemoryQuota))
}
kvcache.GlobalLRUMemUsageTracker.AttachToGlobalTracker(executor.GlobalMemoryUsageTracker)
kvcache.GlobalLRUMemUsageTracker.AttachToGlobalTracker(memory.GlobalMemoryUsageTracker)

t, err := time.ParseDuration(cfg.TiKVClient.StoreLivenessTimeout)
if err != nil || t < 0 {
Expand Down
3 changes: 3 additions & 0 deletions util/memory/tracker.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ 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