forked from vitessio/vitess
-
Notifications
You must be signed in to change notification settings - Fork 0
/
scatter_conn.go
608 lines (545 loc) · 19.9 KB
/
scatter_conn.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
597
598
599
600
601
602
603
604
605
606
607
608
/*
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 vtgate
import (
"flag"
"io"
"sync"
"time"
topodatapb "vitess.io/vitess/go/vt/proto/topodata"
"vitess.io/vitess/go/vt/vttablet/queryservice"
"golang.org/x/net/context"
"vitess.io/vitess/go/sqltypes"
"vitess.io/vitess/go/stats"
"vitess.io/vitess/go/vt/concurrency"
"vitess.io/vitess/go/vt/discovery"
querypb "vitess.io/vitess/go/vt/proto/query"
vtgatepb "vitess.io/vitess/go/vt/proto/vtgate"
vtrpcpb "vitess.io/vitess/go/vt/proto/vtrpc"
"vitess.io/vitess/go/vt/srvtopo"
"vitess.io/vitess/go/vt/topo/topoproto"
"vitess.io/vitess/go/vt/vterrors"
)
var (
messageStreamGracePeriod = flag.Duration("message_stream_grace_period", 30*time.Second, "the amount of time to give for a vttablet to resume if it ends a message stream, usually because of a reparent.")
)
// ScatterConn is used for executing queries across
// multiple shard level connections.
type ScatterConn struct {
timings *stats.MultiTimings
tabletCallErrorCount *stats.CountersWithMultiLabels
txConn *TxConn
gateway Gateway
legacyHealthCheck discovery.LegacyHealthCheck
}
// shardActionFunc defines the contract for a shard action
// outside of a transaction. Every such function executes the
// necessary action on a shard, sends the results to sResults, and
// return an error if any. multiGo is capable of executing
// multiple shardActionFunc actions in parallel and
// consolidating the results and errors for the caller.
type shardActionFunc func(rs *srvtopo.ResolvedShard, i int) error
// shardActionTransactionFunc defines the contract for a shard action
// that may be in a transaction. Every such function executes the
// necessary action on a shard (with an optional Begin call), aggregates
// the results, and return an error if any.
// multiGoTransaction is capable of executing multiple
// shardActionTransactionFunc actions in parallel and consolidating
// the results and errors for the caller.
type shardActionTransactionFunc func(rs *srvtopo.ResolvedShard, i int, shouldBegin bool, transactionID int64, alias *topodatapb.TabletAlias) (int64, *topodatapb.TabletAlias, error)
// NewLegacyScatterConn creates a new ScatterConn.
func NewLegacyScatterConn(statsName string, txConn *TxConn, gw Gateway, hc discovery.LegacyHealthCheck) *ScatterConn {
tabletCallErrorCountStatsName := ""
if statsName != "" {
tabletCallErrorCountStatsName = statsName + "ErrorCount"
}
return &ScatterConn{
timings: stats.NewMultiTimings(
statsName,
"Scatter connection timings",
[]string{"Operation", "Keyspace", "ShardName", "DbType"}),
tabletCallErrorCount: stats.NewCountersWithMultiLabels(
tabletCallErrorCountStatsName,
"Error count from tablet calls in scatter conns",
[]string{"Operation", "Keyspace", "ShardName", "DbType"}),
txConn: txConn,
gateway: gw,
legacyHealthCheck: hc,
}
}
// NewScatterConn creates a new ScatterConn.
func NewScatterConn(statsName string, txConn *TxConn, gw *TabletGateway) *ScatterConn {
// this only works with TabletGateway
tabletCallErrorCountStatsName := ""
if statsName != "" {
tabletCallErrorCountStatsName = statsName + "ErrorCount"
}
return &ScatterConn{
timings: stats.NewMultiTimings(
statsName,
"Scatter connection timings",
[]string{"Operation", "Keyspace", "ShardName", "DbType"}),
tabletCallErrorCount: stats.NewCountersWithMultiLabels(
tabletCallErrorCountStatsName,
"Error count from tablet calls in scatter conns",
[]string{"Operation", "Keyspace", "ShardName", "DbType"}),
txConn: txConn,
gateway: gw,
// gateway has a reference to healthCheck so we don't need this any more
legacyHealthCheck: nil,
}
}
func (stc *ScatterConn) startAction(name string, target *querypb.Target) (time.Time, []string) {
statsKey := []string{name, target.Keyspace, target.Shard, topoproto.TabletTypeLString(target.TabletType)}
startTime := time.Now()
return startTime, statsKey
}
func (stc *ScatterConn) endAction(startTime time.Time, allErrors *concurrency.AllErrorRecorder, statsKey []string, err *error, session *SafeSession) {
if *err != nil {
allErrors.RecordError(*err)
// Don't increment the error counter for duplicate
// keys or bad queries, as those errors are caused by
// client queries and are not VTGate's fault.
ec := vterrors.Code(*err)
if ec != vtrpcpb.Code_ALREADY_EXISTS && ec != vtrpcpb.Code_INVALID_ARGUMENT {
stc.tabletCallErrorCount.Add(statsKey, 1)
}
if ec == vtrpcpb.Code_RESOURCE_EXHAUSTED || ec == vtrpcpb.Code_ABORTED {
session.SetRollback()
}
}
stc.timings.Record(statsKey, startTime)
}
// Execute executes a non-streaming query on the specified shards.
func (stc *ScatterConn) Execute(
ctx context.Context,
query string,
bindVars map[string]*querypb.BindVariable,
rss []*srvtopo.ResolvedShard,
session *SafeSession,
notInTransaction bool,
options *querypb.ExecuteOptions,
autocommit bool,
) (*sqltypes.Result, error) {
// mu protects qr
var mu sync.Mutex
qr := new(sqltypes.Result)
allErrors := stc.multiGoTransaction(
ctx,
"Execute",
rss,
session,
notInTransaction,
func(rs *srvtopo.ResolvedShard, i int, shouldBegin bool, transactionID int64, alias *topodatapb.TabletAlias) (int64, *topodatapb.TabletAlias, error) {
var (
innerqr *sqltypes.Result
err error
opts *querypb.ExecuteOptions
)
switch {
case autocommit:
// As this is auto-commit, the transactionID is supposed to be zero.
if transactionID != 0 {
return 0, nil, vterrors.Errorf(vtrpcpb.Code_FAILED_PRECONDITION, "In autocommit mode, transactionID is non-zero: %d", transactionID)
}
innerqr, err = rs.Gateway.Execute(ctx, rs.Target, query, bindVars, 0, 0, opts)
case shouldBegin:
innerqr, transactionID, alias, err = rs.Gateway.BeginExecute(ctx, rs.Target, session.Savepoints, query, bindVars, 0, options)
default:
var qs queryservice.QueryService
_, usingLegacy := rs.Gateway.(*DiscoveryGateway)
if transactionID != 0 && usingLegacy && rs.Target.TabletType != topodatapb.TabletType_MASTER {
return 0, nil, vterrors.Errorf(vtrpcpb.Code_UNIMPLEMENTED, "replica transactions not supported using the legacy healthcheck")
}
if usingLegacy || transactionID == 0 {
qs = rs.Gateway
} else {
qs, err = rs.Gateway.QueryServiceByAlias(alias)
}
if err == nil {
innerqr, err = qs.Execute(ctx, rs.Target, query, bindVars, transactionID, 0, options)
}
}
if err != nil {
return transactionID, alias, err
}
mu.Lock()
defer mu.Unlock()
// Don't append more rows if row count is exceeded.
if len(qr.Rows) <= *maxMemoryRows {
qr.AppendResult(innerqr)
}
return transactionID, alias, nil
},
)
if len(qr.Rows) > *maxMemoryRows {
return nil, vterrors.Errorf(vtrpcpb.Code_RESOURCE_EXHAUSTED, "in-memory row count exceeded allowed limit of %d", *maxMemoryRows)
}
return qr, allErrors.AggrError(vterrors.Aggregate)
}
// ExecuteMultiShard is like Execute,
// but each shard gets its own Sql Queries and BindVariables.
//
// It always returns a non-nil query result and an array of
// shard errors which may be nil so that callers can optionally
// process a partially-successful operation.
func (stc *ScatterConn) ExecuteMultiShard(
ctx context.Context,
rss []*srvtopo.ResolvedShard,
queries []*querypb.BoundQuery,
session *SafeSession,
notInTransaction bool,
autocommit bool,
) (qr *sqltypes.Result, errs []error) {
// mu protects qr
var mu sync.Mutex
qr = new(sqltypes.Result)
allErrors := stc.multiGoTransaction(
ctx,
"Execute",
rss,
session,
notInTransaction,
func(rs *srvtopo.ResolvedShard, i int, shouldBegin bool, transactionID int64, alias *topodatapb.TabletAlias) (int64, *topodatapb.TabletAlias, error) {
var (
innerqr *sqltypes.Result
err error
opts *querypb.ExecuteOptions
)
if session != nil && session.Session != nil {
opts = session.Session.Options
}
switch {
case autocommit:
// As this is auto-commit, the transactionID is supposed to be zero.
if transactionID != 0 {
return 0, nil, vterrors.Errorf(vtrpcpb.Code_FAILED_PRECONDITION, "In autocommit mode, transactionID is non-zero: %d", transactionID)
}
innerqr, err = rs.Gateway.Execute(ctx, rs.Target, queries[i].Sql, queries[i].BindVariables, 0, 0, opts)
case shouldBegin:
innerqr, transactionID, alias, err = rs.Gateway.BeginExecute(ctx, rs.Target, session.Savepoints, queries[i].Sql, queries[i].BindVariables, 0, opts)
default:
var qs queryservice.QueryService
_, usingLegacy := rs.Gateway.(*DiscoveryGateway)
if usingLegacy || transactionID == 0 {
qs = rs.Gateway
} else {
qs, err = rs.Gateway.QueryServiceByAlias(alias)
}
if err == nil {
innerqr, err = qs.Execute(ctx, rs.Target, queries[i].Sql, queries[i].BindVariables, transactionID, 0, opts)
}
}
if err != nil {
return transactionID, alias, err
}
mu.Lock()
defer mu.Unlock()
// Don't append more rows if row count is exceeded.
if len(qr.Rows) <= *maxMemoryRows {
qr.AppendResult(innerqr)
}
return transactionID, alias, nil
},
)
if len(qr.Rows) > *maxMemoryRows {
return nil, []error{vterrors.Errorf(vtrpcpb.Code_RESOURCE_EXHAUSTED, "in-memory row count exceeded allowed limit of %d", *maxMemoryRows)}
}
return qr, allErrors.GetErrors()
}
func (stc *ScatterConn) processOneStreamingResult(mu *sync.Mutex, fieldSent *bool, qr *sqltypes.Result, callback func(*sqltypes.Result) error) error {
mu.Lock()
defer mu.Unlock()
if *fieldSent {
if len(qr.Rows) == 0 {
// It's another field info result. Don't send.
return nil
}
} else {
if len(qr.Fields) == 0 {
// Unreachable: this can happen only if vttablet misbehaves.
return vterrors.New(vtrpcpb.Code_INTERNAL, "received rows before fields for shard")
}
*fieldSent = true
}
return callback(qr)
}
// StreamExecute executes a streaming query on vttablet. The retry rules are the same.
// Note we guarantee the callback will not be called concurrently
// by multiple go routines, through processOneStreamingResult.
func (stc *ScatterConn) StreamExecute(
ctx context.Context,
query string,
bindVars map[string]*querypb.BindVariable,
rss []*srvtopo.ResolvedShard,
options *querypb.ExecuteOptions,
callback func(reply *sqltypes.Result) error,
) error {
// mu protects fieldSent, replyErr and callback
var mu sync.Mutex
fieldSent := false
allErrors := stc.multiGo("StreamExecute", rss, func(rs *srvtopo.ResolvedShard, i int) error {
return rs.Gateway.StreamExecute(ctx, rs.Target, query, bindVars, 0, options, func(qr *sqltypes.Result) error {
return stc.processOneStreamingResult(&mu, &fieldSent, qr, callback)
})
})
return allErrors.AggrError(vterrors.Aggregate)
}
// StreamExecuteMulti is like StreamExecute,
// but each shard gets its own bindVars. If len(shards) is not equal to
// len(bindVars), the function panics.
// Note we guarantee the callback will not be called concurrently
// by multiple go routines, through processOneStreamingResult.
func (stc *ScatterConn) StreamExecuteMulti(
ctx context.Context,
query string,
rss []*srvtopo.ResolvedShard,
bindVars []map[string]*querypb.BindVariable,
options *querypb.ExecuteOptions,
callback func(reply *sqltypes.Result) error,
) error {
// mu protects fieldSent, callback and replyErr
var mu sync.Mutex
fieldSent := false
allErrors := stc.multiGo("StreamExecute", rss, func(rs *srvtopo.ResolvedShard, i int) error {
return rs.Gateway.StreamExecute(ctx, rs.Target, query, bindVars[i], 0, options, func(qr *sqltypes.Result) error {
return stc.processOneStreamingResult(&mu, &fieldSent, qr, callback)
})
})
return allErrors.AggrError(vterrors.Aggregate)
}
// timeTracker is a convenience wrapper used by MessageStream
// to track how long a stream has been unavailable.
type timeTracker struct {
mu sync.Mutex
timestamps map[*querypb.Target]time.Time
}
func newTimeTracker() *timeTracker {
return &timeTracker{
timestamps: make(map[*querypb.Target]time.Time),
}
}
// Reset resets the timestamp set by Record.
func (tt *timeTracker) Reset(target *querypb.Target) {
tt.mu.Lock()
defer tt.mu.Unlock()
delete(tt.timestamps, target)
}
// Record records the time to Now if there was no previous timestamp,
// and it keeps returning that value until the next Reset.
func (tt *timeTracker) Record(target *querypb.Target) time.Time {
tt.mu.Lock()
defer tt.mu.Unlock()
last, ok := tt.timestamps[target]
if !ok {
last = time.Now()
tt.timestamps[target] = last
}
return last
}
// MessageStream streams messages from the specified shards.
// Note we guarantee the callback will not be called concurrently
// by multiple go routines, through processOneStreamingResult.
func (stc *ScatterConn) MessageStream(ctx context.Context, rss []*srvtopo.ResolvedShard, name string, callback func(*sqltypes.Result) error) error {
// The cancelable context is used for handling errors
// from individual streams.
ctx, cancel := context.WithCancel(ctx)
defer cancel()
// mu is used to merge multiple callback calls into one.
var mu sync.Mutex
fieldSent := false
lastErrors := newTimeTracker()
allErrors := stc.multiGo("MessageStream", rss, func(rs *srvtopo.ResolvedShard, i int) error {
// This loop handles the case where a reparent happens, which can cause
// an individual stream to end. If we don't succeed on the retries for
// messageStreamGracePeriod, we abort and return an error.
for {
err := rs.Gateway.MessageStream(ctx, rs.Target, name, func(qr *sqltypes.Result) error {
lastErrors.Reset(rs.Target)
return stc.processOneStreamingResult(&mu, &fieldSent, qr, callback)
})
// nil and EOF are equivalent. UNAVAILABLE can be returned by vttablet if it's demoted
// from master to replica. For any of these conditions, we have to retry.
if err != nil && err != io.EOF && vterrors.Code(err) != vtrpcpb.Code_UNAVAILABLE {
cancel()
return err
}
// There was no error. We have to see if we need to retry.
// If context was canceled, likely due to client disconnect,
// return normally without retrying.
select {
case <-ctx.Done():
return nil
default:
}
firstErrorTimeStamp := lastErrors.Record(rs.Target)
if time.Since(firstErrorTimeStamp) >= *messageStreamGracePeriod {
// Cancel all streams and return an error.
cancel()
return vterrors.Errorf(vtrpcpb.Code_DEADLINE_EXCEEDED, "message stream from %v has repeatedly failed for longer than %v", rs.Target, *messageStreamGracePeriod)
}
// It's not been too long since our last good send. Wait and retry.
select {
case <-ctx.Done():
return nil
case <-time.After(*messageStreamGracePeriod / 5):
}
}
})
return allErrors.AggrError(vterrors.Aggregate)
}
// Close closes the underlying Gateway.
func (stc *ScatterConn) Close() error {
return stc.gateway.Close(context.Background())
}
// GetGatewayCacheStatus returns a displayable version of the Gateway cache.
func (stc *ScatterConn) GetGatewayCacheStatus() TabletCacheStatusList {
return stc.gateway.CacheStatus()
}
// GetLegacyHealthCheckCacheStatus returns a displayable version of the HealthCheck cache.
func (stc *ScatterConn) GetLegacyHealthCheckCacheStatus() discovery.LegacyTabletsCacheStatusList {
if stc.legacyHealthCheck != nil {
return stc.legacyHealthCheck.CacheStatus()
}
return nil
}
// GetHealthCheckCacheStatus returns a displayable version of the HealthCheck cache.
func (stc *ScatterConn) GetHealthCheckCacheStatus() discovery.TabletsCacheStatusList {
if UsingLegacyGateway() {
panic("this should never be called")
}
return stc.gateway.TabletsCacheStatus()
}
// multiGo performs the requested 'action' on the specified
// shards in parallel. This does not handle any transaction state.
// The action function must match the shardActionFunc2 signature.
func (stc *ScatterConn) multiGo(
name string,
rss []*srvtopo.ResolvedShard,
action shardActionFunc,
) (allErrors *concurrency.AllErrorRecorder) {
allErrors = new(concurrency.AllErrorRecorder)
if len(rss) == 0 {
return allErrors
}
oneShard := func(rs *srvtopo.ResolvedShard, i int) {
var err error
startTime, statsKey := stc.startAction(name, rs.Target)
// Send a dummy session.
// TODO(sougou): plumb a real session through this call.
defer stc.endAction(startTime, allErrors, statsKey, &err, NewSafeSession(nil))
err = action(rs, i)
}
if len(rss) == 1 {
// only one shard, do it synchronously.
oneShard(rss[0], 0)
return allErrors
}
var wg sync.WaitGroup
for i, rs := range rss {
wg.Add(1)
go func(rs *srvtopo.ResolvedShard, i int) {
defer wg.Done()
oneShard(rs, i)
}(rs, i)
}
wg.Wait()
return allErrors
}
// multiGoTransaction performs the requested 'action' on the specified
// ResolvedShards in parallel. For each shard, if the requested
// session is in a transaction, it opens a new transactions on the connection,
// and updates the Session with the transaction id. If the session already
// contains a transaction id for the shard, it reuses it.
// The action function must match the shardActionTransactionFunc signature.
//
// It returns an error recorder in which each shard error is recorded positionally,
// i.e. if rss[2] had an error, then the error recorder will store that error
// in the second position.
func (stc *ScatterConn) multiGoTransaction(
ctx context.Context,
name string,
rss []*srvtopo.ResolvedShard,
session *SafeSession,
notInTransaction bool,
action shardActionTransactionFunc,
) (allErrors *concurrency.AllErrorRecorder) {
numShards := len(rss)
allErrors = new(concurrency.AllErrorRecorder)
if numShards == 0 {
return allErrors
}
oneShard := func(rs *srvtopo.ResolvedShard, i int) {
var err error
startTime, statsKey := stc.startAction(name, rs.Target)
defer stc.endAction(startTime, allErrors, statsKey, &err, session)
shouldBegin, transactionID, alias := transactionInfo(rs.Target, session, notInTransaction)
transactionID, alias, err = action(rs, i, shouldBegin, transactionID, alias)
if shouldBegin && transactionID != 0 {
if appendErr := session.Append(&vtgatepb.Session_ShardSession{
Target: rs.Target,
TransactionId: transactionID,
TabletAlias: alias,
}, stc.txConn.mode); appendErr != nil {
err = appendErr
}
}
}
if numShards == 1 {
// only one shard, do it synchronously.
for i, rs := range rss {
oneShard(rs, i)
}
} else {
var wg sync.WaitGroup
for i, rs := range rss {
wg.Add(1)
go func(rs *srvtopo.ResolvedShard, i int) {
defer wg.Done()
oneShard(rs, i)
}(rs, i)
}
wg.Wait()
}
if session.MustRollback() {
stc.txConn.Rollback(ctx, session)
}
return allErrors
}
// transactionInfo looks at the current session, and returns:
// - shouldBegin: if we should call 'Begin' to get a transactionID
// - transactionID: the transactionID to use, or 0 if not in a transaction.
func transactionInfo(
target *querypb.Target,
session *SafeSession,
notInTransaction bool,
) (shouldBegin bool, transactionID int64, alias *topodatapb.TabletAlias) {
if !session.InTransaction() {
return false, 0, nil
}
// No need to protect ourselves from the race condition between
// Find and Append. The higher level functions ensure that no
// duplicate (target) tuples can execute
// this at the same time.
transactionID, alias = session.Find(target.Keyspace, target.Shard, target.TabletType)
if transactionID != 0 {
return false, transactionID, alias
}
// We are in a transaction at higher level,
// but client requires not to start a transaction for this query.
// If a transaction was started on this conn, we will use it (as above).
if notInTransaction {
return false, 0, nil
}
return true, 0, nil
}