Skip to content

Commit f66c8b3

Browse files
amyangfeisiddontang
authored andcommitted
replication: support parsing event with header only and no checksum (#431)
* replication: support parsing event with header only and no checksum
1 parent 40afdbe commit f66c8b3

File tree

2 files changed

+37
-1
lines changed

2 files changed

+37
-1
lines changed

replication/parser.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ func (p *BinlogParser) parseSingleEvent(r io.Reader, onEvent OnEventFunc) (bool,
120120
return false, errors.Trace(err)
121121
}
122122

123-
if h.EventSize <= uint32(EventHeaderSize) {
123+
if h.EventSize < uint32(EventHeaderSize) {
124124
return false, errors.Errorf("invalid event header, event size is %d, too small", h.EventSize)
125125
}
126126
if n, err = io.CopyN(&buf, r, int64(h.EventSize-EventHeaderSize)); err != nil {

replication/parser_test.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package replication
22

33
import (
4+
"bytes"
5+
46
. "github.com/pingcap/check"
57
)
68

@@ -40,3 +42,37 @@ func (t *testSyncerSuite) TestIndexOutOfRange(c *C) {
4042

4143
c.Assert(err, IsNil)
4244
}
45+
46+
func (t *testSyncerSuite) TestParseEvent(c *C) {
47+
parser := NewBinlogParser()
48+
parser.format = &FormatDescriptionEvent{
49+
Version: 0x4,
50+
ServerVersion: []uint8{0x35, 0x2e, 0x36, 0x2e, 0x32, 0x30, 0x2d, 0x6c, 0x6f, 0x67, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0},
51+
CreateTimestamp: 0x0,
52+
EventHeaderLength: 0x13,
53+
EventTypeHeaderLengths: []uint8{0x38, 0xd, 0x0, 0x8, 0x0, 0x12, 0x0, 0x4, 0x4, 0x4, 0x4, 0x12, 0x0, 0x0, 0x5c, 0x0, 0x4, 0x1a, 0x8, 0x0, 0x0, 0x0, 0x8, 0x8, 0x8, 0x2, 0x0, 0x0, 0x0, 0xa, 0xa, 0xa, 0x19, 0x19, 0x0},
54+
ChecksumAlgorithm: 0x0,
55+
}
56+
testCases := []struct {
57+
byteData []byte
58+
eventSize uint32
59+
}{
60+
{[]byte{0x86, 0x4c, 0x9c, 0x5d, 0x03, 0x65, 0x00, 0x00, 0x00, 0x13, 0x00, 0x00, 0x00, 0x4d, 0x01, 0x00, 0x00, 0x00, 0x00}, uint32(19)},
61+
{[]byte{0x15, 0x50, 0x9c, 0x5d, 0x03, 0x65, 0x00, 0x00, 0x00, 0x17, 0x00, 0x00, 0x00, 0x59, 0x01, 0x00, 0x00, 0x00, 0x00, 0x7d, 0x82, 0xa8, 0x50}, uint32(23)},
62+
}
63+
64+
for _, tc := range testCases {
65+
r := bytes.NewReader(tc.byteData)
66+
_, err := parser.ParseSingleEvent(r, func(e *BinlogEvent) error {
67+
c.Assert(e.Header.EventType, Equals, STOP_EVENT)
68+
c.Assert(e.Header.EventSize, Equals, tc.eventSize)
69+
return nil
70+
})
71+
c.Assert(err, IsNil)
72+
73+
e, err2 := parser.Parse(tc.byteData)
74+
c.Assert(e.Header.EventType, Equals, STOP_EVENT)
75+
c.Assert(e.Header.EventSize, Equals, tc.eventSize)
76+
c.Assert(err2, IsNil)
77+
}
78+
}

0 commit comments

Comments
 (0)