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: track memory usage for merge join #6172

Merged
merged 4 commits into from
Mar 31, 2018
Merged
Show file tree
Hide file tree
Changes from 3 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
28 changes: 23 additions & 5 deletions executor/merge_join.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"github.com/pingcap/tidb/expression"
"github.com/pingcap/tidb/sessionctx/stmtctx"
"github.com/pingcap/tidb/util/chunk"
"github.com/pingcap/tidb/util/memory"
"golang.org/x/net/context"
)

Expand All @@ -43,6 +44,8 @@ type MergeJoinExec struct {

innerRows []chunk.Row
innerIter4Row chunk.Iterator

memTracker *memory.Tracker
}

type mergeJoinOuterTable struct {
Expand Down Expand Up @@ -75,6 +78,8 @@ type mergeJoinInnerTable struct {
curResultInUse bool
resultQueue []*chunk.Chunk
resourceQueue []*chunk.Chunk

memTracker *memory.Tracker
}

func (t *mergeJoinInnerTable) init(ctx context.Context, chk4Reader *chunk.Chunk) (err error) {
Expand All @@ -87,6 +92,7 @@ func (t *mergeJoinInnerTable) init(ctx context.Context, chk4Reader *chunk.Chunk)
t.curRow = t.curIter.End()
t.curResultInUse = false
t.resultQueue = append(t.resultQueue, chk4Reader)
t.memTracker.Consume(chk4Reader.MemoryUsage())
t.firstRow4Key, err = t.nextRow()
t.compareFuncs = make([]chunk.CompareFunc, 0, len(t.joinKeys))
for i := range t.joinKeys {
Expand Down Expand Up @@ -125,12 +131,15 @@ func (t *mergeJoinInnerTable) rowsWithSameKey() ([]chunk.Row, error) {
func (t *mergeJoinInnerTable) nextRow() (chunk.Row, error) {
if t.curRow == t.curIter.End() {
t.reallocReaderResult()
oldMemUsage := t.curResult.MemoryUsage()
err := t.reader.NextChunk(t.ctx, t.curResult)
// error happens or no more data.
if err != nil || t.curResult.NumRows() == 0 {
t.curRow = t.curIter.End()
return t.curRow, errors.Trace(err)
}
newMemUsage := t.curResult.MemoryUsage()
t.memTracker.Consume(newMemUsage - oldMemUsage)
t.curRow = t.curIter.Begin()
}
result := t.curRow
Expand All @@ -151,7 +160,9 @@ func (t *mergeJoinInnerTable) reallocReaderResult() {
// Create a new Chunk and append it to "resourceQueue" if there is no more
// available chunk in "resourceQueue".
if len(t.resourceQueue) == 0 {
t.resourceQueue = append(t.resourceQueue, t.reader.newChunk())
newChunk := t.reader.newChunk()
t.memTracker.Consume(newChunk.MemoryUsage())
t.resourceQueue = append(t.resourceQueue, newChunk)
}

// NOTE: "t.curResult" is always the last element of "resultQueue".
Expand All @@ -165,18 +176,25 @@ func (t *mergeJoinInnerTable) reallocReaderResult() {

// Close implements the Executor Close interface.
func (e *MergeJoinExec) Close() error {
if err := e.baseExecutor.Close(); err != nil {
return errors.Trace(err)
}
return nil
e.memTracker.Detach()
e.memTracker = nil

return errors.Trace(e.baseExecutor.Close())
}

// Open implements the Executor Open interface.
func (e *MergeJoinExec) Open(ctx context.Context) error {
if err := e.baseExecutor.Open(ctx); err != nil {
return errors.Trace(err)
}

e.prepared = false
e.memTracker = memory.NewTracker(e.id, e.ctx.GetSessionVars().MemQuotaMergeJoin)
e.memTracker.AttachTo(e.ctx.GetSessionVars().StmtCtx.MemTracker)

e.innerTable.memTracker = memory.NewTracker("innerTable", -1)
e.innerTable.memTracker.AttachTo(e.memTracker)
Copy link
Contributor

Choose a reason for hiding this comment

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

Do we need to detach e.innerTable.memTracker?

Copy link
Member

Choose a reason for hiding this comment

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

No, innerTable.memTracker is a child of e.memTracker, e.memTracker is detached in Close.


return nil
}

Expand Down
5 changes: 5 additions & 0 deletions sessionctx/variable/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,8 @@ type SessionVars struct {
MemQuotaQuery int64
// MemQuotaHashJoin defines the memory quota for a hash join executor.
MemQuotaHashJoin int64
// MemQuotaMergeJoin defines the memory quota for a merge join executor.
MemQuotaMergeJoin int64
// MemQuotaSort defines the memory quota for a sort executor.
MemQuotaSort int64
// MemQuotaTopn defines the memory quota for a top n executor.
Expand Down Expand Up @@ -323,6 +325,7 @@ func NewSessionVars() *SessionVars {
DMLBatchSize: DefDMLBatchSize,
MemQuotaQuery: DefTiDBMemQuotaQuery,
MemQuotaHashJoin: DefTiDBMemQuotaHashJoin,
MemQuotaMergeJoin: DefTiDBMemQuotaMergeJoin,
MemQuotaSort: DefTiDBMemQuotaSort,
MemQuotaTopn: DefTiDBMemQuotaTopn,
MemQuotaIndexLookupReader: DefTiDBMemQuotaIndexLookupReader,
Expand Down Expand Up @@ -501,6 +504,8 @@ func (s *SessionVars) SetSystemVar(name string, val string) error {
s.MemQuotaQuery = tidbOptInt64(val, DefTiDBMemQuotaQuery)
case TIDBMemQuotaHashJoin:
s.MemQuotaHashJoin = tidbOptInt64(val, DefTiDBMemQuotaHashJoin)
case TIDBMemQuotaMergeJoin:
s.MemQuotaMergeJoin = tidbOptInt64(val, DefTiDBMemQuotaMergeJoin)
case TIDBMemQuotaSort:
s.MemQuotaSort = tidbOptInt64(val, DefTiDBMemQuotaSort)
case TIDBMemQuotaTopn:
Expand Down
1 change: 1 addition & 0 deletions sessionctx/variable/sysvar.go
Original file line number Diff line number Diff line change
Expand Up @@ -624,6 +624,7 @@ var defaultSysVars = []*SysVar{
{ScopeSession, TiDBMaxChunkSize, strconv.Itoa(DefMaxChunkSize)},
{ScopeSession, TIDBMemQuotaQuery, strconv.FormatInt(DefTiDBMemQuotaQuery, 10)},
{ScopeSession, TIDBMemQuotaHashJoin, strconv.FormatInt(DefTiDBMemQuotaHashJoin, 10)},
{ScopeSession, TIDBMemQuotaMergeJoin, strconv.FormatInt(DefTiDBMemQuotaMergeJoin, 10)},
{ScopeSession, TIDBMemQuotaSort, strconv.FormatInt(DefTiDBMemQuotaSort, 10)},
{ScopeSession, TIDBMemQuotaTopn, strconv.FormatInt(DefTiDBMemQuotaTopn, 10)},
{ScopeSession, TIDBMemQuotaIndexLookupReader, strconv.FormatInt(DefTiDBMemQuotaIndexLookupReader, 10)},
Expand Down
3 changes: 3 additions & 0 deletions sessionctx/variable/tidb_vars.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,12 +116,14 @@ const (
// The following session variables controls the memory quota during query execution.
// "tidb_mem_quota_query": control the memory quota of a query.
// "tidb_mem_quota_hashjoin": control the memory quota of "HashJoinExec".
// "tidb_mem_quota_mergejoin": control the memory quota of "MergeJoinExec".
// "tidb_mem_quota_sort": control the memory quota of "SortExec".
// "tidb_mem_quota_topn": control the memory quota of "TopNExec".
// "tidb_mem_quota_indexlookupreader": control the memory quota of "IndexLookUpExecutor".
// "tidb_mem_quota_indexlookupjoin": control the memory quota of "IndexLookUpJoin".
TIDBMemQuotaQuery = "tidb_mem_quota_query" // Bytes.
TIDBMemQuotaHashJoin = "tidb_mem_quota_hashjoin" // Bytes.
TIDBMemQuotaMergeJoin = "tidb_mem_quota_mergejoin" // Bytes.
TIDBMemQuotaSort = "tidb_mem_quota_sort" // Bytes.
TIDBMemQuotaTopn = "tidb_mem_quota_topn" // Bytes.
TIDBMemQuotaIndexLookupReader = "tidb_mem_quota_indexlookupreader" // Bytes.
Expand Down Expand Up @@ -153,6 +155,7 @@ const (
DefDMLBatchSize = 20000
DefTiDBMemQuotaQuery = 32 << 30 // 32GB.
DefTiDBMemQuotaHashJoin = 32 << 30 // 32GB.
DefTiDBMemQuotaMergeJoin = 32 << 30 // 32GB.
DefTiDBMemQuotaSort = 32 << 30 // 32GB.
DefTiDBMemQuotaTopn = 32 << 30 // 32GB.
DefTiDBMemQuotaIndexLookupReader = 32 << 30 // 32GB.
Expand Down