Skip to content

Commit 167a302

Browse files
authored
Merge branch 'master' into remove_failover
2 parents 06e800a + 22158ba commit 167a302

File tree

3 files changed

+24
-18
lines changed

3 files changed

+24
-18
lines changed

Makefile

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,31 @@
11
all: build
22

33
GO111MODULE=on
4+
MYSQL_VERSION ?= 8.0
5+
GO ?= go
46

57
build:
6-
go build -o bin/go-mysqlbinlog cmd/go-mysqlbinlog/main.go
7-
go build -o bin/go-mysqldump cmd/go-mysqldump/main.go
8-
go build -o bin/go-canal cmd/go-canal/main.go
9-
go build -o bin/go-binlogparser cmd/go-binlogparser/main.go
8+
${GO} build -o bin/go-mysqlbinlog cmd/go-mysqlbinlog/main.go
9+
${GO} build -o bin/go-mysqldump cmd/go-mysqldump/main.go
10+
${GO} build -o bin/go-canal cmd/go-canal/main.go
11+
${GO} build -o bin/go-binlogparser cmd/go-binlogparser/main.go
1012

1113
test:
12-
go test --race -timeout 2m ./...
14+
${GO} test --race -timeout 2m ./...
1315

14-
MYSQL_VERSION ?= 8.0
1516
test-local:
1617
docker run --rm -d --network=host --name go-mysql-server \
1718
-e MYSQL_ALLOW_EMPTY_PASSWORD=true \
1819
-e MYSQL_DATABASE=test \
1920
-v $${PWD}/docker/resources/replication.cnf:/etc/mysql/conf.d/replication.cnf \
2021
mysql:$(MYSQL_VERSION)
2122
docker/resources/waitfor.sh 127.0.0.1 3306 \
22-
&& go test -race -v -timeout 2m ./...
23+
&& ${GO} test -race -v -timeout 2m ./...
2324
docker stop go-mysql-server
2425

2526
fmt:
2627
golangci-lint run --fix
2728

2829
clean:
29-
go clean -i ./...
30+
${GO} clean -i ./...
3031
@rm -rf ./bin

replication/event.go

Lines changed: 13 additions & 8 deletions
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

Lines changed: 2 additions & 2 deletions
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)