-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Expand file tree
/
Copy pathgomysql_reader.go
More file actions
155 lines (140 loc) · 5.27 KB
/
Copy pathgomysql_reader.go
File metadata and controls
155 lines (140 loc) · 5.27 KB
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
/*
Copyright 2022 GitHub Inc.
See https://github.com/github/gh-ost/blob/master/LICENSE
*/
package binlog
import (
"context"
"fmt"
"sync"
"time"
"github.com/github/gh-ost/go/base"
"github.com/github/gh-ost/go/mysql"
gomysql "github.com/go-mysql-org/go-mysql/mysql"
"github.com/go-mysql-org/go-mysql/replication"
uuid "github.com/google/uuid"
)
type GoMySQLReader struct {
migrationContext *base.MigrationContext
connectionConfig *mysql.ConnectionConfig
binlogSyncer *replication.BinlogSyncer
binlogStreamer *replication.BinlogStreamer
currentCoordinates mysql.BinlogCoordinates
currentCoordinatesMutex *sync.Mutex
// LastTrxCoords are the coordinates of the last transaction completely read.
// If using the file coordinates it is binlog position of the transaction's XID event.
LastTrxCoords mysql.BinlogCoordinates
}
func NewGoMySQLReader(migrationContext *base.MigrationContext) *GoMySQLReader {
connectionConfig := migrationContext.InspectorConnectionConfig
return &GoMySQLReader{
migrationContext: migrationContext,
connectionConfig: connectionConfig,
currentCoordinatesMutex: &sync.Mutex{},
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,
TimestampStringLocation: time.UTC,
MaxReconnectAttempts: migrationContext.BinlogSyncerMaxReconnectAttempts,
}),
}
}
// ConnectBinlogStreamer
func (gmr *GoMySQLReader) ConnectBinlogStreamer(coordinates mysql.BinlogCoordinates) (err error) {
if coordinates.IsEmpty() {
return gmr.migrationContext.Log.Errorf("empty coordinates at ConnectBinlogStreamer()")
}
gmr.currentCoordinatesMutex.Lock()
defer gmr.currentCoordinatesMutex.Unlock()
gmr.currentCoordinates = coordinates
gmr.migrationContext.Log.Infof("Connecting binlog streamer at %+v", coordinates)
// Start sync with specified GTID set or binlog file and position
if gmr.migrationContext.UseGTIDs {
coords := coordinates.(*mysql.GTIDBinlogCoordinates)
gmr.binlogStreamer, err = gmr.binlogSyncer.StartSyncGTID(coords.GTIDSet)
} else {
coords := gmr.currentCoordinates.(*mysql.FileBinlogCoordinates)
gmr.binlogStreamer, err = gmr.binlogSyncer.StartSync(gomysql.Position{
Name: coords.LogFile,
Pos: uint32(coords.LogPos)},
)
}
return err
}
func (gmr *GoMySQLReader) GetCurrentBinlogCoordinates() mysql.BinlogCoordinates {
gmr.currentCoordinatesMutex.Lock()
defer gmr.currentCoordinatesMutex.Unlock()
return gmr.currentCoordinates.Clone()
}
// StreamEvents reads binlog events and sends them to the given channel.
// It is blocking and should be executed in a goroutine.
func (gmr *GoMySQLReader) StreamEvents(ctx context.Context, canStopStreaming func() bool, eventChannel chan<- *replication.BinlogEvent) error {
for {
if canStopStreaming() {
return nil
}
if err := ctx.Err(); err != nil {
return err
}
ev, err := gmr.binlogStreamer.GetEvent(ctx)
if err != nil {
return err
}
// Update binlog coords if using file-based coords.
// GTID coordinates are updated on receiving GTID events.
if !gmr.migrationContext.UseGTIDs {
gmr.currentCoordinatesMutex.Lock()
coords := gmr.currentCoordinates.(*mysql.FileBinlogCoordinates)
prevCoords := coords.Clone().(*mysql.FileBinlogCoordinates)
coords.LogPos = int64(ev.Header.LogPos)
coords.EventSize = int64(ev.Header.EventSize)
if coords.IsLogPosOverflowBeyond4Bytes(prevCoords) {
gmr.currentCoordinatesMutex.Unlock()
return fmt.Errorf("unexpected rows event at %+v, the binlog end_log_pos is overflow 4 bytes", coords)
}
gmr.currentCoordinatesMutex.Unlock()
}
switch event := ev.Event.(type) {
case *replication.GTIDEvent:
if gmr.migrationContext.UseGTIDs {
sid, err := uuid.FromBytes(event.SID)
if err != nil {
return err
}
gmr.currentCoordinatesMutex.Lock()
if gmr.LastTrxCoords != nil {
gmr.currentCoordinates = gmr.LastTrxCoords.Clone()
}
coords := gmr.currentCoordinates.(*mysql.GTIDBinlogCoordinates)
trxGset := gomysql.NewUUIDSet(sid, gomysql.Interval{Start: event.GNO, Stop: event.GNO + 1})
coords.GTIDSet.AddSet(trxGset)
gmr.currentCoordinatesMutex.Unlock()
}
case *replication.RotateEvent:
if !gmr.migrationContext.UseGTIDs {
gmr.currentCoordinatesMutex.Lock()
coords := gmr.currentCoordinates.(*mysql.FileBinlogCoordinates)
coords.LogFile = string(event.NextLogName)
gmr.migrationContext.Log.Infof("rotate to next log from %s:%d to %s", coords.LogFile, int64(ev.Header.LogPos), event.NextLogName)
gmr.currentCoordinatesMutex.Unlock()
}
case *replication.XIDEvent:
if gmr.migrationContext.UseGTIDs {
gmr.LastTrxCoords = &mysql.GTIDBinlogCoordinates{GTIDSet: event.GSet.(*gomysql.MysqlGTIDSet)}
} else {
gmr.LastTrxCoords = gmr.currentCoordinates.Clone()
}
}
eventChannel <- ev
}
}
func (gmr *GoMySQLReader) Close() error {
gmr.binlogSyncer.Close()
return nil
}