Skip to content

Commit 22158ba

Browse files
dveedenlance6716
andauthored
replication: Make ServerVersion a proper string (#930)
The ServerVersion is a slice of 50 bytes. If the ServerVersion was "9.1.0" then the rest of they bytes would be 0x0. This caused the printout of the FormatDescriptionEvent in go-mysqlbinlog to look weird. Before: ``` === FormatDescriptionEvent === Date: 2024-10-24 14:42:04 Log position: 127 Event size: 123 Version: 4 Server version: 9.1.0^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@ Checksum algorithm: 1 ``` After: ``` === FormatDescriptionEvent === Date: 2024-10-24 14:42:04 Log position: 127 Event size: 123 Version: 4 Server version: 9.1.0 Checksum algorithm: 1 ``` Co-authored-by: lance6716 <[email protected]>
1 parent 51292e4 commit 22158ba

File tree

2 files changed

+15
-10
lines changed

2 files changed

+15
-10
lines changed

replication/event.go

+13-8
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package replication
22

33
import (
4+
"bytes"
45
"encoding/binary"
56
"encoding/hex"
67
"fmt"
@@ -145,9 +146,8 @@ func calcVersionProduct(server string) int {
145146
}
146147

147148
type FormatDescriptionEvent struct {
148-
Version uint16
149-
//len = 50
150-
ServerVersion []byte
149+
Version uint16
150+
ServerVersion string
151151
CreateTimestamp uint32
152152
EventHeaderLength uint8
153153
EventTypeHeaderLengths []byte
@@ -161,8 +161,8 @@ func (e *FormatDescriptionEvent) Decode(data []byte) error {
161161
e.Version = binary.LittleEndian.Uint16(data[pos:])
162162
pos += 2
163163

164-
e.ServerVersion = make([]byte, 50)
165-
copy(e.ServerVersion, data[pos:])
164+
serverVersionRaw := make([]byte, 50)
165+
copy(serverVersionRaw, data[pos:])
166166
pos += 50
167167

168168
e.CreateTimestamp = binary.LittleEndian.Uint32(data[pos:])
@@ -175,13 +175,18 @@ func (e *FormatDescriptionEvent) Decode(data []byte) error {
175175
return errors.Errorf("invalid event header length %d, must 19", e.EventHeaderLength)
176176
}
177177

178-
server := string(e.ServerVersion)
178+
serverVersionLength := bytes.Index(serverVersionRaw, []byte{0x0})
179+
if serverVersionLength < 0 {
180+
e.ServerVersion = string(serverVersionRaw)
181+
} else {
182+
e.ServerVersion = string(serverVersionRaw[:serverVersionLength])
183+
}
179184
checksumProduct := checksumVersionProductMysql
180-
if strings.Contains(strings.ToLower(server), "mariadb") {
185+
if strings.Contains(strings.ToLower(e.ServerVersion), "mariadb") {
181186
checksumProduct = checksumVersionProductMariaDB
182187
}
183188

184-
if calcVersionProduct(string(e.ServerVersion)) >= checksumProduct {
189+
if calcVersionProduct(e.ServerVersion) >= checksumProduct {
185190
// here, the last 5 bytes is 1 byte check sum alg type and 4 byte checksum if exists
186191
e.ChecksumAlgorithm = data[len(data)-5]
187192
e.EventTypeHeaderLengths = data[pos : len(data)-5]

replication/parser_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ func TestIndexOutOfRange(t *testing.T) {
1212

1313
parser.format = &FormatDescriptionEvent{
1414
Version: 0x4,
15-
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},
15+
ServerVersion: "8.0.11",
1616
CreateTimestamp: 0x0,
1717
EventHeaderLength: 0x13,
1818
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, 0x12, 0x34, 0x0, 0xa, 0x28, 0x0},
@@ -48,7 +48,7 @@ func TestParseEvent(t *testing.T) {
4848
parser := NewBinlogParser()
4949
parser.format = &FormatDescriptionEvent{
5050
Version: 0x4,
51-
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+
ServerVersion: "8.0.11",
5252
CreateTimestamp: 0x0,
5353
EventHeaderLength: 0x13,
5454
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, 0x12, 0x34, 0x0, 0xa, 0x28, 0x0},

0 commit comments

Comments
 (0)