Skip to content

Commit

Permalink
vtenv: Introduce vtenv for passing in collation & parser information
Browse files Browse the repository at this point in the history
We have many places taking already 3 arguments for the collation env,
parser & MySQL version. This is due to the refactors to pass in explicit
state.

This introduces `vtenv` to refactor things a bit and slightly clean it
up. We now have one thing that can carry all three dependencies and also
potential future additional ones where we need it.

Signed-off-by: Dirkjan Bussink <[email protected]>
  • Loading branch information
dbussink committed Jan 21, 2024
1 parent f03155f commit 3bec3b9
Show file tree
Hide file tree
Showing 238 changed files with 1,638 additions and 1,798 deletions.
13 changes: 5 additions & 8 deletions go/cmd/vtadmin/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,18 @@ import (
"github.com/spf13/cobra"

_flag "vitess.io/vitess/go/internal/flag"
"vitess.io/vitess/go/mysql/collations"
"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/vtadmin"
"vitess.io/vitess/go/vt/vtadmin/cache"
"vitess.io/vitess/go/vt/vtadmin/cluster"
"vitess.io/vitess/go/vt/vtadmin/grpcserver"
vtadminhttp "vitess.io/vitess/go/vt/vtadmin/http"
"vitess.io/vitess/go/vt/vtadmin/http/debug"
"vitess.io/vitess/go/vt/vtadmin/rbac"
"vitess.io/vitess/go/vt/vtenv"
)

