Skip to content

Commit

Permalink
executor: ignore spill action when trigger global memory kill (#38198)
Browse files Browse the repository at this point in the history
ref #37816
  • Loading branch information
wshwsh12 authored Sep 27, 2022
1 parent c5a28bf commit ec6f60e
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 1 deletion.
38 changes: 38 additions & 0 deletions executor/executor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"runtime"
"strconv"
"strings"
"sync"
"testing"
"time"

Expand Down Expand Up @@ -6126,3 +6127,40 @@ func TestGlobalMemoryControl(t *testing.T) {
})
require.Equal(t, test[0], 0) // Keep 1GB HeapInUse
}

func TestGlobalMemoryControl2(t *testing.T) {
store, dom := testkit.CreateMockStoreAndDomain(t)

tk0 := testkit.NewTestKit(t, store)
tk0.MustExec("set global tidb_mem_oom_action = 'cancel'")
tk0.MustExec("set global tidb_server_memory_limit = 1 << 30")
tk0.MustExec("set global tidb_server_memory_limit_sess_min_size = 128")

sm := &testkit.MockSessionManager{
PS: []*util.ProcessInfo{tk0.Session().ShowProcess()},
}
dom.ServerMemoryLimitHandle().SetSessionManager(sm)
go dom.ServerMemoryLimitHandle().Run()

tk0.MustExec("use test")
tk0.MustExec("create table t(a int)")
tk0.MustExec("insert into t select 1")
for i := 1; i <= 8; i++ {
tk0.MustExec("insert into t select * from t") // 256 Lines
}

var test []int
wg := sync.WaitGroup{}
wg.Add(1)
go func() {
time.Sleep(100 * time.Millisecond) // Make sure the sql is running.
test = make([]int, 128<<20) // Keep 1GB HeapInuse
wg.Done()
}()
sql := "select * from t t1 join t t2 join t t3 on t1.a=t2.a and t1.a=t3.a order by t1.a;" // Need 500MB
require.True(t, strings.Contains(tk0.QueryToErr(sql).Error(), "Out Of Memory Quota!"))
require.Equal(t, tk0.Session().GetSessionVars().StmtCtx.DiskTracker.MaxConsumed(), int64(0))
wg.Wait()
test[0] = 0
runtime.GC()
}
14 changes: 13 additions & 1 deletion util/memory/tracker.go
Original file line number Diff line number Diff line change
Expand Up @@ -405,10 +405,22 @@ func (t *Tracker) Consume(bs int64) {
}
}

tryActionLastOne := func(mu *actionMu, tracker *Tracker) {
mu.Lock()
defer mu.Unlock()
if currentAction := mu.actionOnExceed; currentAction != nil {
for nextAction := currentAction.GetFallback(); nextAction != nil; {
currentAction = nextAction
nextAction = currentAction.GetFallback()
}
currentAction.Action(tracker)
}
}

if bs > 0 && sessionRootTracker != nil {
// Kill the Top1 session
if sessionRootTracker.NeedKill.Load() {
tryAction(&sessionRootTracker.actionMuForHardLimit, sessionRootTracker)
tryActionLastOne(&sessionRootTracker.actionMuForHardLimit, sessionRootTracker)
}
// Update the Top1 session
memUsage := sessionRootTracker.BytesConsumed()
Expand Down

0 comments on commit ec6f60e

Please sign in to comment.