Skip to content

replication: Make ServerVersion a proper string #930

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 13 additions & 8 deletions replication/event.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package replication

import (
"bytes"
"encoding/binary"
"encoding/hex"
"fmt"
Expand Down Expand Up @@ -145,9 +146,8 @@ func calcVersionProduct(server string) int {
}

type FormatDescriptionEvent struct {
Version uint16
//len = 50
ServerVersion []byte
Version uint16
ServerVersion string
CreateTimestamp uint32
EventHeaderLength uint8
EventTypeHeaderLengths []byte
Expand All @@ -161,8 +161,8 @@ func (e *FormatDescriptionEvent) Decode(data []byte) error {
e.Version = binary.LittleEndian.Uint16(data[pos:])
pos += 2

e.ServerVersion = make([]byte, 50)
copy(e.ServerVersion, data[pos:])
serverVersionRaw := make([]byte, 50)
copy(serverVersionRaw, data[pos:])
pos += 50

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

server := string(e.ServerVersion)
serverVersionLength := bytes.Index(serverVersionRaw, []byte{0x0})
if serverVersionLength < 0 {
e.ServerVersion = string(serverVersionRaw)
} else {
e.ServerVersion = string(serverVersionRaw[:serverVersionLength])
}
checksumProduct := checksumVersionProductMysql
if strings.Contains(strings.ToLower(server), "mariadb") {
if strings.Contains(strings.ToLower(e.ServerVersion), "mariadb") {
checksumProduct = checksumVersionProductMariaDB
}

if calcVersionProduct(string(e.ServerVersion)) >= checksumProduct {
if calcVersionProduct(e.ServerVersion) >= checksumProduct {
// here, the last 5 bytes is 1 byte check sum alg type and 4 byte checksum if exists
e.ChecksumAlgorithm = data[len(data)-5]
e.EventTypeHeaderLengths = data[pos : len(data)-5]
Expand Down
4 changes: 2 additions & 2 deletions replication/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ func TestIndexOutOfRange(t *testing.T) {

parser.format = &FormatDescriptionEvent{
Version: 0x4,
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},
ServerVersion: "8.0.11",
CreateTimestamp: 0x0,
EventHeaderLength: 0x13,
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},
Expand Down Expand Up @@ -48,7 +48,7 @@ func TestParseEvent(t *testing.T) {
parser := NewBinlogParser()
parser.format = &FormatDescriptionEvent{
Version: 0x4,
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},
ServerVersion: "8.0.11",
CreateTimestamp: 0x0,
EventHeaderLength: 0x13,
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},
Expand Down