forked from github/gh-ost
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgomysql_reader.go
More file actions
224 lines (204 loc) · 7.39 KB
/
Copy pathgomysql_reader.go
File metadata and controls
224 lines (204 loc) · 7.39 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
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
/*
Copyright 2022 GitHub Inc.
See https://github.com/github/gh-ost/blob/master/LICENSE
*/
package binlog
import (
"fmt"
"sync"
"github.com/github/gh-ost/go/base"
"github.com/github/gh-ost/go/mysql"
"github.com/github/gh-ost/go/sql"
"time"
"context"
gomysql "github.com/go-mysql-org/go-mysql/mysql"
"github.com/go-mysql-org/go-mysql/replication"
uuid "github.com/google/uuid"
)
type RowsEventFilterFunc func(databaseName, tableName string) bool
func newRowsEventDecodeFunc(rowsEventFilter RowsEventFilterFunc) func(*replication.RowsEvent, []byte) error {
if rowsEventFilter == nil {
return nil
}
return func(rowsEvent *replication.RowsEvent, data []byte) error {
pos, err := rowsEvent.DecodeHeader(data)
if err != nil {
return err
}
if !rowsEventFilter(string(rowsEvent.Table.Schema), string(rowsEvent.Table.Table)) {
return nil
}
return rowsEvent.DecodeData(pos, data)
}
}
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, rowsEventFilters ...RowsEventFilterFunc) *GoMySQLReader {
connectionConfig := migrationContext.InspectorConnectionConfig
var rowsEventFilter RowsEventFilterFunc
if len(rowsEventFilters) > 0 {
rowsEventFilter = rowsEventFilters[0]
}
config := 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,
}
config.RowsEventDecodeFunc = newRowsEventDecodeFunc(rowsEventFilter)
return &GoMySQLReader{
migrationContext: migrationContext,
connectionConfig: connectionConfig,
currentCoordinatesMutex: &sync.Mutex{},
binlogSyncer: replication.NewBinlogSyncer(config),
}
}
// 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()
}
func (gmr *GoMySQLReader) handleRowsEvent(ev *replication.BinlogEvent, rowsEvent *replication.RowsEvent, entriesChannel chan<- *BinlogEntry) error {
currentCoords := gmr.GetCurrentBinlogCoordinates()
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(currentCoords)
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)
}
}
// The channel will do the throttling. Whoever is reading from the channel
// decides whether action is taken synchronously (meaning we wait before
// next iteration) or asynchronously (we keep pushing more events)
// In reality, reads will be synchronous
entriesChannel <- binlogEntry
}
return nil
}
// StreamEvents
func (gmr *GoMySQLReader) StreamEvents(canStopStreaming func() bool, entriesChannel chan<- *BinlogEntry) error {
for !canStopStreaming() {
ev, err := gmr.binlogStreamer.GetEvent(context.Background())
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 {
continue
}
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 {
continue
}
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()
}
case *replication.RowsEvent:
if err := gmr.handleRowsEvent(ev, event, entriesChannel); err != nil {
return err
}
}
}
gmr.migrationContext.Log.Debugf("done streaming events")
return nil
}
func (gmr *GoMySQLReader) Close() error {
gmr.binlogSyncer.Close()
return nil
}