forked from pingcap/tidb
-
Notifications
You must be signed in to change notification settings - Fork 1
/
projection.go
364 lines (318 loc) · 11.4 KB
/
projection.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
// Copyright 2018 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 (
"time"
"github.com/pingcap/tidb/expression"
"github.com/pingcap/tidb/sessionctx"
"github.com/pingcap/tidb/util/chunk"
"github.com/pkg/errors"
"golang.org/x/net/context"
)
// This file contains the implementation of the physical Projection Operator:
// https://en.wikipedia.org/wiki/Projection_(relational_algebra)
//
// NOTE:
// 1. The number of "projectionWorker" is controlled by the global session
// variable "tidb_projection_concurrency".
// 2. Unparallel version is used when one of the following situations occurs:
// a. "tidb_projection_concurrency" is set to 0.
// b. The estimated input size is smaller than "tidb_max_chunk_size".
// c. This projection can not be executed vectorially.
type projectionInput struct {
chk *chunk.Chunk
targetWorker *projectionWorker
}
type projectionOutput struct {
chk *chunk.Chunk
done chan error
}
// ProjectionExec implements the physical Projection Operator:
// https://en.wikipedia.org/wiki/Projection_(relational_algebra)
type ProjectionExec struct {
baseExecutor
evaluatorSuit *expression.EvaluatorSuit
calculateNoDelay bool
prepared bool
finishCh chan struct{}
outputCh chan *projectionOutput
fetcher projectionInputFetcher
numWorkers int64
workers []*projectionWorker
childResult *chunk.Chunk
}
// Open implements the Executor Open interface.
func (e *ProjectionExec) Open(ctx context.Context) error {
if err := e.baseExecutor.Open(ctx); err != nil {
return errors.Trace(err)
}
e.prepared = false
// For now a Projection can not be executed vectorially only because it
// contains "SetVar" or "GetVar" functions, in this scenario this
// Projection can not be executed parallelly.
if e.numWorkers > 0 && !e.evaluatorSuit.Vectorizable() {
e.numWorkers = 0
}
if e.isUnparallelExec() {
e.childResult = e.children[0].newFirstChunk()
}
return nil
}
// Next implements the Executor Next interface.
//
// Here we explain the execution flow of the parallel projection implementation.
// There are 3 main components:
// 1. "projectionInputFetcher": Fetch input "Chunk" from child.
// 2. "projectionWorker": Do the projection work.
// 3. "ProjectionExec.Next": Return result to parent.
//
// 1. "projectionInputFetcher" gets its input and output resources from its
// "inputCh" and "outputCh" channel, once the input and output resources are
// abtained, it fetches child's result into "input.chk" and:
// a. Dispatches this input to the worker specified in "input.targetWorker"
// b. Dispatches this output to the main thread: "ProjectionExec.Next"
// c. Dispatches this output to the worker specified in "input.targetWorker"
// It is finished and exited once:
// a. There is no more input from child.
// b. "ProjectionExec" close the "globalFinishCh"
//
// 2. "projectionWorker" gets its input and output resources from its
// "inputCh" and "outputCh" channel, once the input and output resources are
// abtained, it calculates the projection result use "input.chk" as the input
// and "output.chk" as the output, once the calculation is done, it:
// a. Sends "nil" or error to "output.done" to mark this input is finished.
// b. Returns the "input" resource to "projectionInputFetcher.inputCh"
// They are finished and exited once:
// a. "ProjectionExec" closes the "globalFinishCh"
//
// 3. "ProjectionExec.Next" gets its output resources from its "outputCh" channel.
// After receiving an output from "outputCh", it should wait to receive a "nil"
// or error from "output.done" channel. Once a "nil" or error is received:
// a. Returns this output to its parent
// b. Returns the "output" resource to "projectionInputFetcher.outputCh"
//
// +-----------+----------------------+--------------------------+
// | | | |
// | +--------+---------+ +--------+---------+ +--------+---------+
// | | projectionWorker | + projectionWorker | ... + projectionWorker |
// | +------------------+ +------------------+ +------------------+
// | ^ ^ ^ ^ ^ ^
// | | | | | | |
// | inputCh outputCh inputCh outputCh inputCh outputCh
// | ^ ^ ^ ^ ^ ^
// | | | | | | |
// | | |
// | | +----------------->outputCh
// | | | |
// | | | v
// | +-------+-------+--------+ +---------------------+
// | | projectionInputFetcher | | ProjectionExec.Next |
// | +------------------------+ +---------+-----------+
// | ^ ^ |
// | | | |
// | inputCh outputCh |
// | ^ ^ |
// | | | |
// +------------------------------+ +----------------------+
//
func (e *ProjectionExec) Next(ctx context.Context, chk *chunk.Chunk) error {
if e.runtimeStats != nil {
start := time.Now()
defer func() { e.runtimeStats.Record(time.Now().Sub(start), chk.NumRows()) }()
}
chk.GrowAndReset(e.maxChunkSize)
if e.isUnparallelExec() {
return errors.Trace(e.unParallelExecute(ctx, chk))
}
return errors.Trace(e.parallelExecute(ctx, chk))
}
func (e *ProjectionExec) isUnparallelExec() bool {
return e.numWorkers <= 0
}
func (e *ProjectionExec) unParallelExecute(ctx context.Context, chk *chunk.Chunk) error {
err := e.children[0].Next(ctx, e.childResult)
if err != nil {
return errors.Trace(err)
}
err = e.evaluatorSuit.Run(e.ctx, e.childResult, chk)
return errors.Trace(err)
}
func (e *ProjectionExec) parallelExecute(ctx context.Context, chk *chunk.Chunk) error {
if !e.prepared {
e.prepare(ctx)
e.prepared = true
}
output, ok := <-e.outputCh
if !ok {
return nil
}
err := <-output.done
if err != nil {
return errors.Trace(err)
}
chk.SwapColumns(output.chk)
e.fetcher.outputCh <- output
return nil
}
func (e *ProjectionExec) prepare(ctx context.Context) {
e.finishCh = make(chan struct{})
e.outputCh = make(chan *projectionOutput, e.numWorkers)
// Initialize projectionInputFetcher.
e.fetcher = projectionInputFetcher{
child: e.children[0],
globalFinishCh: e.finishCh,
globalOutputCh: e.outputCh,
inputCh: make(chan *projectionInput, e.numWorkers),
outputCh: make(chan *projectionOutput, e.numWorkers),
}
// Initialize projectionWorker.
e.workers = make([]*projectionWorker, 0, e.numWorkers)
for i := int64(0); i < e.numWorkers; i++ {
e.workers = append(e.workers, &projectionWorker{
sctx: e.ctx,
evaluatorSuit: e.evaluatorSuit,
globalFinishCh: e.finishCh,
inputGiveBackCh: e.fetcher.inputCh,
inputCh: make(chan *projectionInput, 1),
outputCh: make(chan *projectionOutput, 1),
})
e.fetcher.inputCh <- &projectionInput{
chk: e.children[0].newFirstChunk(),
targetWorker: e.workers[i],
}
e.fetcher.outputCh <- &projectionOutput{
chk: e.newFirstChunk(),
done: make(chan error, 1),
}
}
go e.fetcher.run(ctx)
for i := range e.workers {
go e.workers[i].run(ctx)
}
}
// Close implements the Executor Close interface.
func (e *ProjectionExec) Close() error {
if e.isUnparallelExec() {
e.childResult = nil
}
if e.outputCh != nil {
close(e.finishCh)
// Wait for "projectionInputFetcher" to finish and exit.
for range e.outputCh {
}
e.outputCh = nil
}
return errors.Trace(e.baseExecutor.Close())
}
type projectionInputFetcher struct {
child Executor
globalFinishCh <-chan struct{}
globalOutputCh chan<- *projectionOutput
inputCh chan *projectionInput
outputCh chan *projectionOutput
}
// run gets projectionInputFetcher's input and output resources from its
// "inputCh" and "outputCh" channel, once the input and output resources are
// abtained, it fetches child's result into "input.chk" and:
// a. Dispatches this input to the worker specified in "input.targetWorker"
// b. Dispatches this output to the main thread: "ProjectionExec.Next"
// c. Dispatches this output to the worker specified in "input.targetWorker"
//
// It is finished and exited once:
// a. There is no more input from child.
// b. "ProjectionExec" close the "globalFinishCh"
func (f *projectionInputFetcher) run(ctx context.Context) {
defer func() {
close(f.globalOutputCh)
}()
for {
input := readProjectionInput(f.inputCh, f.globalFinishCh)
if input == nil {
return
}
targetWorker := input.targetWorker
output := readProjectionOutput(f.outputCh, f.globalFinishCh)
if output == nil {
return
}
f.globalOutputCh <- output
err := f.child.Next(ctx, input.chk)
if err != nil || input.chk.NumRows() == 0 {
output.done <- errors.Trace(err)
return
}
targetWorker.inputCh <- input
targetWorker.outputCh <- output
}
}
type projectionWorker struct {
sctx sessionctx.Context
evaluatorSuit *expression.EvaluatorSuit
globalFinishCh <-chan struct{}
inputGiveBackCh chan<- *projectionInput
// channel "input" and "output" is :
// a. initialized by "ProjectionExec.prepare"
// b. written by "projectionInputFetcher.run"
// c. read by "projectionWorker.run"
inputCh chan *projectionInput
outputCh chan *projectionOutput
}
// run gets projectionWorker's input and output resources from its
// "inputCh" and "outputCh" channel, once the input and output resources are
// abtained, it calculate the projection result use "input.chk" as the input
// and "output.chk" as the output, once the calculation is done, it:
// a. Sends "nil" or error to "output.done" to mark this input is finished.
// b. Returns the "input" resource to "projectionInputFetcher.inputCh".
//
// It is finished and exited once:
// a. "ProjectionExec" closes the "globalFinishCh".
func (w *projectionWorker) run(ctx context.Context) {
for {
input := readProjectionInput(w.inputCh, w.globalFinishCh)
if input == nil {
return
}
output := readProjectionOutput(w.outputCh, w.globalFinishCh)
if output == nil {
return
}
err := w.evaluatorSuit.Run(w.sctx, input.chk, output.chk)
output.done <- errors.Trace(err)
if err != nil {
return
}
w.inputGiveBackCh <- input
}
}
func readProjectionInput(inputCh <-chan *projectionInput, finishCh <-chan struct{}) *projectionInput {
select {
case <-finishCh:
return nil
case input, ok := <-inputCh:
if !ok {
return nil
}
return input
}
}
func readProjectionOutput(outputCh <-chan *projectionOutput, finishCh <-chan struct{}) *projectionOutput {
select {
case <-finishCh:
return nil
case output, ok := <-outputCh:
if !ok {
return nil
}
return output
}
}