forked from pingcap/tidb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparallel_apply.go
399 lines (362 loc) · 10.6 KB
/
parallel_apply.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
// Copyright 2020 PingCAP, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// See the License for the specific language governing permissions and
// limitations under the License.
package executor
import (
"context"
"runtime/trace"
"sync"
"sync/atomic"
"github.com/pingcap/errors"
"github.com/pingcap/failpoint"
"github.com/pingcap/parser/terror"
"github.com/pingcap/tidb/expression"
"github.com/pingcap/tidb/util/chunk"
"github.com/pingcap/tidb/util/codec"
"github.com/pingcap/tidb/util/execdetails"
"github.com/pingcap/tidb/util/logutil"
"github.com/pingcap/tidb/util/memory"
"go.uber.org/zap"
)
type result struct {
chk *chunk.Chunk
err error
}
type outerRow struct {
row *chunk.Row
selected bool // if this row is selected by the outer side
}
// ParallelNestedLoopApplyExec is the executor for apply.
type ParallelNestedLoopApplyExec struct {
baseExecutor
// outer-side fields
outerExec Executor
outerFilter expression.CNFExprs
outerList *chunk.List
outer bool
// inner-side fields
// use slices since the inner side is paralleled
corCols [][]*expression.CorrelatedColumn
innerFilter []expression.CNFExprs
innerExecs []Executor
innerList []*chunk.List
innerChunk []*chunk.Chunk
innerSelected [][]bool
innerIter []chunk.Iterator
outerRow []*chunk.Row
hasMatch []bool
hasNull []bool
joiners []joiner
// fields about concurrency control
concurrency int
started uint32
drained uint32 // drained == true indicates there is no more data
freeChkCh chan *chunk.Chunk
resultChkCh chan result
outerRowCh chan outerRow
exit chan struct{}
workerWg sync.WaitGroup
notifyWg sync.WaitGroup
// fields about cache
cache *applyCache
useCache bool
cacheHitCounter int64
cacheAccessCounter int64
memTracker *memory.Tracker // track memory usage.
}
// Open implements the Executor interface.
func (e *ParallelNestedLoopApplyExec) Open(ctx context.Context) error {
err := e.outerExec.Open(ctx)
if err != nil {
return err
}
e.memTracker = memory.NewTracker(e.id, -1)
e.memTracker.AttachTo(e.ctx.GetSessionVars().StmtCtx.MemTracker)
e.outerList = chunk.NewList(retTypes(e.outerExec), e.initCap, e.maxChunkSize)
e.outerList.GetMemTracker().SetLabel(memory.LabelForOuterList)
e.outerList.GetMemTracker().AttachTo(e.memTracker)
e.innerList = make([]*chunk.List, e.concurrency)
e.innerChunk = make([]*chunk.Chunk, e.concurrency)
e.innerSelected = make([][]bool, e.concurrency)
e.innerIter = make([]chunk.Iterator, e.concurrency)
e.outerRow = make([]*chunk.Row, e.concurrency)
e.hasMatch = make([]bool, e.concurrency)
e.hasNull = make([]bool, e.concurrency)
for i := 0; i < e.concurrency; i++ {
e.innerChunk[i] = newFirstChunk(e.innerExecs[i])
e.innerList[i] = chunk.NewList(retTypes(e.innerExecs[i]), e.initCap, e.maxChunkSize)
e.innerList[i].GetMemTracker().SetLabel(memory.LabelForInnerList)
e.innerList[i].GetMemTracker().AttachTo(e.memTracker)
}
e.freeChkCh = make(chan *chunk.Chunk, e.concurrency)
e.resultChkCh = make(chan result, e.concurrency+1) // innerWorkers + outerWorker
e.outerRowCh = make(chan outerRow)
e.exit = make(chan struct{})
for i := 0; i < e.concurrency; i++ {
e.freeChkCh <- newFirstChunk(e)
}
if e.useCache {
if e.cache, err = newApplyCache(e.ctx); err != nil {
return err
}
e.cache.GetMemTracker().AttachTo(e.memTracker)
}
return nil
}
// Next implements the Executor interface.
func (e *ParallelNestedLoopApplyExec) Next(ctx context.Context, req *chunk.Chunk) (err error) {
if atomic.LoadUint32(&e.drained) == 1 {
req.Reset()
return nil
}
if atomic.CompareAndSwapUint32(&e.started, 0, 1) {
e.workerWg.Add(1)
go e.outerWorker(ctx)
for i := 0; i < e.concurrency; i++ {
e.workerWg.Add(1)
workID := i
go e.innerWorker(ctx, workID)
}
e.notifyWg.Add(1)
go e.notifyWorker(ctx)
}
result := <-e.resultChkCh
if result.err != nil {
return result.err
}
if result.chk == nil { // no more data
req.Reset()
atomic.StoreUint32(&e.drained, 1)
return nil
}
req.SwapColumns(result.chk)
e.freeChkCh <- result.chk
return nil
}
// Close implements the Executor interface.
func (e *ParallelNestedLoopApplyExec) Close() error {
e.memTracker = nil
if atomic.LoadUint32(&e.started) == 1 {
close(e.exit)
e.notifyWg.Wait()
e.started = 0
}
// Wait all workers to finish before Close() is called.
// Otherwise we may got data race.
err := e.outerExec.Close()
if e.runtimeStats != nil {
runtimeStats := newJoinRuntimeStats()
e.ctx.GetSessionVars().StmtCtx.RuntimeStatsColl.RegisterStats(e.id, runtimeStats)
if e.useCache {
var hitRatio float64
if e.cacheAccessCounter > 0 {
hitRatio = float64(e.cacheHitCounter) / float64(e.cacheAccessCounter)
}
runtimeStats.setCacheInfo(true, hitRatio)
} else {
runtimeStats.setCacheInfo(false, 0)
}
runtimeStats.SetConcurrencyInfo(execdetails.NewConcurrencyInfo("Concurrency", e.concurrency))
}
return err
}
// notifyWorker waits for all inner/outer-workers finishing and then put an empty
// chunk into the resultCh to notify the upper executor there is no more data.
func (e *ParallelNestedLoopApplyExec) notifyWorker(ctx context.Context) {
defer e.handleWorkerPanic(ctx, &e.notifyWg)
e.workerWg.Wait()
e.putResult(nil, nil)
}
func (e *ParallelNestedLoopApplyExec) outerWorker(ctx context.Context) {
defer trace.StartRegion(ctx, "ParallelApplyOuterWorker").End()
defer e.handleWorkerPanic(ctx, &e.workerWg)
var selected []bool
var err error
for {
failpoint.Inject("parallelApplyOuterWorkerPanic", nil)
chk := newFirstChunk(e.outerExec)
if err := Next(ctx, e.outerExec, chk); err != nil {
e.putResult(nil, err)
return
}
if chk.NumRows() == 0 {
close(e.outerRowCh)
return
}
e.outerList.Add(chk)
outerIter := chunk.NewIterator4Chunk(chk)
selected, err = expression.VectorizedFilter(e.ctx, e.outerFilter, outerIter, selected)
if err != nil {
e.putResult(nil, err)
return
}
for i := 0; i < chk.NumRows(); i++ {
row := chk.GetRow(i)
select {
case e.outerRowCh <- outerRow{&row, selected[i]}:
case <-e.exit:
return
}
}
}
}
func (e *ParallelNestedLoopApplyExec) innerWorker(ctx context.Context, id int) {
defer trace.StartRegion(ctx, "ParallelApplyInnerWorker").End()
defer e.handleWorkerPanic(ctx, &e.workerWg)
for {
var chk *chunk.Chunk
select {
case chk = <-e.freeChkCh:
case <-e.exit:
return
}
failpoint.Inject("parallelApplyInnerWorkerPanic", nil)
err := e.fillInnerChunk(ctx, id, chk)
if err == nil && chk.NumRows() == 0 { // no more data, this goroutine can exit
return
}
if e.putResult(chk, err) {
return
}
}
}
func (e *ParallelNestedLoopApplyExec) putResult(chk *chunk.Chunk, err error) (exit bool) {
select {
case e.resultChkCh <- result{chk, err}:
return false
case <-e.exit:
return true
}
}
func (e *ParallelNestedLoopApplyExec) handleWorkerPanic(ctx context.Context, wg *sync.WaitGroup) {
if r := recover(); r != nil {
err := errors.Errorf("%v", r)
logutil.Logger(ctx).Error("parallel nested loop join worker panicked", zap.Error(err), zap.Stack("stack"))
e.resultChkCh <- result{nil, err}
}
if wg != nil {
wg.Done()
}
}
// fetchAllInners reads all data from the inner table and stores them in a List.
func (e *ParallelNestedLoopApplyExec) fetchAllInners(ctx context.Context, id int) (err error) {
var key []byte
for _, col := range e.corCols[id] {
*col.Data = e.outerRow[id].GetDatum(col.Index, col.RetType)
if e.useCache {
if key, err = codec.EncodeKey(e.ctx.GetSessionVars().StmtCtx, key, *col.Data); err != nil {
return err
}
}
}
if e.useCache { // look up the cache
atomic.AddInt64(&e.cacheAccessCounter, 1)
failpoint.Inject("parallelApplyGetCachePanic", nil)
value, err := e.cache.Get(key)
if err != nil {
return err
}
if value != nil {
e.innerList[id] = value
atomic.AddInt64(&e.cacheHitCounter, 1)
return nil
}
}
err = e.innerExecs[id].Open(ctx)
defer terror.Call(e.innerExecs[id].Close)
if err != nil {
return err
}
if e.useCache {
// create a new one in this case since it may be in the cache
e.innerList[id] = chunk.NewList(retTypes(e.innerExecs[id]), e.initCap, e.maxChunkSize)
} else {
e.innerList[id].Reset()
}
innerIter := chunk.NewIterator4Chunk(e.innerChunk[id])
for {
err := Next(ctx, e.innerExecs[id], e.innerChunk[id])
if err != nil {
return err
}
if e.innerChunk[id].NumRows() == 0 {
break
}
e.innerSelected[id], err = expression.VectorizedFilter(e.ctx, e.innerFilter[id], innerIter, e.innerSelected[id])
if err != nil {
return err
}
for row := innerIter.Begin(); row != innerIter.End(); row = innerIter.Next() {
if e.innerSelected[id][row.Idx()] {
e.innerList[id].AppendRow(row)
}
}
}
if e.useCache { // update the cache
failpoint.Inject("parallelApplySetCachePanic", nil)
if _, err := e.cache.Set(key, e.innerList[id]); err != nil {
return err
}
}
return nil
}
func (e *ParallelNestedLoopApplyExec) fetchNextOuterRow(id int, req *chunk.Chunk) (row *chunk.Row, exit bool) {
for {
select {
case outerRow, ok := <-e.outerRowCh:
if !ok { // no more data
return nil, false
}
if !outerRow.selected {
if e.outer {
e.joiners[id].onMissMatch(false, *outerRow.row, req)
if req.IsFull() {
return nil, false
}
}
continue // try the next outer row
}
return outerRow.row, false
case <-e.exit:
return nil, true
}
}
}
func (e *ParallelNestedLoopApplyExec) fillInnerChunk(ctx context.Context, id int, req *chunk.Chunk) (err error) {
req.Reset()
for {
if e.innerIter[id] == nil || e.innerIter[id].Current() == e.innerIter[id].End() {
if e.outerRow[id] != nil && !e.hasMatch[id] {
e.joiners[id].onMissMatch(e.hasNull[id], *e.outerRow[id], req)
}
var exit bool
e.outerRow[id], exit = e.fetchNextOuterRow(id, req)
if exit || req.IsFull() || e.outerRow[id] == nil {
return nil
}
e.hasMatch[id] = false
e.hasNull[id] = false
err = e.fetchAllInners(ctx, id)
if err != nil {
return err
}
e.innerIter[id] = chunk.NewIterator4List(e.innerList[id])
e.innerIter[id].Begin()
}
matched, isNull, err := e.joiners[id].tryToMatchInners(*e.outerRow[id], e.innerIter[id], req)
e.hasMatch[id] = e.hasMatch[id] || matched
e.hasNull[id] = e.hasNull[id] || isNull
if err != nil || req.IsFull() {
return err
}
}
}