-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
route.go
596 lines (531 loc) · 17.5 KB
/
route.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
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
/*
Copyright 2019 The Vitess Authors.
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,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package engine
import (
"context"
"fmt"
"sort"
"strconv"
"strings"
"time"
"vitess.io/vitess/go/mysql/collations"
"vitess.io/vitess/go/vt/vtgate/evalengine"
"vitess.io/vitess/go/mysql"
"vitess.io/vitess/go/sqltypes"
"vitess.io/vitess/go/stats"
"vitess.io/vitess/go/vt/key"
"vitess.io/vitess/go/vt/srvtopo"
"vitess.io/vitess/go/vt/vterrors"
"vitess.io/vitess/go/vt/vtgate/vindexes"
querypb "vitess.io/vitess/go/vt/proto/query"
topodatapb "vitess.io/vitess/go/vt/proto/topodata"
)
var _ Primitive = (*Route)(nil)
// Route represents the instructions to route a read query to
// one or many vttablets.
type Route struct {
// TargetTabletType specifies an explicit target destination tablet type
// this is only used in conjunction with TargetDestination
TargetTabletType topodatapb.TabletType
// Query specifies the query to be executed.
Query string
// TableName specifies the tables to send the query to.
TableName string
// FieldQuery specifies the query to be executed for a GetFieldInfo request.
FieldQuery string
// OrderBy specifies the key order for merge sorting. This will be
// set only for scatter queries that need the results to be
// merge-sorted.
OrderBy []OrderByParams
// TruncateColumnCount specifies the number of columns to return
// in the final result. Rest of the columns are truncated
// from the result received. If 0, no truncation happens.
TruncateColumnCount int
// QueryTimeout contains the optional timeout (in milliseconds) to apply to this query
QueryTimeout int
// ScatterErrorsAsWarnings is true if results should be returned even if some shards have an error
ScatterErrorsAsWarnings bool
// RoutingParameters parameters required for query routing.
*RoutingParameters
// NoRoutesSpecialHandling will make the route send a query to arbitrary shard if the routing logic can't find
// the correct shard. This is important for queries where no matches does not mean empty result - examples would be:
// select count(*) from tbl where lookupColumn = 'not there'
// select exists(<subq>)
NoRoutesSpecialHandling bool
// Route does not take inputs
noInputs
// Route does not need transaction handling
noTxNeeded
}
// NewSimpleRoute creates a Route with the bare minimum of parameters.
func NewSimpleRoute(opcode Opcode, keyspace *vindexes.Keyspace) *Route {
return &Route{
RoutingParameters: &RoutingParameters{
Opcode: opcode,
Keyspace: keyspace,
},
}
}
// NewRoute creates a Route.
func NewRoute(opcode Opcode, keyspace *vindexes.Keyspace, query, fieldQuery string) *Route {
return &Route{
RoutingParameters: &RoutingParameters{
Opcode: opcode,
Keyspace: keyspace,
},
Query: query,
FieldQuery: fieldQuery,
}
}
// OrderByParams specifies the parameters for ordering.
// This is used for merge-sorting scatter queries.
type OrderByParams struct {
Col int
// WeightStringCol is the weight_string column that will be used for sorting.
// It is set to -1 if such a column is not added to the query
WeightStringCol int
Desc bool
StarColFixedIndex int
// v3 specific boolean. Used to also add weight strings originating from GroupBys to the Group by clause
FromGroupBy bool
// Type for knowing if the collation is relevant
Type querypb.Type
// Collation ID for comparison using collation
CollationID collations.ID
}
// String returns a string. Used for plan descriptions
func (obp OrderByParams) String() string {
val := strconv.Itoa(obp.Col)
if obp.StarColFixedIndex > obp.Col {
val = strconv.Itoa(obp.StarColFixedIndex)
}
if obp.WeightStringCol != -1 && obp.WeightStringCol != obp.Col {
val = fmt.Sprintf("(%s|%d)", val, obp.WeightStringCol)
}
if obp.Desc {
val += " DESC"
} else {
val += " ASC"
}
if sqltypes.IsText(obp.Type) && obp.CollationID != collations.Unknown {
collation := obp.CollationID.Get()
val += " COLLATE " + collation.Name()
}
return val
}
var (
partialSuccessScatterQueries = stats.NewCounter("PartialSuccessScatterQueries", "Count of partially successful scatter queries")
)
// RouteType returns a description of the query routing type used by the primitive
func (route *Route) RouteType() string {
return route.Opcode.String()
}
// GetKeyspaceName specifies the Keyspace that this primitive routes to.
func (route *Route) GetKeyspaceName() string {
return route.Keyspace.Name
}
// GetTableName specifies the table that this primitive routes to.
func (route *Route) GetTableName() string {
return route.TableName
}
// SetTruncateColumnCount sets the truncate column count.
func (route *Route) SetTruncateColumnCount(count int) {
route.TruncateColumnCount = count
}
// TryExecute performs a non-streaming exec.
func (route *Route) TryExecute(ctx context.Context, vcursor VCursor, bindVars map[string]*querypb.BindVariable, wantfields bool) (*sqltypes.Result, error) {
ctx, cancelFunc := addQueryTimeout(ctx, vcursor, route.QueryTimeout)
defer cancelFunc()
qr, err := route.executeInternal(ctx, vcursor, bindVars, wantfields)
if err != nil {
return nil, err
}
return qr.Truncate(route.TruncateColumnCount), nil
}
// addQueryTimeout adds a query timeout to the context it receives and returns the modified context along with the cancel function.
func addQueryTimeout(ctx context.Context, vcursor VCursor, queryTimeout int) (context.Context, context.CancelFunc) {
timeout := vcursor.Session().GetQueryTimeout(queryTimeout)
if timeout != 0 {
return context.WithTimeout(ctx, time.Duration(timeout)*time.Millisecond)
}
return ctx, func() {}
}
type cxtKey int
const (
IgnoreReserveTxn cxtKey = iota
)
func (route *Route) executeInternal(
ctx context.Context,
vcursor VCursor,
bindVars map[string]*querypb.BindVariable,
wantfields bool,
) (*sqltypes.Result, error) {
rss, bvs, err := route.findRoute(ctx, vcursor, bindVars)
if err != nil {
return nil, err
}
return route.executeShards(ctx, vcursor, bindVars, wantfields, rss, bvs)
}
func (route *Route) executeShards(
ctx context.Context,
vcursor VCursor,
bindVars map[string]*querypb.BindVariable,
wantfields bool,
rss []*srvtopo.ResolvedShard,
bvs []map[string]*querypb.BindVariable,
) (*sqltypes.Result, error) {
// Select Next - sequence query does not need to be executed in a dedicated connection (reserved or transaction)
if route.Opcode == Next {
ctx = context.WithValue(ctx, IgnoreReserveTxn, true)
}
// No route.
if len(rss) == 0 {
if !route.NoRoutesSpecialHandling {
if wantfields {
return route.GetFields(ctx, vcursor, bindVars)
}
return &sqltypes.Result{}, nil
}
// Here we were earlier returning no rows back.
// But this was incorrect for queries like select count(*) from user where name='x'
// If the lookup_vindex for name, returns no shards, we still want a result from here
// with a single row with 0 as the output.
// However, at this level it is hard to distinguish between the cases that need a result
// and the ones that don't. So, we are sending the query to any shard! This is safe because
// the query contains a predicate that make it not match any rows on that shard. (If they did,
// we should have gotten that shard back already from findRoute)
var err error
rss, bvs, err = route.anyShard(ctx, vcursor, bindVars)
if err != nil {
return nil, err
}
}
queries := getQueries(route.Query, bvs)
result, errs := vcursor.ExecuteMultiShard(ctx, route, rss, queries, false /* rollbackOnError */, false /* canAutocommit */)
if errs != nil {
errs = filterOutNilErrors(errs)
if !route.ScatterErrorsAsWarnings || len(errs) == len(rss) {
return nil, vterrors.Aggregate(errs)
}
partialSuccessScatterQueries.Add(1)
for _, err := range errs {
serr := mysql.NewSQLErrorFromError(err).(*mysql.SQLError)
vcursor.Session().RecordWarning(&querypb.QueryWarning{Code: uint32(serr.Num), Message: err.Error()})
}
}
if len(route.OrderBy) == 0 {
return result, nil
}
return route.sort(result)
}
func filterOutNilErrors(errs []error) []error {
var errors []error
for _, err := range errs {
if err != nil {
errors = append(errors, err)
}
}
return errors
}
// TryStreamExecute performs a streaming exec.
func (route *Route) TryStreamExecute(
ctx context.Context,
vcursor VCursor,
bindVars map[string]*querypb.BindVariable,
wantfields bool,
callback func(*sqltypes.Result) error,
) error {
if route.QueryTimeout != 0 {
var cancel context.CancelFunc
ctx, cancel = context.WithTimeout(ctx, time.Duration(route.QueryTimeout)*time.Millisecond)
defer cancel()
}
rss, bvs, err := route.findRoute(ctx, vcursor, bindVars)
if err != nil {
return err
}
return route.streamExecuteShards(ctx, vcursor, bindVars, wantfields, callback, rss, bvs)
}
func (route *Route) streamExecuteShards(
ctx context.Context,
vcursor VCursor,
bindVars map[string]*querypb.BindVariable,
wantfields bool,
callback func(*sqltypes.Result) error,
rss []*srvtopo.ResolvedShard,
bvs []map[string]*querypb.BindVariable,
) error {
// No route.
if len(rss) == 0 {
if !route.NoRoutesSpecialHandling {
if wantfields {
r, err := route.GetFields(ctx, vcursor, bindVars)
if err != nil {
return err
}
return callback(r)
}
return nil
}
// Here we were earlier returning no rows back.
// But this was incorrect for queries like select count(*) from user where name='x'
// If the lookup_vindex for name, returns no shards, we still want a result from here
// with a single row with 0 as the output.
// However, at this level it is hard to distinguish between the cases that need a result
// and the ones that don't. So, we are sending the query to any shard! This is safe because
// the query contains a predicate that make it not match any rows on that shard. (If they did,
// we should have gotten that shard back already from findRoute)
var err error
rss, bvs, err = route.anyShard(ctx, vcursor, bindVars)
if err != nil {
return err
}
}
if len(route.OrderBy) == 0 {
errs := vcursor.StreamExecuteMulti(ctx, route, route.Query, rss, bvs, false /* rollbackOnError */, false /* autocommit */, func(qr *sqltypes.Result) error {
return callback(qr.Truncate(route.TruncateColumnCount))
})
if len(errs) > 0 {
if !route.ScatterErrorsAsWarnings || len(errs) == len(rss) {
return vterrors.Aggregate(errs)
}
partialSuccessScatterQueries.Add(1)
for _, err := range errs {
sErr := mysql.NewSQLErrorFromError(err).(*mysql.SQLError)
vcursor.Session().RecordWarning(&querypb.QueryWarning{Code: uint32(sErr.Num), Message: err.Error()})
}
}
return nil
}
// There is an order by. We have to merge-sort.
return route.mergeSort(ctx, vcursor, bindVars, wantfields, callback, rss, bvs)
}
func (route *Route) mergeSort(
ctx context.Context,
vcursor VCursor,
bindVars map[string]*querypb.BindVariable,
wantfields bool,
callback func(*sqltypes.Result) error,
rss []*srvtopo.ResolvedShard,
bvs []map[string]*querypb.BindVariable,
) error {
prims := make([]StreamExecutor, 0, len(rss))
for i, rs := range rss {
prims = append(prims, &shardRoute{
query: route.Query,
rs: rs,
bv: bvs[i],
primitive: route,
})
}
ms := MergeSort{
Primitives: prims,
OrderBy: route.OrderBy,
ScatterErrorsAsWarnings: route.ScatterErrorsAsWarnings,
}
return vcursor.StreamExecutePrimitive(ctx, &ms, bindVars, wantfields, func(qr *sqltypes.Result) error {
return callback(qr.Truncate(route.TruncateColumnCount))
})
}
// GetFields fetches the field info.
func (route *Route) GetFields(ctx context.Context, vcursor VCursor, bindVars map[string]*querypb.BindVariable) (*sqltypes.Result, error) {
var rs *srvtopo.ResolvedShard
// Use an existing shard session
sss := vcursor.Session().ShardSession()
for _, ss := range sss {
if ss.Target.Keyspace == route.Keyspace.Name {
rs = ss
break
}
}
// If not find, then pick any shard.
if rs == nil {
rss, _, err := vcursor.ResolveDestinations(ctx, route.Keyspace.Name, nil, []key.Destination{key.DestinationAnyShard{}})
if err != nil {
return nil, err
}
if len(rss) != 1 {
// This code is unreachable. It's just a sanity check.
return nil, fmt.Errorf("no shards for keyspace: %s", route.Keyspace.Name)
}
rs = rss[0]
}
qr, err := execShard(ctx, route, vcursor, route.FieldQuery, bindVars, rs, false /* rollbackOnError */, false /* canAutocommit */)
if err != nil {
return nil, err
}
return qr.Truncate(route.TruncateColumnCount), nil
}
func (route *Route) sort(in *sqltypes.Result) (*sqltypes.Result, error) {
var err error
// Since Result is immutable, we make a copy.
// The copy can be shallow because we won't be changing
// the contents of any row.
out := in.ShallowCopy()
comparers := extractSlices(route.OrderBy)
sort.Slice(out.Rows, func(i, j int) bool {
var cmp int
if err != nil {
return true
}
// If there are any errors below, the function sets
// the external err and returns true. Once err is set,
// all subsequent calls return true. This will make
// Slice think that all elements are in the correct
// order and return more quickly.
for _, c := range comparers {
cmp, err = c.compare(out.Rows[i], out.Rows[j])
if err != nil {
return true
}
if cmp == 0 {
continue
}
return cmp < 0
}
return true
})
return out.Truncate(route.TruncateColumnCount), err
}
func (route *Route) description() PrimitiveDescription {
other := map[string]any{
"Query": route.Query,
"Table": route.GetTableName(),
"FieldQuery": route.FieldQuery,
}
if route.Vindex != nil {
other["Vindex"] = route.Vindex.String()
}
if route.Values != nil {
formattedValues := make([]string, 0, len(route.Values))
for _, value := range route.Values {
formattedValues = append(formattedValues, evalengine.FormatExpr(value))
}
other["Values"] = formattedValues
}
if len(route.SysTableTableSchema) != 0 {
sysTabSchema := "["
for idx, tableSchema := range route.SysTableTableSchema {
if idx != 0 {
sysTabSchema += ", "
}
sysTabSchema += evalengine.FormatExpr(tableSchema)
}
sysTabSchema += "]"
other["SysTableTableSchema"] = sysTabSchema
}
if len(route.SysTableTableName) != 0 {
var sysTableName []string
for k, v := range route.SysTableTableName {
sysTableName = append(sysTableName, k+":"+evalengine.FormatExpr(v))
}
sort.Strings(sysTableName)
other["SysTableTableName"] = "[" + strings.Join(sysTableName, ", ") + "]"
}
orderBy := GenericJoin(route.OrderBy, orderByToString)
if orderBy != "" {
other["OrderBy"] = orderBy
}
if route.TruncateColumnCount > 0 {
other["ResultColumns"] = route.TruncateColumnCount
}
if route.ScatterErrorsAsWarnings {
other["ScatterErrorsAsWarnings"] = true
}
if route.QueryTimeout > 0 {
other["QueryTimeout"] = route.QueryTimeout
}
return PrimitiveDescription{
OperatorType: "Route",
Variant: route.Opcode.String(),
Keyspace: route.Keyspace,
TargetDestination: route.TargetDestination,
Other: other,
}
}
func (route *Route) executeAfterLookup(
ctx context.Context,
vcursor VCursor,
bindVars map[string]*querypb.BindVariable,
wantfields bool,
ids []sqltypes.Value,
dest []key.Destination,
) (*sqltypes.Result, error) {
protoIds := make([]*querypb.Value, 0, len(ids))
for _, id := range ids {
protoIds = append(protoIds, sqltypes.ValueToProto(id))
}
rss, _, err := vcursor.ResolveDestinations(ctx, route.Keyspace.Name, protoIds, dest)
if err != nil {
return nil, err
}
bvs := make([]map[string]*querypb.BindVariable, len(rss))
for i := range bvs {
bvs[i] = bindVars
}
return route.executeShards(ctx, vcursor, bindVars, wantfields, rss, bvs)
}
func (route *Route) streamExecuteAfterLookup(
ctx context.Context,
vcursor VCursor,
bindVars map[string]*querypb.BindVariable,
wantfields bool,
callback func(*sqltypes.Result) error,
ids []sqltypes.Value,
dest []key.Destination,
) error {
protoIds := make([]*querypb.Value, 0, len(ids))
for _, id := range ids {
protoIds = append(protoIds, sqltypes.ValueToProto(id))
}
rss, _, err := vcursor.ResolveDestinations(ctx, route.Keyspace.Name, protoIds, dest)
if err != nil {
return err
}
bvs := make([]map[string]*querypb.BindVariable, len(rss))
for i := range bvs {
bvs[i] = bindVars
}
return route.streamExecuteShards(ctx, vcursor, bindVars, wantfields, callback, rss, bvs)
}
func execShard(
ctx context.Context,
primitive Primitive,
vcursor VCursor,
query string,
bindVars map[string]*querypb.BindVariable,
rs *srvtopo.ResolvedShard,
rollbackOnError, canAutocommit bool,
) (*sqltypes.Result, error) {
autocommit := canAutocommit && vcursor.AutocommitApproval()
result, errs := vcursor.ExecuteMultiShard(ctx, primitive, []*srvtopo.ResolvedShard{rs}, []*querypb.BoundQuery{
{
Sql: query,
BindVariables: bindVars,
},
}, rollbackOnError, autocommit)
return result, vterrors.Aggregate(errs)
}
func getQueries(query string, bvs []map[string]*querypb.BindVariable) []*querypb.BoundQuery {
queries := make([]*querypb.BoundQuery, len(bvs))
for i, bv := range bvs {
queries[i] = &querypb.BoundQuery{
Sql: query,
BindVariables: bv,
}
}
return queries
}
func orderByToString(in any) string {
return in.(OrderByParams).String()
}