Skip to content
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

fix: connection leak when concurrent updates #669

Merged
merged 1 commit into from
Apr 7, 2023
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
8 changes: 8 additions & 0 deletions justfile
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,11 @@ cli-raw:
fix:
@imports-formatter .
@license-eye header fix


sysbench MODE="run":
sysbench oltp_read_write --mysql-user=root --mysql-password=123456 --mysql-host=127.0.0.1 --mysql-port=3306 --mysql-db=employees_0000 --histogram=on --report-interval=1 --time=300 --db-ps-mode=disable --threads=64 --tables=250 --table_size=25000 --report-interval=1 --percentile=95 --skip-trx=on --mysql-ignore-errors=1062 --forced-shutdown=1 {{MODE}}


sysbench2 MODE="run":
sysbench oltp_read_write --mysql-user=arana --mysql-password=123456 --mysql-host=127.0.0.1 --mysql-port=13306 --mysql-db=employees --histogram=on --report-interval=1 --time=300 --db-ps-mode=disable --threads=8 --tables=250 --table_size=25000 --report-interval=1 --percentile=95 --skip-trx=on --mysql-ignore-errors=1062 --forced-shutdown=1 {{MODE}}
1 change: 1 addition & 0 deletions pkg/mysql/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -986,6 +986,7 @@ func (conn *BackendConnection) writeHandshakeResponse41(capabilities uint32, scr

// Sanity-check the length.
if pos != len(data) {
conn.c.recycleWritePacket()
return err2.NewSQLError(mysql.CRMalformedPacket, mysql.SSUnknownSQLState, "writeHandshakeResponse41: only packed %v bytes, out of %v allocated", pos, len(data))
}

Expand Down
18 changes: 10 additions & 8 deletions pkg/mysql/conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,7 @@ func (c *Conn) readEphemeralPacket() ([]byte, error) {
if length < mysql.MaxPacketSize {
c.currentEphemeralBuffer = bufPool.Get(length)
if _, err := io.ReadFull(r, *c.currentEphemeralBuffer); err != nil {
defer c.recycleReadPacket()
return nil, errors.Wrapf(err, "io.ReadFull(packet body of length %v) failed", length)
}
return *c.currentEphemeralBuffer, nil
Expand Down Expand Up @@ -371,12 +372,12 @@ func (c *Conn) readEphemeralPacket() ([]byte, error) {

// readEphemeralPacketDirect attempts to read a packet from the socket directly.
// It needs to be used for the first handshake packet the server receives,
// so we do't buffer the SSL negotiation packet. As a shortcut, only
// so we don't buffer the SSL negotiation packet. As a shortcut, only
// packets smaller than MaxPacketSize can be read here.
// This function usually shouldn't be used - use readEphemeralPacket.
func (c *Conn) readEphemeralPacketDirect() ([]byte, error) {
if c.currentEphemeralPolicy != ephemeralUnused {
panic(errors.Errorf("readEphemeralPacketDirect: unexpected currentEphemeralPolicy: %v", c.currentEphemeralPolicy))
panic(fmt.Sprintf("readEphemeralPacketDirect: unexpected currentEphemeralPolicy: %v!", c.currentEphemeralPolicy))
}

var r io.Reader = c.conn
Expand All @@ -396,6 +397,7 @@ func (c *Conn) readEphemeralPacketDirect() ([]byte, error) {
if length < mysql.MaxPacketSize {
c.currentEphemeralBuffer = bufPool.Get(length)
if _, err := io.ReadFull(r, *c.currentEphemeralBuffer); err != nil {
defer c.recycleReadPacket()
return nil, errors.Wrapf(err, "io.ReadFull(packet body of length %v) failed", length)
}
return *c.currentEphemeralBuffer, nil
Expand All @@ -409,7 +411,7 @@ func (c *Conn) readEphemeralPacketDirect() ([]byte, error) {
func (c *Conn) recycleReadPacket() {
if c.currentEphemeralPolicy != ephemeralRead {
// Programming error.
panic(errors.Errorf("trying to call recycleReadPacket while currentEphemeralPolicy is %d", c.currentEphemeralPolicy))
panic(fmt.Sprintf("trying to call recycleReadPacket while currentEphemeralPolicy is %d!", c.currentEphemeralPolicy))
}
if c.currentEphemeralBuffer != nil {
// We are using the pool, put the buffer back in.
Expand Down Expand Up @@ -603,7 +605,7 @@ func (c *Conn) writePacket(data []byte) error {

func (c *Conn) startEphemeralPacket(length int) []byte {
if c.currentEphemeralPolicy != ephemeralUnused {
panic("startEphemeralPacket cannot be used while a packet is already started.")
panic(fmt.Sprintf("startEphemeralPacket cannot be used while a packet is already started, actual is %v!", c.currentEphemeralPolicy))
}

c.currentEphemeralPolicy = ephemeralWrite
Expand All @@ -620,11 +622,11 @@ func (c *Conn) writeEphemeralPacket() error {
switch c.currentEphemeralPolicy {
case ephemeralWrite:
if err := c.writePacket(*c.currentEphemeralBuffer); err != nil {
return errors.WithStack(errors.Wrapf(err, "conn %v", c.ID()))
return errors.Wrapf(err, "conn %v", c.ID())
}
case ephemeralUnused, ephemeralRead:
// Programming error.
panic(errors.Errorf("conn %v: trying to call writeEphemeralPacket while currentEphemeralPolicy is %v", c.ID(), c.currentEphemeralPolicy))
panic(fmt.Sprintf("conn %v: trying to call writeEphemeralPacket while currentEphemeralPolicy is %v!", c.ID(), c.currentEphemeralPolicy))
}

return nil
Expand All @@ -635,7 +637,7 @@ func (c *Conn) writeEphemeralPacket() error {
func (c *Conn) recycleWritePacket() {
if c.currentEphemeralPolicy != ephemeralWrite {
// Programming error.
panic(errors.Errorf("trying to call recycleWritePacket while currentEphemeralPolicy is %d", c.currentEphemeralPolicy))
panic(fmt.Sprintf("trying to call recycleWritePacket while currentEphemeralPolicy is %d!", c.currentEphemeralPolicy))
}
// Release our reference so the buffer can be gced
bufPool.Put(c.currentEphemeralBuffer)
Expand Down Expand Up @@ -772,7 +774,7 @@ func (c *Conn) writeErrorPacket(errorCode uint16, sqlState string, format string
sqlState = mysql.SSUnknownSQLState
}
if len(sqlState) != 5 {
panic("sqlState has to be 5 characters long")
panic(fmt.Sprintf("sqlState has to be 5 characters long, actual is %d!", len(sqlState)))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

不能返回 error 吗?panic 不能轻易启用。

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

返回string吧,感觉更常见些。

}
pos = writeEOFString(data, pos, sqlState)
_ = writeEOFString(data, pos, errorMessage)
Expand Down
3 changes: 3 additions & 0 deletions pkg/mysql/execute_handle.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import (
"github.com/arana-db/arana/pkg/security"
"github.com/arana-db/arana/pkg/trace"
"github.com/arana-db/arana/pkg/util/log"
"github.com/arana-db/arana/pkg/util/misc"
)

func (l *Listener) handleInitDB(c *Conn, ctx *proto.Context) error {
Expand Down Expand Up @@ -117,6 +118,8 @@ func (l *Listener) handleQuery(c *Conn, ctx *proto.Context) error {
statusFlag |= mysql.ServerMoreResultsExists
}

_ = misc.TryClose(result)

if err := c.writeOKPacket(affected, insertId, statusFlag, warn); err != nil {
log.Errorf("failed to write OK packet into client %v: %v", ctx.C.ID(), err)
return err
Expand Down
2 changes: 1 addition & 1 deletion pkg/mysql/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ func (l *Listener) handshake(c *Conn) error {
return err
}

c.recycleReadPacket()
defer c.recycleReadPacket()

handshake, err := l.parseClientHandshakePacket(true, response)
if err != nil {
Expand Down
7 changes: 0 additions & 7 deletions pkg/runtime/runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (
"context"
"errors"
"fmt"
"io"
"sync"
"time"
)
Expand Down Expand Up @@ -413,12 +412,6 @@ func (pi *defaultRuntime) Exec(ctx context.Context, db string, query string, arg
if err != nil {
return nil, perrors.WithStack(err)
}

if closer, ok := res.(io.Closer); ok {
defer func() {
_ = closer.Close()
}()
}
return res, nil
}

Expand Down
8 changes: 8 additions & 0 deletions pkg/util/misc/misc.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package misc

import (
"io"
"regexp"
"sync"
)
Expand Down Expand Up @@ -48,3 +49,10 @@ func ParseTable(input string) (db, tbl string, err error) {
tbl = mat[2]
return
}

func TryClose(i interface{}) error {
if c, ok := i.(io.Closer); ok {
return c.Close()
}
return nil
}