This repository was archived by the owner on Jan 28, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 109
/
Copy pathexchange.go
319 lines (264 loc) · 5.77 KB
/
exchange.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
package plan
import (
"context"
"fmt"
"io"
"sync"
errors "gopkg.in/src-d/go-errors.v1"
"gopkg.in/src-d/go-mysql-server.v0/sql"
)
// ErrNoPartitionable is returned when no Partitionable node is found
// in the Exchange tree.
var ErrNoPartitionable = errors.NewKind("no partitionable node found in exchange tree")
// Exchange is a node that can parallelize the underlying tree iterating
// partitions concurrently.
type Exchange struct {
UnaryNode
Parallelism int
}
// NewExchange creates a new Exchange node.
func NewExchange(
parallelism int,
child sql.Node,
) *Exchange {
return &Exchange{
UnaryNode: UnaryNode{Child: child},
Parallelism: parallelism,
}
}
// RowIter implements the sql.Node interface.
func (e *Exchange) RowIter(ctx *sql.Context) (sql.RowIter, error) {
var t sql.Table
Inspect(e.Child, func(n sql.Node) bool {
if table, ok := n.(sql.Table); ok {
t = table
return false
}
return true
})
if t == nil {
return nil, ErrNoPartitionable.New()
}
partitions, err := t.Partitions(ctx)
if err != nil {
return nil, err
}
return newExchangeRowIter(ctx, e.Parallelism, partitions, e.Child), nil
}
func (e *Exchange) String() string {
p := sql.NewTreePrinter()
_ = p.WriteNode("Exchange(parallelism=%d)", e.Parallelism)
_ = p.WriteChildren(e.Child.String())
return p.String()
}
// TransformUp implements the sql.Node interface.
func (e *Exchange) TransformUp(f sql.TransformNodeFunc) (sql.Node, error) {
child, err := e.Child.TransformUp(f)
if err != nil {
return nil, err
}
return f(NewExchange(e.Parallelism, child))
}
// TransformExpressionsUp implements the sql.Node interface.
func (e *Exchange) TransformExpressionsUp(f sql.TransformExprFunc) (sql.Node, error) {
child, err := e.Child.TransformExpressionsUp(f)
if err != nil {
return nil, err
}
return NewExchange(e.Parallelism, child), nil
}
type exchangeRowIter struct {
ctx *sql.Context
parallelism int
partitions sql.PartitionIter
tree sql.Node
mut sync.Mutex
tokens chan struct{}
started bool
rows chan sql.Row
err chan error
quit chan struct{}
}
func newExchangeRowIter(
ctx *sql.Context,
parallelism int,
iter sql.PartitionIter,
tree sql.Node,
) *exchangeRowIter {
return &exchangeRowIter{
ctx: ctx,
parallelism: parallelism,
rows: make(chan sql.Row, parallelism),
err: make(chan error, 1),
started: false,
tree: tree,
partitions: iter,
quit: make(chan struct{}),
}
}
func (it *exchangeRowIter) releaseToken() {
it.mut.Lock()
defer it.mut.Unlock()
if it.tokens != nil {
it.tokens <- struct{}{}
}
}
func (it *exchangeRowIter) closeTokens() {
it.mut.Lock()
defer it.mut.Unlock()
close(it.tokens)
it.tokens = nil
}
func (it *exchangeRowIter) fillTokens() {
it.mut.Lock()
defer it.mut.Unlock()
it.tokens = make(chan struct{}, it.parallelism)
for i := 0; i < it.parallelism; i++ {
it.tokens <- struct{}{}
}
}
func (it *exchangeRowIter) start() {
it.fillTokens()
var partitions = make(chan sql.Partition)
go it.iterPartitions(partitions)
var wg sync.WaitGroup
for {
select {
case <-it.ctx.Done():
it.err <- context.Canceled
it.closeTokens()
return
case <-it.quit:
it.closeTokens()
return
case p, ok := <-partitions:
if !ok {
it.closeTokens()
wg.Wait()
close(it.rows)
return
}
wg.Add(1)
go func(p sql.Partition) {
it.iterPartition(p)
wg.Done()
it.releaseToken()
}(p)
}
}
}
func (it *exchangeRowIter) iterPartitions(ch chan<- sql.Partition) {
defer func() {
if x := recover(); x != nil {
it.err <- fmt.Errorf("mysql_server caught panic:\n%v", x)
}
close(ch)
if err := it.partitions.Close(); err != nil {
it.err <- err
}
}()
for {
select {
case <-it.ctx.Done():
it.err <- context.Canceled
return
case <-it.quit:
return
case <-it.tokens:
}
p, err := it.partitions.Next()
if err != nil {
if err != io.EOF {
it.err <- err
}
return
}
ch <- p
}
}
func (it *exchangeRowIter) iterPartition(p sql.Partition) {
node, err := it.tree.TransformUp(func(n sql.Node) (sql.Node, error) {
if t, ok := n.(sql.Table); ok {
return &exchangePartition{p, t}, nil
}
return n, nil
})
if err != nil {
it.err <- err
return
}
rows, err := node.RowIter(it.ctx)
if err != nil {
it.err <- err
return
}
defer func() {
if err := rows.Close(); err != nil {
it.err <- err
}
}()
for {
select {
case <-it.ctx.Done():
it.err <- context.Canceled
return
case <-it.quit:
return
default:
}
row, err := rows.Next()
if err != nil {
if err == io.EOF {
break
}
it.err <- err
return
}
it.rows <- row
}
}
func (it *exchangeRowIter) Next() (sql.Row, error) {
if !it.started {
it.started = true
go it.start()
}
select {
case err := <-it.err:
_ = it.Close()
return nil, err
case row, ok := <-it.rows:
if !ok {
return nil, io.EOF
}
return row, nil
}
}
func (it *exchangeRowIter) Close() error {
if it.quit == nil {
return nil
}
close(it.quit)
return nil
}
type exchangePartition struct {
sql.Partition
table sql.Table
}
var _ sql.Node = (*exchangePartition)(nil)
func (p *exchangePartition) String() string {
return fmt.Sprintf("Partition(%s)", string(p.Key()))
}
func (exchangePartition) Children() []sql.Node { return nil }
func (exchangePartition) Resolved() bool { return true }
func (p *exchangePartition) RowIter(ctx *sql.Context) (sql.RowIter, error) {
return p.table.PartitionRows(ctx, p.Partition)
}
func (p *exchangePartition) Schema() sql.Schema {
return p.table.Schema()
}
func (p *exchangePartition) TransformExpressionsUp(sql.TransformExprFunc) (sql.Node, error) {
return p, nil
}
func (p *exchangePartition) TransformUp(sql.TransformNodeFunc) (sql.Node, error) {
return p, nil
}