-
Notifications
You must be signed in to change notification settings - Fork 2k
/
controller.go
189 lines (157 loc) · 4.85 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
/*
Copyright 2018 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 (
"flag"
"fmt"
"strconv"
"time"
"vitess.io/vitess/go/vt/vterrors"
"github.com/golang/protobuf/proto"
"golang.org/x/net/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"
)
var retryDelay = flag.Duration("vreplication_retry_delay", 5*time.Second, "delay before retrying a failed binlog connection")
// 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 {
dbClientFactory func() binlogplayer.DBClient
mysqld mysqlctl.MysqlDaemon
blpStats *binlogplayer.Stats
id uint32
source binlogdatapb.BinlogSource
stopPos string
tabletPicker *tabletPicker
cancel context.CancelFunc
done chan struct{}
// The following fields are updated after start. So, they need synchronization.
sourceTablet sync2.AtomicString
}
// 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) (*controller, error) {
if blpStats == nil {
blpStats = binlogplayer.NewStats()
}
ct := &controller{
dbClientFactory: dbClientFactory,
mysqld: mysqld,
blpStats: blpStats,
done: make(chan struct{}),
}
// id
id, err := strconv.Atoi(params["id"])
if err != nil {
return nil, err
}
ct.id = uint32(id)
// Nothing to do if replication is stopped.
if params["state"] == binlogplayer.BlpStopped {
ct.cancel = func() {}
close(ct.done)
return ct, nil
}
// source, stopPos
if err := proto.UnmarshalText(params["source"], &ct.source); err != nil {
return nil, err
}
ct.stopPos = params["stop_pos"]
// tabletPicker
if v, ok := params["cell"]; ok {
cell = v
}
if v, ok := params["tablet_types"]; ok {
tabletTypesStr = v
}
tp, err := newTabletPicker(ts, cell, 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)
ct.tabletPicker.Close()
close(ct.done)
}()
for {
err := ct.runBlp(ctx)
if err == nil {
return
}
// Sometimes, canceled contexts get wrapped as errors.
select {
case <-ctx.Done():
return
default:
}
log.Warningf("stream %v: %v, retrying after %v", ct.id, err, *retryDelay)
time.Sleep(*retryDelay)
}
}
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()
tablet, err := ct.tabletPicker.Pick(ctx)
if err != nil {
return err
}
ct.sourceTablet.Set(tablet.Alias.String())
if len(ct.source.Tables) > 0 {
// Table names can have search patterns. Resolve them against the schema.
tables, err := mysqlctl.ResolveTables(ct.mysqld, dbClient.DBName(), ct.source.Tables)
if err != nil {
return vterrors.Wrap(err, "failed to resolve table names")
}
player := binlogplayer.NewBinlogPlayerTables(dbClient, tablet, tables, ct.id, ct.blpStats)
return player.ApplyBinlogEvents(ctx)
}
player := binlogplayer.NewBinlogPlayerKeyRange(dbClient, tablet, ct.source.KeyRange, ct.id, ct.blpStats)
return player.ApplyBinlogEvents(ctx)
}
func (ct *controller) Stop() {
ct.cancel()
<-ct.done
}