From d1783bbbebffb91385d4d756523a8e134375311b Mon Sep 17 00:00:00 2001 From: Alain Jobart Date: Mon, 22 May 2017 14:08:36 -0700 Subject: [PATCH] Changing mysql.ComQuery parameter to []byte. So we minimize copies. --- go/mysql/fakesqldb/server.go | 3 ++- go/mysql/query.go | 4 ++-- go/mysql/query_test.go | 2 +- go/mysql/server.go | 4 +++- go/mysql/server_test.go | 3 ++- go/vt/vtgate/plugin_mysql_server.go | 4 ++-- 6 files changed, 12 insertions(+), 8 deletions(-) diff --git a/go/mysql/fakesqldb/server.go b/go/mysql/fakesqldb/server.go index 877d1b4c10..fb606060be 100644 --- a/go/mysql/fakesqldb/server.go +++ b/go/mysql/fakesqldb/server.go @@ -241,7 +241,8 @@ func (db *DB) ConnectionClosed(c *mysql.Conn) { } // ComQuery is part of the mysql.Handler interface. -func (db *DB) ComQuery(c *mysql.Conn, query string) (*sqltypes.Result, error) { +func (db *DB) ComQuery(c *mysql.Conn, q []byte) (*sqltypes.Result, error) { + query := string(q) db.t.Logf("ComQuery(%v): client %v: %v", db.name, c.ConnectionID, query) key := strings.ToLower(query) diff --git a/go/mysql/query.go b/go/mysql/query.go index 3ff6bc5b2f..23d7658995 100644 --- a/go/mysql/query.go +++ b/go/mysql/query.go @@ -454,8 +454,8 @@ func (c *Conn) readComQueryResponse() (uint64, uint64, int, error) { // Server side methods. // -func (c *Conn) parseComQuery(data []byte) string { - return string(data[1:]) +func (c *Conn) parseComQuery(data []byte) []byte { + return data[1:] } func (c *Conn) parseComInitDB(data []byte) string { diff --git a/go/mysql/query_test.go b/go/mysql/query_test.go index f7bdf0933a..f431bcaf2c 100644 --- a/go/mysql/query_test.go +++ b/go/mysql/query_test.go @@ -404,7 +404,7 @@ func checkQueryInternal(t *testing.T, query string, sConn, cConn *Conn, result * if comQuery[0] != ComQuery { t.Fatalf("server got bad packet: %v", comQuery) } - got := sConn.parseComQuery(comQuery) + got := string(sConn.parseComQuery(comQuery)) if got != query { t.Errorf("server got query '%v' but expected '%v'", got, query) } diff --git a/go/mysql/server.go b/go/mysql/server.go index db41bc4f60..269d406d6f 100644 --- a/go/mysql/server.go +++ b/go/mysql/server.go @@ -54,7 +54,9 @@ type Handler interface { ConnectionClosed(c *Conn) // ComQuery is called when a connection receives a query. - ComQuery(c *Conn, query string) (*sqltypes.Result, error) + // Note the contents of the query slice may change after this method + // returns, so the Handler should not hang on to the byte slice. + ComQuery(c *Conn, query []byte) (*sqltypes.Result, error) } // Listener is the MySQL server protocol listener. diff --git a/go/mysql/server_test.go b/go/mysql/server_test.go index 4b5f4edf1d..7669bd0066 100644 --- a/go/mysql/server_test.go +++ b/go/mysql/server_test.go @@ -71,7 +71,8 @@ func (th *testHandler) NewConnection(c *Conn) { func (th *testHandler) ConnectionClosed(c *Conn) { } -func (th *testHandler) ComQuery(c *Conn, query string) (*sqltypes.Result, error) { +func (th *testHandler) ComQuery(c *Conn, q []byte) (*sqltypes.Result, error) { + query := string(q) if query == "error" { return nil, NewSQLError(ERUnknownComError, SSUnknownComError, "forced query handling error for: %v", query) } diff --git a/go/vt/vtgate/plugin_mysql_server.go b/go/vt/vtgate/plugin_mysql_server.go index ec63d0c288..48afc9980a 100644 --- a/go/vt/vtgate/plugin_mysql_server.go +++ b/go/vt/vtgate/plugin_mysql_server.go @@ -69,7 +69,7 @@ func (vh *vtgateHandler) ConnectionClosed(c *mysql.Conn) { } } -func (vh *vtgateHandler) ComQuery(c *mysql.Conn, query string) (*sqltypes.Result, error) { +func (vh *vtgateHandler) ComQuery(c *mysql.Conn, query []byte) (*sqltypes.Result, error) { // FIXME(alainjobart): Add some kind of timeout to the context. ctx := context.Background() @@ -99,7 +99,7 @@ func (vh *vtgateHandler) ComQuery(c *mysql.Conn, query string) (*sqltypes.Result if c.SchemaName != "" { session.TargetString = c.SchemaName } - session, result, err := vh.vtg.Execute(ctx, session, query, make(map[string]interface{})) + session, result, err := vh.vtg.Execute(ctx, session, string(query), make(map[string]interface{})) c.ClientData = session return result, mysql.NewSQLErrorFromError(err) }