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

executor: check memory limit before expand string set in HashAgg #37298

Merged
merged 3 commits into from
Aug 30, 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
1 change: 1 addition & 0 deletions executor/aggregate.go
Original file line number Diff line number Diff line change
Expand Up @@ -420,6 +420,7 @@ func (e *HashAggExec) initForParallelExec(ctx 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