2016-04-06 11:05:21 +00:00
|
|
|
/*
|
|
|
|
Copyright 2016 GitHub Inc.
|
2016-05-16 09:09:17 +00:00
|
|
|
See https://github.com/github/gh-ost/blob/master/LICENSE
|
2016-04-06 11:05:21 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
package logic
|
|
|
|
|
|
|
|
import (
|
|
|
|
gosql "database/sql"
|
|
|
|
"fmt"
|
2016-04-07 13:57:12 +00:00
|
|
|
"strings"
|
2016-05-16 09:03:15 +00:00
|
|
|
"sync"
|
2016-05-20 10:52:14 +00:00
|
|
|
"time"
|
2016-04-07 13:57:12 +00:00
|
|
|
|
2016-05-16 09:09:17 +00:00
|
|
|
"github.com/github/gh-ost/go/base"
|
|
|
|
"github.com/github/gh-ost/go/binlog"
|
|
|
|
"github.com/github/gh-ost/go/mysql"
|
2016-04-06 11:05:21 +00:00
|
|
|
|
|
|
|
"github.com/outbrain/golib/log"
|
|
|
|
"github.com/outbrain/golib/sqlutils"
|
|
|
|
)
|
|
|
|
|
|
|
|
type BinlogEventListener struct {
|
|
|
|
async bool
|
|
|
|
databaseName string
|
|
|
|
tableName string
|
2016-04-06 16:44:54 +00:00
|
|
|
onDmlEvent func(event *binlog.BinlogDMLEvent) error
|
2016-04-06 11:05:21 +00:00
|
|
|
}
|
|
|
|
|
2016-04-07 13:57:12 +00:00
|
|
|
const (
|
2016-05-20 10:52:14 +00:00
|
|
|
EventsChannelBufferSize = 1
|
|
|
|
ReconnectStreamerSleepSeconds = 5
|
2016-04-07 13:57:12 +00:00
|
|
|
)
|
|
|
|
|
2016-04-06 11:05:21 +00:00
|
|
|
// EventsStreamer reads data from binary logs and streams it on. It acts as a publisher,
|
|
|
|
// and interested parties may subscribe for per-table events.
|
|
|
|
type EventsStreamer struct {
|
2016-05-19 13:11:36 +00:00
|
|
|
connectionConfig *mysql.ConnectionConfig
|
|
|
|
db *gosql.DB
|
|
|
|
migrationContext *base.MigrationContext
|
|
|
|
initialBinlogCoordinates *mysql.BinlogCoordinates
|
|
|
|
listeners [](*BinlogEventListener)
|
|
|
|
listenersMutex *sync.Mutex
|
|
|
|
eventsChannel chan *binlog.BinlogEntry
|
2016-05-23 09:12:59 +00:00
|
|
|
binlogReader *binlog.GoMySQLReader
|
2016-04-06 11:05:21 +00:00
|
|
|
}
|
|
|
|
|
2017-08-08 20:36:54 +00:00
|
|
|
func NewEventsStreamer(migrationContext *base.MigrationContext) *EventsStreamer {
|
2016-04-06 11:05:21 +00:00
|
|
|
return &EventsStreamer{
|
2017-08-08 20:36:54 +00:00
|
|
|
connectionConfig: migrationContext.InspectorConnectionConfig,
|
|
|
|
migrationContext: migrationContext,
|
2016-04-06 11:05:21 +00:00
|
|
|
listeners: [](*BinlogEventListener){},
|
2016-05-16 09:03:15 +00:00
|
|
|
listenersMutex: &sync.Mutex{},
|
2016-04-07 13:57:12 +00:00
|
|
|
eventsChannel: make(chan *binlog.BinlogEntry, EventsChannelBufferSize),
|
2016-04-06 11:05:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-19 15:55:37 +00:00
|
|
|
// AddListener registers a new listener for binlog events, on a per-table basis
|
2016-04-06 11:05:21 +00:00
|
|
|
func (this *EventsStreamer) AddListener(
|
2016-04-06 16:44:54 +00:00
|
|
|
async bool, databaseName string, tableName string, onDmlEvent func(event *binlog.BinlogDMLEvent) error) (err error) {
|
2016-05-16 09:03:15 +00:00
|
|
|
|
|
|
|
this.listenersMutex.Lock()
|
|
|
|
defer this.listenersMutex.Unlock()
|
|
|
|
|
2016-04-06 11:05:21 +00:00
|
|
|
if databaseName == "" {
|
|
|
|
return fmt.Errorf("Empty database name in AddListener")
|
|
|
|
}
|
|
|
|
if tableName == "" {
|
|
|
|
return fmt.Errorf("Empty table name in AddListener")
|
|
|
|
}
|
|
|
|
listener := &BinlogEventListener{
|
|
|
|
async: async,
|
|
|
|
databaseName: databaseName,
|
|
|
|
tableName: tableName,
|
2016-04-06 16:44:54 +00:00
|
|
|
onDmlEvent: onDmlEvent,
|
2016-04-06 11:05:21 +00:00
|
|
|
}
|
|
|
|
this.listeners = append(this.listeners, listener)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-06-19 15:55:37 +00:00
|
|
|
// notifyListeners will notify relevant listeners with given DML event. Only
|
|
|
|
// listeners registered for changes on the table on which the DML operates are notified.
|
2016-04-06 16:44:54 +00:00
|
|
|
func (this *EventsStreamer) notifyListeners(binlogEvent *binlog.BinlogDMLEvent) {
|
2016-05-16 09:03:15 +00:00
|
|
|
this.listenersMutex.Lock()
|
|
|
|
defer this.listenersMutex.Unlock()
|
|
|
|
|
2016-04-06 11:05:21 +00:00
|
|
|
for _, listener := range this.listeners {
|
2016-05-19 13:11:36 +00:00
|
|
|
listener := listener
|
2016-04-07 13:57:12 +00:00
|
|
|
if strings.ToLower(listener.databaseName) != strings.ToLower(binlogEvent.DatabaseName) {
|
2016-04-06 11:05:21 +00:00
|
|
|
continue
|
|
|
|
}
|
2016-04-07 13:57:12 +00:00
|
|
|
if strings.ToLower(listener.tableName) != strings.ToLower(binlogEvent.TableName) {
|
2016-04-06 11:05:21 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
if listener.async {
|
|
|
|
go func() {
|
2016-05-19 13:11:36 +00:00
|
|
|
listener.onDmlEvent(binlogEvent)
|
2016-04-06 11:05:21 +00:00
|
|
|
}()
|
|
|
|
} else {
|
2016-05-19 13:11:36 +00:00
|
|
|
listener.onDmlEvent(binlogEvent)
|
2016-04-06 11:05:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (this *EventsStreamer) InitDBConnections() (err error) {
|
|
|
|
EventsStreamerUri := this.connectionConfig.GetDBUri(this.migrationContext.DatabaseName)
|
2017-12-14 23:53:29 +00:00
|
|
|
if this.db, _, err = mysql.GetDB(this.migrationContext.Uuid, EventsStreamerUri); err != nil {
|
2016-04-06 11:05:21 +00:00
|
|
|
return err
|
|
|
|
}
|
2017-09-06 18:25:35 +00:00
|
|
|
if _, err := base.ValidateConnection(this.db, this.connectionConfig); err != nil {
|
2016-04-06 11:05:21 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := this.readCurrentBinlogCoordinates(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-05-23 09:12:59 +00:00
|
|
|
if err := this.initBinlogReader(this.initialBinlogCoordinates); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-06-19 15:55:37 +00:00
|
|
|
// initBinlogReader creates and connects the reader: we hook up to a MySQL server as a replica
|
2016-05-23 09:12:59 +00:00
|
|
|
func (this *EventsStreamer) initBinlogReader(binlogCoordinates *mysql.BinlogCoordinates) error {
|
2017-08-08 20:41:46 +00:00
|
|
|
goMySQLReader, err := binlog.NewGoMySQLReader(this.migrationContext)
|
2016-04-07 13:57:12 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-05-23 09:12:59 +00:00
|
|
|
if err := goMySQLReader.ConnectBinlogStreamer(*binlogCoordinates); err != nil {
|
2016-04-07 13:57:12 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
this.binlogReader = goMySQLReader
|
2016-04-06 11:05:21 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-05-19 13:11:36 +00:00
|
|
|
func (this *EventsStreamer) GetCurrentBinlogCoordinates() *mysql.BinlogCoordinates {
|
|
|
|
return this.binlogReader.GetCurrentBinlogCoordinates()
|
|
|
|
}
|
|
|
|
|
2016-05-23 09:12:59 +00:00
|
|
|
func (this *EventsStreamer) GetReconnectBinlogCoordinates() *mysql.BinlogCoordinates {
|
|
|
|
return &mysql.BinlogCoordinates{LogFile: this.GetCurrentBinlogCoordinates().LogFile, LogPos: 4}
|
|
|
|
}
|
|
|
|
|
2016-06-19 15:55:37 +00:00
|
|
|
// readCurrentBinlogCoordinates reads master status from hooked server
|
2016-04-06 11:05:21 +00:00
|
|
|
func (this *EventsStreamer) readCurrentBinlogCoordinates() error {
|
2016-05-16 09:09:17 +00:00
|
|
|
query := `show /* gh-ost readCurrentBinlogCoordinates */ master status`
|
2016-04-06 11:05:21 +00:00
|
|
|
foundMasterStatus := false
|
|
|
|
err := sqlutils.QueryRowsMap(this.db, query, func(m sqlutils.RowMap) error {
|
2016-05-19 13:11:36 +00:00
|
|
|
this.initialBinlogCoordinates = &mysql.BinlogCoordinates{
|
2016-04-06 11:05:21 +00:00
|
|
|
LogFile: m.GetString("File"),
|
|
|
|
LogPos: m.GetInt64("Position"),
|
|
|
|
}
|
|
|
|
foundMasterStatus = true
|
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if !foundMasterStatus {
|
|
|
|
return fmt.Errorf("Got no results from SHOW MASTER STATUS. Bailing out")
|
|
|
|
}
|
2016-05-19 13:11:36 +00:00
|
|
|
log.Debugf("Streamer binlog coordinates: %+v", *this.initialBinlogCoordinates)
|
2016-04-06 11:05:21 +00:00
|
|
|
return nil
|
|
|
|
}
|
2016-04-07 13:57:12 +00:00
|
|
|
|
|
|
|
// StreamEvents will begin streaming events. It will be blocking, so should be
|
|
|
|
// executed by a goroutine
|
|
|
|
func (this *EventsStreamer) StreamEvents(canStopStreaming func() bool) error {
|
|
|
|
go func() {
|
|
|
|
for binlogEntry := range this.eventsChannel {
|
|
|
|
if binlogEntry.DmlEvent != nil {
|
|
|
|
this.notifyListeners(binlogEntry.DmlEvent)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
2016-05-19 13:11:36 +00:00
|
|
|
// The next should block and execute forever, unless there's a serious error
|
2016-07-25 13:17:30 +00:00
|
|
|
var successiveFailures int64
|
|
|
|
var lastAppliedRowsEventHint mysql.BinlogCoordinates
|
2016-05-19 13:11:36 +00:00
|
|
|
for {
|
2017-08-28 21:05:15 +00:00
|
|
|
if canStopStreaming() {
|
|
|
|
return nil
|
|
|
|
}
|
2016-05-19 13:11:36 +00:00
|
|
|
if err := this.binlogReader.StreamEvents(canStopStreaming, this.eventsChannel); err != nil {
|
2017-08-28 21:05:15 +00:00
|
|
|
if canStopStreaming() {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-05-19 13:11:36 +00:00
|
|
|
log.Infof("StreamEvents encountered unexpected error: %+v", err)
|
2016-05-25 10:27:58 +00:00
|
|
|
this.migrationContext.MarkPointOfInterest()
|
2016-05-20 10:52:14 +00:00
|
|
|
time.Sleep(ReconnectStreamerSleepSeconds * time.Second)
|
2016-05-23 09:12:59 +00:00
|
|
|
|
2016-07-25 13:17:30 +00:00
|
|
|
// See if there's retry overflow
|
|
|
|
if this.binlogReader.LastAppliedRowsEventHint.Equals(&lastAppliedRowsEventHint) {
|
|
|
|
successiveFailures += 1
|
|
|
|
} else {
|
|
|
|
successiveFailures = 0
|
|
|
|
}
|
|
|
|
if successiveFailures > this.migrationContext.MaxRetries() {
|
2016-10-13 11:10:16 +00:00
|
|
|
return fmt.Errorf("%d successive failures in streamer reconnect at coordinates %+v", successiveFailures, this.GetReconnectBinlogCoordinates())
|
2016-07-25 13:17:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Reposition at same binlog file.
|
|
|
|
lastAppliedRowsEventHint = this.binlogReader.LastAppliedRowsEventHint
|
2016-05-23 09:12:59 +00:00
|
|
|
log.Infof("Reconnecting... Will resume at %+v", lastAppliedRowsEventHint)
|
|
|
|
if err := this.initBinlogReader(this.GetReconnectBinlogCoordinates()); err != nil {
|
2016-05-19 13:11:36 +00:00
|
|
|
return err
|
|
|
|
}
|
2016-05-23 09:12:59 +00:00
|
|
|
this.binlogReader.LastAppliedRowsEventHint = lastAppliedRowsEventHint
|
2016-05-19 13:11:36 +00:00
|
|
|
}
|
|
|
|
}
|
2016-04-07 13:57:12 +00:00
|
|
|
}
|
2016-10-27 11:52:37 +00:00
|
|
|
|
|
|
|
func (this *EventsStreamer) Close() (err error) {
|
|
|
|
err = this.binlogReader.Close()
|
|
|
|
log.Infof("Closed streamer connection. err=%+v", err)
|
|
|
|
return err
|
|
|
|
}
|
2017-08-28 21:05:15 +00:00
|
|
|
|
2017-08-28 22:53:47 +00:00
|
|
|
func (this *EventsStreamer) Teardown() {
|
2017-08-28 21:05:15 +00:00
|
|
|
this.db.Close()
|
|
|
|
return
|
|
|
|
}
|