Skip to content

Commit

Permalink
executor: track the memory usage for building range in IndexLookUpExe…
Browse files Browse the repository at this point in the history
…cutor (#56497)

close #56440
  • Loading branch information
wshwsh12 authored Oct 9, 2024
1 parent 19d2760 commit 5b44864
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 3 deletions.
7 changes: 7 additions & 0 deletions pkg/distsql/request_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"sort"
"sync/atomic"
"time"
"unsafe"

"github.com/pingcap/errors"
"github.com/pingcap/failpoint"
Expand Down Expand Up @@ -749,6 +750,9 @@ func indexRangesToKVWithoutSplit(dctx *distsqlctx.DistSQLContext, tids []int64,
krs[i] = make([]kv.KeyRange, 0, len(ranges))
}

if memTracker != nil {
memTracker.Consume(int64(unsafe.Sizeof(kv.KeyRange{})) * int64(len(ranges)))
}
const checkSignalStep = 8
var estimatedMemUsage int64
// encodeIndexKey and EncodeIndexSeekKey is time-consuming, thus we need to
Expand Down Expand Up @@ -777,6 +781,9 @@ func indexRangesToKVWithoutSplit(dctx *distsqlctx.DistSQLContext, tids []int64,
if interruptSignal != nil && interruptSignal.Load().(bool) {
return kv.NewPartitionedKeyRanges(nil), nil
}
if memTracker != nil {
memTracker.HandleKillSignal()
}
}
}
return kv.NewPartitionedKeyRanges(krs), nil
Expand Down
12 changes: 10 additions & 2 deletions pkg/executor/distsql.go
Original file line number Diff line number Diff line change
Expand Up @@ -540,6 +540,14 @@ func (e *IndexLookUpExecutor) Open(ctx context.Context) error {
return err
}
}

if e.memTracker != nil {
e.memTracker.Reset()
} else {
e.memTracker = memory.NewTracker(e.ID(), -1)
}
e.memTracker.AttachTo(e.stmtMemTracker)

err = e.buildTableKeyRanges()
if err != nil {
return err
Expand Down Expand Up @@ -570,7 +578,7 @@ func (e *IndexLookUpExecutor) buildTableKeyRanges() (err error) {
if e.index.ID == -1 {
kvRange, err = distsql.CommonHandleRangesToKVRanges(dctx, []int64{physicalID}, ranges)
} else {
kvRange, err = distsql.IndexRangesToKVRanges(dctx, physicalID, e.index.ID, ranges)
kvRange, err = distsql.IndexRangesToKVRangesWithInterruptSignal(dctx, physicalID, e.index.ID, ranges, e.memTracker, nil)
}
if err != nil {
return err
Expand All @@ -583,7 +591,7 @@ func (e *IndexLookUpExecutor) buildTableKeyRanges() (err error) {
if e.index.ID == -1 {
kvRanges, err = distsql.CommonHandleRangesToKVRanges(dctx, []int64{physicalID}, e.ranges)
} else {
kvRanges, err = distsql.IndexRangesToKVRanges(dctx, physicalID, e.index.ID, e.ranges)
kvRanges, err = distsql.IndexRangesToKVRangesWithInterruptSignal(dctx, physicalID, e.index.ID, e.ranges, e.memTracker, nil)
}
e.kvRanges = kvRanges.FirstPartitionRange()
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/executor/executor_pkg_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ func TestBuildKvRangesForIndexJoinWithoutCwcAndWithMemoryTracker(t *testing.T) {
}

require.Equal(t, 2*bytesConsumed1, bytesConsumed2)
require.Equal(t, int64(20760), bytesConsumed1)
require.Equal(t, int64(25560), bytesConsumed1)
}

func generateIndexRange(vals ...int64) *ranger.Range {
Expand Down

0 comments on commit 5b44864

Please sign in to comment.