Skip to content

MariaDB Metadata skipping and DEPRECATE_EOF #1708

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

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
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
59 changes: 57 additions & 2 deletions benchmark_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ func BenchmarkExec(b *testing.B) {
b.ReportAllocs()
b.ResetTimer()

for i := 0; i < concurrencyLevel; i++ {
for i := 0; i < concurrencyLevel; i++ {
go func() {
for {
if atomic.AddInt64(&remain, -1) < 0 {
Expand Down Expand Up @@ -400,7 +400,7 @@ func benchmark10kRows(b *testing.B, compress bool) {
}

args := make([]any, 200)
for i := 1; i < 200; i+=2 {
for i := 1; i < 200; i += 2 {
args[i] = sval
}
for i := 0; i < 10000; i += 100 {
Expand Down Expand Up @@ -455,3 +455,58 @@ func BenchmarkReceive10kRows(b *testing.B) {
func BenchmarkReceive10kRowsCompressed(b *testing.B) {
benchmark10kRows(b, true)
}

// BenchmarkReceiveMetadata measures performance of receiving lots of metadata compare to data in rows
func BenchmarkReceiveMetadata(b *testing.B) {
tb := (*TB)(b)

// Create a table with 1000 integer fields
createTableQuery := "CREATE TABLE large_integer_table ("
for i := 0; i < 1000; i++ {
createTableQuery += fmt.Sprintf("col_%d INT", i)
if i < 999 {
createTableQuery += ", "
}
}
createTableQuery += ")"

// Initialize database
db := initDB(b, false,
"DROP TABLE IF EXISTS large_integer_table",
createTableQuery,
"INSERT INTO large_integer_table VALUES ("+
strings.Repeat("0,", 999)+"0)", // Insert a row of zeros
)
defer db.Close()

b.Run("query", func(b *testing.B) {
db.SetMaxIdleConns(0)
db.SetMaxIdleConns(1)

// Create a slice to scan all columns
values := make([]any, 1000)
valuePtrs := make([]any, 1000)
for j := range values {
valuePtrs[j] = &values[j]
}

b.ReportAllocs()
b.ResetTimer()

// Prepare a SELECT query to retrieve metadata
stmt := tb.checkStmt(db.Prepare("SELECT * FROM large_integer_table LIMIT 1"))
defer stmt.Close()

// Benchmark metadata retrieval
for range b.N {
rows := tb.checkRows(stmt.Query())

rows.Next()
// Scan the row
err := rows.Scan(valuePtrs...)
tb.check(err)

rows.Close()
}
})
}
29 changes: 19 additions & 10 deletions connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ type mysqlConn struct {
connector *connector
maxAllowedPacket int
maxWriteSize int
flags clientFlag
capabilities capabilityFlag
extCapabilities extendedCapabilityFlag
status statusFlag
sequence uint8
compressSequence uint8
Expand Down Expand Up @@ -223,13 +224,21 @@ func (mc *mysqlConn) Prepare(query string) (driver.Stmt, error) {
columnCount, err := stmt.readPrepareResultPacket()
if err == nil {
if stmt.paramCount > 0 {
if err = mc.readUntilEOF(); err != nil {
if err = mc.skipColumns(stmt.paramCount); err != nil {
return nil, err
}
}

if columnCount > 0 {
err = mc.readUntilEOF()
if mc.extCapabilities&clientCacheMetadata != 0 {
if stmt.columns, err = mc.readColumns(int(columnCount)); err != nil {
return nil, err
}
} else {
if err = mc.skipColumns(int(columnCount)); err != nil {
return nil, err
}
}
}
}

Expand Down Expand Up @@ -370,19 +379,19 @@ func (mc *mysqlConn) exec(query string) error {
}

// Read Result
resLen, err := handleOk.readResultSetHeaderPacket()
resLen, _, err := handleOk.readResultSetHeaderPacket()
if err != nil {
return err
}

if resLen > 0 {
// columns
if err := mc.readUntilEOF(); err != nil {
if err := mc.skipColumns(resLen); err != nil {
return err
}

// rows
if err := mc.readUntilEOF(); err != nil {
if err := mc.skipRows(); err != nil {
return err
}
}
Expand Down Expand Up @@ -419,7 +428,7 @@ func (mc *mysqlConn) query(query string, args []driver.Value) (*textRows, error)

// Read Result
var resLen int
resLen, err = handleOk.readResultSetHeaderPacket()
resLen, _, err = handleOk.readResultSetHeaderPacket()
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -453,22 +462,22 @@ func (mc *mysqlConn) getSystemVar(name string) ([]byte, error) {
}

// Read Result
resLen, err := handleOk.readResultSetHeaderPacket()
resLen, _, err := handleOk.readResultSetHeaderPacket()
if err == nil {
rows := new(textRows)
rows.mc = mc
rows.rs.columns = []mysqlField{{fieldType: fieldTypeVarChar}}

if resLen > 0 {
// Columns
if err := mc.readUntilEOF(); err != nil {
if err := mc.skipColumns(resLen); err != nil {
return nil, err
}
}

dest := make([]driver.Value, resLen)
if err = rows.readRow(dest); err == nil {
return dest[0].([]byte), mc.readUntilEOF()
return dest[0].([]byte), mc.skipRows()
}
}
return nil, err
Expand Down
10 changes: 8 additions & 2 deletions connector.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,12 +131,16 @@ func (c *connector) Connect(ctx context.Context) (driver.Conn, error) {
mc.buf = newBuffer()

// Reading Handshake Initialization Packet
authData, plugin, err := mc.readHandshakePacket()
authData, serverCapabilities, serverExtCapabilities, plugin, err := mc.readHandshakePacket()
if err != nil {
mc.cleanup()
return nil, err
}

if mc.cfg.TLS != nil && serverCapabilities&clientSSL == 0 {
return nil, fmt.Errorf("TLS is required, but server doesn't support it")
}

if plugin == "" {
plugin = defaultAuthPlugin
}
Expand All @@ -153,6 +157,7 @@ func (c *connector) Connect(ctx context.Context) (driver.Conn, error) {
return nil, err
}
}
mc.initCapabilities(serverCapabilities, serverExtCapabilities, mc.cfg)
if err = mc.writeHandshakeResponsePacket(authResp, plugin); err != nil {
mc.cleanup()
return nil, err
Expand All @@ -167,7 +172,8 @@ func (c *connector) Connect(ctx context.Context) (driver.Conn, error) {
return nil, err
}

if mc.cfg.compress && mc.flags&clientCompress == clientCompress {
// compression is enabled after auth, not right after sending handshake response.
if mc.capabilities&clientCompress > 0 {
mc.compress = true
mc.compIO = newCompIO(mc)
}
Expand Down
19 changes: 16 additions & 3 deletions const.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,12 @@ const (
iERR byte = 0xff
)

// https://dev.mysql.com/doc/internals/en/capability-flags.html#packet-Protocol::CapabilityFlags
type clientFlag uint32
// https://dev.mysql.com/doc/dev/mysql-server/latest/group__group__cs__capabilities__flags.html
// https://mariadb.com/kb/en/connection/#capabilities
type capabilityFlag uint32

const (
clientLongPassword clientFlag = 1 << iota
clientMySQL capabilityFlag = 1 << iota
clientFoundRows
clientLongFlag
clientConnectWithDB
Expand All @@ -73,6 +74,18 @@ const (
clientDeprecateEOF
)

// https://mariadb.com/kb/en/connection/#capabilities
type extendedCapabilityFlag uint32

const (
progressIndicator extendedCapabilityFlag = 1 << iota
clientComMulti
clientStmtBulkOperations
clientExtendedMetadata
clientCacheMetadata
clientUnitBulkResult
)

const (
comQuit byte = iota + 1
comInitDB
Expand Down
Loading
Loading