Skip to content

Commit

Permalink
executor: check memory limit before expand string set in HashAgg (#37298
Browse files Browse the repository at this point in the history
)

ref #35635
  • Loading branch information
wshwsh12 authored Aug 30, 2022
1 parent ac6806f commit f600a5c
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
1 change: 1 addition & 0 deletions executor/aggregate.go
Original file line number Diff line number Diff line change
Expand Up @@ -418,6 +418,7 @@ func (e *HashAggExec) initForParallelExec(_ sessionctx.Context) {
}
// There is a bucket in the empty partialResultsMap.
e.memTracker.Consume(hack.DefBucketMemoryUsageForMapStrToSlice*(1<<w.BInMap) + setSize)
groupSet.SetTracker(e.memTracker)
if e.stats != nil {
w.stats = &AggWorkerStat{}
e.stats.FinalStats = append(e.stats.FinalStats, w.stats)
Expand Down
14 changes: 14 additions & 0 deletions util/set/set_with_memory_usage.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,17 @@ package set

import (
"github.com/pingcap/tidb/util/hack"
"github.com/pingcap/tidb/util/memory"
)

// StringSetWithMemoryUsage is a string set with memory usage.
type StringSetWithMemoryUsage struct {
StringSet
bInMap int64

// For tracking large memory usage in time.
// If tracker is non-nil, memDelta will track immediately and reset to 0. Otherwise, memDelta will return and lazy track.
tracker *memory.Tracker
}

// NewStringSetWithMemoryUsage builds a string set.
Expand All @@ -44,10 +49,19 @@ func (s *StringSetWithMemoryUsage) Insert(val string) (memDelta int64) {
if s.Count() > (1<<s.bInMap)*hack.LoadFactorNum/hack.LoadFactorDen {
memDelta = hack.DefBucketMemoryUsageForSetString * (1 << s.bInMap)
s.bInMap++
if s.tracker != nil {
s.tracker.Consume(memDelta)
memDelta = 0
}
}
return memDelta
}

// SetTracker sets memory tracker for StringSetWithMemoryUsage
func (s *StringSetWithMemoryUsage) SetTracker(t *memory.Tracker) {
s.tracker = t
}

// Float64SetWithMemoryUsage is a float64 set with memory usage.
type Float64SetWithMemoryUsage struct {
Float64Set
Expand Down

0 comments on commit f600a5c

Please sign in to comment.