-
Notifications
You must be signed in to change notification settings - Fork 2k
/
engine.go
540 lines (484 loc) · 17.2 KB
/
engine.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
/*
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 vstreamer
import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"net/http"
"strings"
"sync"
"time"
"vitess.io/vitess/go/vt/dbconfigs"
"vitess.io/vitess/go/vt/mysqlctl"
"vitess.io/vitess/go/vt/servenv"
"vitess.io/vitess/go/vt/vterrors"
"vitess.io/vitess/go/acl"
"vitess.io/vitess/go/sqltypes"
"vitess.io/vitess/go/stats"
"vitess.io/vitess/go/vt/log"
"vitess.io/vitess/go/vt/srvtopo"
"vitess.io/vitess/go/vt/topo"
"vitess.io/vitess/go/vt/vtgate/vindexes"
"vitess.io/vitess/go/vt/vttablet/tabletserver/schema"
"vitess.io/vitess/go/vt/vttablet/tabletserver/tabletenv"
"vitess.io/vitess/go/vt/vttablet/tabletserver/throttle"
binlogdatapb "vitess.io/vitess/go/vt/proto/binlogdata"
vschemapb "vitess.io/vitess/go/vt/proto/vschema"
)
const (
throttlerAppName = "vstreamer"
)
// Engine is the engine for handling vreplication streaming requests.
type Engine struct {
env tabletenv.Env
ts srvtopo.Server
se *schema.Engine
cell string
// keyspace is initialized by InitDBConfig
keyspace string
shard string
// wg is incremented for every Stream, and decremented on end.
// Close waits for all current streams to end by waiting on wg.
wg sync.WaitGroup
mu sync.Mutex
isOpen bool
streamIdx int
streamers map[int]*uvstreamer
rowStreamers map[int]*rowStreamer
resultStreamers map[int]*resultStreamer
// watcherOnce is used for initializing vschema
// and setting up the vschema watch. It's guaranteed that
// no stream will start until vschema is initialized by
// the first call through watcherOnce.
watcherOnce sync.Once
lvschema *localVSchema
// stats variables
vschemaErrors *stats.Counter
vschemaUpdates *stats.Counter
// vstreamer metrics
vstreamerPhaseTimings *servenv.TimingsWrapper
vstreamerEventsStreamed *stats.Counter
vstreamerPacketSize *stats.GaugeFunc
vstreamerNumPackets *stats.Counter
resultStreamerNumRows *stats.Counter
resultStreamerNumPackets *stats.Counter
rowStreamerNumRows *stats.Counter
rowStreamerNumPackets *stats.Counter
rowStreamerWaits *servenv.TimingsWrapper
errorCounts *stats.CountersWithSingleLabel
vstreamersCreated *stats.Counter
vstreamersEndedWithErrors *stats.Counter
throttlerClient *throttle.Client
}
// NewEngine creates a new Engine.
// Initialization sequence is: NewEngine->InitDBConfig->Open.
// Open and Close can be called multiple times and are idempotent.
func NewEngine(env tabletenv.Env, ts srvtopo.Server, se *schema.Engine, lagThrottler *throttle.Throttler, cell string) *Engine {
vse := &Engine{
env: env,
ts: ts,
se: se,
cell: cell,
throttlerClient: throttle.NewBackgroundClient(lagThrottler, throttlerAppName, throttle.ThrottleCheckSelf),
streamers: make(map[int]*uvstreamer),
rowStreamers: make(map[int]*rowStreamer),
resultStreamers: make(map[int]*resultStreamer),
lvschema: &localVSchema{vschema: &vindexes.VSchema{}},
vschemaErrors: env.Exporter().NewCounter("VSchemaErrors", "Count of VSchema errors"),
vschemaUpdates: env.Exporter().NewCounter("VSchemaUpdates", "Count of VSchema updates. Does not include errors"),
vstreamerPhaseTimings: env.Exporter().NewTimings("VStreamerPhaseTiming", "Time taken for different phases during vstream copy", "phase-timing"),
vstreamerEventsStreamed: env.Exporter().NewCounter("VStreamerEventsStreamed", "Count of events streamed in VStream API"),
vstreamerPacketSize: env.Exporter().NewGaugeFunc("VStreamPacketSize", "Max packet size for sending vstreamer events", getPacketSize),
vstreamerNumPackets: env.Exporter().NewCounter("VStreamerNumPackets", "Number of packets in vstreamer"),
resultStreamerNumPackets: env.Exporter().NewCounter("ResultStreamerNumPackets", "Number of packets in result streamer"),
resultStreamerNumRows: env.Exporter().NewCounter("ResultStreamerNumRows", "Number of rows sent in result streamer"),
rowStreamerNumPackets: env.Exporter().NewCounter("RowStreamerNumPackets", "Number of packets in row streamer"),
rowStreamerNumRows: env.Exporter().NewCounter("RowStreamerNumRows", "Number of rows sent in row streamer"),
rowStreamerWaits: env.Exporter().NewTimings("RowStreamerWaits", "Total counts and time we've waited when streaming rows in the vstream copy phase", "copy-phase-waits"),
vstreamersCreated: env.Exporter().NewCounter("VStreamersCreated", "Count of vstreamers created"),
vstreamersEndedWithErrors: env.Exporter().NewCounter("VStreamersEndedWithErrors", "Count of vstreamers that ended with errors"),
errorCounts: env.Exporter().NewCountersWithSingleLabel("VStreamerErrors", "Tracks errors in vstreamer", "type", "Catchup", "Copy", "Send", "TablePlan"),
}
env.Exporter().NewGaugeFunc("RowStreamerMaxInnoDBTrxHistLen", "", func() int64 { return env.Config().RowStreamer.MaxInnoDBTrxHistLen })
env.Exporter().NewGaugeFunc("RowStreamerMaxMySQLReplLagSecs", "", func() int64 { return env.Config().RowStreamer.MaxMySQLReplLagSecs })
env.Exporter().HandleFunc("/debug/tablet_vschema", vse.ServeHTTP)
return vse
}
// InitDBConfig initializes the target parameters for the Engine.
func (vse *Engine) InitDBConfig(keyspace, shard string) {
vse.keyspace = keyspace
vse.shard = shard
}
// Open starts the Engine service.
func (vse *Engine) Open() {
vse.mu.Lock()
defer vse.mu.Unlock()
if vse.isOpen {
return
}
log.Info("VStreamer: opening")
vse.isOpen = true
}
// IsOpen checks if the engine is opened
func (vse *Engine) IsOpen() bool {
vse.mu.Lock()
defer vse.mu.Unlock()
return vse.isOpen
}
// Close closes the Engine service.
func (vse *Engine) Close() {
func() {
vse.mu.Lock()
defer vse.mu.Unlock()
if !vse.isOpen {
return
}
// cancels are non-blocking.
for _, s := range vse.streamers {
s.Cancel()
}
for _, s := range vse.rowStreamers {
s.Cancel()
}
for _, s := range vse.resultStreamers {
s.Cancel()
}
vse.isOpen = false
}()
// Wait only after releasing the lock because the end of every
// stream will use the lock to remove the entry from streamers.
vse.wg.Wait()
log.Info("VStreamer: closed")
}
func (vse *Engine) vschema() *vindexes.VSchema {
vse.mu.Lock()
defer vse.mu.Unlock()
return vse.lvschema.vschema
}
// Stream starts a new stream.
// This streams events from the binary logs
func (vse *Engine) Stream(ctx context.Context, startPos string, tablePKs []*binlogdatapb.TableLastPK, filter *binlogdatapb.Filter, send func([]*binlogdatapb.VEvent) error) error {
// Ensure vschema is initialized and the watcher is started.
// Starting of the watcher has to be delayed till the first call to Stream
// because this overhead should be incurred only if someone uses this feature.
vse.watcherOnce.Do(vse.setWatch)
// Create stream and add it to the map.
streamer, idx, err := func() (*uvstreamer, int, error) {
vse.mu.Lock()
defer vse.mu.Unlock()
if !vse.isOpen {
return nil, 0, errors.New("VStreamer is not open")
}
streamer := newUVStreamer(ctx, vse, vse.env.Config().DB.FilteredWithDB(), vse.se, startPos, tablePKs, filter, vse.lvschema, send)
idx := vse.streamIdx
vse.streamers[idx] = streamer
vse.streamIdx++
// Now that we've added the stream, increment wg.
// This must be done before releasing the lock.
vse.wg.Add(1)
return streamer, idx, nil
}()
if err != nil {
return err
}
// Remove stream from map and decrement wg when it ends.
defer func() {
vse.mu.Lock()
defer vse.mu.Unlock()
delete(vse.streamers, idx)
vse.wg.Done()
}()
// No lock is held while streaming, but wg is incremented.
return streamer.Stream()
}
// StreamRows streams rows.
// This streams the table data rows (so we can copy the table data snapshot)
func (vse *Engine) StreamRows(ctx context.Context, query string, lastpk []sqltypes.Value, send func(*binlogdatapb.VStreamRowsResponse) error) error {
// Ensure vschema is initialized and the watcher is started.
// Starting of the watcher has to be delayed till the first call to Stream
// because this overhead should be incurred only if someone uses this feature.
vse.watcherOnce.Do(vse.setWatch)
log.Infof("Streaming rows for query %s, lastpk: %s", query, lastpk)
// Create stream and add it to the map.
rowStreamer, idx, err := func() (*rowStreamer, int, error) {
vse.mu.Lock()
defer vse.mu.Unlock()
if !vse.isOpen {
return nil, 0, errors.New("VStreamer is not open")
}
rowStreamer := newRowStreamer(ctx, vse.env.Config().DB.FilteredWithDB(), vse.se, query, lastpk, vse.lvschema, send, vse)
idx := vse.streamIdx
vse.rowStreamers[idx] = rowStreamer
vse.streamIdx++
// Now that we've added the stream, increment wg.
// This must be done before releasing the lock.
vse.wg.Add(1)
return rowStreamer, idx, nil
}()
if err != nil {
return err
}
// Remove stream from map and decrement wg when it ends.
defer func() {
vse.mu.Lock()
defer vse.mu.Unlock()
delete(vse.rowStreamers, idx)
vse.wg.Done()
}()
// No lock is held while streaming, but wg is incremented.
return rowStreamer.Stream()
}
// StreamResults streams results of the query with the gtid.
func (vse *Engine) StreamResults(ctx context.Context, query string, send func(*binlogdatapb.VStreamResultsResponse) error) error {
// Create stream and add it to the map.
resultStreamer, idx, err := func() (*resultStreamer, int, error) {
vse.mu.Lock()
defer vse.mu.Unlock()
if !vse.isOpen {
return nil, 0, errors.New("VStreamer is not open")
}
resultStreamer := newResultStreamer(ctx, vse.env.Config().DB.FilteredWithDB(), query, send, vse)
idx := vse.streamIdx
vse.resultStreamers[idx] = resultStreamer
vse.streamIdx++
// Now that we've added the stream, increment wg.
// This must be done before releasing the lock.
vse.wg.Add(1)
return resultStreamer, idx, nil
}()
if err != nil {
return err
}
// Remove stream from map and decrement wg when it ends.
defer func() {
vse.mu.Lock()
defer vse.mu.Unlock()
delete(vse.resultStreamers, idx)
vse.wg.Done()
}()
// No lock is held while streaming, but wg is incremented.
return resultStreamer.Stream()
}
// ServeHTTP shows the current VSchema.
func (vse *Engine) ServeHTTP(response http.ResponseWriter, request *http.Request) {
if err := acl.CheckAccessHTTP(request, acl.DEBUGGING); err != nil {
acl.SendError(response, err)
return
}
response.Header().Set("Content-Type", "application/json; charset=utf-8")
vs := vse.vschema()
if vs == nil {
response.Write([]byte("{}"))
}
b, err := json.MarshalIndent(vs, "", " ")
if err != nil {
response.Write([]byte(err.Error()))
return
}
buf := bytes.NewBuffer(nil)
json.HTMLEscape(buf, b)
response.Write(buf.Bytes())
}
func (vse *Engine) setWatch() {
// If there's no toposerver, create an empty vschema.
if vse.ts == nil {
vse.lvschema = &localVSchema{
keyspace: vse.keyspace,
vschema: &vindexes.VSchema{},
}
return
}
// WatchSrvVSchema does not return until the inner func has been called at least once.
vse.ts.WatchSrvVSchema(context.TODO(), vse.cell, func(v *vschemapb.SrvVSchema, err error) bool {
switch {
case err == nil:
// Build vschema down below.
case topo.IsErrType(err, topo.NoNode):
v = nil
default:
log.Errorf("Error fetching vschema: %v", err)
vse.vschemaErrors.Add(1)
return true
}
var vschema *vindexes.VSchema
if v != nil {
vschema = vindexes.BuildVSchema(v)
if err != nil {
log.Errorf("Error building vschema: %v", err)
vse.vschemaErrors.Add(1)
return true
}
} else {
vschema = &vindexes.VSchema{}
}
// Broadcast the change to all streamers.
vse.mu.Lock()
defer vse.mu.Unlock()
vse.lvschema = &localVSchema{
keyspace: vse.keyspace,
vschema: vschema,
}
b, _ := json.MarshalIndent(vschema, "", " ")
log.V(2).Infof("Updated vschema: %s", b)
for _, s := range vse.streamers {
s.SetVSchema(vse.lvschema)
}
vse.vschemaUpdates.Add(1)
return true
})
}
func getPacketSize() int64 {
return int64(*defaultPacketSize)
}
// waitForMySQL ensures that the source is able to stay within defined bounds for
// its MVCC history list (trx rollback segment linked list for old versions of rows
// that should be purged ASAP) and its replica lag (which will be -1 for non-replicas)
// to help ensure that the vstream does not have an outsized harmful impact on the
// source's ability to function normally.
func (vse *Engine) waitForMySQL(ctx context.Context, db dbconfigs.Connector, tableName string) error {
sourceEndpoint, _ := vse.getMySQLEndpoint(ctx, db)
backoff := 1 * time.Second
backoffLimit := backoff * 30
ready := false
recording := false
loopFunc := func() error {
// Exit if the context has been cancelled
if ctx.Err() != nil {
return ctx.Err()
}
// Check the config values each time as they can be updated in the running process via the /debug/env endpoint.
// This allows the user to break out of a wait w/o incurring any downtime or restarting the workflow if they
// need to.
mhll := vse.env.Config().RowStreamer.MaxInnoDBTrxHistLen
mrls := vse.env.Config().RowStreamer.MaxMySQLReplLagSecs
hll := vse.getInnoDBTrxHistoryLen(ctx, db)
rpl := vse.getMySQLReplicationLag(ctx, db)
if hll <= mhll && rpl <= mrls {
ready = true
} else {
log.Infof("VStream source (%s) is not ready to stream more rows. Max InnoDB history length is %d and it was %d, max replication lag is %d (seconds) and it was %d. Will pause and retry.",
sourceEndpoint, mhll, hll, mrls, rpl)
}
return nil
}
for {
if err := loopFunc(); err != nil {
return err
}
if ready {
break
} else {
if !recording {
defer func() {
ct := time.Now()
// Global row streamer waits on the source tablet
vse.rowStreamerWaits.Record("waitForMySQL", ct)
// Waits by the table we're copying from the source tablet
vse.vstreamerPhaseTimings.Record(fmt.Sprintf("%s:waitForMySQL", tableName), ct)
}()
recording = true
}
select {
case <-ctx.Done():
return ctx.Err()
case <-time.After(backoff):
// Exponential backoff with 1.5 as a factor
if backoff != backoffLimit {
nb := time.Duration(float64(backoff) * 1.5)
if nb > backoffLimit {
backoff = backoffLimit
} else {
backoff = nb
}
}
}
}
}
return nil
}
// getInnoDBTrxHistoryLen attempts to query InnoDB's current transaction rollback segment's history
// list length. If the value cannot be determined for any reason then -1 is returned, which means
// "unknown".
func (vse *Engine) getInnoDBTrxHistoryLen(ctx context.Context, db dbconfigs.Connector) int64 {
histLen := int64(-1)
conn, err := db.Connect(ctx)
if err != nil {
return histLen
}
defer conn.Close()
res, err := conn.ExecuteFetch(trxHistoryLenQuery, 1, false)
if err != nil || len(res.Rows) != 1 || res.Rows[0] == nil {
return histLen
}
histLen, _ = res.Rows[0][0].ToInt64()
return histLen
}
// getMySQLReplicationLag attempts to get the seconds_behind_master value.
// If the value cannot be determined for any reason then -1 is returned, which
// means "unknown" or "irrelevant" (meaning it's not actively replicating).
func (vse *Engine) getMySQLReplicationLag(ctx context.Context, db dbconfigs.Connector) int64 {
lagSecs := int64(-1)
conn, err := db.Connect(ctx)
if err != nil {
return lagSecs
}
defer conn.Close()
res, err := conn.ExecuteFetch(replicaLagQuery, 1, true)
if err != nil || len(res.Rows) != 1 || res.Rows[0] == nil {
return lagSecs
}
row := res.Named().Row()
return row.AsInt64("Seconds_Behind_Master", -1)
}
// getMySQLEndpoint returns the host:port value for the vstreamer (MySQL) instance
func (vse *Engine) getMySQLEndpoint(ctx context.Context, db dbconfigs.Connector) (string, error) {
conn, err := db.Connect(ctx)
if err != nil {
return "", err
}
defer conn.Close()
res, err := conn.ExecuteFetch(hostQuery, 1, false)
if err != nil || len(res.Rows) != 1 || res.Rows[0] == nil {
return "", vterrors.Wrap(err, "could not get vstreamer MySQL endpoint")
}
host := res.Rows[0][0].ToString()
port, _ := res.Rows[0][1].ToInt64()
return fmt.Sprintf("%s:%d", host, port), nil
}
// mapPKEquivalentCols gets a PK equivalent from mysqld for the table
// and maps the column names to field indexes in the MinimalTable struct.
func (vse *Engine) mapPKEquivalentCols(ctx context.Context, table *binlogdatapb.MinimalTable) ([]int, error) {
mysqld := mysqlctl.NewMysqld(vse.env.Config().DB)
pkeColNames, err := mysqld.GetPrimaryKeyEquivalentColumns(ctx, vse.env.Config().DB.DBName, table.Name)
if err != nil {
return nil, err
}
pkeCols := make([]int, len(pkeColNames))
matches := 0
for n, field := range table.Fields {
for i, pkeColName := range pkeColNames {
if strings.EqualFold(field.Name, pkeColName) {
pkeCols[i] = n
matches++
break
}
}
if matches == len(pkeColNames) {
break
}
}
return pkeCols, nil
}