-
Notifications
You must be signed in to change notification settings - Fork 0
/
binlog.go
412 lines (347 loc) · 10.7 KB
/
binlog.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
package livesql
import (
"context"
"database/sql"
"errors"
"fmt"
"os"
"reflect"
"strings"
"sync"
"time"
"github.com/base-go/baseql/internal/fields"
"github.com/base-go/baseql/logger"
"github.com/base-go/baseql/sqlgen"
"github.com/go-mysql-org/go-mysql/mysql"
"github.com/go-mysql-org/go-mysql/replication"
)
// Binlog streams the MySQL binary replication log, parses it, and broadcasts
// updates
type Binlog struct {
db *sqlgen.DB
tracker *dbTracker
database string
syncer *replication.BinlogSyncer
streamer *replication.BinlogStreamer
tableVersions map[string]uint64
delayMu sync.Mutex
delay time.Duration
mu sync.Mutex
columnMaps map[string]*columnMap
closed bool
logger logger.Logger
}
// checkVariable checks that the requested MySQL global variable matches
// an expected configuration
func checkVariable(conn *sql.DB, variable, expected string) error {
row := conn.QueryRow(fmt.Sprintf(`SHOW GLOBAL VARIABLES LIKE "%s"`, variable))
var value string
var ignored interface{}
if err := row.Scan(&ignored, &value); err != nil {
return fmt.Errorf("error reading MySQL variable %s: %s", variable, err)
}
if !strings.EqualFold(value, expected) {
return fmt.Errorf("expected MySQL variable %s to be %s, but got %s", variable, expected, value)
}
return nil
}
// getPosition fetches the current MySQL binlog position
func getPosition(conn *sql.DB) (mysql.Position, error) {
row := conn.QueryRow("SHOW MASTER STATUS")
var position mysql.Position
var ignored interface{}
if err := row.Scan(&position.Name, &position.Pos, &ignored, &ignored, &ignored); err != nil {
return mysql.Position{}, fmt.Errorf("error retrieving MySQL binlog position: %s", err)
}
return position, nil
}
// NewBinlog constructs a new Binlog for a given DB.
func NewBinlog(ldb *LiveDB, host string, port uint16, username, password, database string) (*Binlog, error) {
return NewBinlogWithSource(ldb, ldb.Conn, host, port, username, password, database)
}
// NewBinlogWithSource constructs a new Binlog for a given DB and a source DB
//
// NewBinlogWithSource verifies that the given DB has been correctly configured for
// streaming changes.
func NewBinlogWithSource(ldb *LiveDB, sourceDB *sql.DB, host string, port uint16, username, password, database string) (*Binlog, error) {
db := ldb.DB
tracker := ldb.tracker
if err := checkVariable(sourceDB, "binlog_format", "ROW"); err != nil {
return nil, err
}
if err := checkVariable(sourceDB, "binlog_row_image", "FULL"); err != nil {
return nil, err
}
// Slave hosts have a max hostname length of 60 characters.
const maxHostNameLength = 60
localHostName, err := os.Hostname()
if err != nil {
localHostName = "localhost"
}
if len(localHostName) > maxHostNameLength {
runes := []rune(localHostName)
localHostName = string(runes[0:maxHostNameLength])
}
return &Binlog{
db: db,
database: database,
tracker: tracker,
tableVersions: make(map[string]uint64),
columnMaps: make(map[string]*columnMap),
logger: logger.New(),
}, nil
}
// columnMap stores a column permutation indices
type columnMap struct {
expectedColumns int
source []int
}
// fetchColumns fetches the columns of a table
func fetchColumns(conn *sql.DB, database string, table string) ([]string, error) {
rows, err := conn.Query(`
SELECT column_name
FROM information_schema.columns
WHERE table_schema = ? AND table_name = ?
ORDER BY ordinal_position`, database, table)
if err != nil {
return nil, err
}
defer rows.Close()
var columns []string
for rows.Next() {
var column string
if err := rows.Scan(&column); err != nil {
return nil, err
}
columns = append(columns, column)
}
if err := rows.Err(); err != nil {
return nil, err
}
return columns, nil
}
// buildColumnMap constructs a columnMap from column information fetched from
// the database
func buildColumnMap(conn *sql.DB, database string, table *sqlgen.Table) (*columnMap, error) {
columns, err := fetchColumns(conn, database, table.Name)
if err != nil {
return nil, err
}
columnMap := &columnMap{
expectedColumns: len(columns),
}
columnIndex := make(map[string]int)
for i, column := range columns {
columnIndex[column] = i
}
for _, column := range table.Columns {
if idx, ok := columnIndex[column.Name]; ok {
columnMap.source = append(columnMap.source, idx)
} else {
columnMap.source = append(columnMap.source, -1)
}
}
return columnMap, nil
}
// parseBinlogRow parses a binlog row into a struct
func parseBinlogRow(table *sqlgen.Table, binlogRow []interface{}, columnMap *columnMap) (interface{}, error) {
ptr := reflect.New(table.Type)
elem := ptr.Elem()
if len(binlogRow) != columnMap.expectedColumns {
return nil, fmt.Errorf("binlog for %s has %d columns, expected %d",
table.Name, len(binlogRow), columnMap.expectedColumns)
}
scanners := table.Scanners.Get().([]interface{})
defer table.Scanners.Put(scanners)
for i, j := range columnMap.source {
if j == -1 {
continue
}
field := elem.FieldByIndex(table.Columns[i].Index)
if field.Kind() != reflect.Ptr {
field = field.Addr()
}
scanner := scanners[i].(*fields.Scanner)
scanner.Target(field)
if err := scanner.Scan(binlogRow[j]); err != nil {
return nil, fmt.Errorf("binlog: `%s`.`%s` error: %v", table.Name, table.Columns[i].Name, err)
}
}
return ptr.Interface(), nil
}
// getColumnMap returns the a column map for the table, fetching schema
// information if necessary
func (b *Binlog) getColumnMap(table *sqlgen.Table) (*columnMap, error) {
if columnMap, ok := b.columnMaps[table.Name]; ok {
return columnMap, nil
}
columnMap, err := buildColumnMap(b.db.Conn, b.database, table)
if err != nil {
return nil, err
}
b.columnMaps[table.Name] = columnMap
return columnMap, nil
}
var errNoDescriptor = errors.New("no known descriptor")
// delta represents an update to a SQL row
//
// before or after are nil if the row was newly added or deleted
type delta struct {
key string
before, after interface{}
}
// update holds a set of updates to a SQL table
//
// If the binlog had trouble parsing the update, err will be non-nil.
type update struct {
table string
deltas []delta
err error
}
// delayedUpdate holds an update and the earlist timestamp the update can be applied.
type delayedUpdate struct {
*update
applyAfter time.Time
}
// parseBinlogRowsEvent transforms a raw binlog rows event into an *update
//
// Because the binlog does not include a detailed table schema,
// parseBinlogRowsEvent uses the *DB to fetch the table's schema
func (b *Binlog) parseBinlogRowsEvent(event *replication.BinlogEvent) (*update, error) {
rowsEvent, ok := event.Event.(*replication.RowsEvent)
if !ok {
return nil, errors.New("event is not a rows event")
}
update := &update{
table: string(rowsEvent.Table.Table),
}
schema, ok := b.db.Schema.ByName[update.table]
if !ok {
return nil, errNoDescriptor
}
columnMap, err := b.getColumnMap(schema)
if err != nil {
return nil, err
}
// Transform rows into deltas:
switch event.Header.EventType {
case replication.WRITE_ROWS_EVENTv1, replication.WRITE_ROWS_EVENTv2:
for _, binlogRow := range rowsEvent.Rows {
fields, err := parseBinlogRow(schema, binlogRow, columnMap)
if err != nil {
return nil, err
}
update.deltas = append(update.deltas, delta{after: fields})
}
case replication.UPDATE_ROWS_EVENTv1, replication.UPDATE_ROWS_EVENTv2:
if len(rowsEvent.Rows)%2 != 0 {
return nil, errors.New("expected even number of rows for update event")
}
for i := 0; i < len(rowsEvent.Rows); i += 2 {
before, err := parseBinlogRow(schema, rowsEvent.Rows[i], columnMap)
if err != nil {
return nil, err
}
after, err := parseBinlogRow(schema, rowsEvent.Rows[i+1], columnMap)
if err != nil {
return nil, err
}
update.deltas = append(update.deltas, delta{before: before, after: after})
}
case replication.DELETE_ROWS_EVENTv1, replication.DELETE_ROWS_EVENTv2:
for _, binlogRow := range rowsEvent.Rows {
fields, err := parseBinlogRow(schema, binlogRow, columnMap)
if err != nil {
return nil, err
}
update.deltas = append(update.deltas, delta{before: fields})
}
default:
return nil, fmt.Errorf("unknown event type %s", event.Header.EventType.String())
}
return update, nil
}
// SetOutput sets the destination for the error logger.
func (b *Binlog) SetLogger(l logger.Logger) {
b.logger = l
}
// SetUpdateDelay sets the duration by which future updates will be delayed.
func (b *Binlog) SetUpdateDelay(d time.Duration) {
b.delayMu.Lock()
defer b.delayMu.Unlock()
b.delay = d
}
// RunPollLoop is the core binlog function that fetches and distributes updates
// from MySQL
func (b *Binlog) RunPollLoop() error {
updateCh := make(chan delayedUpdate, 1024)
defer close(updateCh)
go func() {
for du := range updateCh {
time.Sleep(du.applyAfter.Sub(time.Now()))
b.tracker.processBinlog(du.update)
}
}()
for {
event, err := b.streamer.GetEvent(context.Background())
if err != nil {
b.mu.Lock()
defer b.mu.Unlock()
if b.closed {
return nil
}
return err
}
switch inner := event.Event.(type) {
case *replication.RowsEvent:
if string(inner.Table.Schema) != b.database {
continue
}
u, err := b.parseBinlogRowsEvent(event)
if err == errNoDescriptor {
continue
} else if err != nil && err.Error() == "sql: database is closed" {
continue
} else if err != nil {
b.logger.Error("livesql: failed to parse rows event", "error", err)
continue
}
b.delayMu.Lock()
delay := b.delay
b.delayMu.Unlock()
updateCh <- delayedUpdate{update: u, applyAfter: time.Now().Add(delay)}
case *replication.TableMapEvent:
if string(inner.Schema) != b.database {
continue
}
table := string(inner.Table)
if version, found := b.tableVersions[table]; !found || version != inner.TableID {
// According to the MySQL source, the TableID is unique for every
// version of the table schema (though not persistent across server
// restarts.)
//
// Whenever the table ID changes, the schema might have changed and we
// flus the stored MySQL column information so we have the correct set
// of columns ready before parsing.
//
// If an update quickly happens twice in a row, we might end up with a
// set of columns that is too new. If the number of columns has
// changed, we detect the error. If the number of columns stays the
// same, we might return garbage data and miss invalidations.
// Hopefully that happens rarely.
b.tableVersions[table] = inner.TableID
delete(b.columnMaps, table)
}
}
}
}
func (b *Binlog) Close() error {
b.mu.Lock()
defer b.mu.Unlock()
if b.closed {
return nil
}
b.closed = true
b.syncer.Close()
return nil
}