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

Change @@version and @@version_comment #7337

Merged
merged 1 commit into from
Jan 26, 2021
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
5 changes: 3 additions & 2 deletions go/mysql/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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"
Expand Down Expand Up @@ -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,
Expand Down
14 changes: 14 additions & 0 deletions go/test/endtoend/vtgate/misc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
21 changes: 18 additions & 3 deletions go/vt/servenv/buildinfo.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 = ""
Expand All @@ -52,18 +57,27 @@ type versionInfo struct {
goVersion string
goOS string
goArch string
version string
}

func (v *versionInfo) Print() {
fmt.Println(v)
}

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() {
Expand All @@ -88,6 +102,7 @@ func init() {
goVersion: runtime.Version(),
goOS: runtime.GOOS,
goArch: runtime.GOARCH,
version: versionName,
}

stats.NewString("BuildHost").Set(AppVersion.buildHost)
Expand Down
52 changes: 52 additions & 0 deletions go/vt/servenv/buildinfo_test.go
Original file line number Diff line number Diff line change
@@ -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())
}
3 changes: 3 additions & 0 deletions go/vt/servenv/version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package servenv

const versionName = "10.0.0-SNAPSHOT"
3 changes: 2 additions & 1 deletion go/vt/sqlparser/ast_rewriting.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
25 changes: 15 additions & 10 deletions go/vt/sqlparser/ast_rewriting_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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`",
Expand Down Expand Up @@ -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")
})
}
}
Expand Down
5 changes: 3 additions & 2 deletions go/vt/sysvars/sysvars.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"}
Expand Down
4 changes: 3 additions & 1 deletion go/vt/vtgate/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}
}
Expand Down
5 changes: 2 additions & 3 deletions go/vt/vtgate/plugin_mysql_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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)
Expand Down