Skip to content

Commit cb33eb8

Browse files
mefcorvisiddontang
authored andcommitted
fix(canal): Preserve binlog filename received in the fake rotate event (go-mysql-org#428)
* fix(canal): Preserve binlog filename received in the fake rotate event go-mysql-org#406
1 parent 87265c0 commit cb33eb8

File tree

1 file changed

+24
-2
lines changed

1 file changed

+24
-2
lines changed

canal/sync.go

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,11 @@ func (c *Canal) runSyncBinlog() error {
4343

4444
savePos := false
4545
force := false
46+
47+
// The name of the binlog file received in the fake rotate event.
48+
// It must be preserved until the new position is saved.
49+
fakeRotateLogName := ""
50+
4651
for {
4752
ev, err := s.GetEvent(c.ctx)
4853
if err != nil {
@@ -51,11 +56,21 @@ func (c *Canal) runSyncBinlog() error {
5156

5257
// Update the delay between the Canal and the Master before the handler hooks are called
5358
c.updateReplicationDelay(ev)
54-
// if log pos equal zero ,it is a fake rotate event,ignore it.
55-
// see https://github.com/mysql/mysql-server/blob/8cc757da3d87bf4a1f07dcfb2d3c96fed3806870/sql/rpl_binlog_sender.cc#L899
59+
60+
// If log pos equals zero then the received event is a fake rotate event and
61+
// contains only a name of the next binlog file
62+
// See https://github.com/mysql/mysql-server/blob/8e797a5d6eb3a87f16498edcb7261a75897babae/sql/rpl_binlog_sender.h#L235
63+
// and https://github.com/mysql/mysql-server/blob/8cc757da3d87bf4a1f07dcfb2d3c96fed3806870/sql/rpl_binlog_sender.cc#L899
5664
if ev.Header.LogPos == 0 {
65+
switch e := ev.Event.(type) {
66+
case *replication.RotateEvent:
67+
fakeRotateLogName = string(e.NextLogName)
68+
log.Infof("received fake rotate event, next log name is %s", e.NextLogName)
69+
}
70+
5771
continue
5872
}
73+
5974
savePos = false
6075
force = false
6176
pos := c.master.Position()
@@ -64,6 +79,11 @@ func (c *Canal) runSyncBinlog() error {
6479
// next binlog pos
6580
pos.Pos = ev.Header.LogPos
6681

82+
// new file name received in the fake rotate event
83+
if fakeRotateLogName != "" {
84+
pos.Name = fakeRotateLogName
85+
}
86+
6787
// We only save position with RotateEvent and XIDEvent.
6888
// For RowsEvent, we can't save the position until meeting XIDEvent
6989
// which tells the whole transaction is over.
@@ -154,6 +174,8 @@ func (c *Canal) runSyncBinlog() error {
154174
if savePos {
155175
c.master.Update(pos)
156176
c.master.UpdateTimestamp(ev.Header.Timestamp)
177+
fakeRotateLogName = ""
178+
157179
if err := c.eventHandler.OnPosSynced(pos, c.master.GTIDSet(), force); err != nil {
158180
return errors.Trace(err)
159181
}

0 commit comments

Comments
 (0)