-
Notifications
You must be signed in to change notification settings - Fork 2.1k
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
go/mysql: improve GTID encoding for OK packet #16361
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -787,15 +787,15 @@ func (c *Conn) writeOKPacketWithHeader(packetOk *PacketOK, headerType byte) erro | |
// assuming CapabilityClientProtocol41 | ||
length += 4 // status_flags + warnings | ||
|
||
hasSessionTrack := c.Capabilities&CapabilityClientSessionTrack == CapabilityClientSessionTrack | ||
hasGtidData := hasSessionTrack && packetOk.statusFlags&ServerSessionStateChanged == ServerSessionStateChanged | ||
|
||
var gtidData []byte | ||
if c.Capabilities&CapabilityClientSessionTrack == CapabilityClientSessionTrack { | ||
|
||
if hasSessionTrack { | ||
length += lenEncStringSize(packetOk.info) // info | ||
if packetOk.statusFlags&ServerSessionStateChanged == ServerSessionStateChanged { | ||
gtidData = getLenEncString([]byte(packetOk.sessionStateData)) | ||
gtidData = append([]byte{0x00}, gtidData...) | ||
gtidData = getLenEncString(gtidData) | ||
gtidData = append([]byte{0x03}, gtidData...) | ||
gtidData = append(getLenEncInt(uint64(len(gtidData))), gtidData...) | ||
if hasGtidData { | ||
gtidData = encGtidData(packetOk.sessionStateData) | ||
length += len(gtidData) | ||
} | ||
} else { | ||
|
@@ -809,50 +809,17 @@ func (c *Conn) writeOKPacketWithHeader(packetOk *PacketOK, headerType byte) erro | |
data.writeLenEncInt(packetOk.lastInsertID) | ||
data.writeUint16(packetOk.statusFlags) | ||
data.writeUint16(packetOk.warnings) | ||
if c.Capabilities&CapabilityClientSessionTrack == CapabilityClientSessionTrack { | ||
if hasSessionTrack { | ||
data.writeLenEncString(packetOk.info) | ||
if packetOk.statusFlags&ServerSessionStateChanged == ServerSessionStateChanged { | ||
data.writeEOFString(string(gtidData)) | ||
if hasGtidData { | ||
data.writeEOFBytes(gtidData) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I guess I can further specialize this explicitly into a Then we avoid the intermediary |
||
} | ||
} else { | ||
data.writeEOFString(packetOk.info) | ||
} | ||
return c.writeEphemeralPacket() | ||
} | ||
|
||
func getLenEncString(value []byte) []byte { | ||
data := getLenEncInt(uint64(len(value))) | ||
return append(data, value...) | ||
} | ||
|
||
func getLenEncInt(i uint64) []byte { | ||
var data []byte | ||
switch { | ||
case i < 251: | ||
data = append(data, byte(i)) | ||
case i < 1<<16: | ||
data = append(data, 0xfc) | ||
data = append(data, byte(i)) | ||
data = append(data, byte(i>>8)) | ||
case i < 1<<24: | ||
data = append(data, 0xfd) | ||
data = append(data, byte(i)) | ||
data = append(data, byte(i>>8)) | ||
data = append(data, byte(i>>16)) | ||
default: | ||
data = append(data, 0xfe) | ||
data = append(data, byte(i)) | ||
data = append(data, byte(i>>8)) | ||
data = append(data, byte(i>>16)) | ||
data = append(data, byte(i>>24)) | ||
data = append(data, byte(i>>32)) | ||
data = append(data, byte(i>>40)) | ||
data = append(data, byte(i>>48)) | ||
data = append(data, byte(i>>56)) | ||
} | ||
return data | ||
} | ||
|
||
func (c *Conn) WriteErrorAndLog(format string, args ...interface{}) bool { | ||
return c.writeErrorAndLog(sqlerror.ERUnknownComError, sqlerror.SSNetError, format, args...) | ||
} | ||
|
@@ -1290,7 +1257,6 @@ func (c *Conn) handleComPrepare(handler Handler, data []byte) (kontinue bool) { | |
c.PrepareData[c.StatementID] = prepare | ||
|
||
fld, err := handler.ComPrepare(c, queries[0], bindVars) | ||
|
||
if err != nil { | ||
return c.writeErrorPacketFromErrorAndLog(err) | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit, but using
!= 0
is more obvious and standard — at least within Vitess — although there's no practical change.