-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
controller.go
304 lines (265 loc) · 9.5 KB
/
controller.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
/*
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 vreplication
import (
"fmt"
"strconv"
"strings"
"time"
"google.golang.org/protobuf/encoding/prototext"
"vitess.io/vitess/go/vt/discovery"
"vitess.io/vitess/go/vt/vterrors"
"context"
"vitess.io/vitess/go/sync2"
"vitess.io/vitess/go/tb"
"vitess.io/vitess/go/vt/binlog/binlogplayer"
"vitess.io/vitess/go/vt/log"
"vitess.io/vitess/go/vt/mysqlctl"
"vitess.io/vitess/go/vt/topo"
binlogdatapb "vitess.io/vitess/go/vt/proto/binlogdata"
topodatapb "vitess.io/vitess/go/vt/proto/topodata"
)
const (
// How many times to retry tablet selection before we
// give up and return an error message that the user
// can see and act upon if needed.
tabletPickerRetries = 5
)
// controller is created by Engine. Members are initialized upfront.
// There is no mutex within a controller becaust its members are
// either read-only or self-synchronized.
type controller struct {
vre *Engine
dbClientFactory func() binlogplayer.DBClient
mysqld mysqlctl.MysqlDaemon
blpStats *binlogplayer.Stats
id uint32
workflow string
source *binlogdatapb.BinlogSource
stopPos string
tabletPicker *discovery.TabletPicker
cancel context.CancelFunc
done chan struct{}
// The following fields are updated after start. So, they need synchronization.
sourceTablet sync2.AtomicString
lastWorkflowError *vterrors.LastError
}
// newController creates a new controller. Unless a stream is explicitly 'Stopped',
// this function launches a goroutine to perform continuous vreplication.
func newController(ctx context.Context, params map[string]string, dbClientFactory func() binlogplayer.DBClient, mysqld mysqlctl.MysqlDaemon, ts *topo.Server, cell, tabletTypesStr string, blpStats *binlogplayer.Stats, vre *Engine) (*controller, error) {
if blpStats == nil {
blpStats = binlogplayer.NewStats()
}
ct := &controller{
vre: vre,
dbClientFactory: dbClientFactory,
mysqld: mysqld,
blpStats: blpStats,
done: make(chan struct{}),
source: &binlogdatapb.BinlogSource{},
}
log.Infof("creating controller with cell: %v, tabletTypes: %v, and params: %v", cell, tabletTypesStr, params)
// id
id, err := strconv.Atoi(params["id"])
if err != nil {
return nil, err
}
ct.id = uint32(id)
ct.workflow = params["workflow"]
ct.lastWorkflowError = vterrors.NewLastError(fmt.Sprintf("VReplication controller %d for workflow %q", ct.id, ct.workflow), maxTimeToRetryError)
state := params["state"]
blpStats.State.Set(state)
// Nothing to do if replication is stopped or is known to have an unrecoverable error.
if state == binlogplayer.BlpStopped || state == binlogplayer.BlpError {
ct.cancel = func() {}
close(ct.done)
return ct, nil
}
// source, stopPos
if err := prototext.Unmarshal([]byte(params["source"]), ct.source); err != nil {
return nil, err
}
ct.stopPos = params["stop_pos"]
if ct.source.GetExternalMysql() == "" {
// tabletPicker
if v := params["cell"]; v != "" {
cell = v
}
if v := params["tablet_types"]; v != "" {
tabletTypesStr = v
}
log.Infof("creating tablet picker for source keyspace/shard %v/%v with cell: %v and tabletTypes: %v", ct.source.Keyspace, ct.source.Shard, cell, tabletTypesStr)
cells := strings.Split(cell, ",")
sourceTopo := ts
if ct.source.ExternalCluster != "" {
sourceTopo, err = sourceTopo.OpenExternalVitessClusterServer(ctx, ct.source.ExternalCluster)
if err != nil {
return nil, err
}
}
tp, err := discovery.NewTabletPicker(sourceTopo, cells, ct.source.Keyspace, ct.source.Shard, tabletTypesStr)
if err != nil {
return nil, err
}
ct.tabletPicker = tp
}
// cancel
ctx, ct.cancel = context.WithCancel(ctx)
go ct.run(ctx)
return ct, nil
}
func (ct *controller) run(ctx context.Context) {
defer func() {
log.Infof("stream %v: stopped", ct.id)
close(ct.done)
}()
for {
err := ct.runBlp(ctx)
if err == nil {
return
}
// Sometimes, canceled contexts get wrapped as errors.
select {
case <-ctx.Done():
log.Warningf("context canceled: %s", err.Error())
return
default:
}
ct.blpStats.ErrorCounts.Add([]string{"Stream Error"}, 1)
binlogplayer.LogError(fmt.Sprintf("error in stream %v, retrying after %v", ct.id, retryDelay), err)
timer := time.NewTimer(retryDelay)
select {
case <-ctx.Done():
log.Warningf("context canceled: %s", err.Error())
timer.Stop()
return
case <-timer.C:
}
}
}
func (ct *controller) runBlp(ctx context.Context) (err error) {
defer func() {
ct.sourceTablet.Set("")
if x := recover(); x != nil {
log.Errorf("stream %v: caught panic: %v\n%s", ct.id, x, tb.Stack(4))
err = fmt.Errorf("panic: %v", x)
}
}()
select {
case <-ctx.Done():
return nil
default:
}
// Call this for youtube-specific customization.
// This should be done every time, in case mysql was restarted.
if err := ct.mysqld.EnableBinlogPlayback(); err != nil {
return err
}
dbClient := ct.dbClientFactory()
if err := dbClient.Connect(); err != nil {
return vterrors.Wrap(err, "can't connect to database")
}
defer dbClient.Close()
var tablet *topodatapb.Tablet
if ct.source.GetExternalMysql() == "" {
log.Infof("trying to find a tablet eligible for vreplication. stream id: %v", ct.id)
tpCtx, tpCancel := context.WithTimeout(ctx, discovery.GetTabletPickerRetryDelay()*tabletPickerRetries)
defer tpCancel()
tablet, err = ct.tabletPicker.PickForStreaming(tpCtx)
if err != nil {
select {
case <-ctx.Done():
default:
ct.blpStats.ErrorCounts.Add([]string{"No Source Tablet Found"}, 1)
ct.setMessage(dbClient, fmt.Sprintf("Error picking tablet: %s", err.Error()))
}
return err
}
ct.setMessage(dbClient, fmt.Sprintf("Picked source tablet: %s", tablet.Alias.String()))
log.Infof("found a tablet eligible for vreplication. stream id: %v tablet: %s", ct.id, tablet.Alias.String())
ct.sourceTablet.Set(tablet.Alias.String())
}
switch {
case len(ct.source.Tables) > 0:
// Table names can have search patterns. Resolve them against the schema.
tables, err := mysqlctl.ResolveTables(ctx, ct.mysqld, dbClient.DBName(), ct.source.Tables)
if err != nil {
ct.blpStats.ErrorCounts.Add([]string{"Invalid Source"}, 1)
return vterrors.Wrap(err, "failed to resolve table names")
}
player := binlogplayer.NewBinlogPlayerTables(dbClient, tablet, tables, ct.id, ct.blpStats)
return player.ApplyBinlogEvents(ctx)
case ct.source.KeyRange != nil:
player := binlogplayer.NewBinlogPlayerKeyRange(dbClient, tablet, ct.source.KeyRange, ct.id, ct.blpStats)
return player.ApplyBinlogEvents(ctx)
case ct.source.Filter != nil:
// Timestamp fields from binlogs are always sent as UTC.
// So, we should set the timezone to be UTC for those values to be correctly inserted.
if _, err := dbClient.ExecuteFetch("set @@session.time_zone = '+00:00'", 10000); err != nil {
return err
}
// Tables may have varying character sets. To ship the bits without interpreting them
// we set the character set to be binary.
if _, err := dbClient.ExecuteFetch("set names binary", 10000); err != nil {
return err
}
// We must apply AUTO_INCREMENT values precisely as we got them. This include the 0 value, which is not recommended in AUTO_INCREMENT, and yet is valid.
if _, err := dbClient.ExecuteFetch("set @@session.sql_mode = CONCAT(@@session.sql_mode, ',NO_AUTO_VALUE_ON_ZERO')", 10000); err != nil {
return err
}
var vsClient VStreamerClient
var err error
if name := ct.source.GetExternalMysql(); name != "" {
vsClient, err = ct.vre.ec.Get(name)
if err != nil {
return err
}
} else {
vsClient = newTabletConnector(tablet)
}
if err := vsClient.Open(ctx); err != nil {
return err
}
defer vsClient.Close(ctx)
vr := newVReplicator(ct.id, ct.source, vsClient, ct.blpStats, dbClient, ct.mysqld, ct.vre)
err = vr.Replicate(ctx)
ct.lastWorkflowError.Record(err)
// If this is a mysql error that we know needs manual intervention OR
// we cannot identify this as non-recoverable, but it has persisted beyond the retry limit (maxTimeToRetryError)
if isUnrecoverableError(err) || !ct.lastWorkflowError.ShouldRetry() {
log.Errorf("vreplication stream %d going into error state due to %+v", ct.id, err)
if errSetState := vr.setState(binlogplayer.BlpError, err.Error()); errSetState != nil {
return err // yes, err and not errSetState.
}
return nil // this will cause vreplicate to quit the workflow
}
return err
}
ct.blpStats.ErrorCounts.Add([]string{"Invalid Source"}, 1)
return fmt.Errorf("missing source")
}
func (ct *controller) setMessage(dbClient binlogplayer.DBClient, message string) error {
ct.blpStats.History.Add(&binlogplayer.StatsHistoryRecord{
Time: time.Now(),
Message: message,
})
query := fmt.Sprintf("update _vt.vreplication set message=%v where id=%v", encodeString(binlogplayer.MessageTruncate(message)), ct.id)
if _, err := dbClient.ExecuteFetch(query, 1); err != nil {
return fmt.Errorf("could not set message: %v: %v", query, err)
}
return nil
}
func (ct *controller) Stop() {
ct.cancel()
<-ct.done
}