Skip to content

Commit b490dc8

Browse files
authored
Merge branch 'master' into tls-in-driver
2 parents 587d49e + 5a98cc1 commit b490dc8

18 files changed

+124
-65
lines changed

CHANGELOG.md

+23
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,26 @@
1+
### Tag v1.5.0 (2022-04-30)
2+
* Replacing github.com/satori/go.uuid with github.com/google/uuid. [#690](https://github.com/go-mysql-org/go-mysql/pull/690) ([martinhrvn](https://github.com/martinhrvn))
3+
* Fix dump gtid regexp. [#688](https://github.com/go-mysql-org/go-mysql/pull/688) ([yrbb](https://github.com/yrbb))
4+
* Utilise byteSlice as a part of struct to avoid allocations. [#686](https://github.com/go-mysql-org/go-mysql/pull/686) ([moredure](https://github.com/moredure))
5+
* Update bytes_buffer_pool.go. [#685](https://github.com/go-mysql-org/go-mysql/pull/685) ([moredure](https://github.com/moredure))
6+
* Add go1.18 to the test matrix. [#684](https://github.com/go-mysql-org/go-mysql/pull/684) ([moredure](https://github.com/moredure))
7+
* Add 1.17 version into the test matrix. [#683](https://github.com/go-mysql-org/go-mysql/pull/683) ([moredure](https://github.com/moredure))
8+
* Stop using channel for memory pooling. [#682](https://github.com/go-mysql-org/go-mysql/pull/682) ([moredure](https://github.com/moredure))
9+
* Fix TIME value near zero will become zero. [#678](https://github.com/go-mysql-org/go-mysql/pull/678) ([lance6716](https://github.com/lance6716))
10+
* Fixed reading connection attributes on server side. [#676](https://github.com/go-mysql-org/go-mysql/pull/676) ([skoef](https://github.com/skoef))
11+
* Support client connection attributes on the client side. [#675](https://github.com/go-mysql-org/go-mysql/pull/675) ([skoef](https://github.com/skoef))
12+
* Support client connection attributes on the server side. [#672](https://github.com/go-mysql-org/go-mysql/pull/672) ([skoef](https://github.com/skoef))
13+
* Added constants for COM_SET_OPTION. [#670](https://github.com/go-mysql-org/go-mysql/pull/670) ([skoef](https://github.com/skoef))
14+
* Implemented ExecuteMultiple. [#668](https://github.com/go-mysql-org/go-mysql/pull/668) ([skoef](https://github.com/skoef))
15+
* Add MysqlGTIDSet.Add() and Minus() methods. [#667](https://github.com/go-mysql-org/go-mysql/pull/667) ([ostinru](https://github.com/ostinru))
16+
* Enabled ineffassign in golangci. [#666](https://github.com/go-mysql-org/go-mysql/pull/666) ([skoef](https://github.com/skoef))
17+
* Enabled gosimple in golangci. [#665](https://github.com/go-mysql-org/go-mysql/pull/665) ([skoef](https://github.com/skoef))
18+
* Add MysqlGTIDSet.Add() and Minus() methods. [#662](https://github.com/go-mysql-org/go-mysql/pull/662) ([ostinru](https://github.com/ostinru))
19+
* Store json as string in rows events. [#658](https://github.com/go-mysql-org/go-mysql/pull/658) ([D3Hunter](https://github.com/D3Hunter))
20+
* Set slave_uuid and replica_uuid. [#656](https://github.com/go-mysql-org/go-mysql/pull/656) ([lance6716](https://github.com/lance6716))
21+
* Fix malformed packet and a panic. [#655](https://github.com/go-mysql-org/go-mysql/pull/655) ([lance6716](https://github.com/lance6716))
22+
* Fix fetchIndexesViaSqlDB/NewTableFromSqlDB for MySQL 8.0. [#527](https://github.com/go-mysql-org/go-mysql/pull/527) ([RobinGeuze](https://github.com/RobinGeuze))
23+
124
### Tag v1.4.0 (2021-12-15)
225
* Fix that forget to readOK after writing. [#652](https://github.com/go-mysql-org/go-mysql/pull/652) ([lance6716](https://github.com/lance6716))
326
* Packet: memory optimization for reading network packet. [#651](https://github.com/go-mysql-org/go-mysql/pull/651) ([zr-hebo](https://github.com/zr-hebo))

README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@ A pure go library to handle MySQL network protocol and replication.
99
## How to migrate to this repo
1010
To change the used package in your repo it's enough to add this `replace` directive to your `go.mod`:
1111
```
12-
replace github.com/siddontang/go-mysql => github.com/go-mysql-org/go-mysql v1.4.0
12+
replace github.com/siddontang/go-mysql => github.com/go-mysql-org/go-mysql v1.5.0
1313
```
1414

15-
v1.4.0 - is the last tag in repo, feel free to choose what you want.
15+
v1.5.0 - is the last tag in repo, feel free to choose what you want.
1616

1717
## Changelog
1818
This repo uses [Changelog](CHANGELOG.md).

canal/sync.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ import (
88
"github.com/go-mysql-org/go-mysql/mysql"
99
"github.com/go-mysql-org/go-mysql/replication"
1010
"github.com/go-mysql-org/go-mysql/schema"
11+
"github.com/google/uuid"
1112
"github.com/pingcap/errors"
1213
"github.com/pingcap/parser/ast"
13-
uuid "github.com/satori/go.uuid"
1414
"github.com/siddontang/go-log/log"
1515
)
1616

client/conn.go

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

33
import (
4+
"bytes"
45
"context"
56
"crypto/tls"
67
"fmt"
@@ -223,30 +224,30 @@ func (c *Conn) ExecuteMultiple(query string, perResultCallback ExecPerResultCall
223224
return nil, errors.Trace(err)
224225
}
225226

226-
var buf []byte
227227
var err error
228228
var result *Result
229-
defer utils.ByteSlicePut(buf)
229+
230+
bs := utils.ByteSliceGet(16)
231+
defer utils.ByteSlicePut(bs)
230232

231233
for {
232-
buf, err = c.ReadPacketReuseMem(utils.ByteSliceGet(16)[:0])
234+
bs.B, err = c.ReadPacketReuseMem(bs.B[:0])
233235
if err != nil {
234236
return nil, errors.Trace(err)
235237
}
236238

237-
switch buf[0] {
239+
switch bs.B[0] {
238240
case OK_HEADER:
239-
result, err = c.handleOKPacket(buf)
241+
result, err = c.handleOKPacket(bs.B)
240242
case ERR_HEADER:
241-
err = c.handleErrorPacket(append([]byte{}, buf...))
243+
err = c.handleErrorPacket(bytes.Repeat(bs.B, 1))
242244
result = nil
243245
case LocalInFile_HEADER:
244246
err = ErrMalformPacket
245247
result = nil
246248
default:
247-
result, err = c.readResultset(buf, false)
249+
result, err = c.readResultset(bs.B, false)
248250
}
249-
250251
// call user-defined callback
251252
perResultCallback(result, err)
252253

client/req.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,11 @@ func (c *Conn) writeCommandBuf(command byte, arg []byte) error {
2121

2222
length := len(arg) + 1
2323
data := utils.ByteSliceGet(length + 4)
24-
data[4] = command
24+
data.B[4] = command
2525

26-
copy(data[5:], arg)
26+
copy(data.B[5:], arg)
2727

28-
err := c.WritePacket(data)
28+
err := c.WritePacket(data.B)
2929

3030
utils.ByteSlicePut(data)
3131

client/resp.go

+24-20
Original file line numberDiff line numberDiff line change
@@ -216,38 +216,42 @@ func (c *Conn) readOK() (*Result, error) {
216216
}
217217

218218
func (c *Conn) readResult(binary bool) (*Result, error) {
219-
firstPkgBuf, err := c.ReadPacketReuseMem(utils.ByteSliceGet(16)[:0])
220-
defer utils.ByteSlicePut(firstPkgBuf)
221-
219+
bs := utils.ByteSliceGet(16)
220+
defer utils.ByteSlicePut(bs)
221+
var err error
222+
bs.B, err = c.ReadPacketReuseMem(bs.B[:0])
222223
if err != nil {
223224
return nil, errors.Trace(err)
224225
}
225226

226-
if firstPkgBuf[0] == OK_HEADER {
227-
return c.handleOKPacket(firstPkgBuf)
228-
} else if firstPkgBuf[0] == ERR_HEADER {
229-
return nil, c.handleErrorPacket(append([]byte{}, firstPkgBuf...))
230-
} else if firstPkgBuf[0] == LocalInFile_HEADER {
227+
switch bs.B[0] {
228+
case OK_HEADER:
229+
return c.handleOKPacket(bs.B)
230+
case ERR_HEADER:
231+
return nil, c.handleErrorPacket(bytes.Repeat(bs.B, 1))
232+
case LocalInFile_HEADER:
231233
return nil, ErrMalformPacket
234+
default:
235+
return c.readResultset(bs.B, binary)
232236
}
233-
234-
return c.readResultset(firstPkgBuf, binary)
235237
}
236238

237239
func (c *Conn) readResultStreaming(binary bool, result *Result, perRowCb SelectPerRowCallback, perResCb SelectPerResultCallback) error {
238-
firstPkgBuf, err := c.ReadPacketReuseMem(utils.ByteSliceGet(16)[:0])
239-
defer utils.ByteSlicePut(firstPkgBuf)
240-
240+
bs := utils.ByteSliceGet(16)
241+
defer utils.ByteSlicePut(bs)
242+
var err error
243+
bs.B, err = c.ReadPacketReuseMem(bs.B[:0])
241244
if err != nil {
242245
return errors.Trace(err)
243246
}
244247

245-
if firstPkgBuf[0] == OK_HEADER {
248+
switch bs.B[0] {
249+
case OK_HEADER:
246250
// https://dev.mysql.com/doc/internals/en/com-query-response.html
247251
// 14.6.4.1 COM_QUERY Response
248252
// If the number of columns in the resultset is 0, this is a OK_Packet.
249253

250-
okResult, err := c.handleOKPacket(firstPkgBuf)
254+
okResult, err := c.handleOKPacket(bs.B)
251255
if err != nil {
252256
return errors.Trace(err)
253257
}
@@ -262,13 +266,13 @@ func (c *Conn) readResultStreaming(binary bool, result *Result, perRowCb SelectP
262266
result.Reset(0)
263267
}
264268
return nil
265-
} else if firstPkgBuf[0] == ERR_HEADER {
266-
return c.handleErrorPacket(append([]byte{}, firstPkgBuf...))
267-
} else if firstPkgBuf[0] == LocalInFile_HEADER {
269+
case ERR_HEADER:
270+
return c.handleErrorPacket(bytes.Repeat(bs.B, 1))
271+
case LocalInFile_HEADER:
268272
return ErrMalformPacket
273+
default:
274+
return c.readResultsetStreaming(bs.B, binary, result, perRowCb, perResCb)
269275
}
270-
271-
return c.readResultsetStreaming(firstPkgBuf, binary, result, perRowCb, perResCb)
272276
}
273277

274278
func (c *Conn) readResultset(data []byte, binary bool) (*Result, error) {

dump/parser.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ func init() {
3535
// The pattern will only match MySQL GTID, as you know SET GLOBAL gtid_slave_pos='0-1-4' is used for MariaDB.
3636
// SET @@GLOBAL.GTID_PURGED='1638041a-0457-11e9-bb9f-00505690b730:1-429405150';
3737
// https://dev.mysql.com/doc/refman/5.7/en/replication-gtids-concepts.html
38-
gtidExp = regexp.MustCompile(`(\w{8}(-\w{4}){3}-\w{12}(:\d+-\d+)+)`)
38+
gtidExp = regexp.MustCompile(`(\w{8}(-\w{4}){3}-\w{12}(:\d+(-\d+)?)+)`)
3939
}
4040

4141
// Parse the dump data with Dumper generate.

dump/parser_test.go

+5
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,11 @@ e7574090-b123-11e8-8bb4-005056a29643:1-12'
4444
`,
4545
"e50bd2d3-6ad7-11e9-890c-42010af0017c:1-5291126581:5291126583-5323107666",
4646
},
47+
{`SET @@GLOBAL.GTID_PURGED='071a84e8-b253-11e8-8472-005056a27e86:1,
48+
2337be48-0456-11e9-bd1c-00505690543b:1-7,
49+
5f1eea9e-b1e5-11e8-bc77-005056a221ed:1-144609156,
50+
e7574090-b123-11e8-8bb4-005056a29643:1'
51+
`, "071a84e8-b253-11e8-8472-005056a27e86:1,2337be48-0456-11e9-bd1c-00505690543b:1-7,5f1eea9e-b1e5-11e8-bc77-005056a221ed:1-144609156,e7574090-b123-11e8-8bb4-005056a29643:1"},
4752
}
4853

4954
for _, tt := range tbls {

dump/setup_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import (
1010

1111
// use docker mysql for test
1212
var host = flag.String("host", "127.0.0.1", "MySQL host")
13-
var port = flag.Int("port", 3306, "MySQL host")
13+
var port = flag.Int("port", 3306, "MySQL port")
1414

1515
var execution = flag.String("exec", "mysqldump", "mysqldump execution path")
1616

go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@ go 1.16
55
require (
66
github.com/BurntSushi/toml v0.3.1
77
github.com/go-sql-driver/mysql v1.5.0
8+
github.com/google/uuid v1.3.0
89
github.com/jmoiron/sqlx v1.3.3
910
github.com/pingcap/check v0.0.0-20190102082844-67f458068fc8
1011
github.com/pingcap/errors v0.11.5-0.20201126102027-b0a155152ca3
1112
github.com/pingcap/log v0.0.0-20210317133921-96f4fcab92a4 // indirect
1213
github.com/pingcap/parser v0.0.0-20210415081931-48e7f467fd74
13-
github.com/satori/go.uuid v1.2.0
1414
github.com/shopspring/decimal v0.0.0-20180709203117-cd690d0c9e24
1515
github.com/siddontang/go v0.0.0-20180604090527-bdc77568d726
1616
github.com/siddontang/go-log v0.0.0-20180807004314-8d05993dda07

go.sum

+2
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ github.com/go-sql-driver/mysql v1.3.0/go.mod h1:zAC/RDZ24gD3HViQzih4MyKcchzm+sOG
1313
github.com/go-sql-driver/mysql v1.5.0 h1:ozyZYNQW3x3HtqT1jira07DN2PArx2v7/mN66gGcHOs=
1414
github.com/go-sql-driver/mysql v1.5.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LBy8hT2VhHyBg=
1515
github.com/google/renameio v0.1.0/go.mod h1:KWCgfxg9yswjAJkECMjeO8J8rahYeXnNhOm40UhjYkI=
16+
github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I=
17+
github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
1618
github.com/jmoiron/sqlx v1.3.3 h1:j82X0bf7oQ27XeqxicSZsTU5suPwKElg3oyxNn43iTk=
1719
github.com/jmoiron/sqlx v1.3.3/go.mod h1:2BljVx/86SuTyjE+aPYlHCTNvZrnJXghYGpNiXLBMCQ=
1820
github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=

mysql/mysql_gtid.go

+6-4
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ import (
1010
"strconv"
1111
"strings"
1212

13+
"github.com/google/uuid"
1314
"github.com/pingcap/errors"
14-
uuid "github.com/satori/go.uuid"
1515
"github.com/siddontang/go/hack"
1616
)
1717

@@ -174,7 +174,7 @@ func ParseUUIDSet(str string) (*UUIDSet, error) {
174174

175175
var err error
176176
s := new(UUIDSet)
177-
if s.SID, err = uuid.FromString(sep[0]); err != nil {
177+
if s.SID, err = uuid.Parse(sep[0]); err != nil {
178178
return nil, errors.Trace(err)
179179
}
180180

@@ -203,7 +203,7 @@ func NewUUIDSet(sid uuid.UUID, in ...Interval) *UUIDSet {
203203
}
204204

205205
func (s *UUIDSet) Contain(sub *UUIDSet) bool {
206-
if !bytes.Equal(s.SID.Bytes(), sub.SID.Bytes()) {
206+
if s.SID != sub.SID {
207207
return false
208208
}
209209

@@ -280,7 +280,9 @@ func (s *UUIDSet) String() string {
280280
}
281281

282282
func (s *UUIDSet) encode(w io.Writer) {
283-
_, _ = w.Write(s.SID.Bytes())
283+
b, _ := s.SID.MarshalBinary()
284+
285+
_, _ = w.Write(b)
284286
n := int64(len(s.Intervals))
285287

286288
_ = binary.Write(w, binary.LittleEndian, n)

replication/binlogsyncer.go

+6-2
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ import (
1010
"sync"
1111
"time"
1212

13+
"github.com/google/uuid"
1314
"github.com/pingcap/errors"
14-
uuid "github.com/satori/go.uuid"
1515
"github.com/siddontang/go-log/log"
1616

1717
"github.com/go-mysql-org/go-mysql/client"
@@ -312,7 +312,11 @@ func (b *BinlogSyncer) registerSlave() error {
312312
return errors.Trace(err)
313313
}
314314

315-
serverUUID := uuid.NewV1()
315+
serverUUID, err := uuid.NewUUID()
316+
if err != nil {
317+
log.Errorf("failed to get new uud %v", err)
318+
return errors.Trace(err)
319+
}
316320
if _, err = b.c.Execute(fmt.Sprintf("SET @slave_uuid = '%s', @replica_uuid = '%s'", serverUUID, serverUUID)); err != nil {
317321
log.Errorf("failed to set @slave_uuid = '%s', err: %v", serverUUID, err)
318322
return errors.Trace(err)

replication/event.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ import (
1010
"time"
1111
"unicode"
1212

13+
"github.com/google/uuid"
1314
"github.com/pingcap/errors"
14-
uuid "github.com/satori/go.uuid"
1515

1616
. "github.com/go-mysql-org/go-mysql/mysql"
1717
)

replication/replication_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ import (
1010
"testing"
1111
"time"
1212

13+
"github.com/google/uuid"
1314
. "github.com/pingcap/check"
14-
uuid "github.com/satori/go.uuid"
1515

1616
"github.com/go-mysql-org/go-mysql/client"
1717
"github.com/go-mysql-org/go-mysql/mysql"
@@ -339,7 +339,7 @@ func (t *testSyncerSuite) TestMysqlGTIDSync(c *C) {
339339

340340
var masterUuid uuid.UUID
341341
if s, _ := r.GetString(0, 1); len(s) > 0 && s != "NONE" {
342-
masterUuid, err = uuid.FromString(s)
342+
masterUuid, err = uuid.Parse(s)
343343
c.Assert(err, IsNil)
344344
}
345345

utils/byte_slice_pool.go

+13-19
Original file line numberDiff line numberDiff line change
@@ -2,35 +2,29 @@ package utils
22

33
import "sync"
44

5+
type ByteSlice struct {
6+
B []byte
7+
}
8+
59
var (
610
byteSlicePool = sync.Pool{
711
New: func() interface{} {
8-
return []byte{}
12+
return new(ByteSlice)
913
},
1014
}
11-
byteSliceChan = make(chan []byte, 10)
1215
)
1316

14-
func ByteSliceGet(length int) (data []byte) {
15-
select {
16-
case data = <-byteSliceChan:
17-
default:
18-
data = byteSlicePool.Get().([]byte)[:0]
19-
}
20-
21-
if cap(data) < length {
22-
data = make([]byte, length)
17+
func ByteSliceGet(length int) *ByteSlice {
18+
data := byteSlicePool.Get().(*ByteSlice)
19+
if cap(data.B) < length {
20+
data.B = make([]byte, length)
2321
} else {
24-
data = data[:length]
22+
data.B = data.B[:length]
2523
}
26-
2724
return data
2825
}
2926

30-
func ByteSlicePut(data []byte) {
31-
select {
32-
case byteSliceChan <- data:
33-
default:
34-
byteSlicePool.Put(data) //nolint:staticcheck
35-
}
27+
func ByteSlicePut(data *ByteSlice) {
28+
data.B = data.B[:0]
29+
byteSlicePool.Put(data)
3630
}

utils/byte_slice_pool_test.go

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package utils
2+
3+
import "testing"
4+
5+
func BenchmarkByteSlicePool(b *testing.B) {
6+
b.ReportAllocs()
7+
for i := 0; i < b.N; i++ {
8+
b := ByteSliceGet(16)
9+
b.B = append(b.B[:0], 0, 1)
10+
ByteSlicePut(b)
11+
}
12+
}

0 commit comments

Comments
 (0)