From 328385babd65fe786868df063dc483264a609edf Mon Sep 17 00:00:00 2001 From: newborn22 <953950914@qq.com> Date: Tue, 10 Dec 2024 20:10:19 +0800 Subject: [PATCH] fix: share mysqlServerPort --- go/internal/global/global.go | 17 ----------------- go/vt/share/share_vars.go | 3 +++ go/vt/vtgate/engine/branch_primitive.go | 4 ++-- go/vt/vtgate/plugin_mysql_server.go | 16 ++++++++++++---- go/vt/vtgate/vtgate_test.go | 3 +-- 5 files changed, 18 insertions(+), 25 deletions(-) create mode 100644 go/vt/share/share_vars.go diff --git a/go/internal/global/global.go b/go/internal/global/global.go index 6623be391f..a939db4eed 100644 --- a/go/internal/global/global.go +++ b/go/internal/global/global.go @@ -6,9 +6,7 @@ Licensed under the Apache v2(found in the LICENSE file in the root directory). package global import ( - "github.com/spf13/pflag" "time" - "vitess.io/vitess/go/vt/servenv" ) // Keyspace @@ -60,18 +58,3 @@ const ( const ( TopoServerConfigOverwriteShard = true ) - -// ***************************************************************************************************************************** - -var ( - MysqlServerPort = -1 -) - -func registerPluginFlags(fs *pflag.FlagSet) { - fs.IntVar(&MysqlServerPort, "mysql_server_port", MysqlServerPort, "If set, also listen for MySQL binary protocol connections on this port.") -} - -func init() { - servenv.OnParseFor("vtgate", registerPluginFlags) - servenv.OnParseFor("vtcombo", registerPluginFlags) -} diff --git a/go/vt/share/share_vars.go b/go/vt/share/share_vars.go new file mode 100644 index 0000000000..0157108e69 --- /dev/null +++ b/go/vt/share/share_vars.go @@ -0,0 +1,3 @@ +package share + +var GetMysqlServerPort func() int diff --git a/go/vt/vtgate/engine/branch_primitive.go b/go/vt/vtgate/engine/branch_primitive.go index 329fa7c8f9..c855467297 100644 --- a/go/vt/vtgate/engine/branch_primitive.go +++ b/go/vt/vtgate/engine/branch_primitive.go @@ -7,11 +7,11 @@ import ( "github.com/spf13/pflag" "strconv" "strings" - "vitess.io/vitess/go/internal/global" "vitess.io/vitess/go/sqltypes" querypb "vitess.io/vitess/go/vt/proto/query" "vitess.io/vitess/go/vt/schemadiff" "vitess.io/vitess/go/vt/servenv" + "vitess.io/vitess/go/vt/share" "vitess.io/vitess/go/vt/sqlparser" "vitess.io/vitess/go/vt/vtgate/branch" ) @@ -142,7 +142,7 @@ func BuildBranchPlan(branchCmd *sqlparser.BranchCommand) (*Branch, error) { b.targetHost = DefaultBranchTargetHost if DefaultBranchTargetPort == -1 { - b.targetPort = global.MysqlServerPort + b.targetPort = share.GetMysqlServerPort() } else { b.targetPort = DefaultBranchTargetPort } diff --git a/go/vt/vtgate/plugin_mysql_server.go b/go/vt/vtgate/plugin_mysql_server.go index 4984aefd03..89d48ec60f 100644 --- a/go/vt/vtgate/plugin_mysql_server.go +++ b/go/vt/vtgate/plugin_mysql_server.go @@ -33,7 +33,7 @@ import ( "sync/atomic" "syscall" "time" - "vitess.io/vitess/go/internal/global" + "vitess.io/vitess/go/vt/share" topodatapb "vitess.io/vitess/go/vt/proto/topodata" @@ -58,6 +58,7 @@ import ( ) var ( + mysqlServerPort = -1 mysqlServerBindAddress string mysqlServerSocketPath string mysqlTCPVersion = "tcp" @@ -85,6 +86,7 @@ var ( ) func registerPluginFlags(fs *pflag.FlagSet) { + fs.IntVar(&mysqlServerPort, "mysql_server_port", mysqlServerPort, "If set, also listen for MySQL binary protocol connections on this port.") fs.StringVar(&mysqlServerBindAddress, "mysql_server_bind_address", mysqlServerBindAddress, "Binds on this address when listening to MySQL binary protocol. Useful to restrict listening to 'localhost' only for instance.") fs.StringVar(&mysqlServerSocketPath, "mysql_server_socket_path", mysqlServerSocketPath, "This option specifies the Unix socket file to use when listening for local connections. By default it will be empty and it won't listen to a unix socket") fs.StringVar(&mysqlTCPVersion, "mysql_tcp_version", mysqlTCPVersion, "Select tcp, tcp4, or tcp6 to control the socket type.") @@ -104,6 +106,12 @@ func registerPluginFlags(fs *pflag.FlagSet) { fs.DurationVar(&mysqlQueryTimeout, "mysql_server_query_timeout", mysqlQueryTimeout, "mysql query timeout") fs.BoolVar(&mysqlConnBufferPooling, "mysql-server-pool-conn-read-buffers", mysqlConnBufferPooling, "If set, the server will pool incoming connection read buffers") fs.StringVar(&mysqlDefaultWorkloadName, "mysql_default_workload", mysqlDefaultWorkloadName, "Default session workload (OLTP, OLAP, DBA)") + + share.GetMysqlServerPort = GetMysqlServerPort +} + +func GetMysqlServerPort() int { + return mysqlServerPort } // vtgateHandler implements the Listener interface. @@ -463,7 +471,7 @@ func initTLSConfig(mysqlListener *mysql.Listener, mysqlSslCert, mysqlSslKey, mys // It should be called only once in a process. func initMySQLProtocol() { // Flag is not set, just return. - if global.MysqlServerPort < 0 && mysqlServerSocketPath == "" { + if mysqlServerPort < 0 && mysqlServerSocketPath == "" { return } @@ -493,10 +501,10 @@ func initMySQLProtocol() { // Create a Listener. var err error vtgateHandle = newVtgateHandler(rpcVTGate) - if global.MysqlServerPort >= 0 { + if mysqlServerPort >= 0 { mysqlListener, err = mysql.NewListener( mysqlTCPVersion, - net.JoinHostPort(mysqlServerBindAddress, fmt.Sprintf("%v", global.MysqlServerPort)), + net.JoinHostPort(mysqlServerBindAddress, fmt.Sprintf("%v", mysqlServerPort)), authServer, vtgateHandle, mysqlConnReadTimeout, diff --git a/go/vt/vtgate/vtgate_test.go b/go/vt/vtgate/vtgate_test.go index b5b3d3c544..ffbfdc3993 100644 --- a/go/vt/vtgate/vtgate_test.go +++ b/go/vt/vtgate/vtgate_test.go @@ -26,7 +26,6 @@ import ( "fmt" "strings" "testing" - "vitess.io/vitess/go/internal/global" "github.com/stretchr/testify/assert" "google.golang.org/protobuf/proto" @@ -69,7 +68,7 @@ func init() { transactionMode = "MULTI" Init(context.Background(), hcVTGateTest, newSandboxForCells([]string{"aa"}), "aa", nil, querypb.ExecuteOptions_Gen4) - global.MysqlServerPort = 0 + mysqlServerPort = 0 mysqlAuthServerImpl = "none" initMySQLProtocol() }