Skip to content

Commit

Permalink
sqlparser: Refactor out servenv and inject everywhere (#14822)
Browse files Browse the repository at this point in the history
Signed-off-by: Dirkjan Bussink <[email protected]>
  • Loading branch information
dbussink authored Dec 19, 2023
1 parent 91bb0b9 commit 071454f
Show file tree
Hide file tree
Showing 329 changed files with 3,654 additions and 3,109 deletions.
16 changes: 13 additions & 3 deletions go/cmd/topo2topo/cli/topo2topo.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"vitess.io/vitess/go/vt/grpccommon"
"vitess.io/vitess/go/vt/logutil"
"vitess.io/vitess/go/vt/servenv"
"vitess.io/vitess/go/vt/sqlparser"
"vitess.io/vitess/go/vt/topo"
"vitess.io/vitess/go/vt/topo/helpers"
)
Expand Down Expand Up @@ -94,12 +95,21 @@ func run(cmd *cobra.Command, args []string) error {
return compareTopos(ctx, fromTS, toTS)
}

return copyTopos(ctx, fromTS, toTS)
parser, err := sqlparser.New(sqlparser.Options{
MySQLServerVersion: servenv.MySQLServerVersion(),
TruncateUILen: servenv.TruncateUILen,
TruncateErrLen: servenv.TruncateErrLen,
})
if err != nil {
return fmt.Errorf("cannot create sqlparser: %w", err)
}

return copyTopos(ctx, fromTS, toTS, parser)
}

