From bc4186242970895a17a202ec9335407fb5b2ee50 Mon Sep 17 00:00:00 2001 From: Andres Taylor Date: Thu, 21 Jan 2021 06:26:51 +0100 Subject: [PATCH] Cleanup in version system variables Signed-off-by: Andres Taylor --- go/mysql/server.go | 5 +-- go/test/endtoend/vtgate/misc_test.go | 14 ++++++++ go/vt/servenv/buildinfo.go | 21 +++++++++-- go/vt/servenv/buildinfo_test.go | 52 +++++++++++++++++++++++++++ go/vt/servenv/version.go | 3 ++ go/vt/sqlparser/ast_rewriting.go | 3 +- go/vt/sqlparser/ast_rewriting_test.go | 25 +++++++------ go/vt/sysvars/sysvars.go | 5 +-- go/vt/vtgate/executor.go | 4 ++- go/vt/vtgate/plugin_mysql_server.go | 5 ++- 10 files changed, 115 insertions(+), 22 deletions(-) create mode 100644 go/vt/servenv/buildinfo_test.go create mode 100644 go/vt/servenv/version.go diff --git a/go/mysql/server.go b/go/mysql/server.go index a6696108893..ff9584be394 100644 --- a/go/mysql/server.go +++ b/go/mysql/server.go @@ -24,6 +24,8 @@ import ( "sync/atomic" "time" + "vitess.io/vitess/go/vt/servenv" + "vitess.io/vitess/go/sqlescape" proxyproto "github.com/pires/go-proxyproto" @@ -42,7 +44,6 @@ import ( const ( // DefaultServerVersion is the default server version we're sending to the client. // Can be changed. - DefaultServerVersion = "5.7.9-Vitess" // timing metric keys connectTimingKey = "Connect" @@ -232,7 +233,7 @@ func NewListenerWithConfig(cfg ListenerConfig) (*Listener, error) { authServer: cfg.AuthServer, handler: cfg.Handler, listener: l, - ServerVersion: DefaultServerVersion, + ServerVersion: servenv.AppVersion.MySQLVersion(), connectionID: 1, connReadTimeout: cfg.ConnReadTimeout, connWriteTimeout: cfg.ConnWriteTimeout, diff --git a/go/test/endtoend/vtgate/misc_test.go b/go/test/endtoend/vtgate/misc_test.go index a6265191c4c..e928b7cbc4b 100644 --- a/go/test/endtoend/vtgate/misc_test.go +++ b/go/test/endtoend/vtgate/misc_test.go @@ -496,6 +496,20 @@ func TestCreateView(t *testing.T) { assertMatches(t, conn, "select * from v1", `[[INT64(1) INT64(1)] [INT64(2) INT64(2)] [INT64(3) INT64(3)] [INT64(4) INT64(4)] [INT64(5) INT64(5)]]`) } +func TestVersions(t *testing.T) { + defer cluster.PanicHandler(t) + ctx := context.Background() + conn, err := mysql.Connect(ctx, &vtParams) + require.NoError(t, err) + defer conn.Close() + + qr := exec(t, conn, `select @@version`) + assert.Contains(t, fmt.Sprintf("%v", qr.Rows), "vitess") + + qr = exec(t, conn, `select @@version_comment`) + assert.Contains(t, fmt.Sprintf("%v", qr.Rows), "Git revision") +} + func TestFlush(t *testing.T) { defer cluster.PanicHandler(t) ctx := context.Background() diff --git a/go/vt/servenv/buildinfo.go b/go/vt/servenv/buildinfo.go index 4eb9bbf3c8f..d73c3e07c57 100644 --- a/go/vt/servenv/buildinfo.go +++ b/go/vt/servenv/buildinfo.go @@ -27,6 +27,11 @@ import ( ) var ( + // MySQLServerVersion is what Vitess will present as it's version during the connection handshake, + // and as the value to the @@version system variable. If nothing is provided, Vitess will report itself as + // a specific MySQL version with the vitess version appended to it + MySQLServerVersion = flag.String("mysql_server_version", "", "MySQL server version to advertise.") + buildHost = "" buildUser = "" buildTime = "" @@ -52,6 +57,7 @@ type versionInfo struct { goVersion string goOS string goArch string + version string } func (v *versionInfo) Print() { @@ -59,11 +65,19 @@ func (v *versionInfo) Print() { } func (v *versionInfo) String() string { - version := fmt.Sprintf("Version: %s", v.buildGitRev) + jenkins := "" if v.jenkinsBuildNumber != 0 { - version = fmt.Sprintf("Version: %s (Jenkins build %d)", v.buildGitRev, v.jenkinsBuildNumber) + jenkins = fmt.Sprintf(" (Jenkins build %d)", v.jenkinsBuildNumber) + } + return fmt.Sprintf("Version: %s%s (Git revision %s branch '%s') built on %s by %s@%s using %s %s/%s", + v.version, jenkins, v.buildGitRev, v.buildGitBranch, v.buildTimePretty, v.buildUser, v.buildHost, v.goVersion, v.goOS, v.goArch) +} + +func (v *versionInfo) MySQLVersion() string { + if *MySQLServerVersion != "" { + return *MySQLServerVersion } - return fmt.Sprintf("%s (Git branch '%s') built on %s by %s@%s using %s %s/%s\n", version, v.buildGitBranch, v.buildTimePretty, v.buildUser, v.buildHost, v.goVersion, v.goOS, v.goArch) + return "5.7.9-vitess-" + v.version } func init() { @@ -88,6 +102,7 @@ func init() { goVersion: runtime.Version(), goOS: runtime.GOOS, goArch: runtime.GOARCH, + version: versionName, } stats.NewString("BuildHost").Set(AppVersion.buildHost) diff --git a/go/vt/servenv/buildinfo_test.go b/go/vt/servenv/buildinfo_test.go new file mode 100644 index 00000000000..63ba5596df7 --- /dev/null +++ b/go/vt/servenv/buildinfo_test.go @@ -0,0 +1,52 @@ +/* +Copyright 2021 The Vitess Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package servenv + +import ( + "testing" + "time" + + "github.com/stretchr/testify/assert" +) + +func TestVersionString(t *testing.T) { + now, _ := time.Parse(time.RFC1123, "Tue, 15 Sep 2020 12:04:10 UTC") + + v := &versionInfo{ + buildHost: "host", + buildUser: "user", + buildTime: now.Unix(), + buildTimePretty: "time is now", + buildGitRev: "d54b87c", + buildGitBranch: "gitBranch", + goVersion: "1.15", + goOS: "amiga", + goArch: "amd64", + version: "v1.2.3-SNAPSHOT", + } + + assert.Equal(t, "Version: v1.2.3-SNAPSHOT (Git revision d54b87c branch 'gitBranch') built on time is now by user@host using 1.15 amiga/amd64", v.String()) + + v.jenkinsBuildNumber = 422 + + assert.Equal(t, "Version: v1.2.3-SNAPSHOT (Jenkins build 422) (Git revision d54b87c branch 'gitBranch') built on time is now by user@host using 1.15 amiga/amd64", v.String()) + + assert.Equal(t, "5.7.9-vitess-v1.2.3-SNAPSHOT", v.MySQLVersion()) + newVersion := "test!" + MySQLServerVersion = &newVersion + assert.Equal(t, newVersion, v.MySQLVersion()) +} diff --git a/go/vt/servenv/version.go b/go/vt/servenv/version.go new file mode 100644 index 00000000000..5028748c55d --- /dev/null +++ b/go/vt/servenv/version.go @@ -0,0 +1,3 @@ +package servenv + +const versionName = "10.0.0-SNAPSHOT" diff --git a/go/vt/sqlparser/ast_rewriting.go b/go/vt/sqlparser/ast_rewriting.go index 151fc4291f7..bc4fbdc9df5 100644 --- a/go/vt/sqlparser/ast_rewriting.go +++ b/go/vt/sqlparser/ast_rewriting.go @@ -238,7 +238,8 @@ func (er *expressionRewriter) sysVarRewrite(cursor *Cursor, node *ColName) { sysvars.SessionEnableSystemSettings.Name, sysvars.ReadAfterWriteGTID.Name, sysvars.ReadAfterWriteTimeOut.Name, - sysvars.VitessVersion.Name, + sysvars.Version.Name, + sysvars.VersionComment.Name, sysvars.SessionTrackGTIDs.Name: cursor.Replace(bindVarExpression("__vt" + lowered)) er.bindVars.AddSysVar(lowered) diff --git a/go/vt/sqlparser/ast_rewriting_test.go b/go/vt/sqlparser/ast_rewriting_test.go index 15b25d068ac..cb9a5ddb85d 100644 --- a/go/vt/sqlparser/ast_rewriting_test.go +++ b/go/vt/sqlparser/ast_rewriting_test.go @@ -27,12 +27,12 @@ import ( ) type myTestCase struct { - in, expected string - liid, db, foundRows, rowCount, rawGTID, rawTimeout, sessTrackGTID bool - ddlStrategy, sessionUUID, sessionEnableSystemSettings bool - udv int - autocommit, clientFoundRows, skipQueryPlanCache bool - sqlSelectLimit, transactionMode, workload, vitessVersion bool + in, expected string + liid, db, foundRows, rowCount, rawGTID, rawTimeout, sessTrackGTID bool + ddlStrategy, sessionUUID, sessionEnableSystemSettings bool + udv int + autocommit, clientFoundRows, skipQueryPlanCache bool + sqlSelectLimit, transactionMode, workload, version, versionComment bool } func TestRewrites(in *testing.T) { @@ -41,9 +41,13 @@ func TestRewrites(in *testing.T) { expected: "SELECT 42", // no bindvar needs }, { - in: "SELECT @@vitess_version", - expected: "SELECT :__vtvitess_version as `@@vitess_version`", - vitessVersion: true, + in: "SELECT @@version", + expected: "SELECT :__vtversion as `@@version`", + version: true, + }, { + in: "SELECT @@version_comment", + expected: "SELECT :__vtversion_comment as `@@version_comment`", + versionComment: true, }, { in: "SELECT @@enable_system_settings", expected: "SELECT :__vtenable_system_settings as `@@enable_system_settings`", @@ -207,7 +211,8 @@ func TestRewrites(in *testing.T) { assert.Equal(tc.rawGTID, result.NeedsSysVar(sysvars.ReadAfterWriteGTID.Name), "should need rawGTID") assert.Equal(tc.rawTimeout, result.NeedsSysVar(sysvars.ReadAfterWriteTimeOut.Name), "should need rawTimeout") assert.Equal(tc.sessTrackGTID, result.NeedsSysVar(sysvars.SessionTrackGTIDs.Name), "should need sessTrackGTID") - assert.Equal(tc.vitessVersion, result.NeedsSysVar(sysvars.VitessVersion.Name), "should need Vitess version") + assert.Equal(tc.version, result.NeedsSysVar(sysvars.Version.Name), "should need Vitess version") + assert.Equal(tc.versionComment, result.NeedsSysVar(sysvars.VersionComment.Name), "should need Vitess version") }) } } diff --git a/go/vt/sysvars/sysvars.go b/go/vt/sysvars/sysvars.go index ecbffa94775..602d0042f83 100644 --- a/go/vt/sysvars/sysvars.go +++ b/go/vt/sysvars/sysvars.go @@ -56,8 +56,9 @@ var ( SessionUUID = SystemVariable{Name: "session_uuid", IdentifierAsString: true} SessionEnableSystemSettings = SystemVariable{Name: "enable_system_settings", IsBoolean: true, Default: on} // Online DDL - DDLStrategy = SystemVariable{Name: "ddl_strategy", IdentifierAsString: true} - VitessVersion = SystemVariable{Name: "vitess_version", IdentifierAsString: true} + DDLStrategy = SystemVariable{Name: "ddl_strategy", IdentifierAsString: true} + Version = SystemVariable{Name: "version"} + VersionComment = SystemVariable{Name: "version_comment"} // Read After Write settings ReadAfterWriteGTID = SystemVariable{Name: "read_after_write_gtid"} diff --git a/go/vt/vtgate/executor.go b/go/vt/vtgate/executor.go index f16e7122d90..4e29b8c8264 100644 --- a/go/vt/vtgate/executor.go +++ b/go/vt/vtgate/executor.go @@ -319,7 +319,9 @@ func (e *Executor) addNeededBindVars(bindVarNeeds *sqlparser.BindVarNeeds, bindV } }) bindVars[key] = sqltypes.StringBindVariable(v) - case sysvars.VitessVersion.Name: + case sysvars.Version.Name: + bindVars[key] = sqltypes.StringBindVariable(servenv.AppVersion.MySQLVersion()) + case sysvars.VersionComment.Name: bindVars[key] = sqltypes.StringBindVariable(servenv.AppVersion.String()) } } diff --git a/go/vt/vtgate/plugin_mysql_server.go b/go/vt/vtgate/plugin_mysql_server.go index dce5e5f37e4..b8168afe01d 100644 --- a/go/vt/vtgate/plugin_mysql_server.go +++ b/go/vt/vtgate/plugin_mysql_server.go @@ -56,7 +56,6 @@ var ( mysqlTCPVersion = flag.String("mysql_tcp_version", "tcp", "Select tcp, tcp4, or tcp6 to control the socket type.") mysqlAuthServerImpl = flag.String("mysql_auth_server_impl", "static", "Which auth server implementation to use. Options: none, ldap, clientcert, static, vault.") mysqlAllowClearTextWithoutTLS = flag.Bool("mysql_allow_clear_text_without_tls", false, "If set, the server will allow the use of a clear text password over non-SSL connections.") - mysqlServerVersion = flag.String("mysql_server_version", mysql.DefaultServerVersion, "MySQL server version to advertise.") mysqlProxyProtocol = flag.Bool("proxy_protocol", false, "Enable HAProxy PROXY protocol on MySQL listener socket") mysqlServerRequireSecureTransport = flag.Bool("mysql_server_require_secure_transport", false, "Reject insecure connections but only if mysql_server_ssl_cert and mysql_server_ssl_key are provided") @@ -425,8 +424,8 @@ func initMySQLProtocol() { if err != nil { log.Exitf("mysql.NewListener failed: %v", err) } - if *mysqlServerVersion != "" { - mysqlListener.ServerVersion = *mysqlServerVersion + if *servenv.MySQLServerVersion != "" { + mysqlListener.ServerVersion = *servenv.MySQLServerVersion } if *mysqlSslCert != "" && *mysqlSslKey != "" { initTLSConfig(mysqlListener, *mysqlSslCert, *mysqlSslKey, *mysqlSslCa, *mysqlServerRequireSecureTransport)