-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmultiquery.go
258 lines (197 loc) · 4.98 KB
/
multiquery.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
// Copyright (c) 2019, Mohlmann Solutions SRL. All rights reserved.
// Use of this source code is governed by a License that can be found in the LICENSE file.
// SPDX-License-Identifier: BSD-3-Clause
package multidb
import (
"context"
"database/sql"
"errors"
"fmt"
"strings"
"sync"
)
// MultiError is a collection of errors which can arise from parallel query execution.
type MultiError struct {
Errors []error
}
func (me *MultiError) Error() string {
if len(me.Errors) == 0 {
return "Unknown error"
}
var bs strings.Builder
bs.WriteString("Multiple errors:")
for i, err := range me.Errors {
bs.WriteString(fmt.Sprintf(" %d: %v;", i+1, err))
}
return bs.String()
}
// checkMultiError returns a single error if all errors in the MultiError are the same.
// Otherwise, it returns the MultiError containing the multiple errors.
// Returns nil if the are no errors.
func checkMultiError(errs []error) error {
var first error
for _, err := range errs {
if ne := new(NodeError); errors.As(err, &ne) {
err = errors.Unwrap(ne)
}
switch {
case err == nil:
break
case first == nil:
first = err
case err != first:
target := &MultiError{
Errors: make([]error, 0, len(errs)),
}
// Purge nil entries
for _, err := range errs {
if err != nil {
target.Errors = append(target.Errors, err)
}
}
return target
}
}
return first
}
// ErrCallbackFunc is called by the individual routines
// executing queries on multiple nodes.
// The function will be called concurently.
type ErrCallbackFunc func(error)
// NodeError is passed to ErrCallbackFunc functions in order to
// destinguish between DB nodes in concurrent query executions.
type NodeError struct {
name string
wrapped error
}
func (ne *NodeError) Error() string {
return fmt.Sprintf("Node %s: %v", ne.name, ne.wrapped)
}
// Name of the node that experienced the error
func (ne *NodeError) Name() string {
return ne.name
}
func (ne *NodeError) Unwrap() error {
return ne.wrapped
}
type executor interface {
ExecContext(ctx context.Context, query string, args ...interface{}) (sql.Result, error)
QueryContext(ctx context.Context, query string, args ...interface{}) (*sql.Rows, error)
QueryRowContext(ctx context.Context, query string, args ...interface{}) *sql.Row
}
func multiExec(ctx context.Context, wg *sync.WaitGroup, xs map[string]executor, errCallback ErrCallbackFunc, query string, args ...interface{}) (sql.Result, error) {
ctx, cancel := context.WithCancel(ctx)
defer cancel()
type result struct {
res sql.Result
err error
}
rc := make(chan result, len(xs))
if wg != nil {
wg.Add(len(xs))
}
for name, x := range xs {
go func(name string, x executor) {
var r result
r.res, r.err = x.ExecContext(ctx, query, args...)
if r.err != nil {
r.err = &NodeError{name, r.err}
if errCallback != nil {
errCallback(r.err)
}
}
rc <- r
if wg != nil {
wg.Done()
}
}(name, x)
}
var errs []error
for i := len(xs); i > 0; i-- {
r := <-rc
if r.err == nil {
return r.res, nil
}
errs = append(errs, r.err)
}
return nil, checkMultiError(errs)
}
func multiQuery(ctx context.Context, wg *sync.WaitGroup, xs map[string]executor, errCallback ErrCallbackFunc, query string, args ...interface{}) (*sql.Rows, error) {
type result struct {
rows *sql.Rows
err error
}
// Buffered channel works ~40% faster, regardless of draining.
rc := make(chan result, len(xs))
if wg != nil {
wg.Add(len(xs))
}
for name, x := range xs {
go func(name string, x executor) {
var r result
r.rows, r.err = x.QueryContext(ctx, query, args...)
if r.err != nil {
r.err = &NodeError{name, r.err}
if errCallback != nil {
errCallback(r.err)
}
}
rc <- r
if wg != nil {
wg.Done()
}
}(name, x)
}
var errs []error
for i := len(xs); i > 0; i-- {
r := <-rc
if r.err == nil {
// Drain channel and close unused Rows
go func(i int) {
for ; i > 0; i-- {
if r := <-rc; r.rows != nil {
r.rows.Close()
}
}
}(i)
return r.rows, nil
}
errs = append(errs, r.err)
}
return nil, checkMultiError(errs)
}
func multiQueryRow(ctx context.Context, wg *sync.WaitGroup, xs map[string]executor, errCallback ErrCallbackFunc, query string, args ...interface{}) (row *sql.Row) {
rc := make(chan *sql.Row, len(xs))
if wg != nil {
wg.Add(len(xs))
}
for name, x := range xs {
go func(name string, x executor) {
row := x.QueryRowContext(ctx, query, args...)
if row != nil { // Prevent panic in benchmarks
if err := row.Err(); err != nil && errCallback != nil {
errCallback(&NodeError{name, err})
}
}
rc <- row
if wg != nil {
wg.Done()
}
}(name, x)
}
for i := len(xs); i > 0; i-- {
row = <-rc
if row != nil && row.Err() == nil {
// Drain channel and close unused Rows
go func() {
for ; i > 0; i-- {
if row := <-rc; row != nil {
row.Scan(&sql.RawBytes{}) // hack to trigger Rows.Close()
}
}
}()
break
}
}
return row
}