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 the memroy usage in HashJoin probe phase #41081

Merged
merged 26 commits into from
Feb 9, 2023
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
d844ef2
fix
wshwsh12 Feb 3, 2023
2cec13c
test
wshwsh12 Feb 6, 2023
30c6b66
Merge branch 'master' into fix-join-mem
wshwsh12 Feb 6, 2023
9dae418
Update executor/hash_table.go
wshwsh12 Feb 6, 2023
6e26138
Update executor/hash_table.go
wshwsh12 Feb 6, 2023
6c5cc0b
fix build
wshwsh12 Feb 6, 2023
9fbb0ad
fmt
wshwsh12 Feb 7, 2023
a51c720
Merge branch 'master' into fix-join-mem
wshwsh12 Feb 7, 2023
9cf84ff
Merge branch 'master' into fix-join-mem
wshwsh12 Feb 7, 2023
4981e19
fix
wshwsh12 Feb 7, 2023
f1cbab8
fix
wshwsh12 Feb 7, 2023
8432996
Merge branch 'master' into fix-join-mem
ti-chi-bot Feb 8, 2023
e73efb2
Merge branch 'master' into fix-join-mem
ti-chi-bot Feb 8, 2023
fe6d1b6
Merge branch 'master' into fix-join-mem
ti-chi-bot Feb 8, 2023
1a33cc0
Merge branch 'master' into fix-join-mem
ti-chi-bot Feb 8, 2023
5f6e8f2
Merge branch 'master' into fix-join-mem
ti-chi-bot Feb 8, 2023
bc35346
Merge branch 'master' into fix-join-mem
ti-chi-bot Feb 8, 2023
dbb8352
Merge branch 'master' into fix-join-mem
ti-chi-bot Feb 8, 2023
8a76389
Merge branch 'master' into fix-join-mem
ti-chi-bot Feb 8, 2023
4172799
Merge branch 'master' into fix-join-mem
ti-chi-bot Feb 8, 2023
c87b073
Merge branch 'master' into fix-join-mem
ti-chi-bot Feb 8, 2023
7eac60a
Merge branch 'master' into fix-join-mem
ti-chi-bot Feb 8, 2023
4c9283a
Merge branch 'master' into fix-join-mem
ti-chi-bot Feb 9, 2023
981160b
Merge branch 'master' into fix-join-mem
ti-chi-bot Feb 9, 2023
f7be57b
Merge branch 'master' into fix-join-mem
ti-chi-bot Feb 9, 2023
c2485b8
Merge branch 'master' into fix-join-mem
wshwsh12 Feb 9, 2023
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
39 changes: 37 additions & 2 deletions executor/hash_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,8 @@ type hashRowContainer struct {
memTracker *memory.Tracker

// chkBuf buffer the data reads from the disk if rowContainer is spilled.
chkBuf *chunk.Chunk
chkBuf *chunk.Chunk
chkBufSizeForOneProbe int64
}

func newHashRowContainer(sCtx sessionctx.Context, hCtx *hashContext, allTypes []*types.FieldType) *hashRowContainer {
Expand Down Expand Up @@ -213,6 +214,15 @@ func (c *hashRowContainer) GetAllMatchedRows(probeHCtx *hashContext, probeSideRo
return matched, nil
}

// signalCheckpointForJoin indicates the times of row probe that a signal detection will be triggered.
const signalCheckpointForJoin int = 1 << 14

// rowSize is the size of Row.
const rowSize = int64(unsafe.Sizeof(chunk.Row{}))

// rowPtrSize is the size of RowPtr.
const rowPtrSize = int64(unsafe.Sizeof(chunk.RowPtr{}))

// GetMatchedRowsAndPtrs get matched rows and Ptrs from probeRow. It can be called
// in multiple goroutines while each goroutine should keep its own
// h and buf.
Expand All @@ -225,7 +235,19 @@ func (c *hashRowContainer) GetMatchedRowsAndPtrs(probeKey uint64, probeRow chunk
matched = matched[:0]
var matchedRow chunk.Row
matchedPtrs = matchedPtrs[:0]
for _, ptr := range innerPtrs {

// Some variables used for memTracker.
var (
matchedDataSize = int64(cap(matched))*rowSize + int64(cap(matchedPtrs))*rowPtrSize
lastChunkBufPointer *chunk.Chunk = nil
memDelta int64 = 0
)
c.chkBuf = nil
c.memTracker.Consume(-c.chkBufSizeForOneProbe + int64(cap(innerPtrs))*rowPtrSize)
defer c.memTracker.Consume(-int64(cap(innerPtrs)) * rowPtrSize)
c.chkBufSizeForOneProbe = 0

for i, ptr := range innerPtrs {
matchedRow, c.chkBuf, err = c.rowContainer.GetRowAndAppendToChunk(ptr, c.chkBuf)
if err != nil {
return nil, nil, err
Expand All @@ -239,6 +261,19 @@ func (c *hashRowContainer) GetMatchedRowsAndPtrs(probeKey uint64, probeRow chunk
atomic.AddInt64(&c.stat.probeCollision, 1)
continue
}
if c.chkBuf != lastChunkBufPointer && lastChunkBufPointer != nil {
lastChunkSize := lastChunkBufPointer.MemoryUsage()
c.chkBufSizeForOneProbe += lastChunkSize
memDelta += lastChunkSize
}
lastChunkBufPointer = c.chkBuf
if i&signalCheckpointForJoin == 0 {
// Trigger Consume for checking the OOM Action signal
memDelta += int64(cap(matched))*rowSize + int64(cap(matchedPtrs))*rowPtrSize - matchedDataSize
matchedDataSize = int64(cap(matched))*rowSize + int64(cap(matchedPtrs))*rowPtrSize
c.memTracker.Consume(memDelta + 1)
memDelta = 0
}
matched = append(matched, matchedRow)
if needPtr {
matchedPtrs = append(matchedPtrs, ptr)
Expand Down
17 changes: 17 additions & 0 deletions executor/join_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2892,3 +2892,20 @@ func TestOuterJoin(t *testing.T) {
),
)
}

func TestCartesianJoinPanic(t *testing.T) {
store := testkit.CreateMockStore(t)
tk := testkit.NewTestKit(t, store)
tk.MustExec("use test")
tk.MustExec("create table t(a int)")
tk.MustExec("insert into t values(1)")
tk.MustExec("set tidb_mem_quota_query = 1 << 30")
tk.MustExec("set global tidb_mem_oom_action = 'CANCEL'")
tk.MustExec("set global tidb_enable_tmp_storage_on_oom = off;")
for i := 0; i < 14; i++ {
tk.MustExec("insert into t select * from t")
}
err := tk.QueryToErr("desc analyze select * from t t1, t t2, t t3, t t4, t t5, t t6;")
require.NotNil(t, err)
require.True(t, strings.Contains(err.Error(), "Out Of Memory Quota!"))
}