2016-03-30 13:43:40 +00:00
|
|
|
/*
|
2022-05-31 19:23:39 +00:00
|
|
|
Copyright 2022 GitHub Inc.
|
2016-05-16 09:09:17 +00:00
|
|
|
See https://github.com/github/gh-ost/blob/master/LICENSE
|
2016-03-30 13:43:40 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
package binlog
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2016-05-23 09:12:59 +00:00
|
|
|
"sync"
|
2016-03-30 13:43:40 +00:00
|
|
|
|
2016-12-28 09:25:42 +00:00
|
|
|
"github.com/github/gh-ost/go/base"
|
2016-05-16 09:09:17 +00:00
|
|
|
"github.com/github/gh-ost/go/mysql"
|
|
|
|
"github.com/github/gh-ost/go/sql"
|
2016-04-06 16:44:54 +00:00
|
|
|
|
2021-07-15 19:49:50 +00:00
|
|
|
gomysql "github.com/go-mysql-org/go-mysql/mysql"
|
|
|
|
"github.com/go-mysql-org/go-mysql/replication"
|
2017-02-12 11:15:53 +00:00
|
|
|
"golang.org/x/net/context"
|
2016-03-30 13:43:40 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type GoMySQLReader struct {
|
2019-10-08 17:49:15 +00:00
|
|
|
migrationContext *base.MigrationContext
|
2016-05-23 09:12:59 +00:00
|
|
|
connectionConfig *mysql.ConnectionConfig
|
|
|
|
binlogSyncer *replication.BinlogSyncer
|
|
|
|
binlogStreamer *replication.BinlogStreamer
|
|
|
|
currentCoordinates mysql.BinlogCoordinates
|
|
|
|
currentCoordinatesMutex *sync.Mutex
|
|
|
|
LastAppliedRowsEventHint mysql.BinlogCoordinates
|
2016-03-30 13:43:40 +00:00
|
|
|
}
|
|
|
|
|
2022-07-06 21:45:26 +00:00
|
|
|
func NewGoMySQLReader(migrationContext *base.MigrationContext) *GoMySQLReader {
|
|
|
|
connectionConfig := migrationContext.InspectorConnectionConfig
|
|
|
|
return &GoMySQLReader{
|
2019-10-08 17:49:15 +00:00
|
|
|
migrationContext: migrationContext,
|
2022-07-06 21:45:26 +00:00
|
|
|
connectionConfig: connectionConfig,
|
2016-05-23 09:12:59 +00:00
|
|
|
currentCoordinates: mysql.BinlogCoordinates{},
|
|
|
|
currentCoordinatesMutex: &sync.Mutex{},
|
2022-07-06 21:45:26 +00:00
|
|
|
binlogSyncer: replication.NewBinlogSyncer(replication.BinlogSyncerConfig{
|
|
|
|
ServerID: uint32(migrationContext.ReplicaServerId),
|
|
|
|
Flavor: gomysql.MySQLFlavor,
|
|
|
|
Host: connectionConfig.Key.Hostname,
|
|
|
|
Port: uint16(connectionConfig.Key.Port),
|
|
|
|
User: connectionConfig.User,
|
|
|
|
Password: connectionConfig.Password,
|
|
|
|
TLSConfig: connectionConfig.TLSConfig(),
|
|
|
|
UseDecimal: true,
|
|
|
|
}),
|
2016-03-30 13:43:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-07 13:57:12 +00:00
|
|
|
// ConnectBinlogStreamer
|
|
|
|
func (this *GoMySQLReader) ConnectBinlogStreamer(coordinates mysql.BinlogCoordinates) (err error) {
|
2016-05-19 13:11:36 +00:00
|
|
|
if coordinates.IsEmpty() {
|
2019-10-08 17:49:15 +00:00
|
|
|
return this.migrationContext.Log.Errorf("Empty coordinates at ConnectBinlogStreamer()")
|
2016-05-19 13:11:36 +00:00
|
|
|
}
|
|
|
|
|
2016-04-07 13:57:12 +00:00
|
|
|
this.currentCoordinates = coordinates
|
2019-10-08 17:49:15 +00:00
|
|
|
this.migrationContext.Log.Infof("Connecting binlog streamer at %+v", this.currentCoordinates)
|
2017-11-08 01:01:51 +00:00
|
|
|
// Start sync with specified binlog file and position
|
2022-05-31 19:23:39 +00:00
|
|
|
this.binlogStreamer, err = this.binlogSyncer.StartSync(gomysql.Position{
|
|
|
|
Name: this.currentCoordinates.LogFile,
|
|
|
|
Pos: uint32(this.currentCoordinates.LogPos),
|
|
|
|
})
|
2016-04-07 13:57:12 +00:00
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-05-19 13:11:36 +00:00
|
|
|
func (this *GoMySQLReader) GetCurrentBinlogCoordinates() *mysql.BinlogCoordinates {
|
2016-05-23 09:12:59 +00:00
|
|
|
this.currentCoordinatesMutex.Lock()
|
|
|
|
defer this.currentCoordinatesMutex.Unlock()
|
|
|
|
returnCoordinates := this.currentCoordinates
|
|
|
|
return &returnCoordinates
|
2016-05-19 13:11:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// StreamEvents
|
|
|
|
func (this *GoMySQLReader) handleRowsEvent(ev *replication.BinlogEvent, rowsEvent *replication.RowsEvent, entriesChannel chan<- *BinlogEntry) error {
|
2016-05-23 09:12:59 +00:00
|
|
|
if this.currentCoordinates.SmallerThanOrEquals(&this.LastAppliedRowsEventHint) {
|
2019-10-08 17:49:15 +00:00
|
|
|
this.migrationContext.Log.Debugf("Skipping handled query at %+v", this.currentCoordinates)
|
2016-05-19 13:11:36 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
dml := ToEventDML(ev.Header.EventType.String())
|
|
|
|
if dml == NotDML {
|
|
|
|
return fmt.Errorf("Unknown DML type: %s", ev.Header.EventType.String())
|
|
|
|
}
|
|
|
|
for i, row := range rowsEvent.Rows {
|
|
|
|
if dml == UpdateDML && i%2 == 1 {
|
|
|
|
// An update has two rows (WHERE+SET)
|
|
|
|
// We do both at the same time
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
binlogEntry := NewBinlogEntryAt(this.currentCoordinates)
|
|
|
|
binlogEntry.DmlEvent = NewBinlogDMLEvent(
|
|
|
|
string(rowsEvent.Table.Schema),
|
|
|
|
string(rowsEvent.Table.Table),
|
|
|
|
dml,
|
|
|
|
)
|
|
|
|
switch dml {
|
|
|
|
case InsertDML:
|
|
|
|
{
|
|
|
|
binlogEntry.DmlEvent.NewColumnValues = sql.ToColumnValues(row)
|
|
|
|
}
|
|
|
|
case UpdateDML:
|
|
|
|
{
|
|
|
|
binlogEntry.DmlEvent.WhereColumnValues = sql.ToColumnValues(row)
|
|
|
|
binlogEntry.DmlEvent.NewColumnValues = sql.ToColumnValues(rowsEvent.Rows[i+1])
|
|
|
|
}
|
|
|
|
case DeleteDML:
|
|
|
|
{
|
|
|
|
binlogEntry.DmlEvent.WhereColumnValues = sql.ToColumnValues(row)
|
|
|
|
}
|
|
|
|
}
|
2018-04-27 06:58:07 +00:00
|
|
|
// The channel will do the throttling. Whoever is reading from the channel
|
2017-11-08 01:02:21 +00:00
|
|
|
// decides whether action is taken synchronously (meaning we wait before
|
2016-05-19 13:11:36 +00:00
|
|
|
// next iteration) or asynchronously (we keep pushing more events)
|
|
|
|
// In reality, reads will be synchronous
|
|
|
|
entriesChannel <- binlogEntry
|
|
|
|
}
|
2016-05-23 09:12:59 +00:00
|
|
|
this.LastAppliedRowsEventHint = this.currentCoordinates
|
2016-05-19 13:11:36 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-04-07 13:57:12 +00:00
|
|
|
// StreamEvents
|
|
|
|
func (this *GoMySQLReader) StreamEvents(canStopStreaming func() bool, entriesChannel chan<- *BinlogEntry) error {
|
2016-10-27 11:52:37 +00:00
|
|
|
if canStopStreaming() {
|
|
|
|
return nil
|
|
|
|
}
|
2016-04-07 13:57:12 +00:00
|
|
|
for {
|
|
|
|
if canStopStreaming() {
|
|
|
|
break
|
|
|
|
}
|
2017-02-12 11:15:53 +00:00
|
|
|
ev, err := this.binlogStreamer.GetEvent(context.Background())
|
2016-04-07 13:57:12 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-05-23 09:12:59 +00:00
|
|
|
func() {
|
|
|
|
this.currentCoordinatesMutex.Lock()
|
|
|
|
defer this.currentCoordinatesMutex.Unlock()
|
|
|
|
this.currentCoordinates.LogPos = int64(ev.Header.LogPos)
|
|
|
|
}()
|
2022-07-06 21:45:26 +00:00
|
|
|
|
|
|
|
switch binlogEvent := ev.Event.(type) {
|
|
|
|
case *replication.RotateEvent:
|
2016-05-23 09:12:59 +00:00
|
|
|
func() {
|
|
|
|
this.currentCoordinatesMutex.Lock()
|
|
|
|
defer this.currentCoordinatesMutex.Unlock()
|
2022-07-06 21:45:26 +00:00
|
|
|
this.currentCoordinates.LogFile = string(binlogEvent.NextLogName)
|
2016-05-23 09:12:59 +00:00
|
|
|
}()
|
2022-07-06 21:45:26 +00:00
|
|
|
this.migrationContext.Log.Infof("rotate to next log from %s:%d to %s", this.currentCoordinates.LogFile, int64(ev.Header.LogPos), binlogEvent.NextLogName)
|
|
|
|
case *replication.RowsEvent:
|
|
|
|
if err := this.handleRowsEvent(ev, binlogEvent, entriesChannel); err != nil {
|
2016-05-19 13:11:36 +00:00
|
|
|
return err
|
2016-04-07 13:57:12 +00:00
|
|
|
}
|
|
|
|
}
|
2016-03-30 13:43:40 +00:00
|
|
|
}
|
2019-10-08 17:49:15 +00:00
|
|
|
this.migrationContext.Log.Debugf("done streaming events")
|
2016-04-07 13:57:12 +00:00
|
|
|
|
|
|
|
return nil
|
2016-03-30 13:43:40 +00:00
|
|
|
}
|
2016-10-27 11:52:37 +00:00
|
|
|
|
|
|
|
func (this *GoMySQLReader) Close() error {
|
2017-12-18 19:03:37 +00:00
|
|
|
this.binlogSyncer.Close()
|
2016-10-27 11:52:37 +00:00
|
|
|
return nil
|
|
|
|
}
|