var (
Expand Down Expand Up @@ -139,23 +138,21 @@ func run(cmd *cobra.Command, args []string) {
log.Warningf("no cache-refresh-key set; forcing cache refreshes will not be possible")
}
cache.SetCacheRefreshKey(cacheRefreshKey)
mysqlServerVersion := servenv.MySQLServerVersion()
collationEnv := collations.NewEnvironment(mysqlServerVersion)

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

if err := s.ListenAndServe(); err != nil {
Expand Down
44 changes: 20 additions & 24 deletions go/cmd/vtcombo/cli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,20 +31,19 @@ import (
"github.com/spf13/cobra"

"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"
"vitess.io/vitess/go/vt/topotools"
"vitess.io/vitess/go/vt/vtcombo"
"vitess.io/vitess/go/vt/vtctld"
"vitess.io/vitess/go/vt/vtenv"
"vitess.io/vitess/go/vt/vtgate"
"vitess.io/vitess/go/vt/vtgate/planbuilder/plancontext"
"vitess.io/vitess/go/vt/vttablet/tabletserver/tabletenv"
Expand Down Expand Up @@ -80,10 +79,9 @@ In particular, it contains:

tpb vttestpb.VTTestTopology
ts *topo.Server
collationEnv *collations.Environment
resilientServer *srvtopo.ResilientServer
parser *sqlparser.Parser
mysqlVersion string

env *vtenv.Environment
)

func init() {
Expand Down Expand Up @@ -120,7 +118,15 @@ func init() {
// user know about this flag.
Main.Flags().MarkHidden("tablet_protocol")

collationEnv = collations.NewEnvironment(servenv.MySQLServerVersion())
var err error
env, err = vtenv.New(vtenv.Options{
MySQLServerVersion: servenv.MySQLServerVersion(),
TruncateUILen: servenv.TruncateUILen,
TruncateErrLen: servenv.TruncateErrLen,
})
if err != nil {
log.Fatalf("unable to initialize env: %v", err)
}
}

func startMysqld(uid uint32) (mysqld *mysqlctl.Mysqld, cnf *mysqlctl.Mycnf, err error) {
Expand All @@ -130,15 +136,15 @@ func startMysqld(uid uint32) (mysqld *mysqlctl.Mysqld, cnf *mysqlctl.Mycnf, err
mycnfFile := mysqlctl.MycnfFile(uid)

if _, statErr := os.Stat(mycnfFile); os.IsNotExist(statErr) {
mysqld, cnf, err = mysqlctl.CreateMysqldAndMycnf(uid, "", mysqlPort, collationEnv)
mysqld, cnf, err = mysqlctl.CreateMysqldAndMycnf(uid, "", mysqlPort, env.CollationEnv())
if err != nil {
return nil, nil, fmt.Errorf("failed to initialize mysql config :%w", err)
}
if err := mysqld.Init(ctx, cnf, ""); err != nil {
return nil, nil, fmt.Errorf("failed to initialize mysql :%w", err)
}
} else {
mysqld, cnf, err = mysqlctl.OpenMysqldAndMycnf(uid, collationEnv)
mysqld, cnf, err = mysqlctl.OpenMysqldAndMycnf(uid, env.CollationEnv())
if err != nil {
return nil, nil, fmt.Errorf("failed to find mysql config: %w", err)
}
Expand Down Expand Up @@ -193,16 +199,6 @@ func run(cmd *cobra.Command, args []string) (err error) {
servenv.Init()
tabletenv.Init()

mysqlVersion = servenv.MySQLServerVersion()
parser, err = sqlparser.New(sqlparser.Options{
MySQLServerVersion: mysqlVersion,
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 All @@ -222,7 +218,7 @@ func run(cmd *cobra.Command, args []string) (err error) {
mysqld.SetReadOnly(false)

} else {
dbconfigs.GlobalDBConfigs.InitWithSocket("", collationEnv)
dbconfigs.GlobalDBConfigs.InitWithSocket("", env.CollationEnv())
mysqld.Mysqld = mysqlctl.NewMysqld(&dbconfigs.GlobalDBConfigs)
servenv.OnClose(mysqld.Close)
}
Expand All @@ -234,7 +230,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, parser, mysqlVersion)
uid, err := vtcombo.InitTabletMap(env, ts, &tpb, mysqld, &dbconfigs.GlobalDBConfigs, schemaDir, startMysql)
if err != nil {
// ensure we start mysql in the event we fail here
if startMysql {
Expand All @@ -259,8 +255,8 @@ func run(cmd *cobra.Command, args []string) (err error) {
}
}

wr := wrangler.New(logutil.NewConsoleLogger(), ts, nil, collationEnv, parser, mysqlVersion)
newUID, err := vtcombo.CreateKs(ctx, ts, &tpb, mysqld, &dbconfigs.GlobalDBConfigs, schemaDir, ks, true, uid, wr, collationEnv, parser, mysqlVersion)
wr := wrangler.New(env, logutil.NewConsoleLogger(), ts, nil)
newUID, err := vtcombo.CreateKs(ctx, env, ts, &tpb, mysqld, &dbconfigs.GlobalDBConfigs, schemaDir, ks, true, uid, wr)
if err != nil {
return err
}
Expand Down Expand Up @@ -310,10 +306,10 @@ func run(cmd *cobra.Command, args []string) (err error) {
vtgate.QueryzHandler = "/debug/vtgate/queryz"

// pass nil for healthcheck, it will get created
vtg := vtgate.Init(context.Background(), nil, resilientServer, tpb.Cells[0], tabletTypesToWait, plannerVersion, collationEnv, mysqlVersion)
vtg := vtgate.Init(context.Background(), env, nil, resilientServer, tpb.Cells[0], tabletTypesToWait, plannerVersion)

// vtctld configuration and init
err = vtctld.InitVtctld(ts, collationEnv, parser, mysqlVersion)
err = vtctld.InitVtctld(env, ts)
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, collationEnv, parser, mysqlVersion)
grpcvtctldserver.StartServer(servenv.GRPCServer, env, ts)
}
})
}
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, parser, mysqlVersion)
grpcvtctlserver.StartServer(servenv.GRPCServer, env, ts)
}
})
}
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, parser)
_, err = vindexes.BuildKeyspace(keyspace, env.Parser())
if err != nil {
log.Fatalf("Invalid keyspace definition: %v", err)
}
Expand Down
14 changes: 5 additions & 9 deletions go/cmd/vtctl/vtctl.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,15 @@ import (
"vitess.io/vitess/go/cmd"
"vitess.io/vitess/go/cmd/vtctldclient/command"
"vitess.io/vitess/go/exit"
"vitess.io/vitess/go/mysql/collations"
"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"
"vitess.io/vitess/go/vt/vtctl/grpcvtctldserver"
"vitess.io/vitess/go/vt/vtctl/localvtctldclient"
"vitess.io/vitess/go/vt/vtenv"
"vitess.io/vitess/go/vt/vttablet/tmclient"
"vitess.io/vitess/go/vt/wrangler"
)
Expand Down Expand Up @@ -131,11 +130,8 @@ func main() {
ctx, cancel := context.WithTimeout(context.Background(), waitTime)
installSignalHandlers(cancel)

mysqlVersion := servenv.MySQLServerVersion()
collationEnv := collations.NewEnvironment(mysqlVersion)

parser, err := sqlparser.New(sqlparser.Options{
MySQLServerVersion: mysqlVersion,
env, err := vtenv.New(vtenv.Options{
MySQLServerVersion: servenv.MySQLServerVersion(),
TruncateUILen: servenv.TruncateUILen,
TruncateErrLen: servenv.TruncateErrLen,
})
Expand Down Expand Up @@ -167,7 +163,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, collationEnv, parser, mysqlVersion)
vtctld := grpcvtctldserver.NewVtctldServer(env, ts)
localvtctldclient.SetServer(vtctld)
command.VtctldClientProtocol = "local"

Expand All @@ -183,7 +179,7 @@ func main() {
fallthrough
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.")
wr := wrangler.New(logutil.NewConsoleLogger(), ts, tmclient.NewTabletManagerClient(), collationEnv, parser, mysqlVersion)
wr := wrangler.New(env, logutil.NewConsoleLogger(), ts, tmclient.NewTabletManagerClient())

if args[0] == "--" {
vtctl.PrintDoubleDashDeprecationNotice(wr)
Expand Down
30 changes: 7 additions & 23 deletions go/cmd/vtctld/cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,16 @@ import (
"github.com/spf13/cobra"

"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"
"vitess.io/vitess/go/vt/vtenv"
)

var (
ts *topo.Server
collationEnv *collations.Environment
parser *sqlparser.Parser
mysqlVersion string
Main = &cobra.Command{
ts *topo.Server
env *vtenv.Environment
Main = &cobra.Command{
Use: "vtctld",
Short: "The Vitess cluster management daemon.",
Long: `vtctld provides web and gRPC interfaces to manage a single Vitess cluster.
Expand Down Expand Up @@ -66,18 +62,16 @@ func run(cmd *cobra.Command, args []string) error {
defer ts.Close()

var err error
mysqlVersion = servenv.MySQLServerVersion()
collationEnv = collations.NewEnvironment(mysqlVersion)
parser, err = sqlparser.New(sqlparser.Options{
MySQLServerVersion: mysqlVersion,
env, err = vtenv.New(vtenv.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, parser, mysqlVersion); err != nil {
if err := vtctld.InitVtctld(env, ts); err != nil {
return err
}

Expand All @@ -103,14 +97,4 @@ 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, collationEnv, parser, mysqlVersion)
grpcvtctldserver.StartServer(servenv.GRPCServer, env, ts)
}
})
}
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, parser, mysqlVersion)
grpcvtctlserver.StartServer(servenv.GRPCServer, env, ts)
}
})
}
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, parser, mysqlVersion)
wr := wrangler.New(env, logutil.NewConsoleLogger(), ts, tmclient.NewTabletManagerClient())
_, err = schemamanager.Run(
ctx,
controller,
schemamanager.NewTabletExecutor("vtctld/schema", wr.TopoServer(), wr.TabletManagerClient(), wr.Logger(), schemaChangeReplicasTimeout, 0, parser),
schemamanager.NewTabletExecutor("vtctld/schema", wr.TopoServer(), wr.TabletManagerClient(), wr.Logger(), schemaChangeReplicasTimeout, 0, env.Parser()),
)
if err != nil {
log.Errorf("Schema change failed, error: %v", err)
Expand Down
Loading

0 comments on commit 3bec3b9

Please sign in to comment.