Repository navigation
Expand file tree
/
Copy pathbinlog_streamer_gtid_test.go
More file actions
568 lines (519 loc) · 21.4 KB
/
Copy pathbinlog_streamer_gtid_test.go
File metadata and controls
568 lines (519 loc) · 21.4 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
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
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
package ghostferry
import (
"encoding/json"
"math"
"sync"
"testing"
"github.com/go-mysql-org/go-mysql/mysql"
"github.com/go-mysql-org/go-mysql/replication"
"github.com/go-mysql-org/go-mysql/schema"
"github.com/google/uuid"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
const (
gtidSetLower = "3e11fa47-71ca-11e1-9e33-c80aa9429562:1-57"
gtidSetTarget = "3e11fa47-71ca-11e1-9e33-c80aa9429562:1-100"
gtidSetPast = "3e11fa47-71ca-11e1-9e33-c80aa9429562:1-150"
)
func mustParseGTID(t *testing.T, s string) mysql.GTIDSet {
t.Helper()
set, err := mysql.ParseMysqlGTIDSet(s)
require.NoError(t, err)
return set
}
func TestRunStopsAtGTIDTransactionBoundary(t *testing.T) {
target := mustParseGTID(t, gtidSetTarget)
sid := uuid.MustParse("3e11fa47-71ca-11e1-9e33-c80aa9429562")
gtid := func() *replication.BinlogEvent {
return &replication.BinlogEvent{
Header: &replication.EventHeader{EventType: replication.GTID_EVENT},
Event: &replication.GTIDEvent{SID: sid[:], GNO: 100},
}
}
query := func(statement string) *replication.BinlogEvent {
return &replication.BinlogEvent{
Header: &replication.EventHeader{EventType: replication.QUERY_EVENT},
Event: &replication.QueryEvent{Query: []byte(statement), GSet: target},
}
}
rows := func(id int) *replication.BinlogEvent {
return &replication.BinlogEvent{
Header: &replication.EventHeader{EventType: replication.WRITE_ROWS_EVENTv2},
Event: &replication.RowsEvent{Rows: [][]interface{}{{id}}},
}
}
xid := func() *replication.BinlogEvent {
return &replication.BinlogEvent{
Header: &replication.EventHeader{EventType: replication.XID_EVENT},
Event: &replication.XIDEvent{GSet: target},
}
}
for _, tc := range []struct {
name string
events []*replication.BinlogEvent
customHandlers []replication.EventType
wantRows [][]interface{}
wantXAPrepare bool
}{
{
name: "savepoints do not commit",
events: []*replication.BinlogEvent{
gtid(), query("BEGIN"), rows(1), query("SAVEPOINT `s`"),
query("ROLLBACK TO SAVEPOINT `s`"), query("RELEASE SAVEPOINT `s`"), rows(2), xid(),
},
wantRows: [][]interface{}{{1}, {2}},
},
{
name: "empty transaction commits without XID",
events: []*replication.BinlogEvent{gtid(), query("BEGIN"), query("COMMIT")},
},
{
name: "rollback closes transaction without XID",
events: []*replication.BinlogEvent{gtid(), query("BEGIN"), query("ROLLBACK")},
},
{
name: "standalone DDL commits without XID",
events: []*replication.BinlogEvent{gtid(), query("CREATE TABLE t (id int)")},
},
{
name: "custom query handler preserves DDL commit",
events: []*replication.BinlogEvent{gtid(), query("CREATE TABLE t (id int)")},
customHandlers: []replication.EventType{replication.QUERY_EVENT},
},
{
name: "custom transaction handlers preserve commit",
events: []*replication.BinlogEvent{gtid(), query("BEGIN"), rows(1), xid()},
customHandlers: []replication.EventType{replication.GTID_EVENT, replication.QUERY_EVENT, replication.XID_EVENT},
wantRows: [][]interface{}{{1}},
},
{
name: "XA one phase consumes rows and prepare marker",
events: []*replication.BinlogEvent{
gtid(), query("XA START X'01',X'',1"), rows(1), query("XA END X'01',X'',1"),
{Header: &replication.EventHeader{EventType: replication.XA_PREPARE_LOG_EVENT}, Event: &replication.GenericEvent{Data: []byte{1}}},
},
customHandlers: []replication.EventType{replication.GTID_EVENT, replication.QUERY_EVENT},
wantRows: [][]interface{}{{1}},
wantXAPrepare: true,
},
{
name: "XA opener accepts whitespace and case variation",
events: []*replication.BinlogEvent{
gtid(), query("xa\t BeGiN X'01',X'',1"), rows(1), query("XA END X'01',X'',1"),
{Header: &replication.EventHeader{EventType: replication.XA_PREPARE_LOG_EVENT}, Event: &replication.GenericEvent{Data: []byte{1}}},
},
wantRows: [][]interface{}{{1}},
wantXAPrepare: true,
},
} {
t.Run(tc.name, func(t *testing.T) {
stream := replication.NewBinlogStreamer()
s := &BinlogStreamer{
BinlogCoordinateMode: BinlogCoordinateGTID,
binlogStreamer: stream,
binlogSyncer: replication.NewBinlogSyncer(replication.BinlogSyncerConfig{ServerID: 1}),
}
s.stopRequested.Store(true)
s.seedGTIDSets(mustParseGTID(t, gtidSetLower))
s.setStopGTIDSet(target)
var deliveredRows [][]interface{}
sawXAPrepare := false
require.NoError(t, s.AddBinlogEventHandler(replication.XA_PREPARE_LOG_EVENT, func(_ *replication.BinlogEvent, query []byte, _ *BinlogEventState) ([]byte, error) {
sawXAPrepare = true
return query, nil
}))
require.NoError(t, s.AddBinlogEventHandler(replication.WRITE_ROWS_EVENTv2, func(ev *replication.BinlogEvent, query []byte, es *BinlogEventState) ([]byte, error) {
deliveredRows = append(deliveredRows, ev.Event.(*replication.RowsEvent).Rows...)
return query, nil
}))
for _, eventType := range tc.customHandlers {
require.NoError(t, s.AddBinlogEventHandler(eventType, func(ev *replication.BinlogEvent, query []byte, es *BinlogEventState) ([]byte, error) {
return query, nil
}))
}
// Fail deterministically instead of polling forever if the commit is missed.
require.NoError(t, s.AddBinlogEventHandler(replication.HEARTBEAT_EVENT, func(ev *replication.BinlogEvent, query []byte, es *BinlogEventState) ([]byte, error) {
t.Fatal("stream continued past the transaction's stop GTID")
return nil, nil
}))
for i, ev := range tc.events {
ev.Header.LogPos = uint32(100 + i)
require.NoError(t, stream.AddEventToStreamer(ev))
}
require.NoError(t, stream.AddEventToStreamer(&replication.BinlogEvent{
Header: &replication.EventHeader{EventType: replication.HEARTBEAT_EVENT},
Event: &replication.GenericEvent{},
}))
s.Run()
assert.Equal(t, tc.wantRows, deliveredRows)
assert.Equal(t, tc.wantXAPrepare, sawXAPrepare)
reached, err := s.GetLastStreamedBinlogCoordinate().HasReached(NewGTIDCoordinate(gtidSetTarget))
require.NoError(t, err)
assert.True(t, reached)
})
}
}
func TestShouldContinueStreaming_FilePosition(t *testing.T) {
s := &BinlogStreamer{}
assert.True(t, s.shouldContinueStreaming())
s.stopRequested.Store(true)
s.stopAtBinlogPosition = mysql.Position{Name: "mysql-bin.000010", Pos: 100}
s.lastStreamedBinlogPosition = mysql.Position{Name: "mysql-bin.000010", Pos: 50}
assert.True(t, s.shouldContinueStreaming())
s.lastStreamedBinlogPosition = mysql.Position{Name: "mysql-bin.000010", Pos: 100}
assert.False(t, s.shouldContinueStreaming())
}
func TestShouldContinueStreaming_GTID(t *testing.T) {
s := &BinlogStreamer{BinlogCoordinateMode: BinlogCoordinateGTID}
assert.True(t, s.shouldContinueStreaming())
s.stopRequested.Store(true)
s.stopAtGTIDSet = mustParseGTID(t, gtidSetTarget)
assert.True(t, s.shouldContinueStreaming())
s.lastStreamedGTIDSet = mustParseGTID(t, gtidSetLower)
assert.True(t, s.shouldContinueStreaming())
s.lastStreamedGTIDSet = mustParseGTID(t, gtidSetTarget)
assert.False(t, s.shouldContinueStreaming())
s.lastStreamedGTIDSet = mustParseGTID(t, gtidSetPast)
assert.False(t, s.shouldContinueStreaming())
}
// TestShouldContinueStreaming_GTIDEmptyStopTarget: an empty executed GTID set
// is a valid stop target (fresh source), so the stream must stop, not hang.
func TestShouldContinueStreaming_GTIDEmptyStopTarget(t *testing.T) {
s := &BinlogStreamer{BinlogCoordinateMode: BinlogCoordinateGTID}
s.stopRequested.Store(true)
s.stopAtGTIDSet = mustParseGTID(t, "") // empty executed set on a fresh source
assert.False(t, s.shouldContinueStreaming())
s.lastStreamedGTIDSet = mustParseGTID(t, gtidSetTarget)
assert.False(t, s.shouldContinueStreaming())
}
func TestGetLastStreamedBinlogCoordinate_GTIDMode(t *testing.T) {
s := &BinlogStreamer{BinlogCoordinateMode: BinlogCoordinateGTID}
coord := s.GetLastStreamedBinlogCoordinate()
assert.True(t, coord.IsGTID())
assert.True(t, coord.IsZero())
s.lastStreamedGTIDSet = mustParseGTID(t, gtidSetTarget)
coord = s.GetLastStreamedBinlogCoordinate()
assert.True(t, coord.IsGTID())
assert.Equal(t, gtidSetTarget, coord.GTIDSet)
}
func TestGetLastStreamedBinlogCoordinate_FilePositionMode(t *testing.T) {
s := &BinlogStreamer{}
s.lastStreamedBinlogPosition = mysql.Position{Name: "mysql-bin.000010", Pos: 100}
coord := s.GetLastStreamedBinlogCoordinate()
assert.True(t, coord.IsFilePosition())
assert.Equal(t, "mysql-bin.000010", coord.Position().Name)
assert.Equal(t, uint32(100), coord.Position().Pos)
}
func TestConnectBinlogStreamerSinceCoordinate_TypeMismatch(t *testing.T) {
// Type mismatch must be rejected before any DB interaction.
s := &BinlogStreamer{BinlogCoordinateMode: BinlogCoordinateGTID}
_, err := s.ConnectBinlogStreamerToMysqlSinceCoordinate(
NewFilePositionCoordinate(mysql.Position{Name: "mysql-bin.000001", Pos: 4}),
)
require.Error(t, err)
s2 := &BinlogStreamer{}
_, err = s2.ConnectBinlogStreamerToMysqlSinceCoordinate(NewGTIDCoordinate(gtidSetTarget))
require.Error(t, err)
}
// TestGTIDCoordinateAccessorsAreRaceFree mirrors the streaming loop and
// Ferry.Progress() concurrently writing/reading the GTID sets (maps); must
// stay clean under -race.
func TestGTIDCoordinateAccessorsAreRaceFree(t *testing.T) {
s := &BinlogStreamer{BinlogCoordinateMode: BinlogCoordinateGTID}
s.logger = LogWithField("tag", "test")
s.seedGTIDSets(mustParseGTID(t, gtidSetLower))
sid := uuid.MustParse("3e11fa47-71ca-11e1-9e33-c80aa9429562")
event := &replication.GTIDEvent{SID: sid[:], GNO: 100}
const iterations = 500
var wg sync.WaitGroup
wg.Add(3)
go func() {
defer wg.Done()
for i := 0; i < iterations; i++ {
if err := s.setResumableBeforeGTID(event); err != nil {
t.Error(err)
return
}
s.setLastStreamedGTIDSet(mustParseGTID(t, gtidSetTarget))
}
}()
go func() {
defer wg.Done()
for i := 0; i < iterations; i++ {
s.setStopGTIDSet(mustParseGTID(t, gtidSetPast))
s.stopRequested.Store(true)
}
}()
go func() {
defer wg.Done()
for i := 0; i < iterations; i++ {
_ = s.GetLastStreamedBinlogCoordinate()
_ = s.GetStopBinlogCoordinate()
_ = s.resumableGTIDClone()
_ = s.shouldContinueStreaming()
}
}()
wg.Wait()
}
func TestRunFinishesReplayedTransactionBeforeCutover(t *testing.T) {
sid := uuid.MustParse("3e11fa47-71ca-11e1-9e33-c80aa9429562")
for _, stopAt := range []string{"GTID", "first replayed update"} {
t.Run(stopAt, func(t *testing.T) {
stream := replication.NewBinlogStreamer()
s := &BinlogStreamer{
BinlogCoordinateMode: BinlogCoordinateGTID,
binlogStreamer: stream,
binlogSyncer: replication.NewBinlogSyncer(replication.BinlogSyncerConfig{ServerID: 1}),
}
s.seedGTIDSets(mustParseGTID(t, gtidSetLower))
s.setStopGTIDSet(mustParseGTID(t, gtidSetTarget))
gtids, updates, value := 0, 0, 0
require.NoError(t, s.AddBinlogEventHandler(replication.GTID_EVENT, func(_ *replication.BinlogEvent, query []byte, _ *BinlogEventState) ([]byte, error) {
gtids++
if gtids == 2 && stopAt == "GTID" {
s.stopRequested.Store(true)
}
return query, nil
}))
require.NoError(t, s.AddBinlogEventHandler(replication.UPDATE_ROWS_EVENTv2, func(ev *replication.BinlogEvent, query []byte, _ *BinlogEventState) ([]byte, error) {
rows := ev.Event.(*replication.RowsEvent).Rows
if value == rows[0][0].(int) {
value = rows[1][0].(int)
}
updates++
if updates == 3 && stopAt == "first replayed update" {
s.stopRequested.Store(true)
}
return query, nil
}))
require.NoError(t, s.AddBinlogEventHandler(replication.HEARTBEAT_EVENT, func(_ *replication.BinlogEvent, _ []byte, _ *BinlogEventState) ([]byte, error) {
t.Fatal("stream continued past the replayed commit")
return nil, nil
}))
add := func(kind replication.EventType, event replication.Event) {
require.NoError(t, stream.AddEventToStreamer(&replication.BinlogEvent{
Header: &replication.EventHeader{EventType: kind, LogPos: 100}, Event: event,
}))
}
for range 2 {
add(replication.GTID_EVENT, &replication.GTIDEvent{SID: sid[:], GNO: 100})
add(replication.QUERY_EVENT, &replication.QueryEvent{Query: []byte("BEGIN"), GSet: mustParseGTID(t, gtidSetTarget)})
add(replication.UPDATE_ROWS_EVENTv2, &replication.RowsEvent{Rows: [][]interface{}{{0}, {1}}})
add(replication.UPDATE_ROWS_EVENTv2, &replication.RowsEvent{Rows: [][]interface{}{{1}, {0}}})
add(replication.XID_EVENT, &replication.XIDEvent{GSet: mustParseGTID(t, gtidSetTarget)})
}
add(replication.HEARTBEAT_EVENT, &replication.GenericEvent{})
s.Run()
assert.Equal(t, 4, updates, "cutover must finish replaying the transaction")
assert.Equal(t, 0, value, "cutover must preserve the committed row value")
})
}
}
func gtidReplayTableSchema(database string) TableSchemaCache {
return TableSchemaCache{
database + ".t": &TableSchema{Table: &schema.Table{
Schema: database, Name: "t",
Columns: []schema.TableColumn{{Name: "id", Type: schema.TYPE_NUMBER}, {Name: "v", Type: schema.TYPE_NUMBER}},
}},
}
}
func queueGTIDReplayTransaction(t *testing.T, stream *replication.BinlogStreamer) {
t.Helper()
sid := uuid.MustParse("3e11fa47-71ca-11e1-9e33-c80aa9429562")
target := mustParseGTID(t, gtidSetTarget)
events := []*replication.BinlogEvent{
{Header: &replication.EventHeader{EventType: replication.GTID_EVENT}, Event: &replication.GTIDEvent{SID: sid[:], GNO: 100}},
{Header: &replication.EventHeader{EventType: replication.QUERY_EVENT}, Event: &replication.QueryEvent{Query: []byte("BEGIN"), GSet: target}},
{Header: &replication.EventHeader{EventType: replication.UPDATE_ROWS_EVENTv2}, Event: &replication.RowsEvent{
Table: &replication.TableMapEvent{Schema: []byte("review"), Table: []byte("t")}, Rows: [][]interface{}{{1, 0}, {1, 1}},
}},
{Header: &replication.EventHeader{EventType: replication.UPDATE_ROWS_EVENTv2}, Event: &replication.RowsEvent{
Table: &replication.TableMapEvent{Schema: []byte("review"), Table: []byte("t")}, Rows: [][]interface{}{{1, 1}, {1, 0}},
}},
{Header: &replication.EventHeader{EventType: replication.XID_EVENT}, Event: &replication.XIDEvent{GSet: target}},
}
for i, event := range events {
event.Header.LogPos = uint32(100 + i)
require.NoError(t, stream.AddEventToStreamer(event))
}
}
func TestRunResumesInterruptedGTIDReplay(t *testing.T) {
const start = "3e11fa47-71ca-11e1-9e33-c80aa9429562:1-99"
for _, inline := range []bool{false, true} {
name := "writer_only"
if inline {
name = "writer_and_inline"
}
t.Run(name, func(t *testing.T) {
newStreamer := func(coord BinlogCoordinate, groups int) *BinlogStreamer {
stream := replication.NewBinlogStreamer()
s := &BinlogStreamer{
BinlogCoordinateMode: BinlogCoordinateGTID,
binlogStreamer: stream,
binlogSyncer: replication.NewBinlogSyncer(replication.BinlogSyncerConfig{ServerID: 1}),
TableSchema: gtidReplayTableSchema("review"),
}
s.seedGTIDSets(mustParseGTID(t, coord.GTIDSet))
s.setStopGTIDSet(mustParseGTID(t, gtidSetTarget))
for range groups {
queueGTIDReplayTransaction(t, stream)
}
require.NoError(t, s.AddBinlogEventHandler(replication.HEARTBEAT_EVENT, func(_ *replication.BinlogEvent, _ []byte, _ *BinlogEventState) ([]byte, error) {
t.Fatal("stream continued past the replayed commit")
return nil, nil
}))
require.NoError(t, stream.AddEventToStreamer(&replication.BinlogEvent{
Header: &replication.EventHeader{EventType: replication.HEARTBEAT_EVENT}, Event: &replication.GenericEvent{},
}))
return s
}
tracker := NewStateTracker(0)
advance := func(coord BinlogCoordinate) {
tracker.UpdateLastResumableSourceBinlogCoordinate(coord)
if inline {
tracker.UpdateLastResumableSourceBinlogCoordinateForInlineVerifier(coord)
}
}
advance(NewGTIDCoordinate(start))
s := newStreamer(NewGTIDCoordinate(start), 2)
consumer, updates, savedConsumer := 0, 0, 0
var saved []byte
apply := func(ev DMLEvent) {
if consumer == ev.OldValues()[1].(int) {
consumer = ev.NewValues()[1].(int)
}
advance(ev.ResumableBinlogCoordinate())
updates++
}
s.AddEventListener(func(events []DMLEvent) error {
for _, ev := range events {
apply(ev)
if updates == 3 {
assert.Equal(t, 1, consumer)
savedConsumer = consumer
var err error
saved, err = json.Marshal(tracker.Serialize(nil, nil))
require.NoError(t, err)
s.stopRequested.Store(true)
}
}
return nil
})
s.Run()
assert.Equal(t, 4, updates, "graceful cutover must finish the replay")
assert.Equal(t, 0, consumer)
var state SerializableState
require.NoError(t, json.Unmarshal(saved, &state))
tracker = NewStateTrackerFromSerializedState(0, &state)
resume, err := state.MinSourceBinlogCoordinate()
require.NoError(t, err)
assert.True(t, mustParseGTID(t, start).Equal(mustParseGTID(t, resume.GTIDSet)), "saved floor: %s", resume.GTIDSet)
consumer, updates = savedConsumer, 0
resumed := newStreamer(resume, 1)
resumed.stopRequested.Store(true)
resumed.AddEventListener(func(events []DMLEvent) error {
for _, ev := range events {
apply(ev)
}
return nil
})
resumed.Run()
assert.Equal(t, 2, updates, "resume must deliver the interrupted transaction")
assert.Equal(t, 0, consumer, "resume must repair the saved intermediate row")
})
}
}
func TestGTIDResumeFloorExcludesOnlyCurrentTransaction(t *testing.T) {
const a = "3e11fa47-71ca-11e1-9e33-c80aa9429562"
const b = "8e12fa47-71ca-11e1-9e33-c80aa9429999"
sid := uuid.MustParse(a)
for _, tc := range []struct {
name, committed, want string
}{
{"new transaction", a + ":1-99", a + ":1-99"},
{"interior replay preserves later and unrelated GTIDs", a + ":1-150," + b + ":1-3:8-10", a + ":1-99:101-150," + b + ":1-3:8-10"},
{"sole transaction", a + ":100", ""},
{"nil committed progress", "", ""},
} {
t.Run(tc.name, func(t *testing.T) {
s := &BinlogStreamer{BinlogCoordinateMode: BinlogCoordinateGTID}
// Start with an existing floor to ensure nil committed progress clears it.
s.seedGTIDSets(mustParseGTID(t, gtidSetTarget))
var committed mysql.GTIDSet
if tc.committed != "" {
committed = mustParseGTID(t, tc.committed)
}
s.setLastStreamedGTIDSet(committed)
s.setStopGTIDSet(mustParseGTID(t, gtidSetPast))
beforeCommitted := s.GetLastStreamedBinlogCoordinate()
beforeStop := s.GetStopBinlogCoordinate()
require.NoError(t, s.setResumableBeforeGTID(&replication.GTIDEvent{SID: sid[:], GNO: 100}))
floor := s.resumableGTIDCoordinate()
assert.True(t, mustParseGTID(t, tc.want).Equal(mustParseGTID(t, floor.GTIDSet)), "resume floor: %s", floor.GTIDSet)
if tc.want == "" {
assert.Empty(t, floor.GTIDSet, "empty floor must not serialize a bare UUID")
}
assert.True(t, mustParseGTID(t, beforeCommitted.GTIDSet).Equal(mustParseGTID(t, s.GetLastStreamedBinlogCoordinate().GTIDSet)))
assert.True(t, mustParseGTID(t, beforeStop.GTIDSet).Equal(mustParseGTID(t, s.GetStopBinlogCoordinate().GTIDSet)))
})
}
}
type recordingGTIDErrorHandler struct {
err error
}
func (h *recordingGTIDErrorHandler) ReportError(_ string, err error) {
h.err = err
}
func (h *recordingGTIDErrorHandler) Fatal(from string, err error) {
h.ReportError(from, err)
}
func TestRunRejectsInvalidGTIDBeforeRows(t *testing.T) {
sid := uuid.MustParse("3e11fa47-71ca-11e1-9e33-c80aa9429562")
for _, tc := range []struct {
name string
event *replication.GTIDEvent
}{
{"malformed SID", &replication.GTIDEvent{SID: []byte{1}, GNO: 100}},
{"zero sequence", &replication.GTIDEvent{SID: sid[:], GNO: 0}},
{"overflowing sequence", &replication.GTIDEvent{SID: sid[:], GNO: math.MaxInt64}},
} {
t.Run(tc.name, func(t *testing.T) {
stream := replication.NewBinlogStreamer()
handler := &recordingGTIDErrorHandler{}
s := &BinlogStreamer{
BinlogCoordinateMode: BinlogCoordinateGTID,
binlogStreamer: stream,
binlogSyncer: replication.NewBinlogSyncer(replication.BinlogSyncerConfig{ServerID: 1}),
TableSchema: gtidReplayTableSchema("review"),
ErrorHandler: handler,
}
s.seedGTIDSets(mustParseGTID(t, gtidSetLower))
beforeResumable := s.resumableGTIDCoordinate()
beforeCommitted := s.GetLastStreamedBinlogCoordinate()
rowsDelivered := false
s.AddEventListener(func(_ []DMLEvent) error {
rowsDelivered = true
return nil
})
require.NoError(t, s.AddBinlogEventHandler(replication.HEARTBEAT_EVENT, func(_ *replication.BinlogEvent, _ []byte, _ *BinlogEventState) ([]byte, error) {
t.Fatal("stream continued after invalid GTID")
return nil, nil
}))
for _, event := range []*replication.BinlogEvent{
{Header: &replication.EventHeader{EventType: replication.GTID_EVENT, LogPos: 100}, Event: tc.event},
{Header: &replication.EventHeader{EventType: replication.UPDATE_ROWS_EVENTv2, LogPos: 101}, Event: &replication.RowsEvent{
Table: &replication.TableMapEvent{Schema: []byte("review"), Table: []byte("t")}, Rows: [][]interface{}{{1, 0}, {1, 1}},
}},
{Header: &replication.EventHeader{EventType: replication.HEARTBEAT_EVENT}, Event: &replication.GenericEvent{}},
} {
require.NoError(t, stream.AddEventToStreamer(event))
}
s.Run()
assert.Error(t, handler.err)
assert.False(t, rowsDelivered)
assert.True(t, mustParseGTID(t, beforeResumable.GTIDSet).Equal(mustParseGTID(t, s.resumableGTIDCoordinate().GTIDSet)))
assert.True(t, mustParseGTID(t, beforeCommitted.GTIDSet).Equal(mustParseGTID(t, s.GetLastStreamedBinlogCoordinate().GTIDSet)))
})
}
}