func copyTopos(ctx context.Context, fromTS, toTS *topo.Server) error {
func copyTopos(ctx context.Context, fromTS, toTS *topo.Server, parser *sqlparser.Parser) error {
if doKeyspaces {
if err := helpers.CopyKeyspaces(ctx, fromTS, toTS); err != nil {
if err := helpers.CopyKeyspaces(ctx, fromTS, toTS, parser); err != nil {
return err
}
}
Expand Down
11 changes: 10 additions & 1 deletion go/cmd/vtadmin/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (
"vitess.io/vitess/go/vt/log"
"vitess.io/vitess/go/vt/logutil"
"vitess.io/vitess/go/vt/servenv"
"vitess.io/vitess/go/vt/sqlparser"
"vitess.io/vitess/go/vt/vtadmin"
"vitess.io/vitess/go/vt/vtadmin/cache"
"vitess.io/vitess/go/vt/vtadmin/cluster"
Expand Down Expand Up @@ -140,12 +141,20 @@ func run(cmd *cobra.Command, args []string) {
cache.SetCacheRefreshKey(cacheRefreshKey)
collationEnv := collations.NewEnvironment(servenv.MySQLServerVersion())

parser, err := sqlparser.New(sqlparser.Options{
MySQLServerVersion: servenv.MySQLServerVersion(),
TruncateUILen: servenv.TruncateUILen,
TruncateErrLen: servenv.TruncateErrLen,
})
if err != nil {
fatal(err)
}
s := vtadmin.NewAPI(clusters, vtadmin.Options{
GRPCOpts: opts,
HTTPOpts: httpOpts,
RBAC: rbacConfig,
EnableDynamicClusters: enableDynamicClusters,
}, collationEnv)
}, collationEnv, parser)
bootSpan.Finish()

if err := s.ListenAndServe(); err != nil {
Expand Down
22 changes: 16 additions & 6 deletions go/cmd/vtcombo/cli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,15 @@ import (

"github.com/spf13/cobra"

"vitess.io/vitess/go/mysql/collations"

"vitess.io/vitess/go/acl"
"vitess.io/vitess/go/mysql/collations"
"vitess.io/vitess/go/mysql/replication"
"vitess.io/vitess/go/vt/dbconfigs"
"vitess.io/vitess/go/vt/log"
"vitess.io/vitess/go/vt/logutil"
"vitess.io/vitess/go/vt/mysqlctl"
"vitess.io/vitess/go/vt/servenv"
"vitess.io/vitess/go/vt/sqlparser"
"vitess.io/vitess/go/vt/srvtopo"
"vitess.io/vitess/go/vt/topo"
"vitess.io/vitess/go/vt/topo/memorytopo"
Expand Down Expand Up @@ -82,6 +82,7 @@ In particular, it contains:
ts *topo.Server
collationEnv *collations.Environment
resilientServer *srvtopo.ResilientServer
parser *sqlparser.Parser
)

func init() {
Expand Down Expand Up @@ -191,6 +192,15 @@ func run(cmd *cobra.Command, args []string) (err error) {
servenv.Init()
tabletenv.Init()

parser, err = sqlparser.New(sqlparser.Options{
MySQLServerVersion: servenv.MySQLServerVersion(),
TruncateUILen: servenv.TruncateUILen,
TruncateErrLen: servenv.TruncateErrLen,
})
if err != nil {
return fmt.Errorf("failed to initialize sql parser: %w", err)
}

var (
mysqld = &vtcomboMysqld{}
cnf *mysqlctl.Mycnf
Expand Down Expand Up @@ -222,7 +232,7 @@ func run(cmd *cobra.Command, args []string) (err error) {
// to be the "internal" protocol that InitTabletMap registers.
cmd.Flags().Set("tablet_manager_protocol", "internal")
cmd.Flags().Set("tablet_protocol", "internal")
uid, err := vtcombo.InitTabletMap(ts, &tpb, mysqld, &dbconfigs.GlobalDBConfigs, schemaDir, startMysql, collationEnv)
uid, err := vtcombo.InitTabletMap(ts, &tpb, mysqld, &dbconfigs.GlobalDBConfigs, schemaDir, startMysql, collationEnv, parser)
if err != nil {
// ensure we start mysql in the event we fail here
if startMysql {
Expand All @@ -247,8 +257,8 @@ func run(cmd *cobra.Command, args []string) (err error) {
}
}

wr := wrangler.New(logutil.NewConsoleLogger(), ts, nil, collationEnv)
newUID, err := vtcombo.CreateKs(ctx, ts, &tpb, mysqld, &dbconfigs.GlobalDBConfigs, schemaDir, ks, true, uid, wr, collationEnv)
wr := wrangler.New(logutil.NewConsoleLogger(), ts, nil, collationEnv, parser)
newUID, err := vtcombo.CreateKs(ctx, ts, &tpb, mysqld, &dbconfigs.GlobalDBConfigs, schemaDir, ks, true, uid, wr, collationEnv, parser)
if err != nil {
return err
}
Expand Down Expand Up @@ -301,7 +311,7 @@ func run(cmd *cobra.Command, args []string) (err error) {
vtg := vtgate.Init(context.Background(), nil, resilientServer, tpb.Cells[0], tabletTypesToWait, plannerVersion, collationEnv)

// vtctld configuration and init
err = vtctld.InitVtctld(ts, collationEnv)
err = vtctld.InitVtctld(ts, collationEnv, parser)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion go/cmd/vtcombo/cli/plugin_grpcvtctldserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import (
func init() {
servenv.OnRun(func() {
if servenv.GRPCCheckServiceMap("vtctld") {
grpcvtctldserver.StartServer(servenv.GRPCServer, ts)
grpcvtctldserver.StartServer(servenv.GRPCServer, ts, parser)
}
})
}
2 changes: 1 addition & 1 deletion go/cmd/vtcombo/cli/plugin_grpcvtctlserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import (
func init() {
servenv.OnRun(func() {
if servenv.GRPCCheckServiceMap("vtctl") {
grpcvtctlserver.StartServer(servenv.GRPCServer, ts, collationEnv)
grpcvtctlserver.StartServer(servenv.GRPCServer, ts, collationEnv, parser)
}
})
}
2 changes: 1 addition & 1 deletion go/cmd/vtcombo/cli/vschema_watcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func loadKeyspacesFromDir(dir string, keyspaces []*vttestpb.Keyspace, ts *topo.S
log.Fatalf("Unable to parse keyspace file %v: %v", ksFile, err)
}

_, err = vindexes.BuildKeyspace(keyspace)
_, err = vindexes.BuildKeyspace(keyspace, parser)
if err != nil {
log.Fatalf("Invalid keyspace definition: %v", err)
}
Expand Down
16 changes: 13 additions & 3 deletions go/cmd/vtctl/vtctl.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import (
"vitess.io/vitess/go/vt/log"
"vitess.io/vitess/go/vt/logutil"
"vitess.io/vitess/go/vt/servenv"
"vitess.io/vitess/go/vt/sqlparser"
"vitess.io/vitess/go/vt/topo"
"vitess.io/vitess/go/vt/vtctl"
"vitess.io/vitess/go/vt/vtctl/grpcvtctldserver"
Expand Down Expand Up @@ -130,6 +131,15 @@ func main() {
ctx, cancel := context.WithTimeout(context.Background(), waitTime)
installSignalHandlers(cancel)

parser, err := sqlparser.New(sqlparser.Options{
MySQLServerVersion: servenv.MySQLServerVersion(),
TruncateUILen: servenv.TruncateUILen,
TruncateErrLen: servenv.TruncateErrLen,
})
if err != nil {
log.Fatalf("cannot initialize sql parser: %v", err)
}

// (TODO:ajm188) <Begin backwards compatibility support>.
//
// For v12, we are going to support new commands by prefixing as:
Expand All @@ -154,7 +164,7 @@ func main() {
// New behavior. Strip off the prefix, and set things up to run through
// the vtctldclient command tree, using the localvtctldclient (in-process)
// client.
vtctld := grpcvtctldserver.NewVtctldServer(ts)
vtctld := grpcvtctldserver.NewVtctldServer(ts, parser)
localvtctldclient.SetServer(vtctld)
command.VtctldClientProtocol = "local"

Expand All @@ -171,15 +181,15 @@ func main() {
default:
log.Warningf("WARNING: vtctl should only be used for VDiff v1 workflows. Please use VDiff v2 and consider using vtctldclient for all other commands.")
collationEnv := collations.NewEnvironment(servenv.MySQLServerVersion())
wr := wrangler.New(logutil.NewConsoleLogger(), ts, tmclient.NewTabletManagerClient(), collationEnv)
wr := wrangler.New(logutil.NewConsoleLogger(), ts, tmclient.NewTabletManagerClient(), collationEnv, parser)

if args[0] == "--" {
vtctl.PrintDoubleDashDeprecationNotice(wr)
args = args[1:]
}

action = args[0]
err := vtctl.RunCommand(ctx, wr, args)
err = vtctl.RunCommand(ctx, wr, args)
cancel()
switch err {
case vtctl.ErrUnknownCommand:
Expand Down
24 changes: 23 additions & 1 deletion go/cmd/vtctld/cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,17 @@ import (

"vitess.io/vitess/go/acl"
"vitess.io/vitess/go/mysql/collations"
"vitess.io/vitess/go/vt/log"
"vitess.io/vitess/go/vt/servenv"
"vitess.io/vitess/go/vt/sqlparser"
"vitess.io/vitess/go/vt/topo"
"vitess.io/vitess/go/vt/vtctld"
)

var (
ts *topo.Server
collationEnv *collations.Environment
parser *sqlparser.Parser
Main = &cobra.Command{
Use: "vtctld",
Short: "The Vitess cluster management daemon.",
Expand Down Expand Up @@ -61,9 +64,18 @@ func run(cmd *cobra.Command, args []string) error {
ts = topo.Open()
defer ts.Close()

var err error
collationEnv = collations.NewEnvironment(servenv.MySQLServerVersion())
parser, err = sqlparser.New(sqlparser.Options{
MySQLServerVersion: servenv.MySQLServerVersion(),
TruncateUILen: servenv.TruncateUILen,
TruncateErrLen: servenv.TruncateErrLen,
})
if err != nil {
return err
}
// Init the vtctld core
if err := vtctld.InitVtctld(ts, collationEnv); err != nil {
if err := vtctld.InitVtctld(ts, collationEnv, parser); err != nil {
return err
}

Expand All @@ -89,4 +101,14 @@ func init() {
servenv.MoveFlagsToCobraCommand(Main)

acl.RegisterFlags(Main.Flags())

var err error
parser, err = sqlparser.New(sqlparser.Options{
MySQLServerVersion: servenv.MySQLServerVersion(),
TruncateUILen: servenv.TruncateUILen,
TruncateErrLen: servenv.TruncateErrLen,
})
if err != nil {
log.Fatalf("cannot initialize sql parser: %v", err)
}
}
2 changes: 1 addition & 1 deletion go/cmd/vtctld/cli/plugin_grpcvtctldserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import (
func init() {
servenv.OnRun(func() {
if servenv.GRPCCheckServiceMap("vtctld") {
grpcvtctldserver.StartServer(servenv.GRPCServer, ts)
grpcvtctldserver.StartServer(servenv.GRPCServer, ts, parser)
}
})
}
2 changes: 1 addition & 1 deletion go/cmd/vtctld/cli/plugin_grpcvtctlserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import (
func init() {
servenv.OnRun(func() {
if servenv.GRPCCheckServiceMap("vtctl") {
grpcvtctlserver.StartServer(servenv.GRPCServer, ts, collationEnv)
grpcvtctlserver.StartServer(servenv.GRPCServer, ts, collationEnv, parser)
}
})
}
4 changes: 2 additions & 2 deletions go/cmd/vtctld/cli/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,11 @@ func initSchema() {
return
}
ctx := context.Background()
wr := wrangler.New(logutil.NewConsoleLogger(), ts, tmclient.NewTabletManagerClient(), collationEnv)
wr := wrangler.New(logutil.NewConsoleLogger(), ts, tmclient.NewTabletManagerClient(), collationEnv, parser)
_, err = schemamanager.Run(
ctx,
controller,
schemamanager.NewTabletExecutor("vtctld/schema", wr.TopoServer(), wr.TabletManagerClient(), wr.Logger(), schemaChangeReplicasTimeout, 0),
schemamanager.NewTabletExecutor("vtctld/schema", wr.TopoServer(), wr.TabletManagerClient(), wr.Logger(), schemaChangeReplicasTimeout, 0, parser),
)
if err != nil {
log.Errorf("Schema change failed, error: %v", err)
Expand Down
16 changes: 15 additions & 1 deletion go/cmd/vtctldclient/command/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,10 @@ import (
"github.com/spf13/cobra"

"vitess.io/vitess/go/trace"
"vitess.io/vitess/go/vt/log"
"vitess.io/vitess/go/vt/logutil"
"vitess.io/vitess/go/vt/servenv"
"vitess.io/vitess/go/vt/sqlparser"
"vitess.io/vitess/go/vt/topo"
"vitess.io/vitess/go/vt/vtctl/grpcvtctldserver"
"vitess.io/vitess/go/vt/vtctl/localvtctldclient"
Expand Down Expand Up @@ -80,6 +82,8 @@ var (
actionTimeout time.Duration
compactOutput bool

parser *sqlparser.Parser

topoOptions = struct {
implementation string
globalServerAddresses []string
Expand Down Expand Up @@ -208,7 +212,7 @@ func getClientForCommand(cmd *cobra.Command) (vtctldclient.VtctldClient, error)
return nil
})
})
vtctld := grpcvtctldserver.NewVtctldServer(ts)
vtctld := grpcvtctldserver.NewVtctldServer(ts, parser)
localvtctldclient.SetServer(vtctld)
VtctldClientProtocol = "local"
server = ""
Expand All @@ -225,4 +229,14 @@ func init() {
Root.PersistentFlags().StringSliceVar(&topoOptions.globalServerAddresses, "topo-global-server-address", topoOptions.globalServerAddresses, "the address of the global topology server(s)")
Root.PersistentFlags().StringVar(&topoOptions.globalRoot, "topo-global-root", topoOptions.globalRoot, "the path of the global topology data in the global topology server")
vreplcommon.RegisterCommands(Root)

var err error
parser, err = sqlparser.New(sqlparser.Options{
MySQLServerVersion: servenv.MySQLServerVersion(),
TruncateUILen: servenv.TruncateUILen,
TruncateErrLen: servenv.TruncateErrLen,
})
if err != nil {
log.Fatalf("failed to initialize sqlparser: %v", err)
}
}
3 changes: 1 addition & 2 deletions go/cmd/vtctldclient/command/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ import (
"vitess.io/vitess/go/protoutil"
"vitess.io/vitess/go/vt/logutil"
"vitess.io/vitess/go/vt/schema"
"vitess.io/vitess/go/vt/sqlparser"
"vitess.io/vitess/go/vt/topo/topoproto"
"vitess.io/vitess/go/vt/vtctl/grpcvtctldserver"

Expand Down Expand Up @@ -123,7 +122,7 @@ func commandApplySchema(cmd *cobra.Command, args []string) error {
allSQL = strings.Join(applySchemaOptions.SQL, ";")
}

parts, err := sqlparser.SplitStatementToPieces(allSQL)
parts, err := parser.SplitStatementToPieces(allSQL)
if err != nil {
return err
}
Expand Down
3 changes: 3 additions & 0 deletions go/cmd/vtctldclient/command/vreplication/common/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@ var (
DeferSecondaryKeys bool
AutoStart bool
StopAfterCopy bool
MySQLServerVersion string
TruncateUILen int
TruncateErrLen int
}{}
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (

"vitess.io/vitess/go/cmd/vtctldclient/command"
"vitess.io/vitess/go/cmd/vtctldclient/command/vreplication/common"
"vitess.io/vitess/go/vt/sqlparser"
"vitess.io/vitess/go/vt/topo"
"vitess.io/vitess/go/vt/topo/memorytopo"
"vitess.io/vitess/go/vt/vtctl/grpcvtctldserver"
Expand Down Expand Up @@ -144,7 +145,7 @@ func SetupLocalVtctldClient(t *testing.T, ctx context.Context, cells ...string)
tmclient.RegisterTabletManagerClientFactory("grpc", func() tmclient.TabletManagerClient {
return nil
})
vtctld := grpcvtctldserver.NewVtctldServer(ts)
vtctld := grpcvtctldserver.NewVtctldServer(ts, sqlparser.NewTestParser())
localvtctldclient.SetServer(vtctld)
command.VtctldClientProtocol = "local"
client, err := vtctldclient.New(command.VtctldClientProtocol, "")
Expand Down
Loading

0 comments on commit 071454f

Please sign in to comment.