forked from vitessio/vitess
/
binlog_streamer.go
361 lines (329 loc) · 9.1 KB
/
binlog_streamer.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
// Copyright 2012, Google Inc. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package binlog
import (
"bufio"
"bytes"
"fmt"
"io"
"os"
"path"
"regexp"
"strconv"
"time"
log "github.com/golang/glog"
"github.com/youtube/vitess/go/sync2"
"github.com/youtube/vitess/go/vt/binlog/proto"
)
var (
// posRE is for extracting server id, end_log_pos and group_id.
posRE = regexp.MustCompile(`^#.*server id ([0-9]+).*end_log_pos ([0-9]+).*group_id ([0-9]+)`)
// rotateRE is for extracting log rotation info.
rotateRE = regexp.MustCompile(`^#.*Rotate to ([^ \t]+).*pos: ([0-9]+)`)
// delimRE is for extracting the delimeter.
delimRE = regexp.MustCompile(`^DELIMITER[ \t]+(.*;)`)
// ignorePrefixes defines the prefixes that can be ignored.
ignorePrefixes = [][]byte{
[]byte("#"),
[]byte("use"),
[]byte("USE"),
[]byte("/*"),
[]byte("SET @@session"),
[]byte("BINLOG"),
}
// statementPrefixes are normal sql statement prefixes.
statementPrefixes = map[string]int{
"begin": proto.BL_BEGIN,
"commit": proto.BL_COMMIT,
"rollback": proto.BL_ROLLBACK,
"insert": proto.BL_DML,
"update": proto.BL_DML,
"delete": proto.BL_DML,
"create": proto.BL_DDL,
"alter": proto.BL_DDL,
"drop": proto.BL_DDL,
"truncate": proto.BL_DDL,
"rename": proto.BL_DDL,
"set": proto.BL_SET,
}
// Misc vars.
HASH_COMMENT = []byte("#")
SLASH_COMMENT = []byte("/*")
DELIM_STMT = []byte("DELIMITER")
DEFAULT_DELIM = []byte(";")
)
type binlogPosition struct {
GroupId, ServerId int64
}
// BinlogStreamer streamer streams binlog events grouped
// by transactions.
type BinlogStreamer struct {
// dbname & dir are set at creation.
dbname string
dir string
svm sync2.ServiceManager
// file, blPos & delim are updated during streaming.
file fileInfo
blPos binlogPosition
delim []byte
}
// sendTransactionFunc is used to send binlog events.
// reply is of type proto.BinlogTransaction.
type sendTransactionFunc func(trans *proto.BinlogTransaction) error
// NewBinlogStreamer creates a BinlogStreamer. dbname specifes
// the db to stream events for, and binlogPrefix is as defined
// by the mycnf variable.
func NewBinlogStreamer(dbname, binlogPrefix string) *BinlogStreamer {
return &BinlogStreamer{
dbname: dbname,
dir: path.Dir(binlogPrefix),
}
}
// Stream starts streaming binlog events from file & pos by repeatedly calling sendTransaction.
func (bls *BinlogStreamer) Stream(file string, pos int64, sendTransaction sendTransactionFunc) (err error) {
if err = bls.file.Init(path.Join(bls.dir, file), pos); err != nil {
return err
}
defer bls.file.Close()
// Launch using service manager so we can stop this
// as needed.
bls.svm.Go(func(_ *sync2.ServiceManager) {
for {
if err = bls.run(sendTransaction); err != nil {
return
}
if err = bls.file.WaitForChange(&bls.svm); err != nil {
return
}
}
})
// Wait for service to exit, and handle errors if any.
bls.svm.Wait()
if err == io.EOF {
log.Infof("Stream ended @ %#v", bls.file)
return nil
}
log.Errorf("Stream error @ %#v, error: %v", bls.file, err)
return fmt.Errorf("stream error @ %#v, error: %v", bls.file, err)
}
// Stop stops the currently executing Stream if there is one.
func (bls *BinlogStreamer) Stop() {
bls.svm.Stop()
}
// run launches mysqlbinlog and starts the stream. It takes care of
// cleaning up the process when streaming returns.
func (bls *BinlogStreamer) run(sendTransaction sendTransactionFunc) (err error) {
mbl := &MysqlBinlog{}
reader, err := mbl.Launch(bls.dbname, bls.file.name, bls.file.pos)
if err != nil {
return fmt.Errorf("launch error: %v", err)
}
defer reader.Close()
err = bls.parseEvents(sendTransaction, reader)
if err != nil {
mbl.Kill()
} else {
err = mbl.Wait()
if err != nil {
err = fmt.Errorf("wait error: %v", err)
}
}
return err
}
// parseEvents parses events and transmits them as transactions for the current mysqlbinlog stream.
func (bls *BinlogStreamer) parseEvents(sendTransaction sendTransactionFunc, reader io.Reader) (err error) {
bls.delim = DEFAULT_DELIM
bufReader := bufio.NewReader(reader)
var statements []proto.Statement
for {
sql, err := bls.nextStatement(bufReader)
if sql == nil {
return err
}
prefix := string(bytes.ToLower(bytes.SplitN(sql, SPACE, 2)[0]))
switch category := statementPrefixes[prefix]; category {
// We trust that mysqlbinlog doesn't send proto.BL_DMLs withot a proto.BL_BEGIN
case proto.BL_BEGIN, proto.BL_ROLLBACK:
statements = nil
case proto.BL_DDL:
statements = append(statements, proto.Statement{Category: category, Sql: sql})
fallthrough
case proto.BL_COMMIT:
trans := &proto.BinlogTransaction{
Statements: statements,
GroupId: bls.blPos.GroupId,
}
if err = sendTransaction(trans); err != nil {
if err == io.EOF {
return err
}
return fmt.Errorf("send reply error: %v", err)
}
statements = nil
// proto.BL_DML, proto.BL_SET or proto.BL_UNRECOGNIZED
default:
statements = append(statements, proto.Statement{Category: category, Sql: sql})
}
}
}
// nextStatement returns the next statement encountered in the binlog stream. If there are
// positional comments, it updates the BinlogStreamer state. It also ignores events that
// are not material. If it returns nil, it's the end of stream. If err is also nil, then
// it was due to a normal termination.
func (bls *BinlogStreamer) nextStatement(bufReader *bufio.Reader) (stmt []byte, err error) {
eventLoop:
for {
// Stop processing if we're shutting down
if bls.svm.State() != sync2.SERVICE_RUNNING {
return nil, io.EOF
}
event, err := bls.readEvent(bufReader)
if err != nil {
if err == io.EOF {
return nil, nil
}
return nil, err
}
values := posRE.FindSubmatch(event)
if values != nil {
bls.blPos.ServerId = mustParseInt64(values[1])
bls.file.Set(mustParseInt64(values[2]))
bls.blPos.GroupId = mustParseInt64(values[3])
continue
}
values = rotateRE.FindSubmatch(event)
if values != nil {
err = bls.file.Rotate(path.Join(bls.dir, string(values[1])), mustParseInt64(values[2]))
if err != nil {
return nil, err
}
continue
}
values = delimRE.FindSubmatch(event)
if values != nil {
bls.delim = values[1]
continue
}
for _, ignorePrefix := range ignorePrefixes {
if bytes.HasPrefix(event, ignorePrefix) {
continue eventLoop
}
}
return event, nil
}
}
// readEvent reads a single binlog event. It can be a single comment line,
// or multiple lines terminated by the delimiter.
func (bls *BinlogStreamer) readEvent(bufReader *bufio.Reader) (event []byte, err error) {
for {
fragment, err := bufReader.ReadSlice('\n')
event = append(event, fragment...)
if err == bufio.ErrBufferFull {
continue
}
if err != nil {
if err == io.EOF {
return event, err
}
return event, fmt.Errorf("read error: %v", err)
}
// Use a different var than event, because you have to keep
// the trailing \n if we continue
trimmed := bytes.TrimSpace(event)
if bytes.HasPrefix(trimmed, HASH_COMMENT) ||
bytes.HasPrefix(trimmed, SLASH_COMMENT) ||
bytes.HasPrefix(trimmed, DELIM_STMT) {
return trimmed, nil
}
if bytes.HasSuffix(trimmed, bls.delim) {
return bytes.TrimSpace(trimmed[:len(trimmed)-len(bls.delim)]), nil
}
}
}
// fileInfo is used to track the current binlog file and position.
type fileInfo struct {
name string
pos int64
handle *os.File
}
func (f *fileInfo) Init(name string, pos int64) error {
err := f.Rotate(name, pos)
if err != nil {
return err
}
// Make sure the current file hasn't rotated.
next := nextFileName(name)
fi, _ := os.Stat(next)
if fi == nil {
// Assume next file doesn't exist
return nil
}
// Next file exists. Check if current file size matches position
fi, err = f.handle.Stat()
if err != nil {
return err
}
if fi.Size() <= pos {
// The file has rotated
return f.Rotate(next, 4)
}
return nil
}
func (f *fileInfo) Rotate(name string, pos int64) (err error) {
if f.handle != nil {
f.handle.Close()
}
f.name = name
f.pos = pos
f.handle, err = os.Open(name)
if err != nil {
return fmt.Errorf("open error: %v", err)
}
return nil
}
func (f *fileInfo) Set(pos int64) {
f.pos = pos
}
func (f *fileInfo) WaitForChange(svm *sync2.ServiceManager) error {
for {
// Stop waiting if we're shutting down
if svm.State() != sync2.SERVICE_RUNNING {
return io.EOF
}
time.Sleep(100 * time.Millisecond)
fi, err := f.handle.Stat()
if err != nil {
return fmt.Errorf("stat error: %v", err)
}
if fi.Size() != f.pos {
return nil
}
}
}
func (f *fileInfo) Close() (err error) {
if f.handle == nil {
return nil
}
err = f.handle.Close()
f.handle = nil
return fmt.Errorf("close error: %v", err)
}
func nextFileName(name string) string {
newname := []byte(name)
index := len(newname) - 1
for newname[index] == '9' && index > 0 {
newname[index] = '0'
index--
}
newname[index] += 1
return string(newname)
}
// mustParseInt64 can be used if you don't expect to fail.
func mustParseInt64(b []byte) int64 {
val, err := strconv.ParseInt(string(b), 10, 64)
if err != nil {
panic(err)
}
return val
}