Skip to content

Commit

Permalink
fix: share mysqlServerPort
Browse files Browse the repository at this point in the history
  • Loading branch information
newborn22 committed Dec 10, 2024
1 parent 6f5e5cb commit 328385b
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 25 deletions.
17 changes: 0 additions & 17 deletions go/internal/global/global.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
}
3 changes: 3 additions & 0 deletions go/vt/share/share_vars.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package share

var GetMysqlServerPort func() int
4 changes: 2 additions & 2 deletions go/vt/vtgate/engine/branch_primitive.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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
}
Expand Down
16 changes: 12 additions & 4 deletions go/vt/vtgate/plugin_mysql_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand All @@ -58,6 +58,7 @@ import (
)

var (
mysqlServerPort = -1
mysqlServerBindAddress string
mysqlServerSocketPath string
mysqlTCPVersion = "tcp"
Expand Down Expand Up @@ -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.")
Expand All @@ -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.
Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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,
Expand Down
3 changes: 1 addition & 2 deletions go/vt/vtgate/vtgate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import (
"fmt"
"strings"
"testing"
"vitess.io/vitess/go/internal/global"

"github.com/stretchr/testify/assert"
"google.golang.org/protobuf/proto"
Expand Down Expand Up @@ -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()
}
Expand Down

0 comments on commit 328385b

Please sign in to comment.