From 6c4c5faa70290226a1a3c92436ba0ad07dd3844d Mon Sep 17 00:00:00 2001 From: Dirkjan Bussink Date: Sun, 21 Jan 2024 14:08:40 +0100 Subject: [PATCH] vtenv: Introduce vtenv for passing in collation & parser information (#14994) Signed-off-by: Dirkjan Bussink --- go/cmd/vtadmin/main.go | 13 +- go/cmd/vtcombo/cli/main.go | 44 ++--- go/cmd/vtcombo/cli/plugin_grpcvtctldserver.go | 2 +- go/cmd/vtcombo/cli/plugin_grpcvtctlserver.go | 2 +- go/cmd/vtcombo/cli/vschema_watcher.go | 2 +- go/cmd/vtctl/vtctl.go | 14 +- go/cmd/vtctld/cli/cli.go | 30 +-- go/cmd/vtctld/cli/plugin_grpcvtctldserver.go | 2 +- go/cmd/vtctld/cli/plugin_grpcvtctlserver.go | 2 +- go/cmd/vtctld/cli/schema.go | 4 +- go/cmd/vtctldclient/command/root.go | 17 +- go/cmd/vtctldclient/command/schema.go | 2 +- .../command/vreplication/common/utils_test.go | 6 +- .../vreplication/vdiff/vdiff_env_test.go | 6 +- go/cmd/vtexplain/cli/vtexplain.go | 11 +- go/cmd/vtgate/cli/cli.go | 16 +- go/cmd/vttablet/cli/cli.go | 35 ++-- .../vrepl/schemadiff_vrepl_suite_test.go | 10 +- .../vtgate/queries/random/simplifier_test.go | 2 + go/test/fuzzing/tabletserver_schema_fuzzer.go | 5 +- go/test/fuzzing/vtctl_fuzzer.go | 6 +- go/test/vschemawrapper/vschema_wrapper.go | 23 +-- go/vt/schemadiff/column.go | 16 +- go/vt/schemadiff/diff.go | 8 +- go/vt/schemadiff/diff_test.go | 25 +-- go/vt/schemadiff/env.go | 23 +-- go/vt/schemadiff/schema.go | 4 +- go/vt/schemadiff/semantics.go | 10 +- go/vt/schemadiff/table.go | 20 +- go/vt/schemadiff/table_test.go | 18 +- go/vt/schemadiff/view_test.go | 10 +- go/vt/sidecardb/sidecardb.go | 29 ++- go/vt/sidecardb/sidecardb_test.go | 39 ++-- go/vt/sqlparser/cached_size.go | 12 ++ go/vt/sqlparser/truncate_query.go | 6 +- go/vt/sqlparser/truncate_query_test.go | 15 ++ go/vt/throttler/demo/throttler_demo.go | 16 +- go/vt/vtadmin/api.go | 34 ++-- go/vt/vtadmin/api_authz_test.go | 106 +++++------ go/vt/vtadmin/api_test.go | 73 ++++--- .../vtadmin/testutil/authztestgen/template.go | 8 +- go/vt/vtadmin/testutil/cluster.go | 7 +- go/vt/vtcombo/tablet_map.go | 31 ++- go/vt/vtctl/endtoend/get_schema_test.go | 8 +- go/vt/vtctl/endtoend/onlineddl_show_test.go | 8 +- go/vt/vtctl/grpcvtctlclient/client_test.go | 8 +- go/vt/vtctl/grpcvtctldclient/client_test.go | 10 +- .../endtoend/init_shard_primary_test.go | 12 +- go/vt/vtctl/grpcvtctldserver/server.go | 13 +- .../grpcvtctldserver/server_slow_test.go | 11 +- go/vt/vtctl/grpcvtctldserver/server_test.go | 180 +++++++++--------- go/vt/vtctl/grpcvtctlserver/server.go | 20 +- go/vt/vtctl/workflow/materializer.go | 20 +- go/vt/vtctl/workflow/materializer_env_test.go | 10 +- go/vt/vtctl/workflow/materializer_test.go | 6 +- go/vt/vtctl/workflow/server.go | 58 ++---- go/vt/vtctl/workflow/server_test.go | 8 +- go/vt/vtctld/action_repository.go | 20 +- go/vt/vtctld/api.go | 6 +- go/vt/vtctld/api_test.go | 8 +- go/vt/vtctld/tablet_data_test.go | 8 +- go/vt/vtctld/vtctld.go | 8 +- go/vt/vtenv/cached_size.go | 37 ++++ go/vt/vtenv/vtenv.go | 93 +++++++++ go/vt/vtenv/vtenv_test.go | 49 +++++ go/vt/vtexplain/vtexplain.go | 20 +- go/vt/vtexplain/vtexplain_test.go | 9 +- go/vt/vtexplain/vtexplain_vtgate.go | 6 +- go/vt/vtexplain/vtexplain_vttablet.go | 11 +- go/vt/vtexplain/vtexplain_vttablet_test.go | 21 +- go/vt/vtgate/engine/distinct.go | 4 +- go/vt/vtgate/engine/fake_vcursor_test.go | 14 +- go/vt/vtgate/engine/filter_test.go | 8 +- go/vt/vtgate/engine/online_ddl.go | 4 +- go/vt/vtgate/engine/primitive.go | 5 +- go/vt/vtgate/engine/projection_test.go | 27 ++- go/vt/vtgate/engine/revert_migration.go | 2 +- go/vt/vtgate/engine/vexplain.go | 2 +- go/vt/vtgate/evalengine/api_compare_test.go | 10 +- go/vt/vtgate/evalengine/cached_size.go | 4 +- go/vt/vtgate/evalengine/compiler.go | 6 +- go/vt/vtgate/evalengine/compiler_test.go | 39 ++-- go/vt/vtgate/evalengine/expr_collate.go | 2 +- go/vt/vtgate/evalengine/expr_compare.go | 18 +- go/vt/vtgate/evalengine/expr_env.go | 33 ++-- go/vt/vtgate/evalengine/expr_logical.go | 4 +- go/vt/vtgate/evalengine/fn_compare.go | 4 +- go/vt/vtgate/evalengine/fn_regexp.go | 10 +- go/vt/vtgate/evalengine/fn_string.go | 8 +- .../evalengine/integration/comparison_test.go | 18 +- .../evalengine/integration/fuzz_test.go | 8 +- go/vt/vtgate/evalengine/mysql_test.go | 7 +- go/vt/vtgate/evalengine/perf_test.go | 10 +- go/vt/vtgate/evalengine/translate.go | 36 ++-- go/vt/vtgate/evalengine/translate_convert.go | 8 +- go/vt/vtgate/evalengine/translate_test.go | 39 ++-- go/vt/vtgate/executor.go | 45 ++--- go/vt/vtgate/executor_framework_test.go | 15 +- go/vt/vtgate/executor_select_test.go | 7 +- go/vt/vtgate/executor_stream_test.go | 6 +- go/vt/vtgate/plan_execute.go | 2 +- go/vt/vtgate/planbuilder/builder.go | 2 +- go/vt/vtgate/planbuilder/collations_test.go | 2 + .../planbuilder/expression_converter.go | 9 +- .../planbuilder/expression_converter_test.go | 9 +- .../planbuilder/operator_transformers.go | 24 +-- .../vtgate/planbuilder/operators/distinct.go | 2 +- .../planbuilder/operators/dml_planning.go | 7 +- go/vt/vtgate/planbuilder/operators/filter.go | 7 +- .../vtgate/planbuilder/operators/hash_join.go | 8 +- .../planbuilder/operators/hash_join_test.go | 2 + .../operators/info_schema_planning.go | 11 +- go/vt/vtgate/planbuilder/operators/insert.go | 14 +- .../planbuilder/operators/projection.go | 7 +- go/vt/vtgate/planbuilder/operators/route.go | 12 +- .../planbuilder/operators/sharded_routing.go | 7 +- go/vt/vtgate/planbuilder/plan_test.go | 51 ++++- .../vtgate/planbuilder/plancontext/vschema.go | 7 +- .../planbuilder/predicate_rewrite_test.go | 11 +- go/vt/vtgate/planbuilder/route.go | 2 +- go/vt/vtgate/planbuilder/select.go | 12 +- go/vt/vtgate/planbuilder/set.go | 7 +- go/vt/vtgate/planbuilder/show.go | 4 +- go/vt/vtgate/planbuilder/show_test.go | 2 + go/vt/vtgate/planbuilder/simplifier_test.go | 4 + go/vt/vtgate/planbuilder/system_variables.go | 27 ++- go/vt/vtgate/planbuilder/vindex_op.go | 7 +- go/vt/vtgate/plugin_mysql_server.go | 2 +- go/vt/vtgate/querylog.go | 2 +- go/vt/vtgate/queryz.go | 2 +- go/vt/vtgate/semantics/FakeSI.go | 10 +- go/vt/vtgate/semantics/analyzer.go | 5 +- go/vt/vtgate/semantics/early_rewriter.go | 39 ++-- go/vt/vtgate/semantics/info_schema.go | 12 +- go/vt/vtgate/semantics/semantic_state.go | 4 +- go/vt/vtgate/semantics/table_collector.go | 2 +- go/vt/vtgate/simplifier/simplifier_test.go | 10 +- go/vt/vtgate/vcursor_impl.go | 28 +-- go/vt/vtgate/vindexes/consistent_lookup.go | 2 +- .../vtgate/vindexes/consistent_lookup_test.go | 7 +- go/vt/vtgate/vindexes/lookup_test.go | 7 +- go/vt/vtgate/vindexes/vindex.go | 3 +- go/vt/vtgate/vtgate.go | 20 +- go/vt/vttablet/endtoend/framework/server.go | 6 +- go/vt/vttablet/onlineddl/analysis.go | 4 +- go/vt/vttablet/onlineddl/executor.go | 50 ++--- go/vt/vttablet/onlineddl/executor_test.go | 19 +- go/vt/vttablet/onlineddl/vrepl.go | 24 +-- go/vt/vttablet/onlineddl/vrepl/foreign_key.go | 8 +- .../onlineddl/vrepl/foreign_key_test.go | 6 +- go/vt/vttablet/tabletmanager/restore.go | 2 +- go/vt/vttablet/tabletmanager/rpc_query.go | 10 +- .../vttablet/tabletmanager/rpc_query_test.go | 2 + .../tabletmanager/rpc_vreplication.go | 2 +- .../tabletmanager/rpc_vreplication_test.go | 11 +- go/vt/vttablet/tabletmanager/tm_init.go | 16 +- .../tabletmanager/vreplication/engine.go | 36 ++-- .../vreplication/external_connector.go | 25 +-- .../vreplication/replica_connector.go | 8 +- .../tabletmanager/vreplication/vcopier.go | 4 +- .../vreplication/vcopier_atomic.go | 2 +- .../tabletmanager/vreplication/vplayer.go | 2 +- .../tabletmanager/vreplication/vreplicator.go | 4 +- .../vttablet/tabletserver/connpool/dbconn.go | 4 +- .../tabletserver/connpool/dbconn_test.go | 6 +- .../tabletserver/connpool/pool_test.go | 8 +- .../tabletserver/exclude_race_test.go | 6 +- .../tabletserver/health_streamer_test.go | 14 +- .../tabletserver/messager/engine_test.go | 5 +- .../messager/message_manager_test.go | 5 +- .../tabletserver/planbuilder/builder.go | 9 +- .../vttablet/tabletserver/planbuilder/plan.go | 6 +- .../tabletserver/planbuilder/plan_test.go | 11 +- go/vt/vttablet/tabletserver/query_engine.go | 14 +- .../tabletserver/query_engine_test.go | 15 +- go/vt/vttablet/tabletserver/query_executor.go | 12 +- .../tabletserver/query_executor_test.go | 5 +- go/vt/vttablet/tabletserver/queryz.go | 2 +- .../tabletserver/repltracker/reader_test.go | 6 +- .../repltracker/repltracker_test.go | 6 +- .../tabletserver/repltracker/writer_test.go | 6 +- go/vt/vttablet/tabletserver/schema/db_test.go | 17 +- go/vt/vttablet/tabletserver/schema/engine.go | 23 +-- .../tabletserver/schema/engine_test.go | 15 +- .../tabletserver/schema/load_table_test.go | 4 +- go/vt/vttablet/tabletserver/schema/tracker.go | 4 +- .../tabletserver/schema/tracker_test.go | 7 +- .../tabletserver/state_manager_test.go | 5 +- .../tabletserver/stateful_connection_pool.go | 2 +- go/vt/vttablet/tabletserver/tabletenv/env.go | 41 ++-- go/vt/vttablet/tabletserver/tabletserver.go | 54 ++---- .../tabletserver/tabletserver_test.go | 50 ++--- .../tabletserver/throttle/throttler_test.go | 6 +- go/vt/vttablet/tabletserver/tx_engine_test.go | 14 +- go/vt/vttablet/tabletserver/tx_pool.go | 2 +- go/vt/vttablet/tabletserver/tx_pool_test.go | 6 +- .../tabletserver/txlimiter/tx_limiter_test.go | 10 +- .../txserializer/tx_serializer_test.go | 24 ++- .../txthrottler/tx_throttler_test.go | 10 +- .../vttablet/tabletserver/vstreamer/engine.go | 2 +- .../tabletserver/vstreamer/main_flaky_test.go | 6 +- .../tabletserver/vstreamer/planbuilder.go | 37 ++-- .../vstreamer/planbuilder_test.go | 43 ++--- .../tabletserver/vstreamer/resultstreamer.go | 2 +- .../tabletserver/vstreamer/rowstreamer.go | 4 +- .../tabletserver/vstreamer/testenv/testenv.go | 6 +- .../tabletserver/vstreamer/vstreamer.go | 14 +- go/vt/vttest/local_cluster.go | 5 +- go/vt/wrangler/external_cluster_test.go | 6 +- go/vt/wrangler/fake_tablet_test.go | 6 +- go/vt/wrangler/materializer.go | 24 +-- go/vt/wrangler/materializer_env_test.go | 5 +- go/vt/wrangler/materializer_test.go | 7 +- go/vt/wrangler/reparent.go | 2 +- go/vt/wrangler/resharder_env_test.go | 6 +- go/vt/wrangler/tablet_test.go | 14 +- go/vt/wrangler/testlib/backup_test.go | 12 +- .../testlib/copy_schema_shard_test.go | 6 +- .../testlib/emergency_reparent_shard_test.go | 8 +- .../testlib/external_reparent_test.go | 16 +- go/vt/wrangler/testlib/fake_tablet.go | 8 +- go/vt/wrangler/testlib/find_tablet_test.go | 6 +- go/vt/wrangler/testlib/permissions_test.go | 8 +- .../testlib/planned_reparent_shard_test.go | 22 +-- go/vt/wrangler/testlib/reparent_utils_test.go | 10 +- go/vt/wrangler/testlib/shard_test.go | 6 +- go/vt/wrangler/testlib/version_test.go | 8 +- go/vt/wrangler/testlib/vtctl_pipe.go | 6 +- go/vt/wrangler/traffic_switcher.go | 11 +- go/vt/wrangler/traffic_switcher_env_test.go | 9 +- go/vt/wrangler/vdiff.go | 54 +++--- go/vt/wrangler/vdiff_env_test.go | 6 +- go/vt/wrangler/vdiff_test.go | 62 ++++-- go/vt/wrangler/vexec.go | 4 +- go/vt/wrangler/vexec_plan.go | 4 +- go/vt/wrangler/vexec_test.go | 14 +- go/vt/wrangler/wrangler.go | 37 ++-- go/vt/wrangler/wrangler_env_test.go | 6 +- 238 files changed, 1638 insertions(+), 1798 deletions(-) create mode 100644 go/vt/vtenv/cached_size.go create mode 100644 go/vt/vtenv/vtenv.go create mode 100644 go/vt/vtenv/vtenv_test.go diff --git a/go/cmd/vtadmin/main.go b/go/cmd/vtadmin/main.go index e02fa24620f..2548986c2ba 100644 --- a/go/cmd/vtadmin/main.go +++ b/go/cmd/vtadmin/main.go @@ -25,12 +25,10 @@ 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" @@ -38,6 +36,7 @@ import ( 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 ( @@ -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 { diff --git a/go/cmd/vtcombo/cli/main.go b/go/cmd/vtcombo/cli/main.go index 779a1da2249..72dcd3fa8b9 100644 --- a/go/cmd/vtcombo/cli/main.go +++ b/go/cmd/vtcombo/cli/main.go @@ -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" @@ -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() { @@ -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) { @@ -130,7 +136,7 @@ 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) } @@ -138,7 +144,7 @@ func startMysqld(uid uint32) (mysqld *mysqlctl.Mysqld, cnf *mysqlctl.Mycnf, err 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) } @@ -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 @@ -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) } @@ -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 { @@ -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 } @@ -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 } diff --git a/go/cmd/vtcombo/cli/plugin_grpcvtctldserver.go b/go/cmd/vtcombo/cli/plugin_grpcvtctldserver.go index ec4d8a1f9d1..7f6000f5af9 100644 --- a/go/cmd/vtcombo/cli/plugin_grpcvtctldserver.go +++ b/go/cmd/vtcombo/cli/plugin_grpcvtctldserver.go @@ -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) } }) } diff --git a/go/cmd/vtcombo/cli/plugin_grpcvtctlserver.go b/go/cmd/vtcombo/cli/plugin_grpcvtctlserver.go index 295bacc69f1..236b83e3d28 100644 --- a/go/cmd/vtcombo/cli/plugin_grpcvtctlserver.go +++ b/go/cmd/vtcombo/cli/plugin_grpcvtctlserver.go @@ -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) } }) } diff --git a/go/cmd/vtcombo/cli/vschema_watcher.go b/go/cmd/vtcombo/cli/vschema_watcher.go index e573109ab9e..112a2a0730f 100644 --- a/go/cmd/vtcombo/cli/vschema_watcher.go +++ b/go/cmd/vtcombo/cli/vschema_watcher.go @@ -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) } diff --git a/go/cmd/vtctl/vtctl.go b/go/cmd/vtctl/vtctl.go index fceee3d8ea6..ba84369620a 100644 --- a/go/cmd/vtctl/vtctl.go +++ b/go/cmd/vtctl/vtctl.go @@ -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" ) @@ -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, }) @@ -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" @@ -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) diff --git a/go/cmd/vtctld/cli/cli.go b/go/cmd/vtctld/cli/cli.go index 70c5a22f450..4f8c57b6b2f 100644 --- a/go/cmd/vtctld/cli/cli.go +++ b/go/cmd/vtctld/cli/cli.go @@ -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. @@ -66,10 +62,8 @@ 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, }) @@ -77,7 +71,7 @@ func run(cmd *cobra.Command, args []string) error { 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 } @@ -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) - } } diff --git a/go/cmd/vtctld/cli/plugin_grpcvtctldserver.go b/go/cmd/vtctld/cli/plugin_grpcvtctldserver.go index 83bb4970d2c..b712d9c0fd0 100644 --- a/go/cmd/vtctld/cli/plugin_grpcvtctldserver.go +++ b/go/cmd/vtctld/cli/plugin_grpcvtctldserver.go @@ -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) } }) } diff --git a/go/cmd/vtctld/cli/plugin_grpcvtctlserver.go b/go/cmd/vtctld/cli/plugin_grpcvtctlserver.go index 295bacc69f1..236b83e3d28 100644 --- a/go/cmd/vtctld/cli/plugin_grpcvtctlserver.go +++ b/go/cmd/vtctld/cli/plugin_grpcvtctlserver.go @@ -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) } }) } diff --git a/go/cmd/vtctld/cli/schema.go b/go/cmd/vtctld/cli/schema.go index 2731ae05751..9f1f9d06072 100644 --- a/go/cmd/vtctld/cli/schema.go +++ b/go/cmd/vtctld/cli/schema.go @@ -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) diff --git a/go/cmd/vtctldclient/command/root.go b/go/cmd/vtctldclient/command/root.go index d7f9538900b..0fc39423bc7 100644 --- a/go/cmd/vtctldclient/command/root.go +++ b/go/cmd/vtctldclient/command/root.go @@ -28,16 +28,15 @@ import ( "github.com/spf13/cobra" - "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/grpcvtctldserver" "vitess.io/vitess/go/vt/vtctl/localvtctldclient" "vitess.io/vitess/go/vt/vtctl/vtctldclient" + "vitess.io/vitess/go/vt/vtenv" "vitess.io/vitess/go/vt/vttablet/tmclient" // These imports ensure init()s within them get called and they register their commands/subcommands. @@ -83,9 +82,7 @@ var ( actionTimeout time.Duration compactOutput bool - collationEnv *collations.Environment - parser *sqlparser.Parser - mysqlVersion string + env *vtenv.Environment topoOptions = struct { implementation string @@ -215,7 +212,7 @@ func getClientForCommand(cmd *cobra.Command) (vtctldclient.VtctldClient, error) return nil }) }) - vtctld := grpcvtctldserver.NewVtctldServer(ts, collationEnv, parser, mysqlVersion) + vtctld := grpcvtctldserver.NewVtctldServer(env, ts) localvtctldclient.SetServer(vtctld) VtctldClientProtocol = "local" server = "" @@ -233,15 +230,13 @@ func init() { 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) - mysqlVersion = servenv.MySQLServerVersion() - collationEnv = collations.NewEnvironment(mysqlVersion) var err error - 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 { - log.Fatalf("failed to initialize sqlparser: %v", err) + log.Fatalf("failed to initialize vtenv: %v", err) } } diff --git a/go/cmd/vtctldclient/command/schema.go b/go/cmd/vtctldclient/command/schema.go index 850fa276f0b..db34bd2588f 100644 --- a/go/cmd/vtctldclient/command/schema.go +++ b/go/cmd/vtctldclient/command/schema.go @@ -122,7 +122,7 @@ func commandApplySchema(cmd *cobra.Command, args []string) error { allSQL = strings.Join(applySchemaOptions.SQL, ";") } - parts, err := parser.SplitStatementToPieces(allSQL) + parts, err := env.Parser().SplitStatementToPieces(allSQL) if err != nil { return err } diff --git a/go/cmd/vtctldclient/command/vreplication/common/utils_test.go b/go/cmd/vtctldclient/command/vreplication/common/utils_test.go index b3c240bcc40..a8a0df2e9b2 100644 --- a/go/cmd/vtctldclient/command/vreplication/common/utils_test.go +++ b/go/cmd/vtctldclient/command/vreplication/common/utils_test.go @@ -26,14 +26,12 @@ import ( "vitess.io/vitess/go/cmd/vtctldclient/command" "vitess.io/vitess/go/cmd/vtctldclient/command/vreplication/common" - "vitess.io/vitess/go/mysql/collations" - "vitess.io/vitess/go/mysql/config" - "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" "vitess.io/vitess/go/vt/vtctl/localvtctldclient" "vitess.io/vitess/go/vt/vtctl/vtctldclient" + "vitess.io/vitess/go/vt/vtenv" "vitess.io/vitess/go/vt/vttablet/tmclient" ) @@ -147,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, collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion) + vtctld := grpcvtctldserver.NewVtctldServer(vtenv.NewTestEnv(), ts) localvtctldclient.SetServer(vtctld) command.VtctldClientProtocol = "local" client, err := vtctldclient.New(command.VtctldClientProtocol, "") diff --git a/go/cmd/vtctldclient/command/vreplication/vdiff/vdiff_env_test.go b/go/cmd/vtctldclient/command/vreplication/vdiff/vdiff_env_test.go index 0a1ff5054cd..10d7762acde 100644 --- a/go/cmd/vtctldclient/command/vreplication/vdiff/vdiff_env_test.go +++ b/go/cmd/vtctldclient/command/vreplication/vdiff/vdiff_env_test.go @@ -25,14 +25,12 @@ import ( "sync" "testing" - "vitess.io/vitess/go/mysql/collations" - "vitess.io/vitess/go/mysql/config" "vitess.io/vitess/go/sqltypes" "vitess.io/vitess/go/vt/grpcclient" - "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/workflow" + "vitess.io/vitess/go/vt/vtenv" "vitess.io/vitess/go/vt/vttablet/queryservice" "vitess.io/vitess/go/vt/vttablet/queryservice/fakes" "vitess.io/vitess/go/vt/vttablet/tabletconn" @@ -86,7 +84,7 @@ func newTestVDiffEnv(t testing.TB, ctx context.Context, sourceShards, targetShar tabletType: topodatapb.TabletType_REPLICA, tmc: newTestVDiffTMClient(), } - env.ws = workflow.NewServer(env.topoServ, env.tmc, collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion) + env.ws = workflow.NewServer(vtenv.NewTestEnv(), env.topoServ, env.tmc) env.tmc.testEnv = env // Generate a unique dialer name. diff --git a/go/cmd/vtexplain/cli/vtexplain.go b/go/cmd/vtexplain/cli/vtexplain.go index 8b20046b1b9..aee920f6486 100644 --- a/go/cmd/vtexplain/cli/vtexplain.go +++ b/go/cmd/vtexplain/cli/vtexplain.go @@ -22,10 +22,9 @@ import ( "os" "vitess.io/vitess/go/acl" - "vitess.io/vitess/go/mysql/collations" "vitess.io/vitess/go/vt/logutil" "vitess.io/vitess/go/vt/servenv" - "vitess.io/vitess/go/vt/sqlparser" + "vitess.io/vitess/go/vt/vtenv" "vitess.io/vitess/go/vt/vtexplain" "vitess.io/vitess/go/vt/vtgate/planbuilder/plancontext" @@ -175,17 +174,15 @@ func parseAndRun() error { Target: dbName, } - 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 { return err } - vte, err := vtexplain.Init(context.Background(), vschema, schema, ksShardMap, opts, collationEnv, parser, mysqlServerVersion) + vte, err := vtexplain.Init(context.Background(), env, vschema, schema, ksShardMap, opts) if err != nil { return err } diff --git a/go/cmd/vtgate/cli/cli.go b/go/cmd/vtgate/cli/cli.go index 7cf6baf1437..c348c4d8296 100644 --- a/go/cmd/vtgate/cli/cli.go +++ b/go/cmd/vtgate/cli/cli.go @@ -23,8 +23,6 @@ import ( "github.com/spf13/cobra" - "vitess.io/vitess/go/mysql/collations" - "vitess.io/vitess/go/acl" "vitess.io/vitess/go/exit" "vitess.io/vitess/go/vt/discovery" @@ -32,6 +30,7 @@ import ( "vitess.io/vitess/go/vt/srvtopo" "vitess.io/vitess/go/vt/topo" "vitess.io/vitess/go/vt/topo/topoproto" + "vitess.io/vitess/go/vt/vtenv" "vitess.io/vitess/go/vt/vterrors" "vitess.io/vitess/go/vt/vtgate" "vitess.io/vitess/go/vt/vtgate/planbuilder/plancontext" @@ -158,12 +157,19 @@ func run(cmd *cobra.Command, args []string) error { return fmt.Errorf("cells_to_watch validation failed: %v", err) } - mysqlVersion := servenv.MySQLServerVersion() plannerVersion, _ := plancontext.PlannerNameToVersion(plannerName) - collationEnv := collations.NewEnvironment(mysqlVersion) + + env, err := vtenv.New(vtenv.Options{ + MySQLServerVersion: servenv.MySQLServerVersion(), + TruncateUILen: servenv.TruncateUILen, + TruncateErrLen: servenv.TruncateErrLen, + }) + if err != nil { + return fmt.Errorf("unable to initialize env: %v", err) + } // pass nil for HealthCheck and it will be created - vtg := vtgate.Init(context.Background(), nil, resilientServer, cell, tabletTypes, plannerVersion, collationEnv, mysqlVersion) + vtg := vtgate.Init(context.Background(), env, nil, resilientServer, cell, tabletTypes, plannerVersion) servenv.OnRun(func() { // Flags are parsed now. Parse the template using the actual flag value and overwrite the current template. diff --git a/go/cmd/vttablet/cli/cli.go b/go/cmd/vttablet/cli/cli.go index 9acdfd5f938..872d7a5ef69 100644 --- a/go/cmd/vttablet/cli/cli.go +++ b/go/cmd/vttablet/cli/cli.go @@ -32,11 +32,11 @@ import ( "vitess.io/vitess/go/vt/log" "vitess.io/vitess/go/vt/mysqlctl" "vitess.io/vitess/go/vt/servenv" - "vitess.io/vitess/go/vt/sqlparser" "vitess.io/vitess/go/vt/tableacl" "vitess.io/vitess/go/vt/tableacl/simpleacl" "vitess.io/vitess/go/vt/topo" "vitess.io/vitess/go/vt/topo/topoproto" + "vitess.io/vitess/go/vt/vtenv" "vitess.io/vitess/go/vt/vttablet/onlineddl" "vitess.io/vitess/go/vt/vttablet/tabletmanager" "vitess.io/vitess/go/vt/vttablet/tabletmanager/vdiff" @@ -113,24 +113,23 @@ func run(cmd *cobra.Command, args []string) error { } mysqlVersion := servenv.MySQLServerVersion() - parser, err := sqlparser.New(sqlparser.Options{ + env, err := vtenv.New(vtenv.Options{ MySQLServerVersion: mysqlVersion, TruncateUILen: servenv.TruncateUILen, TruncateErrLen: servenv.TruncateErrLen, }) if err != nil { - return fmt.Errorf("cannot initialize sql parser: %w", err) + return fmt.Errorf("cannot initialize vtenv: %w", err) } - collationEnv := collations.NewEnvironment(mysqlVersion) // config and mycnf initializations are intertwined. - config, mycnf, err := initConfig(tabletAlias, collationEnv) + config, mycnf, err := initConfig(tabletAlias, env.CollationEnv()) if err != nil { return err } ts := topo.Open() - qsc, err := createTabletServer(context.Background(), config, ts, tabletAlias, collationEnv, parser, mysqlVersion) + qsc, err := createTabletServer(context.Background(), env, config, ts, tabletAlias) if err != nil { ts.Close() return err @@ -143,36 +142,26 @@ func run(cmd *cobra.Command, args []string) error { ts.Close() return fmt.Errorf("failed to extract online DDL binaries: %w", err) } - parser, err = sqlparser.New(sqlparser.Options{ - MySQLServerVersion: mysqlVersion, - TruncateUILen: servenv.TruncateUILen, - TruncateErrLen: servenv.TruncateErrLen, - }) - if err != nil { - return fmt.Errorf("cannot initialize sql parser: %w", err) - } // Initialize and start tm. gRPCPort := int32(0) if servenv.GRPCPort() != 0 { gRPCPort = int32(servenv.GRPCPort()) } - tablet, err := tabletmanager.BuildTabletFromInput(tabletAlias, int32(servenv.Port()), gRPCPort, config.DB, collationEnv) + tablet, err := tabletmanager.BuildTabletFromInput(tabletAlias, int32(servenv.Port()), gRPCPort, config.DB, env.CollationEnv()) if err != nil { return fmt.Errorf("failed to parse --tablet-path: %w", err) } tm = &tabletmanager.TabletManager{ BatchCtx: context.Background(), + Env: env, TopoServer: ts, Cnf: mycnf, MysqlDaemon: mysqld, DBConfigs: config.DB.Clone(), QueryServiceControl: qsc, - UpdateStream: binlog.NewUpdateStream(ts, tablet.Keyspace, tabletAlias.Cell, qsc.SchemaEngine(), parser), - VREngine: vreplication.NewEngine(config, ts, tabletAlias.Cell, mysqld, qsc.LagThrottler(), collationEnv, parser, mysqlVersion), - VDiffEngine: vdiff.NewEngine(ts, tablet, collationEnv, parser), - CollationEnv: collationEnv, - SQLParser: parser, - MySQLVersion: mysqlVersion, + UpdateStream: binlog.NewUpdateStream(ts, tablet.Keyspace, tabletAlias.Cell, qsc.SchemaEngine(), env.Parser()), + VREngine: vreplication.NewEngine(env, config, ts, tabletAlias.Cell, mysqld, qsc.LagThrottler()), + VDiffEngine: vdiff.NewEngine(ts, tablet, env.CollationEnv(), env.Parser()), } if err := tm.Start(tablet, config); err != nil { ts.Close() @@ -260,7 +249,7 @@ func extractOnlineDDL() error { return nil } -func createTabletServer(ctx context.Context, config *tabletenv.TabletConfig, ts *topo.Server, tabletAlias *topodatapb.TabletAlias, collationEnv *collations.Environment, parser *sqlparser.Parser, mysqlVersion string) (*tabletserver.TabletServer, error) { +func createTabletServer(ctx context.Context, env *vtenv.Environment, config *tabletenv.TabletConfig, ts *topo.Server, tabletAlias *topodatapb.TabletAlias) (*tabletserver.TabletServer, error) { if tableACLConfig != "" { // To override default simpleacl, other ACL plugins must set themselves to be default ACL factory tableacl.Register("simpleacl", &simpleacl.Factory{}) @@ -269,7 +258,7 @@ func createTabletServer(ctx context.Context, config *tabletenv.TabletConfig, ts } // creates and registers the query service - qsc := tabletserver.NewTabletServer(ctx, "", config, ts, tabletAlias, collationEnv, parser, mysqlVersion) + qsc := tabletserver.NewTabletServer(ctx, env, "", config, ts, tabletAlias) servenv.OnRun(func() { qsc.Register() addStatusParts(qsc) diff --git a/go/test/endtoend/schemadiff/vrepl/schemadiff_vrepl_suite_test.go b/go/test/endtoend/schemadiff/vrepl/schemadiff_vrepl_suite_test.go index b4411636036..79c3b62ebea 100644 --- a/go/test/endtoend/schemadiff/vrepl/schemadiff_vrepl_suite_test.go +++ b/go/test/endtoend/schemadiff/vrepl/schemadiff_vrepl_suite_test.go @@ -346,12 +346,12 @@ func ignoreAutoIncrement(t *testing.T, createTable string) string { func validateDiff(t *testing.T, fromCreateTable string, toCreateTable string, allowSchemadiffNormalization bool, hints *schemadiff.DiffHints) { // turn the "from" and "to" create statement strings (which we just read via SHOW CREATE TABLE into sqlparser.CreateTable statement) env := schemadiff.NewTestEnv() - fromStmt, err := env.Parser.ParseStrictDDL(fromCreateTable) + fromStmt, err := env.Parser().ParseStrictDDL(fromCreateTable) require.NoError(t, err) fromCreateTableStatement, ok := fromStmt.(*sqlparser.CreateTable) require.True(t, ok) - toStmt, err := env.Parser.ParseStrictDDL(toCreateTable) + toStmt, err := env.Parser().ParseStrictDDL(toCreateTable) require.NoError(t, err) toCreateTableStatement, ok := toStmt.(*sqlparser.CreateTable) require.True(t, ok) @@ -394,7 +394,7 @@ func validateDiff(t *testing.T, fromCreateTable string, toCreateTable string, al // structure is identical. And so we accept that there can be a normalization issue. if allowSchemadiffNormalization { { - stmt, err := env.Parser.ParseStrictDDL(toCreateTable) + stmt, err := env.Parser().ParseStrictDDL(toCreateTable) require.NoError(t, err) createTableStatement, ok := stmt.(*sqlparser.CreateTable) require.True(t, ok) @@ -403,7 +403,7 @@ func validateDiff(t *testing.T, fromCreateTable string, toCreateTable string, al toCreateTable = c.Create().CanonicalStatementString() } { - stmt, err := env.Parser.ParseStrictDDL(resultCreateTable) + stmt, err := env.Parser().ParseStrictDDL(resultCreateTable) require.NoError(t, err) createTableStatement, ok := stmt.(*sqlparser.CreateTable) require.True(t, ok) @@ -418,7 +418,7 @@ func validateDiff(t *testing.T, fromCreateTable string, toCreateTable string, al assert.Equal(t, toCreateTable, resultCreateTable, "mismatched table structure. ALTER query was: %s", diffedAlterQuery) // Also, let's see that our diff agrees there's no change: - resultStmt, err := env.Parser.ParseStrictDDL(resultCreateTable) + resultStmt, err := env.Parser().ParseStrictDDL(resultCreateTable) require.NoError(t, err) resultCreateTableStatement, ok := resultStmt.(*sqlparser.CreateTable) require.True(t, ok) diff --git a/go/test/endtoend/vtgate/queries/random/simplifier_test.go b/go/test/endtoend/vtgate/queries/random/simplifier_test.go index 13f4f891c7b..3f3f78017cd 100644 --- a/go/test/endtoend/vtgate/queries/random/simplifier_test.go +++ b/go/test/endtoend/vtgate/queries/random/simplifier_test.go @@ -22,6 +22,7 @@ import ( "testing" "vitess.io/vitess/go/test/vschemawrapper" + "vitess.io/vitess/go/vt/vtenv" "github.com/stretchr/testify/require" @@ -92,6 +93,7 @@ func simplifyResultsMismatchedQuery(t *testing.T, query string) string { vSchemaWrapper := &vschemawrapper.VSchemaWrapper{ V: vSchema, Version: planbuilder.Gen4, + Env: vtenv.NewTestEnv(), } stmt, err := sqlparser.NewTestParser().Parse(query) diff --git a/go/test/fuzzing/tabletserver_schema_fuzzer.go b/go/test/fuzzing/tabletserver_schema_fuzzer.go index c628593ebb6..5ee680952cc 100644 --- a/go/test/fuzzing/tabletserver_schema_fuzzer.go +++ b/go/test/fuzzing/tabletserver_schema_fuzzer.go @@ -20,11 +20,10 @@ import ( "time" "vitess.io/vitess/go/mysql/collations" - "vitess.io/vitess/go/mysql/config" "vitess.io/vitess/go/mysql/fakesqldb" "vitess.io/vitess/go/sqltypes" "vitess.io/vitess/go/vt/dbconfigs" - "vitess.io/vitess/go/vt/sqlparser" + "vitess.io/vitess/go/vt/vtenv" "vitess.io/vitess/go/vt/vttablet/tabletserver/connpool" "vitess.io/vitess/go/vt/vttablet/tabletserver/schema" "vitess.io/vitess/go/vt/vttablet/tabletserver/tabletenv" @@ -69,7 +68,7 @@ func newTestLoadTable(tableName, comment string, db *fakesqldb.DB) (*schema.Tabl IdleTimeout: 10 * time.Second, } - connPool := connpool.NewPool(tabletenv.NewEnv(nil, "SchemaTest", collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion), "", cfg) + connPool := connpool.NewPool(tabletenv.NewEnv(vtenv.NewTestEnv(), nil, "SchemaTest"), "", cfg) connPool.Open(appParams, dbaParams, appParams) conn, err := connPool.Get(ctx, nil) if err != nil { diff --git a/go/test/fuzzing/vtctl_fuzzer.go b/go/test/fuzzing/vtctl_fuzzer.go index f661c2f547a..cfd19d1ee46 100644 --- a/go/test/fuzzing/vtctl_fuzzer.go +++ b/go/test/fuzzing/vtctl_fuzzer.go @@ -20,13 +20,11 @@ import ( "context" "strings" - "vitess.io/vitess/go/mysql/collations" - "vitess.io/vitess/go/mysql/config" "vitess.io/vitess/go/vt/logutil" - "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" + "vitess.io/vitess/go/vt/vtenv" "vitess.io/vitess/go/vt/vttablet/tmclient" "vitess.io/vitess/go/vt/vttablet/tmclienttest" "vitess.io/vitess/go/vt/wrangler" @@ -183,7 +181,7 @@ func Fuzz(data []byte) int { // Add params to the command commandSlice = append(commandSlice, args...) - _ = vtctl.RunCommand(ctx, wrangler.New(logger, topo, tmc, collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion), commandSlice) + _ = vtctl.RunCommand(ctx, wrangler.New(vtenv.NewTestEnv(), logger, topo, tmc), commandSlice) command++ } diff --git a/go/test/vschemawrapper/vschema_wrapper.go b/go/test/vschemawrapper/vschema_wrapper.go index 5ae3ba15a39..0f8b47a9804 100644 --- a/go/test/vschemawrapper/vschema_wrapper.go +++ b/go/test/vschemawrapper/vschema_wrapper.go @@ -22,7 +22,6 @@ import ( "strings" "vitess.io/vitess/go/mysql/collations" - "vitess.io/vitess/go/mysql/config" "vitess.io/vitess/go/sqltypes" "vitess.io/vitess/go/vt/key" querypb "vitess.io/vitess/go/vt/proto/query" @@ -31,6 +30,7 @@ import ( vtgatepb "vitess.io/vitess/go/vt/proto/vtgate" "vitess.io/vitess/go/vt/sqlparser" "vitess.io/vitess/go/vt/topo/topoproto" + "vitess.io/vitess/go/vt/vtenv" "vitess.io/vitess/go/vt/vterrors" "vitess.io/vitess/go/vt/vtgate/engine" "vitess.io/vitess/go/vt/vtgate/planbuilder/plancontext" @@ -50,7 +50,7 @@ type VSchemaWrapper struct { Version plancontext.PlannerVersion EnableViews bool TestBuilder func(query string, vschema plancontext.VSchema, keyspace string) (*engine.Plan, error) - MySQLServerVersion string + Env *vtenv.Environment } func (vw *VSchemaWrapper) GetPrepareData(stmtName string) *vtgatepb.PrepareData { @@ -84,7 +84,7 @@ func (vw *VSchemaWrapper) PlanPrepareStatement(ctx context.Context, query string if err != nil { return nil, nil, err } - stmt, _, err := vw.SQLParser().Parse2(query) + stmt, _, err := vw.Env.Parser().Parse2(query) if err != nil { return nil, nil, err } @@ -125,22 +125,11 @@ func (vw *VSchemaWrapper) GetSrvVschema() *vschemapb.SrvVSchema { } func (vw *VSchemaWrapper) ConnCollation() collations.ID { - return collations.CollationUtf8mb4ID + return vw.Env.CollationEnv().DefaultConnectionCharset() } -func (vw *VSchemaWrapper) CollationEnv() *collations.Environment { - return collations.MySQL8() -} - -func (vw *VSchemaWrapper) SQLParser() *sqlparser.Parser { - return sqlparser.NewTestParser() -} - -func (vw *VSchemaWrapper) MySQLVersion() string { - if vw.MySQLServerVersion == "" { - return config.DefaultMySQLVersion - } - return vw.MySQLServerVersion +func (vw *VSchemaWrapper) Environment() *vtenv.Environment { + return vw.Env } func (vw *VSchemaWrapper) PlannerWarning(_ string) { diff --git a/go/vt/schemadiff/column.go b/go/vt/schemadiff/column.go index c20f0d23787..d36d226294d 100644 --- a/go/vt/schemadiff/column.go +++ b/go/vt/schemadiff/column.go @@ -108,8 +108,8 @@ func (c *ColumnDefinitionEntity) ColumnDiff( if c.columnDefinition.Type.Charset.Name == "" && c.columnDefinition.Type.Options.Collate != "" { // Column has explicit collation but no charset. We can infer the charset from the collation. - collationID := env.CollationEnv.LookupByName(c.columnDefinition.Type.Options.Collate) - charset := env.CollationEnv.LookupCharsetName(collationID) + collationID := env.CollationEnv().LookupByName(c.columnDefinition.Type.Options.Collate) + charset := env.CollationEnv().LookupCharsetName(collationID) if charset == "" { return nil, vterrors.Errorf(vtrpcpb.Code_INVALID_ARGUMENT, "cannot match charset to collation %v", c.columnDefinition.Type.Options.Collate) } @@ -131,17 +131,17 @@ func (c *ColumnDefinitionEntity) ColumnDiff( c.columnDefinition.Type.Options.Collate = t1cc.collate } if c.columnDefinition.Type.Options.Collate = t1cc.collate; c.columnDefinition.Type.Options.Collate == "" { - collation := env.CollationEnv.DefaultCollationForCharset(t1cc.charset) + collation := env.CollationEnv().DefaultCollationForCharset(t1cc.charset) if collation == collations.Unknown { return nil, vterrors.Errorf(vtrpcpb.Code_INVALID_ARGUMENT, "cannot match collation to charset %v", t1cc.charset) } - c.columnDefinition.Type.Options.Collate = env.CollationEnv.LookupName(collation) + c.columnDefinition.Type.Options.Collate = env.CollationEnv().LookupName(collation) } } if other.columnDefinition.Type.Charset.Name == "" && other.columnDefinition.Type.Options.Collate != "" { // Column has explicit collation but no charset. We can infer the charset from the collation. - collationID := env.CollationEnv.LookupByName(other.columnDefinition.Type.Options.Collate) - charset := env.CollationEnv.LookupCharsetName(collationID) + collationID := env.CollationEnv().LookupByName(other.columnDefinition.Type.Options.Collate) + charset := env.CollationEnv().LookupCharsetName(collationID) if charset == "" { return nil, vterrors.Errorf(vtrpcpb.Code_INVALID_ARGUMENT, "cannot match charset to collation %v", other.columnDefinition.Type.Options.Collate) } @@ -158,11 +158,11 @@ func (c *ColumnDefinitionEntity) ColumnDiff( }() other.columnDefinition.Type.Charset.Name = t2cc.charset if other.columnDefinition.Type.Options.Collate = t2cc.collate; other.columnDefinition.Type.Options.Collate == "" { - collation := env.CollationEnv.DefaultCollationForCharset(t2cc.charset) + collation := env.CollationEnv().DefaultCollationForCharset(t2cc.charset) if collation == collations.Unknown { return nil, vterrors.Errorf(vtrpcpb.Code_INVALID_ARGUMENT, "cannot match collation to charset %v", t2cc.charset) } - other.columnDefinition.Type.Options.Collate = env.CollationEnv.LookupName(collation) + other.columnDefinition.Type.Options.Collate = env.CollationEnv().LookupName(collation) } } } diff --git a/go/vt/schemadiff/diff.go b/go/vt/schemadiff/diff.go index fe58012bda0..d65b9631515 100644 --- a/go/vt/schemadiff/diff.go +++ b/go/vt/schemadiff/diff.go @@ -31,7 +31,7 @@ func DiffCreateTablesQueries(env *Environment, query1 string, query2 string, hin var fromCreateTable *sqlparser.CreateTable var ok bool if query1 != "" { - stmt, err := env.Parser.ParseStrictDDL(query1) + stmt, err := env.Parser().ParseStrictDDL(query1) if err != nil { return nil, err } @@ -42,7 +42,7 @@ func DiffCreateTablesQueries(env *Environment, query1 string, query2 string, hin } var toCreateTable *sqlparser.CreateTable if query2 != "" { - stmt, err := env.Parser.ParseStrictDDL(query2) + stmt, err := env.Parser().ParseStrictDDL(query2) if err != nil { return nil, err } @@ -93,7 +93,7 @@ func DiffCreateViewsQueries(env *Environment, query1 string, query2 string, hint var fromCreateView *sqlparser.CreateView var ok bool if query1 != "" { - stmt, err := env.Parser.ParseStrictDDL(query1) + stmt, err := env.Parser().ParseStrictDDL(query1) if err != nil { return nil, err } @@ -104,7 +104,7 @@ func DiffCreateViewsQueries(env *Environment, query1 string, query2 string, hint } var toCreateView *sqlparser.CreateView if query2 != "" { - stmt, err := env.Parser.ParseStrictDDL(query2) + stmt, err := env.Parser().ParseStrictDDL(query2) if err != nil { return nil, err } diff --git a/go/vt/schemadiff/diff_test.go b/go/vt/schemadiff/diff_test.go index 0d5fd36a274..7cf738c8326 100644 --- a/go/vt/schemadiff/diff_test.go +++ b/go/vt/schemadiff/diff_test.go @@ -25,9 +25,12 @@ import ( "vitess.io/vitess/go/mysql/collations" "vitess.io/vitess/go/vt/sqlparser" + "vitess.io/vitess/go/vt/vtenv" ) func TestDiffTables(t *testing.T) { + env57, err := vtenv.New(vtenv.Options{MySQLServerVersion: "5.7.9"}) + require.NoError(t, err) tt := []struct { name string from string @@ -294,7 +297,7 @@ func TestDiffTables(t *testing.T) { hints: &DiffHints{ TableCharsetCollateStrategy: TableCharsetCollateIgnoreAlways, }, - env: NewEnv(collations.NewEnvironment("5.7.9"), collations.CollationUtf8mb3ID, sqlparser.NewTestParser(), "5.7.9"), + env: NewEnv(env57, collations.CollationUtf8mb3ID), }, } env := NewTestEnv() @@ -309,7 +312,7 @@ func TestDiffTables(t *testing.T) { env = ts.env } if ts.from != "" { - fromStmt, err := env.Parser.ParseStrictDDL(ts.from) + fromStmt, err := env.Parser().ParseStrictDDL(ts.from) assert.NoError(t, err) var ok bool fromCreateTable, ok = fromStmt.(*sqlparser.CreateTable) @@ -317,7 +320,7 @@ func TestDiffTables(t *testing.T) { } var toCreateTable *sqlparser.CreateTable if ts.to != "" { - toStmt, err := env.Parser.ParseStrictDDL(ts.to) + toStmt, err := env.Parser().ParseStrictDDL(ts.to) assert.NoError(t, err) var ok bool toCreateTable, ok = toStmt.(*sqlparser.CreateTable) @@ -356,7 +359,7 @@ func TestDiffTables(t *testing.T) { assert.Equal(t, ts.action, action) // validate we can parse back the statement - _, err = env.Parser.ParseStrictDDL(diff) + _, err = env.Parser().ParseStrictDDL(diff) assert.NoError(t, err) eFrom, eTo := d.Entities() @@ -375,7 +378,7 @@ func TestDiffTables(t *testing.T) { assert.Equal(t, ts.action, action) // validate we can parse back the statement - _, err = env.Parser.ParseStrictDDL(canonicalDiff) + _, err = env.Parser().ParseStrictDDL(canonicalDiff) assert.NoError(t, err) } // let's also check dq, and also validate that dq's statement is identical to d's @@ -442,7 +445,7 @@ func TestDiffViews(t *testing.T) { t.Run(ts.name, func(t *testing.T) { var fromCreateView *sqlparser.CreateView if ts.from != "" { - fromStmt, err := env.Parser.ParseStrictDDL(ts.from) + fromStmt, err := env.Parser().ParseStrictDDL(ts.from) assert.NoError(t, err) var ok bool fromCreateView, ok = fromStmt.(*sqlparser.CreateView) @@ -450,7 +453,7 @@ func TestDiffViews(t *testing.T) { } var toCreateView *sqlparser.CreateView if ts.to != "" { - toStmt, err := env.Parser.ParseStrictDDL(ts.to) + toStmt, err := env.Parser().ParseStrictDDL(ts.to) assert.NoError(t, err) var ok bool toCreateView, ok = toStmt.(*sqlparser.CreateView) @@ -489,7 +492,7 @@ func TestDiffViews(t *testing.T) { assert.Equal(t, ts.action, action) // validate we can parse back the statement - _, err = env.Parser.ParseStrictDDL(diff) + _, err = env.Parser().ParseStrictDDL(diff) assert.NoError(t, err) eFrom, eTo := d.Entities() @@ -508,7 +511,7 @@ func TestDiffViews(t *testing.T) { assert.Equal(t, ts.action, action) // validate we can parse back the statement - _, err = env.Parser.ParseStrictDDL(canonicalDiff) + _, err = env.Parser().ParseStrictDDL(canonicalDiff) assert.NoError(t, err) } @@ -948,11 +951,11 @@ func TestDiffSchemas(t *testing.T) { // validate we can parse back the diff statements for _, s := range statements { - _, err := env.Parser.ParseStrictDDL(s) + _, err := env.Parser().ParseStrictDDL(s) assert.NoError(t, err) } for _, s := range cstatements { - _, err := env.Parser.ParseStrictDDL(s) + _, err := env.Parser().ParseStrictDDL(s) assert.NoError(t, err) } diff --git a/go/vt/schemadiff/env.go b/go/vt/schemadiff/env.go index aedd17eb36d..9037de40b01 100644 --- a/go/vt/schemadiff/env.go +++ b/go/vt/schemadiff/env.go @@ -2,31 +2,24 @@ package schemadiff import ( "vitess.io/vitess/go/mysql/collations" - "vitess.io/vitess/go/mysql/config" - "vitess.io/vitess/go/vt/sqlparser" + "vitess.io/vitess/go/vt/vtenv" ) type Environment struct { - CollationEnv *collations.Environment - DefaultColl collations.ID - Parser *sqlparser.Parser - MySQLVersion string + *vtenv.Environment + DefaultColl collations.ID } func NewTestEnv() *Environment { return &Environment{ - CollationEnv: collations.MySQL8(), - DefaultColl: collations.MySQL8().DefaultConnectionCharset(), - Parser: sqlparser.NewTestParser(), - MySQLVersion: config.DefaultMySQLVersion, + Environment: vtenv.NewTestEnv(), + DefaultColl: collations.MySQL8().DefaultConnectionCharset(), } } -func NewEnv(collEnv *collations.Environment, defaultColl collations.ID, parser *sqlparser.Parser, mysqlVersion string) *Environment { +func NewEnv(env *vtenv.Environment, defaultColl collations.ID) *Environment { return &Environment{ - CollationEnv: collEnv, - DefaultColl: defaultColl, - Parser: parser, - MySQLVersion: mysqlVersion, + Environment: env, + DefaultColl: defaultColl, } } diff --git a/go/vt/schemadiff/schema.go b/go/vt/schemadiff/schema.go index 6cb06f7b31e..84f12e1168a 100644 --- a/go/vt/schemadiff/schema.go +++ b/go/vt/schemadiff/schema.go @@ -104,7 +104,7 @@ func NewSchemaFromStatements(env *Environment, statements []sqlparser.Statement) func NewSchemaFromQueries(env *Environment, queries []string) (*Schema, error) { statements := make([]sqlparser.Statement, 0, len(queries)) for _, q := range queries { - stmt, err := env.Parser.ParseStrictDDL(q) + stmt, err := env.Parser().ParseStrictDDL(q) if err != nil { return nil, err } @@ -116,7 +116,7 @@ func NewSchemaFromQueries(env *Environment, queries []string) (*Schema, error) { // NewSchemaFromSQL creates a valid and normalized schema based on a SQL blob that contains // CREATE statements for various objects (tables, views) func NewSchemaFromSQL(env *Environment, sql string) (*Schema, error) { - statements, err := env.Parser.SplitStatements(sql) + statements, err := env.Parser().SplitStatements(sql) if err != nil { return nil, err } diff --git a/go/vt/schemadiff/semantics.go b/go/vt/schemadiff/semantics.go index d8dfca85035..ee175a37966 100644 --- a/go/vt/schemadiff/semantics.go +++ b/go/vt/schemadiff/semantics.go @@ -18,11 +18,11 @@ package schemadiff import ( "vitess.io/vitess/go/mysql/collations" - "vitess.io/vitess/go/mysql/config" "vitess.io/vitess/go/vt/key" topodatapb "vitess.io/vitess/go/vt/proto/topodata" vschemapb "vitess.io/vitess/go/vt/proto/vschema" "vitess.io/vitess/go/vt/sqlparser" + "vitess.io/vitess/go/vt/vtenv" "vitess.io/vitess/go/vt/vtgate/semantics" "vitess.io/vitess/go/vt/vtgate/vindexes" ) @@ -59,12 +59,8 @@ func (si *declarativeSchemaInformation) ConnCollation() collations.ID { return si.env.DefaultColl } -func (si *declarativeSchemaInformation) CollationEnv() *collations.Environment { - return si.env.CollationEnv -} - -func (si *declarativeSchemaInformation) MySQLVersion() string { - return config.DefaultMySQLVersion +func (si *declarativeSchemaInformation) Environment() *vtenv.Environment { + return si.env.Environment } func (si *declarativeSchemaInformation) ForeignKeyMode(keyspace string) (vschemapb.Keyspace_ForeignKeyMode, error) { diff --git a/go/vt/schemadiff/table.go b/go/vt/schemadiff/table.go index 3288ae24152..97468a124ac 100644 --- a/go/vt/schemadiff/table.go +++ b/go/vt/schemadiff/table.go @@ -389,12 +389,12 @@ func (c *CreateTableEntity) normalizeTableOptions() { switch opt.Name { case "charset": opt.String = strings.ToLower(opt.String) - if charset, ok := c.Env.CollationEnv.CharsetAlias(opt.String); ok { + if charset, ok := c.Env.CollationEnv().CharsetAlias(opt.String); ok { opt.String = charset } case "collate": opt.String = strings.ToLower(opt.String) - if collation, ok := c.Env.CollationEnv.CollationAlias(opt.String); ok { + if collation, ok := c.Env.CollationEnv().CollationAlias(opt.String); ok { opt.String = collation } case "engine": @@ -414,7 +414,7 @@ func (c *CreateTableEntity) GetCharset() string { for _, opt := range c.CreateTable.TableSpec.Options { if strings.ToLower(opt.Name) == "charset" { opt.String = strings.ToLower(opt.String) - if charsetName, ok := c.Env.CollationEnv.CharsetAlias(opt.String); ok { + if charsetName, ok := c.Env.CollationEnv().CharsetAlias(opt.String); ok { return charsetName } return opt.String @@ -429,7 +429,7 @@ func (c *CreateTableEntity) GetCollation() string { for _, opt := range c.CreateTable.TableSpec.Options { if strings.ToLower(opt.Name) == "collate" { opt.String = strings.ToLower(opt.String) - if collationName, ok := c.Env.CollationEnv.CollationAlias(opt.String); ok { + if collationName, ok := c.Env.CollationEnv().CollationAlias(opt.String); ok { return collationName } return opt.String @@ -444,8 +444,8 @@ func (c *CreateTableEntity) Clone() Entity { func getTableCharsetCollate(env *Environment, tableOptions *sqlparser.TableOptions) *charsetCollate { cc := &charsetCollate{ - charset: env.CollationEnv.LookupCharsetName(env.DefaultColl), - collate: env.CollationEnv.LookupName(env.DefaultColl), + charset: env.CollationEnv().LookupCharsetName(env.DefaultColl), + collate: env.CollationEnv().LookupName(env.DefaultColl), } for _, option := range *tableOptions { if strings.EqualFold(option.Name, "charset") { @@ -506,13 +506,13 @@ func (c *CreateTableEntity) normalizeColumnOptions() { // Map any charset aliases to the real charset. This applies mainly right // now to utf8 being an alias for utf8mb3. - if charset, ok := c.Env.CollationEnv.CharsetAlias(col.Type.Charset.Name); ok { + if charset, ok := c.Env.CollationEnv().CharsetAlias(col.Type.Charset.Name); ok { col.Type.Charset.Name = charset } // Map any collation aliases to the real collation. This applies mainly right // now to utf8 being an alias for utf8mb3 collations. - if collation, ok := c.Env.CollationEnv.CollationAlias(col.Type.Options.Collate); ok { + if collation, ok := c.Env.CollationEnv().CollationAlias(col.Type.Options.Collate); ok { col.Type.Options.Collate = collation } @@ -604,7 +604,7 @@ func (c *CreateTableEntity) normalizeColumnOptions() { if col.Type.Charset.Name != "" { col.Type.Charset.Name = "" if col.Type.Options.Collate == "" { - col.Type.Options.Collate = c.Env.CollationEnv.LookupName(c.Env.DefaultColl) + col.Type.Options.Collate = c.Env.CollationEnv().LookupName(c.Env.DefaultColl) } } @@ -1018,7 +1018,7 @@ func (c *CreateTableEntity) diffOptions(alterTable *sqlparser.AlterTable, case "CHARSET": switch hints.TableCharsetCollateStrategy { case TableCharsetCollateStrict: - tableOption = &sqlparser.TableOption{Name: "CHARSET", String: c.Env.CollationEnv.LookupCharsetName(c.Env.DefaultColl), CaseSensitive: true} + tableOption = &sqlparser.TableOption{Name: "CHARSET", String: c.Env.CollationEnv().LookupCharsetName(c.Env.DefaultColl), CaseSensitive: true} // in all other strategies we ignore the charset } case "CHECKSUM": diff --git a/go/vt/schemadiff/table_test.go b/go/vt/schemadiff/table_test.go index 9d653c8f00d..91608d8a65a 100644 --- a/go/vt/schemadiff/table_test.go +++ b/go/vt/schemadiff/table_test.go @@ -1289,12 +1289,12 @@ func TestCreateTableDiff(t *testing.T) { env := NewTestEnv() for _, ts := range tt { t.Run(ts.name, func(t *testing.T) { - fromStmt, err := env.Parser.ParseStrictDDL(ts.from) + fromStmt, err := env.Parser().ParseStrictDDL(ts.from) require.NoError(t, err) fromCreateTable, ok := fromStmt.(*sqlparser.CreateTable) require.True(t, ok) - toStmt, err := env.Parser.ParseStrictDDL(ts.to) + toStmt, err := env.Parser().ParseStrictDDL(ts.to) require.NoError(t, err) toCreateTable, ok := toStmt.(*sqlparser.CreateTable) require.True(t, ok) @@ -1354,7 +1354,7 @@ func TestCreateTableDiff(t *testing.T) { } } // validate we can parse back the statement - _, err := env.Parser.ParseStrictDDL(diff) + _, err := env.Parser().ParseStrictDDL(diff) assert.NoError(t, err) // Validate "from/to" entities @@ -1384,7 +1384,7 @@ func TestCreateTableDiff(t *testing.T) { { cdiff := alter.CanonicalStatementString() assert.Equal(t, ts.cdiff, cdiff) - _, err := env.Parser.ParseStrictDDL(cdiff) + _, err := env.Parser().ParseStrictDDL(cdiff) assert.NoError(t, err) } @@ -1882,12 +1882,12 @@ func TestValidate(t *testing.T) { env := NewTestEnv() for _, ts := range tt { t.Run(ts.name, func(t *testing.T) { - stmt, err := env.Parser.ParseStrictDDL(ts.from) + stmt, err := env.Parser().ParseStrictDDL(ts.from) require.NoError(t, err) fromCreateTable, ok := stmt.(*sqlparser.CreateTable) require.True(t, ok) - stmt, err = env.Parser.ParseStrictDDL(ts.alter) + stmt, err = env.Parser().ParseStrictDDL(ts.alter) require.NoError(t, err) alterTable, ok := stmt.(*sqlparser.AlterTable) require.True(t, ok) @@ -1911,7 +1911,7 @@ func TestValidate(t *testing.T) { require.True(t, ok) applied = c.normalize() - stmt, err := env.Parser.ParseStrictDDL(ts.to) + stmt, err := env.Parser().ParseStrictDDL(ts.to) require.NoError(t, err) toCreateTable, ok := stmt.(*sqlparser.CreateTable) require.True(t, ok) @@ -2196,7 +2196,7 @@ func TestNormalize(t *testing.T) { env := NewTestEnv() for _, ts := range tt { t.Run(ts.name, func(t *testing.T) { - stmt, err := env.Parser.ParseStrictDDL(ts.from) + stmt, err := env.Parser().ParseStrictDDL(ts.from) require.NoError(t, err) fromCreateTable, ok := stmt.(*sqlparser.CreateTable) require.True(t, ok) @@ -2286,7 +2286,7 @@ func TestIndexesCoveringForeignKeyColumns(t *testing.T) { } env := NewTestEnv() - stmt, err := env.Parser.ParseStrictDDL(sql) + stmt, err := env.Parser().ParseStrictDDL(sql) require.NoError(t, err) createTable, ok := stmt.(*sqlparser.CreateTable) require.True(t, ok) diff --git a/go/vt/schemadiff/view_test.go b/go/vt/schemadiff/view_test.go index dd6659f6c0d..e5be9055970 100644 --- a/go/vt/schemadiff/view_test.go +++ b/go/vt/schemadiff/view_test.go @@ -149,12 +149,12 @@ func TestCreateViewDiff(t *testing.T) { env := NewTestEnv() for _, ts := range tt { t.Run(ts.name, func(t *testing.T) { - fromStmt, err := env.Parser.ParseStrictDDL(ts.from) + fromStmt, err := env.Parser().ParseStrictDDL(ts.from) assert.NoError(t, err) fromCreateView, ok := fromStmt.(*sqlparser.CreateView) assert.True(t, ok) - toStmt, err := env.Parser.ParseStrictDDL(ts.to) + toStmt, err := env.Parser().ParseStrictDDL(ts.to) assert.NoError(t, err) toCreateView, ok := toStmt.(*sqlparser.CreateView) assert.True(t, ok) @@ -178,7 +178,7 @@ func TestCreateViewDiff(t *testing.T) { diff := alter.StatementString() assert.Equal(t, ts.diff, diff) // validate we can parse back the statement - _, err := env.Parser.ParseStrictDDL(diff) + _, err := env.Parser().ParseStrictDDL(diff) assert.NoError(t, err) eFrom, eTo := alter.Entities() @@ -200,7 +200,7 @@ func TestCreateViewDiff(t *testing.T) { { cdiff := alter.CanonicalStatementString() assert.Equal(t, ts.cdiff, cdiff) - _, err := env.Parser.ParseStrictDDL(cdiff) + _, err := env.Parser().ParseStrictDDL(cdiff) assert.NoError(t, err) } } @@ -243,7 +243,7 @@ func TestNormalizeView(t *testing.T) { env := NewTestEnv() for _, ts := range tt { t.Run(ts.name, func(t *testing.T) { - stmt, err := env.Parser.ParseStrictDDL(ts.from) + stmt, err := env.Parser().ParseStrictDDL(ts.from) require.NoError(t, err) fromCreateView, ok := stmt.(*sqlparser.CreateView) require.True(t, ok) diff --git a/go/vt/sidecardb/sidecardb.go b/go/vt/sidecardb/sidecardb.go index 6890dd97baf..0947355cd46 100644 --- a/go/vt/sidecardb/sidecardb.go +++ b/go/vt/sidecardb/sidecardb.go @@ -32,6 +32,7 @@ import ( "vitess.io/vitess/go/mysql/collations" "vitess.io/vitess/go/mysql/config" "vitess.io/vitess/go/mysql/sqlerror" + "vitess.io/vitess/go/vt/vtenv" "vitess.io/vitess/go/mysql/fakesqldb" @@ -196,13 +197,11 @@ func printCallerDetails() { } type schemaInit struct { - ctx context.Context - exec Exec - dbCreated bool // The first upgrade/create query will also create the sidecar database if required. - parser *sqlparser.Parser - collEnv *collations.Environment - coll collations.ID - mysqlVersion string + ctx context.Context + env *vtenv.Environment + exec Exec + dbCreated bool // The first upgrade/create query will also create the sidecar database if required. + coll collations.ID } // Exec is a callback that has to be passed to Init() to @@ -234,20 +233,18 @@ func getDDLErrorHistory() []*ddlError { // Init creates or upgrades the sidecar database based on // the declarative schema defined for all tables. -func Init(ctx context.Context, exec Exec, collEnv *collations.Environment, parser *sqlparser.Parser, mysqlVersion string) error { +func Init(ctx context.Context, env *vtenv.Environment, exec Exec) error { printCallerDetails() // for debug purposes only, remove in v17 log.Infof("Starting sidecardb.Init()") once.Do(func() { - loadSchemaDefinitions(parser) + loadSchemaDefinitions(env.Parser()) }) si := &schemaInit{ - ctx: ctx, - exec: exec, - collEnv: collEnv, - parser: parser, - mysqlVersion: mysqlVersion, + ctx: ctx, + exec: exec, + env: env, } // There are paths in the tablet initialization where we @@ -362,7 +359,7 @@ func (si *schemaInit) collation() (collations.ID, error) { switch len(rs.Rows) { case 1: - return si.collEnv.LookupByName(rs.Rows[0][0].ToString()), nil + return si.env.CollationEnv().LookupByName(rs.Rows[0][0].ToString()), nil default: // This should never happen. return collations.Unknown, vterrors.Errorf(vtrpcpb.Code_INTERNAL, "invalid results for SidecarDB query %q as it produced %d rows", sidecarCollationQuery, len(rs.Rows)) @@ -402,7 +399,7 @@ func (si *schemaInit) findTableSchemaDiff(tableName, current, desired string) (s TableCharsetCollateStrategy: schemadiff.TableCharsetCollateIgnoreAlways, AlterTableAlgorithmStrategy: schemadiff.AlterTableAlgorithmStrategyCopy, } - env := schemadiff.NewEnv(si.collEnv, si.coll, si.parser, si.mysqlVersion) + env := schemadiff.NewEnv(si.env, si.coll) diff, err := schemadiff.DiffCreateTablesQueries(env, current, desired, hints) if err != nil { return "", err diff --git a/go/vt/sidecardb/sidecardb_test.go b/go/vt/sidecardb/sidecardb_test.go index 9acdc0be95f..55c2c6cd6b5 100644 --- a/go/vt/sidecardb/sidecardb_test.go +++ b/go/vt/sidecardb/sidecardb_test.go @@ -27,13 +27,12 @@ import ( "vitess.io/vitess/go/constants/sidecar" "vitess.io/vitess/go/vt/dbconfigs" "vitess.io/vitess/go/vt/sqlparser" + "vitess.io/vitess/go/vt/vtenv" "github.com/stretchr/testify/require" "vitess.io/vitess/go/stats" - "vitess.io/vitess/go/mysql/collations" - "vitess.io/vitess/go/mysql/config" "vitess.io/vitess/go/mysql/fakesqldb" "vitess.io/vitess/go/sqltypes" ) @@ -44,9 +43,8 @@ func TestInitErrors(t *testing.T) { db := fakesqldb.New(t) defer db.Close() - collEnv := collations.MySQL8() - parser := sqlparser.NewTestParser() - AddSchemaInitQueries(db, false, parser) + env := vtenv.NewTestEnv() + AddSchemaInitQueries(db, false, env.Parser()) ddlErrorCount.Set(0) ddlCount.Set(0) @@ -74,7 +72,7 @@ func TestInitErrors(t *testing.T) { } // simulate errors for the table creation DDLs applied for tables specified in schemaErrors - stmt, err := parser.Parse(query) + stmt, err := env.Parser().Parse(query) if err != nil { return nil, err } @@ -90,7 +88,7 @@ func TestInitErrors(t *testing.T) { } require.Equal(t, int64(0), getDDLCount()) - err = Init(ctx, exec, collEnv, parser, config.DefaultMySQLVersion) + err = Init(ctx, env, exec) require.NoError(t, err) require.Equal(t, int64(len(sidecarTables)-len(schemaErrors)), getDDLCount()) require.Equal(t, int64(len(schemaErrors)), getDDLErrorCount()) @@ -129,9 +127,8 @@ func TestMiscSidecarDB(t *testing.T) { db := fakesqldb.New(t) defer db.Close() - collEnv := collations.MySQL8() - parser := sqlparser.NewTestParser() - AddSchemaInitQueries(db, false, parser) + env := vtenv.NewTestEnv() + AddSchemaInitQueries(db, false, env.Parser()) db.AddQuery("use dbname", &sqltypes.Result{}) db.AddQueryPattern("set @@session.sql_mode=.*", &sqltypes.Result{}) @@ -156,32 +153,30 @@ func TestMiscSidecarDB(t *testing.T) { require.NoError(t, err) db.AddQuery(dbeq, result) db.AddQuery(sidecar.GetCreateQuery(), &sqltypes.Result{}) - AddSchemaInitQueries(db, false, parser) + AddSchemaInitQueries(db, false, env.Parser()) // tests init on empty db ddlErrorCount.Set(0) ddlCount.Set(0) require.Equal(t, int64(0), getDDLCount()) - err = Init(ctx, exec, collEnv, parser, config.DefaultMySQLVersion) + err = Init(ctx, env, exec) require.NoError(t, err) require.Equal(t, int64(len(sidecarTables)), getDDLCount()) // Include the table DDLs in the expected queries. // This causes them to NOT be created again. - AddSchemaInitQueries(db, true, parser) + AddSchemaInitQueries(db, true, env.Parser()) // tests init on already inited db - err = Init(ctx, exec, collEnv, parser, config.DefaultMySQLVersion) + err = Init(ctx, env, exec) require.NoError(t, err) require.Equal(t, int64(len(sidecarTables)), getDDLCount()) // tests misc paths not covered above si := &schemaInit{ - ctx: ctx, - exec: exec, - collEnv: collations.MySQL8(), - parser: parser, - mysqlVersion: config.DefaultMySQLVersion, + ctx: ctx, + exec: exec, + env: env, } err = si.setCurrentDatabase(sidecar.GetIdentifier()) @@ -232,16 +227,14 @@ func TestAlterTableAlgorithm(t *testing.T) { {"modify column", "t1", "create table if not exists _vt.t1(i int)", "create table if not exists _vt.t(i float)"}, } si := &schemaInit{ - collEnv: collations.MySQL8(), - parser: sqlparser.NewTestParser(), - mysqlVersion: config.DefaultMySQLVersion, + env: vtenv.NewTestEnv(), } copyAlgo := sqlparser.AlgorithmValue("COPY") for _, tc := range testCases { t.Run(tc.testName, func(t *testing.T) { diff, err := si.findTableSchemaDiff(tc.tableName, tc.currentSchema, tc.desiredSchema) require.NoError(t, err) - stmt, err := si.parser.Parse(diff) + stmt, err := si.env.Parser().Parse(diff) require.NoError(t, err) alterTable, ok := stmt.(*sqlparser.AlterTable) require.True(t, ok) diff --git a/go/vt/sqlparser/cached_size.go b/go/vt/sqlparser/cached_size.go index ebac6a68e23..cecd02ceadc 100644 --- a/go/vt/sqlparser/cached_size.go +++ b/go/vt/sqlparser/cached_size.go @@ -3070,6 +3070,18 @@ func (cached *ParsedQuery) CachedSize(alloc bool) int64 { } return size } +func (cached *Parser) CachedSize(alloc bool) int64 { + if cached == nil { + return int64(0) + } + size := int64(0) + if alloc { + size += int64(32) + } + // field version string + size += hack.RuntimeAllocSize(int64(len(cached.version))) + return size +} func (cached *PartitionDefinition) CachedSize(alloc bool) int64 { if cached == nil { return int64(0) diff --git a/go/vt/sqlparser/truncate_query.go b/go/vt/sqlparser/truncate_query.go index 3f4231fe8b5..996ceeb20cf 100644 --- a/go/vt/sqlparser/truncate_query.go +++ b/go/vt/sqlparser/truncate_query.go @@ -26,10 +26,14 @@ func (p *Parser) GetTruncateErrLen() int { func TruncateQuery(query string, max int) string { sql, comments := SplitMarginComments(query) - if max == 0 || len(sql) <= max { + if max == 0 || len(sql) <= max || len(sql) < len(TruncationText) { return comments.Leading + sql + comments.Trailing } + if max < len(TruncationText)+1 { + max = len(TruncationText) + 1 + } + return comments.Leading + sql[:max-(len(TruncationText)+1)] + " " + TruncationText + comments.Trailing } diff --git a/go/vt/sqlparser/truncate_query_test.go b/go/vt/sqlparser/truncate_query_test.go index c7a2eed4493..64d53ae7b10 100644 --- a/go/vt/sqlparser/truncate_query_test.go +++ b/go/vt/sqlparser/truncate_query_test.go @@ -13,6 +13,21 @@ func TestTruncateQuery(t *testing.T) { max int want string }{ + { + query: "select 111", + max: 2, + want: "select 111", + }, + { + query: "select 1111", + max: 2, + want: " [TRUNCATED]", + }, + { + query: "select 11111", + max: 2, + want: " [TRUNCATED]", + }, { query: "select * from test where name = 'abc'", max: 30, diff --git a/go/vt/throttler/demo/throttler_demo.go b/go/vt/throttler/demo/throttler_demo.go index 1f020bf078e..15228475bfb 100644 --- a/go/vt/throttler/demo/throttler_demo.go +++ b/go/vt/throttler/demo/throttler_demo.go @@ -26,9 +26,7 @@ import ( "github.com/spf13/pflag" - "vitess.io/vitess/go/vt/sqlparser" - - "vitess.io/vitess/go/mysql/collations" + "vitess.io/vitess/go/vt/vtenv" "vitess.io/vitess/go/vt/discovery" "vitess.io/vitess/go/vt/log" @@ -118,9 +116,9 @@ type replica struct { wg sync.WaitGroup } -func newReplica(lagUpdateInterval, degrationInterval, degrationDuration time.Duration, ts *topo.Server, collationEnv *collations.Environment, parser *sqlparser.Parser, mysqlVersion string) *replica { +func newReplica(env *vtenv.Environment, lagUpdateInterval, degrationInterval, degrationDuration time.Duration, ts *topo.Server) *replica { t := &testing.T{} - wr := wrangler.New(logutil.NewConsoleLogger(), ts, tmclient.NewTabletManagerClient(), collationEnv, parser, mysqlVersion) + wr := wrangler.New(env, logutil.NewConsoleLogger(), ts, tmclient.NewTabletManagerClient()) fakeTablet := testlib.NewFakeTablet(t, wr, "cell1", 0, topodatapb.TabletType_REPLICA, nil, testlib.TabletKeyspaceShard(t, "ks", "-80")) fakeTablet.StartActionLoop(t, wr) @@ -312,17 +310,15 @@ func main() { log.Infof("start rate set to: %v", rate) ts := memorytopo.NewServer(context.Background(), "cell1") - 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 { log.Fatal(err) } - replica := newReplica(lagUpdateInterval, replicaDegrationInterval, replicaDegrationDuration, ts, collationEnv, parser, mysqlVersion) + replica := newReplica(env, lagUpdateInterval, replicaDegrationInterval, replicaDegrationDuration, ts) primary := &primary{replica: replica} client := newClient(context.Background(), primary, replica, ts) client.run() diff --git a/go/vt/vtadmin/api.go b/go/vt/vtadmin/api.go index 4d82d77b41a..ff49d2ac218 100644 --- a/go/vt/vtadmin/api.go +++ b/go/vt/vtadmin/api.go @@ -32,9 +32,7 @@ import ( "github.com/gorilla/mux" "github.com/patrickmn/go-cache" - "vitess.io/vitess/go/vt/sqlparser" - - "vitess.io/vitess/go/mysql/collations" + "vitess.io/vitess/go/vt/vtenv" "vitess.io/vitess/go/sets" "vitess.io/vitess/go/trace" @@ -82,9 +80,7 @@ type API struct { // we're not super concerned because we will be deleting vtexplain Soon(TM). vtexplainLock sync.Mutex - collationEnv *collations.Environment - parser *sqlparser.Parser - mysqlServerVersion string + env *vtenv.Environment } // Options wraps the configuration options for different components of the @@ -100,7 +96,7 @@ type Options struct { // NewAPI returns a new API, configured to service the given set of clusters, // and configured with the given options. -func NewAPI(clusters []*cluster.Cluster, opts Options, collationEnv *collations.Environment, parser *sqlparser.Parser, mysqlServerVersion string) *API { +func NewAPI(env *vtenv.Environment, clusters []*cluster.Cluster, opts Options) *API { clusterMap := make(map[string]*cluster.Cluster, len(clusters)) for _, cluster := range clusters { clusterMap[cluster.ID] = cluster @@ -143,12 +139,10 @@ func NewAPI(clusters []*cluster.Cluster, opts Options, collationEnv *collations. } api := &API{ - clusters: clusters, - clusterMap: clusterMap, - authz: authz, - collationEnv: collationEnv, - parser: parser, - mysqlServerVersion: mysqlServerVersion, + clusters: clusters, + clusterMap: clusterMap, + authz: authz, + env: env, } if opts.EnableDynamicClusters { @@ -303,13 +297,11 @@ func (api *API) WithCluster(c *cluster.Cluster, id string) dynamic.API { defer api.clusterMu.Unlock() dynamicAPI := &API{ - router: api.router, - serv: api.serv, - authz: api.authz, - options: api.options, - collationEnv: api.collationEnv, - parser: api.parser, - mysqlServerVersion: api.mysqlServerVersion, + router: api.router, + serv: api.serv, + authz: api.authz, + options: api.options, + env: api.env, } if c != nil { @@ -2162,7 +2154,7 @@ func (api *API) VTExplain(ctx context.Context, req *vtadminpb.VTExplainRequest) return nil, er.Error() } - vte, err := vtexplain.Init(ctx, srvVSchema, schema, shardMap, &vtexplain.Options{ReplicationMode: "ROW"}, api.collationEnv, api.parser, api.mysqlServerVersion) + vte, err := vtexplain.Init(ctx, api.env, srvVSchema, schema, shardMap, &vtexplain.Options{ReplicationMode: "ROW"}) if err != nil { return nil, fmt.Errorf("error initilaizing vtexplain: %w", err) } diff --git a/go/vt/vtadmin/api_authz_test.go b/go/vt/vtadmin/api_authz_test.go index cac4a540ca4..0e83c8a1484 100644 --- a/go/vt/vtadmin/api_authz_test.go +++ b/go/vt/vtadmin/api_authz_test.go @@ -27,15 +27,13 @@ import ( "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" - "vitess.io/vitess/go/mysql/collations" - "vitess.io/vitess/go/mysql/config" - "vitess.io/vitess/go/vt/sqlparser" "vitess.io/vitess/go/vt/topo/topoproto" "vitess.io/vitess/go/vt/vtadmin" "vitess.io/vitess/go/vt/vtadmin/cluster" "vitess.io/vitess/go/vt/vtadmin/rbac" "vitess.io/vitess/go/vt/vtadmin/testutil" "vitess.io/vitess/go/vt/vtadmin/vtctldclient/fakevtctldclient" + "vitess.io/vitess/go/vt/vtenv" logutilpb "vitess.io/vitess/go/vt/proto/logutil" mysqlctlpb "vitess.io/vitess/go/vt/proto/mysqlctl" @@ -69,7 +67,7 @@ func TestCreateKeyspace(t *testing.T) { err := opts.RBAC.Reify() require.NoError(t, err, "failed to reify authorization rules: %+v", opts.RBAC.Rules) - api := vtadmin.NewAPI(testClusters(t), opts, collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion) + api := vtadmin.NewAPI(vtenv.NewTestEnv(), testClusters(t), opts) t.Cleanup(func() { if err := api.Close(); err != nil { t.Logf("api did not close cleanly: %s", err.Error()) @@ -134,7 +132,7 @@ func TestCreateShard(t *testing.T) { err := opts.RBAC.Reify() require.NoError(t, err, "failed to reify authorization rules: %+v", opts.RBAC.Rules) - api := vtadmin.NewAPI(testClusters(t), opts, collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion) + api := vtadmin.NewAPI(vtenv.NewTestEnv(), testClusters(t), opts) t.Cleanup(func() { if err := api.Close(); err != nil { t.Logf("api did not close cleanly: %s", err.Error()) @@ -201,7 +199,7 @@ func TestDeleteKeyspace(t *testing.T) { err := opts.RBAC.Reify() require.NoError(t, err, "failed to reify authorization rules: %+v", opts.RBAC.Rules) - api := vtadmin.NewAPI(testClusters(t), opts, collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion) + api := vtadmin.NewAPI(vtenv.NewTestEnv(), testClusters(t), opts) t.Cleanup(func() { if err := api.Close(); err != nil { t.Logf("api did not close cleanly: %s", err.Error()) @@ -266,7 +264,7 @@ func TestDeleteShards(t *testing.T) { err := opts.RBAC.Reify() require.NoError(t, err, "failed to reify authorization rules: %+v", opts.RBAC.Rules) - api := vtadmin.NewAPI(testClusters(t), opts, collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion) + api := vtadmin.NewAPI(vtenv.NewTestEnv(), testClusters(t), opts) t.Cleanup(func() { if err := api.Close(); err != nil { t.Logf("api did not close cleanly: %s", err.Error()) @@ -341,7 +339,7 @@ func TestDeleteTablet(t *testing.T) { err := opts.RBAC.Reify() require.NoError(t, err, "failed to reify authorization rules: %+v", opts.RBAC.Rules) - api := vtadmin.NewAPI(testClusters(t), opts, collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion) + api := vtadmin.NewAPI(vtenv.NewTestEnv(), testClusters(t), opts) t.Cleanup(func() { if err := api.Close(); err != nil { t.Logf("api did not close cleanly: %s", err.Error()) @@ -408,7 +406,7 @@ func TestEmergencyFailoverShard(t *testing.T) { err := opts.RBAC.Reify() require.NoError(t, err, "failed to reify authorization rules: %+v", opts.RBAC.Rules) - api := vtadmin.NewAPI(testClusters(t), opts, collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion) + api := vtadmin.NewAPI(vtenv.NewTestEnv(), testClusters(t), opts) t.Cleanup(func() { if err := api.Close(); err != nil { t.Logf("api did not close cleanly: %s", err.Error()) @@ -484,7 +482,7 @@ func TestFindSchema(t *testing.T) { t.Run("unauthorized actor", func(t *testing.T) { t.Parallel() - api := vtadmin.NewAPI(testClusters(t), opts, collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion) + api := vtadmin.NewAPI(vtenv.NewTestEnv(), testClusters(t), opts) t.Cleanup(func() { if err := api.Close(); err != nil { t.Logf("api did not close cleanly: %s", err.Error()) @@ -505,7 +503,7 @@ func TestFindSchema(t *testing.T) { t.Run("partial access", func(t *testing.T) { t.Parallel() - api := vtadmin.NewAPI(testClusters(t), opts, collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion) + api := vtadmin.NewAPI(vtenv.NewTestEnv(), testClusters(t), opts) t.Cleanup(func() { if err := api.Close(); err != nil { t.Logf("api did not close cleanly: %s", err.Error()) @@ -525,7 +523,7 @@ func TestFindSchema(t *testing.T) { t.Run("full access", func(t *testing.T) { t.Parallel() - api := vtadmin.NewAPI(testClusters(t), opts, collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion) + api := vtadmin.NewAPI(vtenv.NewTestEnv(), testClusters(t), opts) t.Cleanup(func() { if err := api.Close(); err != nil { t.Logf("api did not close cleanly: %s", err.Error()) @@ -574,7 +572,7 @@ func TestGetBackups(t *testing.T) { err := opts.RBAC.Reify() require.NoError(t, err, "failed to reify authorization rules: %+v", opts.RBAC.Rules) - api := vtadmin.NewAPI(testClusters(t), opts, collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion) + api := vtadmin.NewAPI(vtenv.NewTestEnv(), testClusters(t), opts) t.Cleanup(func() { if err := api.Close(); err != nil { t.Logf("api did not close cleanly: %s", err.Error()) @@ -647,7 +645,7 @@ func TestGetCellInfos(t *testing.T) { err := opts.RBAC.Reify() require.NoError(t, err, "failed to reify authorization rules: %+v", opts.RBAC.Rules) - api := vtadmin.NewAPI(testClusters(t), opts, collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion) + api := vtadmin.NewAPI(vtenv.NewTestEnv(), testClusters(t), opts) t.Cleanup(func() { if err := api.Close(); err != nil { t.Logf("api did not close cleanly: %s", err.Error()) @@ -726,7 +724,7 @@ func TestGetCellsAliases(t *testing.T) { err := opts.RBAC.Reify() require.NoError(t, err, "failed to reify authorization rules: %+v", opts.RBAC.Rules) - api := vtadmin.NewAPI(testClusters(t), opts, collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion) + api := vtadmin.NewAPI(vtenv.NewTestEnv(), testClusters(t), opts) t.Cleanup(func() { if err := api.Close(); err != nil { t.Logf("api did not close cleanly: %s", err.Error()) @@ -793,7 +791,7 @@ func TestGetClusters(t *testing.T) { err := opts.RBAC.Reify() require.NoError(t, err, "failed to reify authorization rules: %+v", opts.RBAC.Rules) - api := vtadmin.NewAPI(testClusters(t), opts, collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion) + api := vtadmin.NewAPI(vtenv.NewTestEnv(), testClusters(t), opts) t.Cleanup(func() { if err := api.Close(); err != nil { t.Logf("api did not close cleanly: %s", err.Error()) @@ -867,7 +865,7 @@ func TestGetGates(t *testing.T) { t.Run("unauthorized actor", func(t *testing.T) { t.Parallel() - api := vtadmin.NewAPI(testClusters(t), opts, collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion) + api := vtadmin.NewAPI(vtenv.NewTestEnv(), testClusters(t), opts) t.Cleanup(func() { if err := api.Close(); err != nil { t.Logf("api did not close cleanly: %s", err.Error()) @@ -886,7 +884,7 @@ func TestGetGates(t *testing.T) { t.Run("partial access", func(t *testing.T) { t.Parallel() - api := vtadmin.NewAPI(testClusters(t), opts, collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion) + api := vtadmin.NewAPI(vtenv.NewTestEnv(), testClusters(t), opts) t.Cleanup(func() { if err := api.Close(); err != nil { t.Logf("api did not close cleanly: %s", err.Error()) @@ -906,7 +904,7 @@ func TestGetGates(t *testing.T) { t.Run("full access", func(t *testing.T) { t.Parallel() - api := vtadmin.NewAPI(testClusters(t), opts, collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion) + api := vtadmin.NewAPI(vtenv.NewTestEnv(), testClusters(t), opts) t.Cleanup(func() { if err := api.Close(); err != nil { t.Logf("api did not close cleanly: %s", err.Error()) @@ -947,7 +945,7 @@ func TestGetKeyspace(t *testing.T) { err := opts.RBAC.Reify() require.NoError(t, err, "failed to reify authorization rules: %+v", opts.RBAC.Rules) - api := vtadmin.NewAPI(testClusters(t), opts, collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion) + api := vtadmin.NewAPI(vtenv.NewTestEnv(), testClusters(t), opts) t.Cleanup(func() { if err := api.Close(); err != nil { t.Logf("api did not close cleanly: %s", err.Error()) @@ -1014,7 +1012,7 @@ func TestGetKeyspaces(t *testing.T) { err := opts.RBAC.Reify() require.NoError(t, err, "failed to reify authorization rules: %+v", opts.RBAC.Rules) - api := vtadmin.NewAPI(testClusters(t), opts, collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion) + api := vtadmin.NewAPI(vtenv.NewTestEnv(), testClusters(t), opts) t.Cleanup(func() { if err := api.Close(); err != nil { t.Logf("api did not close cleanly: %s", err.Error()) @@ -1098,7 +1096,7 @@ func TestGetSchema(t *testing.T) { t.Run("unauthorized actor", func(t *testing.T) { t.Parallel() - api := vtadmin.NewAPI(testClusters(t), opts, collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion) + api := vtadmin.NewAPI(vtenv.NewTestEnv(), testClusters(t), opts) t.Cleanup(func() { if err := api.Close(); err != nil { t.Logf("api did not close cleanly: %s", err.Error()) @@ -1121,7 +1119,7 @@ func TestGetSchema(t *testing.T) { t.Run("authorized actor", func(t *testing.T) { t.Parallel() - api := vtadmin.NewAPI(testClusters(t), opts, collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion) + api := vtadmin.NewAPI(vtenv.NewTestEnv(), testClusters(t), opts) t.Cleanup(func() { if err := api.Close(); err != nil { t.Logf("api did not close cleanly: %s", err.Error()) @@ -1174,7 +1172,7 @@ func TestGetSchemas(t *testing.T) { t.Run("unauthorized actor", func(t *testing.T) { t.Parallel() - api := vtadmin.NewAPI(testClusters(t), opts, collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion) + api := vtadmin.NewAPI(vtenv.NewTestEnv(), testClusters(t), opts) t.Cleanup(func() { if err := api.Close(); err != nil { t.Logf("api did not close cleanly: %s", err.Error()) @@ -1193,7 +1191,7 @@ func TestGetSchemas(t *testing.T) { t.Run("partial access", func(t *testing.T) { t.Parallel() - api := vtadmin.NewAPI(testClusters(t), opts, collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion) + api := vtadmin.NewAPI(vtenv.NewTestEnv(), testClusters(t), opts) t.Cleanup(func() { if err := api.Close(); err != nil { t.Logf("api did not close cleanly: %s", err.Error()) @@ -1219,7 +1217,7 @@ func TestGetSchemas(t *testing.T) { t.Run("full access", func(t *testing.T) { t.Parallel() - api := vtadmin.NewAPI(testClusters(t), opts, collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion) + api := vtadmin.NewAPI(vtenv.NewTestEnv(), testClusters(t), opts) t.Cleanup(func() { if err := api.Close(); err != nil { t.Logf("api did not close cleanly: %s", err.Error()) @@ -1272,7 +1270,7 @@ func TestGetShardReplicationPositions(t *testing.T) { err := opts.RBAC.Reify() require.NoError(t, err, "failed to reify authorization rules: %+v", opts.RBAC.Rules) - api := vtadmin.NewAPI(testClusters(t), opts, collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion) + api := vtadmin.NewAPI(vtenv.NewTestEnv(), testClusters(t), opts) t.Cleanup(func() { if err := api.Close(); err != nil { t.Logf("api did not close cleanly: %s", err.Error()) @@ -1353,7 +1351,7 @@ func TestGetSrvVSchema(t *testing.T) { err := opts.RBAC.Reify() require.NoError(t, err, "failed to reify authorization rules: %+v", opts.RBAC.Rules) - api := vtadmin.NewAPI(testClusters(t), opts, collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion) + api := vtadmin.NewAPI(vtenv.NewTestEnv(), testClusters(t), opts) t.Cleanup(func() { if err := api.Close(); err != nil { t.Logf("api did not close cleanly: %s", err.Error()) @@ -1420,7 +1418,7 @@ func TestGetSrvVSchemas(t *testing.T) { err := opts.RBAC.Reify() require.NoError(t, err, "failed to reify authorization rules: %+v", opts.RBAC.Rules) - api := vtadmin.NewAPI(testClusters(t), opts, collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion) + api := vtadmin.NewAPI(vtenv.NewTestEnv(), testClusters(t), opts) t.Cleanup(func() { if err := api.Close(); err != nil { t.Logf("api did not close cleanly: %s", err.Error()) @@ -1501,7 +1499,7 @@ func TestGetTablet(t *testing.T) { err := opts.RBAC.Reify() require.NoError(t, err, "failed to reify authorization rules: %+v", opts.RBAC.Rules) - api := vtadmin.NewAPI(testClusters(t), opts, collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion) + api := vtadmin.NewAPI(vtenv.NewTestEnv(), testClusters(t), opts) t.Cleanup(func() { if err := api.Close(); err != nil { t.Logf("api did not close cleanly: %s", err.Error()) @@ -1572,7 +1570,7 @@ func TestGetTablets(t *testing.T) { err := opts.RBAC.Reify() require.NoError(t, err, "failed to reify authorization rules: %+v", opts.RBAC.Rules) - api := vtadmin.NewAPI(testClusters(t), opts, collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion) + api := vtadmin.NewAPI(vtenv.NewTestEnv(), testClusters(t), opts) t.Cleanup(func() { if err := api.Close(); err != nil { t.Logf("api did not close cleanly: %s", err.Error()) @@ -1653,7 +1651,7 @@ func TestGetVSchema(t *testing.T) { err := opts.RBAC.Reify() require.NoError(t, err, "failed to reify authorization rules: %+v", opts.RBAC.Rules) - api := vtadmin.NewAPI(testClusters(t), opts, collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion) + api := vtadmin.NewAPI(vtenv.NewTestEnv(), testClusters(t), opts) t.Cleanup(func() { if err := api.Close(); err != nil { t.Logf("api did not close cleanly: %s", err.Error()) @@ -1720,7 +1718,7 @@ func TestGetVSchemas(t *testing.T) { err := opts.RBAC.Reify() require.NoError(t, err, "failed to reify authorization rules: %+v", opts.RBAC.Rules) - api := vtadmin.NewAPI(testClusters(t), opts, collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion) + api := vtadmin.NewAPI(vtenv.NewTestEnv(), testClusters(t), opts) t.Cleanup(func() { if err := api.Close(); err != nil { t.Logf("api did not close cleanly: %s", err.Error()) @@ -1810,7 +1808,7 @@ func TestGetVtctlds(t *testing.T) { t.Run("unauthorized actor", func(t *testing.T) { t.Parallel() - api := vtadmin.NewAPI(testClusters(t), opts, collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion) + api := vtadmin.NewAPI(vtenv.NewTestEnv(), testClusters(t), opts) t.Cleanup(func() { if err := api.Close(); err != nil { t.Logf("api did not close cleanly: %s", err.Error()) @@ -1829,7 +1827,7 @@ func TestGetVtctlds(t *testing.T) { t.Run("partial access", func(t *testing.T) { t.Parallel() - api := vtadmin.NewAPI(testClusters(t), opts, collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion) + api := vtadmin.NewAPI(vtenv.NewTestEnv(), testClusters(t), opts) t.Cleanup(func() { if err := api.Close(); err != nil { t.Logf("api did not close cleanly: %s", err.Error()) @@ -1849,7 +1847,7 @@ func TestGetVtctlds(t *testing.T) { t.Run("full access", func(t *testing.T) { t.Parallel() - api := vtadmin.NewAPI(testClusters(t), opts, collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion) + api := vtadmin.NewAPI(vtenv.NewTestEnv(), testClusters(t), opts) t.Cleanup(func() { if err := api.Close(); err != nil { t.Logf("api did not close cleanly: %s", err.Error()) @@ -1890,7 +1888,7 @@ func TestGetWorkflow(t *testing.T) { err := opts.RBAC.Reify() require.NoError(t, err, "failed to reify authorization rules: %+v", opts.RBAC.Rules) - api := vtadmin.NewAPI(testClusters(t), opts, collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion) + api := vtadmin.NewAPI(vtenv.NewTestEnv(), testClusters(t), opts) t.Cleanup(func() { if err := api.Close(); err != nil { t.Logf("api did not close cleanly: %s", err.Error()) @@ -1959,7 +1957,7 @@ func TestGetWorkflows(t *testing.T) { err := opts.RBAC.Reify() require.NoError(t, err, "failed to reify authorization rules: %+v", opts.RBAC.Rules) - api := vtadmin.NewAPI(testClusters(t), opts, collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion) + api := vtadmin.NewAPI(vtenv.NewTestEnv(), testClusters(t), opts) t.Cleanup(func() { if err := api.Close(); err != nil { t.Logf("api did not close cleanly: %s", err.Error()) @@ -2026,7 +2024,7 @@ func TestPingTablet(t *testing.T) { err := opts.RBAC.Reify() require.NoError(t, err, "failed to reify authorization rules: %+v", opts.RBAC.Rules) - api := vtadmin.NewAPI(testClusters(t), opts, collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion) + api := vtadmin.NewAPI(vtenv.NewTestEnv(), testClusters(t), opts) t.Cleanup(func() { if err := api.Close(); err != nil { t.Logf("api did not close cleanly: %s", err.Error()) @@ -2091,7 +2089,7 @@ func TestPlannedFailoverShard(t *testing.T) { err := opts.RBAC.Reify() require.NoError(t, err, "failed to reify authorization rules: %+v", opts.RBAC.Rules) - api := vtadmin.NewAPI(testClusters(t), opts, collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion) + api := vtadmin.NewAPI(vtenv.NewTestEnv(), testClusters(t), opts) t.Cleanup(func() { if err := api.Close(); err != nil { t.Logf("api did not close cleanly: %s", err.Error()) @@ -2158,7 +2156,7 @@ func TestRefreshState(t *testing.T) { err := opts.RBAC.Reify() require.NoError(t, err, "failed to reify authorization rules: %+v", opts.RBAC.Rules) - api := vtadmin.NewAPI(testClusters(t), opts, collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion) + api := vtadmin.NewAPI(vtenv.NewTestEnv(), testClusters(t), opts) t.Cleanup(func() { if err := api.Close(); err != nil { t.Logf("api did not close cleanly: %s", err.Error()) @@ -2223,7 +2221,7 @@ func TestRefreshTabletReplicationSource(t *testing.T) { err := opts.RBAC.Reify() require.NoError(t, err, "failed to reify authorization rules: %+v", opts.RBAC.Rules) - api := vtadmin.NewAPI(testClusters(t), opts, collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion) + api := vtadmin.NewAPI(vtenv.NewTestEnv(), testClusters(t), opts) t.Cleanup(func() { if err := api.Close(); err != nil { t.Logf("api did not close cleanly: %s", err.Error()) @@ -2294,7 +2292,7 @@ func TestReloadSchemas(t *testing.T) { err := opts.RBAC.Reify() require.NoError(t, err, "failed to reify authorization rules: %+v", opts.RBAC.Rules) - api := vtadmin.NewAPI(testClusters(t), opts, collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion) + api := vtadmin.NewAPI(vtenv.NewTestEnv(), testClusters(t), opts) t.Cleanup(func() { if err := api.Close(); err != nil { t.Logf("api did not close cleanly: %s", err.Error()) @@ -2374,7 +2372,7 @@ func TestRunHealthCheck(t *testing.T) { err := opts.RBAC.Reify() require.NoError(t, err, "failed to reify authorization rules: %+v", opts.RBAC.Rules) - api := vtadmin.NewAPI(testClusters(t), opts, collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion) + api := vtadmin.NewAPI(vtenv.NewTestEnv(), testClusters(t), opts) t.Cleanup(func() { if err := api.Close(); err != nil { t.Logf("api did not close cleanly: %s", err.Error()) @@ -2439,7 +2437,7 @@ func TestSetReadOnly(t *testing.T) { err := opts.RBAC.Reify() require.NoError(t, err, "failed to reify authorization rules: %+v", opts.RBAC.Rules) - api := vtadmin.NewAPI(testClusters(t), opts, collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion) + api := vtadmin.NewAPI(vtenv.NewTestEnv(), testClusters(t), opts) t.Cleanup(func() { if err := api.Close(); err != nil { t.Logf("api did not close cleanly: %s", err.Error()) @@ -2504,7 +2502,7 @@ func TestSetReadWrite(t *testing.T) { err := opts.RBAC.Reify() require.NoError(t, err, "failed to reify authorization rules: %+v", opts.RBAC.Rules) - api := vtadmin.NewAPI(testClusters(t), opts, collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion) + api := vtadmin.NewAPI(vtenv.NewTestEnv(), testClusters(t), opts) t.Cleanup(func() { if err := api.Close(); err != nil { t.Logf("api did not close cleanly: %s", err.Error()) @@ -2569,7 +2567,7 @@ func TestStartReplication(t *testing.T) { err := opts.RBAC.Reify() require.NoError(t, err, "failed to reify authorization rules: %+v", opts.RBAC.Rules) - api := vtadmin.NewAPI(testClusters(t), opts, collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion) + api := vtadmin.NewAPI(vtenv.NewTestEnv(), testClusters(t), opts) t.Cleanup(func() { if err := api.Close(); err != nil { t.Logf("api did not close cleanly: %s", err.Error()) @@ -2634,7 +2632,7 @@ func TestStopReplication(t *testing.T) { err := opts.RBAC.Reify() require.NoError(t, err, "failed to reify authorization rules: %+v", opts.RBAC.Rules) - api := vtadmin.NewAPI(testClusters(t), opts, collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion) + api := vtadmin.NewAPI(vtenv.NewTestEnv(), testClusters(t), opts) t.Cleanup(func() { if err := api.Close(); err != nil { t.Logf("api did not close cleanly: %s", err.Error()) @@ -2699,7 +2697,7 @@ func TestTabletExternallyPromoted(t *testing.T) { err := opts.RBAC.Reify() require.NoError(t, err, "failed to reify authorization rules: %+v", opts.RBAC.Rules) - api := vtadmin.NewAPI(testClusters(t), opts, collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion) + api := vtadmin.NewAPI(vtenv.NewTestEnv(), testClusters(t), opts) t.Cleanup(func() { if err := api.Close(); err != nil { t.Logf("api did not close cleanly: %s", err.Error()) @@ -2767,7 +2765,7 @@ func TestVTExplain(t *testing.T) { t.Run("unauthorized actor", func(t *testing.T) { t.Parallel() - api := vtadmin.NewAPI(testClusters(t), opts, collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion) + api := vtadmin.NewAPI(vtenv.NewTestEnv(), testClusters(t), opts) t.Cleanup(func() { if err := api.Close(); err != nil { t.Logf("api did not close cleanly: %s", err.Error()) @@ -2789,7 +2787,7 @@ func TestVTExplain(t *testing.T) { t.Run("authorized actor", func(t *testing.T) { t.Parallel() - api := vtadmin.NewAPI(testClusters(t), opts, collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion) + api := vtadmin.NewAPI(vtenv.NewTestEnv(), testClusters(t), opts) t.Cleanup(func() { if err := api.Close(); err != nil { t.Logf("api did not close cleanly: %s", err.Error()) @@ -2832,7 +2830,7 @@ func TestValidateKeyspace(t *testing.T) { err := opts.RBAC.Reify() require.NoError(t, err, "failed to reify authorization rules: %+v", opts.RBAC.Rules) - api := vtadmin.NewAPI(testClusters(t), opts, collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion) + api := vtadmin.NewAPI(vtenv.NewTestEnv(), testClusters(t), opts) t.Cleanup(func() { if err := api.Close(); err != nil { t.Logf("api did not close cleanly: %s", err.Error()) @@ -2893,7 +2891,7 @@ func TestValidateSchemaKeyspace(t *testing.T) { err := opts.RBAC.Reify() require.NoError(t, err, "failed to reify authorization rules: %+v", opts.RBAC.Rules) - api := vtadmin.NewAPI(testClusters(t), opts, collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion) + api := vtadmin.NewAPI(vtenv.NewTestEnv(), testClusters(t), opts) t.Cleanup(func() { if err := api.Close(); err != nil { t.Logf("api did not close cleanly: %s", err.Error()) @@ -2954,7 +2952,7 @@ func TestValidateVersionKeyspace(t *testing.T) { err := opts.RBAC.Reify() require.NoError(t, err, "failed to reify authorization rules: %+v", opts.RBAC.Rules) - api := vtadmin.NewAPI(testClusters(t), opts, collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion) + api := vtadmin.NewAPI(vtenv.NewTestEnv(), testClusters(t), opts) t.Cleanup(func() { if err := api.Close(); err != nil { t.Logf("api did not close cleanly: %s", err.Error()) diff --git a/go/vt/vtadmin/api_test.go b/go/vt/vtadmin/api_test.go index e310a0237e3..a46a5d54299 100644 --- a/go/vt/vtadmin/api_test.go +++ b/go/vt/vtadmin/api_test.go @@ -32,10 +32,7 @@ import ( "github.com/stretchr/testify/require" "google.golang.org/protobuf/proto" - "vitess.io/vitess/go/vt/sqlparser" - - "vitess.io/vitess/go/mysql/collations" - "vitess.io/vitess/go/mysql/config" + "vitess.io/vitess/go/vt/vtenv" _flag "vitess.io/vitess/go/internal/flag" "vitess.io/vitess/go/test/utils" @@ -560,7 +557,7 @@ func TestFindSchema(t *testing.T) { clusters[i] = vtadmintestutil.BuildCluster(t, cfg) } - api := NewAPI(clusters, Options{}, collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion) + api := NewAPI(vtenv.NewTestEnv(), clusters, Options{}) defer api.Close() resp, err := api.FindSchema(ctx, tt.req) @@ -770,7 +767,7 @@ func TestFindSchema(t *testing.T) { }, ) - api := NewAPI([]*cluster.Cluster{c1, c2}, Options{}, collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion) + api := NewAPI(vtenv.NewTestEnv(), []*cluster.Cluster{c1, c2}, Options{}) defer api.Close() schema, err := api.FindSchema(ctx, &vtadminpb.FindSchemaRequest{ @@ -870,7 +867,7 @@ func TestGetClusters(t *testing.T) { t.Run(tt.name, func(t *testing.T) { t.Parallel() - api := NewAPI(tt.clusters, Options{}, collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion) + api := NewAPI(vtenv.NewTestEnv(), tt.clusters, Options{}) resp, err := api.GetClusters(ctx, &vtadminpb.GetClustersRequest{}) assert.NoError(t, err) @@ -948,7 +945,7 @@ func TestGetGates(t *testing.T) { }, } - api := NewAPI([]*cluster.Cluster{cluster1, cluster2}, Options{}, collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion) + api := NewAPI(vtenv.NewTestEnv(), []*cluster.Cluster{cluster1, cluster2}, Options{}) ctx := context.Background() resp, err := api.GetGates(ctx, &vtadminpb.GetGatesRequest{}) @@ -1070,7 +1067,7 @@ func TestGetKeyspace(t *testing.T) { testutil.AddShards(ctx, t, ts, shards...) topos[i] = ts vtctlds[i] = testutil.NewVtctldServerWithTabletManagerClient(t, ts, nil, func(ts *topo.Server) vtctlservicepb.VtctldServer { - return grpcvtctldserver.NewVtctldServer(ts, collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion) + return grpcvtctldserver.NewVtctldServer(vtenv.NewTestEnv(), ts) }) } @@ -1086,7 +1083,7 @@ func TestGetKeyspace(t *testing.T) { }) } - api := NewAPI(clusters, Options{}, collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion) + api := NewAPI(vtenv.NewTestEnv(), clusters, Options{}) ks, err := api.GetKeyspace(ctx, tt.req) if tt.shouldErr { assert.Error(t, err) @@ -1314,10 +1311,10 @@ func TestGetKeyspaces(t *testing.T) { servers := []vtctlservicepb.VtctldServer{ testutil.NewVtctldServerWithTabletManagerClient(t, topos[0], nil, func(ts *topo.Server) vtctlservicepb.VtctldServer { - return grpcvtctldserver.NewVtctldServer(ts, collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion) + return grpcvtctldserver.NewVtctldServer(vtenv.NewTestEnv(), ts) }), testutil.NewVtctldServerWithTabletManagerClient(t, topos[1], nil, func(ts *topo.Server) vtctlservicepb.VtctldServer { - return grpcvtctldserver.NewVtctldServer(ts, collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion) + return grpcvtctldserver.NewVtctldServer(vtenv.NewTestEnv(), ts) }), } @@ -1339,7 +1336,7 @@ func TestGetKeyspaces(t *testing.T) { }), } - api := NewAPI(clusters, Options{}, collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion) + api := NewAPI(vtenv.NewTestEnv(), clusters, Options{}) resp, err := api.GetKeyspaces(ctx, tt.req) require.NoError(t, err) @@ -1549,7 +1546,7 @@ func TestGetSchema(t *testing.T) { defer cancel() vtctld := testutil.NewVtctldServerWithTabletManagerClient(t, tt.ts, tt.tmc, func(ts *topo.Server) vtctlservicepb.VtctldServer { - return grpcvtctldserver.NewVtctldServer(ts, collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion) + return grpcvtctldserver.NewVtctldServer(vtenv.NewTestEnv(), ts) }) testutil.AddTablets(ctx, t, tt.ts, nil, vtadmintestutil.TopodataTabletsFromVTAdminTablets(tt.tablets)...) @@ -1563,7 +1560,7 @@ func TestGetSchema(t *testing.T) { VtctldClient: client, Tablets: tt.tablets, }) - api := NewAPI([]*cluster.Cluster{c}, Options{}, collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion) + api := NewAPI(vtenv.NewTestEnv(), []*cluster.Cluster{c}, Options{}) defer api.Close() resp, err := api.GetSchema(ctx, tt.req) @@ -1693,7 +1690,7 @@ func TestGetSchema(t *testing.T) { }, ) - api := NewAPI([]*cluster.Cluster{c1, c2}, Options{}, collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion) + api := NewAPI(vtenv.NewTestEnv(), []*cluster.Cluster{c1, c2}, Options{}) defer api.Close() schema, err := api.GetSchema(ctx, &vtadminpb.GetSchemaRequest{ @@ -2203,10 +2200,10 @@ func TestGetSchemas(t *testing.T) { vtctlds := []vtctlservicepb.VtctldServer{ testutil.NewVtctldServerWithTabletManagerClient(t, topos[0], &tmc, func(ts *topo.Server) vtctlservicepb.VtctldServer { - return grpcvtctldserver.NewVtctldServer(ts, collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion) + return grpcvtctldserver.NewVtctldServer(vtenv.NewTestEnv(), ts) }), testutil.NewVtctldServerWithTabletManagerClient(t, topos[1], &tmc, func(ts *topo.Server) vtctlservicepb.VtctldServer { - return grpcvtctldserver.NewVtctldServer(ts, collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion) + return grpcvtctldserver.NewVtctldServer(vtenv.NewTestEnv(), ts) }), } @@ -2247,7 +2244,7 @@ func TestGetSchemas(t *testing.T) { }) } - api := NewAPI(clusters, Options{}, collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion) + api := NewAPI(vtenv.NewTestEnv(), clusters, Options{}) defer api.Close() resp, err := api.GetSchemas(ctx, tt.req) @@ -2468,7 +2465,7 @@ func TestGetSchemas(t *testing.T) { }, ) - api := NewAPI([]*cluster.Cluster{c1, c2}, Options{}, collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion) + api := NewAPI(vtenv.NewTestEnv(), []*cluster.Cluster{c1, c2}, Options{}) defer api.Close() resp, err := api.GetSchemas(context.Background(), &vtadminpb.GetSchemasRequest{ @@ -2642,7 +2639,7 @@ func TestGetSrvKeyspace(t *testing.T) { toposerver := memorytopo.NewServer(ctx, tt.cells...) vtctldserver := testutil.NewVtctldServerWithTabletManagerClient(t, toposerver, &tmc, func(ts *topo.Server) vtctlservicepb.VtctldServer { - return grpcvtctldserver.NewVtctldServer(ts, collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion) + return grpcvtctldserver.NewVtctldServer(vtenv.NewTestEnv(), ts) }) testutil.WithTestServer(t, vtctldserver, func(t *testing.T, vtctldClient vtctldclient.VtctldClient) { @@ -2661,7 +2658,7 @@ func TestGetSrvKeyspace(t *testing.T) { }), } - api := NewAPI(clusters, Options{}, collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion) + api := NewAPI(vtenv.NewTestEnv(), clusters, Options{}) resp, err := api.GetSrvKeyspace(ctx, tt.req) if tt.shouldErr { @@ -2806,7 +2803,7 @@ func TestGetSrvKeyspaces(t *testing.T) { } vtctldserver := testutil.NewVtctldServerWithTabletManagerClient(t, toposerver, &tmc, func(ts *topo.Server) vtctlservicepb.VtctldServer { - return grpcvtctldserver.NewVtctldServer(ts, collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion) + return grpcvtctldserver.NewVtctldServer(vtenv.NewTestEnv(), ts) }) testutil.WithTestServer(t, vtctldserver, func(t *testing.T, vtctldClient vtctldclient.VtctldClient) { @@ -2827,7 +2824,7 @@ func TestGetSrvKeyspaces(t *testing.T) { }), } - api := NewAPI(clusters, Options{}, collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion) + api := NewAPI(vtenv.NewTestEnv(), clusters, Options{}) resp, err := api.GetSrvKeyspaces(ctx, tt.req) if tt.shouldErr { @@ -2971,7 +2968,7 @@ func TestGetSrvVSchema(t *testing.T) { toposerver := memorytopo.NewServer(ctx, tt.cells...) vtctldserver := testutil.NewVtctldServerWithTabletManagerClient(t, toposerver, &tmc, func(ts *topo.Server) vtctlservicepb.VtctldServer { - return grpcvtctldserver.NewVtctldServer(ts, collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion) + return grpcvtctldserver.NewVtctldServer(vtenv.NewTestEnv(), ts) }) testutil.WithTestServer(t, vtctldserver, func(t *testing.T, vtctldClient vtctldclient.VtctldClient) { @@ -2990,7 +2987,7 @@ func TestGetSrvVSchema(t *testing.T) { }), } - api := NewAPI(clusters, Options{}, collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion) + api := NewAPI(vtenv.NewTestEnv(), clusters, Options{}) resp, err := api.GetSrvVSchema(ctx, tt.req) if tt.shouldErr { @@ -3265,7 +3262,7 @@ func TestGetSrvVSchemas(t *testing.T) { toposerver := memorytopo.NewServer(ctx, tt.cells...) vtctldserver := testutil.NewVtctldServerWithTabletManagerClient(t, toposerver, &tmc, func(ts *topo.Server) vtctlservicepb.VtctldServer { - return grpcvtctldserver.NewVtctldServer(ts, collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion) + return grpcvtctldserver.NewVtctldServer(vtenv.NewTestEnv(), ts) }) testutil.WithTestServer(t, vtctldserver, func(t *testing.T, vtctldClient vtctldclient.VtctldClient) { @@ -3284,7 +3281,7 @@ func TestGetSrvVSchemas(t *testing.T) { }), } - api := NewAPI(clusters, Options{}, collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion) + api := NewAPI(vtenv.NewTestEnv(), clusters, Options{}) resp, err := api.GetSrvVSchemas(ctx, tt.req) if tt.shouldErr { @@ -3555,7 +3552,7 @@ func TestGetTablet(t *testing.T) { }) } - api := NewAPI(clusters, Options{}, collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion) + api := NewAPI(vtenv.NewTestEnv(), clusters, Options{}) resp, err := api.GetTablet(ctx, tt.req) if tt.shouldErr { assert.Error(t, err) @@ -3750,7 +3747,7 @@ func TestGetTablets(t *testing.T) { }) } - api := NewAPI(clusters, Options{}, collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion) + api := NewAPI(vtenv.NewTestEnv(), clusters, Options{}) resp, err := api.GetTablets(ctx, tt.req) if tt.shouldErr { assert.Error(t, err) @@ -3881,7 +3878,7 @@ func TestGetVSchema(t *testing.T) { t.Parallel() clusters := []*cluster.Cluster{vtadmintestutil.BuildCluster(t, tt.clusterCfg)} - api := NewAPI(clusters, Options{}, collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion) + api := NewAPI(vtenv.NewTestEnv(), clusters, Options{}) resp, err := api.GetVSchema(ctx, tt.req) if tt.shouldErr { @@ -4211,7 +4208,7 @@ func TestGetVSchemas(t *testing.T) { } clusters := vtadmintestutil.BuildClusters(t, tt.clusterCfgs...) - api := NewAPI(clusters, Options{}, collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion) + api := NewAPI(vtenv.NewTestEnv(), clusters, Options{}) resp, err := api.GetVSchemas(ctx, tt.req) if tt.shouldErr { @@ -4295,7 +4292,7 @@ func TestGetVtctlds(t *testing.T) { }, } - api := NewAPI([]*cluster.Cluster{cluster1, cluster2}, Options{}, collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion) + api := NewAPI(vtenv.NewTestEnv(), []*cluster.Cluster{cluster1, cluster2}, Options{}) ctx := context.Background() resp, err := api.GetVtctlds(ctx, &vtadminpb.GetVtctldsRequest{}) @@ -4427,7 +4424,7 @@ func TestGetWorkflow(t *testing.T) { t.Run(tt.name, func(t *testing.T) { t.Parallel() - api := NewAPI(vtadmintestutil.BuildClusters(t, tt.cfgs...), Options{}, collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion) + api := NewAPI(vtenv.NewTestEnv(), vtadmintestutil.BuildClusters(t, tt.cfgs...), Options{}) resp, err := api.GetWorkflow(ctx, tt.req) if tt.shouldErr { @@ -4866,7 +4863,7 @@ func TestGetWorkflows(t *testing.T) { t.Run(tt.name, func(t *testing.T) { t.Parallel() - api := NewAPI(vtadmintestutil.BuildClusters(t, tt.cfgs...), Options{}, collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion) + api := NewAPI(vtenv.NewTestEnv(), vtadmintestutil.BuildClusters(t, tt.cfgs...), Options{}) resp, err := api.GetWorkflows(ctx, tt.req) if tt.shouldErr { @@ -5117,7 +5114,7 @@ func TestVTExplain(t *testing.T) { } vtctldserver := testutil.NewVtctldServerWithTabletManagerClient(t, toposerver, &tmc, func(ts *topo.Server) vtctlservicepb.VtctldServer { - return grpcvtctldserver.NewVtctldServer(ts, collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion) + return grpcvtctldserver.NewVtctldServer(vtenv.NewTestEnv(), ts) }) testutil.WithTestServer(t, vtctldserver, func(t *testing.T, vtctldClient vtctldclient.VtctldClient) { @@ -5156,7 +5153,7 @@ func TestVTExplain(t *testing.T) { }), } - api := NewAPI(clusters, Options{}, collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion) + api := NewAPI(vtenv.NewTestEnv(), clusters, Options{}) resp, err := api.VTExplain(ctx, tt.req) if tt.expectedError != nil { @@ -5358,7 +5355,7 @@ func TestServeHTTP(t *testing.T) { t.Run(tt.name, func(t *testing.T) { t.Parallel() - api := NewAPI(tt.clusters, Options{EnableDynamicClusters: tt.enableDynamicClusters}, collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion) + api := NewAPI(vtenv.NewTestEnv(), tt.clusters, Options{EnableDynamicClusters: tt.enableDynamicClusters}) // Copy the Cookie over to a new Request req := httptest.NewRequest(http.MethodGet, "/api/clusters", nil) diff --git a/go/vt/vtadmin/testutil/authztestgen/template.go b/go/vt/vtadmin/testutil/authztestgen/template.go index 3f5dbba62e5..4a829a54a61 100644 --- a/go/vt/vtadmin/testutil/authztestgen/template.go +++ b/go/vt/vtadmin/testutil/authztestgen/template.go @@ -45,15 +45,13 @@ import ( "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" - "vitess.io/vitess/go/mysql/collations" - "vitess.io/vitess/go/mysql/config" - "vitess.io/vitess/go/vt/sqlparser" "vitess.io/vitess/go/vt/topo/topoproto" "vitess.io/vitess/go/vt/vtadmin" "vitess.io/vitess/go/vt/vtadmin/cluster" "vitess.io/vitess/go/vt/vtadmin/rbac" "vitess.io/vitess/go/vt/vtadmin/testutil" "vitess.io/vitess/go/vt/vtadmin/vtctldclient/fakevtctldclient" + "vitess.io/vitess/go/vt/vtenv" logutilpb "vitess.io/vitess/go/vt/proto/logutil" mysqlctlpb "vitess.io/vitess/go/vt/proto/mysqlctl" @@ -91,7 +89,7 @@ func Test{{ .Method }}(t *testing.T) { require.NoError(t, err, "failed to reify authorization rules: %+v", opts.RBAC.Rules) {{ if not .SerializeCases }} - api := vtadmin.NewAPI(testClusters(t), opts, collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion) + api := vtadmin.NewAPI(vtenv.NewTestEnv(), testClusters(t), opts) t.Cleanup(func() { if err := api.Close(); err != nil { t.Logf("api did not close cleanly: %s", err.Error()) @@ -104,7 +102,7 @@ func Test{{ .Method }}(t *testing.T) { t.Run("{{ .Name }}", func(t *testing.T) { t.Parallel() {{ if $test.SerializeCases }} - api := vtadmin.NewAPI(testClusters(t), opts, collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion) + api := vtadmin.NewAPI(vtenv.NewTestEnv(), testClusters(t), opts) t.Cleanup(func() { if err := api.Close(); err != nil { t.Logf("api did not close cleanly: %s", err.Error()) diff --git a/go/vt/vtadmin/testutil/cluster.go b/go/vt/vtadmin/testutil/cluster.go index 379933e1cda..793ef3b2142 100644 --- a/go/vt/vtadmin/testutil/cluster.go +++ b/go/vt/vtadmin/testutil/cluster.go @@ -27,10 +27,6 @@ import ( "github.com/stretchr/testify/require" "google.golang.org/grpc" - "vitess.io/vitess/go/mysql/collations" - "vitess.io/vitess/go/mysql/config" - "vitess.io/vitess/go/vt/sqlparser" - "vitess.io/vitess/go/vt/grpcclient" "vitess.io/vitess/go/vt/topo" "vitess.io/vitess/go/vt/topo/memorytopo" @@ -45,6 +41,7 @@ import ( grpcvtctldtestutil "vitess.io/vitess/go/vt/vtctl/grpcvtctldserver/testutil" "vitess.io/vitess/go/vt/vtctl/localvtctldclient" "vitess.io/vitess/go/vt/vtctl/vtctldclient" + "vitess.io/vitess/go/vt/vtenv" vtadminpb "vitess.io/vitess/go/vt/proto/vtadmin" vtctlservicepb "vitess.io/vitess/go/vt/proto/vtctlservice" @@ -173,7 +170,7 @@ func BuildIntegrationTestCluster(t testing.TB, ctx context.Context, c *vtadminpb ts, factory := memorytopo.NewServerAndFactory(ctx, cells...) vtctld := grpcvtctldtestutil.NewVtctldServerWithTabletManagerClient(t, ts, nil, func(ts *topo.Server) vtctlservicepb.VtctldServer { - return grpcvtctldserver.NewVtctldServer(ts, collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion) + return grpcvtctldserver.NewVtctldServer(vtenv.NewTestEnv(), ts) }) localclient := localvtctldclient.New(vtctld) diff --git a/go/vt/vtcombo/tablet_map.go b/go/vt/vtcombo/tablet_map.go index 6b3b44886e6..369a8138b5a 100644 --- a/go/vt/vtcombo/tablet_map.go +++ b/go/vt/vtcombo/tablet_map.go @@ -23,7 +23,6 @@ import ( "path" "time" - "vitess.io/vitess/go/mysql/collations" "vitess.io/vitess/go/sqltypes" "vitess.io/vitess/go/vt/dbconfigs" "vitess.io/vitess/go/vt/grpcclient" @@ -32,10 +31,10 @@ import ( "vitess.io/vitess/go/vt/logutil" "vitess.io/vitess/go/vt/mysqlctl" "vitess.io/vitess/go/vt/mysqlctl/tmutils" - "vitess.io/vitess/go/vt/sqlparser" "vitess.io/vitess/go/vt/topo" "vitess.io/vitess/go/vt/topo/topoproto" "vitess.io/vitess/go/vt/topotools" + "vitess.io/vitess/go/vt/vtenv" "vitess.io/vitess/go/vt/vterrors" "vitess.io/vitess/go/vt/vtgate/vindexes" "vitess.io/vitess/go/vt/vttablet/queryservice" @@ -77,6 +76,7 @@ var tabletMap map[uint32]*comboTablet // it to the map. If it's a primary tablet, it also issues a TER. func CreateTablet( ctx context.Context, + env *vtenv.Environment, ts *topo.Server, cell string, uid uint32, @@ -84,9 +84,6 @@ func CreateTablet( tabletType topodatapb.TabletType, mysqld mysqlctl.MysqlDaemon, dbcfgs *dbconfigs.DBConfigs, - collationEnv *collations.Environment, - parser *sqlparser.Parser, - mysqlVersion string, ) error { alias := &topodatapb.TabletAlias{ Cell: cell, @@ -94,7 +91,7 @@ func CreateTablet( } log.Infof("Creating %v tablet %v for %v/%v", tabletType, topoproto.TabletAliasString(alias), keyspace, shard) - controller := tabletserver.NewServer(ctx, topoproto.TabletAliasString(alias), ts, alias, collationEnv, parser, mysqlVersion) + controller := tabletserver.NewServer(ctx, env, topoproto.TabletAliasString(alias), ts, alias) initTabletType := tabletType if tabletType == topodatapb.TabletType_PRIMARY { initTabletType = topodatapb.TabletType_REPLICA @@ -105,13 +102,11 @@ func CreateTablet( } tm := &tabletmanager.TabletManager{ BatchCtx: context.Background(), + Env: env, TopoServer: ts, MysqlDaemon: mysqld, DBConfigs: dbcfgs, QueryServiceControl: controller, - CollationEnv: collationEnv, - SQLParser: parser, - MySQLVersion: mysqlVersion, } tablet := &topodatapb.Tablet{ Alias: alias, @@ -171,15 +166,13 @@ func InitRoutingRules( // InitTabletMap creates the action tms and associated data structures // for all tablets, based on the vttest proto parameter. func InitTabletMap( + env *vtenv.Environment, ts *topo.Server, tpb *vttestpb.VTTestTopology, mysqld mysqlctl.MysqlDaemon, dbcfgs *dbconfigs.DBConfigs, schemaDir string, ensureDatabase bool, - collationEnv *collations.Environment, - parser *sqlparser.Parser, - mysqlVersion string, ) (uint32, error) { tabletMap = make(map[uint32]*comboTablet) @@ -195,11 +188,11 @@ func InitTabletMap( }) // iterate through the keyspaces - wr := wrangler.New(logutil.NewConsoleLogger(), ts, nil, collationEnv, parser, mysqlVersion) + wr := wrangler.New(env, logutil.NewConsoleLogger(), ts, nil) var uid uint32 = 1 for _, kpb := range tpb.Keyspaces { var err error - uid, err = CreateKs(ctx, ts, tpb, mysqld, dbcfgs, schemaDir, kpb, ensureDatabase, uid, wr, collationEnv, parser, mysqlVersion) + uid, err = CreateKs(ctx, env, ts, tpb, mysqld, dbcfgs, schemaDir, kpb, ensureDatabase, uid, wr) if err != nil { return 0, err } @@ -290,6 +283,7 @@ func DeleteKs( // CreateKs creates keyspace, shards and tablets with mysql database func CreateKs( ctx context.Context, + env *vtenv.Environment, ts *topo.Server, tpb *vttestpb.VTTestTopology, mysqld mysqlctl.MysqlDaemon, @@ -299,9 +293,6 @@ func CreateKs( ensureDatabase bool, uid uint32, wr *wrangler.Wrangler, - collationEnv *collations.Environment, - parser *sqlparser.Parser, - mysqlVersion string, ) (uint32, error) { keyspace := kpb.Name @@ -351,7 +342,7 @@ func CreateKs( replicas-- // create the primary - if err := CreateTablet(ctx, ts, cell, uid, keyspace, shard, dbname, topodatapb.TabletType_PRIMARY, mysqld, dbcfgs.Clone(), collationEnv, parser, mysqlVersion); err != nil { + if err := CreateTablet(ctx, env, ts, cell, uid, keyspace, shard, dbname, topodatapb.TabletType_PRIMARY, mysqld, dbcfgs.Clone()); err != nil { return 0, err } uid++ @@ -359,7 +350,7 @@ func CreateKs( for i := 0; i < replicas; i++ { // create a replica tablet - if err := CreateTablet(ctx, ts, cell, uid, keyspace, shard, dbname, topodatapb.TabletType_REPLICA, mysqld, dbcfgs.Clone(), collationEnv, parser, mysqlVersion); err != nil { + if err := CreateTablet(ctx, env, ts, cell, uid, keyspace, shard, dbname, topodatapb.TabletType_REPLICA, mysqld, dbcfgs.Clone()); err != nil { return 0, err } uid++ @@ -367,7 +358,7 @@ func CreateKs( for i := 0; i < rdonlys; i++ { // create a rdonly tablet - if err := CreateTablet(ctx, ts, cell, uid, keyspace, shard, dbname, topodatapb.TabletType_RDONLY, mysqld, dbcfgs.Clone(), collationEnv, parser, mysqlVersion); err != nil { + if err := CreateTablet(ctx, env, ts, cell, uid, keyspace, shard, dbname, topodatapb.TabletType_RDONLY, mysqld, dbcfgs.Clone()); err != nil { return 0, err } uid++ diff --git a/go/vt/vtctl/endtoend/get_schema_test.go b/go/vt/vtctl/endtoend/get_schema_test.go index 8365d89425f..a9829a193f3 100644 --- a/go/vt/vtctl/endtoend/get_schema_test.go +++ b/go/vt/vtctl/endtoend/get_schema_test.go @@ -4,10 +4,8 @@ import ( "context" "testing" - "vitess.io/vitess/go/mysql/collations" - "vitess.io/vitess/go/mysql/config" "vitess.io/vitess/go/test/utils" - "vitess.io/vitess/go/vt/sqlparser" + "vitess.io/vitess/go/vt/vtenv" "github.com/google/uuid" "github.com/stretchr/testify/assert" @@ -164,7 +162,7 @@ func TestGetSchema(t *testing.T) { logger := logutil.NewMemoryLogger() - err := vtctl.RunCommand(ctx, wrangler.New(logger, topo, &tmc, collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion), []string{ + err := vtctl.RunCommand(ctx, wrangler.New(vtenv.NewTestEnv(), logger, topo, &tmc), []string{ "GetSchema", topoproto.TabletAliasString(tablet.Alias), }) @@ -204,7 +202,7 @@ func TestGetSchema(t *testing.T) { }, } - err = vtctl.RunCommand(ctx, wrangler.New(logger, topo, &tmc, collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion), []string{ + err = vtctl.RunCommand(ctx, wrangler.New(vtenv.NewTestEnv(), logger, topo, &tmc), []string{ "GetSchema", "--table_sizes_only", topoproto.TabletAliasString(tablet.Alias), diff --git a/go/vt/vtctl/endtoend/onlineddl_show_test.go b/go/vt/vtctl/endtoend/onlineddl_show_test.go index 98c79275497..500f33aa3ce 100644 --- a/go/vt/vtctl/endtoend/onlineddl_show_test.go +++ b/go/vt/vtctl/endtoend/onlineddl_show_test.go @@ -9,15 +9,11 @@ import ( "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" - "vitess.io/vitess/go/vt/sqlparser" - - "vitess.io/vitess/go/mysql/collations" - "vitess.io/vitess/go/mysql/config" - "vitess.io/vitess/go/vt/logutil" "vitess.io/vitess/go/vt/topo/memorytopo" "vitess.io/vitess/go/vt/vtctl" "vitess.io/vitess/go/vt/vtctl/grpcvtctldserver/testutil" + "vitess.io/vitess/go/vt/vtenv" "vitess.io/vitess/go/vt/vttablet/tmclient" "vitess.io/vitess/go/vt/vttablet/tmclienttest" "vitess.io/vitess/go/vt/wrangler" @@ -124,7 +120,7 @@ func onlineDDLTest(t *testing.T, args []string, expectedQuery string) { tmclienttest.SetProtocol("go.vt.vtctl.endtoend", t.Name()) logger := logutil.NewMemoryLogger() - wr := wrangler.New(logger, fakeTopo, &tmc, collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion) + wr := wrangler.New(vtenv.NewTestEnv(), logger, fakeTopo, &tmc) err := vtctl.RunCommand(ctx, wr, args) assert.Error(t, err) diff --git a/go/vt/vtctl/grpcvtctlclient/client_test.go b/go/vt/vtctl/grpcvtctlclient/client_test.go index efc5dffd68a..d065a706c65 100644 --- a/go/vt/vtctl/grpcvtctlclient/client_test.go +++ b/go/vt/vtctl/grpcvtctlclient/client_test.go @@ -28,13 +28,11 @@ import ( "github.com/stretchr/testify/require" "google.golang.org/grpc" - "vitess.io/vitess/go/mysql/collations" - "vitess.io/vitess/go/mysql/config" "vitess.io/vitess/go/vt/grpcclient" "vitess.io/vitess/go/vt/servenv" - "vitess.io/vitess/go/vt/sqlparser" "vitess.io/vitess/go/vt/vtctl/grpcvtctlserver" "vitess.io/vitess/go/vt/vtctl/vtctlclienttest" + "vitess.io/vitess/go/vt/vtenv" vtctlservicepb "vitess.io/vitess/go/vt/proto/vtctlservice" ) @@ -55,7 +53,7 @@ func TestVtctlServer(t *testing.T) { // Create a gRPC server and listen on the port server := grpc.NewServer() - vtctlservicepb.RegisterVtctlServer(server, grpcvtctlserver.NewVtctlServer(ts, collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion)) + vtctlservicepb.RegisterVtctlServer(server, grpcvtctlserver.NewVtctlServer(vtenv.NewTestEnv(), ts)) go server.Serve(listener) // Create a VtctlClient gRPC client to talk to the fake server @@ -89,7 +87,7 @@ func TestVtctlAuthClient(t *testing.T) { opts = append(opts, grpc.UnaryInterceptor(servenv.FakeAuthUnaryInterceptor)) server := grpc.NewServer(opts...) - vtctlservicepb.RegisterVtctlServer(server, grpcvtctlserver.NewVtctlServer(ts, collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion)) + vtctlservicepb.RegisterVtctlServer(server, grpcvtctlserver.NewVtctlServer(vtenv.NewTestEnv(), ts)) go server.Serve(listener) authJSON := `{ diff --git a/go/vt/vtctl/grpcvtctldclient/client_test.go b/go/vt/vtctl/grpcvtctldclient/client_test.go index 398406894ce..1de9b6c895c 100644 --- a/go/vt/vtctl/grpcvtctldclient/client_test.go +++ b/go/vt/vtctl/grpcvtctldclient/client_test.go @@ -23,15 +23,13 @@ import ( "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" - "vitess.io/vitess/go/mysql/collations" - "vitess.io/vitess/go/mysql/config" "vitess.io/vitess/go/test/utils" - "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" "vitess.io/vitess/go/vt/vtctl/grpcvtctldserver/testutil" "vitess.io/vitess/go/vt/vtctl/vtctldclient" + "vitess.io/vitess/go/vt/vtenv" topodatapb "vitess.io/vitess/go/vt/proto/topodata" vtctldatapb "vitess.io/vitess/go/vt/proto/vtctldata" @@ -44,7 +42,7 @@ func TestFindAllShardsInKeyspace(t *testing.T) { ts := memorytopo.NewServer(ctx, "cell1") defer ts.Close() vtctld := testutil.NewVtctldServerWithTabletManagerClient(t, ts, nil, func(ts *topo.Server) vtctlservicepb.VtctldServer { - return grpcvtctldserver.NewVtctldServer(ts, collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion) + return grpcvtctldserver.NewVtctldServer(vtenv.NewTestEnv(), ts) }) testutil.WithTestServer(t, vtctld, func(t *testing.T, client vtctldclient.VtctldClient) { @@ -91,7 +89,7 @@ func TestGetKeyspace(t *testing.T) { ts := memorytopo.NewServer(ctx, "cell1") defer ts.Close() vtctld := testutil.NewVtctldServerWithTabletManagerClient(t, ts, nil, func(ts *topo.Server) vtctlservicepb.VtctldServer { - return grpcvtctldserver.NewVtctldServer(ts, collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion) + return grpcvtctldserver.NewVtctldServer(vtenv.NewTestEnv(), ts) }) testutil.WithTestServer(t, vtctld, func(t *testing.T, client vtctldclient.VtctldClient) { @@ -120,7 +118,7 @@ func TestGetKeyspaces(t *testing.T) { ts := memorytopo.NewServer(ctx, "cell1") defer ts.Close() vtctld := testutil.NewVtctldServerWithTabletManagerClient(t, ts, nil, func(ts *topo.Server) vtctlservicepb.VtctldServer { - return grpcvtctldserver.NewVtctldServer(ts, collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion) + return grpcvtctldserver.NewVtctldServer(vtenv.NewTestEnv(), ts) }) testutil.WithTestServer(t, vtctld, func(t *testing.T, client vtctldclient.VtctldClient) { diff --git a/go/vt/vtctl/grpcvtctldserver/endtoend/init_shard_primary_test.go b/go/vt/vtctl/grpcvtctldserver/endtoend/init_shard_primary_test.go index 095673a0ca3..f83861d7fc8 100644 --- a/go/vt/vtctl/grpcvtctldserver/endtoend/init_shard_primary_test.go +++ b/go/vt/vtctl/grpcvtctldserver/endtoend/init_shard_primary_test.go @@ -21,11 +21,9 @@ import ( "fmt" "testing" - "vitess.io/vitess/go/mysql/collations" - "vitess.io/vitess/go/mysql/config" "vitess.io/vitess/go/test/utils" "vitess.io/vitess/go/vt/mysqlctl" - "vitess.io/vitess/go/vt/sqlparser" + "vitess.io/vitess/go/vt/vtenv" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" @@ -49,7 +47,7 @@ func TestInitShardPrimary(t *testing.T) { ts := memorytopo.NewServer(ctx, "cell1") tmc := tmclient.NewTabletManagerClient() defer tmc.Close() - wr := wrangler.New(logutil.NewConsoleLogger(), ts, tmc, collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion) + wr := wrangler.New(vtenv.NewTestEnv(), logutil.NewConsoleLogger(), ts, tmc) primaryDb := fakesqldb.New(t) defer primaryDb.Close() @@ -96,7 +94,7 @@ func TestInitShardPrimary(t *testing.T) { tablet.TM.QueryServiceControl.(*tabletservermock.Controller).SetQueryServiceEnabledForTests(true) } - vtctld := grpcvtctldserver.NewVtctldServer(ts, collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion) + vtctld := grpcvtctldserver.NewVtctldServer(vtenv.NewTestEnv(), ts) resp, err := vtctld.InitShardPrimary(context.Background(), &vtctldatapb.InitShardPrimaryRequest{ Keyspace: tablet1.Tablet.Keyspace, Shard: tablet1.Tablet.Shard, @@ -112,7 +110,7 @@ func TestInitShardPrimaryNoFormerPrimary(t *testing.T) { defer cancel() ts := memorytopo.NewServer(ctx, "cell1") tmc := tmclient.NewTabletManagerClient() - wr := wrangler.New(logutil.NewConsoleLogger(), ts, tmc, collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion) + wr := wrangler.New(vtenv.NewTestEnv(), logutil.NewConsoleLogger(), ts, tmc) primaryDb := fakesqldb.New(t) defer primaryDb.Close() @@ -151,7 +149,7 @@ func TestInitShardPrimaryNoFormerPrimary(t *testing.T) { tablet.TM.QueryServiceControl.(*tabletservermock.Controller).SetQueryServiceEnabledForTests(true) } - vtctld := grpcvtctldserver.NewVtctldServer(ts, collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion) + vtctld := grpcvtctldserver.NewVtctldServer(vtenv.NewTestEnv(), ts) _, err := vtctld.InitShardPrimary(context.Background(), &vtctldatapb.InitShardPrimaryRequest{ Keyspace: tablet1.Tablet.Keyspace, Shard: tablet1.Tablet.Shard, diff --git a/go/vt/vtctl/grpcvtctldserver/server.go b/go/vt/vtctl/grpcvtctldserver/server.go index e325df180aa..966e97ca3f6 100644 --- a/go/vt/vtctl/grpcvtctldserver/server.go +++ b/go/vt/vtctl/grpcvtctldserver/server.go @@ -34,8 +34,6 @@ import ( "google.golang.org/grpc" "vitess.io/vitess/go/event" - "vitess.io/vitess/go/mysql/collations" - "vitess.io/vitess/go/mysql/config" "vitess.io/vitess/go/netutil" "vitess.io/vitess/go/protoutil" "vitess.io/vitess/go/sets" @@ -62,6 +60,7 @@ import ( "vitess.io/vitess/go/vt/vtctl/reparentutil" "vitess.io/vitess/go/vt/vtctl/schematools" "vitess.io/vitess/go/vt/vtctl/workflow" + "vitess.io/vitess/go/vt/vtenv" "vitess.io/vitess/go/vt/vterrors" "vitess.io/vitess/go/vt/vtgate/vindexes" "vitess.io/vitess/go/vt/vttablet/tmclient" @@ -94,13 +93,13 @@ type VtctldServer struct { } // NewVtctldServer returns a new VtctldServer for the given topo server. -func NewVtctldServer(ts *topo.Server, collationEnv *collations.Environment, parser *sqlparser.Parser, mysqlVersion string) *VtctldServer { +func NewVtctldServer(env *vtenv.Environment, ts *topo.Server) *VtctldServer { tmc := tmclient.NewTabletManagerClient() return &VtctldServer{ ts: ts, tmc: tmc, - ws: workflow.NewServer(ts, tmc, collationEnv, parser, mysqlVersion), + ws: workflow.NewServer(env, ts, tmc), } } @@ -110,7 +109,7 @@ func NewTestVtctldServer(ts *topo.Server, tmc tmclient.TabletManagerClient) *Vtc return &VtctldServer{ ts: ts, tmc: tmc, - ws: workflow.NewServer(ts, tmc, collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion), + ws: workflow.NewServer(vtenv.NewTestEnv(), ts, tmc), } } @@ -4991,8 +4990,8 @@ func (s *VtctldServer) WorkflowUpdate(ctx context.Context, req *vtctldatapb.Work } // StartServer registers a VtctldServer for RPCs on the given gRPC server. -func StartServer(s *grpc.Server, ts *topo.Server, collationEnv *collations.Environment, parser *sqlparser.Parser, mysqlVersion string) { - vtctlservicepb.RegisterVtctldServer(s, NewVtctldServer(ts, collationEnv, parser, mysqlVersion)) +func StartServer(s *grpc.Server, env *vtenv.Environment, ts *topo.Server) { + vtctlservicepb.RegisterVtctldServer(s, NewVtctldServer(env, ts)) } // getTopologyCell is a helper method that returns a topology cell given its path. diff --git a/go/vt/vtctl/grpcvtctldserver/server_slow_test.go b/go/vt/vtctl/grpcvtctldserver/server_slow_test.go index c2625925867..987ecf9d951 100644 --- a/go/vt/vtctl/grpcvtctldserver/server_slow_test.go +++ b/go/vt/vtctl/grpcvtctldserver/server_slow_test.go @@ -24,10 +24,6 @@ import ( "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" - "vitess.io/vitess/go/vt/sqlparser" - - "vitess.io/vitess/go/mysql/collations" - "vitess.io/vitess/go/mysql/config" "vitess.io/vitess/go/mysql/replication" "vitess.io/vitess/go/mysql" @@ -35,6 +31,7 @@ import ( "vitess.io/vitess/go/vt/topo" "vitess.io/vitess/go/vt/topo/memorytopo" "vitess.io/vitess/go/vt/vtctl/grpcvtctldserver/testutil" + "vitess.io/vitess/go/vt/vtenv" "vitess.io/vitess/go/vt/vttablet/tmclient" replicationdatapb "vitess.io/vitess/go/vt/proto/replicationdata" @@ -314,7 +311,7 @@ func TestEmergencyReparentShardSlow(t *testing.T) { }, tt.tablets...) vtctld := testutil.NewVtctldServerWithTabletManagerClient(t, ts, tt.tmc, func(ts *topo.Server) vtctlservicepb.VtctldServer { - return NewVtctldServer(ts, collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion) + return NewVtctldServer(vtenv.NewTestEnv(), ts) }) resp, err := vtctld.EmergencyReparentShard(ctx, tt.req) @@ -612,7 +609,7 @@ func TestPlannedReparentShardSlow(t *testing.T) { }, tt.tablets...) vtctld := testutil.NewVtctldServerWithTabletManagerClient(t, ts, tt.tmc, func(ts *topo.Server) vtctlservicepb.VtctldServer { - return NewVtctldServer(ts, collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion) + return NewVtctldServer(vtenv.NewTestEnv(), ts) }) resp, err := vtctld.PlannedReparentShard(ctx, tt.req) @@ -742,7 +739,7 @@ func TestSleepTablet(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { vtctld := testutil.NewVtctldServerWithTabletManagerClient(t, ts, &tt.tmc, func(ts *topo.Server) vtctlservicepb.VtctldServer { - return NewVtctldServer(ts, collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion) + return NewVtctldServer(vtenv.NewTestEnv(), ts) }) start := time.Now() diff --git a/go/vt/vtctl/grpcvtctldserver/server_test.go b/go/vt/vtctl/grpcvtctldserver/server_test.go index 3e934141ff5..4a70ec0408e 100644 --- a/go/vt/vtctl/grpcvtctldserver/server_test.go +++ b/go/vt/vtctl/grpcvtctldserver/server_test.go @@ -28,14 +28,11 @@ import ( "time" _flag "vitess.io/vitess/go/internal/flag" - "vitess.io/vitess/go/vt/sqlparser" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" "vitess.io/vitess/go/mysql" - "vitess.io/vitess/go/mysql/collations" - "vitess.io/vitess/go/mysql/config" "vitess.io/vitess/go/mysql/replication" "vitess.io/vitess/go/protoutil" "vitess.io/vitess/go/sqltypes" @@ -49,6 +46,7 @@ import ( "vitess.io/vitess/go/vt/vtctl/grpcvtctldserver/testutil" "vitess.io/vitess/go/vt/vtctl/localvtctldclient" "vitess.io/vitess/go/vt/vtctl/schematools" + "vitess.io/vitess/go/vt/vtenv" "vitess.io/vitess/go/vt/vttablet/tmclient" "vitess.io/vitess/go/vt/vttablet/tmclienttest" @@ -88,7 +86,7 @@ func TestPanicHandler(t *testing.T) { }() vtctld := testutil.NewVtctldServerWithTabletManagerClient(t, nil, nil, func(ts *topo.Server) vtctlservicepb.VtctldServer { - return NewVtctldServer(ts, collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion) + return NewVtctldServer(vtenv.NewTestEnv(), ts) }) _, err := vtctld.AddCellInfo(context.Background(), nil) @@ -144,7 +142,7 @@ func TestAddCellInfo(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { vtctld := testutil.NewVtctldServerWithTabletManagerClient(t, tt.ts, nil, func(ts *topo.Server) vtctlservicepb.VtctldServer { - return NewVtctldServer(ts, collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion) + return NewVtctldServer(vtenv.NewTestEnv(), ts) }) _, err := vtctld.AddCellInfo(ctx, tt.req) if tt.shouldErr { @@ -217,7 +215,7 @@ func TestAddCellsAlias(t *testing.T) { } vtctld := testutil.NewVtctldServerWithTabletManagerClient(t, tt.ts, nil, func(ts *topo.Server) vtctlservicepb.VtctldServer { - return NewVtctldServer(ts, collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion) + return NewVtctldServer(vtenv.NewTestEnv(), ts) }) _, err := vtctld.AddCellsAlias(ctx, tt.req) if tt.shouldErr { @@ -329,7 +327,7 @@ func TestApplyRoutingRules(t *testing.T) { } vtctld := testutil.NewVtctldServerWithTabletManagerClient(t, ts, nil, func(ts *topo.Server) vtctlservicepb.VtctldServer { - return NewVtctldServer(ts, collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion) + return NewVtctldServer(vtenv.NewTestEnv(), ts) }) _, err := vtctld.ApplyRoutingRules(ctx, tt.req) if tt.shouldErr { @@ -597,7 +595,7 @@ func TestApplyVSchema(t *testing.T) { defer cancel() ts := memorytopo.NewServer(ctx, "zone1") vtctld := testutil.NewVtctldServerWithTabletManagerClient(t, ts, nil, func(ts *topo.Server) vtctlservicepb.VtctldServer { - return NewVtctldServer(ts, collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion) + return NewVtctldServer(vtenv.NewTestEnv(), ts) }) testutil.AddKeyspace(ctx, t, ts, &vtctldatapb.Keyspace{ @@ -880,7 +878,7 @@ func TestBackup(t *testing.T) { testutil.AddTablet(ctx, t, tt.ts, tt.tablet, nil) } vtctld := testutil.NewVtctldServerWithTabletManagerClient(t, tt.ts, tt.tmc, func(ts *topo.Server) vtctlservicepb.VtctldServer { - return NewVtctldServer(ts, collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion) + return NewVtctldServer(vtenv.NewTestEnv(), ts) }) client := localvtctldclient.New(vtctld) stream, err := client.Backup(ctx, tt.req) @@ -1220,7 +1218,7 @@ func TestBackupShard(t *testing.T) { }, tt.tablets..., ) vtctld := testutil.NewVtctldServerWithTabletManagerClient(t, tt.ts, tt.tmc, func(ts *topo.Server) vtctlservicepb.VtctldServer { - return NewVtctldServer(ts, collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion) + return NewVtctldServer(vtenv.NewTestEnv(), ts) }) client := localvtctldclient.New(vtctld) stream, err := client.BackupShard(ctx, tt.req) @@ -1440,7 +1438,7 @@ func TestCancelSchemaMigration(t *testing.T) { }, test.tablets...) vtctld := testutil.NewVtctldServerWithTabletManagerClient(t, ts, test.tmc, func(ts *topo.Server) vtctlservicepb.VtctldServer { - return NewVtctldServer(ts, collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion) + return NewVtctldServer(vtenv.NewTestEnv(), ts) }) resp, err := vtctld.CancelSchemaMigration(ctx, test.req) @@ -1673,7 +1671,7 @@ func TestChangeTabletType(t *testing.T) { vtctld := testutil.NewVtctldServerWithTabletManagerClient(t, ts, &testutil.TabletManagerClient{ TopoServer: ts, }, func(ts *topo.Server) vtctlservicepb.VtctldServer { - return NewVtctldServer(ts, collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion) + return NewVtctldServer(vtenv.NewTestEnv(), ts) }) testutil.AddTablets(ctx, t, ts, &testutil.AddTabletOptions{ @@ -1721,7 +1719,7 @@ func TestChangeTabletType(t *testing.T) { vtctld := testutil.NewVtctldServerWithTabletManagerClient(t, ts, &testutil.TabletManagerClient{ TopoServer: nil, }, func(ts *topo.Server) vtctlservicepb.VtctldServer { - return NewVtctldServer(ts, collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion) + return NewVtctldServer(vtenv.NewTestEnv(), ts) }) testutil.AddTablet(ctx, t, ts, &topodatapb.Tablet{ @@ -1943,7 +1941,7 @@ func TestCleanupSchemaMigration(t *testing.T) { }, test.tablets...) vtctld := testutil.NewVtctldServerWithTabletManagerClient(t, ts, test.tmc, func(ts *topo.Server) vtctlservicepb.VtctldServer { - return NewVtctldServer(ts, collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion) + return NewVtctldServer(vtenv.NewTestEnv(), ts) }) resp, err := vtctld.CleanupSchemaMigration(ctx, test.req) @@ -2145,7 +2143,7 @@ func TestForceCutOverSchemaMigration(t *testing.T) { }, test.tablets...) vtctld := testutil.NewVtctldServerWithTabletManagerClient(t, ts, test.tmc, func(ts *topo.Server) vtctlservicepb.VtctldServer { - return NewVtctldServer(ts, collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion) + return NewVtctldServer(vtenv.NewTestEnv(), ts) }) resp, err := vtctld.ForceCutOverSchemaMigration(ctx, test.req) @@ -2349,7 +2347,7 @@ func TestCompleteSchemaMigration(t *testing.T) { }, test.tablets...) vtctld := testutil.NewVtctldServerWithTabletManagerClient(t, ts, test.tmc, func(ts *topo.Server) vtctlservicepb.VtctldServer { - return NewVtctldServer(ts, collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion) + return NewVtctldServer(vtenv.NewTestEnv(), ts) }) resp, err := vtctld.CompleteSchemaMigration(ctx, test.req) @@ -2605,7 +2603,7 @@ func TestCreateKeyspace(t *testing.T) { defer cancel() ts := memorytopo.NewServer(ctx, cells...) vtctld := testutil.NewVtctldServerWithTabletManagerClient(t, ts, nil, func(ts *topo.Server) vtctlservicepb.VtctldServer { - return NewVtctldServer(ts, collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion) + return NewVtctldServer(vtenv.NewTestEnv(), ts) }) for name, ks := range tt.topo { @@ -2883,7 +2881,7 @@ func TestCreateShard(t *testing.T) { defer cancel() ts, topofactory := memorytopo.NewServerAndFactory(ctx, "zone1") vtctld := testutil.NewVtctldServerWithTabletManagerClient(t, ts, nil, func(ts *topo.Server) vtctlservicepb.VtctldServer { - return NewVtctldServer(ts, collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion) + return NewVtctldServer(vtenv.NewTestEnv(), ts) }) for _, ks := range tt.keyspaces { @@ -2938,7 +2936,7 @@ func TestDeleteCellInfo(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { vtctld := testutil.NewVtctldServerWithTabletManagerClient(t, tt.ts, nil, func(ts *topo.Server) vtctlservicepb.VtctldServer { - return NewVtctldServer(ts, collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion) + return NewVtctldServer(vtenv.NewTestEnv(), ts) }) _, err := vtctld.DeleteCellInfo(ctx, tt.req) if tt.shouldErr { @@ -2999,7 +2997,7 @@ func TestDeleteCellsAlias(t *testing.T) { } vtctld := testutil.NewVtctldServerWithTabletManagerClient(t, tt.ts, nil, func(ts *topo.Server) vtctlservicepb.VtctldServer { - return NewVtctldServer(ts, collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion) + return NewVtctldServer(vtenv.NewTestEnv(), ts) }) _, err := vtctld.DeleteCellsAlias(ctx, tt.req) if tt.shouldErr { @@ -3231,7 +3229,7 @@ func TestDeleteKeyspace(t *testing.T) { ts, topofactory := memorytopo.NewServerAndFactory(ctx, cells...) defer ts.Close() vtctld := testutil.NewVtctldServerWithTabletManagerClient(t, ts, nil, func(ts *topo.Server) vtctlservicepb.VtctldServer { - return NewVtctldServer(ts, collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion) + return NewVtctldServer(vtenv.NewTestEnv(), ts) }) testutil.AddKeyspaces(ctx, t, ts, tt.keyspaces...) @@ -3745,7 +3743,7 @@ func TestDeleteShards(t *testing.T) { ts, topofactory := memorytopo.NewServerAndFactory(ctx, cells...) vtctld := testutil.NewVtctldServerWithTabletManagerClient(t, ts, nil, func(ts *topo.Server) vtctlservicepb.VtctldServer { - return NewVtctldServer(ts, collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion) + return NewVtctldServer(vtenv.NewTestEnv(), ts) }) testutil.AddShards(ctx, t, ts, tt.shards...) @@ -3888,7 +3886,7 @@ func TestDeleteSrvKeyspace(t *testing.T) { } vtctld := testutil.NewVtctldServerWithTabletManagerClient(t, ts, nil, func(ts *topo.Server) vtctlservicepb.VtctldServer { - return NewVtctldServer(ts, collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion) + return NewVtctldServer(vtenv.NewTestEnv(), ts) }) _, err := vtctld.DeleteSrvVSchema(ctx, tt.req) if tt.shouldErr { @@ -4349,7 +4347,7 @@ func TestDeleteTablets(t *testing.T) { defer cancel() ts, topofactory := memorytopo.NewServerAndFactory(ctx, "zone1") vtctld := testutil.NewVtctldServerWithTabletManagerClient(t, ts, nil, func(ts *topo.Server) vtctlservicepb.VtctldServer { - return NewVtctldServer(ts, collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion) + return NewVtctldServer(vtenv.NewTestEnv(), ts) }) // Setup tablets and shards @@ -4578,7 +4576,7 @@ func TestEmergencyReparentShard(t *testing.T) { }, tt.tablets...) vtctld := testutil.NewVtctldServerWithTabletManagerClient(t, tt.ts, tt.tmc, func(ts *topo.Server) vtctlservicepb.VtctldServer { - return NewVtctldServer(ts, collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion) + return NewVtctldServer(vtenv.NewTestEnv(), ts) }) resp, err := vtctld.EmergencyReparentShard(ctx, tt.req) @@ -4720,7 +4718,7 @@ func TestExecuteFetchAsApp(t *testing.T) { testutil.AddTablet(ctx, t, ts, tt.tablet, nil) vtctld := testutil.NewVtctldServerWithTabletManagerClient(t, ts, tt.tmc, func(ts *topo.Server) vtctlservicepb.VtctldServer { - return NewVtctldServer(ts, collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion) + return NewVtctldServer(vtenv.NewTestEnv(), ts) }) resp, err := vtctld.ExecuteFetchAsApp(ctx, tt.req) if tt.shouldErr { @@ -4847,7 +4845,7 @@ func TestExecuteFetchAsDBA(t *testing.T) { testutil.AddTablet(ctx, t, ts, tt.tablet, nil) vtctld := testutil.NewVtctldServerWithTabletManagerClient(t, ts, tt.tmc, func(ts *topo.Server) vtctlservicepb.VtctldServer { - return NewVtctldServer(ts, collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion) + return NewVtctldServer(vtenv.NewTestEnv(), ts) }) resp, err := vtctld.ExecuteFetchAsDBA(ctx, tt.req) if tt.shouldErr { @@ -5032,7 +5030,7 @@ func TestExecuteHook(t *testing.T) { t.Run(tt.name, func(t *testing.T) { testutil.AddTablets(ctx, t, tt.ts, nil, tt.tablets...) vtctld := testutil.NewVtctldServerWithTabletManagerClient(t, tt.ts, tt.tmc, func(ts *topo.Server) vtctlservicepb.VtctldServer { - return NewVtctldServer(ts, collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion) + return NewVtctldServer(vtenv.NewTestEnv(), ts) }) _, err := vtctld.ExecuteHook(ctx, tt.req) @@ -5053,7 +5051,7 @@ func TestFindAllShardsInKeyspace(t *testing.T) { defer cancel() ts := memorytopo.NewServer(ctx, "cell1") vtctld := testutil.NewVtctldServerWithTabletManagerClient(t, ts, nil, func(ts *topo.Server) vtctlservicepb.VtctldServer { - return NewVtctldServer(ts, collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion) + return NewVtctldServer(vtenv.NewTestEnv(), ts) }) ks := &vtctldatapb.Keyspace{ @@ -5095,7 +5093,7 @@ func TestGetBackups(t *testing.T) { defer cancel() ts := memorytopo.NewServer(ctx) vtctld := testutil.NewVtctldServerWithTabletManagerClient(t, ts, nil, func(ts *topo.Server) vtctlservicepb.VtctldServer { - return NewVtctldServer(ts, collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion) + return NewVtctldServer(vtenv.NewTestEnv(), ts) }) testutil.BackupStorage.Backups = map[string][]string{ @@ -5203,7 +5201,7 @@ func TestGetKeyspace(t *testing.T) { defer cancel() ts := memorytopo.NewServer(ctx, "cell1") vtctld := testutil.NewVtctldServerWithTabletManagerClient(t, ts, nil, func(ts *topo.Server) vtctlservicepb.VtctldServer { - return NewVtctldServer(ts, collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion) + return NewVtctldServer(vtenv.NewTestEnv(), ts) }) expected := &vtctldatapb.GetKeyspaceResponse{ @@ -5229,7 +5227,7 @@ func TestGetCellInfoNames(t *testing.T) { defer cancel() ts := memorytopo.NewServer(ctx, "cell1", "cell2", "cell3") vtctld := testutil.NewVtctldServerWithTabletManagerClient(t, ts, nil, func(ts *topo.Server) vtctlservicepb.VtctldServer { - return NewVtctldServer(ts, collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion) + return NewVtctldServer(vtenv.NewTestEnv(), ts) }) resp, err := vtctld.GetCellInfoNames(ctx, &vtctldatapb.GetCellInfoNamesRequest{}) @@ -5238,7 +5236,7 @@ func TestGetCellInfoNames(t *testing.T) { ts = memorytopo.NewServer(ctx) vtctld = testutil.NewVtctldServerWithTabletManagerClient(t, ts, nil, func(ts *topo.Server) vtctlservicepb.VtctldServer { - return NewVtctldServer(ts, collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion) + return NewVtctldServer(vtenv.NewTestEnv(), ts) }) resp, err = vtctld.GetCellInfoNames(ctx, &vtctldatapb.GetCellInfoNamesRequest{}) @@ -5247,7 +5245,7 @@ func TestGetCellInfoNames(t *testing.T) { ts, topofactory := memorytopo.NewServerAndFactory(ctx, "cell1") vtctld = testutil.NewVtctldServerWithTabletManagerClient(t, ts, nil, func(ts *topo.Server) vtctlservicepb.VtctldServer { - return NewVtctldServer(ts, collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion) + return NewVtctldServer(vtenv.NewTestEnv(), ts) }) topofactory.SetError(assert.AnError) @@ -5262,7 +5260,7 @@ func TestGetCellInfo(t *testing.T) { defer cancel() ts := memorytopo.NewServer(ctx) vtctld := testutil.NewVtctldServerWithTabletManagerClient(t, ts, nil, func(ts *topo.Server) vtctlservicepb.VtctldServer { - return NewVtctldServer(ts, collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion) + return NewVtctldServer(vtenv.NewTestEnv(), ts) }) expected := &topodatapb.CellInfo{ @@ -5290,7 +5288,7 @@ func TestGetCellsAliases(t *testing.T) { defer cancel() ts := memorytopo.NewServer(ctx, "c11", "c12", "c13", "c21", "c22") vtctld := testutil.NewVtctldServerWithTabletManagerClient(t, ts, nil, func(ts *topo.Server) vtctlservicepb.VtctldServer { - return NewVtctldServer(ts, collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion) + return NewVtctldServer(vtenv.NewTestEnv(), ts) }) alias1 := &topodatapb.CellsAlias{ @@ -5317,7 +5315,7 @@ func TestGetCellsAliases(t *testing.T) { ts, topofactory := memorytopo.NewServerAndFactory(ctx) vtctld = testutil.NewVtctldServerWithTabletManagerClient(t, ts, nil, func(ts *topo.Server) vtctlservicepb.VtctldServer { - return NewVtctldServer(ts, collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion) + return NewVtctldServer(vtenv.NewTestEnv(), ts) }) topofactory.SetError(assert.AnError) @@ -5398,7 +5396,7 @@ func TestGetFullStatus(t *testing.T) { ServerUuid: tt.serverUUID, }, }, func(ts *topo.Server) vtctlservicepb.VtctldServer { - return NewVtctldServer(ts, collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion) + return NewVtctldServer(vtenv.NewTestEnv(), ts) }) testutil.AddTablets(ctx, t, ts, &testutil.AddTabletOptions{ @@ -5424,7 +5422,7 @@ func TestGetKeyspaces(t *testing.T) { defer cancel() ts, topofactory := memorytopo.NewServerAndFactory(ctx, "cell1") vtctld := testutil.NewVtctldServerWithTabletManagerClient(t, ts, nil, func(ts *topo.Server) vtctlservicepb.VtctldServer { - return NewVtctldServer(ts, collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion) + return NewVtctldServer(vtenv.NewTestEnv(), ts) }) resp, err := vtctld.GetKeyspaces(ctx, &vtctldatapb.GetKeyspacesRequest{}) @@ -5592,7 +5590,7 @@ func TestGetPermissions(t *testing.T) { testutil.AddTablets(ctx, t, ts, nil, tt.tablets...) vtctld := testutil.NewVtctldServerWithTabletManagerClient(t, ts, &tt.tmc, func(ts *topo.Server) vtctlservicepb.VtctldServer { - return NewVtctldServer(ts, collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion) + return NewVtctldServer(vtenv.NewTestEnv(), ts) }) resp, err := vtctld.GetPermissions(ctx, tt.req) if tt.shouldErr { @@ -5668,7 +5666,7 @@ func TestGetRoutingRules(t *testing.T) { } vtctld := testutil.NewVtctldServerWithTabletManagerClient(t, ts, nil, func(ts *topo.Server) vtctlservicepb.VtctldServer { - return NewVtctldServer(ts, collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion) + return NewVtctldServer(vtenv.NewTestEnv(), ts) }) resp, err := vtctld.GetRoutingRules(ctx, &vtctldatapb.GetRoutingRulesRequest{}) if tt.shouldErr { @@ -5693,7 +5691,7 @@ func TestGetSchema(t *testing.T) { }{}, } vtctld := testutil.NewVtctldServerWithTabletManagerClient(t, ts, &tmc, func(ts *topo.Server) vtctlservicepb.VtctldServer { - return NewVtctldServer(ts, collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion) + return NewVtctldServer(vtenv.NewTestEnv(), ts) }) validAlias := &topodatapb.TabletAlias{ @@ -6058,7 +6056,7 @@ func TestGetSchemaMigrations(t *testing.T) { ts, factory := memorytopo.NewServerAndFactory(ctx, cells...) testutil.AddTablets(ctx, t, ts, &testutil.AddTabletOptions{AlsoSetShardPrimary: true}, test.tablets...) vtctld := testutil.NewVtctldServerWithTabletManagerClient(t, ts, tmc, func(ts *topo.Server) vtctlservicepb.VtctldServer { - return NewVtctldServer(ts, collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion) + return NewVtctldServer(vtenv.NewTestEnv(), ts) }) if test.failTopo { @@ -6149,7 +6147,7 @@ func TestGetShard(t *testing.T) { defer cancel() ts, topofactory := memorytopo.NewServerAndFactory(ctx, cells...) vtctld := testutil.NewVtctldServerWithTabletManagerClient(t, ts, nil, func(ts *topo.Server) vtctlservicepb.VtctldServer { - return NewVtctldServer(ts, collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion) + return NewVtctldServer(vtenv.NewTestEnv(), ts) }) testutil.AddShards(ctx, t, ts, tt.topo...) @@ -6286,7 +6284,7 @@ func TestGetSrvKeyspaceNames(t *testing.T) { } vtctld := testutil.NewVtctldServerWithTabletManagerClient(t, ts, nil, func(ts *topo.Server) vtctlservicepb.VtctldServer { - return NewVtctldServer(ts, collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion) + return NewVtctldServer(vtenv.NewTestEnv(), ts) }) resp, err := vtctld.GetSrvKeyspaceNames(ctx, tt.req) if tt.shouldErr { @@ -6443,7 +6441,7 @@ func TestGetSrvKeyspaces(t *testing.T) { testutil.AddSrvKeyspaces(t, ts, tt.srvKeyspaces...) vtctld := testutil.NewVtctldServerWithTabletManagerClient(t, ts, nil, func(ts *topo.Server) vtctlservicepb.VtctldServer { - return NewVtctldServer(ts, collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion) + return NewVtctldServer(vtenv.NewTestEnv(), ts) }) if tt.topoErr != nil { @@ -6470,7 +6468,7 @@ func TestGetSrvVSchema(t *testing.T) { defer cancel() ts, topofactory := memorytopo.NewServerAndFactory(ctx, "zone1", "zone2") vtctld := testutil.NewVtctldServerWithTabletManagerClient(t, ts, nil, func(ts *topo.Server) vtctlservicepb.VtctldServer { - return NewVtctldServer(ts, collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion) + return NewVtctldServer(vtenv.NewTestEnv(), ts) }) zone1SrvVSchema := &vschemapb.SrvVSchema{ @@ -6681,7 +6679,7 @@ func TestGetSrvVSchemas(t *testing.T) { defer cancel() ts, topofactory := memorytopo.NewServerAndFactory(ctx, "zone1", "zone2", "zone3") vtctld := testutil.NewVtctldServerWithTabletManagerClient(t, ts, nil, func(ts *topo.Server) vtctlservicepb.VtctldServer { - return NewVtctldServer(ts, collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion) + return NewVtctldServer(vtenv.NewTestEnv(), ts) }) zone1SrvVSchema := &vschemapb.SrvVSchema{ @@ -6741,7 +6739,7 @@ func TestGetTablet(t *testing.T) { defer cancel() ts := memorytopo.NewServer(ctx, "cell1") vtctld := testutil.NewVtctldServerWithTabletManagerClient(t, ts, nil, func(ts *topo.Server) vtctlservicepb.VtctldServer { - return NewVtctldServer(ts, collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion) + return NewVtctldServer(vtenv.NewTestEnv(), ts) }) tablet := &topodatapb.Tablet{ @@ -7364,7 +7362,7 @@ func TestGetTablets(t *testing.T) { defer cancel() ts := memorytopo.NewServer(ctx, tt.cells...) vtctld := testutil.NewVtctldServerWithTabletManagerClient(t, ts, nil, func(ts *topo.Server) vtctlservicepb.VtctldServer { - return NewVtctldServer(ts, collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion) + return NewVtctldServer(vtenv.NewTestEnv(), ts) }) testutil.AddTablets(ctx, t, ts, nil, tt.tablets...) @@ -7388,7 +7386,7 @@ func TestGetTopologyPath(t *testing.T) { defer cancel() ts := memorytopo.NewServer(ctx, "cell1", "cell2", "cell3") vtctld := testutil.NewVtctldServerWithTabletManagerClient(t, ts, nil, func(ts *topo.Server) vtctlservicepb.VtctldServer { - return NewVtctldServer(ts, collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion) + return NewVtctldServer(vtenv.NewTestEnv(), ts) }) err := ts.CreateKeyspace(ctx, "keyspace1", &topodatapb.Keyspace{}) @@ -7477,7 +7475,7 @@ func TestGetVSchema(t *testing.T) { defer cancel() ts := memorytopo.NewServer(ctx, "zone1") vtctld := testutil.NewVtctldServerWithTabletManagerClient(t, ts, nil, func(ts *topo.Server) vtctlservicepb.VtctldServer { - return NewVtctldServer(ts, collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion) + return NewVtctldServer(vtenv.NewTestEnv(), ts) }) t.Run("found", func(t *testing.T) { @@ -7708,7 +7706,7 @@ func TestLaunchSchemaMigration(t *testing.T) { }, test.tablets...) vtctld := testutil.NewVtctldServerWithTabletManagerClient(t, ts, test.tmc, func(ts *topo.Server) vtctlservicepb.VtctldServer { - return NewVtctldServer(ts, collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion) + return NewVtctldServer(vtenv.NewTestEnv(), ts) }) resp, err := vtctld.LaunchSchemaMigration(ctx, test.req) @@ -7795,7 +7793,7 @@ func TestPingTablet(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { vtctld := testutil.NewVtctldServerWithTabletManagerClient(t, ts, &tt.tmc, func(ts *topo.Server) vtctlservicepb.VtctldServer { - return NewVtctldServer(ts, collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion) + return NewVtctldServer(vtenv.NewTestEnv(), ts) }) resp, err := vtctld.PingTablet(ctx, tt.req) @@ -8043,7 +8041,7 @@ func TestPlannedReparentShard(t *testing.T) { }, tt.tablets...) vtctld := testutil.NewVtctldServerWithTabletManagerClient(t, tt.ts, tt.tmc, func(ts *topo.Server) vtctlservicepb.VtctldServer { - return NewVtctldServer(ts, collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion) + return NewVtctldServer(vtenv.NewTestEnv(), ts) }) resp, err := vtctld.PlannedReparentShard(ctx, tt.req) @@ -8086,7 +8084,7 @@ func TestRebuildKeyspaceGraph(t *testing.T) { Name: "testkeyspace", }) vtctld := testutil.NewVtctldServerWithTabletManagerClient(t, ts, nil, func(ts *topo.Server) vtctlservicepb.VtctldServer { - return NewVtctldServer(ts, collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion) + return NewVtctldServer(vtenv.NewTestEnv(), ts) }) _, err := vtctld.RebuildKeyspaceGraph(ctx, &vtctldatapb.RebuildKeyspaceGraphRequest{ @@ -8103,7 +8101,7 @@ func TestRebuildKeyspaceGraph(t *testing.T) { ts := memorytopo.NewServer(ctx, "zone1") vtctld := testutil.NewVtctldServerWithTabletManagerClient(t, ts, nil, func(ts *topo.Server) vtctlservicepb.VtctldServer { - return NewVtctldServer(ts, collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion) + return NewVtctldServer(vtenv.NewTestEnv(), ts) }) _, err := vtctld.RebuildKeyspaceGraph(context.Background(), &vtctldatapb.RebuildKeyspaceGraphRequest{ @@ -8123,7 +8121,7 @@ func TestRebuildKeyspaceGraph(t *testing.T) { Name: "testkeyspace", }) vtctld := testutil.NewVtctldServerWithTabletManagerClient(t, ts, nil, func(ts *topo.Server) vtctlservicepb.VtctldServer { - return NewVtctldServer(ts, collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion) + return NewVtctldServer(vtenv.NewTestEnv(), ts) }) factory.SetError(assert.AnError) @@ -8144,7 +8142,7 @@ func TestRebuildKeyspaceGraph(t *testing.T) { Name: "testkeyspace", }) vtctld := testutil.NewVtctldServerWithTabletManagerClient(t, ts, nil, func(ts *topo.Server) vtctlservicepb.VtctldServer { - return NewVtctldServer(ts, collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion) + return NewVtctldServer(vtenv.NewTestEnv(), ts) }) lctx, unlock, lerr := ts.LockKeyspace(context.Background(), "testkeyspace", "test lock") @@ -8193,7 +8191,7 @@ func TestRebuildVSchemaGraph(t *testing.T) { } vtctld := testutil.NewVtctldServerWithTabletManagerClient(t, ts, nil, func(ts *topo.Server) vtctlservicepb.VtctldServer { - return NewVtctldServer(ts, collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion) + return NewVtctldServer(vtenv.NewTestEnv(), ts) }) _, err := vtctld.RebuildVSchemaGraph(ctx, req) if tt.shouldErr { @@ -8292,7 +8290,7 @@ func TestRefreshState(t *testing.T) { } vtctld := testutil.NewVtctldServerWithTabletManagerClient(t, tt.ts, &tmc, func(ts *topo.Server) vtctlservicepb.VtctldServer { - return NewVtctldServer(ts, collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion) + return NewVtctldServer(vtenv.NewTestEnv(), ts) }) _, err := vtctld.RefreshState(ctx, tt.req) if tt.shouldErr { @@ -8477,7 +8475,7 @@ func TestRefreshStateByShard(t *testing.T) { } vtctld := testutil.NewVtctldServerWithTabletManagerClient(t, tt.ts, tmc, func(ts *topo.Server) vtctlservicepb.VtctldServer { - return NewVtctldServer(ts, collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion) + return NewVtctldServer(vtenv.NewTestEnv(), ts) }) resp, err := vtctld.RefreshStateByShard(ctx, tt.req) if tt.shouldErr { @@ -8581,7 +8579,7 @@ func TestReloadSchema(t *testing.T) { testutil.AddTablets(ctx, t, ts, nil, tt.tablets...) vtctld := testutil.NewVtctldServerWithTabletManagerClient(t, ts, &tt.tmc, func(ts *topo.Server) vtctlservicepb.VtctldServer { - return NewVtctldServer(ts, collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion) + return NewVtctldServer(vtenv.NewTestEnv(), ts) }) _, err := vtctld.ReloadSchema(ctx, tt.req) if tt.shouldErr { @@ -8679,7 +8677,7 @@ func TestReloadSchemaKeyspace(t *testing.T) { }, tt.tablets...) vtctld := testutil.NewVtctldServerWithTabletManagerClient(t, ts, &tt.tmc, func(ts *topo.Server) vtctlservicepb.VtctldServer { - return NewVtctldServer(ts, collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion) + return NewVtctldServer(vtenv.NewTestEnv(), ts) }) resp, err := vtctld.ReloadSchemaKeyspace(ctx, tt.req) if tt.shouldErr { @@ -8837,7 +8835,7 @@ func TestReloadSchemaShard(t *testing.T) { }, tt.tablets...) vtctld := testutil.NewVtctldServerWithTabletManagerClient(t, ts, &tt.tmc, func(ts *topo.Server) vtctlservicepb.VtctldServer { - return NewVtctldServer(ts, collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion) + return NewVtctldServer(vtenv.NewTestEnv(), ts) }) resp, err := vtctld.ReloadSchemaShard(ctx, tt.req) if tt.shouldErr { @@ -8856,7 +8854,7 @@ func TestRemoveBackup(t *testing.T) { defer cancel() ts := memorytopo.NewServer(ctx) vtctld := testutil.NewVtctldServerWithTabletManagerClient(t, ts, nil, func(ts *topo.Server) vtctlservicepb.VtctldServer { - return NewVtctldServer(ts, collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion) + return NewVtctldServer(vtenv.NewTestEnv(), ts) }) setup := func() { @@ -9047,7 +9045,7 @@ func TestRemoveKeyspaceCell(t *testing.T) { defer cancel() ts, topofactory := memorytopo.NewServerAndFactory(ctx, cells...) vtctld := testutil.NewVtctldServerWithTabletManagerClient(t, ts, nil, func(ts *topo.Server) vtctlservicepb.VtctldServer { - return NewVtctldServer(ts, collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion) + return NewVtctldServer(vtenv.NewTestEnv(), ts) }) // Setup topo @@ -9336,7 +9334,7 @@ func TestRemoveShardCell(t *testing.T) { defer cancel() ts, topofactory := memorytopo.NewServerAndFactory(ctx, cells...) vtctld := testutil.NewVtctldServerWithTabletManagerClient(t, ts, nil, func(ts *topo.Server) vtctlservicepb.VtctldServer { - return NewVtctldServer(ts, collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion) + return NewVtctldServer(vtenv.NewTestEnv(), ts) }) // Setup shard topos and replication graphs. @@ -9946,7 +9944,7 @@ func TestReparentTablet(t *testing.T) { defer cancel() ts, topofactory := memorytopo.NewServerAndFactory(ctx, cells...) vtctld := testutil.NewVtctldServerWithTabletManagerClient(t, ts, tt.tmc, func(ts *topo.Server) vtctlservicepb.VtctldServer { - return NewVtctldServer(ts, collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion) + return NewVtctldServer(vtenv.NewTestEnv(), ts) }) testutil.AddTablets(ctx, t, ts, &testutil.AddTabletOptions{ @@ -10079,7 +10077,7 @@ func TestRestoreFromBackup(t *testing.T) { }, tt.tablets..., ) vtctld := testutil.NewVtctldServerWithTabletManagerClient(t, tt.ts, tt.tmc, func(ts *topo.Server) vtctlservicepb.VtctldServer { - return NewVtctldServer(ts, collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion) + return NewVtctldServer(vtenv.NewTestEnv(), ts) }) client := localvtctldclient.New(vtctld) stream, err := client.RestoreFromBackup(ctx, tt.req) @@ -10297,7 +10295,7 @@ func TestRetrySchemaMigration(t *testing.T) { }, test.tablets...) vtctld := testutil.NewVtctldServerWithTabletManagerClient(t, ts, test.tmc, func(ts *topo.Server) vtctlservicepb.VtctldServer { - return NewVtctldServer(ts, collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion) + return NewVtctldServer(vtenv.NewTestEnv(), ts) }) resp, err := vtctld.RetrySchemaMigration(ctx, test.req) @@ -10404,7 +10402,7 @@ func TestRunHealthCheck(t *testing.T) { testutil.AddTablets(ctx, t, ts, nil, tt.tablets...) vtctld := testutil.NewVtctldServerWithTabletManagerClient(t, ts, &tt.tmc, func(ts *topo.Server) vtctlservicepb.VtctldServer { - return NewVtctldServer(ts, collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion) + return NewVtctldServer(vtenv.NewTestEnv(), ts) }) _, err := vtctld.RunHealthCheck(ctx, tt.req) if tt.shouldErr { @@ -10484,7 +10482,7 @@ func TestSetKeyspaceDurabilityPolicy(t *testing.T) { testutil.AddKeyspaces(ctx, t, ts, tt.keyspaces...) vtctld := testutil.NewVtctldServerWithTabletManagerClient(t, ts, nil, func(ts *topo.Server) vtctlservicepb.VtctldServer { - return NewVtctldServer(ts, collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion) + return NewVtctldServer(vtenv.NewTestEnv(), ts) }) resp, err := vtctld.SetKeyspaceDurabilityPolicy(ctx, tt.req) if tt.expectedErr != "" { @@ -10581,7 +10579,7 @@ func TestSetShardIsPrimaryServing(t *testing.T) { } vtctld := testutil.NewVtctldServerWithTabletManagerClient(t, tt.ts, nil, func(ts *topo.Server) vtctlservicepb.VtctldServer { - return NewVtctldServer(ts, collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion) + return NewVtctldServer(vtenv.NewTestEnv(), ts) }) resp, err := vtctld.SetShardIsPrimaryServing(tt.ctx, tt.req) if tt.shouldErr { @@ -10831,7 +10829,7 @@ func TestSetShardTabletControl(t *testing.T) { } vtctld := testutil.NewVtctldServerWithTabletManagerClient(t, tt.ts, nil, func(ts *topo.Server) vtctlservicepb.VtctldServer { - return NewVtctldServer(ts, collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion) + return NewVtctldServer(vtenv.NewTestEnv(), ts) }) resp, err := vtctld.SetShardTabletControl(tt.ctx, tt.req) if tt.shouldErr { @@ -11035,7 +11033,7 @@ func TestSetWritable(t *testing.T) { testutil.AddTablets(ctx, t, ts, nil, tt.tablets...) vtctld := testutil.NewVtctldServerWithTabletManagerClient(t, ts, &tt.tmc, func(ts *topo.Server) vtctlservicepb.VtctldServer { - return NewVtctldServer(ts, collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion) + return NewVtctldServer(vtenv.NewTestEnv(), ts) }) _, err := vtctld.SetWritable(ctx, tt.req) @@ -11056,7 +11054,7 @@ func TestShardReplicationAdd(t *testing.T) { defer cancel() ts := memorytopo.NewServer(ctx, "zone1") vtctld := testutil.NewVtctldServerWithTabletManagerClient(t, ts, nil, func(ts *topo.Server) vtctlservicepb.VtctldServer { - return NewVtctldServer(ts, collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion) + return NewVtctldServer(vtenv.NewTestEnv(), ts) }) tablets := []*topodatapb.Tablet{ @@ -11351,7 +11349,7 @@ func TestShardReplicationPositions(t *testing.T) { }, tt.tablets...) vtctld := testutil.NewVtctldServerWithTabletManagerClient(t, tt.ts, tt.tmc, func(ts *topo.Server) vtctlservicepb.VtctldServer { - return NewVtctldServer(ts, collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion) + return NewVtctldServer(vtenv.NewTestEnv(), ts) }) requestCtx := ctx @@ -11382,7 +11380,7 @@ func TestShardReplicationRemove(t *testing.T) { ts := memorytopo.NewServer(ctx, "zone1") vtctld := testutil.NewVtctldServerWithTabletManagerClient(t, ts, nil, func(ts *topo.Server) vtctlservicepb.VtctldServer { - return NewVtctldServer(ts, collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion) + return NewVtctldServer(vtenv.NewTestEnv(), ts) }) tablets := []*topodatapb.Tablet{ @@ -11542,7 +11540,7 @@ func TestSourceShardAdd(t *testing.T) { defer cancel() ts := memorytopo.NewServer(ctx, "zone1") vtctld := testutil.NewVtctldServerWithTabletManagerClient(t, ts, nil, func(ts *topo.Server) vtctlservicepb.VtctldServer { - return NewVtctldServer(ts, collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion) + return NewVtctldServer(vtenv.NewTestEnv(), ts) }) testutil.AddShards(ctx, t, ts, tt.shards...) @@ -11677,7 +11675,7 @@ func TestSourceShardDelete(t *testing.T) { defer cancel() ts := memorytopo.NewServer(ctx, "zone1") vtctld := testutil.NewVtctldServerWithTabletManagerClient(t, ts, nil, func(ts *topo.Server) vtctlservicepb.VtctldServer { - return NewVtctldServer(ts, collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion) + return NewVtctldServer(vtenv.NewTestEnv(), ts) }) testutil.AddShards(ctx, t, ts, tt.shards...) @@ -11869,7 +11867,7 @@ func TestStartReplication(t *testing.T) { AlsoSetShardPrimary: true, }, tt.tablets...) vtctld := testutil.NewVtctldServerWithTabletManagerClient(t, ts, &tt.tmc, func(ts *topo.Server) vtctlservicepb.VtctldServer { - return NewVtctldServer(ts, collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion) + return NewVtctldServer(vtenv.NewTestEnv(), ts) }) _, err := vtctld.StartReplication(ctx, tt.req) @@ -12006,7 +12004,7 @@ func TestStopReplication(t *testing.T) { testutil.AddTablets(ctx, t, ts, nil, tt.tablets...) vtctld := testutil.NewVtctldServerWithTabletManagerClient(t, ts, &tt.tmc, func(ts *topo.Server) vtctlservicepb.VtctldServer { - return NewVtctldServer(ts, collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion) + return NewVtctldServer(vtenv.NewTestEnv(), ts) }) _, err := vtctld.StopReplication(ctx, tt.req) @@ -12393,7 +12391,7 @@ func TestTabletExternallyReparented(t *testing.T) { TopoServer: ts, } vtctld := testutil.NewVtctldServerWithTabletManagerClient(t, ts, &tmc, func(ts *topo.Server) vtctlservicepb.VtctldServer { - return NewVtctldServer(ts, collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion) + return NewVtctldServer(vtenv.NewTestEnv(), ts) }) if tt.tmcHasNoTopo { @@ -12578,7 +12576,7 @@ func TestUpdateCellInfo(t *testing.T) { } vtctld := testutil.NewVtctldServerWithTabletManagerClient(t, ts, nil, func(ts *topo.Server) vtctlservicepb.VtctldServer { - return NewVtctldServer(ts, collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion) + return NewVtctldServer(vtenv.NewTestEnv(), ts) }) resp, err := vtctld.UpdateCellInfo(ctx, tt.req) if tt.shouldErr { @@ -12728,7 +12726,7 @@ func TestUpdateCellsAlias(t *testing.T) { } vtctld := testutil.NewVtctldServerWithTabletManagerClient(t, ts, nil, func(ts *topo.Server) vtctlservicepb.VtctldServer { - return NewVtctldServer(ts, collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion) + return NewVtctldServer(vtenv.NewTestEnv(), ts) }) resp, err := vtctld.UpdateCellsAlias(ctx, tt.req) if tt.shouldErr { @@ -12836,7 +12834,7 @@ func TestValidate(t *testing.T) { SkipShardCreation: false, }, tablets...) vtctld := testutil.NewVtctldServerWithTabletManagerClient(t, ts, nil, func(ts *topo.Server) vtctlservicepb.VtctldServer { - return NewVtctldServer(ts, collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion) + return NewVtctldServer(vtenv.NewTestEnv(), ts) }) resp, err := vtctld.Validate(ctx, &vtctldatapb.ValidateRequest{ @@ -12953,7 +12951,7 @@ func TestValidateSchemaKeyspace(t *testing.T) { }, tablets...) vtctld := testutil.NewVtctldServerWithTabletManagerClient(t, ts, &tmc, func(ts *topo.Server) vtctlservicepb.VtctldServer { - return NewVtctldServer(ts, collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion) + return NewVtctldServer(vtenv.NewTestEnv(), ts) }) schema1 := &tabletmanagerdatapb.SchemaDefinition{ @@ -13139,7 +13137,7 @@ func TestValidateVersionKeyspace(t *testing.T) { }, tablets...) vtctld := testutil.NewVtctldServerWithTabletManagerClient(t, ts, &tmc, func(ts *topo.Server) vtctlservicepb.VtctldServer { - return NewVtctldServer(ts, collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion) + return NewVtctldServer(vtenv.NewTestEnv(), ts) }) tests := []*struct { @@ -13254,7 +13252,7 @@ func TestValidateVersionShard(t *testing.T) { }, tablets...) vtctld := testutil.NewVtctldServerWithTabletManagerClient(t, ts, &tmc, func(ts *topo.Server) vtctlservicepb.VtctldServer { - return NewVtctldServer(ts, collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion) + return NewVtctldServer(vtenv.NewTestEnv(), ts) }) tests := []*struct { @@ -13846,7 +13844,7 @@ func TestValidateShard(t *testing.T) { } vtctld := testutil.NewVtctldServerWithTabletManagerClient(t, tt.ts, tt.tmc, func(ts *topo.Server) vtctlservicepb.VtctldServer { - return NewVtctldServer(ts, collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion) + return NewVtctldServer(vtenv.NewTestEnv(), ts) }) resp, err := vtctld.ValidateShard(ctx, tt.req) if tt.shouldErr { diff --git a/go/vt/vtctl/grpcvtctlserver/server.go b/go/vt/vtctl/grpcvtctlserver/server.go index d4cc448ef4f..d89f91b2d29 100644 --- a/go/vt/vtctl/grpcvtctlserver/server.go +++ b/go/vt/vtctl/grpcvtctlserver/server.go @@ -25,9 +25,7 @@ import ( "google.golang.org/grpc" - "vitess.io/vitess/go/vt/sqlparser" - - "vitess.io/vitess/go/mysql/collations" + "vitess.io/vitess/go/vt/vtenv" "vitess.io/vitess/go/vt/logutil" "vitess.io/vitess/go/vt/servenv" @@ -44,15 +42,13 @@ import ( // VtctlServer is our RPC server type VtctlServer struct { vtctlservicepb.UnimplementedVtctlServer - ts *topo.Server - collationEnv *collations.Environment - parser *sqlparser.Parser - mysqlVersion string + ts *topo.Server + env *vtenv.Environment } // NewVtctlServer returns a new Vtctl Server for the topo server. -func NewVtctlServer(ts *topo.Server, collationEnv *collations.Environment, parser *sqlparser.Parser, mysqlVersion string) *VtctlServer { - return &VtctlServer{ts: ts, collationEnv: collationEnv, parser: parser, mysqlVersion: mysqlVersion} +func NewVtctlServer(env *vtenv.Environment, ts *topo.Server) *VtctlServer { + return &VtctlServer{env: env, ts: ts} } // ExecuteVtctlCommand is part of the vtctldatapb.VtctlServer interface @@ -79,13 +75,13 @@ func (s *VtctlServer) ExecuteVtctlCommand(args *vtctldatapb.ExecuteVtctlCommandR // create the wrangler tmc := tmclient.NewTabletManagerClient() defer tmc.Close() - wr := wrangler.New(logger, s.ts, tmc, s.collationEnv, s.parser, s.mysqlVersion) + wr := wrangler.New(s.env, logger, s.ts, tmc) // execute the command return vtctl.RunCommand(stream.Context(), wr, args.Args) } // StartServer registers the VtctlServer for RPCs -func StartServer(s *grpc.Server, ts *topo.Server, collationEnv *collations.Environment, parser *sqlparser.Parser, mysqlVersion string) { - vtctlservicepb.RegisterVtctlServer(s, NewVtctlServer(ts, collationEnv, parser, mysqlVersion)) +func StartServer(s *grpc.Server, env *vtenv.Environment, ts *topo.Server) { + vtctlservicepb.RegisterVtctlServer(s, NewVtctlServer(env, ts)) } diff --git a/go/vt/vtctl/workflow/materializer.go b/go/vt/vtctl/workflow/materializer.go index 59890b77711..a965d801cee 100644 --- a/go/vt/vtctl/workflow/materializer.go +++ b/go/vt/vtctl/workflow/materializer.go @@ -24,7 +24,6 @@ import ( "text/template" "time" - "vitess.io/vitess/go/mysql/collations" "vitess.io/vitess/go/sqltypes" "vitess.io/vitess/go/vt/concurrency" "vitess.io/vitess/go/vt/key" @@ -34,6 +33,7 @@ import ( "vitess.io/vitess/go/vt/sqlparser" "vitess.io/vitess/go/vt/topo" "vitess.io/vitess/go/vt/vtctl/schematools" + "vitess.io/vitess/go/vt/vtenv" "vitess.io/vitess/go/vt/vterrors" "vitess.io/vitess/go/vt/vtgate/vindexes" "vitess.io/vitess/go/vt/vttablet/tabletmanager/vreplication" @@ -65,9 +65,7 @@ type materializer struct { primaryVindexesDiffer bool workflowType binlogdatapb.VReplicationWorkflowType - parser *sqlparser.Parser - collationEnv *collations.Environment - mysqlVersion string + env *vtenv.Environment } func (mz *materializer) getWorkflowSubType() (binlogdatapb.VReplicationWorkflowSubType, error) { @@ -202,7 +200,7 @@ func (mz *materializer) generateInserts(ctx context.Context, sourceShards []*top } // Validate non-empty query. - stmt, err := mz.parser.Parse(ts.SourceExpression) + stmt, err := mz.env.Parser().Parse(ts.SourceExpression) if err != nil { return "", err } @@ -301,7 +299,7 @@ func (mz *materializer) generateBinlogSources(ctx context.Context, targetShard * } // Validate non-empty query. - stmt, err := mz.parser.Parse(ts.SourceExpression) + stmt, err := mz.env.Parser().Parse(ts.SourceExpression) if err != nil { return nil, err } @@ -411,7 +409,7 @@ func (mz *materializer) deploySchema() error { if createDDL == createDDLAsCopy || createDDL == createDDLAsCopyDropConstraint || createDDL == createDDLAsCopyDropForeignKeys { if ts.SourceExpression != "" { // Check for table if non-empty SourceExpression. - sourceTableName, err := mz.parser.TableFromStatement(ts.SourceExpression) + sourceTableName, err := mz.env.Parser().TableFromStatement(ts.SourceExpression) if err != nil { return err } @@ -427,7 +425,7 @@ func (mz *materializer) deploySchema() error { } if createDDL == createDDLAsCopyDropConstraint { - strippedDDL, err := stripTableConstraints(ddl, mz.parser) + strippedDDL, err := stripTableConstraints(ddl, mz.env.Parser()) if err != nil { return err } @@ -436,7 +434,7 @@ func (mz *materializer) deploySchema() error { } if createDDL == createDDLAsCopyDropForeignKeys { - strippedDDL, err := stripTableForeignKeys(ddl, mz.parser) + strippedDDL, err := stripTableForeignKeys(ddl, mz.env.Parser()) if err != nil { return err } @@ -457,7 +455,7 @@ func (mz *materializer) deploySchema() error { // We use schemadiff to normalize the schema. // For now, and because this is could have wider implications, we ignore any errors in // reading the source schema. - env := schemadiff.NewEnv(mz.collationEnv, mz.collationEnv.DefaultConnectionCharset(), mz.parser, mz.mysqlVersion) + env := schemadiff.NewEnv(mz.env, mz.env.CollationEnv().DefaultConnectionCharset()) schema, err := schemadiff.NewSchemaFromQueries(env, applyDDLs) if err != nil { log.Error(vterrors.Wrapf(err, "AtomicCopy: failed to normalize schema via schemadiff")) @@ -490,7 +488,7 @@ func (mz *materializer) buildMaterializer() error { if err != nil { return err } - targetVSchema, err := vindexes.BuildKeyspaceSchema(vschema, ms.TargetKeyspace, mz.parser) + targetVSchema, err := vindexes.BuildKeyspaceSchema(vschema, ms.TargetKeyspace, mz.env.Parser()) if err != nil { return err } diff --git a/go/vt/vtctl/workflow/materializer_env_test.go b/go/vt/vtctl/workflow/materializer_env_test.go index 7af65bde65c..452c5755a10 100644 --- a/go/vt/vtctl/workflow/materializer_env_test.go +++ b/go/vt/vtctl/workflow/materializer_env_test.go @@ -28,13 +28,11 @@ import ( "google.golang.org/protobuf/proto" - "vitess.io/vitess/go/mysql/collations" - "vitess.io/vitess/go/mysql/config" "vitess.io/vitess/go/sqltypes" "vitess.io/vitess/go/vt/mysqlctl/tmutils" - "vitess.io/vitess/go/vt/sqlparser" "vitess.io/vitess/go/vt/topo" "vitess.io/vitess/go/vt/topo/memorytopo" + "vitess.io/vitess/go/vt/vtenv" "vitess.io/vitess/go/vt/vterrors" "vitess.io/vitess/go/vt/vttablet/tmclient" @@ -84,8 +82,8 @@ func newTestMaterializerEnv(t *testing.T, ctx context.Context, ms *vtctldatapb.M cell: "cell", tmc: newTestMaterializerTMClient(), } - parser := sqlparser.NewTestParser() - env.ws = NewServer(env.topoServ, env.tmc, collations.MySQL8(), parser, config.DefaultMySQLVersion) + venv := vtenv.NewTestEnv() + env.ws = NewServer(venv, env.topoServ, env.tmc) tabletID := 100 for _, shard := range sources { _ = env.addTablet(tabletID, env.ms.SourceKeyspace, shard, topodatapb.TabletType_PRIMARY) @@ -101,7 +99,7 @@ func newTestMaterializerEnv(t *testing.T, ctx context.Context, ms *vtctldatapb.M for _, ts := range ms.TableSettings { tableName := ts.TargetTable - table, err := parser.TableFromStatement(ts.SourceExpression) + table, err := venv.Parser().TableFromStatement(ts.SourceExpression) if err == nil { tableName = table.Name.String() } diff --git a/go/vt/vtctl/workflow/materializer_test.go b/go/vt/vtctl/workflow/materializer_test.go index c2736067c23..82cc07fdf7f 100644 --- a/go/vt/vtctl/workflow/materializer_test.go +++ b/go/vt/vtctl/workflow/materializer_test.go @@ -28,9 +28,8 @@ import ( "golang.org/x/exp/maps" "google.golang.org/protobuf/proto" - "vitess.io/vitess/go/mysql/collations" - "vitess.io/vitess/go/mysql/config" "vitess.io/vitess/go/vt/sqlparser" + "vitess.io/vitess/go/vt/vtenv" "vitess.io/vitess/go/sqltypes" "vitess.io/vitess/go/test/utils" @@ -3017,7 +3016,7 @@ func TestMaterializerNoSourcePrimary(t *testing.T) { cell: "cell", tmc: newTestMaterializerTMClient(), } - env.ws = NewServer(env.topoServ, env.tmc, collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion) + env.ws = NewServer(vtenv.NewTestEnv(), env.topoServ, env.tmc) defer env.close() tabletID := 100 @@ -3567,6 +3566,7 @@ func TestKeyRangesEqualOptimization(t *testing.T) { tmc: env.tmc, ms: ms, workflowType: workflowType, + env: vtenv.NewTestEnv(), } err = mz.createMoveTablesStreams(tc.moveTablesReq) require.NoError(t, err, "createMoveTablesStreams failed: %v", err) diff --git a/go/vt/vtctl/workflow/server.go b/go/vt/vtctl/workflow/server.go index dc194302c8e..e298d3f64f3 100644 --- a/go/vt/vtctl/workflow/server.go +++ b/go/vt/vtctl/workflow/server.go @@ -35,7 +35,6 @@ import ( "google.golang.org/protobuf/encoding/prototext" "google.golang.org/protobuf/proto" - "vitess.io/vitess/go/mysql/collations" "vitess.io/vitess/go/mysql/sqlerror" "vitess.io/vitess/go/protoutil" "vitess.io/vitess/go/sets" @@ -56,6 +55,7 @@ import ( "vitess.io/vitess/go/vt/topotools" "vitess.io/vitess/go/vt/vtctl/schematools" "vitess.io/vitess/go/vt/vtctl/workflow/vexec" + "vitess.io/vitess/go/vt/vtenv" "vitess.io/vitess/go/vt/vterrors" "vitess.io/vitess/go/vt/vtgate/vindexes" "vitess.io/vitess/go/vt/vttablet/tabletmanager/vdiff" @@ -144,34 +144,22 @@ type Server struct { ts *topo.Server tmc tmclient.TabletManagerClient // Limit the number of concurrent background goroutines if needed. - sem *semaphore.Weighted - collationEnv *collations.Environment - parser *sqlparser.Parser - mysqlVersion string + sem *semaphore.Weighted + env *vtenv.Environment } // NewServer returns a new server instance with the given topo.Server and // TabletManagerClient. -func NewServer(ts *topo.Server, tmc tmclient.TabletManagerClient, collationEnv *collations.Environment, parser *sqlparser.Parser, mysqlVersion string) *Server { +func NewServer(env *vtenv.Environment, ts *topo.Server, tmc tmclient.TabletManagerClient) *Server { return &Server{ - ts: ts, - tmc: tmc, - collationEnv: collationEnv, - parser: parser, - mysqlVersion: mysqlVersion, + ts: ts, + tmc: tmc, + env: env, } } func (s *Server) SQLParser() *sqlparser.Parser { - return s.parser -} - -func (s *Server) CollationEnv() *collations.Environment { - return s.collationEnv -} - -func (s *Server) MySQLVersion() string { - return s.mysqlVersion + return s.env.Parser() } // CheckReshardingJournalExistsOnTablet returns the journal (or an empty @@ -431,7 +419,7 @@ func (s *Server) GetWorkflows(ctx context.Context, req *vtctldatapb.GetWorkflows where, ) - vx := vexec.NewVExec(req.Keyspace, "", s.ts, s.tmc, s.SQLParser()) + vx := vexec.NewVExec(req.Keyspace, "", s.ts, s.tmc, s.env.Parser()) vx.SetShardSubset(req.Shards) results, err := vx.QueryContext(ctx, query) if err != nil { @@ -1345,14 +1333,12 @@ func (s *Server) LookupVindexExternalize(ctx context.Context, req *vtctldatapb.L // tables based on the materialization specs. func (s *Server) Materialize(ctx context.Context, ms *vtctldatapb.MaterializeSettings) error { mz := &materializer{ - ctx: ctx, - ts: s.ts, - sourceTs: s.ts, - tmc: s.tmc, - ms: ms, - parser: s.SQLParser(), - collationEnv: s.CollationEnv(), - mysqlVersion: s.MySQLVersion(), + ctx: ctx, + ts: s.ts, + sourceTs: s.ts, + tmc: s.tmc, + ms: ms, + env: s.env, } err := mz.createMaterializerStreams() @@ -1491,9 +1477,7 @@ func (s *Server) moveTablesCreate(ctx context.Context, req *vtctldatapb.MoveTabl tmc: s.tmc, ms: ms, workflowType: workflowType, - collationEnv: s.CollationEnv(), - parser: s.SQLParser(), - mysqlVersion: s.MySQLVersion(), + env: s.env, } err = mz.createMoveTablesStreams(req) if err != nil { @@ -1968,7 +1952,7 @@ func (s *Server) WorkflowDelete(ctx context.Context, req *vtctldatapb.WorkflowDe deleteReq := &tabletmanagerdatapb.DeleteVReplicationWorkflowRequest{ Workflow: req.Workflow, } - vx := vexec.NewVExec(req.Keyspace, req.Workflow, s.ts, s.tmc, s.SQLParser()) + vx := vexec.NewVExec(req.Keyspace, req.Workflow, s.ts, s.tmc, s.env.Parser()) vx.SetShardSubset(req.Shards) callback := func(ctx context.Context, tablet *topo.TabletInfo) (*querypb.QueryResult, error) { res, err := s.tmc.DeleteVReplicationWorkflow(ctx, tablet.Tablet, deleteReq) @@ -2246,7 +2230,7 @@ func (s *Server) WorkflowUpdate(ctx context.Context, req *vtctldatapb.WorkflowUp span.Annotate("state", req.TabletRequest.State) span.Annotate("shards", req.TabletRequest.Shards) - vx := vexec.NewVExec(req.Keyspace, req.TabletRequest.Workflow, s.ts, s.tmc, s.SQLParser()) + vx := vexec.NewVExec(req.Keyspace, req.TabletRequest.Workflow, s.ts, s.tmc, s.env.Parser()) vx.SetShardSubset(req.TabletRequest.Shards) callback := func(ctx context.Context, tablet *topo.TabletInfo) (*querypb.QueryResult, error) { res, err := s.tmc.UpdateVReplicationWorkflow(ctx, tablet.Tablet, req.TabletRequest) @@ -2660,7 +2644,7 @@ func (s *Server) buildTrafficSwitcher(ctx context.Context, targetKeyspace, workf if err != nil { return nil, err } - ts.sourceKSSchema, err = vindexes.BuildKeyspaceSchema(vs, ts.sourceKeyspace, s.SQLParser()) + ts.sourceKSSchema, err = vindexes.BuildKeyspaceSchema(vs, ts.sourceKeyspace, s.env.Parser()) if err != nil { return nil, err } @@ -3252,7 +3236,7 @@ func (s *Server) switchWrites(ctx context.Context, req *vtctldatapb.WorkflowSwit } if !journalsExist { ts.Logger().Infof("No previous journals were found. Proceeding normally.") - sm, err := BuildStreamMigrator(ctx, ts, cancel, s.parser) + sm, err := BuildStreamMigrator(ctx, ts, cancel, s.env.Parser()) if err != nil { return handleError("failed to migrate the workflow streams", err) } @@ -3583,7 +3567,7 @@ func (s *Server) prepareCreateLookup(ctx context.Context, workflow, keyspace str if !strings.Contains(vindex.Type, "lookup") { return nil, nil, nil, vterrors.Errorf(vtrpcpb.Code_INVALID_ARGUMENT, "vindex %s is not a lookup type", vindex.Type) } - targetKeyspace, targetTableName, err = s.parser.ParseTable(vindex.Params["table"]) + targetKeyspace, targetTableName, err = s.env.Parser().ParseTable(vindex.Params["table"]) if err != nil || targetKeyspace == "" { return nil, nil, nil, vterrors.Errorf(vtrpcpb.Code_INVALID_ARGUMENT, "vindex table name (%s) must be in the form .", vindex.Params["table"]) } diff --git a/go/vt/vtctl/workflow/server_test.go b/go/vt/vtctl/workflow/server_test.go index 8aaacf53833..4ff6a21778b 100644 --- a/go/vt/vtctl/workflow/server_test.go +++ b/go/vt/vtctl/workflow/server_test.go @@ -25,13 +25,11 @@ import ( "github.com/stretchr/testify/require" "google.golang.org/protobuf/encoding/prototext" - "vitess.io/vitess/go/mysql/collations" - "vitess.io/vitess/go/mysql/config" "vitess.io/vitess/go/sqltypes" "vitess.io/vitess/go/test/utils" - "vitess.io/vitess/go/vt/sqlparser" "vitess.io/vitess/go/vt/topo/memorytopo" "vitess.io/vitess/go/vt/topo/topoproto" + "vitess.io/vitess/go/vt/vtenv" "vitess.io/vitess/go/vt/vttablet/tmclient" binlogdatapb "vitess.io/vitess/go/vt/proto/binlogdata" @@ -147,7 +145,7 @@ func TestCheckReshardingJournalExistsOnTablet(t *testing.T) { }, } - ws := NewServer(nil, tmc, collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion) + ws := NewServer(vtenv.NewTestEnv(), nil, tmc) journal, exists, err := ws.CheckReshardingJournalExistsOnTablet(ctx, tt.tablet, 1) if tt.shouldErr { assert.Error(t, err) @@ -175,7 +173,7 @@ func TestVDiffCreate(t *testing.T) { ctx := context.Background() ts := memorytopo.NewServer(ctx, "cell") tmc := &fakeTMC{} - s := NewServer(ts, tmc, collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion) + s := NewServer(vtenv.NewTestEnv(), ts, tmc) tests := []struct { name string diff --git a/go/vt/vtctld/action_repository.go b/go/vt/vtctld/action_repository.go index 07fb3cf955b..e0f6c45535a 100644 --- a/go/vt/vtctld/action_repository.go +++ b/go/vt/vtctld/action_repository.go @@ -23,9 +23,7 @@ import ( "github.com/spf13/pflag" - "vitess.io/vitess/go/vt/sqlparser" - - "vitess.io/vitess/go/mysql/collations" + "vitess.io/vitess/go/vt/vtenv" "vitess.io/vitess/go/acl" "vitess.io/vitess/go/vt/logutil" @@ -83,26 +81,22 @@ type actionTabletRecord struct { // ActionRepository is a repository of actions that can be performed // on a {Keyspace,Shard,Tablet}. type ActionRepository struct { + env *vtenv.Environment keyspaceActions map[string]actionKeyspaceMethod shardActions map[string]actionShardMethod tabletActions map[string]actionTabletRecord ts *topo.Server - collationEnv *collations.Environment - parser *sqlparser.Parser - mysqlVersion string } // NewActionRepository creates and returns a new ActionRepository, // with no actions. -func NewActionRepository(ts *topo.Server, collationEnv *collations.Environment, parser *sqlparser.Parser, mysqlVersion string) *ActionRepository { +func NewActionRepository(env *vtenv.Environment, ts *topo.Server) *ActionRepository { return &ActionRepository{ + env: env, keyspaceActions: make(map[string]actionKeyspaceMethod), shardActions: make(map[string]actionShardMethod), tabletActions: make(map[string]actionTabletRecord), ts: ts, - collationEnv: collationEnv, - parser: parser, - mysqlVersion: mysqlVersion, } } @@ -135,7 +129,7 @@ func (ar *ActionRepository) ApplyKeyspaceAction(ctx context.Context, actionName, } ctx, cancel := context.WithTimeout(ctx, actionTimeout) - wr := wrangler.New(logutil.NewConsoleLogger(), ar.ts, tmclient.NewTabletManagerClient(), ar.collationEnv, ar.parser, ar.mysqlVersion) + wr := wrangler.New(ar.env, logutil.NewConsoleLogger(), ar.ts, tmclient.NewTabletManagerClient()) output, err := action(ctx, wr, keyspace) cancel() if err != nil { @@ -162,7 +156,7 @@ func (ar *ActionRepository) ApplyShardAction(ctx context.Context, actionName, ke } ctx, cancel := context.WithTimeout(ctx, actionTimeout) - wr := wrangler.New(logutil.NewConsoleLogger(), ar.ts, tmclient.NewTabletManagerClient(), ar.collationEnv, ar.parser, ar.mysqlVersion) + wr := wrangler.New(ar.env, logutil.NewConsoleLogger(), ar.ts, tmclient.NewTabletManagerClient()) output, err := action(ctx, wr, keyspace, shard) cancel() if err != nil { @@ -196,7 +190,7 @@ func (ar *ActionRepository) ApplyTabletAction(ctx context.Context, actionName st // run the action ctx, cancel := context.WithTimeout(ctx, actionTimeout) - wr := wrangler.New(logutil.NewConsoleLogger(), ar.ts, tmclient.NewTabletManagerClient(), ar.collationEnv, ar.parser, ar.mysqlVersion) + wr := wrangler.New(ar.env, logutil.NewConsoleLogger(), ar.ts, tmclient.NewTabletManagerClient()) output, err := action.method(ctx, wr, tabletAlias) cancel() if err != nil { diff --git a/go/vt/vtctld/api.go b/go/vt/vtctld/api.go index 48171cd4a70..0452fce3c3d 100644 --- a/go/vt/vtctld/api.go +++ b/go/vt/vtctld/api.go @@ -487,7 +487,7 @@ func initAPI(ctx context.Context, ts *topo.Server, actions *ActionRepository) { logstream := logutil.NewMemoryLogger() - wr := wrangler.New(logstream, ts, tmClient, actions.collationEnv, actions.parser, actions.mysqlVersion) + wr := wrangler.New(actions.env, logstream, ts, tmClient) err := vtctl.RunCommand(r.Context(), wr, args) if err != nil { resp.Error = err.Error() @@ -523,7 +523,7 @@ func initAPI(ctx context.Context, ts *topo.Server, actions *ActionRepository) { logger := logutil.NewCallbackLogger(func(ev *logutilpb.Event) { w.Write([]byte(logutil.EventString(ev))) }) - wr := wrangler.New(logger, ts, tmClient, actions.collationEnv, actions.parser, actions.mysqlVersion) + wr := wrangler.New(actions.env, logger, ts, tmClient) apiCallUUID, err := schema.CreateUUID() if err != nil { @@ -531,7 +531,7 @@ func initAPI(ctx context.Context, ts *topo.Server, actions *ActionRepository) { } requestContext := fmt.Sprintf("vtctld/api:%s", apiCallUUID) - executor := schemamanager.NewTabletExecutor(requestContext, wr.TopoServer(), wr.TabletManagerClient(), wr.Logger(), time.Duration(req.ReplicaTimeoutSeconds)*time.Second, 0, actions.parser) + executor := schemamanager.NewTabletExecutor(requestContext, wr.TopoServer(), wr.TabletManagerClient(), wr.Logger(), time.Duration(req.ReplicaTimeoutSeconds)*time.Second, 0, actions.env.Parser()) if err := executor.SetDDLStrategy(req.DDLStrategy); err != nil { return fmt.Errorf("error setting DDL strategy: %v", err) } diff --git a/go/vt/vtctld/api_test.go b/go/vt/vtctld/api_test.go index 0ff769568ea..d8ac8beccc1 100644 --- a/go/vt/vtctld/api_test.go +++ b/go/vt/vtctld/api_test.go @@ -27,13 +27,9 @@ import ( "github.com/stretchr/testify/require" - "vitess.io/vitess/go/vt/sqlparser" - - "vitess.io/vitess/go/mysql/collations" - "vitess.io/vitess/go/mysql/config" - "vitess.io/vitess/go/vt/servenv/testutils" "vitess.io/vitess/go/vt/topo/memorytopo" + "vitess.io/vitess/go/vt/vtenv" "vitess.io/vitess/go/vt/wrangler" topodatapb "vitess.io/vitess/go/vt/proto/topodata" @@ -52,7 +48,7 @@ func TestAPI(t *testing.T) { cells := []string{"cell1", "cell2"} ts := memorytopo.NewServer(ctx, cells...) defer ts.Close() - actionRepo := NewActionRepository(ts, collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion) + actionRepo := NewActionRepository(vtenv.NewTestEnv(), ts) server := testutils.HTTPTestServer() defer server.Close() diff --git a/go/vt/vtctld/tablet_data_test.go b/go/vt/vtctld/tablet_data_test.go index 2d6cbc0b4f2..bbb03e3b878 100644 --- a/go/vt/vtctld/tablet_data_test.go +++ b/go/vt/vtctld/tablet_data_test.go @@ -25,15 +25,11 @@ import ( "google.golang.org/protobuf/proto" - "vitess.io/vitess/go/vt/sqlparser" - - "vitess.io/vitess/go/mysql/collations" - "vitess.io/vitess/go/mysql/config" - "vitess.io/vitess/go/vt/logutil" querypb "vitess.io/vitess/go/vt/proto/query" topodatapb "vitess.io/vitess/go/vt/proto/topodata" "vitess.io/vitess/go/vt/topo/memorytopo" + "vitess.io/vitess/go/vt/vtenv" "vitess.io/vitess/go/vt/vttablet/grpcqueryservice" "vitess.io/vitess/go/vt/vttablet/queryservice" "vitess.io/vitess/go/vt/vttablet/queryservice/fakes" @@ -117,7 +113,7 @@ func TestTabletData(t *testing.T) { defer cancel() ts := memorytopo.NewServer(ctx, "cell1", "cell2") defer ts.Close() - wr := wrangler.New(logutil.NewConsoleLogger(), ts, tmclient.NewTabletManagerClient(), collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion) + wr := wrangler.New(vtenv.NewTestEnv(), logutil.NewConsoleLogger(), ts, tmclient.NewTabletManagerClient()) if err := ts.CreateKeyspace(context.Background(), "ks", &topodatapb.Keyspace{}); err != nil { t.Fatalf("CreateKeyspace failed: %v", err) diff --git a/go/vt/vtctld/vtctld.go b/go/vt/vtctld/vtctld.go index 1d36646a189..5ca3908c053 100644 --- a/go/vt/vtctld/vtctld.go +++ b/go/vt/vtctld/vtctld.go @@ -23,9 +23,7 @@ import ( "github.com/spf13/pflag" - "vitess.io/vitess/go/vt/sqlparser" - - "vitess.io/vitess/go/mysql/collations" + "vitess.io/vitess/go/vt/vtenv" "vitess.io/vitess/go/vt/servenv" @@ -52,8 +50,8 @@ func registerVtctldFlags(fs *pflag.FlagSet) { } // InitVtctld initializes all the vtctld functionality. -func InitVtctld(ts *topo.Server, collationEnv *collations.Environment, parser *sqlparser.Parser, mysqlVersion string) error { - actionRepo := NewActionRepository(ts, collationEnv, parser, mysqlVersion) +func InitVtctld(env *vtenv.Environment, ts *topo.Server) error { + actionRepo := NewActionRepository(env, ts) // keyspace actions actionRepo.RegisterKeyspaceAction("ValidateKeyspace", diff --git a/go/vt/vtenv/cached_size.go b/go/vt/vtenv/cached_size.go new file mode 100644 index 00000000000..808cc4cdca3 --- /dev/null +++ b/go/vt/vtenv/cached_size.go @@ -0,0 +1,37 @@ +/* +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. +*/ +// Code generated by Sizegen. DO NOT EDIT. + +package vtenv + +import hack "vitess.io/vitess/go/hack" + +func (cached *Environment) CachedSize(alloc bool) int64 { + if cached == nil { + return int64(0) + } + size := int64(0) + if alloc { + size += int64(48) + } + // field collationEnv *vitess.io/vitess/go/mysql/collations.Environment + size += cached.collationEnv.CachedSize(true) + // field parser *vitess.io/vitess/go/vt/sqlparser.Parser + size += cached.parser.CachedSize(true) + // field mysqlVersion string + size += hack.RuntimeAllocSize(int64(len(cached.mysqlVersion))) + return size +} diff --git a/go/vt/vtenv/vtenv.go b/go/vt/vtenv/vtenv.go new file mode 100644 index 00000000000..71bf089cf24 --- /dev/null +++ b/go/vt/vtenv/vtenv.go @@ -0,0 +1,93 @@ +/* +Copyright 2024 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 vtenv + +import ( + "vitess.io/vitess/go/mysql/collations" + "vitess.io/vitess/go/mysql/config" + "vitess.io/vitess/go/vt/sqlparser" +) + +type Environment struct { + collationEnv *collations.Environment + parser *sqlparser.Parser + mysqlVersion string + truncateUILen int + truncateErrLen int +} + +type Options struct { + MySQLServerVersion string + TruncateUILen int + TruncateErrLen int +} + +func New(cfg Options) (*Environment, error) { + if cfg.MySQLServerVersion == "" { + cfg.MySQLServerVersion = config.DefaultMySQLVersion + } + parser, err := sqlparser.New(sqlparser.Options{ + MySQLServerVersion: cfg.MySQLServerVersion, + TruncateErrLen: cfg.TruncateErrLen, + TruncateUILen: cfg.TruncateUILen, + }) + if err != nil { + return nil, err + } + return &Environment{ + collationEnv: collations.NewEnvironment(cfg.MySQLServerVersion), + parser: parser, + mysqlVersion: cfg.MySQLServerVersion, + truncateUILen: cfg.TruncateUILen, + truncateErrLen: cfg.TruncateErrLen, + }, nil +} + +func NewTestEnv() *Environment { + return &Environment{ + collationEnv: collations.NewEnvironment(config.DefaultMySQLVersion), + parser: sqlparser.NewTestParser(), + mysqlVersion: config.DefaultMySQLVersion, + truncateUILen: 512, + truncateErrLen: 0, + } +} + +func (e *Environment) CollationEnv() *collations.Environment { + return e.collationEnv +} + +func (e *Environment) Parser() *sqlparser.Parser { + return e.parser +} + +func (e *Environment) MySQLVersion() string { + return e.mysqlVersion +} + +// TruncateForUI is used when displaying queries on various Vitess status pages +// to keep the pages small enough to load and render properly +func (e *Environment) TruncateForUI(query string) string { + return sqlparser.TruncateQuery(query, e.truncateUILen) +} + +// TruncateForLog is used when displaying queries as part of error logs +// to avoid overwhelming logging systems with potentially long queries and +// bind value data. +func (e *Environment) TruncateForLog(query string) string { + return sqlparser.TruncateQuery(query, e.truncateErrLen) +} diff --git a/go/vt/vtenv/vtenv_test.go b/go/vt/vtenv/vtenv_test.go new file mode 100644 index 00000000000..f0d15e5156b --- /dev/null +++ b/go/vt/vtenv/vtenv_test.go @@ -0,0 +1,49 @@ +package vtenv + +import ( + "testing" + + "github.com/stretchr/testify/assert" + + "vitess.io/vitess/go/mysql/collations" + "vitess.io/vitess/go/mysql/config" + "vitess.io/vitess/go/vt/sqlparser" +) + +func TestNewDefaults(t *testing.T) { + e, err := New(Options{}) + assert.NoError(t, err) + assert.Equal(t, config.DefaultMySQLVersion, e.MySQLVersion()) + assert.Equal(t, collations.MySQL8(), e.CollationEnv()) + assert.Equal(t, 0, e.Parser().GetTruncateErrLen()) + assert.Equal(t, "foo", e.TruncateForLog("foo")) + assert.Equal(t, "foo", e.TruncateForUI("foo")) +} + +func TestNewCustom(t *testing.T) { + e, err := New(Options{ + MySQLServerVersion: "8.0.34", + TruncateErrLen: 15, + TruncateUILen: 16, + }) + assert.NoError(t, err) + assert.Equal(t, "8.0.34", e.MySQLVersion()) + assert.Equal(t, collations.MySQL8(), e.CollationEnv()) + assert.Equal(t, 15, e.Parser().GetTruncateErrLen()) + assert.Equal(t, "sel [TRUNCATED]", e.TruncateForLog("select 11111111111")) + assert.Equal(t, "sele [TRUNCATED]", e.TruncateForUI("select 11111111111")) +} + +func TestNewError(t *testing.T) { + _, err := New(Options{ + MySQLServerVersion: "invalid", + }) + assert.Error(t, err) +} + +func TestNewTestEnv(t *testing.T) { + e := NewTestEnv() + assert.Equal(t, config.DefaultMySQLVersion, e.MySQLVersion()) + assert.Equal(t, collations.MySQL8(), e.CollationEnv()) + assert.Equal(t, sqlparser.NewTestParser(), e.Parser()) +} diff --git a/go/vt/vtexplain/vtexplain.go b/go/vt/vtexplain/vtexplain.go index 40803382e56..55a0e52b005 100644 --- a/go/vt/vtexplain/vtexplain.go +++ b/go/vt/vtexplain/vtexplain.go @@ -28,7 +28,7 @@ import ( "github.com/spf13/pflag" - "vitess.io/vitess/go/mysql/collations" + "vitess.io/vitess/go/vt/vtenv" "vitess.io/vitess/go/vt/discovery" "vitess.io/vitess/go/vt/servenv" @@ -147,9 +147,7 @@ type ( batchTime *sync2.Batcher globalTabletEnv *tabletEnv - collationEnv *collations.Environment - parser *sqlparser.Parser - mysqlVersion string + env *vtenv.Environment } ) @@ -185,18 +183,18 @@ type TabletActions struct { } // Init sets up the fake execution environment -func Init(ctx context.Context, vSchemaStr, sqlSchema, ksShardMapStr string, opts *Options, collationEnv *collations.Environment, parser *sqlparser.Parser, mysqlVersion string) (*VTExplain, error) { +func Init(ctx context.Context, env *vtenv.Environment, vSchemaStr, sqlSchema, ksShardMapStr string, opts *Options) (*VTExplain, error) { // Verify options if opts.ReplicationMode != "ROW" && opts.ReplicationMode != "STATEMENT" { return nil, fmt.Errorf("invalid replication mode \"%s\"", opts.ReplicationMode) } - parsedDDLs, err := parseSchema(sqlSchema, opts, parser) + parsedDDLs, err := parseSchema(sqlSchema, opts, env.Parser()) if err != nil { return nil, fmt.Errorf("parseSchema: %v", err) } - tabletEnv, err := newTabletEnvironment(parsedDDLs, opts, collationEnv) + tabletEnv, err := newTabletEnvironment(parsedDDLs, opts, env.CollationEnv()) if err != nil { return nil, fmt.Errorf("initTabletEnvironment: %v", err) } @@ -205,9 +203,7 @@ func Init(ctx context.Context, vSchemaStr, sqlSchema, ksShardMapStr string, opts TargetString: "", Autocommit: true, }, - collationEnv: collationEnv, - parser: parser, - mysqlVersion: mysqlVersion, + env: env, } vte.setGlobalTabletEnv(tabletEnv) err = vte.initVtgateExecutor(ctx, vSchemaStr, ksShardMapStr, opts) @@ -303,7 +299,7 @@ func (vte *VTExplain) Run(sql string) ([]*Explain, error) { sql = s } - sql, rem, err = vte.parser.SplitStatement(sql) + sql, rem, err = vte.env.Parser().SplitStatement(sql) if err != nil { return nil, err } @@ -390,7 +386,7 @@ func (vte *VTExplain) specialHandlingOfSavepoints(q *MysqlQuery) error { return nil } - stmt, err := vte.parser.Parse(q.SQL) + stmt, err := vte.env.Parser().Parse(q.SQL) if err != nil { return err } diff --git a/go/vt/vtexplain/vtexplain_test.go b/go/vt/vtexplain/vtexplain_test.go index de8a07714cd..1c60ea5c34a 100644 --- a/go/vt/vtexplain/vtexplain_test.go +++ b/go/vt/vtexplain/vtexplain_test.go @@ -28,10 +28,7 @@ import ( "github.com/google/go-cmp/cmp" "github.com/stretchr/testify/require" - "vitess.io/vitess/go/vt/sqlparser" - - "vitess.io/vitess/go/mysql/collations" - "vitess.io/vitess/go/mysql/config" + "vitess.io/vitess/go/vt/vtenv" "vitess.io/vitess/go/test/utils" @@ -70,7 +67,7 @@ func initTest(ctx context.Context, mode string, opts *Options, topts *testopts, } opts.ExecutionMode = mode - vte, err := Init(ctx, string(vSchema), string(schema), shardmap, opts, collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion) + vte, err := Init(ctx, vtenv.NewTestEnv(), string(vSchema), string(schema), shardmap, opts) require.NoError(t, err, "vtexplain Init error\n%s", string(schema)) return vte } @@ -349,7 +346,7 @@ func TestInit(t *testing.T) { } }` schema := "create table table_missing_primary_vindex (id int primary key)" - _, err := Init(ctx, vschema, schema, "", defaultTestOpts(), collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion) + _, err := Init(ctx, vtenv.NewTestEnv(), vschema, schema, "", defaultTestOpts()) require.Error(t, err) require.Contains(t, err.Error(), "missing primary col vindex") } diff --git a/go/vt/vtexplain/vtexplain_vtgate.go b/go/vt/vtexplain/vtexplain_vtgate.go index 79c76a6dae0..f24725fcd7b 100644 --- a/go/vt/vtexplain/vtexplain_vtgate.go +++ b/go/vt/vtexplain/vtexplain_vtgate.go @@ -75,7 +75,7 @@ func (vte *VTExplain) initVtgateExecutor(ctx context.Context, vSchemaStr, ksShar var schemaTracker vtgate.SchemaInfo // no schema tracker for these tests queryLogBufferSize := 10 plans := theine.NewStore[vtgate.PlanCacheKey, *engine.Plan](4*1024*1024, false) - vte.vtgateExecutor = vtgate.NewExecutor(ctx, vte.explainTopo, vtexplainCell, resolver, opts.Normalize, false, streamSize, plans, schemaTracker, false, opts.PlannerVersion, 0, vte.collationEnv, vte.parser, vte.mysqlVersion) + vte.vtgateExecutor = vtgate.NewExecutor(ctx, vte.env, vte.explainTopo, vtexplainCell, resolver, opts.Normalize, false, streamSize, plans, schemaTracker, false, opts.PlannerVersion, 0) vte.vtgateExecutor.SetQueryLogger(streamlog.New[*logstats.LogStats]("VTGate", queryLogBufferSize)) return nil @@ -107,7 +107,7 @@ func (vte *VTExplain) buildTopology(ctx context.Context, opts *Options, vschemaS if err != nil { return err } - schema := vindexes.BuildVSchema(&srvVSchema, vte.parser) + schema := vindexes.BuildVSchema(&srvVSchema, vte.env.Parser()) for ks, ksSchema := range schema.Keyspaces { if ksSchema.Error != nil { return vterrors.Wrapf(ksSchema.Error, "vschema failed to load on keyspace [%s]", ks) @@ -143,7 +143,7 @@ func (vte *VTExplain) buildTopology(ctx context.Context, opts *Options, vschemaS log.Infof("registering test tablet %s for keyspace %s shard %s", hostname, ks, shard.Name) tablet := vte.healthCheck.AddFakeTablet(vtexplainCell, hostname, 1, ks, shard.Name, topodatapb.TabletType_PRIMARY, true, 1, nil, func(t *topodatapb.Tablet) queryservice.QueryService { - return vte.newTablet(ctx, opts, t, vte.collationEnv, vte.parser, vte.mysqlVersion) + return vte.newTablet(ctx, vte.env, opts, t) }) vte.explainTopo.TabletConns[hostname] = tablet.(*explainTablet) vte.explainTopo.KeyspaceShards[ks][shard.Name] = shard diff --git a/go/vt/vtexplain/vtexplain_vttablet.go b/go/vt/vtexplain/vtexplain_vttablet.go index 6777d84c81a..a227488925d 100644 --- a/go/vt/vtexplain/vtexplain_vttablet.go +++ b/go/vt/vtexplain/vtexplain_vttablet.go @@ -24,6 +24,7 @@ import ( "sync" "vitess.io/vitess/go/vt/sidecardb" + "vitess.io/vitess/go/vt/vtenv" "vitess.io/vitess/go/mysql" "vitess.io/vitess/go/mysql/collations" @@ -104,9 +105,9 @@ type explainTablet struct { var _ queryservice.QueryService = (*explainTablet)(nil) -func (vte *VTExplain) newTablet(ctx context.Context, opts *Options, t *topodatapb.Tablet, collationEnv *collations.Environment, parser *sqlparser.Parser, mysqlVersion string) *explainTablet { +func (vte *VTExplain) newTablet(ctx context.Context, env *vtenv.Environment, opts *Options, t *topodatapb.Tablet) *explainTablet { db := fakesqldb.New(nil) - sidecardb.AddSchemaInitQueries(db, true, vte.parser) + sidecardb.AddSchemaInitQueries(db, true, env.Parser()) config := tabletenv.NewCurrentConfig() config.TrackSchemaVersions = false @@ -119,9 +120,9 @@ func (vte *VTExplain) newTablet(ctx context.Context, opts *Options, t *topodatap config.EnableTableGC = false // XXX much of this is cloned from the tabletserver tests - tsv := tabletserver.NewTabletServer(ctx, topoproto.TabletAliasString(t.Alias), config, memorytopo.NewServer(ctx, ""), t.Alias, collationEnv, parser, mysqlVersion) + tsv := tabletserver.NewTabletServer(ctx, env, topoproto.TabletAliasString(t.Alias), config, memorytopo.NewServer(ctx, ""), t.Alias) - tablet := explainTablet{db: db, tsv: tsv, vte: vte, collationEnv: collationEnv} + tablet := explainTablet{db: db, tsv: tsv, vte: vte, collationEnv: env.CollationEnv()} db.Handler = &tablet tablet.QueryService = queryservice.Wrap( @@ -592,7 +593,7 @@ func (t *explainTablet) handleSelect(query string) (*sqltypes.Result, error) { // Parse the select statement to figure out the table and columns // that were referenced so that the synthetic response has the // expected field names and types. - stmt, err := t.vte.parser.Parse(query) + stmt, err := t.vte.env.Parser().Parse(query) if err != nil { return nil, err } diff --git a/go/vt/vtexplain/vtexplain_vttablet_test.go b/go/vt/vtexplain/vtexplain_vttablet_test.go index fcc2c4a6966..8de1a146ce6 100644 --- a/go/vt/vtexplain/vtexplain_vttablet_test.go +++ b/go/vt/vtexplain/vtexplain_vttablet_test.go @@ -24,11 +24,9 @@ import ( "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" - "vitess.io/vitess/go/vt/sqlparser" - "vitess.io/vitess/go/mysql/collations" - "vitess.io/vitess/go/mysql/config" - + "vitess.io/vitess/go/vt/sqlparser" + "vitess.io/vitess/go/vt/vtenv" "vitess.io/vitess/go/vt/vttablet/tabletserver/schema" topodatapb "vitess.io/vitess/go/vt/proto/topodata" @@ -75,9 +73,7 @@ create table t2 ( ctx, cancel := context.WithCancel(context.Background()) defer cancel() - collationEnv := collations.MySQL8() - parser := sqlparser.NewTestParser() - vte, err := Init(ctx, testVSchema, testSchema, "", opts, collationEnv, parser, config.DefaultMySQLVersion) + vte, err := Init(ctx, vtenv.NewTestEnv(), testVSchema, testSchema, "", opts) require.NoError(t, err) defer vte.Stop() @@ -124,9 +120,8 @@ create table test_partitioned ( PARTITION p2018_06_16 VALUES LESS THAN (1529132400) ENGINE = InnoDB, PARTITION p2018_06_17 VALUES LESS THAN (1529218800) ENGINE = InnoDB)*/; ` - collationEnv := collations.MySQL8() - parser := sqlparser.NewTestParser() - ddls, err := parseSchema(testSchema, &Options{StrictDDL: false}, parser) + env := vtenv.NewTestEnv() + ddls, err := parseSchema(testSchema, &Options{StrictDDL: false}, env.Parser()) if err != nil { t.Fatalf("parseSchema: %v", err) } @@ -136,14 +131,14 @@ create table test_partitioned ( vte := initTest(ctx, ModeMulti, defaultTestOpts(), &testopts{}, t) defer vte.Stop() - tabletEnv, _ := newTabletEnvironment(ddls, defaultTestOpts(), collationEnv) + tabletEnv, _ := newTabletEnvironment(ddls, defaultTestOpts(), env.CollationEnv()) vte.setGlobalTabletEnv(tabletEnv) - tablet := vte.newTablet(ctx, defaultTestOpts(), &topodatapb.Tablet{ + tablet := vte.newTablet(ctx, env, defaultTestOpts(), &topodatapb.Tablet{ Keyspace: "test_keyspace", Shard: "-80", Alias: &topodatapb.TabletAlias{}, - }, collationEnv, parser, config.DefaultMySQLVersion) + }) se := tablet.tsv.SchemaEngine() tables := se.GetSchema() diff --git a/go/vt/vtgate/engine/distinct.go b/go/vt/vtgate/engine/distinct.go index 6eea40d281a..e292d516d51 100644 --- a/go/vt/vtgate/engine/distinct.go +++ b/go/vt/vtgate/engine/distinct.go @@ -183,7 +183,7 @@ func (d *Distinct) TryExecute(ctx context.Context, vcursor VCursor, bindVars map InsertID: input.InsertID, } - pt := newProbeTable(d.CheckCols, vcursor.CollationEnv()) + pt := newProbeTable(d.CheckCols, vcursor.Environment().CollationEnv()) for _, row := range input.Rows { exists, err := pt.exists(row) @@ -204,7 +204,7 @@ func (d *Distinct) TryExecute(ctx context.Context, vcursor VCursor, bindVars map func (d *Distinct) TryStreamExecute(ctx context.Context, vcursor VCursor, bindVars map[string]*querypb.BindVariable, wantfields bool, callback func(*sqltypes.Result) error) error { var mu sync.Mutex - pt := newProbeTable(d.CheckCols, vcursor.CollationEnv()) + pt := newProbeTable(d.CheckCols, vcursor.Environment().CollationEnv()) err := vcursor.StreamExecutePrimitive(ctx, d.Source, bindVars, wantfields, func(input *sqltypes.Result) error { result := &sqltypes.Result{ Fields: input.Fields, diff --git a/go/vt/vtgate/engine/fake_vcursor_test.go b/go/vt/vtgate/engine/fake_vcursor_test.go index 019592621b5..08a40c0d835 100644 --- a/go/vt/vtgate/engine/fake_vcursor_test.go +++ b/go/vt/vtgate/engine/fake_vcursor_test.go @@ -37,6 +37,7 @@ import ( "vitess.io/vitess/go/vt/key" "vitess.io/vitess/go/vt/sqlparser" "vitess.io/vitess/go/vt/srvtopo" + "vitess.io/vitess/go/vt/vtenv" "vitess.io/vitess/go/vt/vtgate/vindexes" binlogdatapb "vitess.io/vitess/go/vt/proto/binlogdata" @@ -134,17 +135,8 @@ func (t *noopVCursor) ConnCollation() collations.ID { } // CollationEnv implements VCursor -func (t *noopVCursor) CollationEnv() *collations.Environment { - return collations.MySQL8() -} - -// SQLParser implements VCursor -func (t *noopVCursor) SQLParser() *sqlparser.Parser { - return sqlparser.NewTestParser() -} - -func (*noopVCursor) MySQLVersion() string { - return config.DefaultMySQLVersion +func (t *noopVCursor) Environment() *vtenv.Environment { + return vtenv.NewTestEnv() } func (t *noopVCursor) TimeZone() *time.Location { diff --git a/go/vt/vtgate/engine/filter_test.go b/go/vt/vtgate/engine/filter_test.go index ac4ea56e174..fc888019dfe 100644 --- a/go/vt/vtgate/engine/filter_test.go +++ b/go/vt/vtgate/engine/filter_test.go @@ -24,9 +24,9 @@ import ( "github.com/stretchr/testify/require" "vitess.io/vitess/go/mysql/collations" - "vitess.io/vitess/go/mysql/config" "vitess.io/vitess/go/sqltypes" "vitess.io/vitess/go/vt/sqlparser" + "vitess.io/vitess/go/vt/vtenv" "vitess.io/vitess/go/vt/vtgate/evalengine" ) @@ -73,8 +73,7 @@ func TestFilterPass(t *testing.T) { pred, err := evalengine.Translate(predicate, &evalengine.Config{ Collation: utf8mb4Bin, ResolveColumn: evalengine.FieldResolver(tc.res.Fields).Column, - CollationEnv: collations.MySQL8(), - MySQLVersion: config.DefaultMySQLVersion, + Environment: vtenv.NewTestEnv(), }) require.NoError(t, err) @@ -132,8 +131,7 @@ func TestFilterStreaming(t *testing.T) { pred, err := evalengine.Translate(predicate, &evalengine.Config{ Collation: utf8mb4Bin, ResolveColumn: evalengine.FieldResolver(tc.res[0].Fields).Column, - CollationEnv: collations.MySQL8(), - MySQLVersion: config.DefaultMySQLVersion, + Environment: vtenv.NewTestEnv(), }) require.NoError(t, err) diff --git a/go/vt/vtgate/engine/online_ddl.go b/go/vt/vtgate/engine/online_ddl.go index 62126da4d08..6025f4ed623 100644 --- a/go/vt/vtgate/engine/online_ddl.go +++ b/go/vt/vtgate/engine/online_ddl.go @@ -78,7 +78,7 @@ func (v *OnlineDDL) TryExecute(ctx context.Context, vcursor VCursor, bindVars ma { Name: "uuid", Type: sqltypes.VarChar, - Charset: uint32(vcursor.CollationEnv().DefaultConnectionCharset()), + Charset: uint32(vcursor.ConnCollation()), }, }, Rows: [][]sqltypes.Value{}, @@ -89,7 +89,7 @@ func (v *OnlineDDL) TryExecute(ctx context.Context, vcursor VCursor, bindVars ma migrationContext = fmt.Sprintf("vtgate:%s", vcursor.Session().GetSessionUUID()) } onlineDDLs, err := schema.NewOnlineDDLs(v.GetKeyspaceName(), v.SQL, v.DDL, - v.DDLStrategySetting, migrationContext, "", vcursor.SQLParser(), + v.DDLStrategySetting, migrationContext, "", vcursor.Environment().Parser(), ) if err != nil { return result, err diff --git a/go/vt/vtgate/engine/primitive.go b/go/vt/vtgate/engine/primitive.go index 66540be89ea..1c0e7de7a19 100644 --- a/go/vt/vtgate/engine/primitive.go +++ b/go/vt/vtgate/engine/primitive.go @@ -25,6 +25,7 @@ import ( "vitess.io/vitess/go/vt/key" "vitess.io/vitess/go/vt/sqlparser" "vitess.io/vitess/go/vt/srvtopo" + "vitess.io/vitess/go/vt/vtenv" "vitess.io/vitess/go/vt/vtgate/vindexes" binlogdatapb "vitess.io/vitess/go/vt/proto/binlogdata" @@ -87,11 +88,9 @@ type ( Session() SessionActions ConnCollation() collations.ID - CollationEnv() *collations.Environment - SQLParser() *sqlparser.Parser + Environment() *vtenv.Environment TimeZone() *time.Location SQLMode() string - MySQLVersion() string ExecuteLock(ctx context.Context, rs *srvtopo.ResolvedShard, query *querypb.BoundQuery, lockFuncType sqlparser.LockingFuncType) (*sqltypes.Result, error) diff --git a/go/vt/vtgate/engine/projection_test.go b/go/vt/vtgate/engine/projection_test.go index 6e6326c4a4a..af3b23fdf03 100644 --- a/go/vt/vtgate/engine/projection_test.go +++ b/go/vt/vtgate/engine/projection_test.go @@ -25,11 +25,11 @@ import ( "github.com/stretchr/testify/require" "vitess.io/vitess/go/mysql/collations" - "vitess.io/vitess/go/mysql/config" "vitess.io/vitess/go/sqltypes" querypb "vitess.io/vitess/go/vt/proto/query" "vitess.io/vitess/go/vt/sqlparser" + "vitess.io/vitess/go/vt/vtenv" "vitess.io/vitess/go/vt/vtgate/evalengine" ) @@ -40,9 +40,8 @@ func TestMultiply(t *testing.T) { Right: &sqlparser.Offset{V: 1}, } evalExpr, err := evalengine.Translate(expr, &evalengine.Config{ - CollationEnv: collations.MySQL8(), - Collation: collations.MySQL8().DefaultConnectionCharset(), - MySQLVersion: config.DefaultMySQLVersion, + Environment: vtenv.NewTestEnv(), + Collation: collations.MySQL8().DefaultConnectionCharset(), }) require.NoError(t, err) fp := &fakePrimitive{ @@ -84,9 +83,8 @@ func TestProjectionStreaming(t *testing.T) { Right: &sqlparser.Offset{V: 1}, } evalExpr, err := evalengine.Translate(expr, &evalengine.Config{ - CollationEnv: collations.MySQL8(), - Collation: collations.MySQL8().DefaultConnectionCharset(), - MySQLVersion: config.DefaultMySQLVersion, + Environment: vtenv.NewTestEnv(), + Collation: collations.MySQL8().DefaultConnectionCharset(), }) require.NoError(t, err) fp := &fakePrimitive{ @@ -131,9 +129,8 @@ func TestEmptyInput(t *testing.T) { Right: &sqlparser.Offset{V: 1}, } evalExpr, err := evalengine.Translate(expr, &evalengine.Config{ - CollationEnv: collations.MySQL8(), - Collation: collations.MySQL8().DefaultConnectionCharset(), - MySQLVersion: config.DefaultMySQLVersion, + Environment: vtenv.NewTestEnv(), + Collation: collations.MySQL8().DefaultConnectionCharset(), }) require.NoError(t, err) fp := &fakePrimitive{ @@ -165,9 +162,8 @@ func TestEmptyInput(t *testing.T) { func TestHexAndBinaryArgument(t *testing.T) { hexExpr, err := evalengine.Translate(sqlparser.NewArgument("vtg1"), &evalengine.Config{ - CollationEnv: collations.MySQL8(), - Collation: collations.MySQL8().DefaultConnectionCharset(), - MySQLVersion: config.DefaultMySQLVersion, + Environment: vtenv.NewTestEnv(), + Collation: collations.MySQL8().DefaultConnectionCharset(), }) require.NoError(t, err) proj := &Projection{ @@ -213,9 +209,8 @@ func TestFields(t *testing.T) { for _, testCase := range testCases { t.Run(testCase.name, func(t *testing.T) { bindExpr, err := evalengine.Translate(sqlparser.NewArgument("vtg1"), &evalengine.Config{ - CollationEnv: collations.MySQL8(), - Collation: collations.MySQL8().DefaultConnectionCharset(), - MySQLVersion: config.DefaultMySQLVersion, + Environment: vtenv.NewTestEnv(), + Collation: collations.MySQL8().DefaultConnectionCharset(), }) require.NoError(t, err) proj := &Projection{ diff --git a/go/vt/vtgate/engine/revert_migration.go b/go/vt/vtgate/engine/revert_migration.go index 23275ddd043..b60d6196228 100644 --- a/go/vt/vtgate/engine/revert_migration.go +++ b/go/vt/vtgate/engine/revert_migration.go @@ -88,7 +88,7 @@ func (v *RevertMigration) TryExecute(ctx context.Context, vcursor VCursor, bindV return nil, err } ddlStrategySetting.Strategy = schema.DDLStrategyOnline // and we keep the options as they were - onlineDDL, err := schema.NewOnlineDDL(v.GetKeyspaceName(), "", sql, ddlStrategySetting, fmt.Sprintf("vtgate:%s", vcursor.Session().GetSessionUUID()), "", vcursor.SQLParser()) + onlineDDL, err := schema.NewOnlineDDL(v.GetKeyspaceName(), "", sql, ddlStrategySetting, fmt.Sprintf("vtgate:%s", vcursor.Session().GetSessionUUID()), "", vcursor.Environment().Parser()) if err != nil { return result, err } diff --git a/go/vt/vtgate/engine/vexplain.go b/go/vt/vtgate/engine/vexplain.go index 23b8ebc2139..010901021fa 100644 --- a/go/vt/vtgate/engine/vexplain.go +++ b/go/vt/vtgate/engine/vexplain.go @@ -122,7 +122,7 @@ func (v *VExplain) convertToVExplainAllResult(ctx context.Context, vcursor VCurs explainQuery := fmt.Sprintf("explain format = json %v", entry.Query) // We rely on the parser to see if the query we have is explainable or not // If we get an error in parsing then we can't execute explain on the given query, and we skip it - _, err := vcursor.SQLParser().Parse(explainQuery) + _, err := vcursor.Environment().Parser().Parse(explainQuery) if err != nil { continue } diff --git a/go/vt/vtgate/evalengine/api_compare_test.go b/go/vt/vtgate/evalengine/api_compare_test.go index a1e63a3f643..b483f8e52c0 100644 --- a/go/vt/vtgate/evalengine/api_compare_test.go +++ b/go/vt/vtgate/evalengine/api_compare_test.go @@ -30,9 +30,9 @@ import ( "github.com/stretchr/testify/require" "vitess.io/vitess/go/mysql/collations" - "vitess.io/vitess/go/mysql/config" vtrpcpb "vitess.io/vitess/go/vt/proto/vtrpc" "vitess.io/vitess/go/vt/sqlparser" + "vitess.io/vitess/go/vt/vtenv" "vitess.io/vitess/go/vt/vterrors" "vitess.io/vitess/go/sqltypes" @@ -71,13 +71,13 @@ func (tc testCase) run(t *testing.T) { for i, value := range tc.row { fields[i] = &querypb.Field{Type: value.Type()} } - env := NewExpressionEnv(context.Background(), tc.bv, NewEmptyVCursor(collations.MySQL8(), time.UTC, config.DefaultMySQLVersion)) + venv := vtenv.NewTestEnv() + env := NewExpressionEnv(context.Background(), tc.bv, NewEmptyVCursor(venv, time.UTC)) env.Row = tc.row ast := &astCompiler{ cfg: &Config{ - Collation: collations.CollationUtf8mb4ID, - CollationEnv: collations.MySQL8(), - MySQLVersion: config.DefaultMySQLVersion, + Collation: collations.CollationUtf8mb4ID, + Environment: venv, }, } cmp, err := ast.translateComparisonExpr2(tc.op, tc.v1, tc.v2) diff --git a/go/vt/vtgate/evalengine/cached_size.go b/go/vt/vtgate/evalengine/cached_size.go index dfc1c349fc9..983e5f05cea 100644 --- a/go/vt/vtgate/evalengine/cached_size.go +++ b/go/vt/vtgate/evalengine/cached_size.go @@ -397,12 +397,12 @@ func (cached *UntypedExpr) CachedSize(alloc bool) int64 { if alloc { size += int64(96) } + // field env *vitess.io/vitess/go/vt/vtenv.Environment + size += cached.env.CachedSize(true) // field ir vitess.io/vitess/go/vt/vtgate/evalengine.IR if cc, ok := cached.ir.(cachedObject); ok { size += cc.CachedSize(true) } - // field collationEnv *vitess.io/vitess/go/mysql/collations.Environment - size += cached.collationEnv.CachedSize(true) // field needTypes []vitess.io/vitess/go/vt/vtgate/evalengine.typedIR { size += hack.RuntimeAllocSize(int64(cap(cached.needTypes)) * int64(16)) diff --git a/go/vt/vtgate/evalengine/compiler.go b/go/vt/vtgate/evalengine/compiler.go index fa4284e931d..5cb1426b55c 100644 --- a/go/vt/vtgate/evalengine/compiler.go +++ b/go/vt/vtgate/evalengine/compiler.go @@ -24,6 +24,7 @@ import ( "vitess.io/vitess/go/sqltypes" "vitess.io/vitess/go/vt/proto/vtrpc" "vitess.io/vitess/go/vt/sqlparser" + "vitess.io/vitess/go/vt/vtenv" "vitess.io/vitess/go/vt/vterrors" ) @@ -34,8 +35,7 @@ type compiler struct { dynamicTypes []ctype asm assembler sqlmode SQLMode - collationEnv *collations.Environment - mysqlVersion string + env *vtenv.Environment } type CompilerLog interface { @@ -421,7 +421,7 @@ func (c *compiler) compareNumericTypes(lt ctype, rt ctype) (swapped bool) { } func (c *compiler) compareAsStrings(lt ctype, rt ctype) error { - merged, coerceLeft, coerceRight, err := mergeCollations(lt.Col, rt.Col, lt.Type, rt.Type, c.collationEnv) + merged, coerceLeft, coerceRight, err := mergeCollations(lt.Col, rt.Col, lt.Type, rt.Type, c.env.CollationEnv()) if err != nil { return err } diff --git a/go/vt/vtgate/evalengine/compiler_test.go b/go/vt/vtgate/evalengine/compiler_test.go index 062be38655b..351f679a84a 100644 --- a/go/vt/vtgate/evalengine/compiler_test.go +++ b/go/vt/vtgate/evalengine/compiler_test.go @@ -27,10 +27,10 @@ import ( "github.com/olekukonko/tablewriter" "vitess.io/vitess/go/mysql/collations" - "vitess.io/vitess/go/mysql/config" "vitess.io/vitess/go/sqltypes" querypb "vitess.io/vitess/go/vt/proto/query" "vitess.io/vitess/go/vt/sqlparser" + "vitess.io/vitess/go/vt/vtenv" "vitess.io/vitess/go/vt/vtgate/evalengine" "vitess.io/vitess/go/vt/vtgate/evalengine/testcases" ) @@ -99,16 +99,16 @@ func TestCompilerReference(t *testing.T) { defer func() { evalengine.SystemTime = time.Now }() track := NewTracker() - parser := sqlparser.NewTestParser() + venv := vtenv.NewTestEnv() for _, tc := range testcases.Cases { t.Run(tc.Name(), func(t *testing.T) { var supported, total int - env := evalengine.EmptyExpressionEnv(collations.MySQL8(), config.DefaultMySQLVersion) + env := evalengine.EmptyExpressionEnv(venv) tc.Run(func(query string, row []sqltypes.Value) { env.Row = row - stmt, err := parser.ParseExpr(query) + stmt, err := venv.Parser().ParseExpr(query) if err != nil { // no need to test un-parseable queries return @@ -119,8 +119,7 @@ func TestCompilerReference(t *testing.T) { ResolveColumn: fields.Column, ResolveType: fields.Type, Collation: collations.CollationUtf8mb4ID, - CollationEnv: collations.MySQL8(), - MySQLVersion: config.DefaultMySQLVersion, + Environment: venv, NoConstantFolding: true, } @@ -607,11 +606,10 @@ func TestCompilerSingle(t *testing.T) { } tz, _ := time.LoadLocation("Europe/Madrid") - mysqlVersion := config.DefaultMySQLVersion - parser := sqlparser.NewTestParser() + venv := vtenv.NewTestEnv() for _, tc := range testCases { t.Run(tc.expression, func(t *testing.T) { - expr, err := parser.ParseExpr(tc.expression) + expr, err := venv.Parser().ParseExpr(tc.expression) if err != nil { t.Fatal(err) } @@ -621,8 +619,7 @@ func TestCompilerSingle(t *testing.T) { ResolveColumn: fields.Column, ResolveType: fields.Type, Collation: collations.CollationUtf8mb4ID, - CollationEnv: collations.MySQL8(), - MySQLVersion: config.DefaultMySQLVersion, + Environment: venv, NoConstantFolding: true, } @@ -631,7 +628,7 @@ func TestCompilerSingle(t *testing.T) { t.Fatal(err) } - env := evalengine.NewExpressionEnv(context.Background(), nil, evalengine.NewEmptyVCursor(collations.MySQL8(), tz, mysqlVersion)) + env := evalengine.NewExpressionEnv(context.Background(), nil, evalengine.NewEmptyVCursor(venv, tz)) env.SetTime(time.Date(2023, 10, 24, 12, 0, 0, 0, tz)) env.Row = tc.values @@ -689,10 +686,10 @@ func TestBindVarLiteral(t *testing.T) { }, } - parser := sqlparser.NewTestParser() + venv := vtenv.NewTestEnv() for _, tc := range testCases { t.Run(tc.expression, func(t *testing.T) { - expr, err := parser.ParseExpr(tc.expression) + expr, err := venv.Parser().ParseExpr(tc.expression) if err != nil { t.Fatal(err) } @@ -704,8 +701,7 @@ func TestBindVarLiteral(t *testing.T) { ResolveColumn: fields.Column, ResolveType: fields.Type, Collation: collations.CollationUtf8mb4ID, - CollationEnv: collations.MySQL8(), - MySQLVersion: config.DefaultMySQLVersion, + Environment: venv, NoConstantFolding: true, } @@ -716,7 +712,7 @@ func TestBindVarLiteral(t *testing.T) { result := `VARCHAR("ÿ")` - env := evalengine.EmptyExpressionEnv(collations.MySQL8(), config.DefaultMySQLVersion) + env := evalengine.EmptyExpressionEnv(venv) env.BindVars = map[string]*querypb.BindVariable{ "vtg1": tc.bindVar, } @@ -756,18 +752,17 @@ func TestCompilerNonConstant(t *testing.T) { }, } - parser := sqlparser.NewTestParser() + venv := vtenv.NewTestEnv() for _, tc := range testCases { t.Run(tc.expression, func(t *testing.T) { - expr, err := parser.ParseExpr(tc.expression) + expr, err := venv.Parser().ParseExpr(tc.expression) if err != nil { t.Fatal(err) } cfg := &evalengine.Config{ Collation: collations.CollationUtf8mb4ID, - CollationEnv: collations.MySQL8(), - MySQLVersion: config.DefaultMySQLVersion, + Environment: venv, NoConstantFolding: true, } @@ -776,7 +771,7 @@ func TestCompilerNonConstant(t *testing.T) { t.Fatal(err) } - env := evalengine.EmptyExpressionEnv(collations.MySQL8(), config.DefaultMySQLVersion) + env := evalengine.EmptyExpressionEnv(venv) var prev string for i := 0; i < 1000; i++ { expected, err := env.EvaluateAST(converted) diff --git a/go/vt/vtgate/evalengine/expr_collate.go b/go/vt/vtgate/evalengine/expr_collate.go index a3613204ada..b54f679bee3 100644 --- a/go/vt/vtgate/evalengine/expr_collate.go +++ b/go/vt/vtgate/evalengine/expr_collate.go @@ -111,7 +111,7 @@ func (expr *CollateExpr) compile(c *compiler) (ctype, error) { switch ct.Type { case sqltypes.VarChar: - if err := c.collationEnv.EnsureCollate(ct.Col.Collation, expr.TypedCollation.Collation); err != nil { + if err := c.env.CollationEnv().EnsureCollate(ct.Col.Collation, expr.TypedCollation.Collation); err != nil { return ctype{}, vterrors.New(vtrpcpb.Code_INVALID_ARGUMENT, err.Error()) } fallthrough diff --git a/go/vt/vtgate/evalengine/expr_compare.go b/go/vt/vtgate/evalengine/expr_compare.go index 131f0bafe95..0cd90c4e9ff 100644 --- a/go/vt/vtgate/evalengine/expr_compare.go +++ b/go/vt/vtgate/evalengine/expr_compare.go @@ -312,25 +312,25 @@ func (c *ComparisonExpr) eval(env *ExpressionEnv) (eval, error) { func (expr *ComparisonExpr) compileAsTuple(c *compiler) (ctype, error) { switch expr.Op.(type) { case compareNullSafeEQ: - c.asm.CmpTupleNullsafe(c.collationEnv) + c.asm.CmpTupleNullsafe(c.env.CollationEnv()) return ctype{Type: sqltypes.Int64, Col: collationNumeric, Flag: flagIsBoolean}, nil case compareEQ: - c.asm.CmpTuple(c.collationEnv, true) + c.asm.CmpTuple(c.env.CollationEnv(), true) c.asm.Cmp_eq_n() case compareNE: - c.asm.CmpTuple(c.collationEnv, true) + c.asm.CmpTuple(c.env.CollationEnv(), true) c.asm.Cmp_ne_n() case compareLT: - c.asm.CmpTuple(c.collationEnv, false) + c.asm.CmpTuple(c.env.CollationEnv(), false) c.asm.Cmp_lt_n() case compareLE: - c.asm.CmpTuple(c.collationEnv, false) + c.asm.CmpTuple(c.env.CollationEnv(), false) c.asm.Cmp_le_n() case compareGT: - c.asm.CmpTuple(c.collationEnv, false) + c.asm.CmpTuple(c.env.CollationEnv(), false) c.asm.Cmp_gt_n() case compareGE: - c.asm.CmpTuple(c.collationEnv, false) + c.asm.CmpTuple(c.env.CollationEnv(), false) c.asm.Cmp_ge_n() default: panic("invalid comparison operator") @@ -563,7 +563,7 @@ func (expr *InExpr) compile(c *compiler) (ctype, error) { if err != nil { return ctype{}, err } - c.asm.In_slow(c.collationEnv, expr.Negate) + c.asm.In_slow(c.env.CollationEnv(), expr.Negate) } return ctype{Type: sqltypes.Int64, Col: collationNumeric, Flag: flagIsBoolean | (nullableFlags(lhs.Flag) | (rt.Flag & flagNullable))}, nil @@ -645,7 +645,7 @@ func (expr *LikeExpr) compile(c *compiler) (ctype, error) { var coerceRight colldata.Coercion if lt.Col.Collation != rt.Col.Collation { - merged, coerceLeft, coerceRight, err = colldata.Merge(c.collationEnv, lt.Col, rt.Col, colldata.CoercionOptions{ + merged, coerceLeft, coerceRight, err = colldata.Merge(c.env.CollationEnv(), lt.Col, rt.Col, colldata.CoercionOptions{ ConvertToSuperset: true, ConvertWithCoercion: true, }) diff --git a/go/vt/vtgate/evalengine/expr_env.go b/go/vt/vtgate/evalengine/expr_env.go index ab4f59d92ff..6e09b03cffb 100644 --- a/go/vt/vtgate/evalengine/expr_env.go +++ b/go/vt/vtgate/evalengine/expr_env.go @@ -27,14 +27,14 @@ import ( "vitess.io/vitess/go/sqltypes" "vitess.io/vitess/go/vt/callerid" querypb "vitess.io/vitess/go/vt/proto/query" + "vitess.io/vitess/go/vt/vtenv" ) type VCursor interface { TimeZone() *time.Location GetKeyspace() string SQLMode() string - CollationEnv() *collations.Environment - MySQLVersion() string + Environment() *vtenv.Environment } type ( @@ -79,7 +79,7 @@ func (env *ExpressionEnv) currentDatabase() string { } func (env *ExpressionEnv) currentVersion() string { - return env.vc.MySQLVersion() + return env.vc.Environment().MySQLVersion() } func (env *ExpressionEnv) currentTimezone() *time.Location { @@ -121,9 +121,12 @@ func (env *ExpressionEnv) VCursor() VCursor { } type emptyVCursor struct { - collationEnv *collations.Environment - tz *time.Location - mysqlVersion string + env *vtenv.Environment + tz *time.Location +} + +func (e *emptyVCursor) Environment() *vtenv.Environment { + return e.env } func (e *emptyVCursor) TimeZone() *time.Location { @@ -138,21 +141,13 @@ func (e *emptyVCursor) SQLMode() string { return config.DefaultSQLMode } -func (e *emptyVCursor) CollationEnv() *collations.Environment { - return e.collationEnv -} - -func (e *emptyVCursor) MySQLVersion() string { - return e.mysqlVersion -} - -func NewEmptyVCursor(collationEnv *collations.Environment, tz *time.Location, mysqlVersion string) VCursor { - return &emptyVCursor{collationEnv: collationEnv, tz: tz, mysqlVersion: mysqlVersion} +func NewEmptyVCursor(env *vtenv.Environment, tz *time.Location) VCursor { + return &emptyVCursor{env: env, tz: tz} } // EmptyExpressionEnv returns a new ExpressionEnv with no bind vars or row -func EmptyExpressionEnv(collationEnv *collations.Environment, mysqlVersion string) *ExpressionEnv { - return NewExpressionEnv(context.Background(), nil, NewEmptyVCursor(collationEnv, time.Local, mysqlVersion)) +func EmptyExpressionEnv(env *vtenv.Environment) *ExpressionEnv { + return NewExpressionEnv(context.Background(), nil, NewEmptyVCursor(env, time.Local)) } // NewExpressionEnv returns an expression environment with no current row, but with bindvars @@ -161,7 +156,7 @@ func NewExpressionEnv(ctx context.Context, bindVars map[string]*querypb.BindVari env.user = callerid.ImmediateCallerIDFromContext(ctx) env.SetTime(time.Now()) env.sqlmode = ParseSQLMode(vc.SQLMode()) - env.collationEnv = vc.CollationEnv() + env.collationEnv = vc.Environment().CollationEnv() return env } diff --git a/go/vt/vtgate/evalengine/expr_logical.go b/go/vt/vtgate/evalengine/expr_logical.go index c9332e613c3..ef59616b97c 100644 --- a/go/vt/vtgate/evalengine/expr_logical.go +++ b/go/vt/vtgate/evalengine/expr_logical.go @@ -691,7 +691,7 @@ func (cs *CaseExpr) compile(c *compiler) (ctype, error) { } ta.add(then.Type, then.Flag) - if err := ca.add(then.Col, c.collationEnv); err != nil { + if err := ca.add(then.Col, c.env.CollationEnv()); err != nil { return ctype{}, err } } @@ -703,7 +703,7 @@ func (cs *CaseExpr) compile(c *compiler) (ctype, error) { } ta.add(els.Type, els.Flag) - if err := ca.add(els.Col, c.collationEnv); err != nil { + if err := ca.add(els.Col, c.env.CollationEnv()); err != nil { return ctype{}, err } } diff --git a/go/vt/vtgate/evalengine/fn_compare.go b/go/vt/vtgate/evalengine/fn_compare.go index 1af01048315..1303ac7614d 100644 --- a/go/vt/vtgate/evalengine/fn_compare.go +++ b/go/vt/vtgate/evalengine/fn_compare.go @@ -72,7 +72,7 @@ func (b *builtinCoalesce) compile(c *compiler) (ctype, error) { f = 0 } ta.add(tt.Type, tt.Flag) - if err := ca.add(tt.Col, c.collationEnv); err != nil { + if err := ca.add(tt.Col, c.env.CollationEnv()); err != nil { return ctype{}, err } } @@ -288,7 +288,7 @@ func (call *builtinMultiComparison) compile_c(c *compiler, args []ctype) (ctype, var f typeFlag for _, arg := range args { f |= nullableFlags(arg.Flag) - if err := ca.add(arg.Col, c.collationEnv); err != nil { + if err := ca.add(arg.Col, c.env.CollationEnv()); err != nil { return ctype{}, err } } diff --git a/go/vt/vtgate/evalengine/fn_regexp.go b/go/vt/vtgate/evalengine/fn_regexp.go index 848b8128500..d0d11e4bc57 100644 --- a/go/vt/vtgate/evalengine/fn_regexp.go +++ b/go/vt/vtgate/evalengine/fn_regexp.go @@ -218,7 +218,7 @@ func compileConstantRegex(c *compiler, args TupleExpr, pat, mt int, cs collation return nil, errNonConstantRegexp } var err error - staticEnv := EmptyExpressionEnv(c.collationEnv, c.mysqlVersion) + staticEnv := EmptyExpressionEnv(c.env) pattern, err = simplifyExpr(staticEnv, pattern) if err != nil { return nil, err @@ -348,7 +348,7 @@ func (r *builtinRegexpLike) compile(c *compiler) (ctype, error) { skips = append(skips, c.compileNullCheckArg(f, 2)) } - merged, flags, err := compileRegexpCollation(c.collationEnv, input, pat, "regexp_like") + merged, flags, err := compileRegexpCollation(c.env.CollationEnv(), input, pat, "regexp_like") if err != nil { return ctype{}, err } @@ -555,7 +555,7 @@ func (r *builtinRegexpInstr) compile(c *compiler) (ctype, error) { } } - merged, flags, err := compileRegexpCollation(c.collationEnv, input, pat, "regexp_instr") + merged, flags, err := compileRegexpCollation(c.env.CollationEnv(), input, pat, "regexp_instr") if err != nil { return ctype{}, err } @@ -732,7 +732,7 @@ func (r *builtinRegexpSubstr) compile(c *compiler) (ctype, error) { } } - merged, flags, err := compileRegexpCollation(c.collationEnv, input, pat, "regexp_substr") + merged, flags, err := compileRegexpCollation(c.env.CollationEnv(), input, pat, "regexp_substr") if err != nil { return ctype{}, err } @@ -972,7 +972,7 @@ func (r *builtinRegexpReplace) compile(c *compiler) (ctype, error) { } } - merged, flags, err := compileRegexpCollation(c.collationEnv, input, pat, "regexp_replace") + merged, flags, err := compileRegexpCollation(c.env.CollationEnv(), input, pat, "regexp_replace") if err != nil { return ctype{}, err } diff --git a/go/vt/vtgate/evalengine/fn_string.go b/go/vt/vtgate/evalengine/fn_string.go index 8ab7a23d461..f69b8db1e72 100644 --- a/go/vt/vtgate/evalengine/fn_string.go +++ b/go/vt/vtgate/evalengine/fn_string.go @@ -412,7 +412,7 @@ func (expr *builtinCollation) compile(c *compiler) (ctype, error) { skip := c.asm.jumpFrom() - c.asm.Fn_COLLATION(c.collationEnv, collationUtf8mb3) + c.asm.Fn_COLLATION(c.env.CollationEnv(), collationUtf8mb3) c.asm.jumpDestination(skip) return ctype{Type: sqltypes.VarChar, Col: collationUtf8mb3}, nil @@ -800,7 +800,7 @@ func (expr *builtinStrcmp) compile(c *compiler) (ctype, error) { if sqltypes.IsNumber(lt.Type) || sqltypes.IsNumber(rt.Type) { mcol = collationNumeric } else { - mcol, _, _, err = colldata.Merge(c.collationEnv, lt.Col, rt.Col, colldata.CoercionOptions{ + mcol, _, _, err = colldata.Merge(c.env.CollationEnv(), lt.Col, rt.Col, colldata.CoercionOptions{ ConvertToSuperset: true, ConvertWithCoercion: true, }) @@ -1133,7 +1133,7 @@ func (call *builtinConcat) compile(c *compiler) (ctype, error) { args = append(args, a) tt = concatSQLType(a.Type, tt) - err = ca.add(a.Col, c.collationEnv) + err = ca.add(a.Col, c.env.CollationEnv()) if err != nil { return ctype{}, err } @@ -1256,7 +1256,7 @@ func (call *builtinConcatWs) compile(c *compiler) (ctype, error) { } tt = concatSQLType(a.Type, tt) - err = ca.add(a.Col, c.collationEnv) + err = ca.add(a.Col, c.env.CollationEnv()) if err != nil { return ctype{}, err } diff --git a/go/vt/vtgate/evalengine/integration/comparison_test.go b/go/vt/vtgate/evalengine/integration/comparison_test.go index d6271dd15ef..ea327601975 100644 --- a/go/vt/vtgate/evalengine/integration/comparison_test.go +++ b/go/vt/vtgate/evalengine/integration/comparison_test.go @@ -28,6 +28,7 @@ import ( "time" "github.com/spf13/pflag" + "github.com/stretchr/testify/require" "vitess.io/vitess/go/mysql" "vitess.io/vitess/go/mysql/collations" @@ -39,6 +40,7 @@ import ( querypb "vitess.io/vitess/go/vt/proto/query" "vitess.io/vitess/go/vt/proto/vtrpc" "vitess.io/vitess/go/vt/servenv" + "vitess.io/vitess/go/vt/vtenv" "vitess.io/vitess/go/vt/vtgate/evalengine" "vitess.io/vitess/go/vt/vtgate/evalengine/testcases" ) @@ -204,7 +206,7 @@ func compareRemoteExprEnv(t *testing.T, collationEnv *collations.Environment, en var seenGoldenTests []GoldenTest type vcursor struct { - mysqlVersion string + env *vtenv.Environment } func (vc *vcursor) GetKeyspace() string { @@ -219,12 +221,8 @@ func (vc *vcursor) SQLMode() string { return config.DefaultSQLMode } -func (vc *vcursor) CollationEnv() *collations.Environment { - return collations.MySQL8() -} - -func (vc *vcursor) MySQLVersion() string { - return vc.mysqlVersion +func (vc *vcursor) Environment() *vtenv.Environment { + return vc.env } func initTimezoneData(t *testing.T, conn *mysql.Conn) { @@ -263,12 +261,16 @@ func TestMySQL(t *testing.T) { servenv.OnParse(registerFlags) initTimezoneData(t, conn) + venv, err := vtenv.New(vtenv.Options{ + MySQLServerVersion: conn.ServerVersion, + }) + require.NoError(t, err) for _, tc := range testcases.Cases { t.Run(tc.Name(), func(t *testing.T) { ctx := callerid.NewContext(context.Background(), &vtrpc.CallerID{Principal: "testuser"}, &querypb.VTGateCallerID{ Username: "vt_dba", }) - env := evalengine.NewExpressionEnv(ctx, nil, &vcursor{mysqlVersion: conn.ServerVersion}) + env := evalengine.NewExpressionEnv(ctx, nil, &vcursor{env: venv}) tc.Run(func(query string, row []sqltypes.Value) { env.Row = row compareRemoteExprEnv(t, collationEnv, env, conn, query, tc.Schema, tc.Compare) diff --git a/go/vt/vtgate/evalengine/integration/fuzz_test.go b/go/vt/vtgate/evalengine/integration/fuzz_test.go index 59ecf9503dc..cb29b9c9c32 100644 --- a/go/vt/vtgate/evalengine/integration/fuzz_test.go +++ b/go/vt/vtgate/evalengine/integration/fuzz_test.go @@ -31,10 +31,10 @@ import ( "github.com/spf13/pflag" "vitess.io/vitess/go/mysql/collations" - "vitess.io/vitess/go/mysql/config" "vitess.io/vitess/go/sqltypes" querypb "vitess.io/vitess/go/vt/proto/query" "vitess.io/vitess/go/vt/sqlparser" + "vitess.io/vitess/go/vt/vtenv" "vitess.io/vitess/go/vt/vtgate/evalengine" "vitess.io/vitess/go/vt/vtgate/evalengine/testcases" "vitess.io/vitess/go/vt/vtgate/simplifier" @@ -147,9 +147,8 @@ func evaluateLocalEvalengine(env *evalengine.ExpressionEnv, query string, fields cfg := &evalengine.Config{ ResolveColumn: evalengine.FieldResolver(fields).Column, Collation: collations.CollationUtf8mb4ID, - CollationEnv: env.VCursor().CollationEnv(), + Environment: env.VCursor().Environment(), NoConstantFolding: !debugSimplify, - MySQLVersion: env.VCursor().MySQLVersion(), } expr, err = evalengine.Translate(astExpr, cfg) return @@ -199,10 +198,11 @@ func TestGenerateFuzzCases(t *testing.T) { var conn = mysqlconn(t) defer conn.Close() + venv := vtenv.NewTestEnv() compareWithMySQL := func(expr sqlparser.Expr) *mismatch { query := "SELECT " + sqlparser.String(expr) - env := evalengine.EmptyExpressionEnv(collations.MySQL8(), config.DefaultMySQLVersion) + env := evalengine.EmptyExpressionEnv(venv) eval, localErr := evaluateLocalEvalengine(env, query, nil) remote, remoteErr := conn.ExecuteFetch(query, 1, false) diff --git a/go/vt/vtgate/evalengine/mysql_test.go b/go/vt/vtgate/evalengine/mysql_test.go index 6beedbe110a..6434d7dcfbf 100644 --- a/go/vt/vtgate/evalengine/mysql_test.go +++ b/go/vt/vtgate/evalengine/mysql_test.go @@ -26,8 +26,8 @@ import ( "testing" "vitess.io/vitess/go/mysql/collations" - "vitess.io/vitess/go/mysql/config" "vitess.io/vitess/go/vt/sqlparser" + "vitess.io/vitess/go/vt/vtenv" ) func internalExpression(e Expr) IR { @@ -70,8 +70,7 @@ func convert(t *testing.T, query string, simplify bool) (Expr, error) { cfg := &Config{ Collation: collations.CollationUtf8mb4ID, - CollationEnv: collations.MySQL8(), - MySQLVersion: config.DefaultMySQLVersion, + Environment: vtenv.NewTestEnv(), NoConstantFolding: !simplify, } @@ -91,7 +90,7 @@ func testSingle(t *testing.T, query string) (EvalResult, error) { if err != nil { return EvalResult{}, err } - return EmptyExpressionEnv(collations.MySQL8(), config.DefaultMySQLVersion).Evaluate(converted) + return EmptyExpressionEnv(vtenv.NewTestEnv()).Evaluate(converted) } func TestMySQLGolden(t *testing.T) { diff --git a/go/vt/vtgate/evalengine/perf_test.go b/go/vt/vtgate/evalengine/perf_test.go index cb57e19a12a..f3e0b32de08 100644 --- a/go/vt/vtgate/evalengine/perf_test.go +++ b/go/vt/vtgate/evalengine/perf_test.go @@ -4,9 +4,8 @@ import ( "testing" "vitess.io/vitess/go/mysql/collations" - "vitess.io/vitess/go/mysql/config" "vitess.io/vitess/go/sqltypes" - "vitess.io/vitess/go/vt/sqlparser" + "vitess.io/vitess/go/vt/vtenv" "vitess.io/vitess/go/vt/vtgate/evalengine" ) @@ -23,9 +22,9 @@ func BenchmarkCompilerExpressions(b *testing.B) { {"comparison_f", "column0 = 12", []sqltypes.Value{sqltypes.NewFloat64(420.0)}}, } - parser := sqlparser.NewTestParser() + venv := vtenv.NewTestEnv() for _, tc := range testCases { - expr, err := parser.ParseExpr(tc.expression) + expr, err := venv.Parser().ParseExpr(tc.expression) if err != nil { b.Fatal(err) } @@ -35,8 +34,7 @@ func BenchmarkCompilerExpressions(b *testing.B) { ResolveColumn: fields.Column, ResolveType: fields.Type, Collation: collations.CollationUtf8mb4ID, - CollationEnv: collations.MySQL8(), - MySQLVersion: config.DefaultMySQLVersion, + Environment: venv, } translated, err := evalengine.Translate(expr, cfg) diff --git a/go/vt/vtgate/evalengine/translate.go b/go/vt/vtgate/evalengine/translate.go index f218c286245..d1c32b113c2 100644 --- a/go/vt/vtgate/evalengine/translate.go +++ b/go/vt/vtgate/evalengine/translate.go @@ -27,6 +27,7 @@ import ( querypb "vitess.io/vitess/go/vt/proto/query" vtrpcpb "vitess.io/vitess/go/vt/proto/vtrpc" "vitess.io/vitess/go/vt/sqlparser" + "vitess.io/vitess/go/vt/vtenv" "vitess.io/vitess/go/vt/vterrors" ) @@ -334,7 +335,7 @@ func (ast *astCompiler) translateCollateExpr(collate *sqlparser.CollateExpr) (IR if err != nil { return nil, err } - coll := ast.cfg.CollationEnv.LookupByName(collate.Collation) + coll := ast.cfg.Environment.CollationEnv().LookupByName(collate.Collation) if coll == collations.Unknown { return nil, vterrors.Errorf(vtrpcpb.Code_INVALID_ARGUMENT, "Unknown collation: '%s'", collate.Collation) } @@ -345,7 +346,7 @@ func (ast *astCompiler) translateCollateExpr(collate *sqlparser.CollateExpr) (IR Coercibility: collations.CoerceExplicit, Repertoire: collations.RepertoireUnicode, }, - CollationEnv: ast.cfg.CollationEnv, + CollationEnv: ast.cfg.Environment.CollationEnv(), }, nil } @@ -359,7 +360,7 @@ func (ast *astCompiler) translateIntroducerExpr(introduced *sqlparser.Introducer if strings.ToLower(introduced.CharacterSet) == "_binary" { collation = collations.CollationBinaryID } else { - defaultCollation := ast.cfg.CollationEnv.DefaultCollationForCharset(introduced.CharacterSet[1:]) + defaultCollation := ast.cfg.Environment.CollationEnv().DefaultCollationForCharset(introduced.CharacterSet[1:]) if defaultCollation == collations.Unknown { panic(fmt.Sprintf("unknown character set: %s", introduced.CharacterSet)) } @@ -390,7 +391,7 @@ func (ast *astCompiler) translateIntroducerExpr(introduced *sqlparser.Introducer Coercibility: collations.CoerceExplicit, Repertoire: collations.RepertoireUnicode, }, - CollationEnv: ast.cfg.CollationEnv, + CollationEnv: ast.cfg.Environment.CollationEnv(), }, nil default: panic("character set introducers are only supported for literals and arguments") @@ -422,7 +423,7 @@ func (ast *astCompiler) translateUnaryExpr(unary *sqlparser.UnaryExpr) (IR, erro case sqlparser.TildaOp: return &BitwiseNotExpr{UnaryExpr: UnaryExpr{expr}}, nil case sqlparser.NStringOp: - return &ConvertExpr{UnaryExpr: UnaryExpr{expr}, Type: "NCHAR", Collation: collations.CollationUtf8mb3ID, CollationEnv: ast.cfg.CollationEnv}, nil + return &ConvertExpr{UnaryExpr: UnaryExpr{expr}, Type: "NCHAR", Collation: collations.CollationUtf8mb3ID, CollationEnv: ast.cfg.Environment.CollationEnv()}, nil default: return nil, translateExprNotSupported(unary) } @@ -572,8 +573,7 @@ type Config struct { NoConstantFolding bool NoCompilation bool SQLMode SQLMode - CollationEnv *collations.Environment - MySQLVersion string + Environment *vtenv.Environment } func Translate(e sqlparser.Expr, cfg *Config) (Expr, error) { @@ -589,7 +589,7 @@ func Translate(e sqlparser.Expr, cfg *Config) (Expr, error) { } if !cfg.NoConstantFolding { - staticEnv := EmptyExpressionEnv(cfg.CollationEnv, cfg.MySQLVersion) + staticEnv := EmptyExpressionEnv(cfg.Environment) expr, err = simplifyExpr(staticEnv, expr) if err != nil { return nil, err @@ -601,15 +601,15 @@ func Translate(e sqlparser.Expr, cfg *Config) (Expr, error) { } if len(ast.untyped) == 0 && !cfg.NoCompilation { - comp := compiler{collation: cfg.Collation, collationEnv: cfg.CollationEnv, sqlmode: cfg.SQLMode, mysqlVersion: cfg.MySQLVersion} + comp := compiler{collation: cfg.Collation, env: cfg.Environment, sqlmode: cfg.SQLMode} return comp.compile(expr) } return &UntypedExpr{ - ir: expr, - collation: cfg.Collation, - collationEnv: cfg.CollationEnv, - needTypes: ast.untyped, + env: cfg.Environment, + ir: expr, + collation: cfg.Collation, + needTypes: ast.untyped, }, nil } @@ -625,11 +625,11 @@ type typedExpr struct { err error } -func (typed *typedExpr) compile(expr IR, collation collations.ID, collationEnv *collations.Environment, sqlmode SQLMode) (*CompiledExpr, error) { +func (typed *typedExpr) compile(env *vtenv.Environment, expr IR, collation collations.ID, sqlmode SQLMode) (*CompiledExpr, error) { typed.once.Do(func() { comp := compiler{ + env: env, collation: collation, - collationEnv: collationEnv, dynamicTypes: typed.types, sqlmode: sqlmode, } @@ -646,11 +646,11 @@ type typedIR interface { // UntypedExpr is a translated expression that cannot be compiled ahead of time because it // contains dynamic types. type UntypedExpr struct { + env *vtenv.Environment // ir is the translated IR for the expression ir IR // collation is the default collation for the translated expression - collation collations.ID - collationEnv *collations.Environment + collation collations.ID // needTypes are the IR nodes in ir that could not be typed ahead of time: these must // necessarily be either Column or BindVariable nodes, as all other nodes can always // be statically typed. The dynamicTypeOffset field on each node is the offset of @@ -700,7 +700,7 @@ func (u *UntypedExpr) Compile(env *ExpressionEnv) (*CompiledExpr, error) { if err != nil { return nil, err } - return typed.compile(u.ir, u.collation, u.collationEnv, env.sqlmode) + return typed.compile(u.env, u.ir, u.collation, env.sqlmode) } func (u *UntypedExpr) typeof(env *ExpressionEnv) (ctype, error) { diff --git a/go/vt/vtgate/evalengine/translate_convert.go b/go/vt/vtgate/evalengine/translate_convert.go index 133315e69af..353d4d70c86 100644 --- a/go/vt/vtgate/evalengine/translate_convert.go +++ b/go/vt/vtgate/evalengine/translate_convert.go @@ -32,7 +32,7 @@ func (ast *astCompiler) binaryCollationForCollation(collation collations.ID) col if binary == nil { return collations.Unknown } - return ast.cfg.CollationEnv.BinaryCollationForCharset(binary.Charset().Name()) + return ast.cfg.Environment.CollationEnv().BinaryCollationForCharset(binary.Charset().Name()) } func (ast *astCompiler) translateConvertCharset(charset string, binary bool) (collations.ID, error) { @@ -47,7 +47,7 @@ func (ast *astCompiler) translateConvertCharset(charset string, binary bool) (co return collation, nil } charset = strings.ToLower(charset) - collationID := ast.cfg.CollationEnv.DefaultCollationForCharset(charset) + collationID := ast.cfg.Environment.CollationEnv().DefaultCollationForCharset(charset) if collationID == collations.Unknown { return collations.Unknown, vterrors.Errorf(vtrpcpb.Code_INVALID_ARGUMENT, "Unknown character set: '%s'", charset) } @@ -66,7 +66,7 @@ func (ast *astCompiler) translateConvertExpr(expr sqlparser.Expr, convertType *s err error ) - convert.CollationEnv = ast.cfg.CollationEnv + convert.CollationEnv = ast.cfg.Environment.CollationEnv() convert.Inner, err = ast.translateExpr(expr) if err != nil { return nil, err @@ -124,7 +124,7 @@ func (ast *astCompiler) translateConvertUsingExpr(expr *sqlparser.ConvertUsingEx err error ) - using.CollationEnv = ast.cfg.CollationEnv + using.CollationEnv = ast.cfg.Environment.CollationEnv() using.Inner, err = ast.translateExpr(expr.Expr) if err != nil { return nil, err diff --git a/go/vt/vtgate/evalengine/translate_test.go b/go/vt/vtgate/evalengine/translate_test.go index a3f95fb860e..3702230e22e 100644 --- a/go/vt/vtgate/evalengine/translate_test.go +++ b/go/vt/vtgate/evalengine/translate_test.go @@ -23,9 +23,9 @@ import ( "time" "vitess.io/vitess/go/mysql/collations" - "vitess.io/vitess/go/mysql/config" "vitess.io/vitess/go/sqltypes" "vitess.io/vitess/go/vt/sqlparser" + "vitess.io/vitess/go/vt/vtenv" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" @@ -114,9 +114,10 @@ func TestTranslateSimplification(t *testing.T) { {"json->>\"$.c\"", ok("JSON_UNQUOTE(JSON_EXTRACT(`json`, '$.c'))"), ok("JSON_UNQUOTE(JSON_EXTRACT(`json`, '$.c'))")}, } + venv := vtenv.NewTestEnv() for _, tc := range testCases { t.Run(tc.expression, func(t *testing.T) { - stmt, err := sqlparser.NewTestParser().Parse("select " + tc.expression) + stmt, err := venv.Parser().Parse("select " + tc.expression) if err != nil { t.Fatal(err) } @@ -127,9 +128,8 @@ func TestTranslateSimplification(t *testing.T) { cfg := &Config{ ResolveColumn: fields.Column, - Collation: collations.MySQL8().DefaultConnectionCharset(), - CollationEnv: collations.MySQL8(), - MySQLVersion: config.DefaultMySQLVersion, + Collation: venv.CollationEnv().DefaultConnectionCharset(), + Environment: venv, NoConstantFolding: true, NoCompilation: true, } @@ -299,6 +299,7 @@ func TestEvaluate(t *testing.T) { expected: False, }} + venv := vtenv.NewTestEnv() for _, test := range tests { t.Run(test.expression, func(t *testing.T) { // Given @@ -306,9 +307,8 @@ func TestEvaluate(t *testing.T) { require.NoError(t, err) astExpr := stmt.(*sqlparser.Select).SelectExprs[0].(*sqlparser.AliasedExpr).Expr sqltypesExpr, err := Translate(astExpr, &Config{ - Collation: collations.MySQL8().DefaultConnectionCharset(), - CollationEnv: collations.MySQL8(), - MySQLVersion: config.DefaultMySQLVersion, + Collation: venv.CollationEnv().DefaultConnectionCharset(), + Environment: venv, }) require.Nil(t, err) require.NotNil(t, sqltypesExpr) @@ -319,7 +319,7 @@ func TestEvaluate(t *testing.T) { "uint32_bind_variable": sqltypes.Uint32BindVariable(21), "uint64_bind_variable": sqltypes.Uint64BindVariable(22), "float_bind_variable": sqltypes.Float64BindVariable(2.2), - }, NewEmptyVCursor(collations.MySQL8(), time.Local, config.DefaultMySQLVersion)) + }, NewEmptyVCursor(venv, time.Local)) // When r, err := env.Evaluate(sqltypesExpr) @@ -348,23 +348,22 @@ func TestEvaluateTuple(t *testing.T) { expected: []sqltypes.Value{sqltypes.NewInt64(1), sqltypes.NewVarChar("2"), sqltypes.NewDecimal("4.0")}, }} + venv := vtenv.NewTestEnv() for _, test := range tests { t.Run(test.expression, func(t *testing.T) { // Given stmt, err := sqlparser.NewTestParser().Parse("select " + test.expression) require.NoError(t, err) astExpr := stmt.(*sqlparser.Select).SelectExprs[0].(*sqlparser.AliasedExpr).Expr - collationEnv := collations.MySQL8() sqltypesExpr, err := Translate(astExpr, &Config{ - Collation: collationEnv.DefaultConnectionCharset(), - CollationEnv: collationEnv, - MySQLVersion: config.DefaultMySQLVersion, + Collation: venv.CollationEnv().DefaultConnectionCharset(), + Environment: venv, }) require.Nil(t, err) require.NotNil(t, sqltypesExpr) // When - r, err := EmptyExpressionEnv(collationEnv, config.DefaultMySQLVersion).Evaluate(sqltypesExpr) + r, err := EmptyExpressionEnv(venv).Evaluate(sqltypesExpr) // Then require.NoError(t, err) @@ -390,6 +389,7 @@ func TestTranslationFailures(t *testing.T) { }, } + venv := vtenv.NewTestEnv() for _, testcase := range testcases { t.Run(testcase.expression, func(t *testing.T) { // Given @@ -397,9 +397,8 @@ func TestTranslationFailures(t *testing.T) { require.NoError(t, err) astExpr := stmt.(*sqlparser.Select).SelectExprs[0].(*sqlparser.AliasedExpr).Expr _, err = Translate(astExpr, &Config{ - Collation: collations.MySQL8().DefaultConnectionCharset(), - CollationEnv: collations.MySQL8(), - MySQLVersion: config.DefaultMySQLVersion, + Collation: venv.CollationEnv().DefaultConnectionCharset(), + Environment: venv, }) require.EqualError(t, err, testcase.expectedErr) }) @@ -427,6 +426,7 @@ func TestCardinalityWithBindVariables(t *testing.T) { {expr: `1 IN ::bar`}, } + venv := vtenv.NewTestEnv() for _, testcase := range testcases { t.Run(testcase.expr, func(t *testing.T) { err := func() error { @@ -437,9 +437,8 @@ func TestCardinalityWithBindVariables(t *testing.T) { astExpr := stmt.(*sqlparser.Select).SelectExprs[0].(*sqlparser.AliasedExpr).Expr _, err = Translate(astExpr, &Config{ - Collation: collations.MySQL8().DefaultConnectionCharset(), - CollationEnv: collations.MySQL8(), - MySQLVersion: config.DefaultMySQLVersion, + Collation: venv.CollationEnv().DefaultConnectionCharset(), + Environment: venv, NoCompilation: true, }) return err diff --git a/go/vt/vtgate/executor.go b/go/vt/vtgate/executor.go index 6816794618b..c395fedacfe 100644 --- a/go/vt/vtgate/executor.go +++ b/go/vt/vtgate/executor.go @@ -32,6 +32,7 @@ import ( "vitess.io/vitess/go/cache/theine" "vitess.io/vitess/go/streamlog" + "vitess.io/vitess/go/vt/vtenv" "vitess.io/vitess/go/vt/vthash" "vitess.io/vitess/go/acl" @@ -93,6 +94,7 @@ func init() { // Executor is the engine that executes queries by utilizing // the abilities of the underlying vttablets. type Executor struct { + env *vtenv.Environment serv srvtopo.Server cell string resolver *Resolver @@ -122,10 +124,6 @@ type Executor struct { warmingReadsPercent int warmingReadsChannel chan bool - - collEnv *collations.Environment - parser *sqlparser.Parser - mysqlVersion string } var executorOnce sync.Once @@ -146,6 +144,7 @@ func DefaultPlanCache() *PlanCache { // NewExecutor creates a new Executor. func NewExecutor( ctx context.Context, + env *vtenv.Environment, serv srvtopo.Server, cell string, resolver *Resolver, @@ -156,11 +155,9 @@ func NewExecutor( noScatter bool, pv plancontext.PlannerVersion, warmingReadsPercent int, - collationEnv *collations.Environment, - parser *sqlparser.Parser, - mysqlVersion string, ) *Executor { e := &Executor{ + env: env, serv: serv, cell: cell, resolver: resolver, @@ -175,9 +172,6 @@ func NewExecutor( plans: plans, warmingReadsPercent: warmingReadsPercent, warmingReadsChannel: make(chan bool, warmingReadsConcurrency), - collEnv: collationEnv, - parser: parser, - mysqlVersion: mysqlVersion, } vschemaacl.Init() @@ -187,7 +181,7 @@ func NewExecutor( serv: serv, cell: cell, schema: e.schemaTracker, - parser: parser, + parser: env.Parser(), } serv.WatchSrvVSchema(ctx, cell, e.vm.VSchemaUpdate) @@ -234,7 +228,7 @@ func (e *Executor) Execute(ctx context.Context, mysqlCtx vtgateservice.MySQLConn } if result != nil && len(result.Rows) > warnMemoryRows { warnings.Add("ResultsExceeded", 1) - piiSafeSQL, err := e.parser.RedactSQLQuery(sql) + piiSafeSQL, err := e.env.Parser().RedactSQLQuery(sql) if err != nil { piiSafeSQL = logStats.StmtType } @@ -368,7 +362,7 @@ func (e *Executor) StreamExecute( saveSessionStats(safeSession, srr.stmtType, srr.rowsAffected, srr.insertID, srr.rowsReturned, err) if srr.rowsReturned > warnMemoryRows { warnings.Add("ResultsExceeded", 1) - piiSafeSQL, err := e.parser.RedactSQLQuery(sql) + piiSafeSQL, err := e.env.Parser().RedactSQLQuery(sql) if err != nil { piiSafeSQL = logStats.StmtType } @@ -510,16 +504,15 @@ func (e *Executor) addNeededBindVars(vcursor *vcursorImpl, bindVarNeeds *sqlpars bindVars[key] = sqltypes.StringBindVariable(mysqlSocketPath()) default: if value, hasSysVar := session.SystemVariables[sysVar]; hasSysVar { - expr, err := e.parser.ParseExpr(value) + expr, err := e.env.Parser().ParseExpr(value) if err != nil { return err } evalExpr, err := evalengine.Translate(expr, &evalengine.Config{ - Collation: vcursor.collation, - CollationEnv: e.collEnv, - MySQLVersion: e.mysqlVersion, - SQLMode: evalengine.ParseSQLMode(vcursor.SQLMode()), + Collation: vcursor.collation, + Environment: e.env, + SQLMode: evalengine.ParseSQLMode(vcursor.SQLMode()), }) if err != nil { return err @@ -1353,7 +1346,7 @@ func (e *Executor) handlePrepare(ctx context.Context, safeSession *SafeSession, query, comments := sqlparser.SplitMarginComments(sql) vcursor, _ := newVCursorImpl(safeSession, comments, e, logStats, e.vm, e.VSchema(), e.resolver.resolver, e.serv, e.warnShardedOnly, e.pv) - stmt, reservedVars, err := parseAndValidateQuery(query, e.parser) + stmt, reservedVars, err := parseAndValidateQuery(query, e.env.Parser()) if err != nil { return nil, err } @@ -1524,7 +1517,7 @@ func (e *Executor) ReleaseLock(ctx context.Context, session *SafeSession) error // planPrepareStmt implements the IExecutor interface func (e *Executor) planPrepareStmt(ctx context.Context, vcursor *vcursorImpl, query string) (*engine.Plan, sqlparser.Statement, error) { - stmt, reservedVars, err := parseAndValidateQuery(query, e.parser) + stmt, reservedVars, err := parseAndValidateQuery(query, e.env.Parser()) if err != nil { return nil, nil, err } @@ -1558,14 +1551,6 @@ func (e *Executor) Close() { e.plans.Close() } -func (e *Executor) collationEnv() *collations.Environment { - return e.collEnv -} - -func (e *Executor) sqlparser() *sqlparser.Parser { - return e.parser -} - -func (e *Executor) mysqlServerVersion() string { - return e.mysqlVersion +func (e *Executor) environment() *vtenv.Environment { + return e.env } diff --git a/go/vt/vtgate/executor_framework_test.go b/go/vt/vtgate/executor_framework_test.go index 59e360e871e..332139c4a78 100644 --- a/go/vt/vtgate/executor_framework_test.go +++ b/go/vt/vtgate/executor_framework_test.go @@ -30,8 +30,6 @@ import ( "vitess.io/vitess/go/cache/theine" "vitess.io/vitess/go/constants/sidecar" - "vitess.io/vitess/go/mysql/collations" - "vitess.io/vitess/go/mysql/config" "vitess.io/vitess/go/sqltypes" "vitess.io/vitess/go/streamlog" "vitess.io/vitess/go/test/utils" @@ -42,8 +40,8 @@ import ( topodatapb "vitess.io/vitess/go/vt/proto/topodata" vtgatepb "vitess.io/vitess/go/vt/proto/vtgate" "vitess.io/vitess/go/vt/sidecardb" - "vitess.io/vitess/go/vt/sqlparser" "vitess.io/vitess/go/vt/srvtopo" + "vitess.io/vitess/go/vt/vtenv" "vitess.io/vitess/go/vt/vtgate/engine" "vitess.io/vitess/go/vt/vtgate/logstats" "vitess.io/vitess/go/vt/vtgate/vindexes" @@ -183,7 +181,7 @@ func createExecutorEnvCallback(t testing.TB, eachShard func(shard, ks string, ta // one-off queries from thrashing the cache. Disable the doorkeeper in the tests to prevent flakiness. plans := theine.NewStore[PlanCacheKey, *engine.Plan](queryPlanCacheMemory, false) - executor = NewExecutor(ctx, serv, cell, resolver, false, false, testBufferSize, plans, nil, false, querypb.ExecuteOptions_Gen4, 0, collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion) + executor = NewExecutor(ctx, vtenv.NewTestEnv(), serv, cell, resolver, false, false, testBufferSize, plans, nil, false, querypb.ExecuteOptions_Gen4, 0) executor.SetQueryLogger(queryLogger) key.AnyShardPicker = DestinationAnyShardPickerFirstShard{} @@ -230,10 +228,9 @@ func createCustomExecutor(t testing.TB, vschema string, mysqlVersion string) (ex queryLogger := streamlog.New[*logstats.LogStats]("VTGate", queryLogBufferSize) plans := DefaultPlanCache() - parser, err := sqlparser.New(sqlparser.Options{MySQLServerVersion: mysqlVersion}) + env, err := vtenv.New(vtenv.Options{MySQLServerVersion: mysqlVersion}) require.NoError(t, err) - collationEnv := collations.NewEnvironment(mysqlVersion) - executor = NewExecutor(ctx, serv, cell, resolver, false, false, testBufferSize, plans, nil, false, querypb.ExecuteOptions_Gen4, 0, collationEnv, parser, mysqlVersion) + executor = NewExecutor(ctx, env, serv, cell, resolver, false, false, testBufferSize, plans, nil, false, querypb.ExecuteOptions_Gen4, 0) executor.SetQueryLogger(queryLogger) t.Cleanup(func() { @@ -270,7 +267,7 @@ func createCustomExecutorSetValues(t testing.TB, vschema string, values []*sqlty sbclookup = hc.AddTestTablet(cell, "0", 1, KsTestUnsharded, "0", topodatapb.TabletType_PRIMARY, true, 1, nil) queryLogger := streamlog.New[*logstats.LogStats]("VTGate", queryLogBufferSize) plans := DefaultPlanCache() - executor = NewExecutor(ctx, serv, cell, resolver, false, false, testBufferSize, plans, nil, false, querypb.ExecuteOptions_Gen4, 0, collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion) + executor = NewExecutor(ctx, vtenv.NewTestEnv(), serv, cell, resolver, false, false, testBufferSize, plans, nil, false, querypb.ExecuteOptions_Gen4, 0) executor.SetQueryLogger(queryLogger) t.Cleanup(func() { @@ -295,7 +292,7 @@ func createExecutorEnvWithPrimaryReplicaConn(t testing.TB, ctx context.Context, replica = hc.AddTestTablet(cell, "0-replica", 1, KsTestUnsharded, "0", topodatapb.TabletType_REPLICA, true, 1, nil) queryLogger := streamlog.New[*logstats.LogStats]("VTGate", queryLogBufferSize) - executor = NewExecutor(ctx, serv, cell, resolver, false, false, testBufferSize, DefaultPlanCache(), nil, false, querypb.ExecuteOptions_Gen4, warmingReadsPercent, collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion) + executor = NewExecutor(ctx, vtenv.NewTestEnv(), serv, cell, resolver, false, false, testBufferSize, DefaultPlanCache(), nil, false, querypb.ExecuteOptions_Gen4, warmingReadsPercent) executor.SetQueryLogger(queryLogger) t.Cleanup(func() { diff --git a/go/vt/vtgate/executor_select_test.go b/go/vt/vtgate/executor_select_test.go index a6c1b47819f..dd214f9dfae 100644 --- a/go/vt/vtgate/executor_select_test.go +++ b/go/vt/vtgate/executor_select_test.go @@ -28,10 +28,9 @@ import ( _flag "vitess.io/vitess/go/internal/flag" "vitess.io/vitess/go/mysql/collations" - "vitess.io/vitess/go/mysql/config" "vitess.io/vitess/go/streamlog" - "vitess.io/vitess/go/vt/sqlparser" "vitess.io/vitess/go/vt/topo/topoproto" + "vitess.io/vitess/go/vt/vtenv" "vitess.io/vitess/go/vt/vtgate/logstats" "github.com/google/go-cmp/cmp" @@ -1625,7 +1624,7 @@ func TestSelectListArg(t *testing.T) { func createExecutor(ctx context.Context, serv *sandboxTopo, cell string, resolver *Resolver) *Executor { queryLogger := streamlog.New[*logstats.LogStats]("VTGate", queryLogBufferSize) plans := DefaultPlanCache() - ex := NewExecutor(ctx, serv, cell, resolver, false, false, testBufferSize, plans, nil, false, querypb.ExecuteOptions_Gen4, 0, collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion) + ex := NewExecutor(ctx, vtenv.NewTestEnv(), serv, cell, resolver, false, false, testBufferSize, plans, nil, false, querypb.ExecuteOptions_Gen4, 0) ex.SetQueryLogger(queryLogger) return ex } @@ -3251,7 +3250,7 @@ func TestStreamOrderByLimitWithMultipleResults(t *testing.T) { } queryLogger := streamlog.New[*logstats.LogStats]("VTGate", queryLogBufferSize) plans := DefaultPlanCache() - executor := NewExecutor(ctx, serv, cell, resolver, true, false, testBufferSize, plans, nil, false, querypb.ExecuteOptions_Gen4, 0, collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion) + executor := NewExecutor(ctx, vtenv.NewTestEnv(), serv, cell, resolver, true, false, testBufferSize, plans, nil, false, querypb.ExecuteOptions_Gen4, 0) executor.SetQueryLogger(queryLogger) defer executor.Close() // some sleep for all goroutines to start diff --git a/go/vt/vtgate/executor_stream_test.go b/go/vt/vtgate/executor_stream_test.go index e89bb22dfaf..b8cfeaf3cd5 100644 --- a/go/vt/vtgate/executor_stream_test.go +++ b/go/vt/vtgate/executor_stream_test.go @@ -23,8 +23,6 @@ import ( "github.com/stretchr/testify/require" - "vitess.io/vitess/go/mysql/collations" - "vitess.io/vitess/go/mysql/config" "vitess.io/vitess/go/sqltypes" "vitess.io/vitess/go/streamlog" "vitess.io/vitess/go/test/utils" @@ -32,7 +30,7 @@ import ( querypb "vitess.io/vitess/go/vt/proto/query" topodatapb "vitess.io/vitess/go/vt/proto/topodata" vtgatepb "vitess.io/vitess/go/vt/proto/vtgate" - "vitess.io/vitess/go/vt/sqlparser" + "vitess.io/vitess/go/vt/vtenv" "vitess.io/vitess/go/vt/vtgate/logstats" _ "vitess.io/vitess/go/vt/vtgate/vindexes" "vitess.io/vitess/go/vt/vttablet/sandboxconn" @@ -69,7 +67,7 @@ func TestStreamSQLSharded(t *testing.T) { queryLogger := streamlog.New[*logstats.LogStats]("VTGate", queryLogBufferSize) plans := DefaultPlanCache() - executor := NewExecutor(ctx, serv, cell, resolver, false, false, testBufferSize, plans, nil, false, querypb.ExecuteOptions_Gen4, 0, collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion) + executor := NewExecutor(ctx, vtenv.NewTestEnv(), serv, cell, resolver, false, false, testBufferSize, plans, nil, false, querypb.ExecuteOptions_Gen4, 0) executor.SetQueryLogger(queryLogger) defer executor.Close() diff --git a/go/vt/vtgate/plan_execute.go b/go/vt/vtgate/plan_execute.go index 657a1792c26..4e2c3bfea4c 100644 --- a/go/vt/vtgate/plan_execute.go +++ b/go/vt/vtgate/plan_execute.go @@ -80,7 +80,7 @@ func (e *Executor) newExecute( query, comments := sqlparser.SplitMarginComments(sql) // 2: Parse and Validate query - stmt, reservedVars, err := parseAndValidateQuery(query, e.parser) + stmt, reservedVars, err := parseAndValidateQuery(query, e.env.Parser()) if err != nil { return err } diff --git a/go/vt/vtgate/planbuilder/builder.go b/go/vt/vtgate/planbuilder/builder.go index 1a982a78489..edb0db85c5e 100644 --- a/go/vt/vtgate/planbuilder/builder.go +++ b/go/vt/vtgate/planbuilder/builder.go @@ -70,7 +70,7 @@ func singleTable(ks, tbl string) string { // TestBuilder builds a plan for a query based on the specified vschema. // This method is only used from tests func TestBuilder(query string, vschema plancontext.VSchema, keyspace string) (*engine.Plan, error) { - stmt, reserved, err := vschema.SQLParser().Parse2(query) + stmt, reserved, err := vschema.Environment().Parser().Parse2(query) if err != nil { return nil, err } diff --git a/go/vt/vtgate/planbuilder/collations_test.go b/go/vt/vtgate/planbuilder/collations_test.go index 7325d189e43..8e90bdf5fcc 100644 --- a/go/vt/vtgate/planbuilder/collations_test.go +++ b/go/vt/vtgate/planbuilder/collations_test.go @@ -21,6 +21,7 @@ import ( "testing" "vitess.io/vitess/go/test/vschemawrapper" + "vitess.io/vitess/go/vt/vtenv" "github.com/stretchr/testify/require" @@ -45,6 +46,7 @@ func (tc *collationTestCase) run(t *testing.T) { V: loadSchema(t, "vschemas/schema.json", false), SysVarEnabled: true, Version: Gen4, + Env: vtenv.NewTestEnv(), } tc.addCollationsToSchema(vschemaWrapper) diff --git a/go/vt/vtgate/planbuilder/expression_converter.go b/go/vt/vtgate/planbuilder/expression_converter.go index c8b731b4af3..961c4f1fb4b 100644 --- a/go/vt/vtgate/planbuilder/expression_converter.go +++ b/go/vt/vtgate/planbuilder/expression_converter.go @@ -22,6 +22,7 @@ import ( "vitess.io/vitess/go/mysql/collations" "vitess.io/vitess/go/vt/sqlparser" + "vitess.io/vitess/go/vt/vtenv" "vitess.io/vitess/go/vt/vtgate/engine" "vitess.io/vitess/go/vt/vtgate/evalengine" "vitess.io/vitess/go/vt/vtgate/planbuilder/plancontext" @@ -29,9 +30,8 @@ import ( type expressionConverter struct { tabletExpressions []sqlparser.Expr - collationEnv *collations.Environment + env *vtenv.Environment collation collations.ID - mysqlVersion string } func booleanValues(astExpr sqlparser.Expr) evalengine.Expr { @@ -84,9 +84,8 @@ func (ec *expressionConverter) convert(astExpr sqlparser.Expr, boolean, identifi } } evalExpr, err := evalengine.Translate(astExpr, &evalengine.Config{ - Collation: ec.collation, - CollationEnv: ec.collationEnv, - MySQLVersion: ec.mysqlVersion, + Collation: ec.collation, + Environment: ec.env, }) if err != nil { if !strings.Contains(err.Error(), evalengine.ErrTranslateExprNotSupported) { diff --git a/go/vt/vtgate/planbuilder/expression_converter_test.go b/go/vt/vtgate/planbuilder/expression_converter_test.go index f7430bc0092..c92cc184262 100644 --- a/go/vt/vtgate/planbuilder/expression_converter_test.go +++ b/go/vt/vtgate/planbuilder/expression_converter_test.go @@ -21,9 +21,8 @@ import ( "github.com/stretchr/testify/require" - "vitess.io/vitess/go/mysql/collations" - "vitess.io/vitess/go/mysql/config" "vitess.io/vitess/go/vt/sqlparser" + "vitess.io/vitess/go/vt/vtenv" "vitess.io/vitess/go/vt/vtgate/evalengine" ) @@ -45,6 +44,7 @@ func TestConversion(t *testing.T) { expressionsOut: e(evalengine.NewColumn(0, evalengine.Type{}, nil)), }} + venv := vtenv.NewTestEnv() for _, tc := range queries { t.Run(tc.expressionsIn, func(t *testing.T) { statement, err := sqlparser.NewTestParser().Parse("select " + tc.expressionsIn) @@ -52,9 +52,8 @@ func TestConversion(t *testing.T) { slct := statement.(*sqlparser.Select) exprs := extract(slct.SelectExprs) ec := &expressionConverter{ - collationEnv: collations.MySQL8(), - collation: collations.MySQL8().DefaultConnectionCharset(), - mysqlVersion: config.DefaultMySQLVersion, + env: venv, + collation: venv.CollationEnv().DefaultConnectionCharset(), } var result []evalengine.Expr for _, expr := range exprs { diff --git a/go/vt/vtgate/planbuilder/operator_transformers.go b/go/vt/vtgate/planbuilder/operator_transformers.go index 4c67c8c5fb2..8cfab78bcfa 100644 --- a/go/vt/vtgate/planbuilder/operator_transformers.go +++ b/go/vt/vtgate/planbuilder/operator_transformers.go @@ -26,6 +26,7 @@ import ( "vitess.io/vitess/go/slice" "vitess.io/vitess/go/sqltypes" "vitess.io/vitess/go/vt/sqlparser" + "vitess.io/vitess/go/vt/vtenv" "vitess.io/vitess/go/vt/vterrors" "vitess.io/vitess/go/vt/vtgate/engine" "vitess.io/vitess/go/vt/vtgate/engine/opcode" @@ -273,14 +274,14 @@ func transformAggregator(ctx *plancontext.PlanningContext, op *operators.Aggrega oa := &orderedAggregate{ resultsBuilder: newResultsBuilder(plan, nil), - collationEnv: ctx.VSchema.CollationEnv(), + collationEnv: ctx.VSchema.Environment().CollationEnv(), } for _, aggr := range op.Aggregations { if aggr.OpCode == opcode.AggregateUnassigned { return nil, vterrors.VT12001(fmt.Sprintf("in scatter query: aggregation function '%s'", sqlparser.String(aggr.Original))) } - aggrParam := engine.NewAggregateParam(aggr.OpCode, aggr.ColOffset, aggr.Alias, ctx.VSchema.CollationEnv()) + aggrParam := engine.NewAggregateParam(aggr.OpCode, aggr.ColOffset, aggr.Alias, ctx.VSchema.Environment().CollationEnv()) aggrParam.Expr = aggr.Func aggrParam.Original = aggr.Original aggrParam.OrigOpcode = aggr.OriginalOpCode @@ -295,7 +296,7 @@ func transformAggregator(ctx *plancontext.PlanningContext, op *operators.Aggrega WeightStringCol: groupBy.WSOffset, Expr: groupBy.Inner, Type: typ, - CollationEnv: ctx.VSchema.CollationEnv(), + CollationEnv: ctx.VSchema.Environment().CollationEnv(), }) } @@ -336,7 +337,7 @@ func createMemorySort(ctx *plancontext.PlanningContext, src logicalPlan, orderin WeightStringCol: ordering.WOffset[idx], Desc: order.Inner.Direction == sqlparser.DescOrder, Type: typ, - CollationEnv: ctx.VSchema.CollationEnv(), + CollationEnv: ctx.VSchema.Environment().CollationEnv(), }) } @@ -564,7 +565,7 @@ func buildRouteLogicalPlan(ctx *plancontext.PlanningContext, op *operators.Route WeightStringCol: order.WOffset, Desc: order.Direction == sqlparser.DescOrder, Type: typ, - CollationEnv: ctx.VSchema.CollationEnv(), + CollationEnv: ctx.VSchema.Environment().CollationEnv(), }) } if err != nil { @@ -828,15 +829,14 @@ func transformLimit(ctx *plancontext.PlanningContext, op *operators.Limit) (logi return nil, err } - return createLimit(plan, op.AST, ctx.VSchema.CollationEnv(), ctx.VSchema.MySQLVersion()) + return createLimit(plan, op.AST, ctx.VSchema.Environment(), ctx.VSchema.ConnCollation()) } -func createLimit(input logicalPlan, limit *sqlparser.Limit, collationEnv *collations.Environment, mysqlVersion string) (logicalPlan, error) { +func createLimit(input logicalPlan, limit *sqlparser.Limit, env *vtenv.Environment, coll collations.ID) (logicalPlan, error) { plan := newLimit(input) cfg := &evalengine.Config{ - Collation: collationEnv.DefaultConnectionCharset(), - CollationEnv: collationEnv, - MySQLVersion: mysqlVersion, + Collation: coll, + Environment: env, } pv, err := evalengine.Translate(limit.Rowcount, cfg) if err != nil { @@ -890,7 +890,7 @@ func transformHashJoin(ctx *plancontext.PlanningContext, op *operators.HashJoin) fmt.Sprintf("missing type information for [%s]", strings.Join(missingTypes, ", "))) } - comparisonType, err := evalengine.CoerceTypes(ltyp, rtyp, ctx.VSchema.CollationEnv()) + comparisonType, err := evalengine.CoerceTypes(ltyp, rtyp, ctx.VSchema.Environment().CollationEnv()) if err != nil { return nil, err } @@ -906,7 +906,7 @@ func transformHashJoin(ctx *plancontext.PlanningContext, op *operators.HashJoin) ASTPred: op.JoinPredicate(), Collation: comparisonType.Collation(), ComparisonType: comparisonType.Type(), - CollationEnv: ctx.VSchema.CollationEnv(), + CollationEnv: ctx.VSchema.Environment().CollationEnv(), }, }, nil } diff --git a/go/vt/vtgate/planbuilder/operators/distinct.go b/go/vt/vtgate/planbuilder/operators/distinct.go index a7fa63a0c92..eeddd928f66 100644 --- a/go/vt/vtgate/planbuilder/operators/distinct.go +++ b/go/vt/vtgate/planbuilder/operators/distinct.go @@ -61,7 +61,7 @@ func (d *Distinct) planOffsets(ctx *plancontext.PlanningContext) Operator { Col: idx, WsCol: wsCol, Type: typ, - CollationEnv: ctx.VSchema.CollationEnv(), + CollationEnv: ctx.VSchema.Environment().CollationEnv(), }) } return nil diff --git a/go/vt/vtgate/planbuilder/operators/dml_planning.go b/go/vt/vtgate/planbuilder/operators/dml_planning.go index 4354ded15b7..530f18391e2 100644 --- a/go/vt/vtgate/planbuilder/operators/dml_planning.go +++ b/go/vt/vtgate/planbuilder/operators/dml_planning.go @@ -80,10 +80,9 @@ func buildChangedVindexesValues( } found = true pv, err := evalengine.Translate(assignment.Expr.EvalExpr, &evalengine.Config{ - ResolveType: ctx.SemTable.TypeForExpr, - Collation: ctx.SemTable.Collation, - CollationEnv: ctx.VSchema.CollationEnv(), - MySQLVersion: ctx.VSchema.MySQLVersion(), + ResolveType: ctx.SemTable.TypeForExpr, + Collation: ctx.SemTable.Collation, + Environment: ctx.VSchema.Environment(), }) if err != nil { panic(invalidUpdateExpr(assignment.Name.Name.String(), assignment.Expr.EvalExpr)) diff --git a/go/vt/vtgate/planbuilder/operators/filter.go b/go/vt/vtgate/planbuilder/operators/filter.go index d438f9a5095..17e5ac64b71 100644 --- a/go/vt/vtgate/planbuilder/operators/filter.go +++ b/go/vt/vtgate/planbuilder/operators/filter.go @@ -119,10 +119,9 @@ func (f *Filter) Compact(*plancontext.PlanningContext) (Operator, *ApplyResult) func (f *Filter) planOffsets(ctx *plancontext.PlanningContext) Operator { cfg := &evalengine.Config{ - ResolveType: ctx.SemTable.TypeForExpr, - Collation: ctx.SemTable.Collation, - CollationEnv: ctx.VSchema.CollationEnv(), - MySQLVersion: ctx.VSchema.MySQLVersion(), + ResolveType: ctx.SemTable.TypeForExpr, + Collation: ctx.SemTable.Collation, + Environment: ctx.VSchema.Environment(), } predicate := sqlparser.AndExpressions(f.Predicates...) diff --git a/go/vt/vtgate/planbuilder/operators/hash_join.go b/go/vt/vtgate/planbuilder/operators/hash_join.go index 8e4046a1ee5..e5a87cf4ef8 100644 --- a/go/vt/vtgate/planbuilder/operators/hash_join.go +++ b/go/vt/vtgate/planbuilder/operators/hash_join.go @@ -332,10 +332,9 @@ func (hj *HashJoin) addColumn(ctx *plancontext.PlanningContext, in sqlparser.Exp rewrittenExpr := sqlparser.CopyOnRewrite(in, pre, r.post, ctx.SemTable.CopySemanticInfo).(sqlparser.Expr) cfg := &evalengine.Config{ - ResolveType: ctx.SemTable.TypeForExpr, - Collation: ctx.SemTable.Collation, - CollationEnv: ctx.VSchema.CollationEnv(), - MySQLVersion: ctx.VSchema.MySQLVersion(), + ResolveType: ctx.SemTable.TypeForExpr, + Collation: ctx.SemTable.Collation, + Environment: ctx.VSchema.Environment(), } eexpr, err := evalengine.Translate(rewrittenExpr, cfg) if err != nil { @@ -435,6 +434,7 @@ func (hj *HashJoin) addSingleSidedColumn( cfg := &evalengine.Config{ ResolveType: ctx.SemTable.TypeForExpr, Collation: ctx.SemTable.Collation, + Environment: ctx.VSchema.Environment(), } eexpr, err := evalengine.Translate(rewrittenExpr, cfg) if err != nil { diff --git a/go/vt/vtgate/planbuilder/operators/hash_join_test.go b/go/vt/vtgate/planbuilder/operators/hash_join_test.go index 038c2d65da0..2bf1d08d2b6 100644 --- a/go/vt/vtgate/planbuilder/operators/hash_join_test.go +++ b/go/vt/vtgate/planbuilder/operators/hash_join_test.go @@ -24,6 +24,7 @@ import ( "vitess.io/vitess/go/test/vschemawrapper" "vitess.io/vitess/go/vt/sqlparser" + "vitess.io/vitess/go/vt/vtenv" "vitess.io/vitess/go/vt/vtgate/planbuilder/plancontext" "vitess.io/vitess/go/vt/vtgate/semantics" "vitess.io/vitess/go/vt/vtgate/vindexes" @@ -66,6 +67,7 @@ func TestOffsetPlanning(t *testing.T) { VSchema: &vschemawrapper.VSchemaWrapper{ V: &vindexes.VSchema{}, SysVarEnabled: true, + Env: vtenv.NewTestEnv(), }, } lid := semantics.SingleTableSet(0) diff --git a/go/vt/vtgate/planbuilder/operators/info_schema_planning.go b/go/vt/vtgate/planbuilder/operators/info_schema_planning.go index f31dee1e8ca..f8dc9b9d281 100644 --- a/go/vt/vtgate/planbuilder/operators/info_schema_planning.go +++ b/go/vt/vtgate/planbuilder/operators/info_schema_planning.go @@ -46,8 +46,7 @@ func (isr *InfoSchemaRouting) UpdateRoutingParams(ctx *plancontext.PlanningConte eexpr, err := evalengine.Translate(expr, &evalengine.Config{ Collation: collations.SystemCollation.Collation, ResolveColumn: NotImplementedSchemaInfoResolver, - CollationEnv: ctx.VSchema.CollationEnv(), - MySQLVersion: ctx.VSchema.MySQLVersion(), + Environment: ctx.VSchema.Environment(), }) if err != nil { panic(err) @@ -60,8 +59,7 @@ func (isr *InfoSchemaRouting) UpdateRoutingParams(ctx *plancontext.PlanningConte eexpr, err := evalengine.Translate(expr, &evalengine.Config{ Collation: collations.SystemCollation.Collation, ResolveColumn: NotImplementedSchemaInfoResolver, - CollationEnv: ctx.VSchema.CollationEnv(), - MySQLVersion: ctx.VSchema.MySQLVersion(), + Environment: ctx.VSchema.Environment(), }) if err != nil { panic(err) @@ -124,7 +122,7 @@ func extractInfoSchemaRoutingPredicate(ctx *plancontext.PlanningContext, in sqlp return false, "", nil } - isSchemaName, col := isTableOrSchemaRoutable(cmp, ctx.VSchema.MySQLVersion()) + isSchemaName, col := isTableOrSchemaRoutable(cmp, ctx.VSchema.Environment().MySQLVersion()) rhs := cmp.Right if col == nil || !shouldRewrite(rhs) { return false, "", nil @@ -135,8 +133,7 @@ func extractInfoSchemaRoutingPredicate(ctx *plancontext.PlanningContext, in sqlp _, err := evalengine.Translate(rhs, &evalengine.Config{ Collation: collations.SystemCollation.Collation, ResolveColumn: NotImplementedSchemaInfoResolver, - CollationEnv: ctx.VSchema.CollationEnv(), - MySQLVersion: ctx.VSchema.MySQLVersion(), + Environment: ctx.VSchema.Environment(), }) if err != nil { // if we can't translate this to an evalengine expression, diff --git a/go/vt/vtgate/planbuilder/operators/insert.go b/go/vt/vtgate/planbuilder/operators/insert.go index c0167db0e8d..9d0048d9322 100644 --- a/go/vt/vtgate/planbuilder/operators/insert.go +++ b/go/vt/vtgate/planbuilder/operators/insert.go @@ -506,10 +506,9 @@ func insertRowsPlan(ctx *plancontext.PlanningContext, insOp *Insert, ins *sqlpar colNum, _ := findOrAddColumn(ins, col) for rowNum, row := range rows { innerpv, err := evalengine.Translate(row[colNum], &evalengine.Config{ - ResolveType: ctx.SemTable.TypeForExpr, - Collation: ctx.SemTable.Collation, - CollationEnv: ctx.VSchema.CollationEnv(), - MySQLVersion: ctx.VSchema.MySQLVersion(), + ResolveType: ctx.SemTable.TypeForExpr, + Collation: ctx.SemTable.Collation, + Environment: ctx.VSchema.Environment(), }) if err != nil { panic(err) @@ -638,10 +637,9 @@ func modifyForAutoinc(ctx *plancontext.PlanningContext, ins *sqlparser.Insert, v } var err error gen.Values, err = evalengine.Translate(autoIncValues, &evalengine.Config{ - ResolveType: ctx.SemTable.TypeForExpr, - Collation: ctx.SemTable.Collation, - CollationEnv: ctx.VSchema.CollationEnv(), - MySQLVersion: ctx.VSchema.MySQLVersion(), + ResolveType: ctx.SemTable.TypeForExpr, + Collation: ctx.SemTable.Collation, + Environment: ctx.VSchema.Environment(), }) if err != nil { panic(err) diff --git a/go/vt/vtgate/planbuilder/operators/projection.go b/go/vt/vtgate/planbuilder/operators/projection.go index 773819276ad..c06038f893c 100644 --- a/go/vt/vtgate/planbuilder/operators/projection.go +++ b/go/vt/vtgate/planbuilder/operators/projection.go @@ -554,10 +554,9 @@ func (p *Projection) planOffsets(ctx *plancontext.PlanningContext) Operator { // for everything else, we'll turn to the evalengine eexpr, err := evalengine.Translate(rewritten, &evalengine.Config{ - ResolveType: ctx.SemTable.TypeForExpr, - Collation: ctx.SemTable.Collation, - CollationEnv: ctx.VSchema.CollationEnv(), - MySQLVersion: ctx.VSchema.MySQLVersion(), + ResolveType: ctx.SemTable.TypeForExpr, + Collation: ctx.SemTable.Collation, + Environment: ctx.VSchema.Environment(), }) if err != nil { panic(err) diff --git a/go/vt/vtgate/planbuilder/operators/route.go b/go/vt/vtgate/planbuilder/operators/route.go index 16369e4d90d..5303cac401b 100644 --- a/go/vt/vtgate/planbuilder/operators/route.go +++ b/go/vt/vtgate/planbuilder/operators/route.go @@ -20,6 +20,7 @@ import ( "fmt" topodatapb "vitess.io/vitess/go/vt/proto/topodata" + "vitess.io/vitess/go/vt/vtenv" "vitess.io/vitess/go/mysql/collations" "vitess.io/vitess/go/slice" @@ -121,7 +122,7 @@ func UpdateRoutingLogic(ctx *plancontext.PlanningContext, expr sqlparser.Expr, r } nr := &NoneRouting{keyspace: ks} - if isConstantFalse(expr, ctx.VSchema.ConnCollation(), ctx.VSchema.CollationEnv(), ctx.VSchema.MySQLVersion()) { + if isConstantFalse(ctx.VSchema.Environment(), expr, ctx.VSchema.ConnCollation()) { return nr } @@ -165,12 +166,11 @@ func UpdateRoutingLogic(ctx *plancontext.PlanningContext, expr sqlparser.Expr, r // isConstantFalse checks whether this predicate can be evaluated at plan-time. If it returns `false` or `null`, // we know that the query will not return anything, and this can be used to produce better plans -func isConstantFalse(expr sqlparser.Expr, collation collations.ID, collationEnv *collations.Environment, mysqlVersion string) bool { - eenv := evalengine.EmptyExpressionEnv(collationEnv, mysqlVersion) +func isConstantFalse(env *vtenv.Environment, expr sqlparser.Expr, collation collations.ID) bool { + eenv := evalengine.EmptyExpressionEnv(env) eexpr, err := evalengine.Translate(expr, &evalengine.Config{ - Collation: collation, - CollationEnv: collationEnv, - MySQLVersion: mysqlVersion, + Collation: collation, + Environment: env, }) if err != nil { return false diff --git a/go/vt/vtgate/planbuilder/operators/sharded_routing.go b/go/vt/vtgate/planbuilder/operators/sharded_routing.go index 2a7f59d4a51..ef6117b1d8e 100644 --- a/go/vt/vtgate/planbuilder/operators/sharded_routing.go +++ b/go/vt/vtgate/planbuilder/operators/sharded_routing.go @@ -653,10 +653,9 @@ func tryMergeJoinShardedRouting( func makeEvalEngineExpr(ctx *plancontext.PlanningContext, n sqlparser.Expr) evalengine.Expr { for _, expr := range ctx.SemTable.GetExprAndEqualities(n) { ee, _ := evalengine.Translate(expr, &evalengine.Config{ - Collation: ctx.SemTable.Collation, - ResolveType: ctx.SemTable.TypeForExpr, - CollationEnv: ctx.VSchema.CollationEnv(), - MySQLVersion: ctx.VSchema.MySQLVersion(), + Collation: ctx.SemTable.Collation, + ResolveType: ctx.SemTable.TypeForExpr, + Environment: ctx.VSchema.Environment(), }) if ee != nil { return ee diff --git a/go/vt/vtgate/planbuilder/plan_test.go b/go/vt/vtgate/planbuilder/plan_test.go index ee109a47f55..a680af9b7fb 100644 --- a/go/vt/vtgate/planbuilder/plan_test.go +++ b/go/vt/vtgate/planbuilder/plan_test.go @@ -39,6 +39,7 @@ import ( "vitess.io/vitess/go/vt/sidecardb" "vitess.io/vitess/go/vt/sqlparser" "vitess.io/vitess/go/vt/topo/memorytopo" + "vitess.io/vitess/go/vt/vtenv" "vitess.io/vitess/go/vt/vtgate/engine" "vitess.io/vitess/go/vt/vtgate/planbuilder/operators" "vitess.io/vitess/go/vt/vtgate/planbuilder/plancontext" @@ -59,6 +60,7 @@ func TestPlan(t *testing.T) { TabletType_: topodatapb.TabletType_PRIMARY, SysVarEnabled: true, TestBuilder: TestBuilder, + Env: vtenv.NewTestEnv(), } testOutputTempDir := makeTestOutput(t) addPKs(t, vschemaWrapper.V, "user", []string{"user", "music"}) @@ -106,6 +108,7 @@ func TestForeignKeyPlanning(t *testing.T) { vschemaWrapper := &vschemawrapper.VSchemaWrapper{ V: vschema, TestBuilder: TestBuilder, + Env: vtenv.NewTestEnv(), } testOutputTempDir := makeTestOutput(t) @@ -122,6 +125,7 @@ func TestForeignKeyChecksOn(t *testing.T) { V: vschema, TestBuilder: TestBuilder, ForeignKeyChecksState: &fkChecksState, + Env: vtenv.NewTestEnv(), } testOutputTempDir := makeTestOutput(t) @@ -138,6 +142,7 @@ func TestForeignKeyChecksOff(t *testing.T) { V: vschema, TestBuilder: TestBuilder, ForeignKeyChecksState: &fkChecksState, + Env: vtenv.NewTestEnv(), } testOutputTempDir := makeTestOutput(t) @@ -232,7 +237,14 @@ func addPKs(t *testing.T, vschema *vindexes.VSchema, ks string, tbls []string) { func TestSystemTables57(t *testing.T) { // first we move everything to use 5.7 logic - vschemaWrapper := &vschemawrapper.VSchemaWrapper{V: loadSchema(t, "vschemas/schema.json", true), MySQLServerVersion: "5.7.9"} + env, err := vtenv.New(vtenv.Options{ + MySQLServerVersion: "5.7.9", + }) + require.NoError(t, err) + vschemaWrapper := &vschemawrapper.VSchemaWrapper{ + V: loadSchema(t, "vschemas/schema.json", true), + Env: env, + } testOutputTempDir := makeTestOutput(t) testFile(t, "info_schema57_cases.json", testOutputTempDir, vschemaWrapper, false) } @@ -241,6 +253,7 @@ func TestSysVarSetDisabled(t *testing.T) { vschemaWrapper := &vschemawrapper.VSchemaWrapper{ V: loadSchema(t, "vschemas/schema.json", true), SysVarEnabled: false, + Env: vtenv.NewTestEnv(), } testFile(t, "set_sysvar_disabled_cases.json", makeTestOutput(t), vschemaWrapper, false) @@ -250,6 +263,7 @@ func TestViews(t *testing.T) { vschemaWrapper := &vschemawrapper.VSchemaWrapper{ V: loadSchema(t, "vschemas/schema.json", true), EnableViews: true, + Env: vtenv.NewTestEnv(), } testFile(t, "view_cases.json", makeTestOutput(t), vschemaWrapper, false) @@ -265,6 +279,7 @@ func TestOne(t *testing.T) { vschema := &vschemawrapper.VSchemaWrapper{ V: lv, TestBuilder: TestBuilder, + Env: vtenv.NewTestEnv(), } testFile(t, "onecase.json", "", vschema, false) @@ -272,7 +287,8 @@ func TestOne(t *testing.T) { func TestOneTPCC(t *testing.T) { vschema := &vschemawrapper.VSchemaWrapper{ - V: loadSchema(t, "vschemas/tpcc_schema.json", true), + V: loadSchema(t, "vschemas/tpcc_schema.json", true), + Env: vtenv.NewTestEnv(), } testFile(t, "onecase.json", "", vschema, false) @@ -285,6 +301,7 @@ func TestOneWithMainAsDefault(t *testing.T) { Name: "main", Sharded: false, }, + Env: vtenv.NewTestEnv(), } testFile(t, "onecase.json", "", vschema, false) @@ -297,6 +314,7 @@ func TestOneWithSecondUserAsDefault(t *testing.T) { Name: "second_user", Sharded: true, }, + Env: vtenv.NewTestEnv(), } testFile(t, "onecase.json", "", vschema, false) @@ -309,6 +327,7 @@ func TestOneWithUserAsDefault(t *testing.T) { Name: "user", Sharded: true, }, + Env: vtenv.NewTestEnv(), } testFile(t, "onecase.json", "", vschema, false) @@ -320,6 +339,7 @@ func TestOneWithTPCHVSchema(t *testing.T) { vschema := &vschemawrapper.VSchemaWrapper{ V: loadSchema(t, "vschemas/tpch_schema.json", true), SysVarEnabled: true, + Env: vtenv.NewTestEnv(), } testFile(t, "onecase.json", "", vschema, false) @@ -327,7 +347,14 @@ func TestOneWithTPCHVSchema(t *testing.T) { func TestOneWith57Version(t *testing.T) { // first we move everything to use 5.7 logic - vschema := &vschemawrapper.VSchemaWrapper{V: loadSchema(t, "vschemas/schema.json", true), MySQLServerVersion: "5.7.9"} + env, err := vtenv.New(vtenv.Options{ + MySQLServerVersion: "5.7.9", + }) + require.NoError(t, err) + vschema := &vschemawrapper.VSchemaWrapper{ + V: loadSchema(t, "vschemas/schema.json", true), + Env: env, + } testFile(t, "onecase.json", "", vschema, false) } @@ -336,6 +363,7 @@ func TestRubyOnRailsQueries(t *testing.T) { vschemaWrapper := &vschemawrapper.VSchemaWrapper{ V: loadSchema(t, "vschemas/rails_schema.json", true), SysVarEnabled: true, + Env: vtenv.NewTestEnv(), } testFile(t, "rails_cases.json", makeTestOutput(t), vschemaWrapper, false) @@ -345,6 +373,7 @@ func TestOLTP(t *testing.T) { vschemaWrapper := &vschemawrapper.VSchemaWrapper{ V: loadSchema(t, "vschemas/oltp_schema.json", true), SysVarEnabled: true, + Env: vtenv.NewTestEnv(), } testFile(t, "oltp_cases.json", makeTestOutput(t), vschemaWrapper, false) @@ -354,6 +383,7 @@ func TestTPCC(t *testing.T) { vschemaWrapper := &vschemawrapper.VSchemaWrapper{ V: loadSchema(t, "vschemas/tpcc_schema.json", true), SysVarEnabled: true, + Env: vtenv.NewTestEnv(), } testFile(t, "tpcc_cases.json", makeTestOutput(t), vschemaWrapper, false) @@ -363,6 +393,7 @@ func TestTPCH(t *testing.T) { vschemaWrapper := &vschemawrapper.VSchemaWrapper{ V: loadSchema(t, "vschemas/tpch_schema.json", true), SysVarEnabled: true, + Env: vtenv.NewTestEnv(), } testFile(t, "tpch_cases.json", makeTestOutput(t), vschemaWrapper, false) @@ -384,6 +415,7 @@ func benchmarkWorkload(b *testing.B, name string) { vschemaWrapper := &vschemawrapper.VSchemaWrapper{ V: loadSchema(b, "vschemas/"+name+"_schema.json", true), SysVarEnabled: true, + Env: vtenv.NewTestEnv(), } testCases := readJSONTests(name + "_cases.json") @@ -403,7 +435,9 @@ func TestBypassPlanningShardTargetFromFile(t *testing.T) { Sharded: false, }, TabletType_: topodatapb.TabletType_PRIMARY, - Dest: key.DestinationShard("-80")} + Dest: key.DestinationShard("-80"), + Env: vtenv.NewTestEnv(), + } testFile(t, "bypass_shard_cases.json", makeTestOutput(t), vschema, false) } @@ -419,6 +453,7 @@ func TestBypassPlanningKeyrangeTargetFromFile(t *testing.T) { }, TabletType_: topodatapb.TabletType_PRIMARY, Dest: key.DestinationExactKeyRange{KeyRange: keyRange[0]}, + Env: vtenv.NewTestEnv(), } testFile(t, "bypass_keyrange_cases.json", makeTestOutput(t), vschema, false) @@ -435,6 +470,7 @@ func TestWithDefaultKeyspaceFromFile(t *testing.T) { Sharded: false, }, TabletType_: topodatapb.TabletType_PRIMARY, + Env: vtenv.NewTestEnv(), } ts := memorytopo.NewServer(ctx, "cell1") ts.CreateKeyspace(ctx, "main", &topodatapb.Keyspace{}) @@ -468,6 +504,7 @@ func TestWithDefaultKeyspaceFromFileSharded(t *testing.T) { Sharded: true, }, TabletType_: topodatapb.TabletType_PRIMARY, + Env: vtenv.NewTestEnv(), } testOutputTempDir := makeTestOutput(t) @@ -483,6 +520,7 @@ func TestWithUserDefaultKeyspaceFromFileSharded(t *testing.T) { Sharded: true, }, TabletType_: topodatapb.TabletType_PRIMARY, + Env: vtenv.NewTestEnv(), } testOutputTempDir := makeTestOutput(t) @@ -495,6 +533,7 @@ func TestWithSystemSchemaAsDefaultKeyspace(t *testing.T) { V: loadSchema(t, "vschemas/schema.json", true), Keyspace: &vindexes.Keyspace{Name: "information_schema"}, TabletType_: topodatapb.TabletType_PRIMARY, + Env: vtenv.NewTestEnv(), } testFile(t, "sysschema_default.json", makeTestOutput(t), vschema, false) @@ -509,6 +548,7 @@ func TestOtherPlanningFromFile(t *testing.T) { Sharded: false, }, TabletType_: topodatapb.TabletType_PRIMARY, + Env: vtenv.NewTestEnv(), } testOutputTempDir := makeTestOutput(t) @@ -688,6 +728,7 @@ func BenchmarkPlanner(b *testing.B) { vschema := &vschemawrapper.VSchemaWrapper{ V: loadSchema(b, "vschemas/schema.json", true), SysVarEnabled: true, + Env: vtenv.NewTestEnv(), } for _, filename := range benchMarkFiles { testCases := readJSONTests(filename) @@ -704,6 +745,7 @@ func BenchmarkSemAnalysis(b *testing.B) { vschema := &vschemawrapper.VSchemaWrapper{ V: loadSchema(b, "vschemas/schema.json", true), SysVarEnabled: true, + Env: vtenv.NewTestEnv(), } for i := 0; i < b.N; i++ { @@ -738,6 +780,7 @@ func BenchmarkSelectVsDML(b *testing.B) { V: loadSchema(b, "vschemas/schema.json", true), SysVarEnabled: true, Version: Gen4, + Env: vtenv.NewTestEnv(), } dmlCases := readJSONTests("dml_cases.json") diff --git a/go/vt/vtgate/planbuilder/plancontext/vschema.go b/go/vt/vtgate/planbuilder/plancontext/vschema.go index bf2f50b6117..1bca80cbc94 100644 --- a/go/vt/vtgate/planbuilder/plancontext/vschema.go +++ b/go/vt/vtgate/planbuilder/plancontext/vschema.go @@ -6,6 +6,7 @@ import ( vschemapb "vitess.io/vitess/go/vt/proto/vschema" vtgatepb "vitess.io/vitess/go/vt/proto/vtgate" + "vitess.io/vitess/go/vt/vtenv" "vitess.io/vitess/go/vt/vtgate/engine" "vitess.io/vitess/go/mysql/collations" @@ -41,8 +42,7 @@ type VSchema interface { Planner() PlannerVersion SetPlannerVersion(pv PlannerVersion) ConnCollation() collations.ID - CollationEnv() *collations.Environment - MySQLVersion() string + Environment() *vtenv.Environment // ErrorIfShardedF will return an error if the keyspace is sharded, // and produce a warning if the vtgate if configured to do so @@ -93,9 +93,6 @@ type VSchema interface { // StorePrepareData stores the prepared data in the session. StorePrepareData(name string, v *vtgatepb.PrepareData) - - // SQLParser returns the proper sqlparser instance with the right version. - SQLParser() *sqlparser.Parser } // PlannerNameToVersion returns the numerical representation of the planner diff --git a/go/vt/vtgate/planbuilder/predicate_rewrite_test.go b/go/vt/vtgate/planbuilder/predicate_rewrite_test.go index cd6bf10bac2..ba1d60ff234 100644 --- a/go/vt/vtgate/planbuilder/predicate_rewrite_test.go +++ b/go/vt/vtgate/planbuilder/predicate_rewrite_test.go @@ -27,10 +27,10 @@ import ( "github.com/stretchr/testify/require" "vitess.io/vitess/go/mysql/collations" - "vitess.io/vitess/go/mysql/config" "vitess.io/vitess/go/sqltypes" "vitess.io/vitess/go/vt/sqlparser" + "vitess.io/vitess/go/vt/vtenv" "vitess.io/vitess/go/vt/vtgate/evalengine" ) @@ -91,6 +91,7 @@ func TestFuzzRewriting(t *testing.T) { // values - trying TRUE, FALSE and NULL. If the two expressions do not return the same value, // this is considered a test failure. + venv := vtenv.NewTestEnv() start := time.Now() for time.Since(start) < 1*time.Second { tc := testCase{ @@ -104,21 +105,19 @@ func TestFuzzRewriting(t *testing.T) { simplified := sqlparser.RewritePredicate(predicate) original, err := evalengine.Translate(predicate, &evalengine.Config{ + Environment: venv, Collation: collations.MySQL8().DefaultConnectionCharset(), - CollationEnv: collations.MySQL8(), - MySQLVersion: config.DefaultMySQLVersion, ResolveColumn: resolveForFuzz, }) require.NoError(t, err) simpler, err := evalengine.Translate(simplified.(sqlparser.Expr), &evalengine.Config{ + Environment: venv, Collation: collations.MySQL8().DefaultConnectionCharset(), - CollationEnv: collations.MySQL8(), - MySQLVersion: config.DefaultMySQLVersion, ResolveColumn: resolveForFuzz, }) require.NoError(t, err) - env := evalengine.EmptyExpressionEnv(collations.MySQL8(), config.DefaultMySQLVersion) + env := evalengine.EmptyExpressionEnv(venv) env.Row = make([]sqltypes.Value, tc.nodes) for i := range env.Row { env.Row[i] = sqltypes.NewInt32(1) diff --git a/go/vt/vtgate/planbuilder/route.go b/go/vt/vtgate/planbuilder/route.go index c03ab8c8801..d4f72f0ff3a 100644 --- a/go/vt/vtgate/planbuilder/route.go +++ b/go/vt/vtgate/planbuilder/route.go @@ -73,7 +73,7 @@ func (rb *route) Wireup(ctx *plancontext.PlanningContext) error { } query, args := planableVindex.Query() - stmt, reserved, err := ctx.VSchema.SQLParser().Parse2(query) + stmt, reserved, err := ctx.VSchema.Environment().Parser().Parse2(query) if err != nil { return err } diff --git a/go/vt/vtgate/planbuilder/select.go b/go/vt/vtgate/planbuilder/select.go index 8e0ea2b6b72..13671e7efa0 100644 --- a/go/vt/vtgate/planbuilder/select.go +++ b/go/vt/vtgate/planbuilder/select.go @@ -130,7 +130,7 @@ func buildSQLCalcFoundRowsPlan( return nil, nil, err } - statement2, reserved2, err := vschema.SQLParser().Parse2(originalQuery) + statement2, reserved2, err := vschema.Environment().Parser().Parse2(originalQuery) if err != nil { return nil, nil, err } @@ -290,9 +290,8 @@ func handleDualSelects(sel *sqlparser.Select, vschema plancontext.VSchema) (engi elem := &engine.LockFunc{Typ: expr.Expr.(*sqlparser.LockingFunc)} if lFunc.Name != nil { n, err := evalengine.Translate(lFunc.Name, &evalengine.Config{ - Collation: vschema.ConnCollation(), - CollationEnv: vschema.CollationEnv(), - MySQLVersion: vschema.MySQLVersion(), + Collation: vschema.ConnCollation(), + Environment: vschema.Environment(), }) if err != nil { return nil, err @@ -306,9 +305,8 @@ func handleDualSelects(sel *sqlparser.Select, vschema plancontext.VSchema) (engi return nil, vterrors.VT12001(fmt.Sprintf("LOCK function and other expression: [%s] in same select query", sqlparser.String(expr))) } exprs[i], err = evalengine.Translate(expr.Expr, &evalengine.Config{ - Collation: vschema.ConnCollation(), - CollationEnv: vschema.CollationEnv(), - MySQLVersion: vschema.MySQLVersion(), + Collation: vschema.ConnCollation(), + Environment: vschema.Environment(), }) if err != nil { return nil, nil diff --git a/go/vt/vtgate/planbuilder/set.go b/go/vt/vtgate/planbuilder/set.go index dda14d5f38d..bf6820b7489 100644 --- a/go/vt/vtgate/planbuilder/set.go +++ b/go/vt/vtgate/planbuilder/set.go @@ -56,9 +56,8 @@ func buildSetPlan(stmt *sqlparser.Set, vschema plancontext.VSchema) (*planResult var err error ec := &expressionConverter{ - collationEnv: vschema.CollationEnv(), - collation: vschema.ConnCollation(), - mysqlVersion: vschema.MySQLVersion(), + env: vschema.Environment(), + collation: vschema.ConnCollation(), } for _, expr := range stmt.Exprs { @@ -84,7 +83,7 @@ func buildSetPlan(stmt *sqlparser.Set, vschema plancontext.VSchema) (*planResult } setOps = append(setOps, setOp) case sqlparser.NextTxScope, sqlparser.SessionScope: - planFunc, err := sysvarPlanningFuncs.Get(expr, vschema.CollationEnv(), vschema.SQLParser(), vschema.MySQLVersion()) + planFunc, err := sysvarPlanningFuncs.Get(vschema.Environment(), expr) if err != nil { return nil, err } diff --git a/go/vt/vtgate/planbuilder/show.go b/go/vt/vtgate/planbuilder/show.go index 2955e1f0fca..1830f197fa8 100644 --- a/go/vt/vtgate/planbuilder/show.go +++ b/go/vt/vtgate/planbuilder/show.go @@ -561,11 +561,11 @@ func buildShowVGtidPlan(show *sqlparser.ShowBasic, vschema plancontext.VSchema) } return &engine.OrderedAggregate{ Aggregates: []*engine.AggregateParams{ - engine.NewAggregateParam(popcode.AggregateGtid, 1, "global vgtid_executed", vschema.CollationEnv()), + engine.NewAggregateParam(popcode.AggregateGtid, 1, "global vgtid_executed", vschema.Environment().CollationEnv()), }, TruncateColumnCount: 2, Input: send, - CollationEnv: vschema.CollationEnv(), + CollationEnv: vschema.Environment().CollationEnv(), }, nil } diff --git a/go/vt/vtgate/planbuilder/show_test.go b/go/vt/vtgate/planbuilder/show_test.go index f68622e7a27..931c914149d 100644 --- a/go/vt/vtgate/planbuilder/show_test.go +++ b/go/vt/vtgate/planbuilder/show_test.go @@ -22,6 +22,7 @@ import ( "testing" "vitess.io/vitess/go/test/vschemawrapper" + "vitess.io/vitess/go/vt/vtenv" "github.com/stretchr/testify/require" @@ -35,6 +36,7 @@ import ( func TestBuildDBPlan(t *testing.T) { vschema := &vschemawrapper.VSchemaWrapper{ Keyspace: &vindexes.Keyspace{Name: "main"}, + Env: vtenv.NewTestEnv(), } testCases := []struct { diff --git a/go/vt/vtgate/planbuilder/simplifier_test.go b/go/vt/vtgate/planbuilder/simplifier_test.go index e13fef7ae70..61ed220bd44 100644 --- a/go/vt/vtgate/planbuilder/simplifier_test.go +++ b/go/vt/vtgate/planbuilder/simplifier_test.go @@ -27,6 +27,7 @@ import ( "vitess.io/vitess/go/test/vschemawrapper" "vitess.io/vitess/go/vt/log" "vitess.io/vitess/go/vt/sqlparser" + "vitess.io/vitess/go/vt/vtenv" "vitess.io/vitess/go/vt/vterrors" "vitess.io/vitess/go/vt/vtgate/simplifier" ) @@ -40,6 +41,7 @@ func TestSimplifyBuggyQuery(t *testing.T) { vschema := &vschemawrapper.VSchemaWrapper{ V: loadSchema(t, "vschemas/schema.json", true), Version: Gen4, + Env: vtenv.NewTestEnv(), } stmt, reserved, err := sqlparser.NewTestParser().Parse2(query) require.NoError(t, err) @@ -62,6 +64,7 @@ func TestSimplifyPanic(t *testing.T) { vschema := &vschemawrapper.VSchemaWrapper{ V: loadSchema(t, "vschemas/schema.json", true), Version: Gen4, + Env: vtenv.NewTestEnv(), } stmt, reserved, err := sqlparser.NewTestParser().Parse2(query) require.NoError(t, err) @@ -83,6 +86,7 @@ func TestUnsupportedFile(t *testing.T) { vschema := &vschemawrapper.VSchemaWrapper{ V: loadSchema(t, "vschemas/schema.json", true), Version: Gen4, + Env: vtenv.NewTestEnv(), } fmt.Println(vschema) for _, tcase := range readJSONTests("unsupported_cases.txt") { diff --git a/go/vt/vtgate/planbuilder/system_variables.go b/go/vt/vtgate/planbuilder/system_variables.go index 4517b6b359b..8ef968a2ac8 100644 --- a/go/vt/vtgate/planbuilder/system_variables.go +++ b/go/vt/vtgate/planbuilder/system_variables.go @@ -20,19 +20,17 @@ import ( "fmt" "sync" - "vitess.io/vitess/go/mysql/collations" "vitess.io/vitess/go/vt/sqlparser" "vitess.io/vitess/go/vt/sysvars" + "vitess.io/vitess/go/vt/vtenv" "vitess.io/vitess/go/vt/vterrors" "vitess.io/vitess/go/vt/vtgate/evalengine" ) type sysvarPlanCache struct { - funcs map[string]planFunc - once sync.Once - collationEnv *collations.Environment - parser *sqlparser.Parser - mysqlVersion string + funcs map[string]planFunc + once sync.Once + env *vtenv.Environment } func (pc *sysvarPlanCache) initForSettings(systemVariables []sysvars.SystemVariable, f func(setting) planFunc) { @@ -57,16 +55,15 @@ func (pc *sysvarPlanCache) initForSettings(systemVariables []sysvars.SystemVaria } func (pc *sysvarPlanCache) parseAndBuildDefaultValue(sysvar sysvars.SystemVariable) evalengine.Expr { - stmt, err := pc.parser.Parse(fmt.Sprintf("select %s", sysvar.Default)) + stmt, err := pc.env.Parser().Parse(fmt.Sprintf("select %s", sysvar.Default)) if err != nil { panic(fmt.Sprintf("bug in set plan init - default value for %s not parsable: %s", sysvar.Name, sysvar.Default)) } sel := stmt.(*sqlparser.Select) aliasedExpr := sel.SelectExprs[0].(*sqlparser.AliasedExpr) def, err := evalengine.Translate(aliasedExpr.Expr, &evalengine.Config{ - Collation: pc.collationEnv.DefaultConnectionCharset(), - CollationEnv: pc.collationEnv, - MySQLVersion: pc.mysqlVersion, + Collation: pc.env.CollationEnv().DefaultConnectionCharset(), + Environment: pc.env, }) if err != nil { panic(fmt.Sprintf("bug in set plan init - default value for %s not able to convert to evalengine.Expr: %s", sysvar.Name, sysvar.Default)) @@ -74,11 +71,9 @@ func (pc *sysvarPlanCache) parseAndBuildDefaultValue(sysvar sysvars.SystemVariab return def } -func (pc *sysvarPlanCache) init(collationEnv *collations.Environment, parser *sqlparser.Parser, mysqlVersion string) { +func (pc *sysvarPlanCache) init(env *vtenv.Environment) { pc.once.Do(func() { - pc.collationEnv = collationEnv - pc.parser = parser - pc.mysqlVersion = mysqlVersion + pc.env = env pc.funcs = make(map[string]planFunc) pc.initForSettings(sysvars.ReadOnly, buildSetOpReadOnly) pc.initForSettings(sysvars.IgnoreThese, buildSetOpIgnore) @@ -91,8 +86,8 @@ func (pc *sysvarPlanCache) init(collationEnv *collations.Environment, parser *sq var sysvarPlanningFuncs sysvarPlanCache -func (pc *sysvarPlanCache) Get(expr *sqlparser.SetExpr, collationEnv *collations.Environment, parser *sqlparser.Parser, mysqlVersion string) (planFunc, error) { - pc.init(collationEnv, parser, mysqlVersion) +func (pc *sysvarPlanCache) Get(env *vtenv.Environment, expr *sqlparser.SetExpr) (planFunc, error) { + pc.init(env) pf, ok := pc.funcs[expr.Var.Name.Lowered()] if !ok { return nil, vterrors.VT05006(sqlparser.String(expr)) diff --git a/go/vt/vtgate/planbuilder/vindex_op.go b/go/vt/vtgate/planbuilder/vindex_op.go index cfc205eef79..ffd304aa06d 100644 --- a/go/vt/vtgate/planbuilder/vindex_op.go +++ b/go/vt/vtgate/planbuilder/vindex_op.go @@ -33,10 +33,9 @@ func transformVindexPlan(ctx *plancontext.PlanningContext, op *operators.Vindex) } expr, err := evalengine.Translate(op.Value, &evalengine.Config{ - Collation: ctx.SemTable.Collation, - ResolveType: ctx.SemTable.TypeForExpr, - CollationEnv: ctx.VSchema.CollationEnv(), - MySQLVersion: ctx.VSchema.MySQLVersion(), + Collation: ctx.SemTable.Collation, + ResolveType: ctx.SemTable.TypeForExpr, + Environment: ctx.VSchema.Environment(), }) if err != nil { return nil, err diff --git a/go/vt/vtgate/plugin_mysql_server.go b/go/vt/vtgate/plugin_mysql_server.go index 0508b7029ba..465def6e9a7 100644 --- a/go/vt/vtgate/plugin_mysql_server.go +++ b/go/vt/vtgate/plugin_mysql_server.go @@ -421,7 +421,7 @@ func (vh *vtgateHandler) KillQuery(connectionID uint32) error { } func (vh *vtgateHandler) SQLParser() *sqlparser.Parser { - return vh.vtg.executor.parser + return vh.vtg.executor.env.Parser() } func (vh *vtgateHandler) session(c *mysql.Conn) *vtgatepb.Session { diff --git a/go/vt/vtgate/querylog.go b/go/vt/vtgate/querylog.go index 5d0dedf87d7..bddc799363d 100644 --- a/go/vt/vtgate/querylog.go +++ b/go/vt/vtgate/querylog.go @@ -42,7 +42,7 @@ func (e *Executor) defaultQueryLogger() error { servenv.HTTPHandleFunc(QueryLogzHandler, func(w http.ResponseWriter, r *http.Request) { ch := queryLogger.Subscribe("querylogz") defer queryLogger.Unsubscribe(ch) - querylogzHandler(ch, w, r, e.parser) + querylogzHandler(ch, w, r, e.env.Parser()) }) servenv.HTTPHandleFunc(QueryzHandler, func(w http.ResponseWriter, r *http.Request) { diff --git a/go/vt/vtgate/queryz.go b/go/vt/vtgate/queryz.go index 93bf347eeff..540b014e11c 100644 --- a/go/vt/vtgate/queryz.go +++ b/go/vt/vtgate/queryz.go @@ -144,7 +144,7 @@ func queryzHandler(e *Executor, w http.ResponseWriter, r *http.Request) { e.ForEachPlan(func(plan *engine.Plan) bool { Value := &queryzRow{ - Query: logz.Wrappable(e.parser.TruncateForUI(plan.Original)), + Query: logz.Wrappable(e.env.Parser().TruncateForUI(plan.Original)), } Value.Count, Value.tm, Value.ShardQueries, Value.RowsAffected, Value.RowsReturned, Value.Errors = plan.Stats() var timepq time.Duration diff --git a/go/vt/vtgate/semantics/FakeSI.go b/go/vt/vtgate/semantics/FakeSI.go index 860232c8acc..933f4cd40f8 100644 --- a/go/vt/vtgate/semantics/FakeSI.go +++ b/go/vt/vtgate/semantics/FakeSI.go @@ -20,11 +20,11 @@ import ( "fmt" "vitess.io/vitess/go/mysql/collations" - "vitess.io/vitess/go/mysql/config" "vitess.io/vitess/go/vt/key" topodatapb "vitess.io/vitess/go/vt/proto/topodata" vschemapb "vitess.io/vitess/go/vt/proto/vschema" "vitess.io/vitess/go/vt/sqlparser" + "vitess.io/vitess/go/vt/vtenv" "vitess.io/vitess/go/vt/vtgate/vindexes" ) @@ -51,12 +51,8 @@ func (*FakeSI) ConnCollation() collations.ID { return collations.CollationUtf8mb4ID } -func (*FakeSI) CollationEnv() *collations.Environment { - return collations.MySQL8() -} - -func (*FakeSI) MySQLVersion() string { - return config.DefaultMySQLVersion +func (s *FakeSI) Environment() *vtenv.Environment { + return vtenv.NewTestEnv() } func (s *FakeSI) ForeignKeyMode(keyspace string) (vschemapb.Keyspace_ForeignKeyMode, error) { diff --git a/go/vt/vtgate/semantics/analyzer.go b/go/vt/vtgate/semantics/analyzer.go index d71d9fd00fa..eba50d47286 100644 --- a/go/vt/vtgate/semantics/analyzer.go +++ b/go/vt/vtgate/semantics/analyzer.go @@ -50,7 +50,7 @@ func newAnalyzer(dbName string, si SchemaInformation) *analyzer { a := &analyzer{ scoper: s, tables: newTableCollector(s, si, dbName), - typer: newTyper(si.CollationEnv()), + typer: newTyper(si.Environment().CollationEnv()), } s.org = a a.tables.org = a @@ -58,11 +58,10 @@ func newAnalyzer(dbName string, si SchemaInformation) *analyzer { b := newBinder(s, a, a.tables, a.typer) a.binder = b a.rewriter = &earlyRewriter{ + env: si.Environment(), scoper: s, binder: b, expandedColumns: map[sqlparser.TableName][]*sqlparser.ColName{}, - collationEnv: si.CollationEnv(), - mysqlVersion: si.MySQLVersion(), } s.binder = b return a diff --git a/go/vt/vtgate/semantics/early_rewriter.go b/go/vt/vtgate/semantics/early_rewriter.go index e4ad42fef7b..08d432ae9d0 100644 --- a/go/vt/vtgate/semantics/early_rewriter.go +++ b/go/vt/vtgate/semantics/early_rewriter.go @@ -20,9 +20,9 @@ import ( "fmt" "strconv" - "vitess.io/vitess/go/mysql/collations" vtrpcpb "vitess.io/vitess/go/vt/proto/vtrpc" "vitess.io/vitess/go/vt/sqlparser" + "vitess.io/vitess/go/vt/vtenv" "vitess.io/vitess/go/vt/vterrors" "vitess.io/vitess/go/vt/vtgate/evalengine" ) @@ -33,8 +33,7 @@ type earlyRewriter struct { clause string warning string expandedColumns map[sqlparser.TableName][]*sqlparser.ColName - collationEnv *collations.Environment - mysqlVersion string + env *vtenv.Environment } func (r *earlyRewriter) down(cursor *sqlparser.Cursor) error { @@ -54,9 +53,9 @@ func (r *earlyRewriter) down(cursor *sqlparser.Cursor) error { return r.handleOrderByAndGroupBy(cursor.Parent(), iter) case *sqlparser.OrExpr: - rewriteOrExpr(cursor, node, r.collationEnv, r.mysqlVersion) + rewriteOrExpr(r.env, cursor, node) case *sqlparser.AndExpr: - rewriteAndExpr(cursor, node, r.collationEnv, r.mysqlVersion) + rewriteAndExpr(r.env, cursor, node) case *sqlparser.NotExpr: rewriteNotExpr(cursor, node) case sqlparser.GroupBy: @@ -481,35 +480,34 @@ func (r *earlyRewriter) rewriteOrderByExpr(node *sqlparser.Literal) (sqlparser.E } // rewriteOrExpr rewrites OR expressions when the right side is FALSE. -func rewriteOrExpr(cursor *sqlparser.Cursor, node *sqlparser.OrExpr, collationEnv *collations.Environment, mysqlVersion string) { - newNode := rewriteOrFalse(*node, collationEnv, mysqlVersion) +func rewriteOrExpr(env *vtenv.Environment, cursor *sqlparser.Cursor, node *sqlparser.OrExpr) { + newNode := rewriteOrFalse(env, *node) if newNode != nil { cursor.ReplaceAndRevisit(newNode) } } // rewriteAndExpr rewrites AND expressions when either side is TRUE. -func rewriteAndExpr(cursor *sqlparser.Cursor, node *sqlparser.AndExpr, collationEnv *collations.Environment, mysqlVersion string) { - newNode := rewriteAndTrue(*node, collationEnv, mysqlVersion) +func rewriteAndExpr(env *vtenv.Environment, cursor *sqlparser.Cursor, node *sqlparser.AndExpr) { + newNode := rewriteAndTrue(env, *node) if newNode != nil { cursor.ReplaceAndRevisit(newNode) } } -func rewriteAndTrue(andExpr sqlparser.AndExpr, collationEnv *collations.Environment, mysqlVersion string) sqlparser.Expr { +func rewriteAndTrue(env *vtenv.Environment, andExpr sqlparser.AndExpr) sqlparser.Expr { // we are looking for the pattern `WHERE c = 1 AND 1 = 1` isTrue := func(subExpr sqlparser.Expr) bool { - coll := collationEnv.DefaultConnectionCharset() + coll := env.CollationEnv().DefaultConnectionCharset() evalEnginePred, err := evalengine.Translate(subExpr, &evalengine.Config{ - CollationEnv: collationEnv, - Collation: coll, - MySQLVersion: mysqlVersion, + Environment: env, + Collation: coll, }) if err != nil { return false } - env := evalengine.EmptyExpressionEnv(collationEnv, mysqlVersion) + env := evalengine.EmptyExpressionEnv(env) res, err := env.Evaluate(evalEnginePred) if err != nil { return false @@ -597,20 +595,19 @@ func realCloneOfColNames(expr sqlparser.Expr, union bool) sqlparser.Expr { }, nil).(sqlparser.Expr) } -func rewriteOrFalse(orExpr sqlparser.OrExpr, collationEnv *collations.Environment, mysqlVersion string) sqlparser.Expr { +func rewriteOrFalse(env *vtenv.Environment, orExpr sqlparser.OrExpr) sqlparser.Expr { // we are looking for the pattern `WHERE c = 1 OR 1 = 0` isFalse := func(subExpr sqlparser.Expr) bool { - coll := collationEnv.DefaultConnectionCharset() + coll := env.CollationEnv().DefaultConnectionCharset() evalEnginePred, err := evalengine.Translate(subExpr, &evalengine.Config{ - CollationEnv: collationEnv, - Collation: coll, - MySQLVersion: mysqlVersion, + Environment: env, + Collation: coll, }) if err != nil { return false } - env := evalengine.EmptyExpressionEnv(collationEnv, mysqlVersion) + env := evalengine.EmptyExpressionEnv(env) res, err := env.Evaluate(evalEnginePred) if err != nil { return false diff --git a/go/vt/vtgate/semantics/info_schema.go b/go/vt/vtgate/semantics/info_schema.go index 0442ab7ca19..d7470e2fd0a 100644 --- a/go/vt/vtgate/semantics/info_schema.go +++ b/go/vt/vtgate/semantics/info_schema.go @@ -26,6 +26,7 @@ import ( topodatapb "vitess.io/vitess/go/vt/proto/topodata" vschemapb "vitess.io/vitess/go/vt/proto/vschema" "vitess.io/vitess/go/vt/sqlparser" + "vitess.io/vitess/go/vt/vtenv" "vitess.io/vitess/go/vt/vtgate/vindexes" ) @@ -1610,7 +1611,7 @@ var infoSchema80 = getInfoSchema80() // newSchemaInfo returns a SchemaInformation that has the column information for all info_schema tables func newSchemaInfo(inner SchemaInformation) SchemaInformation { - return &infoSchemaWithColumns{inner: inner, infoSchemaData: loadSchemaInfo(inner.MySQLVersion())} + return &infoSchemaWithColumns{inner: inner, infoSchemaData: loadSchemaInfo(inner.Environment().MySQLVersion())} } func loadSchemaInfo(version string) map[string][]vindexes.Column { @@ -1645,13 +1646,8 @@ func (i *infoSchemaWithColumns) ConnCollation() collations.ID { return i.inner.ConnCollation() } -// CollationEnv implements the SchemaInformation interface -func (i *infoSchemaWithColumns) CollationEnv() *collations.Environment { - return i.inner.CollationEnv() -} - -func (i *infoSchemaWithColumns) MySQLVersion() string { - return i.inner.MySQLVersion() +func (i *infoSchemaWithColumns) Environment() *vtenv.Environment { + return i.inner.Environment() } func (i *infoSchemaWithColumns) ForeignKeyMode(keyspace string) (vschemapb.Keyspace_ForeignKeyMode, error) { diff --git a/go/vt/vtgate/semantics/semantic_state.go b/go/vt/vtgate/semantics/semantic_state.go index 68eaf89933c..d52875de9b8 100644 --- a/go/vt/vtgate/semantics/semantic_state.go +++ b/go/vt/vtgate/semantics/semantic_state.go @@ -26,6 +26,7 @@ import ( vschemapb "vitess.io/vitess/go/vt/proto/vschema" vtrpcpb "vitess.io/vitess/go/vt/proto/vtrpc" "vitess.io/vitess/go/vt/sqlparser" + "vitess.io/vitess/go/vt/vtenv" "vitess.io/vitess/go/vt/vterrors" "vitess.io/vitess/go/vt/vtgate/evalengine" "vitess.io/vitess/go/vt/vtgate/vindexes" @@ -152,8 +153,7 @@ type ( SchemaInformation interface { FindTableOrVindex(tablename sqlparser.TableName) (*vindexes.Table, vindexes.Vindex, string, topodatapb.TabletType, key.Destination, error) ConnCollation() collations.ID - CollationEnv() *collations.Environment - MySQLVersion() string + Environment() *vtenv.Environment // ForeignKeyMode returns the foreign_key flag value ForeignKeyMode(keyspace string) (vschemapb.Keyspace_ForeignKeyMode, error) GetForeignKeyChecksState() *bool diff --git a/go/vt/vtgate/semantics/table_collector.go b/go/vt/vtgate/semantics/table_collector.go index bcf0402433a..3d070583347 100644 --- a/go/vt/vtgate/semantics/table_collector.go +++ b/go/vt/vtgate/semantics/table_collector.go @@ -235,7 +235,7 @@ func (tc *tableCollector) createTable( ASTNode: alias, Table: tbl, isInfSchema: isInfSchema, - collationEnv: tc.si.CollationEnv(), + collationEnv: tc.si.Environment().CollationEnv(), } if alias.As.IsEmpty() { diff --git a/go/vt/vtgate/simplifier/simplifier_test.go b/go/vt/vtgate/simplifier/simplifier_test.go index f63f346b279..340497da8ef 100644 --- a/go/vt/vtgate/simplifier/simplifier_test.go +++ b/go/vt/vtgate/simplifier/simplifier_test.go @@ -23,9 +23,9 @@ import ( "github.com/stretchr/testify/require" "vitess.io/vitess/go/mysql/collations" - "vitess.io/vitess/go/mysql/config" "vitess.io/vitess/go/vt/log" "vitess.io/vitess/go/vt/sqlparser" + "vitess.io/vitess/go/vt/vtenv" "vitess.io/vitess/go/vt/vtgate/evalengine" ) @@ -120,17 +120,17 @@ func TestSimplifyEvalEngineExpr(t *testing.T) { // L0 p0 := plus(p11, p12) + venv := vtenv.NewTestEnv() expr := SimplifyExpr(p0, func(expr sqlparser.Expr) bool { collationEnv := collations.MySQL8() local, err := evalengine.Translate(expr, &evalengine.Config{ - CollationEnv: collationEnv, - Collation: collationEnv.DefaultConnectionCharset(), - MySQLVersion: config.DefaultMySQLVersion, + Environment: venv, + Collation: collationEnv.DefaultConnectionCharset(), }) if err != nil { return false } - res, err := evalengine.EmptyExpressionEnv(collationEnv, config.DefaultMySQLVersion).Evaluate(local) + res, err := evalengine.EmptyExpressionEnv(venv).Evaluate(local) if err != nil { return false } diff --git a/go/vt/vtgate/vcursor_impl.go b/go/vt/vtgate/vcursor_impl.go index 756c0b0fb32..67093046ded 100644 --- a/go/vt/vtgate/vcursor_impl.go +++ b/go/vt/vtgate/vcursor_impl.go @@ -46,6 +46,7 @@ import ( "vitess.io/vitess/go/vt/topo" topoprotopb "vitess.io/vitess/go/vt/topo/topoproto" "vitess.io/vitess/go/vt/topotools" + "vitess.io/vitess/go/vt/vtenv" "vitess.io/vitess/go/vt/vterrors" "vitess.io/vitess/go/vt/vtgate/buffer" "vitess.io/vitess/go/vt/vtgate/engine" @@ -84,9 +85,7 @@ type iExecute interface { VSchema() *vindexes.VSchema planPrepareStmt(ctx context.Context, vcursor *vcursorImpl, query string) (*engine.Plan, sqlparser.Statement, error) - collationEnv() *collations.Environment - sqlparser() *sqlparser.Parser - mysqlServerVersion() string + environment() *vtenv.Environment } // VSchemaOperator is an interface to Vschema Operations @@ -166,7 +165,7 @@ func newVCursorImpl( } } if connCollation == collations.Unknown { - connCollation = executor.collEnv.DefaultConnectionCharset() + connCollation = executor.env.CollationEnv().DefaultConnectionCharset() } warmingReadsPct := 0 @@ -210,18 +209,9 @@ func (vc *vcursorImpl) ConnCollation() collations.ID { return vc.collation } -// ConnCollation returns the collation of this session -func (vc *vcursorImpl) CollationEnv() *collations.Environment { - return vc.executor.collationEnv() -} - -// ConnCollation returns the collation of this session -func (vc *vcursorImpl) MySQLVersion() string { - return vc.executor.mysqlServerVersion() -} - -func (vc *vcursorImpl) SQLParser() *sqlparser.Parser { - return vc.executor.sqlparser() +// Environment returns the vtenv associated with this session +func (vc *vcursorImpl) Environment() *vtenv.Environment { + return vc.executor.environment() } func (vc *vcursorImpl) TimeZone() *time.Location { @@ -1100,7 +1090,7 @@ func (vc *vcursorImpl) keyForPlan(ctx context.Context, query string, buf io.Stri _, _ = buf.WriteString(vc.keyspace) _, _ = buf.WriteString(vindexes.TabletTypeSuffix[vc.tabletType]) _, _ = buf.WriteString("+Collate:") - _, _ = buf.WriteString(vc.CollationEnv().LookupName(vc.collation)) + _, _ = buf.WriteString(vc.Environment().CollationEnv().LookupName(vc.collation)) if vc.destination != nil { switch vc.destination.(type) { @@ -1258,7 +1248,7 @@ func (vc *vcursorImpl) ThrottleApp(ctx context.Context, throttledAppRule *topoda } func (vc *vcursorImpl) CanUseSetVar() bool { - return vc.SQLParser().IsMySQL80AndAbove() && setVarEnabled + return vc.Environment().Parser().IsMySQL80AndAbove() && setVarEnabled } func (vc *vcursorImpl) ReleaseLock(ctx context.Context) error { @@ -1287,7 +1277,7 @@ func (vc *vcursorImpl) cloneWithAutocommitSession() *vcursorImpl { } func (vc *vcursorImpl) VExplainLogging() { - vc.safeSession.EnableLogging(vc.SQLParser()) + vc.safeSession.EnableLogging(vc.Environment().Parser()) } func (vc *vcursorImpl) GetVExplainLogs() []engine.ExecuteEntry { diff --git a/go/vt/vtgate/vindexes/consistent_lookup.go b/go/vt/vtgate/vindexes/consistent_lookup.go index d51d9d2e386..f065bfca5e0 100644 --- a/go/vt/vtgate/vindexes/consistent_lookup.go +++ b/go/vt/vtgate/vindexes/consistent_lookup.go @@ -412,7 +412,7 @@ func (lu *clCommon) Delete(ctx context.Context, vcursor VCursor, rowsColValues [ func (lu *clCommon) Update(ctx context.Context, vcursor VCursor, oldValues []sqltypes.Value, ksid []byte, newValues []sqltypes.Value) error { equal := true for i := range oldValues { - result, err := evalengine.NullsafeCompare(oldValues[i], newValues[i], vcursor.CollationEnv(), vcursor.ConnCollation()) + result, err := evalengine.NullsafeCompare(oldValues[i], newValues[i], vcursor.Environment().CollationEnv(), vcursor.ConnCollation()) // errors from NullsafeCompare can be ignored. if they are real problems, we'll see them in the Create/Update if err != nil || result != 0 { equal = false diff --git a/go/vt/vtgate/vindexes/consistent_lookup_test.go b/go/vt/vtgate/vindexes/consistent_lookup_test.go index a6f1046e201..0279ecaba78 100644 --- a/go/vt/vtgate/vindexes/consistent_lookup_test.go +++ b/go/vt/vtgate/vindexes/consistent_lookup_test.go @@ -40,6 +40,7 @@ import ( vtgatepb "vitess.io/vitess/go/vt/proto/vtgate" vtrpcpb "vitess.io/vitess/go/vt/proto/vtrpc" "vitess.io/vitess/go/vt/sqlparser" + "vitess.io/vitess/go/vt/vtenv" "vitess.io/vitess/go/vt/vterrors" ) @@ -560,11 +561,11 @@ func (vc *loggingVCursor) InTransactionAndIsDML() bool { } func (vc *loggingVCursor) ConnCollation() collations.ID { - return vc.CollationEnv().DefaultConnectionCharset() + return vc.Environment().CollationEnv().DefaultConnectionCharset() } -func (vc *loggingVCursor) CollationEnv() *collations.Environment { - return collations.MySQL8() +func (vc *loggingVCursor) Environment() *vtenv.Environment { + return vtenv.NewTestEnv() } type bv struct { diff --git a/go/vt/vtgate/vindexes/lookup_test.go b/go/vt/vtgate/vindexes/lookup_test.go index 736017f89eb..8041a395a8e 100644 --- a/go/vt/vtgate/vindexes/lookup_test.go +++ b/go/vt/vtgate/vindexes/lookup_test.go @@ -35,6 +35,7 @@ import ( topodatapb "vitess.io/vitess/go/vt/proto/topodata" vtgatepb "vitess.io/vitess/go/vt/proto/vtgate" vtrpcpb "vitess.io/vitess/go/vt/proto/vtrpc" + "vitess.io/vitess/go/vt/vtenv" "vitess.io/vitess/go/vt/vterrors" ) @@ -115,11 +116,11 @@ func (vc *vcursor) execute(query string, bindvars map[string]*querypb.BindVariab } func (vc *vcursor) ConnCollation() collations.ID { - return vc.CollationEnv().DefaultConnectionCharset() + return vc.Environment().CollationEnv().DefaultConnectionCharset() } -func (vc *vcursor) CollationEnv() *collations.Environment { - return collations.MySQL8() +func (vc *vcursor) Environment() *vtenv.Environment { + return vtenv.NewTestEnv() } func lookupCreateVindexTestCase( diff --git a/go/vt/vtgate/vindexes/vindex.go b/go/vt/vtgate/vindexes/vindex.go index e9e346c7b89..e3d5a6d7e4d 100644 --- a/go/vt/vtgate/vindexes/vindex.go +++ b/go/vt/vtgate/vindexes/vindex.go @@ -25,6 +25,7 @@ import ( "vitess.io/vitess/go/sqltypes" "vitess.io/vitess/go/vt/key" "vitess.io/vitess/go/vt/sqlparser" + "vitess.io/vitess/go/vt/vtenv" "vitess.io/vitess/go/vt/vterrors" querypb "vitess.io/vitess/go/vt/proto/query" @@ -44,7 +45,7 @@ type ( InTransactionAndIsDML() bool LookupRowLockShardSession() vtgatepb.CommitOrder ConnCollation() collations.ID - CollationEnv() *collations.Environment + Environment() *vtenv.Environment } // Vindex defines the interface required to register a vindex. diff --git a/go/vt/vtgate/vtgate.go b/go/vt/vtgate/vtgate.go index 2fb0fdd6685..0a28220a796 100644 --- a/go/vt/vtgate/vtgate.go +++ b/go/vt/vtgate/vtgate.go @@ -30,7 +30,7 @@ import ( "github.com/spf13/pflag" - "vitess.io/vitess/go/mysql/collations" + "vitess.io/vitess/go/vt/vtenv" "vitess.io/vitess/go/acl" "vitess.io/vitess/go/sqltypes" @@ -243,13 +243,12 @@ var RegisterVTGates []RegisterVTGate // Init initializes VTGate server. func Init( ctx context.Context, + env *vtenv.Environment, hc discovery.HealthCheck, serv srvtopo.Server, cell string, tabletTypesToWait []topodatapb.TabletType, pv plancontext.PlannerVersion, - collationEnv *collations.Environment, - mysqlServerVersion string, ) *VTGate { // Build objects from low to high level. // Start with the gateway. If we can't reach the topology service, @@ -300,19 +299,10 @@ func Init( log.Fatal("Failed to create a new sidecar database identifier cache during init as one already existed!") } - parser, err := sqlparser.New(sqlparser.Options{ - MySQLServerVersion: mysqlServerVersion, - TruncateUILen: servenv.TruncateUILen, - TruncateErrLen: servenv.TruncateErrLen, - }) - if err != nil { - log.Fatalf("unable to initialize sql parser: %v", err) - } - var si SchemaInfo // default nil var st *vtschema.Tracker if enableSchemaChangeSignal { - st = vtschema.NewTracker(gw.hc.Subscribe(), enableViews, parser) + st = vtschema.NewTracker(gw.hc.Subscribe(), enableViews, env.Parser()) addKeyspacesToTracker(ctx, srvResolver, st, gw) si = st } @@ -321,6 +311,7 @@ func Init( executor := NewExecutor( ctx, + env, serv, cell, resolver, @@ -332,9 +323,6 @@ func Init( noScatter, pv, warmingReadsPercent, - collationEnv, - parser, - mysqlServerVersion, ) if err := executor.defaultQueryLogger(); err != nil { diff --git a/go/vt/vttablet/endtoend/framework/server.go b/go/vt/vttablet/endtoend/framework/server.go index f50f125eb6b..0258dee9186 100644 --- a/go/vt/vttablet/endtoend/framework/server.go +++ b/go/vt/vttablet/endtoend/framework/server.go @@ -23,12 +23,10 @@ import ( "net/http" "time" - "vitess.io/vitess/go/mysql/collations" - "vitess.io/vitess/go/mysql/config" "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/vtenv" "vitess.io/vitess/go/yaml2" "vitess.io/vitess/go/vt/topo/memorytopo" @@ -80,7 +78,7 @@ func StartCustomServer(ctx context.Context, connParams, connAppDebugParams mysql } TopoServer = memorytopo.NewServer(ctx, "") - Server = tabletserver.NewTabletServer(ctx, "", cfg, TopoServer, &topodatapb.TabletAlias{}, collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion) + Server = tabletserver.NewTabletServer(ctx, vtenv.NewTestEnv(), "", cfg, TopoServer, &topodatapb.TabletAlias{}) Server.Register() err := Server.StartService(Target, dbcfgs, nil /* mysqld */) if err != nil { diff --git a/go/vt/vttablet/onlineddl/analysis.go b/go/vt/vttablet/onlineddl/analysis.go index 3749eb272b4..1dc073bb7d0 100644 --- a/go/vt/vttablet/onlineddl/analysis.go +++ b/go/vt/vttablet/onlineddl/analysis.go @@ -75,7 +75,7 @@ func (e *Executor) getCreateTableStatement(ctx context.Context, tableName string if err != nil { return nil, vterrors.Wrapf(err, "in Executor.getCreateTableStatement()") } - stmt, err := e.env.SQLParser().ParseStrictDDL(showCreateTable) + stmt, err := e.env.Environment().Parser().ParseStrictDDL(showCreateTable) if err != nil { return nil, err } @@ -192,7 +192,7 @@ func analyzeInstantDDL(alterTable *sqlparser.AlterTable, createTable *sqlparser. // analyzeSpecialAlterPlan checks if the given ALTER onlineDDL, and for the current state of affected table, // can be executed in a special way. If so, it returns with a "special plan" func (e *Executor) analyzeSpecialAlterPlan(ctx context.Context, onlineDDL *schema.OnlineDDL, capableOf capabilities.CapableOf) (*SpecialAlterPlan, error) { - ddlStmt, _, err := schema.ParseOnlineDDLStatement(onlineDDL.SQL, e.env.SQLParser()) + ddlStmt, _, err := schema.ParseOnlineDDLStatement(onlineDDL.SQL, e.env.Environment().Parser()) if err != nil { return nil, err } diff --git a/go/vt/vttablet/onlineddl/executor.go b/go/vt/vttablet/onlineddl/executor.go index 053c9ccf158..e868f243be1 100644 --- a/go/vt/vttablet/onlineddl/executor.go +++ b/go/vt/vttablet/onlineddl/executor.go @@ -304,7 +304,7 @@ func (e *Executor) executeQueryWithSidecarDBReplacement(ctx context.Context, que defer conn.Recycle() // Replace any provided sidecar DB qualifiers with the correct one. - uq, err := e.env.SQLParser().ReplaceTableQualifiers(query, sidecar.DefaultName, sidecar.GetName()) + uq, err := e.env.Environment().Parser().ReplaceTableQualifiers(query, sidecar.DefaultName, sidecar.GetName()) if err != nil { return nil, err } @@ -413,7 +413,7 @@ func (e *Executor) allowConcurrentMigration(onlineDDL *schema.OnlineDDL) (action } var err error - action, err = onlineDDL.GetAction(e.env.SQLParser()) + action, err = onlineDDL.GetAction(e.env.Environment().Parser()) if err != nil { return action, false } @@ -800,7 +800,7 @@ func (e *Executor) killTableLockHoldersAndAccessors(ctx context.Context, tableNa for _, row := range rs.Named().Rows { threadId := row.AsInt64("id", 0) infoQuery := row.AsString("info", "") - stmt, err := e.env.SQLParser().Parse(infoQuery) + stmt, err := e.env.Environment().Parser().Parse(infoQuery) if err != nil { log.Error(vterrors.Errorf(vtrpcpb.Code_INVALID_ARGUMENT, "unable to parse processlist Info query: %v", infoQuery)) continue @@ -1381,7 +1381,7 @@ func (e *Executor) duplicateCreateTable(ctx context.Context, onlineDDL *schema.O constraintMap map[string]string, err error, ) { - stmt, err := e.env.SQLParser().ParseStrictDDL(originalShowCreateTable) + stmt, err := e.env.Environment().Parser().ParseStrictDDL(originalShowCreateTable) if err != nil { return nil, nil, nil, err } @@ -1447,7 +1447,7 @@ func (e *Executor) initVreplicationOriginalMigration(ctx context.Context, online return nil, err } - stmt, err := e.env.SQLParser().ParseStrictDDL(onlineDDL.SQL) + stmt, err := e.env.Environment().Parser().ParseStrictDDL(onlineDDL.SQL) if err != nil { return nil, err } @@ -1474,7 +1474,7 @@ func (e *Executor) initVreplicationOriginalMigration(ctx context.Context, online return v, err } - v = NewVRepl(onlineDDL.UUID, e.keyspace, e.shard, e.dbName, onlineDDL.Table, vreplTableName, originalShowCreateTable, vreplShowCreateTable, onlineDDL.SQL, onlineDDL.StrategySetting().IsAnalyzeTableFlag(), e.env.CollationEnv(), e.env.SQLParser(), e.env.MySQLVersion()) + v = NewVRepl(e.env.Environment(), onlineDDL.UUID, e.keyspace, e.shard, e.dbName, onlineDDL.Table, vreplTableName, originalShowCreateTable, vreplShowCreateTable, onlineDDL.SQL, onlineDDL.StrategySetting().IsAnalyzeTableFlag()) return v, nil } @@ -1528,7 +1528,7 @@ func (e *Executor) initVreplicationRevertMigration(ctx context.Context, onlineDD if err := e.updateArtifacts(ctx, onlineDDL.UUID, vreplTableName); err != nil { return v, err } - v = NewVRepl(onlineDDL.UUID, e.keyspace, e.shard, e.dbName, onlineDDL.Table, vreplTableName, "", "", "", false, e.env.CollationEnv(), e.env.SQLParser(), e.env.MySQLVersion()) + v = NewVRepl(e.env.Environment(), onlineDDL.UUID, e.keyspace, e.shard, e.dbName, onlineDDL.Table, vreplTableName, "", "", "", false) v.pos = revertStream.pos return v, nil } @@ -2398,7 +2398,7 @@ func (e *Executor) reviewEmptyTableRevertMigrations(ctx context.Context, onlineD // Try to update table name and ddl_action // Failure to do so fails the migration - revertUUID, err := onlineDDL.GetRevertUUID(e.env.SQLParser()) + revertUUID, err := onlineDDL.GetRevertUUID(e.env.Environment().Parser()) if err != nil { return false, e.failMigration(ctx, onlineDDL, fmt.Errorf("cannot analyze revert UUID for revert migration %s: %v", onlineDDL.UUID, err)) } @@ -2559,7 +2559,7 @@ func (e *Executor) reviewQueuedMigrations(ctx context.Context) error { func (e *Executor) validateMigrationRevertible(ctx context.Context, revertMigration *schema.OnlineDDL, revertingMigrationUUID string) (err error) { // Validation: migration to revert exists and is in complete state - action, actionStr, err := revertMigration.GetActionStr(e.env.SQLParser()) + action, actionStr, err := revertMigration.GetActionStr(e.env.Environment().Parser()) if err != nil { return err } @@ -2628,7 +2628,7 @@ func (e *Executor) validateMigrationRevertible(ctx context.Context, revertMigrat // - what type of migration we're reverting? (CREATE/DROP/ALTER) // - revert appropriately to the type of migration func (e *Executor) executeRevert(ctx context.Context, onlineDDL *schema.OnlineDDL) (err error) { - revertUUID, err := onlineDDL.GetRevertUUID(e.env.SQLParser()) + revertUUID, err := onlineDDL.GetRevertUUID(e.env.Environment().Parser()) if err != nil { return fmt.Errorf("cannot run a revert migration %v: %+v", onlineDDL.UUID, err) } @@ -2741,7 +2741,7 @@ func (e *Executor) executeRevert(ctx context.Context, onlineDDL *schema.OnlineDD func (e *Executor) evaluateDeclarativeDiff(ctx context.Context, onlineDDL *schema.OnlineDDL) (diff schemadiff.EntityDiff, err error) { // Modify the CREATE TABLE statement to indicate a different, made up table name, known as the "comparison table" - ddlStmt, _, err := schema.ParseOnlineDDLStatement(onlineDDL.SQL, e.env.SQLParser()) + ddlStmt, _, err := schema.ParseOnlineDDLStatement(onlineDDL.SQL, e.env.Environment().Parser()) if err != nil { return nil, err } @@ -2795,7 +2795,7 @@ func (e *Executor) evaluateDeclarativeDiff(ctx context.Context, onlineDDL *schem if newShowCreateTable == "" { return nil, vterrors.Errorf(vtrpcpb.Code_INTERNAL, "unexpected: cannot find table or view even as it was just created: %v", onlineDDL.Table) } - senv := schemadiff.NewEnv(e.env.CollationEnv(), e.env.CollationEnv().DefaultConnectionCharset(), e.env.SQLParser(), e.env.MySQLVersion()) + senv := schemadiff.NewEnv(e.env.Environment(), e.env.Environment().CollationEnv().DefaultConnectionCharset()) hints := &schemadiff.DiffHints{AutoIncrementStrategy: schemadiff.AutoIncrementApplyHigher} switch ddlStmt.(type) { case *sqlparser.CreateTable: @@ -2862,7 +2862,7 @@ func (e *Executor) analyzeDropDDLActionMigration(ctx context.Context, onlineDDL } } } - stmt, err := e.env.SQLParser().ParseStrictDDL(originalShowCreateTable) + stmt, err := e.env.Environment().Parser().ParseStrictDDL(originalShowCreateTable) if err != nil { return err } @@ -2908,7 +2908,7 @@ func (e *Executor) executeDropDDLActionMigration(ctx context.Context, onlineDDL // We transform a DROP TABLE into a RENAME TABLE statement, so as to remove the table safely and asynchronously. - ddlStmt, _, err := schema.ParseOnlineDDLStatement(onlineDDL.SQL, e.env.SQLParser()) + ddlStmt, _, err := schema.ParseOnlineDDLStatement(onlineDDL.SQL, e.env.Environment().Parser()) if err != nil { return failMigration(err) } @@ -2951,7 +2951,7 @@ func (e *Executor) executeCreateDDLActionMigration(ctx context.Context, onlineDD e.migrationMutex.Lock() defer e.migrationMutex.Unlock() - ddlStmt, _, err := schema.ParseOnlineDDLStatement(onlineDDL.SQL, e.env.SQLParser()) + ddlStmt, _, err := schema.ParseOnlineDDLStatement(onlineDDL.SQL, e.env.Environment().Parser()) if err != nil { return failMigration(err) } @@ -3038,7 +3038,7 @@ func (e *Executor) executeAlterViewOnline(ctx context.Context, onlineDDL *schema if err != nil { return err } - stmt, _, err := schema.ParseOnlineDDLStatement(onlineDDL.SQL, e.env.SQLParser()) + stmt, _, err := schema.ParseOnlineDDLStatement(onlineDDL.SQL, e.env.Environment().Parser()) if err != nil { return err } @@ -3197,7 +3197,7 @@ func (e *Executor) executeAlterDDLActionMigration(ctx context.Context, onlineDDL failMigration := func(err error) error { return e.failMigration(ctx, onlineDDL, err) } - ddlStmt, _, err := schema.ParseOnlineDDLStatement(onlineDDL.SQL, e.env.SQLParser()) + ddlStmt, _, err := schema.ParseOnlineDDLStatement(onlineDDL.SQL, e.env.Environment().Parser()) if err != nil { return failMigration(err) } @@ -3270,7 +3270,7 @@ func (e *Executor) executeMigration(ctx context.Context, onlineDDL *schema.Onlin return e.failMigration(ctx, onlineDDL, err) } - ddlAction, err := onlineDDL.GetAction(e.env.SQLParser()) + ddlAction, err := onlineDDL.GetAction(e.env.Environment().Parser()) if err != nil { return failMigration(err) } @@ -3304,7 +3304,7 @@ func (e *Executor) executeMigration(ctx context.Context, onlineDDL *schema.Onlin // - Implicitly do nothing, if the table does not exist { // Sanity: reject IF NOT EXISTS statements, because they don't make sense (or are ambiguous) in declarative mode - ddlStmt, _, err := schema.ParseOnlineDDLStatement(onlineDDL.SQL, e.env.SQLParser()) + ddlStmt, _, err := schema.ParseOnlineDDLStatement(onlineDDL.SQL, e.env.Environment().Parser()) if err != nil { return failMigration(err) } @@ -3331,7 +3331,7 @@ func (e *Executor) executeMigration(ctx context.Context, onlineDDL *schema.Onlin // - Implicitly do nothing, if the table exists and is identical to CREATE statement // Sanity: reject IF NOT EXISTS statements, because they don't make sense (or are ambiguous) in declarative mode - ddlStmt, _, err := schema.ParseOnlineDDLStatement(onlineDDL.SQL, e.env.SQLParser()) + ddlStmt, _, err := schema.ParseOnlineDDLStatement(onlineDDL.SQL, e.env.Environment().Parser()) if err != nil { return failMigration(err) } @@ -3472,7 +3472,7 @@ func (e *Executor) runNextMigration(ctx context.Context) error { } { // We strip out any VT query comments because our simplified parser doesn't work well with comments - ddlStmt, _, err := schema.ParseOnlineDDLStatement(onlineDDL.SQL, e.env.SQLParser()) + ddlStmt, _, err := schema.ParseOnlineDDLStatement(onlineDDL.SQL, e.env.Environment().Parser()) if err == nil { ddlStmt.SetComments(sqlparser.Comments{}) onlineDDL.SQL = sqlparser.String(ddlStmt) @@ -4870,7 +4870,7 @@ func (e *Executor) submittedMigrationConflictsWithPendingMigrationInSingletonCon return false } // Let's see if the pending migration is a revert: - if _, err := pendingOnlineDDL.GetRevertUUID(e.env.SQLParser()); err != nil { + if _, err := pendingOnlineDDL.GetRevertUUID(e.env.Environment().Parser()); err != nil { // Not a revert. So the pending migration definitely conflicts with our migration. return true } @@ -5005,13 +5005,13 @@ func (e *Executor) SubmitMigration( // OK, this is a new UUID - _, actionStr, err := onlineDDL.GetActionStr(e.env.SQLParser()) + _, actionStr, err := onlineDDL.GetActionStr(e.env.Environment().Parser()) if err != nil { return nil, err } log.Infof("SubmitMigration: request to submit migration %s; action=%s, table=%s", onlineDDL.UUID, actionStr, onlineDDL.Table) - revertedUUID, _ := onlineDDL.GetRevertUUID(e.env.SQLParser()) // Empty value if the migration is not actually a REVERT. Safe to ignore error. + revertedUUID, _ := onlineDDL.GetRevertUUID(e.env.Environment().Parser()) // Empty value if the migration is not actually a REVERT. Safe to ignore error. retainArtifactsSeconds := int64((retainOnlineDDLTables).Seconds()) if retainArtifacts, _ := onlineDDL.StrategySetting().RetainArtifactsDuration(); retainArtifacts != 0 { // Explicit retention indicated by `--retain-artifact` DDL strategy flag for this migration. Override! @@ -5037,7 +5037,7 @@ func (e *Executor) SubmitMigration( sqltypes.BoolBindVariable(onlineDDL.StrategySetting().IsPostponeCompletion()), sqltypes.BoolBindVariable(allowConcurrentMigration), sqltypes.StringBindVariable(revertedUUID), - sqltypes.BoolBindVariable(onlineDDL.IsView(e.env.SQLParser())), + sqltypes.BoolBindVariable(onlineDDL.IsView(e.env.Environment().Parser())), ) if err != nil { return nil, err diff --git a/go/vt/vttablet/onlineddl/executor_test.go b/go/vt/vttablet/onlineddl/executor_test.go index d392a59f613..c6fc0044c91 100644 --- a/go/vt/vttablet/onlineddl/executor_test.go +++ b/go/vt/vttablet/onlineddl/executor_test.go @@ -28,8 +28,7 @@ import ( "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" - "vitess.io/vitess/go/mysql/collations" - "vitess.io/vitess/go/mysql/config" + "vitess.io/vitess/go/vt/vtenv" "vitess.io/vitess/go/vt/vttablet/tabletserver/tabletenv" "vitess.io/vitess/go/vt/schema" @@ -49,7 +48,7 @@ func TestGetConstraintType(t *testing.T) { func TestValidateAndEditCreateTableStatement(t *testing.T) { e := Executor{ - env: tabletenv.NewEnv(nil, "ValidateAndEditCreateTableStatementTest", collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion), + env: tabletenv.NewEnv(vtenv.NewTestEnv(), nil, "ValidateAndEditCreateTableStatementTest"), } tt := []struct { name string @@ -162,7 +161,7 @@ func TestValidateAndEditCreateTableStatement(t *testing.T) { } for _, tc := range tt { t.Run(tc.name, func(t *testing.T) { - stmt, err := e.env.SQLParser().ParseStrictDDL(tc.query) + stmt, err := e.env.Environment().Parser().ParseStrictDDL(tc.query) require.NoError(t, err) createTable, ok := stmt.(*sqlparser.CreateTable) require.True(t, ok) @@ -193,7 +192,7 @@ func TestValidateAndEditCreateTableStatement(t *testing.T) { func TestValidateAndEditAlterTableStatement(t *testing.T) { e := Executor{ - env: tabletenv.NewEnv(nil, "TestValidateAndEditAlterTableStatementTest", collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion), + env: tabletenv.NewEnv(vtenv.NewTestEnv(), nil, "TestValidateAndEditAlterTableStatementTest"), } tt := []struct { alter string @@ -264,7 +263,7 @@ func TestValidateAndEditAlterTableStatement(t *testing.T) { } for _, tc := range tt { t.Run(tc.alter, func(t *testing.T) { - stmt, err := e.env.SQLParser().ParseStrictDDL(tc.alter) + stmt, err := e.env.Environment().Parser().ParseStrictDDL(tc.alter) require.NoError(t, err) alterTable, ok := stmt.(*sqlparser.AlterTable) require.True(t, ok) @@ -287,7 +286,7 @@ func TestValidateAndEditAlterTableStatement(t *testing.T) { func TestAddInstantAlgorithm(t *testing.T) { e := Executor{ - env: tabletenv.NewEnv(nil, "AddInstantAlgorithmTest", collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion), + env: tabletenv.NewEnv(vtenv.NewTestEnv(), nil, "AddInstantAlgorithmTest"), } tt := []struct { alter string @@ -312,7 +311,7 @@ func TestAddInstantAlgorithm(t *testing.T) { } for _, tc := range tt { t.Run(tc.alter, func(t *testing.T) { - stmt, err := e.env.SQLParser().ParseStrictDDL(tc.alter) + stmt, err := e.env.Environment().Parser().ParseStrictDDL(tc.alter) require.NoError(t, err) alterTable, ok := stmt.(*sqlparser.AlterTable) require.True(t, ok) @@ -322,7 +321,7 @@ func TestAddInstantAlgorithm(t *testing.T) { assert.Equal(t, tc.expect, alterInstant) - stmt, err = e.env.SQLParser().ParseStrictDDL(alterInstant) + stmt, err = e.env.Environment().Parser().ParseStrictDDL(alterInstant) require.NoError(t, err) _, ok = stmt.(*sqlparser.AlterTable) require.True(t, ok) @@ -332,7 +331,7 @@ func TestAddInstantAlgorithm(t *testing.T) { func TestDuplicateCreateTable(t *testing.T) { e := Executor{ - env: tabletenv.NewEnv(nil, "DuplicateCreateTableTest", collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion), + env: tabletenv.NewEnv(vtenv.NewTestEnv(), nil, "DuplicateCreateTableTest"), } ctx := context.Background() onlineDDL := &schema.OnlineDDL{UUID: "a5a563da_dc1a_11ec_a416_0a43f95f28a3", Table: "something", Strategy: "vitess", Options: "--unsafe-allow-foreign-keys"} diff --git a/go/vt/vttablet/onlineddl/vrepl.go b/go/vt/vttablet/onlineddl/vrepl.go index 0c473185cce..ef38fb7d012 100644 --- a/go/vt/vttablet/onlineddl/vrepl.go +++ b/go/vt/vttablet/onlineddl/vrepl.go @@ -40,6 +40,7 @@ import ( "vitess.io/vitess/go/vt/log" "vitess.io/vitess/go/vt/schema" "vitess.io/vitess/go/vt/sqlparser" + "vitess.io/vitess/go/vt/vtenv" "vitess.io/vitess/go/vt/vterrors" "vitess.io/vitess/go/vt/vttablet/onlineddl/vrepl" "vitess.io/vitess/go/vt/vttablet/tabletmanager/vreplication" @@ -137,13 +138,13 @@ type VRepl struct { convertCharset map[string](*binlogdatapb.CharsetConversion) - collationEnv *collations.Environment - sqlparser *sqlparser.Parser - mysqlVersion string + env *vtenv.Environment } // NewVRepl creates a VReplication handler for Online DDL -func NewVRepl(workflow string, +func NewVRepl( + env *vtenv.Environment, + workflow string, keyspace string, shard string, dbName string, @@ -153,11 +154,9 @@ func NewVRepl(workflow string, vreplShowCreateTable string, alterQuery string, analyzeTable bool, - collationEnv *collations.Environment, - parser *sqlparser.Parser, - mysqlVersion string, ) *VRepl { return &VRepl{ + env: env, workflow: workflow, keyspace: keyspace, shard: shard, @@ -172,9 +171,6 @@ func NewVRepl(workflow string, enumToTextMap: map[string]string{}, intToEnumMap: map[string]bool{}, convertCharset: map[string](*binlogdatapb.CharsetConversion){}, - collationEnv: collationEnv, - sqlparser: parser, - mysqlVersion: mysqlVersion, } } @@ -394,7 +390,7 @@ func (v *VRepl) analyzeAlter(ctx context.Context) error { // Happens for REVERT return nil } - if err := v.parser.ParseAlterStatement(v.alterQuery, v.sqlparser); err != nil { + if err := v.parser.ParseAlterStatement(v.alterQuery, v.env.Parser()); err != nil { return err } if v.parser.IsRenameTable() { @@ -465,7 +461,7 @@ func (v *VRepl) analyzeTables(ctx context.Context, conn *dbconnpool.DBConnection } v.addedUniqueKeys = vrepl.AddedUniqueKeys(sourceUniqueKeys, targetUniqueKeys, v.parser.ColumnRenameMap()) v.removedUniqueKeys = vrepl.RemovedUniqueKeys(sourceUniqueKeys, targetUniqueKeys, v.parser.ColumnRenameMap()) - v.removedForeignKeyNames, err = vrepl.RemovedForeignKeyNames(v.sqlparser, v.collationEnv, v.mysqlVersion, v.originalShowCreateTable, v.vreplShowCreateTable) + v.removedForeignKeyNames, err = vrepl.RemovedForeignKeyNames(v.env, v.originalShowCreateTable, v.vreplShowCreateTable) if err != nil { return err } @@ -563,11 +559,11 @@ func (v *VRepl) generateFilterQuery(ctx context.Context) error { case sourceCol.Type == vrepl.StringColumnType: // Check source and target charset/encoding. If needed, create // a binlogdatapb.CharsetConversion entry (later written to vreplication) - fromCollation := v.collationEnv.DefaultCollationForCharset(sourceCol.Charset) + fromCollation := v.env.CollationEnv().DefaultCollationForCharset(sourceCol.Charset) if fromCollation == collations.Unknown { return vterrors.Errorf(vtrpcpb.Code_INVALID_ARGUMENT, "Character set %s not supported for column %s", sourceCol.Charset, sourceCol.Name) } - toCollation := v.collationEnv.DefaultCollationForCharset(targetCol.Charset) + toCollation := v.env.CollationEnv().DefaultCollationForCharset(targetCol.Charset) // Let's see if target col is at all textual if targetCol.Type == vrepl.StringColumnType && toCollation == collations.Unknown { return vterrors.Errorf(vtrpcpb.Code_INVALID_ARGUMENT, "Character set %s not supported for column %s", targetCol.Charset, targetCol.Name) diff --git a/go/vt/vttablet/onlineddl/vrepl/foreign_key.go b/go/vt/vttablet/onlineddl/vrepl/foreign_key.go index 8e277fa0336..343df2a8a80 100644 --- a/go/vt/vttablet/onlineddl/vrepl/foreign_key.go +++ b/go/vt/vttablet/onlineddl/vrepl/foreign_key.go @@ -21,23 +21,21 @@ limitations under the License. package vrepl import ( - "vitess.io/vitess/go/mysql/collations" "vitess.io/vitess/go/vt/schemadiff" "vitess.io/vitess/go/vt/sqlparser" + "vitess.io/vitess/go/vt/vtenv" ) // RemovedForeignKeyNames returns the names of removed foreign keys, ignoring mere name changes func RemovedForeignKeyNames( - parser *sqlparser.Parser, - collEnv *collations.Environment, - mysqlVersion string, + venv *vtenv.Environment, originalCreateTable string, vreplCreateTable string, ) (names []string, err error) { if originalCreateTable == "" || vreplCreateTable == "" { return nil, nil } - env := schemadiff.NewEnv(collEnv, collEnv.DefaultConnectionCharset(), parser, mysqlVersion) + env := schemadiff.NewEnv(venv, venv.CollationEnv().DefaultConnectionCharset()) diffHints := schemadiff.DiffHints{ConstraintNamesStrategy: schemadiff.ConstraintNamesIgnoreAll} diff, err := schemadiff.DiffCreateTablesQueries(env, originalCreateTable, vreplCreateTable, &diffHints) if err != nil { diff --git a/go/vt/vttablet/onlineddl/vrepl/foreign_key_test.go b/go/vt/vttablet/onlineddl/vrepl/foreign_key_test.go index 520e4b1d739..95b2c84e66e 100644 --- a/go/vt/vttablet/onlineddl/vrepl/foreign_key_test.go +++ b/go/vt/vttablet/onlineddl/vrepl/foreign_key_test.go @@ -25,9 +25,7 @@ import ( "github.com/stretchr/testify/assert" - "vitess.io/vitess/go/mysql/collations" - "vitess.io/vitess/go/mysql/config" - "vitess.io/vitess/go/vt/sqlparser" + "vitess.io/vitess/go/vt/vtenv" ) func TestRemovedForeignKeyNames(t *testing.T) { @@ -70,7 +68,7 @@ func TestRemovedForeignKeyNames(t *testing.T) { } for _, tcase := range tcases { t.Run(tcase.before, func(t *testing.T) { - names, err := RemovedForeignKeyNames(sqlparser.NewTestParser(), collations.MySQL8(), config.DefaultMySQLVersion, tcase.before, tcase.after) + names, err := RemovedForeignKeyNames(vtenv.NewTestEnv(), tcase.before, tcase.after) assert.NoError(t, err) assert.Equal(t, tcase.names, names) }) diff --git a/go/vt/vttablet/tabletmanager/restore.go b/go/vt/vttablet/tabletmanager/restore.go index dbc8f66a316..6b37edb5244 100644 --- a/go/vt/vttablet/tabletmanager/restore.go +++ b/go/vt/vttablet/tabletmanager/restore.go @@ -426,7 +426,7 @@ func (tm *TabletManager) getGTIDFromTimestamp(ctx context.Context, pos replicati Port: connParams.Port, } dbCfgs.SetDbParams(*connParams, *connParams, *connParams) - vsClient := vreplication.NewReplicaConnector(connParams, tm.CollationEnv, tm.SQLParser, tm.MySQLVersion) + vsClient := vreplication.NewReplicaConnector(tm.Env, connParams) filter := &binlogdatapb.Filter{ Rules: []*binlogdatapb.Rule{{ diff --git a/go/vt/vttablet/tabletmanager/rpc_query.go b/go/vt/vttablet/tabletmanager/rpc_query.go index e80909ab7cf..b4c84afec91 100644 --- a/go/vt/vttablet/tabletmanager/rpc_query.go +++ b/go/vt/vttablet/tabletmanager/rpc_query.go @@ -94,7 +94,7 @@ func (tm *TabletManager) ExecuteFetchAsDba(ctx context.Context, req *tabletmanag _, _ = conn.ExecuteFetch("USE "+sqlescape.EscapeID(req.DbName), 1, false) } - statements, _, countCreate, allowZeroInDate, err := analyzeExecuteFetchAsDbaMultiQuery(string(req.Query), tm.SQLParser) + statements, _, countCreate, allowZeroInDate, err := analyzeExecuteFetchAsDbaMultiQuery(string(req.Query), tm.Env.Parser()) if err != nil { return nil, err } @@ -115,7 +115,7 @@ func (tm *TabletManager) ExecuteFetchAsDba(ctx context.Context, req *tabletmanag // Replace any provided sidecar database qualifiers with the correct one. // TODO(shlomi): we use ReplaceTableQualifiersMultiQuery for backwards compatibility. In v20 we will not accept // multi statement queries in ExecuteFetchAsDBA. This will be rewritten as ReplaceTableQualifiers() - uq, err := tm.SQLParser.ReplaceTableQualifiersMultiQuery(string(req.Query), sidecar.DefaultName, sidecar.GetName()) + uq, err := tm.Env.Parser().ReplaceTableQualifiersMultiQuery(string(req.Query), sidecar.DefaultName, sidecar.GetName()) if err != nil { return nil, err } @@ -168,7 +168,7 @@ func (tm *TabletManager) ExecuteFetchAsAllPrivs(ctx context.Context, req *tablet } // Replace any provided sidecar database qualifiers with the correct one. - uq, err := tm.SQLParser.ReplaceTableQualifiers(string(req.Query), sidecar.DefaultName, sidecar.GetName()) + uq, err := tm.Env.Parser().ReplaceTableQualifiers(string(req.Query), sidecar.DefaultName, sidecar.GetName()) if err != nil { return nil, err } @@ -195,7 +195,7 @@ func (tm *TabletManager) ExecuteFetchAsApp(ctx context.Context, req *tabletmanag } defer conn.Recycle() // Replace any provided sidecar database qualifiers with the correct one. - uq, err := tm.SQLParser.ReplaceTableQualifiers(string(req.Query), sidecar.DefaultName, sidecar.GetName()) + uq, err := tm.Env.Parser().ReplaceTableQualifiers(string(req.Query), sidecar.DefaultName, sidecar.GetName()) if err != nil { return nil, err } @@ -212,7 +212,7 @@ func (tm *TabletManager) ExecuteQuery(ctx context.Context, req *tabletmanagerdat tablet := tm.Tablet() target := &querypb.Target{Keyspace: tablet.Keyspace, Shard: tablet.Shard, TabletType: tablet.Type} // Replace any provided sidecar database qualifiers with the correct one. - uq, err := tm.SQLParser.ReplaceTableQualifiers(string(req.Query), sidecar.DefaultName, sidecar.GetName()) + uq, err := tm.Env.Parser().ReplaceTableQualifiers(string(req.Query), sidecar.DefaultName, sidecar.GetName()) if err != nil { return nil, err } diff --git a/go/vt/vttablet/tabletmanager/rpc_query_test.go b/go/vt/vttablet/tabletmanager/rpc_query_test.go index d03eb4acbaa..e30f63b362b 100644 --- a/go/vt/vttablet/tabletmanager/rpc_query_test.go +++ b/go/vt/vttablet/tabletmanager/rpc_query_test.go @@ -30,6 +30,7 @@ import ( "vitess.io/vitess/go/vt/dbconfigs" "vitess.io/vitess/go/vt/mysqlctl" "vitess.io/vitess/go/vt/sqlparser" + "vitess.io/vitess/go/vt/vtenv" "vitess.io/vitess/go/vt/vttablet/tabletservermock" tabletmanagerdatapb "vitess.io/vitess/go/vt/proto/tabletmanagerdata" @@ -126,6 +127,7 @@ func TestTabletManager_ExecuteFetchAsDba(t *testing.T) { DBConfigs: dbconfigs.NewTestDBConfigs(cp, cp, dbName), QueryServiceControl: tabletservermock.NewController(), _waitForGrantsComplete: make(chan struct{}), + Env: vtenv.NewTestEnv(), } close(tm._waitForGrantsComplete) diff --git a/go/vt/vttablet/tabletmanager/rpc_vreplication.go b/go/vt/vttablet/tabletmanager/rpc_vreplication.go index 60881b4eab3..0319146321a 100644 --- a/go/vt/vttablet/tabletmanager/rpc_vreplication.go +++ b/go/vt/vttablet/tabletmanager/rpc_vreplication.go @@ -329,7 +329,7 @@ func (tm *TabletManager) UpdateVReplicationWorkflow(ctx context.Context, req *ta // VReplicationExec executes a vreplication command. func (tm *TabletManager) VReplicationExec(ctx context.Context, query string) (*querypb.QueryResult, error) { // Replace any provided sidecar database qualifiers with the correct one. - uq, err := tm.SQLParser.ReplaceTableQualifiers(query, sidecar.DefaultName, sidecar.GetName()) + uq, err := tm.Env.Parser().ReplaceTableQualifiers(query, sidecar.DefaultName, sidecar.GetName()) if err != nil { return nil, err } diff --git a/go/vt/vttablet/tabletmanager/rpc_vreplication_test.go b/go/vt/vttablet/tabletmanager/rpc_vreplication_test.go index f92d04220f9..5ef9b4cd8c6 100644 --- a/go/vt/vttablet/tabletmanager/rpc_vreplication_test.go +++ b/go/vt/vttablet/tabletmanager/rpc_vreplication_test.go @@ -25,8 +25,7 @@ import ( "strings" "testing" - "vitess.io/vitess/go/mysql/collations" - "vitess.io/vitess/go/mysql/config" + "vitess.io/vitess/go/vt/vtenv" "vitess.io/vitess/go/vt/vttablet" "github.com/stretchr/testify/require" @@ -113,7 +112,7 @@ func TestCreateVReplicationWorkflow(t *testing.T) { targetTablet := tenv.addTablet(t, targetTabletUID, targetKs, shard) defer tenv.deleteTablet(targetTablet.tablet) - ws := workflow.NewServer(tenv.ts, tenv.tmc, collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion) + ws := workflow.NewServer(vtenv.NewTestEnv(), tenv.ts, tenv.tmc) tests := []struct { name string @@ -270,7 +269,7 @@ func TestMoveTables(t *testing.T) { }, }) - ws := workflow.NewServer(tenv.ts, tenv.tmc, collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion) + ws := workflow.NewServer(vtenv.NewTestEnv(), tenv.ts, tenv.tmc) tenv.mysqld.Schema = defaultSchema tenv.mysqld.Schema.DatabaseSchema = tenv.dbName @@ -658,7 +657,7 @@ func TestSourceShardSelection(t *testing.T) { defer tenv.deleteTablet(tt.tablet) } - ws := workflow.NewServer(tenv.ts, tenv.tmc, collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion) + ws := workflow.NewServer(vtenv.NewTestEnv(), tenv.ts, tenv.tmc) tenv.ts.SaveVSchema(ctx, sourceKs, &vschemapb.Keyspace{ Sharded: true, @@ -857,7 +856,7 @@ func TestFailedMoveTablesCreateCleanup(t *testing.T) { sourceKs, shard, table, table) tenv := newTestEnv(t, ctx, sourceKs, []string{shard}) defer tenv.close() - ws := workflow.NewServer(tenv.ts, tenv.tmc, collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion) + ws := workflow.NewServer(vtenv.NewTestEnv(), tenv.ts, tenv.tmc) sourceTablet := tenv.addTablet(t, sourceTabletUID, sourceKs, shard) defer tenv.deleteTablet(sourceTablet.tablet) diff --git a/go/vt/vttablet/tabletmanager/tm_init.go b/go/vt/vttablet/tabletmanager/tm_init.go index 21ed6315fa7..c4ad332a470 100644 --- a/go/vt/vttablet/tabletmanager/tm_init.go +++ b/go/vt/vttablet/tabletmanager/tm_init.go @@ -36,6 +36,7 @@ package tabletmanager import ( "context" "encoding/hex" + "errors" "fmt" "math/rand" "regexp" @@ -63,11 +64,11 @@ import ( querypb "vitess.io/vitess/go/vt/proto/query" topodatapb "vitess.io/vitess/go/vt/proto/topodata" "vitess.io/vitess/go/vt/servenv" - "vitess.io/vitess/go/vt/sqlparser" "vitess.io/vitess/go/vt/topo" "vitess.io/vitess/go/vt/topo/topoproto" "vitess.io/vitess/go/vt/topotools" "vitess.io/vitess/go/vt/vtctl/reparentutil" + "vitess.io/vitess/go/vt/vtenv" "vitess.io/vitess/go/vt/vterrors" "vitess.io/vitess/go/vt/vttablet/tabletmanager/vdiff" "vitess.io/vitess/go/vt/vttablet/tabletmanager/vreplication" @@ -91,7 +92,6 @@ var ( skipBuildInfoTags = "/.*/" initTags flagutil.StringMapValue - initPopulateMetadata bool initTimeout = 1 * time.Minute mysqlShutdownTimeout = 5 * time.Minute ) @@ -156,9 +156,7 @@ type TabletManager struct { UpdateStream binlog.UpdateStreamControl VREngine *vreplication.Engine VDiffEngine *vdiff.Engine - CollationEnv *collations.Environment - SQLParser *sqlparser.Parser - MySQLVersion string + Env *vtenv.Environment // tmState manages the TabletManager state. tmState *tmState @@ -369,7 +367,7 @@ func (tm *TabletManager) Start(tablet *topodatapb.Tablet, config *tabletenv.Tabl if err := tm.checkPrimaryShip(ctx, si); err != nil { return err } - if err := tm.checkMysql(ctx); err != nil { + if err := tm.checkMysql(); err != nil { return err } if err := tm.initTablet(ctx); err != nil { @@ -468,7 +466,7 @@ func (tm *TabletManager) Close() { // Stop shuts down the tm. Normally this is not necessary, since we use // servenv OnTerm and OnClose hooks to coordinate shutdown automatically, // while taking lameduck into account. However, this may be useful for tests, -// when you want to clean up an tm immediately. +// when you want to clean up a tm immediately. func (tm *TabletManager) Stop() { // Stop the shard sync loop and wait for it to exit. This needs to be done // here in addition to in Close() because tests do not call Close(). @@ -705,7 +703,7 @@ func (tm *TabletManager) checkPrimaryShip(ctx context.Context, si *topo.ShardInf return nil } -func (tm *TabletManager) checkMysql(ctx context.Context) error { +func (tm *TabletManager) checkMysql() error { appConfig, err := tm.DBConfigs.AppWithDB().MysqlParams() if err != nil { return err @@ -902,7 +900,7 @@ func (tm *TabletManager) withRetry(ctx context.Context, description string, work backoff := 1 * time.Second for { err := work() - if err == nil || err == context.Canceled || err == context.DeadlineExceeded { + if err == nil || errors.Is(err, context.Canceled) || errors.Is(err, context.DeadlineExceeded) { return err } diff --git a/go/vt/vttablet/tabletmanager/vreplication/engine.go b/go/vt/vttablet/tabletmanager/vreplication/engine.go index 74488799b43..2b2937056ac 100644 --- a/go/vt/vttablet/tabletmanager/vreplication/engine.go +++ b/go/vt/vttablet/tabletmanager/vreplication/engine.go @@ -28,16 +28,14 @@ import ( "time" "vitess.io/vitess/go/constants/sidecar" - "vitess.io/vitess/go/mysql/collations" - "vitess.io/vitess/go/mysql/config" "vitess.io/vitess/go/mysql/sqlerror" "vitess.io/vitess/go/sqltypes" "vitess.io/vitess/go/vt/binlog/binlogplayer" "vitess.io/vitess/go/vt/dbconfigs" "vitess.io/vitess/go/vt/log" "vitess.io/vitess/go/vt/mysqlctl" - "vitess.io/vitess/go/vt/sqlparser" "vitess.io/vitess/go/vt/topo" + "vitess.io/vitess/go/vt/vtenv" "vitess.io/vitess/go/vt/vterrors" "vitess.io/vitess/go/vt/vttablet/tabletserver/tabletenv" "vitess.io/vitess/go/vt/vttablet/tabletserver/throttle" @@ -114,9 +112,7 @@ type Engine struct { // production. shortcircuit bool - collationEnv *collations.Environment - parser *sqlparser.Parser - mysqlVersion string + env *vtenv.Environment } type journalEvent struct { @@ -133,18 +129,16 @@ type PostCopyAction struct { // NewEngine creates a new Engine. // A nil ts means that the Engine is disabled. -func NewEngine(config *tabletenv.TabletConfig, ts *topo.Server, cell string, mysqld mysqlctl.MysqlDaemon, lagThrottler *throttle.Throttler, collationEnv *collations.Environment, parser *sqlparser.Parser, mysqlVersion string) *Engine { +func NewEngine(env *vtenv.Environment, config *tabletenv.TabletConfig, ts *topo.Server, cell string, mysqld mysqlctl.MysqlDaemon, lagThrottler *throttle.Throttler) *Engine { vre := &Engine{ + env: env, controllers: make(map[int32]*controller), ts: ts, cell: cell, mysqld: mysqld, journaler: make(map[string]*journalEvent), - ec: newExternalConnector(config.ExternalConnections, collationEnv, parser, mysqlVersion), + ec: newExternalConnector(env, config.ExternalConnections), throttlerClient: throttle.NewBackgroundClient(lagThrottler, throttlerapp.VReplicationName, throttle.ThrottleCheckPrimaryWrite), - collationEnv: collationEnv, - parser: parser, - mysqlVersion: mysqlVersion, } return vre @@ -157,17 +151,19 @@ func (vre *Engine) InitDBConfig(dbcfgs *dbconfigs.DBConfigs) { return } vre.dbClientFactoryFiltered = func() binlogplayer.DBClient { - return binlogplayer.NewDBClient(dbcfgs.FilteredWithDB(), vre.parser) + return binlogplayer.NewDBClient(dbcfgs.FilteredWithDB(), vre.env.Parser()) } vre.dbClientFactoryDba = func() binlogplayer.DBClient { - return binlogplayer.NewDBClient(dbcfgs.DbaWithDB(), vre.parser) + return binlogplayer.NewDBClient(dbcfgs.DbaWithDB(), vre.env.Parser()) } vre.dbName = dbcfgs.DBName } // NewTestEngine creates a new Engine for testing. func NewTestEngine(ts *topo.Server, cell string, mysqld mysqlctl.MysqlDaemon, dbClientFactoryFiltered func() binlogplayer.DBClient, dbClientFactoryDba func() binlogplayer.DBClient, dbname string, externalConfig map[string]*dbconfigs.DBConfigs) *Engine { + env := vtenv.NewTestEnv() vre := &Engine{ + env: env, controllers: make(map[int32]*controller), ts: ts, cell: cell, @@ -176,10 +172,7 @@ func NewTestEngine(ts *topo.Server, cell string, mysqld mysqlctl.MysqlDaemon, db dbClientFactoryDba: dbClientFactoryDba, dbName: dbname, journaler: make(map[string]*journalEvent), - ec: newExternalConnector(externalConfig, collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion), - collationEnv: collations.MySQL8(), - parser: sqlparser.NewTestParser(), - mysqlVersion: config.DefaultMySQLVersion, + ec: newExternalConnector(env, externalConfig), } return vre } @@ -187,7 +180,9 @@ func NewTestEngine(ts *topo.Server, cell string, mysqld mysqlctl.MysqlDaemon, db // NewSimpleTestEngine creates a new Engine for testing that can // also short circuit functions as needed. func NewSimpleTestEngine(ts *topo.Server, cell string, mysqld mysqlctl.MysqlDaemon, dbClientFactoryFiltered func() binlogplayer.DBClient, dbClientFactoryDba func() binlogplayer.DBClient, dbname string, externalConfig map[string]*dbconfigs.DBConfigs) *Engine { + env := vtenv.NewTestEnv() vre := &Engine{ + env: env, controllers: make(map[int32]*controller), ts: ts, cell: cell, @@ -196,11 +191,8 @@ func NewSimpleTestEngine(ts *topo.Server, cell string, mysqld mysqlctl.MysqlDaem dbClientFactoryDba: dbClientFactoryDba, dbName: dbname, journaler: make(map[string]*journalEvent), - ec: newExternalConnector(externalConfig, collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion), + ec: newExternalConnector(env, externalConfig), shortcircuit: true, - collationEnv: collations.MySQL8(), - parser: sqlparser.NewTestParser(), - mysqlVersion: config.DefaultMySQLVersion, } return vre } @@ -377,7 +369,7 @@ func (vre *Engine) exec(query string, runAsAdmin bool) (*sqltypes.Result, error) } defer vre.updateStats() - plan, err := buildControllerPlan(query, vre.parser) + plan, err := buildControllerPlan(query, vre.env.Parser()) if err != nil { return nil, err } diff --git a/go/vt/vttablet/tabletmanager/vreplication/external_connector.go b/go/vt/vttablet/tabletmanager/vreplication/external_connector.go index 96ba9e64761..873bf498c14 100644 --- a/go/vt/vttablet/tabletmanager/vreplication/external_connector.go +++ b/go/vt/vttablet/tabletmanager/vreplication/external_connector.go @@ -20,7 +20,6 @@ import ( "context" "sync" - "vitess.io/vitess/go/mysql/collations" "vitess.io/vitess/go/sqltypes" "vitess.io/vitess/go/vt/dbconfigs" "vitess.io/vitess/go/vt/grpcclient" @@ -28,7 +27,7 @@ import ( querypb "vitess.io/vitess/go/vt/proto/query" topodatapb "vitess.io/vitess/go/vt/proto/topodata" vtrpcpb "vitess.io/vitess/go/vt/proto/vtrpc" - "vitess.io/vitess/go/vt/sqlparser" + "vitess.io/vitess/go/vt/vtenv" "vitess.io/vitess/go/vt/vterrors" "vitess.io/vitess/go/vt/vttablet/queryservice" "vitess.io/vitess/go/vt/vttablet/tabletconn" @@ -59,21 +58,17 @@ type VStreamerClient interface { } type externalConnector struct { - mu sync.Mutex - dbconfigs map[string]*dbconfigs.DBConfigs - connectors map[string]*mysqlConnector - collationEnv *collations.Environment - parser *sqlparser.Parser - mysqlVersion string + env *vtenv.Environment + mu sync.Mutex + dbconfigs map[string]*dbconfigs.DBConfigs + connectors map[string]*mysqlConnector } -func newExternalConnector(dbcfgs map[string]*dbconfigs.DBConfigs, collationEnv *collations.Environment, parser *sqlparser.Parser, mysqlVersion string) *externalConnector { +func newExternalConnector(env *vtenv.Environment, dbcfgs map[string]*dbconfigs.DBConfigs) *externalConnector { return &externalConnector{ - dbconfigs: dbcfgs, - connectors: make(map[string]*mysqlConnector), - collationEnv: collationEnv, - parser: parser, - mysqlVersion: mysqlVersion, + env: env, + dbconfigs: dbcfgs, + connectors: make(map[string]*mysqlConnector), } } @@ -98,7 +93,7 @@ func (ec *externalConnector) Get(name string) (*mysqlConnector, error) { return nil, vterrors.Errorf(vtrpcpb.Code_NOT_FOUND, "external mysqlConnector %v not found", name) } c := &mysqlConnector{} - c.env = tabletenv.NewEnv(config, name, ec.collationEnv, ec.parser, ec.mysqlVersion) + c.env = tabletenv.NewEnv(ec.env, config, name) c.se = schema.NewEngine(c.env) c.vstreamer = vstreamer.NewEngine(c.env, nil, c.se, nil, "") c.vstreamer.InitDBConfig("", "") diff --git a/go/vt/vttablet/tabletmanager/vreplication/replica_connector.go b/go/vt/vttablet/tabletmanager/vreplication/replica_connector.go index d4d4950b66d..c3cd073f0bf 100644 --- a/go/vt/vttablet/tabletmanager/vreplication/replica_connector.go +++ b/go/vt/vttablet/tabletmanager/vreplication/replica_connector.go @@ -18,9 +18,8 @@ package vreplication import ( "vitess.io/vitess/go/mysql" - "vitess.io/vitess/go/mysql/collations" "vitess.io/vitess/go/vt/dbconfigs" - "vitess.io/vitess/go/vt/sqlparser" + "vitess.io/vitess/go/vt/vtenv" "vitess.io/vitess/go/vt/vttablet/tabletserver/tabletenv" "vitess.io/vitess/go/vt/vttablet/tabletserver/throttle/throttlerapp" @@ -36,8 +35,7 @@ import ( // This is used by binlog server to make vstream connection // using the vstream connection, it will parse the events from binglog // to fetch the corresponding GTID for required recovery time -func NewReplicaConnector(connParams *mysql.ConnParams, collationEnv *collations.Environment, parser *sqlparser.Parser, mysqlVersion string) *ReplicaConnector { - +func NewReplicaConnector(venv *vtenv.Environment, connParams *mysql.ConnParams) *ReplicaConnector { // Construct config := tabletenv.NewDefaultConfig() dbCfg := &dbconfigs.DBConfigs{ @@ -47,7 +45,7 @@ func NewReplicaConnector(connParams *mysql.ConnParams, collationEnv *collations. dbCfg.SetDbParams(*connParams, *connParams, *connParams) config.DB = dbCfg c := &ReplicaConnector{conn: connParams} - env := tabletenv.NewEnv(config, "source", collationEnv, parser, mysqlVersion) + env := tabletenv.NewEnv(venv, config, "source") c.se = schema.NewEngine(env) c.se.SkipMetaCheck = true c.vstreamer = vstreamer.NewEngine(env, nil, c.se, nil, "") diff --git a/go/vt/vttablet/tabletmanager/vreplication/vcopier.go b/go/vt/vttablet/tabletmanager/vreplication/vcopier.go index 3f4a5f2710e..4779e607960 100644 --- a/go/vt/vttablet/tabletmanager/vreplication/vcopier.go +++ b/go/vt/vttablet/tabletmanager/vreplication/vcopier.go @@ -219,7 +219,7 @@ func newVCopierCopyWorker( func (vc *vcopier) initTablesForCopy(ctx context.Context) error { defer vc.vr.dbClient.Rollback() - plan, err := buildReplicatorPlan(vc.vr.source, vc.vr.colInfoMap, nil, vc.vr.stats, vc.vr.vre.collationEnv, vc.vr.vre.parser) + plan, err := buildReplicatorPlan(vc.vr.source, vc.vr.colInfoMap, nil, vc.vr.stats, vc.vr.vre.env.CollationEnv(), vc.vr.vre.env.Parser()) if err != nil { return err } @@ -385,7 +385,7 @@ func (vc *vcopier) copyTable(ctx context.Context, tableName string, copyState ma log.Infof("Copying table %s, lastpk: %v", tableName, copyState[tableName]) - plan, err := buildReplicatorPlan(vc.vr.source, vc.vr.colInfoMap, nil, vc.vr.stats, vc.vr.vre.collationEnv, vc.vr.vre.parser) + plan, err := buildReplicatorPlan(vc.vr.source, vc.vr.colInfoMap, nil, vc.vr.stats, vc.vr.vre.env.CollationEnv(), vc.vr.vre.env.Parser()) if err != nil { return err } diff --git a/go/vt/vttablet/tabletmanager/vreplication/vcopier_atomic.go b/go/vt/vttablet/tabletmanager/vreplication/vcopier_atomic.go index d0adc970382..02e1188cdb7 100644 --- a/go/vt/vttablet/tabletmanager/vreplication/vcopier_atomic.go +++ b/go/vt/vttablet/tabletmanager/vreplication/vcopier_atomic.go @@ -54,7 +54,7 @@ func newCopyAllState(vc *vcopier) (*copyAllState, error) { state := ©AllState{ vc: vc, } - plan, err := buildReplicatorPlan(vc.vr.source, vc.vr.colInfoMap, nil, vc.vr.stats, vc.vr.vre.collationEnv, vc.vr.vre.parser) + plan, err := buildReplicatorPlan(vc.vr.source, vc.vr.colInfoMap, nil, vc.vr.stats, vc.vr.vre.env.CollationEnv(), vc.vr.vre.env.Parser()) if err != nil { return nil, err } diff --git a/go/vt/vttablet/tabletmanager/vreplication/vplayer.go b/go/vt/vttablet/tabletmanager/vreplication/vplayer.go index f1265a1dd68..f2cb0a96e71 100644 --- a/go/vt/vttablet/tabletmanager/vreplication/vplayer.go +++ b/go/vt/vttablet/tabletmanager/vreplication/vplayer.go @@ -180,7 +180,7 @@ func (vp *vplayer) play(ctx context.Context) error { return nil } - plan, err := buildReplicatorPlan(vp.vr.source, vp.vr.colInfoMap, vp.copyState, vp.vr.stats, vp.vr.vre.collationEnv, vp.vr.vre.parser) + plan, err := buildReplicatorPlan(vp.vr.source, vp.vr.colInfoMap, vp.copyState, vp.vr.stats, vp.vr.vre.env.CollationEnv(), vp.vr.vre.env.Parser()) if err != nil { vp.vr.stats.ErrorCounts.Add([]string{"Plan"}, 1) return err diff --git a/go/vt/vttablet/tabletmanager/vreplication/vreplicator.go b/go/vt/vttablet/tabletmanager/vreplication/vreplicator.go index 7c1237ca7ab..11633d95f33 100644 --- a/go/vt/vttablet/tabletmanager/vreplication/vreplicator.go +++ b/go/vt/vttablet/tabletmanager/vreplication/vreplicator.go @@ -736,7 +736,7 @@ func (vr *vreplicator) getTableSecondaryKeys(ctx context.Context, tableName stri } tableSchema := schema.TableDefinitions[0].Schema var secondaryKeys []*sqlparser.IndexDefinition - parsedDDL, err := vr.vre.parser.ParseStrictDDL(tableSchema) + parsedDDL, err := vr.vre.env.Parser().ParseStrictDDL(tableSchema) if err != nil { return secondaryKeys, err } @@ -983,7 +983,7 @@ func (vr *vreplicator) execPostCopyActions(ctx context.Context, tableName string // the table schema and if so move forward and delete the // post_copy_action record. if sqlErr, ok := err.(*sqlerror.SQLError); ok && sqlErr.Number() == sqlerror.ERDupKeyName { - stmt, err := vr.vre.parser.ParseStrictDDL(action.Task) + stmt, err := vr.vre.env.Parser().ParseStrictDDL(action.Task) if err != nil { return failedAlterErr } diff --git a/go/vt/vttablet/tabletserver/connpool/dbconn.go b/go/vt/vttablet/tabletserver/connpool/dbconn.go index 7876cdf00db..bedbdc66c0a 100644 --- a/go/vt/vttablet/tabletserver/connpool/dbconn.go +++ b/go/vt/vttablet/tabletserver/connpool/dbconn.go @@ -484,9 +484,9 @@ func (dbc *Conn) CurrentForLogging() string { if dbc.env != nil && dbc.env.Config() != nil && !dbc.env.Config().SanitizeLogMessages { queryToLog = dbc.Current() } else { - queryToLog, _ = dbc.env.SQLParser().RedactSQLQuery(dbc.Current()) + queryToLog, _ = dbc.env.Environment().Parser().RedactSQLQuery(dbc.Current()) } - return dbc.env.SQLParser().TruncateForLog(queryToLog) + return dbc.env.Environment().Parser().TruncateForLog(queryToLog) } func (dbc *Conn) applySameSetting(ctx context.Context) (err error) { diff --git a/go/vt/vttablet/tabletserver/connpool/dbconn_test.go b/go/vt/vttablet/tabletserver/connpool/dbconn_test.go index 76c4d32079a..d22b8f1c311 100644 --- a/go/vt/vttablet/tabletserver/connpool/dbconn_test.go +++ b/go/vt/vttablet/tabletserver/connpool/dbconn_test.go @@ -27,15 +27,13 @@ import ( "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" - "vitess.io/vitess/go/mysql/collations" - "vitess.io/vitess/go/mysql/config" "vitess.io/vitess/go/mysql/fakesqldb" "vitess.io/vitess/go/mysql/sqlerror" "vitess.io/vitess/go/pools/smartconnpool" "vitess.io/vitess/go/sqltypes" "vitess.io/vitess/go/vt/dbconfigs" querypb "vitess.io/vitess/go/vt/proto/query" - "vitess.io/vitess/go/vt/sqlparser" + "vitess.io/vitess/go/vt/vtenv" "vitess.io/vitess/go/vt/vttablet/tabletserver/tabletenv" ) @@ -334,7 +332,7 @@ func TestDBNoPoolConnKill(t *testing.T) { params := dbconfigs.New(db.ConnParams()) connPool.Open(params, params, params) defer connPool.Close() - dbConn, err := NewConn(context.Background(), params, connPool.dbaPool, nil, tabletenv.NewEnv(nil, "TestDBNoPoolConnKill", collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion)) + dbConn, err := NewConn(context.Background(), params, connPool.dbaPool, nil, tabletenv.NewEnv(vtenv.NewTestEnv(), nil, "TestDBNoPoolConnKill")) if dbConn != nil { defer dbConn.Close() } diff --git a/go/vt/vttablet/tabletserver/connpool/pool_test.go b/go/vt/vttablet/tabletserver/connpool/pool_test.go index 1a418bfeafd..28f3e27803a 100644 --- a/go/vt/vttablet/tabletserver/connpool/pool_test.go +++ b/go/vt/vttablet/tabletserver/connpool/pool_test.go @@ -24,14 +24,12 @@ import ( "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" - "vitess.io/vitess/go/mysql/collations" - "vitess.io/vitess/go/mysql/config" "vitess.io/vitess/go/mysql/fakesqldb" "vitess.io/vitess/go/pools/smartconnpool" "vitess.io/vitess/go/sqltypes" "vitess.io/vitess/go/vt/callerid" "vitess.io/vitess/go/vt/dbconfigs" - "vitess.io/vitess/go/vt/sqlparser" + "vitess.io/vitess/go/vt/vtenv" "vitess.io/vitess/go/vt/vttablet/tabletserver/tabletenv" ) @@ -61,7 +59,7 @@ func TestConnPoolTimeout(t *testing.T) { } cfg.Timeout = time.Second cfg.IdleTimeout = 10 * time.Second - connPool := NewPool(tabletenv.NewEnv(nil, "PoolTest", collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion), "TestPool", cfg) + connPool := NewPool(tabletenv.NewEnv(vtenv.NewTestEnv(), nil, "PoolTest"), "TestPool", cfg) params := dbconfigs.New(db.ConnParams()) connPool.Open(params, params, params) defer connPool.Close() @@ -338,7 +336,7 @@ func newPool() *Pool { } func newPoolWithCapacity(capacity int) *Pool { - return NewPool(tabletenv.NewEnv(nil, "PoolTest", collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion), "TestPool", tabletenv.ConnPoolConfig{ + return NewPool(tabletenv.NewEnv(vtenv.NewTestEnv(), nil, "PoolTest"), "TestPool", tabletenv.ConnPoolConfig{ Size: capacity, IdleTimeout: 10 * time.Second, }) diff --git a/go/vt/vttablet/tabletserver/exclude_race_test.go b/go/vt/vttablet/tabletserver/exclude_race_test.go index ee4364968c3..5fa66caf6ef 100644 --- a/go/vt/vttablet/tabletserver/exclude_race_test.go +++ b/go/vt/vttablet/tabletserver/exclude_race_test.go @@ -13,7 +13,7 @@ import ( "vitess.io/vitess/go/mysql/config" "vitess.io/vitess/go/sqltypes" querypb "vitess.io/vitess/go/vt/proto/query" - "vitess.io/vitess/go/vt/sqlparser" + "vitess.io/vitess/go/vt/vtenv" "vitess.io/vitess/go/vt/vttablet/tabletserver/tabletenv" ) @@ -27,13 +27,13 @@ func TestHandlePanicAndSendLogStatsMessageTruncation(t *testing.T) { tl := newTestLogger() defer tl.Close() logStats := tabletenv.NewLogStats(ctx, "TestHandlePanicAndSendLogStatsMessageTruncation") - parser, err := sqlparser.New(sqlparser.Options{ + env, err := vtenv.New(vtenv.Options{ MySQLServerVersion: config.DefaultMySQLVersion, TruncateErrLen: 32, }) require.NoError(t, err) - db, tsv := setupTabletServerTestCustom(t, ctx, tabletenv.NewDefaultConfig(), "", parser) + db, tsv := setupTabletServerTestCustom(t, ctx, tabletenv.NewDefaultConfig(), "", env) defer tsv.StopService() defer db.Close() diff --git a/go/vt/vttablet/tabletserver/health_streamer_test.go b/go/vt/vttablet/tabletserver/health_streamer_test.go index bf985cc643e..14a1899d07b 100644 --- a/go/vt/vttablet/tabletserver/health_streamer_test.go +++ b/go/vt/vttablet/tabletserver/health_streamer_test.go @@ -30,14 +30,12 @@ import ( "google.golang.org/protobuf/proto" "vitess.io/vitess/go/mysql" - "vitess.io/vitess/go/mysql/collations" - "vitess.io/vitess/go/mysql/config" "vitess.io/vitess/go/mysql/fakesqldb" "vitess.io/vitess/go/sqltypes" "vitess.io/vitess/go/vt/dbconfigs" querypb "vitess.io/vitess/go/vt/proto/query" topodatapb "vitess.io/vitess/go/vt/proto/topodata" - "vitess.io/vitess/go/vt/sqlparser" + "vitess.io/vitess/go/vt/vtenv" "vitess.io/vitess/go/vt/vttablet/tabletserver/schema" "vitess.io/vitess/go/vt/vttablet/tabletserver/tabletenv" ) @@ -46,7 +44,7 @@ func TestHealthStreamerClosed(t *testing.T) { db := fakesqldb.New(t) defer db.Close() cfg := newConfig(db) - env := tabletenv.NewEnv(cfg, "ReplTrackerTest", collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion) + env := tabletenv.NewEnv(vtenv.NewTestEnv(), cfg, "ReplTrackerTest") alias := &topodatapb.TabletAlias{ Cell: "cell", Uid: 1, @@ -73,7 +71,7 @@ func TestNotServingPrimaryNoWrite(t *testing.T) { cfg := newConfig(db) cfg.SignalWhenSchemaChange = true - env := tabletenv.NewEnv(cfg, "TestNotServingPrimary", collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion) + env := tabletenv.NewEnv(vtenv.NewTestEnv(), cfg, "TestNotServingPrimary") alias := &topodatapb.TabletAlias{ Cell: "cell", Uid: 1, @@ -104,7 +102,7 @@ func TestHealthStreamerBroadcast(t *testing.T) { cfg := newConfig(db) cfg.SignalWhenSchemaChange = false - env := tabletenv.NewEnv(cfg, "ReplTrackerTest", collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion) + env := tabletenv.NewEnv(vtenv.NewTestEnv(), cfg, "ReplTrackerTest") alias := &topodatapb.TabletAlias{ Cell: "cell", Uid: 1, @@ -219,7 +217,7 @@ func TestReloadSchema(t *testing.T) { cfg.SignalWhenSchemaChange = testcase.enableSchemaChange cfg.SchemaReloadInterval = 100 * time.Millisecond - env := tabletenv.NewEnv(cfg, "ReplTrackerTest", collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion) + env := tabletenv.NewEnv(vtenv.NewTestEnv(), cfg, "ReplTrackerTest") alias := &topodatapb.TabletAlias{ Cell: "cell", Uid: 1, @@ -335,7 +333,7 @@ func TestReloadView(t *testing.T) { cfg.SchemaReloadInterval = 100 * time.Millisecond cfg.EnableViews = true - env := tabletenv.NewEnv(cfg, "TestReloadView", collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion) + env := tabletenv.NewEnv(vtenv.NewTestEnv(), cfg, "TestReloadView") alias := &topodatapb.TabletAlias{Cell: "cell", Uid: 1} se := schema.NewEngine(env) hs := newHealthStreamer(env, alias, se) diff --git a/go/vt/vttablet/tabletserver/messager/engine_test.go b/go/vt/vttablet/tabletserver/messager/engine_test.go index bee868aaf5d..eda585694f1 100644 --- a/go/vt/vttablet/tabletserver/messager/engine_test.go +++ b/go/vt/vttablet/tabletserver/messager/engine_test.go @@ -21,11 +21,10 @@ import ( "reflect" "testing" - "vitess.io/vitess/go/mysql/collations" - "vitess.io/vitess/go/mysql/config" "vitess.io/vitess/go/sqltypes" vtrpcpb "vitess.io/vitess/go/vt/proto/vtrpc" "vitess.io/vitess/go/vt/sqlparser" + "vitess.io/vitess/go/vt/vtenv" "vitess.io/vitess/go/vt/vterrors" "vitess.io/vitess/go/vt/vttablet/tabletserver/schema" "vitess.io/vitess/go/vt/vttablet/tabletserver/tabletenv" @@ -155,7 +154,7 @@ func TestEngineGenerate(t *testing.T) { func newTestEngine() *Engine { cfg := tabletenv.NewDefaultConfig() tsv := &fakeTabletServer{ - Env: tabletenv.NewEnv(cfg, "MessagerTest", collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion), + Env: tabletenv.NewEnv(vtenv.NewTestEnv(), cfg, "MessagerTest"), } se := schema.NewEngine(tsv) te := NewEngine(tsv, se, newFakeVStreamer()) diff --git a/go/vt/vttablet/tabletserver/messager/message_manager_test.go b/go/vt/vttablet/tabletserver/messager/message_manager_test.go index 7d7db4966fb..fdf39556e5c 100644 --- a/go/vt/vttablet/tabletserver/messager/message_manager_test.go +++ b/go/vt/vttablet/tabletserver/messager/message_manager_test.go @@ -31,11 +31,10 @@ import ( "github.com/stretchr/testify/assert" "golang.org/x/sync/semaphore" - "vitess.io/vitess/go/mysql/collations" - "vitess.io/vitess/go/mysql/config" "vitess.io/vitess/go/sqltypes" "vitess.io/vitess/go/test/utils" "vitess.io/vitess/go/vt/sqlparser" + "vitess.io/vitess/go/vt/vtenv" "vitess.io/vitess/go/vt/vttablet/tabletserver/schema" "vitess.io/vitess/go/vt/vttablet/tabletserver/tabletenv" "vitess.io/vitess/go/vt/vttablet/tabletserver/throttle/throttlerapp" @@ -835,7 +834,7 @@ type fakeTabletServer struct { func newFakeTabletServer() *fakeTabletServer { cfg := tabletenv.NewDefaultConfig() return &fakeTabletServer{ - Env: tabletenv.NewEnv(cfg, "MessagerTest", collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion), + Env: tabletenv.NewEnv(vtenv.NewTestEnv(), cfg, "MessagerTest"), } } diff --git a/go/vt/vttablet/tabletserver/planbuilder/builder.go b/go/vt/vttablet/tabletserver/planbuilder/builder.go index 0f0c9ac4a45..94f5fc1caa2 100644 --- a/go/vt/vttablet/tabletserver/planbuilder/builder.go +++ b/go/vt/vttablet/tabletserver/planbuilder/builder.go @@ -19,8 +19,8 @@ package planbuilder import ( "strings" - "vitess.io/vitess/go/mysql/collations" "vitess.io/vitess/go/vt/sqlparser" + "vitess.io/vitess/go/vt/vtenv" "vitess.io/vitess/go/vt/vterrors" "vitess.io/vitess/go/vt/vtgate/evalengine" "vitess.io/vitess/go/vt/vttablet/tabletserver/schema" @@ -28,7 +28,7 @@ import ( vtrpcpb "vitess.io/vitess/go/vt/proto/vtrpc" ) -func analyzeSelect(sel *sqlparser.Select, tables map[string]*schema.Table, collationEnv *collations.Environment, mysqlVersion string) (plan *Plan, err error) { +func analyzeSelect(env *vtenv.Environment, sel *sqlparser.Select, tables map[string]*schema.Table) (plan *Plan, err error) { plan = &Plan{ PlanID: PlanSelect, FullQuery: GenerateLimitQuery(sel), @@ -50,9 +50,8 @@ func analyzeSelect(sel *sqlparser.Select, tables map[string]*schema.Table, colla } plan.PlanID = PlanNextval v, err := evalengine.Translate(nextVal.Expr, &evalengine.Config{ - CollationEnv: collationEnv, - Collation: collationEnv.DefaultConnectionCharset(), - MySQLVersion: mysqlVersion, + Environment: env, + Collation: env.CollationEnv().DefaultConnectionCharset(), }) if err != nil { return nil, err diff --git a/go/vt/vttablet/tabletserver/planbuilder/plan.go b/go/vt/vttablet/tabletserver/planbuilder/plan.go index 660acdc796b..7b1e57c2f90 100644 --- a/go/vt/vttablet/tabletserver/planbuilder/plan.go +++ b/go/vt/vttablet/tabletserver/planbuilder/plan.go @@ -20,9 +20,9 @@ import ( "encoding/json" "strings" - "vitess.io/vitess/go/mysql/collations" "vitess.io/vitess/go/vt/sqlparser" "vitess.io/vitess/go/vt/tableacl" + "vitess.io/vitess/go/vt/vtenv" "vitess.io/vitess/go/vt/vterrors" "vitess.io/vitess/go/vt/vtgate/evalengine" "vitess.io/vitess/go/vt/vttablet/tabletserver/schema" @@ -202,7 +202,7 @@ func (plan *Plan) TableNames() (names []string) { } // Build builds a plan based on the schema. -func Build(statement sqlparser.Statement, tables map[string]*schema.Table, dbName string, viewsEnabled bool, collationEnv *collations.Environment, mysqlVersion string) (plan *Plan, err error) { +func Build(env *vtenv.Environment, statement sqlparser.Statement, tables map[string]*schema.Table, dbName string, viewsEnabled bool) (plan *Plan, err error) { switch stmt := statement.(type) { case *sqlparser.Union: plan, err = &Plan{ @@ -210,7 +210,7 @@ func Build(statement sqlparser.Statement, tables map[string]*schema.Table, dbNam FullQuery: GenerateLimitQuery(stmt), }, nil case *sqlparser.Select: - plan, err = analyzeSelect(stmt, tables, collationEnv, mysqlVersion) + plan, err = analyzeSelect(env, stmt, tables) case *sqlparser.Insert: plan, err = analyzeInsert(stmt, tables) case *sqlparser.Update: diff --git a/go/vt/vttablet/tabletserver/planbuilder/plan_test.go b/go/vt/vttablet/tabletserver/planbuilder/plan_test.go index 0276dde95d0..9569121cb8f 100644 --- a/go/vt/vttablet/tabletserver/planbuilder/plan_test.go +++ b/go/vt/vttablet/tabletserver/planbuilder/plan_test.go @@ -30,10 +30,9 @@ import ( "github.com/stretchr/testify/require" - "vitess.io/vitess/go/mysql/collations" - "vitess.io/vitess/go/mysql/config" "vitess.io/vitess/go/vt/sqlparser" "vitess.io/vitess/go/vt/tableacl" + "vitess.io/vitess/go/vt/vtenv" "vitess.io/vitess/go/vt/vttablet/tabletserver/schema" ) @@ -86,7 +85,7 @@ func testPlan(t *testing.T, fileName string) { var err error statement, err := parser.Parse(tcase.input) if err == nil { - plan, err = Build(statement, testSchema, "dbName", false, collations.MySQL8(), config.DefaultMySQLVersion) + plan, err = Build(vtenv.NewTestEnv(), statement, testSchema, "dbName", false) } PassthroughDMLs = false @@ -124,7 +123,7 @@ func TestPlanInReservedConn(t *testing.T) { var err error statement, err := parser.Parse(tcase.input) if err == nil { - plan, err = Build(statement, testSchema, "dbName", false, collations.MySQL8(), config.DefaultMySQLVersion) + plan, err = Build(vtenv.NewTestEnv(), statement, testSchema, "dbName", false) } PassthroughDMLs = false @@ -176,7 +175,7 @@ func TestCustom(t *testing.T) { if err != nil { t.Fatalf("Got error: %v, parsing sql: %v", err.Error(), tcase.input) } - plan, err := Build(statement, schem, "dbName", false, collations.MySQL8(), config.DefaultMySQLVersion) + plan, err := Build(vtenv.NewTestEnv(), statement, schem, "dbName", false) var out string if err != nil { out = err.Error() @@ -265,7 +264,7 @@ func TestLockPlan(t *testing.T) { var err error statement, err := parser.Parse(tcase.input) if err == nil { - plan, err = Build(statement, testSchema, "dbName", false, collations.MySQL8(), config.DefaultMySQLVersion) + plan, err = Build(vtenv.NewTestEnv(), statement, testSchema, "dbName", false) } var out string diff --git a/go/vt/vttablet/tabletserver/query_engine.go b/go/vt/vttablet/tabletserver/query_engine.go index abdca687c10..b87c5cf97ba 100644 --- a/go/vt/vttablet/tabletserver/query_engine.go +++ b/go/vt/vttablet/tabletserver/query_engine.go @@ -359,11 +359,11 @@ func (qe *QueryEngine) Close() { var errNoCache = errors.New("plan should not be cached") func (qe *QueryEngine) getPlan(curSchema *currentSchema, sql string) (*TabletPlan, error) { - statement, err := qe.env.SQLParser().Parse(sql) + statement, err := qe.env.Environment().Parser().Parse(sql) if err != nil { return nil, err } - splan, err := planbuilder.Build(statement, curSchema.tables, qe.env.Config().DB.DBName, qe.env.Config().EnableViews, qe.env.CollationEnv(), qe.env.MySQLVersion()) + splan, err := planbuilder.Build(qe.env.Environment(), statement, curSchema.tables, qe.env.Config().DB.DBName, qe.env.Config().EnableViews) if err != nil { return nil, err } @@ -402,7 +402,7 @@ func (qe *QueryEngine) GetPlan(ctx context.Context, logStats *tabletenv.LogStats } func (qe *QueryEngine) getStreamPlan(curSchema *currentSchema, sql string) (*TabletPlan, error) { - statement, err := qe.env.SQLParser().Parse(sql) + statement, err := qe.env.Environment().Parser().Parse(sql) if err != nil { return nil, err } @@ -479,7 +479,7 @@ func (qe *QueryEngine) GetConnSetting(ctx context.Context, settings []string) (* cacheKey := SettingsCacheKey(buf.String()) connSetting, _, err := qe.settings.GetOrLoad(cacheKey, 0, func() (*smartconnpool.Setting, error) { // build the setting queries - query, resetQuery, err := planbuilder.BuildSettingQuery(settings, qe.env.SQLParser()) + query, resetQuery, err := planbuilder.BuildSettingQuery(settings, qe.env.Environment().Parser()) if err != nil { return nil, err } @@ -609,7 +609,7 @@ func (qe *QueryEngine) handleHTTPQueryPlans(response http.ResponseWriter, reques response.Header().Set("Content-Type", "text/plain") qe.ForEachPlan(func(plan *TabletPlan) bool { - response.Write([]byte(fmt.Sprintf("%#v\n", qe.env.SQLParser().TruncateForUI(plan.Original)))) + response.Write([]byte(fmt.Sprintf("%#v\n", qe.env.Environment().Parser().TruncateForUI(plan.Original)))) if b, err := json.MarshalIndent(plan.Plan, "", " "); err != nil { response.Write([]byte(err.Error())) } else { @@ -629,7 +629,7 @@ func (qe *QueryEngine) handleHTTPQueryStats(response http.ResponseWriter, reques var qstats []perQueryStats qe.ForEachPlan(func(plan *TabletPlan) bool { var pqstats perQueryStats - pqstats.Query = unicoded(qe.env.SQLParser().TruncateForUI(plan.Original)) + pqstats.Query = unicoded(qe.env.Environment().Parser().TruncateForUI(plan.Original)) pqstats.Table = plan.TableName().String() pqstats.Plan = plan.PlanID pqstats.QueryCount, pqstats.Time, pqstats.MysqlTime, pqstats.RowsAffected, pqstats.RowsReturned, pqstats.ErrorCount = plan.Stats() @@ -697,7 +697,7 @@ func (qe *QueryEngine) handleHTTPConsolidations(response http.ResponseWriter, re for _, v := range items { var query string if streamlog.GetRedactDebugUIQueries() { - query, _ = qe.env.SQLParser().RedactSQLQuery(v.Query) + query, _ = qe.env.Environment().Parser().RedactSQLQuery(v.Query) } else { query = v.Query } diff --git a/go/vt/vttablet/tabletserver/query_engine_test.go b/go/vt/vttablet/tabletserver/query_engine_test.go index f5c56d93ea0..8dbe18ef13c 100644 --- a/go/vt/vttablet/tabletserver/query_engine_test.go +++ b/go/vt/vttablet/tabletserver/query_engine_test.go @@ -32,9 +32,8 @@ import ( "time" "vitess.io/vitess/go/cache/theine" - "vitess.io/vitess/go/mysql/collations" - "vitess.io/vitess/go/mysql/config" "vitess.io/vitess/go/vt/proto/topodata" + "vitess.io/vitess/go/vt/vtenv" "vitess.io/vitess/go/vt/sqlparser" @@ -64,7 +63,7 @@ func TestStrictMode(t *testing.T) { // Test default behavior. cfg := tabletenv.NewDefaultConfig() cfg.DB = newDBConfigs(db) - env := tabletenv.NewEnv(cfg, "TabletServerTest", collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion) + env := tabletenv.NewEnv(vtenv.NewTestEnv(), cfg, "TabletServerTest") se := schema.NewEngine(env) qe := NewQueryEngine(env, se) qe.se.InitDBConfig(newDBConfigs(db).DbaWithDB()) @@ -357,7 +356,7 @@ func newTestQueryEngine(idleTimeout time.Duration, strict bool, dbcfgs *dbconfig cfg.OltpReadPool.IdleTimeout = idleTimeout cfg.OlapReadPool.IdleTimeout = idleTimeout cfg.TxPool.IdleTimeout = idleTimeout - env := tabletenv.NewEnv(cfg, "TabletServerTest", collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion) + env := tabletenv.NewEnv(vtenv.NewTestEnv(), cfg, "TabletServerTest") se := schema.NewEngine(env) qe := NewQueryEngine(env, se) // the integration tests that check cache behavior do not expect a doorkeeper; disable it @@ -457,7 +456,7 @@ func benchmarkPlanCache(b *testing.B, db *fakesqldb.DB, par int) { cfg := tabletenv.NewDefaultConfig() cfg.DB = dbcfgs - env := tabletenv.NewEnv(cfg, "TabletServerTest", collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion) + env := tabletenv.NewEnv(vtenv.NewTestEnv(), cfg, "TabletServerTest") se := schema.NewEngine(env) qe := NewQueryEngine(env, se) @@ -515,7 +514,7 @@ func TestPlanCachePollution(t *testing.T) { cfg.DB = dbcfgs // config.LFUQueryCacheSizeBytes = 3 * 1024 * 1024 - env := tabletenv.NewEnv(cfg, "TabletServerTest", collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion) + env := tabletenv.NewEnv(vtenv.NewTestEnv(), cfg, "TabletServerTest") se := schema.NewEngine(env) qe := NewQueryEngine(env, se) @@ -831,7 +830,7 @@ func TestAddQueryStats(t *testing.T) { cfg := tabletenv.NewDefaultConfig() cfg.DB = newDBConfigs(fakesqldb.New(t)) cfg.EnablePerWorkloadTableMetrics = testcase.enablePerWorkloadTableMetrics - env := tabletenv.NewEnv(cfg, "TestAddQueryStats_"+testcase.name, collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion) + env := tabletenv.NewEnv(vtenv.NewTestEnv(), cfg, "TestAddQueryStats_"+testcase.name) se := schema.NewEngine(env) qe := NewQueryEngine(env, se) qe.AddStats(testcase.planType, testcase.tableName, testcase.workload, testcase.tabletType, testcase.queryCount, testcase.duration, testcase.mysqlTime, testcase.rowsAffected, testcase.rowsReturned, testcase.errorCount, testcase.errorCode) @@ -872,7 +871,7 @@ func TestPlanPoolUnsafe(t *testing.T) { t.Run(tcase.name, func(t *testing.T) { statement, err := sqlparser.NewTestParser().Parse(tcase.query) require.NoError(t, err) - plan, err := planbuilder.Build(statement, map[string]*schema.Table{}, "dbName", false, collations.MySQL8(), config.DefaultMySQLVersion) + plan, err := planbuilder.Build(vtenv.NewTestEnv(), statement, map[string]*schema.Table{}, "dbName", false) // Plan building will not fail, but it will mark that reserved connection is needed. // checking plan is valid will fail. require.NoError(t, err) diff --git a/go/vt/vttablet/tabletserver/query_executor.go b/go/vt/vttablet/tabletserver/query_executor.go index 8c3367d80e0..5b84defad16 100644 --- a/go/vt/vttablet/tabletserver/query_executor.go +++ b/go/vt/vttablet/tabletserver/query_executor.go @@ -614,13 +614,13 @@ func (*QueryExecutor) BeginAgain(ctx context.Context, dc *StatefulConnection) er } func (qre *QueryExecutor) execNextval() (*sqltypes.Result, error) { - env := evalengine.NewExpressionEnv(qre.ctx, qre.bindVars, evalengine.NewEmptyVCursor(qre.tsv.collationEnv, time.Local, qre.tsv.mysqlVersion)) + env := evalengine.NewExpressionEnv(qre.ctx, qre.bindVars, evalengine.NewEmptyVCursor(qre.tsv.Environment(), time.Local)) result, err := env.Evaluate(qre.plan.NextCount) if err != nil { return nil, err } tableName := qre.plan.TableName() - v := result.Value(qre.tsv.collationEnv.DefaultConnectionCharset()) + v := result.Value(qre.tsv.env.CollationEnv().DefaultConnectionCharset()) inc, err := v.ToInt64() if err != nil || inc < 1 { return nil, vterrors.Errorf(vtrpcpb.Code_INVALID_ARGUMENT, "invalid increment for sequence %s: %s", tableName, v.String()) @@ -757,7 +757,7 @@ func (qre *QueryExecutor) verifyRowCount(count, maxrows int64) error { if warnThreshold > 0 && count > warnThreshold { callerID := callerid.ImmediateCallerIDFromContext(qre.ctx) qre.tsv.Stats().Warnings.Add("ResultsExceeded", 1) - log.Warningf("caller id: %s row count %v exceeds warning threshold %v: %q", callerID.Username, count, warnThreshold, queryAsString(qre.plan.FullQuery.Query, qre.bindVars, qre.tsv.Config().SanitizeLogMessages, true, qre.tsv.SQLParser())) + log.Warningf("caller id: %s row count %v exceeds warning threshold %v: %q", callerID.Username, count, warnThreshold, queryAsString(qre.plan.FullQuery.Query, qre.bindVars, qre.tsv.Config().SanitizeLogMessages, true, qre.tsv.env.Parser())) } return nil } @@ -1146,7 +1146,7 @@ func (qre *QueryExecutor) GetSchemaDefinitions(tableType querypb.SchemaTableType } func (qre *QueryExecutor) getViewDefinitions(viewNames []string, callback func(schemaRes *querypb.GetSchemaResponse) error) error { - query, err := eschema.GetFetchViewQuery(viewNames, qre.tsv.SQLParser()) + query, err := eschema.GetFetchViewQuery(viewNames, qre.tsv.env.Parser()) if err != nil { return err } @@ -1154,7 +1154,7 @@ func (qre *QueryExecutor) getViewDefinitions(viewNames []string, callback func(s } func (qre *QueryExecutor) getTableDefinitions(tableNames []string, callback func(schemaRes *querypb.GetSchemaResponse) error) error { - query, err := eschema.GetFetchTableQuery(tableNames, qre.tsv.SQLParser()) + query, err := eschema.GetFetchTableQuery(tableNames, qre.tsv.env.Parser()) if err != nil { return err } @@ -1162,7 +1162,7 @@ func (qre *QueryExecutor) getTableDefinitions(tableNames []string, callback func } func (qre *QueryExecutor) getAllDefinitions(tableNames []string, callback func(schemaRes *querypb.GetSchemaResponse) error) error { - query, err := eschema.GetFetchTableAndViewsQuery(tableNames, qre.tsv.SQLParser()) + query, err := eschema.GetFetchTableAndViewsQuery(tableNames, qre.tsv.env.Parser()) if err != nil { return err } diff --git a/go/vt/vttablet/tabletserver/query_executor_test.go b/go/vt/vttablet/tabletserver/query_executor_test.go index 71979018a9c..33012059a11 100644 --- a/go/vt/vttablet/tabletserver/query_executor_test.go +++ b/go/vt/vttablet/tabletserver/query_executor_test.go @@ -29,10 +29,9 @@ import ( "github.com/stretchr/testify/require" "vitess.io/vitess/go/vt/sqlparser" + "vitess.io/vitess/go/vt/vtenv" "vitess.io/vitess/go/mysql" - "vitess.io/vitess/go/mysql/collations" - "vitess.io/vitess/go/mysql/config" "vitess.io/vitess/go/mysql/fakesqldb" "vitess.io/vitess/go/sqltypes" "vitess.io/vitess/go/sync2" @@ -1491,7 +1490,7 @@ func newTestTabletServer(ctx context.Context, flags executorFlags, db *fakesqldb } dbconfigs := newDBConfigs(db) cfg.DB = dbconfigs - tsv := NewTabletServer(ctx, "TabletServerTest", cfg, memorytopo.NewServer(ctx, ""), &topodatapb.TabletAlias{}, collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion) + tsv := NewTabletServer(ctx, vtenv.NewTestEnv(), "TabletServerTest", cfg, memorytopo.NewServer(ctx, ""), &topodatapb.TabletAlias{}) target := &querypb.Target{TabletType: topodatapb.TabletType_PRIMARY} err := tsv.StartService(target, dbconfigs, nil /* mysqld */) if cfg.TwoPCEnable { diff --git a/go/vt/vttablet/tabletserver/queryz.go b/go/vt/vttablet/tabletserver/queryz.go index f56402a1fdb..5d674b260cf 100644 --- a/go/vt/vttablet/tabletserver/queryz.go +++ b/go/vt/vttablet/tabletserver/queryz.go @@ -156,7 +156,7 @@ func queryzHandler(qe *QueryEngine, w http.ResponseWriter, r *http.Request) { return true } Value := &queryzRow{ - Query: logz.Wrappable(qe.env.SQLParser().TruncateForUI(plan.Original)), + Query: logz.Wrappable(qe.env.Environment().Parser().TruncateForUI(plan.Original)), Table: plan.TableName().String(), Plan: plan.PlanID, } diff --git a/go/vt/vttablet/tabletserver/repltracker/reader_test.go b/go/vt/vttablet/tabletserver/repltracker/reader_test.go index 89fe0594716..e065b05da7a 100644 --- a/go/vt/vttablet/tabletserver/repltracker/reader_test.go +++ b/go/vt/vttablet/tabletserver/repltracker/reader_test.go @@ -24,13 +24,11 @@ import ( "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" - "vitess.io/vitess/go/mysql/collations" - "vitess.io/vitess/go/mysql/config" "vitess.io/vitess/go/mysql/fakesqldb" "vitess.io/vitess/go/sqltypes" "vitess.io/vitess/go/test/utils" "vitess.io/vitess/go/vt/dbconfigs" - "vitess.io/vitess/go/vt/sqlparser" + "vitess.io/vitess/go/vt/vtenv" "vitess.io/vitess/go/vt/vttablet/tabletserver/tabletenv" querypb "vitess.io/vitess/go/vt/proto/query" @@ -147,7 +145,7 @@ func newReader(db *fakesqldb.DB, frozenTime *time.Time) *heartbeatReader { dbc := dbconfigs.NewTestDBConfigs(cp, cp, "") cfg.DB = dbc - tr := newHeartbeatReader(tabletenv.NewEnv(cfg, "ReaderTest", collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion)) + tr := newHeartbeatReader(tabletenv.NewEnv(vtenv.NewTestEnv(), cfg, "ReaderTest")) tr.keyspaceShard = "test:0" if frozenTime != nil { diff --git a/go/vt/vttablet/tabletserver/repltracker/repltracker_test.go b/go/vt/vttablet/tabletserver/repltracker/repltracker_test.go index 4681a31ae98..5e6150ddeb3 100644 --- a/go/vt/vttablet/tabletserver/repltracker/repltracker_test.go +++ b/go/vt/vttablet/tabletserver/repltracker/repltracker_test.go @@ -23,14 +23,12 @@ import ( "github.com/stretchr/testify/assert" - "vitess.io/vitess/go/mysql/collations" - "vitess.io/vitess/go/mysql/config" "vitess.io/vitess/go/mysql/fakesqldb" "vitess.io/vitess/go/vt/dbconfigs" "vitess.io/vitess/go/vt/mysqlctl" querypb "vitess.io/vitess/go/vt/proto/query" topodatapb "vitess.io/vitess/go/vt/proto/topodata" - "vitess.io/vitess/go/vt/sqlparser" + "vitess.io/vitess/go/vt/vtenv" "vitess.io/vitess/go/vt/vttablet/tabletserver/tabletenv" ) @@ -44,7 +42,7 @@ func TestReplTracker(t *testing.T) { params := db.ConnParams() cp := *params cfg.DB = dbconfigs.NewTestDBConfigs(cp, cp, "") - env := tabletenv.NewEnv(cfg, "ReplTrackerTest", collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion) + env := tabletenv.NewEnv(vtenv.NewTestEnv(), cfg, "ReplTrackerTest") alias := &topodatapb.TabletAlias{ Cell: "cell", Uid: 1, diff --git a/go/vt/vttablet/tabletserver/repltracker/writer_test.go b/go/vt/vttablet/tabletserver/repltracker/writer_test.go index 03e76c29645..0add32a1de0 100644 --- a/go/vt/vttablet/tabletserver/repltracker/writer_test.go +++ b/go/vt/vttablet/tabletserver/repltracker/writer_test.go @@ -25,13 +25,11 @@ import ( "github.com/stretchr/testify/assert" - "vitess.io/vitess/go/mysql/collations" - "vitess.io/vitess/go/mysql/config" "vitess.io/vitess/go/mysql/fakesqldb" "vitess.io/vitess/go/sqltypes" "vitess.io/vitess/go/vt/dbconfigs" topodatapb "vitess.io/vitess/go/vt/proto/topodata" - "vitess.io/vitess/go/vt/sqlparser" + "vitess.io/vitess/go/vt/vtenv" "vitess.io/vitess/go/vt/vttablet/tabletserver/tabletenv" ) @@ -119,7 +117,7 @@ func newTestWriter(db *fakesqldb.DB, frozenTime *time.Time) *heartbeatWriter { cp := *params dbc := dbconfigs.NewTestDBConfigs(cp, cp, "") - tw := newHeartbeatWriter(tabletenv.NewEnv(cfg, "WriterTest", collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion), &topodatapb.TabletAlias{Cell: "test", Uid: 1111}) + tw := newHeartbeatWriter(tabletenv.NewEnv(vtenv.NewTestEnv(), cfg, "WriterTest"), &topodatapb.TabletAlias{Cell: "test", Uid: 1111}) tw.keyspaceShard = "test:0" if frozenTime != nil { diff --git a/go/vt/vttablet/tabletserver/schema/db_test.go b/go/vt/vttablet/tabletserver/schema/db_test.go index 5e1bc429136..792f6ef94be 100644 --- a/go/vt/vttablet/tabletserver/schema/db_test.go +++ b/go/vt/vttablet/tabletserver/schema/db_test.go @@ -26,13 +26,12 @@ import ( "golang.org/x/exp/maps" "vitess.io/vitess/go/constants/sidecar" - "vitess.io/vitess/go/mysql/collations" - "vitess.io/vitess/go/mysql/config" "vitess.io/vitess/go/mysql/fakesqldb" "vitess.io/vitess/go/sqltypes" "vitess.io/vitess/go/vt/dbconfigs" querypb "vitess.io/vitess/go/vt/proto/query" "vitess.io/vitess/go/vt/sqlparser" + "vitess.io/vitess/go/vt/vtenv" "vitess.io/vitess/go/vt/vttablet/tabletserver/connpool" "vitess.io/vitess/go/vt/vttablet/tabletserver/tabletenv" ) @@ -99,7 +98,7 @@ func TestGenerateFullQuery(t *testing.T) { func TestGetCreateStatement(t *testing.T) { db := fakesqldb.New(t) - env := tabletenv.NewEnv(nil, "TestGetCreateStatement", collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion) + env := tabletenv.NewEnv(vtenv.NewTestEnv(), nil, "TestGetCreateStatement") conn, err := connpool.NewConn(context.Background(), dbconfigs.New(db.ConnParams()), nil, nil, env) require.NoError(t, err) @@ -135,7 +134,7 @@ func TestGetCreateStatement(t *testing.T) { func TestGetChangedViewNames(t *testing.T) { db := fakesqldb.New(t) - env := tabletenv.NewEnv(nil, "TestGetChangedViewNames", collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion) + env := tabletenv.NewEnv(vtenv.NewTestEnv(), nil, "TestGetChangedViewNames") conn, err := connpool.NewConn(context.Background(), dbconfigs.New(db.ConnParams()), nil, nil, env) require.NoError(t, err) @@ -169,7 +168,7 @@ func TestGetChangedViewNames(t *testing.T) { func TestGetViewDefinition(t *testing.T) { db := fakesqldb.New(t) - env := tabletenv.NewEnv(nil, "TestGetViewDefinition", collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion) + env := tabletenv.NewEnv(vtenv.NewTestEnv(), nil, "TestGetViewDefinition") conn, err := connpool.NewConn(context.Background(), dbconfigs.New(db.ConnParams()), nil, nil, env) require.NoError(t, err) @@ -342,7 +341,7 @@ func TestGetMismatchedTableNames(t *testing.T) { for _, tc := range testCases { t.Run(tc.name, func(t *testing.T) { db := fakesqldb.New(t) - env := tabletenv.NewEnv(nil, tc.name, collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion) + env := tabletenv.NewEnv(vtenv.NewTestEnv(), nil, tc.name) conn, err := connpool.NewConn(context.Background(), dbconfigs.New(db.ConnParams()), nil, nil, env) require.NoError(t, err) @@ -463,7 +462,7 @@ func TestReloadTablesInDB(t *testing.T) { for _, tc := range testCases { t.Run(tc.name, func(t *testing.T) { db := fakesqldb.New(t) - env := tabletenv.NewEnv(nil, tc.name, collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion) + env := tabletenv.NewEnv(vtenv.NewTestEnv(), nil, tc.name) conn, err := connpool.NewConn(context.Background(), dbconfigs.New(db.ConnParams()), nil, nil, env) require.NoError(t, err) @@ -596,7 +595,7 @@ func TestReloadViewsInDB(t *testing.T) { for _, tc := range testCases { t.Run(tc.name, func(t *testing.T) { db := fakesqldb.New(t) - env := tabletenv.NewEnv(nil, tc.name, collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion) + env := tabletenv.NewEnv(vtenv.NewTestEnv(), nil, tc.name) conn, err := connpool.NewConn(context.Background(), dbconfigs.New(db.ConnParams()), nil, nil, env) require.NoError(t, err) @@ -887,7 +886,7 @@ func TestReloadDataInDB(t *testing.T) { for _, tc := range testCases { t.Run(tc.name, func(t *testing.T) { db := fakesqldb.New(t) - env := tabletenv.NewEnv(nil, tc.name, collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion) + env := tabletenv.NewEnv(vtenv.NewTestEnv(), nil, tc.name) conn, err := connpool.NewConn(context.Background(), dbconfigs.New(db.ConnParams()), nil, nil, env) require.NoError(t, err) diff --git a/go/vt/vttablet/tabletserver/schema/engine.go b/go/vt/vttablet/tabletserver/schema/engine.go index 32828d78acb..1995bd5472d 100644 --- a/go/vt/vttablet/tabletserver/schema/engine.go +++ b/go/vt/vttablet/tabletserver/schema/engine.go @@ -29,10 +29,9 @@ import ( "golang.org/x/exp/maps" "vitess.io/vitess/go/constants/sidecar" - "vitess.io/vitess/go/mysql/collations" - "vitess.io/vitess/go/mysql/config" "vitess.io/vitess/go/mysql/replication" "vitess.io/vitess/go/mysql/sqlerror" + "vitess.io/vitess/go/vt/vtenv" "vitess.io/vitess/go/acl" "vitess.io/vitess/go/mysql" @@ -163,7 +162,7 @@ func (se *Engine) syncSidecarDB(ctx context.Context, conn *dbconnpool.DBConnecti } return conn.ExecuteFetch(query, maxRows, true) } - if err := sidecardb.Init(ctx, exec, se.env.CollationEnv(), se.env.SQLParser(), se.env.MySQLVersion()); err != nil { + if err := sidecardb.Init(ctx, se.env.Environment(), exec); err != nil { log.Errorf("Error in sidecardb.Init: %+v", err) if se.env.Config().DB.HasGlobalSettings() { log.Warning("Ignoring sidecardb.Init error for unmanaged tablets") @@ -500,7 +499,7 @@ func (se *Engine) reload(ctx context.Context, includeStats bool) error { log.V(2).Infof("Reading schema for table: %s", tableName) tableType := row[1].String() - table, err := LoadTable(conn, se.cp.DBName(), tableName, tableType, row[3].ToString(), se.env.CollationEnv()) + table, err := LoadTable(conn, se.cp.DBName(), tableName, tableType, row[3].ToString(), se.env.Environment().CollationEnv()) if err != nil { if isView := strings.Contains(tableType, tmutils.TableView); isView { log.Warningf("Failed reading schema for the view: %s, error: %v", tableName, err) @@ -537,7 +536,7 @@ func (se *Engine) reload(ctx context.Context, includeStats bool) error { if shouldUseDatabase { // If reloadDataInDB succeeds, then we don't want to prevent sending the broadcast notification. // So, we do this step in the end when we can receive no more errors that fail the reload operation. - err = reloadDataInDB(ctx, conn.Conn, altered, created, dropped, se.env.SQLParser()) + err = reloadDataInDB(ctx, conn.Conn, altered, created, dropped, se.env.Environment().Parser()) if err != nil { log.Errorf("error in updating schema information in Engine.reload() - %v", err) } @@ -829,7 +828,7 @@ func NewEngineForTests() *Engine { isOpen: true, tables: make(map[string]*Table), historian: newHistorian(false, 0, nil), - env: tabletenv.NewEnv(tabletenv.NewDefaultConfig(), "SchemaEngineForTests", collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion), + env: tabletenv.NewEnv(vtenv.NewTestEnv(), tabletenv.NewDefaultConfig(), "SchemaEngineForTests"), } return se } @@ -845,16 +844,8 @@ func (se *Engine) GetDBConnector() dbconfigs.Connector { return se.cp } -func (se *Engine) CollationEnv() *collations.Environment { - return se.env.CollationEnv() -} - -func (se *Engine) SQLParser() *sqlparser.Parser { - return se.env.SQLParser() -} - -func (se *Engine) MySQLVersion() string { - return se.env.MySQLVersion() +func (se *Engine) Environment() *vtenv.Environment { + return se.env.Environment() } func extractNamesFromTablesList(tables []*Table) []string { diff --git a/go/vt/vttablet/tabletserver/schema/engine_test.go b/go/vt/vttablet/tabletserver/schema/engine_test.go index e850489f1b4..b9492cbd185 100644 --- a/go/vt/vttablet/tabletserver/schema/engine_test.go +++ b/go/vt/vttablet/tabletserver/schema/engine_test.go @@ -34,8 +34,6 @@ import ( "vitess.io/vitess/go/constants/sidecar" "vitess.io/vitess/go/event/syslogger" "vitess.io/vitess/go/mysql" - "vitess.io/vitess/go/mysql/collations" - "vitess.io/vitess/go/mysql/config" "vitess.io/vitess/go/mysql/fakesqldb" "vitess.io/vitess/go/mysql/replication" "vitess.io/vitess/go/mysql/sqlerror" @@ -45,6 +43,7 @@ import ( "vitess.io/vitess/go/vt/dbconfigs" querypb "vitess.io/vitess/go/vt/proto/query" "vitess.io/vitess/go/vt/sqlparser" + "vitess.io/vitess/go/vt/vtenv" "vitess.io/vitess/go/vt/vttablet/tabletserver/connpool" "vitess.io/vitess/go/vt/vttablet/tabletserver/schema/schematest" "vitess.io/vitess/go/vt/vttablet/tabletserver/tabletenv" @@ -581,7 +580,7 @@ func newEngine(reloadTime time.Duration, idleTimeout time.Duration, schemaMaxAge cfg.OlapReadPool.IdleTimeout = idleTimeout cfg.TxPool.IdleTimeout = idleTimeout cfg.SchemaVersionMaxAgeSeconds = schemaMaxAgeSeconds - se := NewEngine(tabletenv.NewEnv(cfg, "SchemaTest", collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion)) + se := NewEngine(tabletenv.NewEnv(vtenv.NewTestEnv(), cfg, "SchemaTest")) se.InitDBConfig(newDBConfigs(db).DbaWithDB()) return se } @@ -765,7 +764,7 @@ func TestEngineMysqlTime(t *testing.T) { t.Run(tt.name, func(t *testing.T) { se := &Engine{} db := fakesqldb.New(t) - env := tabletenv.NewEnv(nil, tt.name, collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion) + env := tabletenv.NewEnv(vtenv.NewTestEnv(), nil, tt.name) conn, err := connpool.NewConn(context.Background(), dbconfigs.New(db.ConnParams()), nil, nil, env) require.NoError(t, err) @@ -872,7 +871,7 @@ func TestEnginePopulatePrimaryKeys(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { db := fakesqldb.New(t) - env := tabletenv.NewEnv(nil, tt.name, collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion) + env := tabletenv.NewEnv(vtenv.NewTestEnv(), nil, tt.name) conn, err := connpool.NewConn(context.Background(), dbconfigs.New(db.ConnParams()), nil, nil, env) require.NoError(t, err) se := &Engine{} @@ -934,7 +933,7 @@ func TestEngineUpdateInnoDBRowsRead(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { db := fakesqldb.New(t) - env := tabletenv.NewEnv(nil, tt.name, collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion) + env := tabletenv.NewEnv(vtenv.NewTestEnv(), nil, tt.name) conn, err := connpool.NewConn(context.Background(), dbconfigs.New(db.ConnParams()), nil, nil, env) require.NoError(t, err) se := &Engine{} @@ -962,7 +961,7 @@ func TestEngineUpdateInnoDBRowsRead(t *testing.T) { // TestEngineGetTableData tests the functionality of getTableData function func TestEngineGetTableData(t *testing.T) { db := fakesqldb.New(t) - env := tabletenv.NewEnv(nil, "TestEngineGetTableData", collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion) + env := tabletenv.NewEnv(vtenv.NewTestEnv(), nil, "TestEngineGetTableData") conn, err := connpool.NewConn(context.Background(), dbconfigs.New(db.ConnParams()), nil, nil, env) require.NoError(t, err) @@ -1137,7 +1136,7 @@ func TestEngineReload(t *testing.T) { cfg := tabletenv.NewDefaultConfig() cfg.DB = newDBConfigs(db) cfg.SignalWhenSchemaChange = true - env := tabletenv.NewEnv(nil, "TestEngineReload", collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion) + env := tabletenv.NewEnv(vtenv.NewTestEnv(), nil, "TestEngineReload") conn, err := connpool.NewConn(context.Background(), dbconfigs.New(db.ConnParams()), nil, nil, env) require.NoError(t, err) diff --git a/go/vt/vttablet/tabletserver/schema/load_table_test.go b/go/vt/vttablet/tabletserver/schema/load_table_test.go index 37f517ade0b..6416e2e306e 100644 --- a/go/vt/vttablet/tabletserver/schema/load_table_test.go +++ b/go/vt/vttablet/tabletserver/schema/load_table_test.go @@ -24,9 +24,9 @@ import ( "time" "vitess.io/vitess/go/mysql/collations" - "vitess.io/vitess/go/mysql/config" "vitess.io/vitess/go/test/utils" "vitess.io/vitess/go/vt/dbconfigs" + "vitess.io/vitess/go/vt/vtenv" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" @@ -236,7 +236,7 @@ func newTestLoadTable(tableType string, comment string, db *fakesqldb.DB) (*Tabl Size: 2, IdleTimeout: 10 * time.Second, } - connPool := connpool.NewPool(tabletenv.NewEnv(nil, "SchemaTest", collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion), "", cfg) + connPool := connpool.NewPool(tabletenv.NewEnv(vtenv.NewTestEnv(), nil, "SchemaTest"), "", cfg) connPool.Open(appParams, dbaParams, appParams) conn, err := connPool.Get(ctx, nil) if err != nil { diff --git a/go/vt/vttablet/tabletserver/schema/tracker.go b/go/vt/vttablet/tabletserver/schema/tracker.go index 58019c4c018..bce5e4b33d6 100644 --- a/go/vt/vttablet/tabletserver/schema/tracker.go +++ b/go/vt/vttablet/tabletserver/schema/tracker.go @@ -134,12 +134,12 @@ func (tr *Tracker) process(ctx context.Context) { gtid = event.Gtid } if event.Type == binlogdatapb.VEventType_DDL && - MustReloadSchemaOnDDL(event.Statement, tr.engine.cp.DBName(), tr.env.SQLParser()) { + MustReloadSchemaOnDDL(event.Statement, tr.engine.cp.DBName(), tr.env.Environment().Parser()) { if err := tr.schemaUpdated(gtid, event.Statement, event.Timestamp); err != nil { tr.env.Stats().ErrorCounters.Add(vtrpcpb.Code_INTERNAL.String(), 1) log.Errorf("Error updating schema: %s for ddl %s, gtid %s", - tr.env.SQLParser().TruncateForLog(err.Error()), event.Statement, gtid) + tr.env.Environment().Parser().TruncateForLog(err.Error()), event.Statement, gtid) } } } diff --git a/go/vt/vttablet/tabletserver/schema/tracker_test.go b/go/vt/vttablet/tabletserver/schema/tracker_test.go index 39056b41df2..32f68597779 100644 --- a/go/vt/vttablet/tabletserver/schema/tracker_test.go +++ b/go/vt/vttablet/tabletserver/schema/tracker_test.go @@ -22,11 +22,10 @@ import ( "github.com/stretchr/testify/require" - "vitess.io/vitess/go/mysql/collations" - "vitess.io/vitess/go/mysql/config" "vitess.io/vitess/go/sqltypes" binlogdatapb "vitess.io/vitess/go/vt/proto/binlogdata" "vitess.io/vitess/go/vt/sqlparser" + "vitess.io/vitess/go/vt/vtenv" "vitess.io/vitess/go/vt/vttablet/tabletserver/tabletenv" "vitess.io/vitess/go/vt/vttablet/tabletserver/throttle/throttlerapp" ) @@ -80,7 +79,7 @@ func TestTracker(t *testing.T) { } cfg := se.env.Config() cfg.TrackSchemaVersions = true - env := tabletenv.NewEnv(cfg, "TrackerTest", collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion) + env := tabletenv.NewEnv(vtenv.NewTestEnv(), cfg, "TrackerTest") initial := env.Stats().ErrorCounters.Counts()["INTERNAL"] tracker := NewTracker(env, vs, se) tracker.Open() @@ -124,7 +123,7 @@ func TestTrackerShouldNotInsertInitialSchema(t *testing.T) { } cfg := se.env.Config() cfg.TrackSchemaVersions = true - env := tabletenv.NewEnv(cfg, "TrackerTest", collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion) + env := tabletenv.NewEnv(vtenv.NewTestEnv(), cfg, "TrackerTest") tracker := NewTracker(env, vs, se) tracker.Open() <-vs.done diff --git a/go/vt/vttablet/tabletserver/state_manager_test.go b/go/vt/vttablet/tabletserver/state_manager_test.go index 2241910075e..59909888935 100644 --- a/go/vt/vttablet/tabletserver/state_manager_test.go +++ b/go/vt/vttablet/tabletserver/state_manager_test.go @@ -29,9 +29,8 @@ import ( "google.golang.org/protobuf/proto" "vitess.io/vitess/go/vt/sqlparser" + "vitess.io/vitess/go/vt/vtenv" - "vitess.io/vitess/go/mysql/collations" - "vitess.io/vitess/go/mysql/config" "vitess.io/vitess/go/mysql/fakesqldb" "vitess.io/vitess/go/vt/dbconfigs" "vitess.io/vitess/go/vt/log" @@ -735,7 +734,7 @@ func newTestStateManager(t *testing.T) *stateManager { order.Store(0) cfg := tabletenv.NewDefaultConfig() parser := sqlparser.NewTestParser() - env := tabletenv.NewEnv(cfg, "StateManagerTest", collations.MySQL8(), parser, config.DefaultMySQLVersion) + env := tabletenv.NewEnv(vtenv.NewTestEnv(), cfg, "StateManagerTest") sm := &stateManager{ statelessql: NewQueryList("stateless", parser), statefulql: NewQueryList("stateful", parser), diff --git a/go/vt/vttablet/tabletserver/stateful_connection_pool.go b/go/vt/vttablet/tabletserver/stateful_connection_pool.go index a28d153dca1..64268825b70 100644 --- a/go/vt/vttablet/tabletserver/stateful_connection_pool.go +++ b/go/vt/vttablet/tabletserver/stateful_connection_pool.go @@ -93,7 +93,7 @@ func (sf *StatefulConnectionPool) Close() { if conn.IsInTransaction() { thing = "transaction" } - log.Warningf("killing %s for shutdown: %s", thing, conn.String(sf.env.Config().SanitizeLogMessages, sf.env.SQLParser())) + log.Warningf("killing %s for shutdown: %s", thing, conn.String(sf.env.Config().SanitizeLogMessages, sf.env.Environment().Parser())) sf.env.Stats().InternalErrors.Add("StrayTransactions", 1) conn.Close() conn.Releasef("pool closed") diff --git a/go/vt/vttablet/tabletserver/tabletenv/env.go b/go/vt/vttablet/tabletserver/tabletenv/env.go index e90425d4b27..27b4330c735 100644 --- a/go/vt/vttablet/tabletserver/tabletenv/env.go +++ b/go/vt/vttablet/tabletserver/tabletenv/env.go @@ -19,11 +19,10 @@ limitations under the License. package tabletenv import ( - "vitess.io/vitess/go/mysql/collations" "vitess.io/vitess/go/tb" "vitess.io/vitess/go/vt/log" "vitess.io/vitess/go/vt/servenv" - "vitess.io/vitess/go/vt/sqlparser" + "vitess.io/vitess/go/vt/vtenv" ) // Env defines the functions supported by TabletServer @@ -33,42 +32,34 @@ type Env interface { Config() *TabletConfig Exporter() *servenv.Exporter Stats() *Stats - SQLParser() *sqlparser.Parser LogError() - CollationEnv() *collations.Environment - MySQLVersion() string + Environment() *vtenv.Environment } type testEnv struct { - config *TabletConfig - exporter *servenv.Exporter - stats *Stats - collationEnv *collations.Environment - parser *sqlparser.Parser - mysqlVersion string + config *TabletConfig + exporter *servenv.Exporter + stats *Stats + env *vtenv.Environment } // NewEnv creates an Env that can be used for tabletserver subcomponents // without an actual TabletServer. -func NewEnv(config *TabletConfig, exporterName string, collationEnv *collations.Environment, parser *sqlparser.Parser, mysqlVersion string) Env { +func NewEnv(env *vtenv.Environment, config *TabletConfig, exporterName string) Env { exporter := servenv.NewExporter(exporterName, "Tablet") return &testEnv{ - config: config, - exporter: exporter, - stats: NewStats(exporter), - collationEnv: collationEnv, - parser: parser, - mysqlVersion: mysqlVersion, + config: config, + exporter: exporter, + stats: NewStats(exporter), + env: env, } } -func (*testEnv) CheckMySQL() {} -func (te *testEnv) Config() *TabletConfig { return te.config } -func (te *testEnv) Exporter() *servenv.Exporter { return te.exporter } -func (te *testEnv) Stats() *Stats { return te.stats } -func (te *testEnv) CollationEnv() *collations.Environment { return te.collationEnv } -func (te *testEnv) SQLParser() *sqlparser.Parser { return te.parser } -func (te *testEnv) MySQLVersion() string { return te.mysqlVersion } +func (*testEnv) CheckMySQL() {} +func (te *testEnv) Config() *TabletConfig { return te.config } +func (te *testEnv) Exporter() *servenv.Exporter { return te.exporter } +func (te *testEnv) Stats() *Stats { return te.stats } +func (te *testEnv) Environment() *vtenv.Environment { return te.env } func (te *testEnv) LogError() { if x := recover(); x != nil { diff --git a/go/vt/vttablet/tabletserver/tabletserver.go b/go/vt/vttablet/tabletserver/tabletserver.go index a70eeaadb68..6ecc46c68ab 100644 --- a/go/vt/vttablet/tabletserver/tabletserver.go +++ b/go/vt/vttablet/tabletserver/tabletserver.go @@ -34,7 +34,6 @@ import ( "time" "vitess.io/vitess/go/acl" - "vitess.io/vitess/go/mysql/collations" "vitess.io/vitess/go/mysql/sqlerror" "vitess.io/vitess/go/pools/smartconnpool" "vitess.io/vitess/go/sqltypes" @@ -52,6 +51,7 @@ import ( "vitess.io/vitess/go/vt/tableacl" "vitess.io/vitess/go/vt/topo" "vitess.io/vitess/go/vt/topo/topoproto" + "vitess.io/vitess/go/vt/vtenv" "vitess.io/vitess/go/vt/vterrors" "vitess.io/vitess/go/vt/vttablet/onlineddl" "vitess.io/vitess/go/vt/vttablet/queryservice" @@ -129,17 +129,7 @@ type TabletServer struct { // This field is only stored for testing checkMysqlGaugeFunc *stats.GaugeFunc - collationEnv *collations.Environment - parser *sqlparser.Parser - mysqlVersion string -} - -func (tsv *TabletServer) SQLParser() *sqlparser.Parser { - return tsv.parser -} - -func (tsv *TabletServer) MySQLVersion() string { - return tsv.mysqlVersion + env *vtenv.Environment } var _ queryservice.QueryService = (*TabletServer)(nil) @@ -150,8 +140,8 @@ var _ queryservice.QueryService = (*TabletServer)(nil) var RegisterFunctions []func(Controller) // NewServer creates a new TabletServer based on the command line flags. -func NewServer(ctx context.Context, name string, topoServer *topo.Server, alias *topodatapb.TabletAlias, collationEnv *collations.Environment, parser *sqlparser.Parser, mysqlVersion string) *TabletServer { - return NewTabletServer(ctx, name, tabletenv.NewCurrentConfig(), topoServer, alias, collationEnv, parser, mysqlVersion) +func NewServer(ctx context.Context, env *vtenv.Environment, name string, topoServer *topo.Server, alias *topodatapb.TabletAlias) *TabletServer { + return NewTabletServer(ctx, env, name, tabletenv.NewCurrentConfig(), topoServer, alias) } var ( @@ -161,7 +151,7 @@ var ( // NewTabletServer creates an instance of TabletServer. Only the first // instance of TabletServer will expose its state variables. -func NewTabletServer(ctx context.Context, name string, config *tabletenv.TabletConfig, topoServer *topo.Server, alias *topodatapb.TabletAlias, collationEnv *collations.Environment, parser *sqlparser.Parser, mysqlVersion string) *TabletServer { +func NewTabletServer(ctx context.Context, env *vtenv.Environment, name string, config *tabletenv.TabletConfig, topoServer *topo.Server, alias *topodatapb.TabletAlias) *TabletServer { exporter := servenv.NewExporter(name, "Tablet") tsv := &TabletServer{ exporter: exporter, @@ -172,9 +162,7 @@ func NewTabletServer(ctx context.Context, name string, config *tabletenv.TabletC enableHotRowProtection: config.HotRowProtection.Mode != tabletenv.Disable, topoServer: topoServer, alias: alias.CloneVT(), - collationEnv: collationEnv, - parser: parser, - mysqlVersion: mysqlVersion, + env: env, } tsv.QueryTimeout.Store(config.Oltp.QueryTimeout.Nanoseconds()) @@ -187,9 +175,9 @@ func NewTabletServer(ctx context.Context, name string, config *tabletenv.TabletC return tsv.sm.Target().TabletType } - tsv.statelessql = NewQueryList("oltp-stateless", parser) - tsv.statefulql = NewQueryList("oltp-stateful", parser) - tsv.olapql = NewQueryList("olap", parser) + tsv.statelessql = NewQueryList("oltp-stateless", env.Parser()) + tsv.statefulql = NewQueryList("oltp-stateful", env.Parser()) + tsv.olapql = NewQueryList("olap", env.Parser()) tsv.se = schema.NewEngine(tsv) tsv.hs = newHealthStreamer(tsv, alias, tsv.se) tsv.rt = repltracker.NewReplTracker(tsv, alias) @@ -318,9 +306,9 @@ func (tsv *TabletServer) Stats() *tabletenv.Stats { return tsv.stats } -// Stats satisfies tabletenv.Env. -func (tsv *TabletServer) CollationEnv() *collations.Environment { - return tsv.collationEnv +// Environment satisfies tabletenv.Env. +func (tsv *TabletServer) Environment() *vtenv.Environment { + return tsv.env } // LogError satisfies tabletenv.Env. @@ -1590,13 +1578,13 @@ func (tsv *TabletServer) handlePanicAndSendLogStats( // not a concern. var messagef, logMessage, query, truncatedQuery string messagef = fmt.Sprintf("Uncaught panic for %%v:\n%v\n%s", x, tb.Stack(4) /* Skip the last 4 boiler-plate frames. */) - query = queryAsString(sql, bindVariables, tsv.TerseErrors, false, tsv.SQLParser()) + query = queryAsString(sql, bindVariables, tsv.TerseErrors, false, tsv.env.Parser()) terr := vterrors.Errorf(vtrpcpb.Code_UNKNOWN, "%s", fmt.Sprintf(messagef, query)) if tsv.TerseErrors == tsv.Config().SanitizeLogMessages { - truncatedQuery = queryAsString(sql, bindVariables, tsv.TerseErrors, true, tsv.SQLParser()) + truncatedQuery = queryAsString(sql, bindVariables, tsv.TerseErrors, true, tsv.env.Parser()) logMessage = fmt.Sprintf(messagef, truncatedQuery) } else { - truncatedQuery = queryAsString(sql, bindVariables, tsv.Config().SanitizeLogMessages, true, tsv.SQLParser()) + truncatedQuery = queryAsString(sql, bindVariables, tsv.Config().SanitizeLogMessages, true, tsv.env.Parser()) logMessage = fmt.Sprintf(messagef, truncatedQuery) } log.Error(logMessage) @@ -1656,20 +1644,20 @@ func (tsv *TabletServer) convertAndLogError(ctx context.Context, sql string, bin sqlState := sqlErr.SQLState() errnum := sqlErr.Number() if tsv.TerseErrors && errCode != vtrpcpb.Code_FAILED_PRECONDITION { - err = vterrors.Errorf(errCode, "(errno %d) (sqlstate %s)%s: %s", errnum, sqlState, callerID, queryAsString(sql, bindVariables, tsv.TerseErrors, false, tsv.SQLParser())) + err = vterrors.Errorf(errCode, "(errno %d) (sqlstate %s)%s: %s", errnum, sqlState, callerID, queryAsString(sql, bindVariables, tsv.TerseErrors, false, tsv.env.Parser())) if logMethod != nil { - message = fmt.Sprintf("(errno %d) (sqlstate %s)%s: %s", errnum, sqlState, callerID, queryAsString(sql, bindVariables, tsv.Config().SanitizeLogMessages, true, tsv.SQLParser())) + message = fmt.Sprintf("(errno %d) (sqlstate %s)%s: %s", errnum, sqlState, callerID, queryAsString(sql, bindVariables, tsv.Config().SanitizeLogMessages, true, tsv.env.Parser())) } } else { - err = vterrors.Errorf(errCode, "%s (errno %d) (sqlstate %s)%s: %s", sqlErr.Message, errnum, sqlState, callerID, queryAsString(sql, bindVariables, false, false, tsv.SQLParser())) + err = vterrors.Errorf(errCode, "%s (errno %d) (sqlstate %s)%s: %s", sqlErr.Message, errnum, sqlState, callerID, queryAsString(sql, bindVariables, false, false, tsv.env.Parser())) if logMethod != nil { - message = fmt.Sprintf("%s (errno %d) (sqlstate %s)%s: %s", sqlErr.Message, errnum, sqlState, callerID, queryAsString(sql, bindVariables, tsv.Config().SanitizeLogMessages, true, tsv.SQLParser())) + message = fmt.Sprintf("%s (errno %d) (sqlstate %s)%s: %s", sqlErr.Message, errnum, sqlState, callerID, queryAsString(sql, bindVariables, tsv.Config().SanitizeLogMessages, true, tsv.env.Parser())) } } } else { err = vterrors.Errorf(errCode, "%v%s", err.Error(), callerID) if logMethod != nil { - message = fmt.Sprintf("%v: %v", err, queryAsString(sql, bindVariables, tsv.Config().SanitizeLogMessages, true, tsv.SQLParser())) + message = fmt.Sprintf("%v: %v", err, queryAsString(sql, bindVariables, tsv.Config().SanitizeLogMessages, true, tsv.env.Parser())) } } @@ -1857,7 +1845,7 @@ func (tsv *TabletServer) registerQuerylogzHandler() { tsv.exporter.HandleFunc("/querylogz", func(w http.ResponseWriter, r *http.Request) { ch := tabletenv.StatsLogger.Subscribe("querylogz") defer tabletenv.StatsLogger.Unsubscribe(ch) - querylogzHandler(ch, w, r, tsv.parser) + querylogzHandler(ch, w, r, tsv.env.Parser()) }) } diff --git a/go/vt/vttablet/tabletserver/tabletserver_test.go b/go/vt/vttablet/tabletserver/tabletserver_test.go index ae245a68175..e08e300eafe 100644 --- a/go/vt/vttablet/tabletserver/tabletserver_test.go +++ b/go/vt/vttablet/tabletserver/tabletserver_test.go @@ -30,11 +30,11 @@ import ( "testing" "time" - "vitess.io/vitess/go/mysql/collations" "vitess.io/vitess/go/mysql/config" "vitess.io/vitess/go/mysql/sqlerror" "vitess.io/vitess/go/vt/callerid" "vitess.io/vitess/go/vt/sidecardb" + "vitess.io/vitess/go/vt/vtenv" "vitess.io/vitess/go/mysql/fakesqldb" "vitess.io/vitess/go/test/utils" @@ -444,7 +444,7 @@ func TestTabletServerBeginFail(t *testing.T) { defer cancel() cfg := tabletenv.NewDefaultConfig() cfg.TxPool.Size = 1 - db, tsv := setupTabletServerTestCustom(t, ctx, cfg, "", sqlparser.NewTestParser()) + db, tsv := setupTabletServerTestCustom(t, ctx, cfg, "", vtenv.NewTestEnv()) defer tsv.StopService() defer db.Close() @@ -952,7 +952,7 @@ func TestSerializeTransactionsSameRow(t *testing.T) { cfg.HotRowProtection.MaxConcurrency = 1 // Reduce the txpool to 2 because we should never consume more than two slots. cfg.TxPool.Size = 2 - db, tsv := setupTabletServerTestCustom(t, ctx, cfg, "", sqlparser.NewTestParser()) + db, tsv := setupTabletServerTestCustom(t, ctx, cfg, "", vtenv.NewTestEnv()) defer tsv.StopService() defer db.Close() @@ -1059,7 +1059,7 @@ func TestDMLQueryWithoutWhereClause(t *testing.T) { cfg.HotRowProtection.Mode = tabletenv.Enable cfg.HotRowProtection.MaxConcurrency = 1 cfg.TxPool.Size = 2 - db, tsv := setupTabletServerTestCustom(t, ctx, cfg, "", sqlparser.NewTestParser()) + db, tsv := setupTabletServerTestCustom(t, ctx, cfg, "", vtenv.NewTestEnv()) defer tsv.StopService() defer db.Close() @@ -1087,7 +1087,7 @@ func TestSerializeTransactionsSameRow_ConcurrentTransactions(t *testing.T) { cfg.HotRowProtection.MaxConcurrency = 2 // Reduce the txpool to 2 because we should never consume more than two slots. cfg.TxPool.Size = 2 - db, tsv := setupTabletServerTestCustom(t, ctx, cfg, "", sqlparser.NewTestParser()) + db, tsv := setupTabletServerTestCustom(t, ctx, cfg, "", vtenv.NewTestEnv()) defer tsv.StopService() defer db.Close() @@ -1223,7 +1223,7 @@ func TestSerializeTransactionsSameRow_TooManyPendingRequests(t *testing.T) { cfg.HotRowProtection.Mode = tabletenv.Enable cfg.HotRowProtection.MaxQueueSize = 1 cfg.HotRowProtection.MaxConcurrency = 1 - db, tsv := setupTabletServerTestCustom(t, ctx, cfg, "", sqlparser.NewTestParser()) + db, tsv := setupTabletServerTestCustom(t, ctx, cfg, "", vtenv.NewTestEnv()) defer tsv.StopService() defer db.Close() @@ -1306,7 +1306,7 @@ func TestSerializeTransactionsSameRow_RequestCanceled(t *testing.T) { cfg := tabletenv.NewDefaultConfig() cfg.HotRowProtection.Mode = tabletenv.Enable cfg.HotRowProtection.MaxConcurrency = 1 - db, tsv := setupTabletServerTestCustom(t, ctx, cfg, "", sqlparser.NewTestParser()) + db, tsv := setupTabletServerTestCustom(t, ctx, cfg, "", vtenv.NewTestEnv()) defer tsv.StopService() defer db.Close() @@ -1560,7 +1560,7 @@ func TestHandleExecUnknownError(t *testing.T) { defer cancel() logStats := tabletenv.NewLogStats(ctx, "TestHandleExecError") cfg := tabletenv.NewDefaultConfig() - tsv := NewTabletServer(ctx, "TabletServerTest", cfg, memorytopo.NewServer(ctx, ""), &topodatapb.TabletAlias{}, collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion) + tsv := NewTabletServer(ctx, vtenv.NewTestEnv(), "TabletServerTest", cfg, memorytopo.NewServer(ctx, ""), &topodatapb.TabletAlias{}) defer tsv.handlePanicAndSendLogStats("select * from test_table", nil, logStats) panic("unknown exec error") } @@ -1682,7 +1682,7 @@ func TestHandleExecTabletError(t *testing.T) { ctx, cancel := context.WithCancel(context.Background()) defer cancel() cfg := tabletenv.NewDefaultConfig() - tsv := NewTabletServer(ctx, "TabletServerTest", cfg, memorytopo.NewServer(ctx, ""), &topodatapb.TabletAlias{}, collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion) + tsv := NewTabletServer(ctx, vtenv.NewTestEnv(), "TabletServerTest", cfg, memorytopo.NewServer(ctx, ""), &topodatapb.TabletAlias{}) tl := newTestLogger() defer tl.Close() err := tsv.convertAndLogError( @@ -1707,7 +1707,7 @@ func TestTerseErrors(t *testing.T) { cfg := tabletenv.NewDefaultConfig() cfg.TerseErrors = true cfg.SanitizeLogMessages = false - tsv := NewTabletServer(ctx, "TabletServerTest", cfg, memorytopo.NewServer(ctx, ""), &topodatapb.TabletAlias{}, collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion) + tsv := NewTabletServer(ctx, vtenv.NewTestEnv(), "TabletServerTest", cfg, memorytopo.NewServer(ctx, ""), &topodatapb.TabletAlias{}) tl := newTestLogger() defer tl.Close() @@ -1741,7 +1741,7 @@ func TestSanitizeLogMessages(t *testing.T) { cfg := tabletenv.NewDefaultConfig() cfg.TerseErrors = false cfg.SanitizeLogMessages = true - tsv := NewTabletServer(ctx, "TabletServerTest", cfg, memorytopo.NewServer(ctx, ""), &topodatapb.TabletAlias{}, collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion) + tsv := NewTabletServer(ctx, vtenv.NewTestEnv(), "TabletServerTest", cfg, memorytopo.NewServer(ctx, ""), &topodatapb.TabletAlias{}) tl := newTestLogger() defer tl.Close() @@ -1774,7 +1774,7 @@ func TestTerseErrorsNonSQLError(t *testing.T) { defer cancel() cfg := tabletenv.NewDefaultConfig() cfg.TerseErrors = true - tsv := NewTabletServer(ctx, "TabletServerTest", cfg, memorytopo.NewServer(ctx, ""), &topodatapb.TabletAlias{}, collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion) + tsv := NewTabletServer(ctx, vtenv.NewTestEnv(), "TabletServerTest", cfg, memorytopo.NewServer(ctx, ""), &topodatapb.TabletAlias{}) tl := newTestLogger() defer tl.Close() err := tsv.convertAndLogError( @@ -1799,7 +1799,7 @@ func TestSanitizeLogMessagesNonSQLError(t *testing.T) { cfg := tabletenv.NewDefaultConfig() cfg.TerseErrors = false cfg.SanitizeLogMessages = true - tsv := NewTabletServer(ctx, "TabletServerTest", cfg, memorytopo.NewServer(ctx, ""), &topodatapb.TabletAlias{}, collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion) + tsv := NewTabletServer(ctx, vtenv.NewTestEnv(), "TabletServerTest", cfg, memorytopo.NewServer(ctx, ""), &topodatapb.TabletAlias{}) tl := newTestLogger() defer tl.Close() err := tsv.convertAndLogError( @@ -1824,7 +1824,7 @@ func TestSanitizeMessagesBindVars(t *testing.T) { cfg := tabletenv.NewDefaultConfig() cfg.TerseErrors = true cfg.SanitizeLogMessages = true - tsv := NewTabletServer(ctx, "TabletServerTest", cfg, memorytopo.NewServer(ctx, ""), &topodatapb.TabletAlias{}, collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion) + tsv := NewTabletServer(ctx, vtenv.NewTestEnv(), "TabletServerTest", cfg, memorytopo.NewServer(ctx, ""), &topodatapb.TabletAlias{}) tl := newTestLogger() defer tl.Close() @@ -1855,7 +1855,7 @@ func TestSanitizeMessagesNoBindVars(t *testing.T) { cfg := tabletenv.NewDefaultConfig() cfg.TerseErrors = true cfg.SanitizeLogMessages = true - tsv := NewTabletServer(ctx, "TabletServerTest", cfg, memorytopo.NewServer(ctx, ""), &topodatapb.TabletAlias{}, collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion) + tsv := NewTabletServer(ctx, vtenv.NewTestEnv(), "TabletServerTest", cfg, memorytopo.NewServer(ctx, ""), &topodatapb.TabletAlias{}) tl := newTestLogger() defer tl.Close() err := tsv.convertAndLogError(ctx, "", nil, vterrors.Errorf(vtrpcpb.Code_DEADLINE_EXCEEDED, "sensitive message"), nil) @@ -1873,7 +1873,7 @@ func TestTruncateErrorLen(t *testing.T) { defer cancel() cfg := tabletenv.NewDefaultConfig() cfg.TruncateErrorLen = 32 - tsv := NewTabletServer(ctx, "TabletServerTest", cfg, memorytopo.NewServer(ctx, ""), &topodatapb.TabletAlias{}, collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion) + tsv := NewTabletServer(ctx, vtenv.NewTestEnv(), "TabletServerTest", cfg, memorytopo.NewServer(ctx, ""), &topodatapb.TabletAlias{}) tl := newTestLogger() defer tl.Close() err := tsv.convertAndLogError( @@ -1899,12 +1899,12 @@ func TestTruncateMessages(t *testing.T) { cfg.TerseErrors = false // Sanitize the log messages, which means that the bind vars are omitted cfg.SanitizeLogMessages = true - parser, err := sqlparser.New(sqlparser.Options{ + env, err := vtenv.New(vtenv.Options{ MySQLServerVersion: config.DefaultMySQLVersion, TruncateErrLen: 52, }) require.NoError(t, err) - tsv := NewTabletServer(ctx, "TabletServerTest", cfg, memorytopo.NewServer(ctx, ""), &topodatapb.TabletAlias{}, collations.MySQL8(), parser, config.DefaultMySQLVersion) + tsv := NewTabletServer(ctx, env, "TabletServerTest", cfg, memorytopo.NewServer(ctx, ""), &topodatapb.TabletAlias{}) tl := newTestLogger() defer tl.Close() @@ -1931,7 +1931,7 @@ func TestTruncateMessages(t *testing.T) { t.Errorf("log got '%s', want '%s'", tl.getLog(0), wantLog) } - parser.SetTruncateErrLen(140) + env.Parser().SetTruncateErrLen(140) err = tsv.convertAndLogError( ctx, sql, @@ -1958,7 +1958,7 @@ func TestTerseErrorsIgnoreFailoverInProgress(t *testing.T) { defer cancel() cfg := tabletenv.NewDefaultConfig() cfg.TerseErrors = true - tsv := NewTabletServer(ctx, "TabletServerTest", cfg, memorytopo.NewServer(ctx, ""), &topodatapb.TabletAlias{}, collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion) + tsv := NewTabletServer(ctx, vtenv.NewTestEnv(), "TabletServerTest", cfg, memorytopo.NewServer(ctx, ""), &topodatapb.TabletAlias{}) tl := newTestLogger() defer tl.Close() err := tsv.convertAndLogError(ctx, "select * from test_table where id = :a", @@ -2000,7 +2000,7 @@ func TestACLHUP(t *testing.T) { defer cancel() tableacl.Register("simpleacl", &simpleacl.Factory{}) cfg := tabletenv.NewDefaultConfig() - tsv := NewTabletServer(ctx, "TabletServerTest", cfg, memorytopo.NewServer(ctx, ""), &topodatapb.TabletAlias{}, collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion) + tsv := NewTabletServer(ctx, vtenv.NewTestEnv(), "TabletServerTest", cfg, memorytopo.NewServer(ctx, ""), &topodatapb.TabletAlias{}) f, err := os.CreateTemp("", "tableacl") require.NoError(t, err) @@ -2510,13 +2510,13 @@ func TestDatabaseNameReplaceByKeyspaceNameReserveBeginExecuteMethod(t *testing.T func setupTabletServerTest(t testing.TB, ctx context.Context, keyspaceName string) (*fakesqldb.DB, *TabletServer) { cfg := tabletenv.NewDefaultConfig() - return setupTabletServerTestCustom(t, ctx, cfg, keyspaceName, sqlparser.NewTestParser()) + return setupTabletServerTestCustom(t, ctx, cfg, keyspaceName, vtenv.NewTestEnv()) } -func setupTabletServerTestCustom(t testing.TB, ctx context.Context, cfg *tabletenv.TabletConfig, keyspaceName string, parser *sqlparser.Parser) (*fakesqldb.DB, *TabletServer) { +func setupTabletServerTestCustom(t testing.TB, ctx context.Context, cfg *tabletenv.TabletConfig, keyspaceName string, env *vtenv.Environment) (*fakesqldb.DB, *TabletServer) { db := setupFakeDB(t) - sidecardb.AddSchemaInitQueries(db, true, parser) - tsv := NewTabletServer(ctx, "TabletServerTest", cfg, memorytopo.NewServer(ctx, ""), &topodatapb.TabletAlias{}, collations.MySQL8(), parser, config.DefaultMySQLVersion) + sidecardb.AddSchemaInitQueries(db, true, env.Parser()) + tsv := NewTabletServer(ctx, env, "TabletServerTest", cfg, memorytopo.NewServer(ctx, ""), &topodatapb.TabletAlias{}) require.Equal(t, StateNotConnected, tsv.sm.State()) dbcfgs := newDBConfigs(db) target := &querypb.Target{ diff --git a/go/vt/vttablet/tabletserver/throttle/throttler_test.go b/go/vt/vttablet/tabletserver/throttle/throttler_test.go index f191bf41cce..25de8ca96f5 100644 --- a/go/vt/vttablet/tabletserver/throttle/throttler_test.go +++ b/go/vt/vttablet/tabletserver/throttle/throttler_test.go @@ -28,10 +28,8 @@ import ( "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" - "vitess.io/vitess/go/mysql/collations" - cfg "vitess.io/vitess/go/mysql/config" - "vitess.io/vitess/go/vt/sqlparser" "vitess.io/vitess/go/vt/topo" + "vitess.io/vitess/go/vt/vtenv" "vitess.io/vitess/go/vt/vttablet/tabletserver/connpool" "vitess.io/vitess/go/vt/vttablet/tabletserver/tabletenv" "vitess.io/vitess/go/vt/vttablet/tabletserver/throttle/config" @@ -111,7 +109,7 @@ func newTestThrottler() *Throttler { s.ThrottleThreshold = &atomic.Uint64{} s.ThrottleThreshold.Store(1) } - env := tabletenv.NewEnv(nil, "TabletServerTest", collations.MySQL8(), sqlparser.NewTestParser(), cfg.DefaultMySQLVersion) + env := tabletenv.NewEnv(vtenv.NewTestEnv(), nil, "TabletServerTest") throttler := &Throttler{ mysqlClusterProbesChan: make(chan *mysql.ClusterProbes), mysqlClusterThresholds: cache.New(cache.NoExpiration, 0), diff --git a/go/vt/vttablet/tabletserver/tx_engine_test.go b/go/vt/vttablet/tabletserver/tx_engine_test.go index bb74a3dd158..7a97a8f331f 100644 --- a/go/vt/vttablet/tabletserver/tx_engine_test.go +++ b/go/vt/vttablet/tabletserver/tx_engine_test.go @@ -25,9 +25,7 @@ import ( "testing" "time" - "vitess.io/vitess/go/mysql/collations" - "vitess.io/vitess/go/mysql/config" - "vitess.io/vitess/go/vt/sqlparser" + "vitess.io/vitess/go/vt/vtenv" "vitess.io/vitess/go/vt/vttablet/tabletserver/tx" "github.com/stretchr/testify/assert" @@ -51,7 +49,7 @@ func TestTxEngineClose(t *testing.T) { cfg.TxPool.Size = 10 cfg.Oltp.TxTimeout = 100 * time.Millisecond cfg.GracePeriods.Shutdown = 0 - te := NewTxEngine(tabletenv.NewEnv(cfg, "TabletServerTest", collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion)) + te := NewTxEngine(tabletenv.NewEnv(vtenv.NewTestEnv(), cfg, "TabletServerTest")) // Normal close. te.AcceptReadWrite() @@ -154,7 +152,7 @@ func TestTxEngineBegin(t *testing.T) { db.AddQueryPattern(".*", &sqltypes.Result{}) cfg := tabletenv.NewDefaultConfig() cfg.DB = newDBConfigs(db) - te := NewTxEngine(tabletenv.NewEnv(cfg, "TabletServerTest", collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion)) + te := NewTxEngine(tabletenv.NewEnv(vtenv.NewTestEnv(), cfg, "TabletServerTest")) for _, exec := range []func() (int64, string, error){ func() (int64, string, error) { @@ -200,7 +198,7 @@ func TestTxEngineRenewFails(t *testing.T) { db.AddQueryPattern(".*", &sqltypes.Result{}) cfg := tabletenv.NewDefaultConfig() cfg.DB = newDBConfigs(db) - te := NewTxEngine(tabletenv.NewEnv(cfg, "TabletServerTest", collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion)) + te := NewTxEngine(tabletenv.NewEnv(vtenv.NewTestEnv(), cfg, "TabletServerTest")) te.AcceptReadOnly() options := &querypb.ExecuteOptions{} connID, _, err := te.ReserveBegin(ctx, options, nil, nil) @@ -538,7 +536,7 @@ func setupTxEngine(db *fakesqldb.DB) *TxEngine { cfg.TxPool.Size = 10 cfg.Oltp.TxTimeout = 100 * time.Millisecond cfg.GracePeriods.Shutdown = 0 - te := NewTxEngine(tabletenv.NewEnv(cfg, "TabletServerTest", collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion)) + te := NewTxEngine(tabletenv.NewEnv(vtenv.NewTestEnv(), cfg, "TabletServerTest")) return te } @@ -570,7 +568,7 @@ func TestTxEngineFailReserve(t *testing.T) { db.AddQueryPattern(".*", &sqltypes.Result{}) cfg := tabletenv.NewDefaultConfig() cfg.DB = newDBConfigs(db) - te := NewTxEngine(tabletenv.NewEnv(cfg, "TabletServerTest", collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion)) + te := NewTxEngine(tabletenv.NewEnv(vtenv.NewTestEnv(), cfg, "TabletServerTest")) options := &querypb.ExecuteOptions{} _, err := te.Reserve(ctx, options, 0, nil) diff --git a/go/vt/vttablet/tabletserver/tx_pool.go b/go/vt/vttablet/tabletserver/tx_pool.go index f01d12b655c..52f356e0cca 100644 --- a/go/vt/vttablet/tabletserver/tx_pool.go +++ b/go/vt/vttablet/tabletserver/tx_pool.go @@ -130,7 +130,7 @@ func (tp *TxPool) Shutdown(ctx context.Context) { func (tp *TxPool) transactionKiller() { defer tp.env.LogError() for _, conn := range tp.scp.GetElapsedTimeout(vterrors.TxKillerRollback) { - log.Warningf("killing transaction (exceeded timeout: %v): %s", conn.timeout, conn.String(tp.env.Config().SanitizeLogMessages, tp.env.SQLParser())) + log.Warningf("killing transaction (exceeded timeout: %v): %s", conn.timeout, conn.String(tp.env.Config().SanitizeLogMessages, tp.env.Environment().Parser())) switch { case conn.IsTainted(): conn.Close() diff --git a/go/vt/vttablet/tabletserver/tx_pool_test.go b/go/vt/vttablet/tabletserver/tx_pool_test.go index 0b41c799711..9edbe5b3592 100644 --- a/go/vt/vttablet/tabletserver/tx_pool_test.go +++ b/go/vt/vttablet/tabletserver/tx_pool_test.go @@ -23,12 +23,10 @@ import ( "testing" "time" - "vitess.io/vitess/go/mysql/collations" - "vitess.io/vitess/go/mysql/config" "vitess.io/vitess/go/vt/callerid" "vitess.io/vitess/go/vt/dbconfigs" vtrpcpb "vitess.io/vitess/go/vt/proto/vtrpc" - "vitess.io/vitess/go/vt/sqlparser" + "vitess.io/vitess/go/vt/vtenv" "vitess.io/vitess/go/vt/vterrors" "vitess.io/vitess/go/vt/vttablet/tabletserver/tx" @@ -826,7 +824,7 @@ func newEnv(exporterName string) tabletenv.Env { cfg.OltpReadPool.IdleTimeout = 30 * time.Second cfg.OlapReadPool.IdleTimeout = 30 * time.Second cfg.TxPool.IdleTimeout = 30 * time.Second - env := tabletenv.NewEnv(cfg, exporterName, collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion) + env := tabletenv.NewEnv(vtenv.NewTestEnv(), cfg, exporterName) return env } diff --git a/go/vt/vttablet/tabletserver/txlimiter/tx_limiter_test.go b/go/vt/vttablet/tabletserver/txlimiter/tx_limiter_test.go index dd58fad7a40..46c95193f6f 100644 --- a/go/vt/vttablet/tabletserver/txlimiter/tx_limiter_test.go +++ b/go/vt/vttablet/tabletserver/txlimiter/tx_limiter_test.go @@ -19,10 +19,8 @@ package txlimiter import ( "testing" - "vitess.io/vitess/go/mysql/collations" - "vitess.io/vitess/go/mysql/config" "vitess.io/vitess/go/vt/callerid" - "vitess.io/vitess/go/vt/sqlparser" + "vitess.io/vitess/go/vt/vtenv" "vitess.io/vitess/go/vt/vttablet/tabletserver/tabletenv" querypb "vitess.io/vitess/go/vt/proto/query" @@ -50,7 +48,7 @@ func TestTxLimiter_DisabledAllowsAll(t *testing.T) { cfg.TransactionLimitByPrincipal = false cfg.TransactionLimitByComponent = false cfg.TransactionLimitBySubcomponent = false - limiter := New(tabletenv.NewEnv(cfg, "TabletServerTest", collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion)) + limiter := New(tabletenv.NewEnv(vtenv.NewTestEnv(), cfg, "TabletServerTest")) im, ef := createCallers("", "", "", "") for i := 0; i < 5; i++ { if got, want := limiter.Get(im, ef), true; got != want { @@ -72,7 +70,7 @@ func TestTxLimiter_LimitsOnlyOffendingUser(t *testing.T) { cfg.TransactionLimitBySubcomponent = false // This should allow 3 slots to all users - newlimiter := New(tabletenv.NewEnv(cfg, "TabletServerTest", collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion)) + newlimiter := New(tabletenv.NewEnv(vtenv.NewTestEnv(), cfg, "TabletServerTest")) limiter, ok := newlimiter.(*Impl) if !ok { t.Fatalf("New returned limiter of unexpected type: got %T, want %T", newlimiter, limiter) @@ -138,7 +136,7 @@ func TestTxLimiterDryRun(t *testing.T) { cfg.TransactionLimitBySubcomponent = false // This should allow 3 slots to all users - newlimiter := New(tabletenv.NewEnv(cfg, "TabletServerTest", collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion)) + newlimiter := New(tabletenv.NewEnv(vtenv.NewTestEnv(), cfg, "TabletServerTest")) limiter, ok := newlimiter.(*Impl) if !ok { t.Fatalf("New returned limiter of unexpected type: got %T, want %T", newlimiter, limiter) diff --git a/go/vt/vttablet/tabletserver/txserializer/tx_serializer_test.go b/go/vt/vttablet/tabletserver/txserializer/tx_serializer_test.go index 61fbe0ab86c..e1b4b5a7612 100644 --- a/go/vt/vttablet/tabletserver/txserializer/tx_serializer_test.go +++ b/go/vt/vttablet/tabletserver/txserializer/tx_serializer_test.go @@ -26,10 +26,8 @@ import ( "testing" "time" - "vitess.io/vitess/go/mysql/collations" - "vitess.io/vitess/go/mysql/config" "vitess.io/vitess/go/streamlog" - "vitess.io/vitess/go/vt/sqlparser" + "vitess.io/vitess/go/vt/vtenv" "vitess.io/vitess/go/vt/vterrors" "vitess.io/vitess/go/vt/vttablet/tabletserver/tabletenv" @@ -50,7 +48,7 @@ func TestTxSerializer_NoHotRow(t *testing.T) { cfg.HotRowProtection.MaxQueueSize = 1 cfg.HotRowProtection.MaxGlobalQueueSize = 1 cfg.HotRowProtection.MaxConcurrency = 5 - txs := New(tabletenv.NewEnv(cfg, "TxSerializerTest", collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion)) + txs := New(tabletenv.NewEnv(vtenv.NewTestEnv(), cfg, "TxSerializerTest")) resetVariables(txs) done, waited, err := txs.Wait(context.Background(), "t1 where1", "t1") @@ -82,7 +80,7 @@ func TestTxSerializerRedactDebugUI(t *testing.T) { cfg.HotRowProtection.MaxQueueSize = 1 cfg.HotRowProtection.MaxGlobalQueueSize = 1 cfg.HotRowProtection.MaxConcurrency = 5 - txs := New(tabletenv.NewEnv(cfg, "TxSerializerTest", collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion)) + txs := New(tabletenv.NewEnv(vtenv.NewTestEnv(), cfg, "TxSerializerTest")) resetVariables(txs) done, waited, err := txs.Wait(context.Background(), "t1 where1", "t1") @@ -106,7 +104,7 @@ func TestTxSerializerRedactDebugUI(t *testing.T) { func TestKeySanitization(t *testing.T) { cfg := tabletenv.NewDefaultConfig() - txs := New(tabletenv.NewEnv(cfg, "TxSerializerTest", collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion)) + txs := New(tabletenv.NewEnv(vtenv.NewTestEnv(), cfg, "TxSerializerTest")) // with a where clause key := "t1 where c1='foo'" want := "t1 ... [REDACTED]" @@ -128,7 +126,7 @@ func TestTxSerializer(t *testing.T) { cfg.HotRowProtection.MaxQueueSize = 2 cfg.HotRowProtection.MaxGlobalQueueSize = 3 cfg.HotRowProtection.MaxConcurrency = 1 - txs := New(tabletenv.NewEnv(cfg, "TxSerializerTest", collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion)) + txs := New(tabletenv.NewEnv(vtenv.NewTestEnv(), cfg, "TxSerializerTest")) resetVariables(txs) // tx1. @@ -201,7 +199,7 @@ func TestTxSerializer_ConcurrentTransactions(t *testing.T) { cfg.HotRowProtection.MaxQueueSize = 3 cfg.HotRowProtection.MaxGlobalQueueSize = 3 cfg.HotRowProtection.MaxConcurrency = 2 - txs := New(tabletenv.NewEnv(cfg, "TxSerializerTest", collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion)) + txs := New(tabletenv.NewEnv(vtenv.NewTestEnv(), cfg, "TxSerializerTest")) resetVariables(txs) // tx1. @@ -324,7 +322,7 @@ func TestTxSerializerCancel(t *testing.T) { cfg.HotRowProtection.MaxQueueSize = 4 cfg.HotRowProtection.MaxGlobalQueueSize = 4 cfg.HotRowProtection.MaxConcurrency = 2 - txs := New(tabletenv.NewEnv(cfg, "TxSerializerTest", collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion)) + txs := New(tabletenv.NewEnv(vtenv.NewTestEnv(), cfg, "TxSerializerTest")) resetVariables(txs) // tx3 and tx4 will record their number once they're done waiting. @@ -425,7 +423,7 @@ func TestTxSerializerDryRun(t *testing.T) { cfg.HotRowProtection.MaxQueueSize = 1 cfg.HotRowProtection.MaxGlobalQueueSize = 2 cfg.HotRowProtection.MaxConcurrency = 1 - txs := New(tabletenv.NewEnv(cfg, "TxSerializerTest", collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion)) + txs := New(tabletenv.NewEnv(vtenv.NewTestEnv(), cfg, "TxSerializerTest")) resetVariables(txs) // tx1. @@ -495,7 +493,7 @@ func TestTxSerializerGlobalQueueOverflow(t *testing.T) { cfg.HotRowProtection.MaxQueueSize = 1 cfg.HotRowProtection.MaxGlobalQueueSize = 1 cfg.HotRowProtection.MaxConcurrency = 1 - txs := New(tabletenv.NewEnv(cfg, "TxSerializerTest", collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion)) + txs := New(tabletenv.NewEnv(vtenv.NewTestEnv(), cfg, "TxSerializerTest")) // tx1. done1, waited1, err1 := txs.Wait(context.Background(), "t1 where1", "t1") @@ -536,7 +534,7 @@ func TestTxSerializerPending(t *testing.T) { cfg.HotRowProtection.MaxQueueSize = 1 cfg.HotRowProtection.MaxGlobalQueueSize = 1 cfg.HotRowProtection.MaxConcurrency = 1 - txs := New(tabletenv.NewEnv(cfg, "TxSerializerTest", collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion)) + txs := New(tabletenv.NewEnv(vtenv.NewTestEnv(), cfg, "TxSerializerTest")) if got, want := txs.Pending("t1 where1"), 0; got != want { t.Errorf("there should be no pending transaction: got = %v, want = %v", got, want) } @@ -547,7 +545,7 @@ func BenchmarkTxSerializer_NoHotRow(b *testing.B) { cfg.HotRowProtection.MaxQueueSize = 1 cfg.HotRowProtection.MaxGlobalQueueSize = 1 cfg.HotRowProtection.MaxConcurrency = 5 - txs := New(tabletenv.NewEnv(cfg, "TxSerializerTest", collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion)) + txs := New(tabletenv.NewEnv(vtenv.NewTestEnv(), cfg, "TxSerializerTest")) b.ResetTimer() diff --git a/go/vt/vttablet/tabletserver/txthrottler/tx_throttler_test.go b/go/vt/vttablet/tabletserver/txthrottler/tx_throttler_test.go index 93b4c106cd5..8099901af60 100644 --- a/go/vt/vttablet/tabletserver/txthrottler/tx_throttler_test.go +++ b/go/vt/vttablet/tabletserver/txthrottler/tx_throttler_test.go @@ -28,14 +28,12 @@ import ( "github.com/stretchr/testify/assert" "go.uber.org/mock/gomock" - "vitess.io/vitess/go/mysql/collations" - "vitess.io/vitess/go/mysql/config" "vitess.io/vitess/go/vt/discovery" - "vitess.io/vitess/go/vt/sqlparser" "vitess.io/vitess/go/vt/throttler" "vitess.io/vitess/go/vt/topo" "vitess.io/vitess/go/vt/topo/memorytopo" "vitess.io/vitess/go/vt/topo/topoproto" + "vitess.io/vitess/go/vt/vtenv" "vitess.io/vitess/go/vt/vttablet/tabletserver/tabletenv" querypb "vitess.io/vitess/go/vt/proto/query" @@ -45,7 +43,7 @@ import ( func TestDisabledThrottler(t *testing.T) { cfg := tabletenv.NewDefaultConfig() cfg.EnableTxThrottler = false - env := tabletenv.NewEnv(cfg, t.Name(), collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion) + env := tabletenv.NewEnv(vtenv.NewTestEnv(), cfg, t.Name()) throttler := NewTxThrottler(env, nil) throttler.InitDBConfig(&querypb.Target{ Keyspace: "keyspace", @@ -109,7 +107,7 @@ func TestEnabledThrottler(t *testing.T) { cfg.EnableTxThrottler = true cfg.TxThrottlerTabletTypes = &topoproto.TabletTypeListFlag{topodatapb.TabletType_REPLICA} - env := tabletenv.NewEnv(cfg, t.Name(), collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion) + env := tabletenv.NewEnv(vtenv.NewTestEnv(), cfg, t.Name()) throttler := NewTxThrottler(env, ts) throttlerImpl, _ := throttler.(*txThrottler) assert.NotNil(t, throttlerImpl) @@ -172,7 +170,7 @@ func TestFetchKnownCells(t *testing.T) { func TestDryRunThrottler(t *testing.T) { cfg := tabletenv.NewDefaultConfig() - env := tabletenv.NewEnv(cfg, t.Name(), collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion) + env := tabletenv.NewEnv(vtenv.NewTestEnv(), cfg, t.Name()) testCases := []struct { Name string diff --git a/go/vt/vttablet/tabletserver/vstreamer/engine.go b/go/vt/vttablet/tabletserver/vstreamer/engine.go index 977d0453513..398f7b4e27e 100644 --- a/go/vt/vttablet/tabletserver/vstreamer/engine.go +++ b/go/vt/vttablet/tabletserver/vstreamer/engine.go @@ -432,7 +432,7 @@ func (vse *Engine) setWatch() { } var vschema *vindexes.VSchema if v != nil { - vschema = vindexes.BuildVSchema(v, vse.env.SQLParser()) + vschema = vindexes.BuildVSchema(v, vse.env.Environment().Parser()) if err != nil { log.Errorf("Error building vschema: %v", err) vse.vschemaErrors.Add(1) diff --git a/go/vt/vttablet/tabletserver/vstreamer/main_flaky_test.go b/go/vt/vttablet/tabletserver/vstreamer/main_flaky_test.go index 7780a8a9e30..d4b8e62341a 100644 --- a/go/vt/vttablet/tabletserver/vstreamer/main_flaky_test.go +++ b/go/vt/vttablet/tabletserver/vstreamer/main_flaky_test.go @@ -26,10 +26,8 @@ import ( _flag "vitess.io/vitess/go/internal/flag" "vitess.io/vitess/go/mysql" - "vitess.io/vitess/go/mysql/collations" - "vitess.io/vitess/go/mysql/config" "vitess.io/vitess/go/vt/dbconfigs" - "vitess.io/vitess/go/vt/sqlparser" + "vitess.io/vitess/go/vt/vtenv" "vitess.io/vitess/go/vt/vttablet/tabletserver/tabletenv" "vitess.io/vitess/go/vt/vttablet/tabletserver/vstreamer/testenv" ) @@ -96,7 +94,7 @@ func customEngine(t *testing.T, modifier func(mysql.ConnParams) mysql.ConnParams cfg := env.TabletEnv.Config().Clone() cfg.DB = dbconfigs.NewTestDBConfigs(modified, modified, modified.DbName) - engine := NewEngine(tabletenv.NewEnv(cfg, "VStreamerTest", collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion), env.SrvTopo, env.SchemaEngine, nil, env.Cells[0]) + engine := NewEngine(tabletenv.NewEnv(vtenv.NewTestEnv(), cfg, "VStreamerTest"), env.SrvTopo, env.SchemaEngine, nil, env.Cells[0]) engine.InitDBConfig(env.KeyspaceName, env.ShardName) engine.Open() return engine diff --git a/go/vt/vttablet/tabletserver/vstreamer/planbuilder.go b/go/vt/vttablet/tabletserver/vstreamer/planbuilder.go index bcf27607839..5f4f9ea7fb7 100644 --- a/go/vt/vttablet/tabletserver/vstreamer/planbuilder.go +++ b/go/vt/vttablet/tabletserver/vstreamer/planbuilder.go @@ -29,6 +29,7 @@ import ( "vitess.io/vitess/go/vt/key" "vitess.io/vitess/go/vt/log" "vitess.io/vitess/go/vt/sqlparser" + "vitess.io/vitess/go/vt/vtenv" "vitess.io/vitess/go/vt/vterrors" "vitess.io/vitess/go/vt/vtgate/evalengine" "vitess.io/vitess/go/vt/vtgate/vindexes" @@ -57,8 +58,7 @@ type Plan struct { // of the table. Filters []Filter - collationEnv *collations.Environment - mysqlVersion string + env *vtenv.Environment } // Opcode enumerates the operators supported in a where clause @@ -231,7 +231,7 @@ func (plan *Plan) filter(values, result []sqltypes.Value, charsets []collations. return false, nil } default: - match, err := compare(filter.Opcode, values[filter.ColNum], filter.Value, plan.collationEnv, charsets[filter.ColNum]) + match, err := compare(filter.Opcode, values[filter.ColNum], filter.Value, plan.env.CollationEnv(), charsets[filter.ColNum]) if err != nil { return false, err } @@ -347,7 +347,7 @@ func tableMatches(table sqlparser.TableName, dbname string, filter *binlogdatapb return ruleMatches(table.Name.String(), filter) } -func buildPlan(ti *Table, vschema *localVSchema, filter *binlogdatapb.Filter, collationEnv *collations.Environment, parser *sqlparser.Parser, mysqlVersion string) (*Plan, error) { +func buildPlan(env *vtenv.Environment, ti *Table, vschema *localVSchema, filter *binlogdatapb.Filter) (*Plan, error) { for _, rule := range filter.Rules { switch { case strings.HasPrefix(rule.Match, "/"): @@ -359,9 +359,9 @@ func buildPlan(ti *Table, vschema *localVSchema, filter *binlogdatapb.Filter, co if !result { continue } - return buildREPlan(ti, vschema, rule.Filter, collationEnv, mysqlVersion) + return buildREPlan(env, ti, vschema, rule.Filter) case rule.Match == ti.Name: - return buildTablePlan(ti, vschema, rule.Filter, collationEnv, parser, mysqlVersion) + return buildTablePlan(env, ti, vschema, rule.Filter) } } return nil, nil @@ -369,11 +369,10 @@ func buildPlan(ti *Table, vschema *localVSchema, filter *binlogdatapb.Filter, co // buildREPlan handles cases where Match has a regular expression. // If so, the Filter can be an empty string or a keyrange, like "-80". -func buildREPlan(ti *Table, vschema *localVSchema, filter string, collationEnv *collations.Environment, mysqlVersion string) (*Plan, error) { +func buildREPlan(env *vtenv.Environment, ti *Table, vschema *localVSchema, filter string) (*Plan, error) { plan := &Plan{ - Table: ti, - collationEnv: collationEnv, - mysqlVersion: mysqlVersion, + env: env, + Table: ti, } plan.ColExprs = make([]ColExpr, len(ti.Fields)) for i, col := range ti.Fields { @@ -414,8 +413,8 @@ func buildREPlan(ti *Table, vschema *localVSchema, filter string, collationEnv * // BuildTablePlan handles cases where a specific table name is specified. // The filter must be a select statement. -func buildTablePlan(ti *Table, vschema *localVSchema, query string, collationEnv *collations.Environment, parser *sqlparser.Parser, mysqlVersion string) (*Plan, error) { - sel, fromTable, err := analyzeSelect(query, parser) +func buildTablePlan(env *vtenv.Environment, ti *Table, vschema *localVSchema, query string) (*Plan, error) { + sel, fromTable, err := analyzeSelect(query, env.Parser()) if err != nil { log.Errorf("%s", err.Error()) return nil, err @@ -426,9 +425,8 @@ func buildTablePlan(ti *Table, vschema *localVSchema, query string, collationEnv } plan := &Plan{ - Table: ti, - collationEnv: collationEnv, - mysqlVersion: mysqlVersion, + Table: ti, + env: env, } if err := plan.analyzeWhere(vschema, sel.Where); err != nil { log.Errorf("%s", err.Error()) @@ -540,14 +538,13 @@ func (plan *Plan) analyzeWhere(vschema *localVSchema, where *sqlparser.Where) er return fmt.Errorf("unexpected: %v", sqlparser.String(expr)) } pv, err := evalengine.Translate(val, &evalengine.Config{ - Collation: plan.collationEnv.DefaultConnectionCharset(), - CollationEnv: plan.collationEnv, - MySQLVersion: plan.mysqlVersion, + Collation: plan.env.CollationEnv().DefaultConnectionCharset(), + Environment: plan.env, }) if err != nil { return err } - env := evalengine.EmptyExpressionEnv(plan.collationEnv, plan.mysqlVersion) + env := evalengine.EmptyExpressionEnv(plan.env) resolved, err := env.Evaluate(pv) if err != nil { return err @@ -555,7 +552,7 @@ func (plan *Plan) analyzeWhere(vschema *localVSchema, where *sqlparser.Where) er plan.Filters = append(plan.Filters, Filter{ Opcode: opcode, ColNum: colnum, - Value: resolved.Value(plan.collationEnv.DefaultConnectionCharset()), + Value: resolved.Value(plan.env.CollationEnv().DefaultConnectionCharset()), }) case *sqlparser.FuncExpr: if !expr.Name.EqualString("in_keyrange") { diff --git a/go/vt/vttablet/tabletserver/vstreamer/planbuilder_test.go b/go/vt/vttablet/tabletserver/vstreamer/planbuilder_test.go index a98f36a282c..19fa5ee06a2 100644 --- a/go/vt/vttablet/tabletserver/vstreamer/planbuilder_test.go +++ b/go/vt/vttablet/tabletserver/vstreamer/planbuilder_test.go @@ -26,11 +26,11 @@ import ( "vitess.io/vitess/go/json2" "vitess.io/vitess/go/mysql" "vitess.io/vitess/go/mysql/collations" - "vitess.io/vitess/go/mysql/config" "vitess.io/vitess/go/sqltypes" "vitess.io/vitess/go/test/utils" "vitess.io/vitess/go/vt/proto/topodata" "vitess.io/vitess/go/vt/sqlparser" + "vitess.io/vitess/go/vt/vtenv" "vitess.io/vitess/go/vt/vtgate/vindexes" binlogdatapb "vitess.io/vitess/go/vt/proto/binlogdata" @@ -258,8 +258,7 @@ func TestPlanBuilder(t *testing.T) { Flags: uint32(querypb.MySqlFlag_BINARY_FLAG), }, }}, - collationEnv: collations.MySQL8(), - mysqlVersion: config.DefaultMySQLVersion, + env: vtenv.NewTestEnv(), }, }, { inTable: t1, @@ -290,8 +289,7 @@ func TestPlanBuilder(t *testing.T) { VindexColumns: []int{0}, KeyRange: nil, }}, - collationEnv: collations.MySQL8(), - mysqlVersion: config.DefaultMySQLVersion, + env: vtenv.NewTestEnv(), }, }, { inTable: t1, @@ -314,8 +312,7 @@ func TestPlanBuilder(t *testing.T) { Flags: uint32(querypb.MySqlFlag_BINARY_FLAG), }, }}, - collationEnv: collations.MySQL8(), - mysqlVersion: config.DefaultMySQLVersion, + env: vtenv.NewTestEnv(), }, }, { inTable: t1, @@ -338,8 +335,7 @@ func TestPlanBuilder(t *testing.T) { Flags: uint32(querypb.MySqlFlag_BINARY_FLAG), }, }}, - collationEnv: collations.MySQL8(), - mysqlVersion: config.DefaultMySQLVersion, + env: vtenv.NewTestEnv(), }, }, { inTable: t1, @@ -362,8 +358,7 @@ func TestPlanBuilder(t *testing.T) { Flags: uint32(querypb.MySqlFlag_NUM_FLAG), }, }}, - collationEnv: collations.MySQL8(), - mysqlVersion: config.DefaultMySQLVersion, + env: vtenv.NewTestEnv(), }, }, { inTable: t1, @@ -394,8 +389,7 @@ func TestPlanBuilder(t *testing.T) { VindexColumns: []int{0}, KeyRange: nil, }}, - collationEnv: collations.MySQL8(), - mysqlVersion: config.DefaultMySQLVersion, + env: vtenv.NewTestEnv(), }, }, { inTable: t1, @@ -426,8 +420,7 @@ func TestPlanBuilder(t *testing.T) { VindexColumns: []int{0}, KeyRange: nil, }}, - collationEnv: collations.MySQL8(), - mysqlVersion: config.DefaultMySQLVersion, + env: vtenv.NewTestEnv(), }, }, { inTable: t1, @@ -458,8 +451,7 @@ func TestPlanBuilder(t *testing.T) { VindexColumns: nil, KeyRange: nil, }}, - collationEnv: collations.MySQL8(), - mysqlVersion: config.DefaultMySQLVersion, + env: vtenv.NewTestEnv(), }, }, { inTable: t2, @@ -493,8 +485,7 @@ func TestPlanBuilder(t *testing.T) { VindexColumns: []int{0, 1}, KeyRange: nil, }}, - collationEnv: collations.MySQL8(), - mysqlVersion: config.DefaultMySQLVersion, + env: vtenv.NewTestEnv(), }, }, { inTable: t1, @@ -518,8 +509,7 @@ func TestPlanBuilder(t *testing.T) { }, }}, convertUsingUTF8Columns: map[string]bool{"val": true}, - collationEnv: collations.MySQL8(), - mysqlVersion: config.DefaultMySQLVersion, + env: vtenv.NewTestEnv(), }, }, { inTable: regional, @@ -543,8 +533,7 @@ func TestPlanBuilder(t *testing.T) { Vindex: testLocalVSchema.vschema.Keyspaces["ks"].Vindexes["region_vdx"], VindexColumns: []int{0, 1}, }}, - collationEnv: collations.MySQL8(), - mysqlVersion: config.DefaultMySQLVersion, + env: vtenv.NewTestEnv(), }, }, { inTable: t1, @@ -655,9 +644,9 @@ func TestPlanBuilder(t *testing.T) { }} for _, tcase := range testcases { t.Run(tcase.inRule.String(), func(t *testing.T) { - plan, err := buildPlan(tcase.inTable, testLocalVSchema, &binlogdatapb.Filter{ + plan, err := buildPlan(vtenv.NewTestEnv(), tcase.inTable, testLocalVSchema, &binlogdatapb.Filter{ Rules: []*binlogdatapb.Rule{tcase.inRule}, - }, collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion) + }) if tcase.outErr != "" { assert.Nil(t, plan) @@ -752,9 +741,9 @@ func TestPlanBuilderFilterComparison(t *testing.T) { for _, tcase := range testcases { t.Run(tcase.name, func(t *testing.T) { - plan, err := buildPlan(t1, testLocalVSchema, &binlogdatapb.Filter{ + plan, err := buildPlan(vtenv.NewTestEnv(), t1, testLocalVSchema, &binlogdatapb.Filter{ Rules: []*binlogdatapb.Rule{{Match: "t1", Filter: tcase.inFilter}}, - }, collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion) + }) if tcase.outErr != "" { assert.Nil(t, plan) diff --git a/go/vt/vttablet/tabletserver/vstreamer/resultstreamer.go b/go/vt/vttablet/tabletserver/vstreamer/resultstreamer.go index 88084d62a50..834b8b88378 100644 --- a/go/vt/vttablet/tabletserver/vstreamer/resultstreamer.go +++ b/go/vt/vttablet/tabletserver/vstreamer/resultstreamer.go @@ -62,7 +62,7 @@ func (rs *resultStreamer) Cancel() { } func (rs *resultStreamer) Stream() error { - _, fromTable, err := analyzeSelect(rs.query, rs.vse.env.SQLParser()) + _, fromTable, err := analyzeSelect(rs.query, rs.vse.env.Environment().Parser()) if err != nil { return err } diff --git a/go/vt/vttablet/tabletserver/vstreamer/rowstreamer.go b/go/vt/vttablet/tabletserver/vstreamer/rowstreamer.go index cd6d1ef5246..712248d7470 100644 --- a/go/vt/vttablet/tabletserver/vstreamer/rowstreamer.go +++ b/go/vt/vttablet/tabletserver/vstreamer/rowstreamer.go @@ -140,7 +140,7 @@ func (rs *rowStreamer) Stream() error { func (rs *rowStreamer) buildPlan() error { // This pre-parsing is required to extract the table name // and create its metadata. - sel, fromTable, err := analyzeSelect(rs.query, rs.se.SQLParser()) + sel, fromTable, err := analyzeSelect(rs.query, rs.se.Environment().Parser()) if err != nil { return err } @@ -177,7 +177,7 @@ func (rs *rowStreamer) buildPlan() error { // This is because the row format of a read is identical // to the row format of a binlog event. So, the same // filtering will work. - rs.plan, err = buildTablePlan(ti, rs.vschema, rs.query, rs.se.CollationEnv(), rs.se.SQLParser(), rs.se.MySQLVersion()) + rs.plan, err = buildTablePlan(rs.se.Environment(), ti, rs.vschema, rs.query) if err != nil { log.Errorf("%s", err.Error()) return err diff --git a/go/vt/vttablet/tabletserver/vstreamer/testenv/testenv.go b/go/vt/vttablet/tabletserver/vstreamer/testenv/testenv.go index 2263b4b714f..c056ef1d7e1 100644 --- a/go/vt/vttablet/tabletserver/vstreamer/testenv/testenv.go +++ b/go/vt/vttablet/tabletserver/vstreamer/testenv/testenv.go @@ -26,14 +26,12 @@ import ( "strings" "vitess.io/vitess/go/json2" - "vitess.io/vitess/go/mysql/collations" - "vitess.io/vitess/go/mysql/config" "vitess.io/vitess/go/vt/dbconfigs" "vitess.io/vitess/go/vt/mysqlctl" - "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/vtenv" "vitess.io/vitess/go/vt/vttablet/tabletserver/schema" "vitess.io/vitess/go/vt/vttablet/tabletserver/tabletenv" "vitess.io/vitess/go/vt/vttest" @@ -111,7 +109,7 @@ func Init(ctx context.Context) (*Env, error) { te.Dbcfgs = dbconfigs.NewTestDBConfigs(te.cluster.MySQLConnParams(), te.cluster.MySQLAppDebugConnParams(), te.cluster.DbName()) conf := tabletenv.NewDefaultConfig() conf.DB = te.Dbcfgs - te.TabletEnv = tabletenv.NewEnv(conf, "VStreamerTest", collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion) + te.TabletEnv = tabletenv.NewEnv(vtenv.NewTestEnv(), conf, "VStreamerTest") te.Mysqld = mysqlctl.NewMysqld(te.Dbcfgs) pos, _ := te.Mysqld.PrimaryPosition() if strings.HasPrefix(strings.ToLower(pos.GTIDSet.Flavor()), string(mysqlctl.FlavorMariaDB)) { diff --git a/go/vt/vttablet/tabletserver/vstreamer/vstreamer.go b/go/vt/vttablet/tabletserver/vstreamer/vstreamer.go index eac4f04ab24..9c63f8a499c 100644 --- a/go/vt/vttablet/tabletserver/vstreamer/vstreamer.go +++ b/go/vt/vttablet/tabletserver/vstreamer/vstreamer.go @@ -503,7 +503,7 @@ func (vs *vstreamer) parseEvent(ev mysql.BinlogEvent) ([]*binlogdatapb.VEvent, e Type: binlogdatapb.VEventType_COMMIT, }) case sqlparser.StmtDDL: - if mustSendDDL(q, vs.cp.DBName(), vs.filter, vs.vse.env.SQLParser()) { + if mustSendDDL(q, vs.cp.DBName(), vs.filter, vs.vse.env.Environment().Parser()) { vevents = append(vevents, &binlogdatapb.VEvent{ Type: binlogdatapb.VEventType_GTID, Gtid: replication.EncodePosition(vs.pos), @@ -520,7 +520,7 @@ func (vs *vstreamer) parseEvent(ev mysql.BinlogEvent) ([]*binlogdatapb.VEvent, e Type: binlogdatapb.VEventType_OTHER, }) } - if schema.MustReloadSchemaOnDDL(q.SQL, vs.cp.DBName(), vs.vse.env.SQLParser()) { + if schema.MustReloadSchemaOnDDL(q.SQL, vs.cp.DBName(), vs.vse.env.Environment().Parser()) { vs.se.ReloadAt(context.Background(), vs.pos) } case sqlparser.StmtSavepoint: @@ -682,7 +682,7 @@ func (vs *vstreamer) buildJournalPlan(id uint64, tm *mysql.TableMap) error { // Build a normal table plan, which means, return all rows // and columns as is. Special handling is done when we actually // receive the row event. We'll build a JOURNAL event instead. - plan, err := buildREPlan(table, nil, "", vs.se.CollationEnv(), vs.se.MySQLVersion()) + plan, err := buildREPlan(vs.se.Environment(), table, nil, "") if err != nil { return err } @@ -716,7 +716,7 @@ func (vs *vstreamer) buildVersionPlan(id uint64, tm *mysql.TableMap) error { // Build a normal table plan, which means, return all rows // and columns as is. Special handling is done when we actually // receive the row event. We'll build a JOURNAL event instead. - plan, err := buildREPlan(table, nil, "", vs.se.CollationEnv(), vs.se.MySQLVersion()) + plan, err := buildREPlan(vs.se.Environment(), table, nil, "") if err != nil { return err } @@ -738,7 +738,7 @@ func (vs *vstreamer) buildTablePlan(id uint64, tm *mysql.TableMap) (*binlogdatap Name: tm.Name, Fields: cols, } - plan, err := buildPlan(table, vs.vschema, vs.filter, vs.se.CollationEnv(), vs.se.SQLParser(), vs.se.MySQLVersion()) + plan, err := buildPlan(vs.se.Environment(), table, vs.vschema, vs.filter) if err != nil { return nil, err } @@ -768,7 +768,7 @@ func (vs *vstreamer) buildTableColumns(tm *mysql.TableMap) ([]*querypb.Field, er if err != nil { return nil, fmt.Errorf("unsupported type: %d, position: %d", typ, i) } - coll := collations.CollationForType(t, vs.se.CollationEnv().DefaultConnectionCharset()) + coll := collations.CollationForType(t, vs.se.Environment().CollationEnv().DefaultConnectionCharset()) fields = append(fields, &querypb.Field{ Name: fmt.Sprintf("@%d", i+1), Type: t, @@ -957,7 +957,7 @@ func (vs *vstreamer) rebuildPlans() error { // cause that to change. continue } - newPlan, err := buildPlan(plan.Table, vs.vschema, vs.filter, vs.se.CollationEnv(), vs.se.SQLParser(), vs.se.MySQLVersion()) + newPlan, err := buildPlan(vs.se.Environment(), plan.Table, vs.vschema, vs.filter) if err != nil { return err } diff --git a/go/vt/vttest/local_cluster.go b/go/vt/vttest/local_cluster.go index c7a86420ad3..da19d1c6fb8 100644 --- a/go/vt/vttest/local_cluster.go +++ b/go/vt/vttest/local_cluster.go @@ -35,12 +35,11 @@ import ( "google.golang.org/protobuf/encoding/prototext" "google.golang.org/protobuf/proto" - "vitess.io/vitess/go/vt/sqlparser" + "vitess.io/vitess/go/vt/vtenv" "vitess.io/vitess/go/constants/sidecar" "vitess.io/vitess/go/mysql" "vitess.io/vitess/go/mysql/collations" - "vitess.io/vitess/go/mysql/config" "vitess.io/vitess/go/sqltypes" "vitess.io/vitess/go/vt/log" "vitess.io/vitess/go/vt/proto/logutil" @@ -557,7 +556,7 @@ func (db *LocalCluster) createVTSchema() error { return db.ExecuteFetch(query, "") } - if err := sidecardb.Init(context.Background(), sidecardbExec, collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion); err != nil { + if err := sidecardb.Init(context.Background(), vtenv.NewTestEnv(), sidecardbExec); err != nil { return err } return nil diff --git a/go/vt/wrangler/external_cluster_test.go b/go/vt/wrangler/external_cluster_test.go index d21e370d5cd..9876e2bf999 100644 --- a/go/vt/wrangler/external_cluster_test.go +++ b/go/vt/wrangler/external_cluster_test.go @@ -6,13 +6,11 @@ import ( "github.com/stretchr/testify/require" - "vitess.io/vitess/go/mysql/collations" - "vitess.io/vitess/go/mysql/config" "vitess.io/vitess/go/test/utils" "vitess.io/vitess/go/vt/logutil" "vitess.io/vitess/go/vt/proto/topodata" - "vitess.io/vitess/go/vt/sqlparser" "vitess.io/vitess/go/vt/topo/memorytopo" + "vitess.io/vitess/go/vt/vtenv" ) func TestVitessCluster(t *testing.T) { @@ -20,7 +18,7 @@ func TestVitessCluster(t *testing.T) { defer cancel() ts := memorytopo.NewServer(ctx, "zone1") tmc := newTestWranglerTMClient() - wr := New(logutil.NewConsoleLogger(), ts, tmc, collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion) + wr := New(vtenv.NewTestEnv(), logutil.NewConsoleLogger(), ts, tmc) name, topoType, topoServer, topoRoot := "c1", "x", "y", "z" t.Run("Zero clusters to start", func(t *testing.T) { diff --git a/go/vt/wrangler/fake_tablet_test.go b/go/vt/wrangler/fake_tablet_test.go index bdd805cfe0a..b70a64d644e 100644 --- a/go/vt/wrangler/fake_tablet_test.go +++ b/go/vt/wrangler/fake_tablet_test.go @@ -27,7 +27,6 @@ import ( "google.golang.org/grpc" "vitess.io/vitess/go/mysql/collations" - "vitess.io/vitess/go/mysql/config" "vitess.io/vitess/go/mysql/fakesqldb" "vitess.io/vitess/go/netutil" "vitess.io/vitess/go/vt/dbconfigs" @@ -36,6 +35,7 @@ import ( topodatapb "vitess.io/vitess/go/vt/proto/topodata" "vitess.io/vitess/go/vt/sqlparser" "vitess.io/vitess/go/vt/topo" + "vitess.io/vitess/go/vt/vtenv" "vitess.io/vitess/go/vt/vttablet/grpctmserver" "vitess.io/vitess/go/vt/vttablet/queryservice" "vitess.io/vitess/go/vt/vttablet/queryservice/fakes" @@ -199,9 +199,7 @@ func (ft *fakeTablet) StartActionLoop(t *testing.T, wr *Wrangler) { DBConfigs: &dbconfigs.DBConfigs{}, QueryServiceControl: tabletservermock.NewController(), VDiffEngine: vdiff2.NewEngine(wr.TopoServer(), ft.Tablet, collations.MySQL8(), sqlparser.NewTestParser()), - CollationEnv: collations.MySQL8(), - SQLParser: sqlparser.NewTestParser(), - MySQLVersion: config.DefaultMySQLVersion, + Env: vtenv.NewTestEnv(), } if err := ft.TM.Start(ft.Tablet, nil); err != nil { t.Fatal(err) diff --git a/go/vt/wrangler/materializer.go b/go/vt/wrangler/materializer.go index f2df675e3c1..5b159e35a11 100644 --- a/go/vt/wrangler/materializer.go +++ b/go/vt/wrangler/materializer.go @@ -178,7 +178,7 @@ func (wr *Wrangler) MoveTables(ctx context.Context, workflow, sourceKeyspace, ta return err } if len(tables) > 0 { - err = wr.validateSourceTablesExist(ctx, sourceKeyspace, ksTables, tables) + err = wr.validateSourceTablesExist(sourceKeyspace, ksTables, tables) if err != nil { return err } @@ -193,7 +193,7 @@ func (wr *Wrangler) MoveTables(ctx context.Context, workflow, sourceKeyspace, ta excludeTables = strings.TrimSpace(excludeTables) if excludeTables != "" { excludeTablesList = strings.Split(excludeTables, ",") - err = wr.validateSourceTablesExist(ctx, sourceKeyspace, ksTables, excludeTablesList) + err = wr.validateSourceTablesExist(sourceKeyspace, ksTables, excludeTablesList) if err != nil { return err } @@ -366,7 +366,7 @@ func (wr *Wrangler) MoveTables(ctx context.Context, workflow, sourceKeyspace, ta return nil } -func (wr *Wrangler) validateSourceTablesExist(ctx context.Context, sourceKeyspace string, ksTables, tables []string) error { +func (wr *Wrangler) validateSourceTablesExist(sourceKeyspace string, ksTables, tables []string) error { // validate that tables provided are present in the source keyspace var missingTables []string for _, table := range tables { @@ -445,7 +445,7 @@ func (wr *Wrangler) checkIfPreviousJournalExists(ctx context.Context, mz *materi mu sync.Mutex exists bool tablets []string - ws = workflow.NewServer(wr.ts, wr.tmc, wr.collationEnv, wr.parser, wr.mysqlVersion) + ws = workflow.NewServer(wr.env, wr.ts, wr.tmc) ) err := forAllSources(func(si *topo.ShardInfo) error { @@ -540,7 +540,7 @@ func (wr *Wrangler) prepareCreateLookup(ctx context.Context, keyspace string, sp return nil, nil, nil, fmt.Errorf("vindex %s is not a lookup type", vindex.Type) } - targetKeyspace, targetTableName, err = wr.parser.ParseTable(vindex.Params["table"]) + targetKeyspace, targetTableName, err = wr.env.Parser().ParseTable(vindex.Params["table"]) if err != nil || targetKeyspace == "" { return nil, nil, nil, fmt.Errorf("vindex table name must be in the form .
. Got: %v", vindex.Params["table"]) } @@ -837,7 +837,7 @@ func (wr *Wrangler) ExternalizeVindex(ctx context.Context, qualifiedVindexName s return fmt.Errorf("vindex %s not found in vschema", qualifiedVindexName) } - targetKeyspace, targetTableName, err := wr.parser.ParseTable(sourceVindex.Params["table"]) + targetKeyspace, targetTableName, err := wr.env.Parser().ParseTable(sourceVindex.Params["table"]) if err != nil || targetKeyspace == "" { return fmt.Errorf("vindex table name must be in the form .
. Got: %v", sourceVindex.Params["table"]) } @@ -1064,7 +1064,7 @@ func (wr *Wrangler) buildMaterializer(ctx context.Context, ms *vtctldatapb.Mater if err != nil { return nil, err } - targetVSchema, err := vindexes.BuildKeyspaceSchema(vschema, ms.TargetKeyspace, wr.parser) + targetVSchema, err := vindexes.BuildKeyspaceSchema(vschema, ms.TargetKeyspace, wr.env.Parser()) if err != nil { return nil, err } @@ -1220,7 +1220,7 @@ func (mz *materializer) deploySchema(ctx context.Context) error { if createDDL == createDDLAsCopy || createDDL == createDDLAsCopyDropConstraint || createDDL == createDDLAsCopyDropForeignKeys { if ts.SourceExpression != "" { // Check for table if non-empty SourceExpression. - sourceTableName, err := mz.wr.parser.TableFromStatement(ts.SourceExpression) + sourceTableName, err := mz.wr.env.Parser().TableFromStatement(ts.SourceExpression) if err != nil { return err } @@ -1236,7 +1236,7 @@ func (mz *materializer) deploySchema(ctx context.Context) error { } if createDDL == createDDLAsCopyDropConstraint { - strippedDDL, err := stripTableConstraints(ddl, mz.wr.parser) + strippedDDL, err := stripTableConstraints(ddl, mz.wr.env.Parser()) if err != nil { return err } @@ -1245,7 +1245,7 @@ func (mz *materializer) deploySchema(ctx context.Context) error { } if createDDL == createDDLAsCopyDropForeignKeys { - strippedDDL, err := stripTableForeignKeys(ddl, mz.wr.parser) + strippedDDL, err := stripTableForeignKeys(ddl, mz.wr.env.Parser()) if err != nil { return err } @@ -1266,7 +1266,7 @@ func (mz *materializer) deploySchema(ctx context.Context) error { // We use schemadiff to normalize the schema. // For now, and because this is could have wider implications, we ignore any errors in // reading the source schema. - env := schemadiff.NewEnv(mz.wr.collationEnv, mz.wr.collationEnv.DefaultConnectionCharset(), mz.wr.parser, mz.wr.mysqlVersion) + env := schemadiff.NewEnv(mz.wr.env, mz.wr.env.CollationEnv().DefaultConnectionCharset()) schema, err := schemadiff.NewSchemaFromQueries(env, applyDDLs) if err != nil { log.Error(vterrors.Wrapf(err, "AtomicCopy: failed to normalize schema via schemadiff")) @@ -1368,7 +1368,7 @@ func (mz *materializer) generateInserts(ctx context.Context, sourceShards []*top } // Validate non-empty query. - stmt, err := mz.wr.parser.Parse(ts.SourceExpression) + stmt, err := mz.wr.env.Parser().Parse(ts.SourceExpression) if err != nil { return "", err } diff --git a/go/vt/wrangler/materializer_env_test.go b/go/vt/wrangler/materializer_env_test.go index 157acd2d3f6..3d5797a6bff 100644 --- a/go/vt/wrangler/materializer_env_test.go +++ b/go/vt/wrangler/materializer_env_test.go @@ -30,8 +30,6 @@ import ( "go.uber.org/goleak" - "vitess.io/vitess/go/mysql/collations" - "vitess.io/vitess/go/mysql/config" "vitess.io/vitess/go/sqltypes" "vitess.io/vitess/go/vt/log" "vitess.io/vitess/go/vt/logutil" @@ -39,6 +37,7 @@ import ( "vitess.io/vitess/go/vt/sqlparser" "vitess.io/vitess/go/vt/topo" "vitess.io/vitess/go/vt/topo/memorytopo" + "vitess.io/vitess/go/vt/vtenv" "vitess.io/vitess/go/vt/vttablet/tmclient" _flag "vitess.io/vitess/go/internal/flag" @@ -132,7 +131,7 @@ func newTestMaterializerEnv(t *testing.T, ctx context.Context, ms *vtctldatapb.M tmc: newTestMaterializerTMClient(), } parser := sqlparser.NewTestParser() - env.wr = New(logutil.NewConsoleLogger(), env.topoServ, env.tmc, collations.MySQL8(), parser, config.DefaultMySQLVersion) + env.wr = New(vtenv.NewTestEnv(), logutil.NewConsoleLogger(), env.topoServ, env.tmc) tabletID := 100 for _, shard := range sources { _ = env.addTablet(tabletID, env.ms.SourceKeyspace, shard, topodatapb.TabletType_PRIMARY) diff --git a/go/vt/wrangler/materializer_test.go b/go/vt/wrangler/materializer_test.go index d4895e69084..bdeee1e6ac3 100644 --- a/go/vt/wrangler/materializer_test.go +++ b/go/vt/wrangler/materializer_test.go @@ -30,14 +30,13 @@ import ( "google.golang.org/protobuf/encoding/prototext" "google.golang.org/protobuf/proto" - "vitess.io/vitess/go/mysql/collations" - "vitess.io/vitess/go/mysql/config" "vitess.io/vitess/go/sqltypes" "vitess.io/vitess/go/test/utils" "vitess.io/vitess/go/vt/logutil" "vitess.io/vitess/go/vt/sqlparser" "vitess.io/vitess/go/vt/topo/memorytopo" "vitess.io/vitess/go/vt/topo/topoproto" + "vitess.io/vitess/go/vt/vtenv" "vitess.io/vitess/go/vt/vtgate/vindexes" binlogdatapb "vitess.io/vitess/go/vt/proto/binlogdata" @@ -1544,7 +1543,7 @@ func TestCreateLookupVindexFailures(t *testing.T) { defer cancel() topoServ := memorytopo.NewServer(ctx, "cell") - wr := New(logutil.NewConsoleLogger(), topoServ, nil, collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion) + wr := New(vtenv.NewTestEnv(), logutil.NewConsoleLogger(), topoServ, nil) unique := map[string]*vschemapb.Vindex{ "v": { @@ -2544,7 +2543,7 @@ func TestMaterializerNoSourcePrimary(t *testing.T) { cell: "cell", tmc: newTestMaterializerTMClient(), } - env.wr = New(logutil.NewConsoleLogger(), env.topoServ, env.tmc, collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion) + env.wr = New(vtenv.NewTestEnv(), logutil.NewConsoleLogger(), env.topoServ, env.tmc) defer env.close() tabletID := 100 diff --git a/go/vt/wrangler/reparent.go b/go/vt/wrangler/reparent.go index 79eaf640dfd..98c5821f7c3 100644 --- a/go/vt/wrangler/reparent.go +++ b/go/vt/wrangler/reparent.go @@ -60,7 +60,7 @@ func (wr *Wrangler) InitShardPrimary(ctx context.Context, keyspace, shard string ev := &events.Reparent{} // do the work - err = grpcvtctldserver.NewVtctldServer(wr.ts, wr.collationEnv, wr.parser, wr.mysqlVersion).InitShardPrimaryLocked(ctx, ev, &vtctldatapb.InitShardPrimaryRequest{ + err = grpcvtctldserver.NewVtctldServer(wr.env, wr.ts).InitShardPrimaryLocked(ctx, ev, &vtctldatapb.InitShardPrimaryRequest{ Keyspace: keyspace, Shard: shard, PrimaryElectTabletAlias: primaryElectTabletAlias, diff --git a/go/vt/wrangler/resharder_env_test.go b/go/vt/wrangler/resharder_env_test.go index ca81bded537..3fbdde6f52e 100644 --- a/go/vt/wrangler/resharder_env_test.go +++ b/go/vt/wrangler/resharder_env_test.go @@ -26,14 +26,12 @@ import ( "github.com/stretchr/testify/require" - "vitess.io/vitess/go/mysql/collations" - "vitess.io/vitess/go/mysql/config" "vitess.io/vitess/go/sqltypes" "vitess.io/vitess/go/vt/key" "vitess.io/vitess/go/vt/logutil" - "vitess.io/vitess/go/vt/sqlparser" "vitess.io/vitess/go/vt/topo" "vitess.io/vitess/go/vt/topo/memorytopo" + "vitess.io/vitess/go/vt/vtenv" "vitess.io/vitess/go/vt/vttablet/tmclient" querypb "vitess.io/vitess/go/vt/proto/query" @@ -96,7 +94,7 @@ func newTestResharderEnv(t *testing.T, ctx context.Context, sources, targets []s cell: "cell", tmc: newTestResharderTMClient(), } - env.wr = New(logutil.NewConsoleLogger(), env.topoServ, env.tmc, collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion) + env.wr = New(vtenv.NewTestEnv(), logutil.NewConsoleLogger(), env.topoServ, env.tmc) initTopo(t, env.topoServ, "ks", sources, targets, []string{"cell"}) tabletID := 100 for _, shard := range sources { diff --git a/go/vt/wrangler/tablet_test.go b/go/vt/wrangler/tablet_test.go index 31b641d3ecc..c5ae032fe07 100644 --- a/go/vt/wrangler/tablet_test.go +++ b/go/vt/wrangler/tablet_test.go @@ -21,13 +21,11 @@ import ( "strings" "testing" - "vitess.io/vitess/go/mysql/collations" - "vitess.io/vitess/go/mysql/config" "vitess.io/vitess/go/vt/logutil" topodatapb "vitess.io/vitess/go/vt/proto/topodata" - "vitess.io/vitess/go/vt/sqlparser" "vitess.io/vitess/go/vt/topo" "vitess.io/vitess/go/vt/topo/memorytopo" + "vitess.io/vitess/go/vt/vtenv" ) // TestInitTabletShardConversion makes sure InitTablet converts the @@ -39,7 +37,7 @@ func TestInitTabletShardConversion(t *testing.T) { cell := "cell1" ts := memorytopo.NewServer(ctx, cell) - wr := New(logutil.NewConsoleLogger(), ts, nil, collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion) + wr := New(vtenv.NewTestEnv(), logutil.NewConsoleLogger(), ts, nil) tablet := &topodatapb.Tablet{ Alias: &topodatapb.TabletAlias{ @@ -73,7 +71,7 @@ func TestDeleteTabletBasic(t *testing.T) { cell := "cell1" ts := memorytopo.NewServer(ctx, cell) - wr := New(logutil.NewConsoleLogger(), ts, nil, collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion) + wr := New(vtenv.NewTestEnv(), logutil.NewConsoleLogger(), ts, nil) tablet := &topodatapb.Tablet{ Alias: &topodatapb.TabletAlias{ @@ -105,7 +103,7 @@ func TestDeleteTabletTruePrimary(t *testing.T) { cell := "cell1" ts := memorytopo.NewServer(ctx, cell) - wr := New(logutil.NewConsoleLogger(), ts, nil, collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion) + wr := New(vtenv.NewTestEnv(), logutil.NewConsoleLogger(), ts, nil) tablet := &topodatapb.Tablet{ Alias: &topodatapb.TabletAlias{ @@ -152,7 +150,7 @@ func TestDeleteTabletFalsePrimary(t *testing.T) { cell := "cell1" ts := memorytopo.NewServer(ctx, cell) - wr := New(logutil.NewConsoleLogger(), ts, nil, collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion) + wr := New(vtenv.NewTestEnv(), logutil.NewConsoleLogger(), ts, nil) tablet1 := &topodatapb.Tablet{ Alias: &topodatapb.TabletAlias{ @@ -204,7 +202,7 @@ func TestDeleteTabletShardNonExisting(t *testing.T) { cell := "cell1" ts := memorytopo.NewServer(ctx, cell) - wr := New(logutil.NewConsoleLogger(), ts, nil, collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion) + wr := New(vtenv.NewTestEnv(), logutil.NewConsoleLogger(), ts, nil) tablet := &topodatapb.Tablet{ Alias: &topodatapb.TabletAlias{ diff --git a/go/vt/wrangler/testlib/backup_test.go b/go/vt/wrangler/testlib/backup_test.go index 2ceba682b9b..0de8bfd78f3 100644 --- a/go/vt/wrangler/testlib/backup_test.go +++ b/go/vt/wrangler/testlib/backup_test.go @@ -29,8 +29,6 @@ import ( "vitess.io/vitess/go/mysql" "vitess.io/vitess/go/mysql/capabilities" - "vitess.io/vitess/go/mysql/collations" - "vitess.io/vitess/go/mysql/config" "vitess.io/vitess/go/mysql/fakesqldb" "vitess.io/vitess/go/mysql/replication" "vitess.io/vitess/go/sqltypes" @@ -39,10 +37,10 @@ import ( "vitess.io/vitess/go/vt/mysqlctl" "vitess.io/vitess/go/vt/mysqlctl/backupstorage" "vitess.io/vitess/go/vt/mysqlctl/filebackupstorage" - "vitess.io/vitess/go/vt/sqlparser" "vitess.io/vitess/go/vt/topo" "vitess.io/vitess/go/vt/topo/memorytopo" "vitess.io/vitess/go/vt/topo/topoproto" + "vitess.io/vitess/go/vt/vtenv" "vitess.io/vitess/go/vt/vttablet/tmclient" "vitess.io/vitess/go/vt/wrangler" @@ -94,7 +92,7 @@ func testBackupRestore(t *testing.T, cDetails *compressionDetails) error { db := fakesqldb.New(t) defer db.Close() ts := memorytopo.NewServer(ctx, "cell1", "cell2") - wr := wrangler.New(logutil.NewConsoleLogger(), ts, tmclient.NewTabletManagerClient(), collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion) + wr := wrangler.New(vtenv.NewTestEnv(), logutil.NewConsoleLogger(), ts, tmclient.NewTabletManagerClient()) vp := NewVtctlPipe(t, ts) defer vp.Close() @@ -346,7 +344,7 @@ func TestBackupRestoreLagged(t *testing.T) { db := fakesqldb.New(t) defer db.Close() ts := memorytopo.NewServer(ctx, "cell1", "cell2") - wr := wrangler.New(logutil.NewConsoleLogger(), ts, tmclient.NewTabletManagerClient(), collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion) + wr := wrangler.New(vtenv.NewTestEnv(), logutil.NewConsoleLogger(), ts, tmclient.NewTabletManagerClient()) vp := NewVtctlPipe(t, ts) defer vp.Close() @@ -565,7 +563,7 @@ func TestRestoreUnreachablePrimary(t *testing.T) { db := fakesqldb.New(t) defer db.Close() ts := memorytopo.NewServer(ctx, "cell1") - wr := wrangler.New(logutil.NewConsoleLogger(), ts, tmclient.NewTabletManagerClient(), collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion) + wr := wrangler.New(vtenv.NewTestEnv(), logutil.NewConsoleLogger(), ts, tmclient.NewTabletManagerClient()) vp := NewVtctlPipe(t, ts) defer vp.Close() @@ -740,7 +738,7 @@ func TestDisableActiveReparents(t *testing.T) { db := fakesqldb.New(t) defer db.Close() ts := memorytopo.NewServer(ctx, "cell1", "cell2") - wr := wrangler.New(logutil.NewConsoleLogger(), ts, tmclient.NewTabletManagerClient(), collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion) + wr := wrangler.New(vtenv.NewTestEnv(), logutil.NewConsoleLogger(), ts, tmclient.NewTabletManagerClient()) vp := NewVtctlPipe(t, ts) defer vp.Close() diff --git a/go/vt/wrangler/testlib/copy_schema_shard_test.go b/go/vt/wrangler/testlib/copy_schema_shard_test.go index c0d78716f19..262a93f4e23 100644 --- a/go/vt/wrangler/testlib/copy_schema_shard_test.go +++ b/go/vt/wrangler/testlib/copy_schema_shard_test.go @@ -22,16 +22,14 @@ import ( "testing" "time" - "vitess.io/vitess/go/mysql/collations" - "vitess.io/vitess/go/mysql/config" "vitess.io/vitess/go/mysql/fakesqldb" "vitess.io/vitess/go/sqltypes" "vitess.io/vitess/go/vt/discovery" "vitess.io/vitess/go/vt/logutil" "vitess.io/vitess/go/vt/mysqlctl/tmutils" - "vitess.io/vitess/go/vt/sqlparser" "vitess.io/vitess/go/vt/topo/memorytopo" "vitess.io/vitess/go/vt/topo/topoproto" + "vitess.io/vitess/go/vt/vtenv" "vitess.io/vitess/go/vt/vttablet/tabletmanager/vreplication" "vitess.io/vitess/go/vt/vttablet/tmclient" "vitess.io/vitess/go/vt/wrangler" @@ -58,7 +56,7 @@ func copySchema(t *testing.T, useShardAsSource bool) { ctx, cancel := context.WithCancel(context.Background()) defer cancel() ts := memorytopo.NewServer(ctx, "cell1", "cell2") - wr := wrangler.New(logutil.NewConsoleLogger(), ts, tmclient.NewTabletManagerClient(), collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion) + wr := wrangler.New(vtenv.NewTestEnv(), logutil.NewConsoleLogger(), ts, tmclient.NewTabletManagerClient()) vp := NewVtctlPipe(t, ts) defer vp.Close() diff --git a/go/vt/wrangler/testlib/emergency_reparent_shard_test.go b/go/vt/wrangler/testlib/emergency_reparent_shard_test.go index 428f6dfdbbb..efdca1fc725 100644 --- a/go/vt/wrangler/testlib/emergency_reparent_shard_test.go +++ b/go/vt/wrangler/testlib/emergency_reparent_shard_test.go @@ -26,16 +26,14 @@ import ( "github.com/stretchr/testify/require" "vitess.io/vitess/go/mysql" - "vitess.io/vitess/go/mysql/collations" - "vitess.io/vitess/go/mysql/config" "vitess.io/vitess/go/mysql/replication" "vitess.io/vitess/go/sets" "vitess.io/vitess/go/vt/discovery" "vitess.io/vitess/go/vt/logutil" - "vitess.io/vitess/go/vt/sqlparser" "vitess.io/vitess/go/vt/topo/memorytopo" "vitess.io/vitess/go/vt/topo/topoproto" "vitess.io/vitess/go/vt/vtctl/reparentutil/reparenttestutil" + "vitess.io/vitess/go/vt/vtenv" "vitess.io/vitess/go/vt/vttablet/tmclient" "vitess.io/vitess/go/vt/wrangler" @@ -52,7 +50,7 @@ func TestEmergencyReparentShard(t *testing.T) { ctx, cancel := context.WithCancel(context.Background()) defer cancel() ts := memorytopo.NewServer(ctx, "cell1", "cell2") - wr := wrangler.New(logutil.NewConsoleLogger(), ts, tmclient.NewTabletManagerClient(), collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion) + wr := wrangler.New(vtenv.NewTestEnv(), logutil.NewConsoleLogger(), ts, tmclient.NewTabletManagerClient()) vp := NewVtctlPipe(t, ts) defer vp.Close() @@ -206,7 +204,7 @@ func TestEmergencyReparentShardPrimaryElectNotBest(t *testing.T) { discovery.SetTabletPickerRetryDelay(5 * time.Millisecond) ts := memorytopo.NewServer(ctx, "cell1", "cell2") - wr := wrangler.New(logutil.NewConsoleLogger(), ts, tmclient.NewTabletManagerClient(), collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion) + wr := wrangler.New(vtenv.NewTestEnv(), logutil.NewConsoleLogger(), ts, tmclient.NewTabletManagerClient()) // Create a primary, a couple good replicas oldPrimary := NewFakeTablet(t, wr, "cell1", 0, topodatapb.TabletType_PRIMARY, nil) diff --git a/go/vt/wrangler/testlib/external_reparent_test.go b/go/vt/wrangler/testlib/external_reparent_test.go index bd588947ca7..fdc1ca664ee 100644 --- a/go/vt/wrangler/testlib/external_reparent_test.go +++ b/go/vt/wrangler/testlib/external_reparent_test.go @@ -24,14 +24,12 @@ import ( "github.com/stretchr/testify/assert" - "vitess.io/vitess/go/mysql/collations" - "vitess.io/vitess/go/mysql/config" "vitess.io/vitess/go/vt/discovery" "vitess.io/vitess/go/vt/logutil" - "vitess.io/vitess/go/vt/sqlparser" "vitess.io/vitess/go/vt/topo/memorytopo" "vitess.io/vitess/go/vt/topo/topoproto" "vitess.io/vitess/go/vt/topotools" + "vitess.io/vitess/go/vt/vtenv" "vitess.io/vitess/go/vt/vttablet/tmclient" "vitess.io/vitess/go/vt/wrangler" @@ -52,7 +50,7 @@ func TestTabletExternallyReparentedBasic(t *testing.T) { ctx, cancel := context.WithTimeout(context.Background(), time.Second*30) defer cancel() ts := memorytopo.NewServer(ctx, "cell1") - wr := wrangler.New(logutil.NewConsoleLogger(), ts, tmclient.NewTabletManagerClient(), collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion) + wr := wrangler.New(vtenv.NewTestEnv(), logutil.NewConsoleLogger(), ts, tmclient.NewTabletManagerClient()) vp := NewVtctlPipe(t, ts) defer vp.Close() @@ -145,7 +143,7 @@ func TestTabletExternallyReparentedToReplica(t *testing.T) { ctx, cancel := context.WithTimeout(context.Background(), time.Second*30) defer cancel() ts := memorytopo.NewServer(ctx, "cell1") - wr := wrangler.New(logutil.NewConsoleLogger(), ts, tmclient.NewTabletManagerClient(), collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion) + wr := wrangler.New(vtenv.NewTestEnv(), logutil.NewConsoleLogger(), ts, tmclient.NewTabletManagerClient()) // Create an old primary, a new primary, two good replicas, one bad replica oldPrimary := NewFakeTablet(t, wr, "cell1", 0, topodatapb.TabletType_PRIMARY, nil) @@ -228,7 +226,7 @@ func TestTabletExternallyReparentedWithDifferentMysqlPort(t *testing.T) { ctx, cancel := context.WithTimeout(context.Background(), time.Second*30) defer cancel() ts := memorytopo.NewServer(ctx, "cell1") - wr := wrangler.New(logutil.NewConsoleLogger(), ts, tmclient.NewTabletManagerClient(), collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion) + wr := wrangler.New(vtenv.NewTestEnv(), logutil.NewConsoleLogger(), ts, tmclient.NewTabletManagerClient()) // Create an old primary, a new primary, two good replicas, one bad replica oldPrimary := NewFakeTablet(t, wr, "cell1", 0, topodatapb.TabletType_PRIMARY, nil) @@ -321,7 +319,7 @@ func TestTabletExternallyReparentedContinueOnUnexpectedPrimary(t *testing.T) { ctx, cancel := context.WithTimeout(context.Background(), time.Second*30) defer cancel() ts := memorytopo.NewServer(ctx, "cell1") - wr := wrangler.New(logutil.NewConsoleLogger(), ts, tmclient.NewTabletManagerClient(), collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion) + wr := wrangler.New(vtenv.NewTestEnv(), logutil.NewConsoleLogger(), ts, tmclient.NewTabletManagerClient()) // Create an old primary, a new primary, two good replicas, one bad replica oldPrimary := NewFakeTablet(t, wr, "cell1", 0, topodatapb.TabletType_PRIMARY, nil) @@ -407,7 +405,7 @@ func TestTabletExternallyReparentedRerun(t *testing.T) { ctx, cancel := context.WithTimeout(context.Background(), time.Second*30) defer cancel() ts := memorytopo.NewServer(ctx, "cell1") - wr := wrangler.New(logutil.NewConsoleLogger(), ts, tmclient.NewTabletManagerClient(), collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion) + wr := wrangler.New(vtenv.NewTestEnv(), logutil.NewConsoleLogger(), ts, tmclient.NewTabletManagerClient()) // Create an old primary, a new primary, and a good replica. oldPrimary := NewFakeTablet(t, wr, "cell1", 0, topodatapb.TabletType_PRIMARY, nil) @@ -511,7 +509,7 @@ func TestRPCTabletExternallyReparentedDemotesPrimaryToConfiguredTabletType(t *te ctx, cancel := context.WithTimeout(context.Background(), time.Second*30) defer cancel() ts := memorytopo.NewServer(ctx, "cell1") - wr := wrangler.New(logutil.NewConsoleLogger(), ts, tmclient.NewTabletManagerClient(), collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion) + wr := wrangler.New(vtenv.NewTestEnv(), logutil.NewConsoleLogger(), ts, tmclient.NewTabletManagerClient()) // Create an old primary and a new primary oldPrimary := NewFakeTablet(t, wr, "cell1", 0, topodatapb.TabletType_SPARE, nil) diff --git a/go/vt/wrangler/testlib/fake_tablet.go b/go/vt/wrangler/testlib/fake_tablet.go index eee29d0f838..97d74edf3f7 100644 --- a/go/vt/wrangler/testlib/fake_tablet.go +++ b/go/vt/wrangler/testlib/fake_tablet.go @@ -29,15 +29,13 @@ import ( "google.golang.org/grpc" - "vitess.io/vitess/go/mysql/collations" - "vitess.io/vitess/go/mysql/config" "vitess.io/vitess/go/mysql/fakesqldb" "vitess.io/vitess/go/vt/binlog/binlogplayer" "vitess.io/vitess/go/vt/dbconfigs" "vitess.io/vitess/go/vt/mysqlctl" - "vitess.io/vitess/go/vt/sqlparser" "vitess.io/vitess/go/vt/topo" "vitess.io/vitess/go/vt/topo/topoproto" + "vitess.io/vitess/go/vt/vtenv" "vitess.io/vitess/go/vt/vttablet/grpctmserver" "vitess.io/vitess/go/vt/vttablet/tabletconntest" "vitess.io/vitess/go/vt/vttablet/tabletmanager" @@ -212,9 +210,7 @@ func (ft *FakeTablet) StartActionLoop(t *testing.T, wr *wrangler.Wrangler) { DBConfigs: &dbconfigs.DBConfigs{}, QueryServiceControl: tabletservermock.NewController(), VREngine: vreplication.NewTestEngine(wr.TopoServer(), ft.Tablet.Alias.Cell, ft.FakeMysqlDaemon, binlogplayer.NewFakeDBClient, binlogplayer.NewFakeDBClient, topoproto.TabletDbName(ft.Tablet), nil), - CollationEnv: collations.MySQL8(), - SQLParser: sqlparser.NewTestParser(), - MySQLVersion: config.DefaultMySQLVersion, + Env: vtenv.NewTestEnv(), } if err := ft.TM.Start(ft.Tablet, nil); err != nil { t.Fatalf("Error in tablet - %v, err - %v", topoproto.TabletAliasString(ft.Tablet.Alias), err.Error()) diff --git a/go/vt/wrangler/testlib/find_tablet_test.go b/go/vt/wrangler/testlib/find_tablet_test.go index cc8ae23e454..069eb913ddf 100644 --- a/go/vt/wrangler/testlib/find_tablet_test.go +++ b/go/vt/wrangler/testlib/find_tablet_test.go @@ -21,14 +21,12 @@ import ( "testing" "time" - "vitess.io/vitess/go/mysql/collations" - "vitess.io/vitess/go/mysql/config" "vitess.io/vitess/go/vt/logutil" - "vitess.io/vitess/go/vt/sqlparser" "vitess.io/vitess/go/vt/topo" "vitess.io/vitess/go/vt/topo/memorytopo" "vitess.io/vitess/go/vt/topo/topoproto" "vitess.io/vitess/go/vt/topotools" + "vitess.io/vitess/go/vt/vtenv" "vitess.io/vitess/go/vt/vttablet/tmclient" "vitess.io/vitess/go/vt/wrangler" @@ -39,7 +37,7 @@ func TestFindTablet(t *testing.T) { ctx, cancel := context.WithTimeout(context.Background(), time.Second*30) defer cancel() ts := memorytopo.NewServer(ctx, "cell1", "cell2") - wr := wrangler.New(logutil.NewConsoleLogger(), ts, tmclient.NewTabletManagerClient(), collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion) + wr := wrangler.New(vtenv.NewTestEnv(), logutil.NewConsoleLogger(), ts, tmclient.NewTabletManagerClient()) // Create an old primary, two good replicas oldPrimary := NewFakeTablet(t, wr, "cell1", 0, topodatapb.TabletType_PRIMARY, nil) diff --git a/go/vt/wrangler/testlib/permissions_test.go b/go/vt/wrangler/testlib/permissions_test.go index e66ab8307b0..ba110a30d87 100644 --- a/go/vt/wrangler/testlib/permissions_test.go +++ b/go/vt/wrangler/testlib/permissions_test.go @@ -22,17 +22,15 @@ import ( "testing" "time" - "vitess.io/vitess/go/mysql/collations" - "vitess.io/vitess/go/mysql/config" "vitess.io/vitess/go/vt/discovery" - "vitess.io/vitess/go/vt/sqlparser" "vitess.io/vitess/go/vt/topo/topoproto" + "vitess.io/vitess/go/vt/vtenv" + "vitess.io/vitess/go/vt/vttablet/tmclient" "vitess.io/vitess/go/sqltypes" "vitess.io/vitess/go/vt/logutil" "vitess.io/vitess/go/vt/topo" "vitess.io/vitess/go/vt/topo/memorytopo" - "vitess.io/vitess/go/vt/vttablet/tmclient" "vitess.io/vitess/go/vt/wrangler" querypb "vitess.io/vitess/go/vt/proto/query" @@ -50,7 +48,7 @@ func TestPermissions(t *testing.T) { ctx, cancel := context.WithTimeout(context.Background(), time.Second*30) defer cancel() ts := memorytopo.NewServer(ctx, "cell1", "cell2") - wr := wrangler.New(logutil.NewConsoleLogger(), ts, tmclient.NewTabletManagerClient(), collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion) + wr := wrangler.New(vtenv.NewTestEnv(), logutil.NewConsoleLogger(), ts, tmclient.NewTabletManagerClient()) vp := NewVtctlPipe(t, ts) defer vp.Close() diff --git a/go/vt/wrangler/testlib/planned_reparent_shard_test.go b/go/vt/wrangler/testlib/planned_reparent_shard_test.go index 9febf4d54ce..7069df9d3e1 100644 --- a/go/vt/wrangler/testlib/planned_reparent_shard_test.go +++ b/go/vt/wrangler/testlib/planned_reparent_shard_test.go @@ -22,11 +22,9 @@ import ( "testing" "time" - "vitess.io/vitess/go/mysql/collations" - "vitess.io/vitess/go/mysql/config" "vitess.io/vitess/go/mysql/replication" "vitess.io/vitess/go/vt/mysqlctl" - "vitess.io/vitess/go/vt/sqlparser" + "vitess.io/vitess/go/vt/vtenv" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" @@ -54,7 +52,7 @@ func TestPlannedReparentShardNoPrimaryProvided(t *testing.T) { ctx, cancel := context.WithCancel(context.Background()) defer cancel() ts := memorytopo.NewServer(ctx, "cell1", "cell2") - wr := wrangler.New(logutil.NewConsoleLogger(), ts, tmclient.NewTabletManagerClient(), collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion) + wr := wrangler.New(vtenv.NewTestEnv(), logutil.NewConsoleLogger(), ts, tmclient.NewTabletManagerClient()) vp := NewVtctlPipe(t, ts) defer vp.Close() @@ -170,7 +168,7 @@ func TestPlannedReparentShardNoError(t *testing.T) { ctx, cancel := context.WithCancel(context.Background()) defer cancel() ts := memorytopo.NewServer(ctx, "cell1", "cell2") - wr := wrangler.New(logutil.NewConsoleLogger(), ts, tmclient.NewTabletManagerClient(), collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion) + wr := wrangler.New(vtenv.NewTestEnv(), logutil.NewConsoleLogger(), ts, tmclient.NewTabletManagerClient()) vp := NewVtctlPipe(t, ts) defer vp.Close() @@ -306,7 +304,7 @@ func TestPlannedReparentInitialization(t *testing.T) { ctx, cancel := context.WithCancel(context.Background()) defer cancel() ts := memorytopo.NewServer(ctx, "cell1", "cell2") - wr := wrangler.New(logutil.NewConsoleLogger(), ts, tmclient.NewTabletManagerClient(), collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion) + wr := wrangler.New(vtenv.NewTestEnv(), logutil.NewConsoleLogger(), ts, tmclient.NewTabletManagerClient()) vp := NewVtctlPipe(t, ts) defer vp.Close() @@ -392,7 +390,7 @@ func TestPlannedReparentShardWaitForPositionFail(t *testing.T) { ctx, cancel := context.WithCancel(context.Background()) defer cancel() ts := memorytopo.NewServer(ctx, "cell1", "cell2") - wr := wrangler.New(logutil.NewConsoleLogger(), ts, tmclient.NewTabletManagerClient(), collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion) + wr := wrangler.New(vtenv.NewTestEnv(), logutil.NewConsoleLogger(), ts, tmclient.NewTabletManagerClient()) vp := NewVtctlPipe(t, ts) defer vp.Close() @@ -500,7 +498,7 @@ func TestPlannedReparentShardWaitForPositionTimeout(t *testing.T) { ctx, cancel := context.WithCancel(context.Background()) defer cancel() ts := memorytopo.NewServer(ctx, "cell1", "cell2") - wr := wrangler.New(logutil.NewConsoleLogger(), ts, tmclient.NewTabletManagerClient(), collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion) + wr := wrangler.New(vtenv.NewTestEnv(), logutil.NewConsoleLogger(), ts, tmclient.NewTabletManagerClient()) vp := NewVtctlPipe(t, ts) defer vp.Close() @@ -606,7 +604,7 @@ func TestPlannedReparentShardRelayLogError(t *testing.T) { ctx, cancel := context.WithCancel(context.Background()) defer cancel() ts := memorytopo.NewServer(ctx, "cell1") - wr := wrangler.New(logutil.NewConsoleLogger(), ts, tmclient.NewTabletManagerClient(), collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion) + wr := wrangler.New(vtenv.NewTestEnv(), logutil.NewConsoleLogger(), ts, tmclient.NewTabletManagerClient()) vp := NewVtctlPipe(t, ts) defer vp.Close() @@ -686,7 +684,7 @@ func TestPlannedReparentShardRelayLogErrorStartReplication(t *testing.T) { ctx, cancel := context.WithCancel(context.Background()) defer cancel() ts := memorytopo.NewServer(ctx, "cell1") - wr := wrangler.New(logutil.NewConsoleLogger(), ts, tmclient.NewTabletManagerClient(), collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion) + wr := wrangler.New(vtenv.NewTestEnv(), logutil.NewConsoleLogger(), ts, tmclient.NewTabletManagerClient()) vp := NewVtctlPipe(t, ts) defer vp.Close() @@ -771,7 +769,7 @@ func TestPlannedReparentShardPromoteReplicaFail(t *testing.T) { ctx, cancel := context.WithCancel(context.Background()) defer cancel() ts := memorytopo.NewServer(ctx, "cell1", "cell2") - wr := wrangler.New(logutil.NewConsoleLogger(), ts, tmclient.NewTabletManagerClient(), collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion) + wr := wrangler.New(vtenv.NewTestEnv(), logutil.NewConsoleLogger(), ts, tmclient.NewTabletManagerClient()) vp := NewVtctlPipe(t, ts) defer vp.Close() @@ -911,7 +909,7 @@ func TestPlannedReparentShardSamePrimary(t *testing.T) { ctx, cancel := context.WithCancel(context.Background()) defer cancel() ts := memorytopo.NewServer(ctx, "cell1", "cell2") - wr := wrangler.New(logutil.NewConsoleLogger(), ts, tmclient.NewTabletManagerClient(), collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion) + wr := wrangler.New(vtenv.NewTestEnv(), logutil.NewConsoleLogger(), ts, tmclient.NewTabletManagerClient()) vp := NewVtctlPipe(t, ts) defer vp.Close() diff --git a/go/vt/wrangler/testlib/reparent_utils_test.go b/go/vt/wrangler/testlib/reparent_utils_test.go index 0a671f52f03..8dfc3efb20d 100644 --- a/go/vt/wrangler/testlib/reparent_utils_test.go +++ b/go/vt/wrangler/testlib/reparent_utils_test.go @@ -24,17 +24,15 @@ import ( "github.com/stretchr/testify/require" - "vitess.io/vitess/go/mysql/collations" - "vitess.io/vitess/go/mysql/config" "vitess.io/vitess/go/mysql/replication" "vitess.io/vitess/go/vt/discovery" "vitess.io/vitess/go/vt/logutil" - "vitess.io/vitess/go/vt/sqlparser" "vitess.io/vitess/go/vt/topo" "vitess.io/vitess/go/vt/topo/memorytopo" "vitess.io/vitess/go/vt/topo/topoproto" "vitess.io/vitess/go/vt/vtctl/reparentutil" "vitess.io/vitess/go/vt/vtctl/reparentutil/reparenttestutil" + "vitess.io/vitess/go/vt/vtenv" "vitess.io/vitess/go/vt/vttablet/tmclient" "vitess.io/vitess/go/vt/wrangler" @@ -51,7 +49,7 @@ func TestShardReplicationStatuses(t *testing.T) { ctx, cancel := context.WithCancel(context.Background()) defer cancel() ts := memorytopo.NewServer(ctx, "cell1", "cell2") - wr := wrangler.New(logutil.NewConsoleLogger(), ts, tmclient.NewTabletManagerClient(), collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion) + wr := wrangler.New(vtenv.NewTestEnv(), logutil.NewConsoleLogger(), ts, tmclient.NewTabletManagerClient()) // create shard and tablets if _, err := ts.GetOrCreateShard(ctx, "test_keyspace", "0"); err != nil { @@ -135,7 +133,7 @@ func TestReparentTablet(t *testing.T) { ctx, cancel := context.WithCancel(context.Background()) defer cancel() ts := memorytopo.NewServer(ctx, "cell1", "cell2") - wr := wrangler.New(logutil.NewConsoleLogger(), ts, tmclient.NewTabletManagerClient(), collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion) + wr := wrangler.New(vtenv.NewTestEnv(), logutil.NewConsoleLogger(), ts, tmclient.NewTabletManagerClient()) // create shard and tablets if _, err := ts.GetOrCreateShard(ctx, "test_keyspace", "0"); err != nil { @@ -192,7 +190,7 @@ func TestSetReplicationSource(t *testing.T) { ctx, cancel := context.WithCancel(context.Background()) defer cancel() ts := memorytopo.NewServer(ctx, "cell1", "cell2") - wr := wrangler.New(logutil.NewConsoleLogger(), ts, tmclient.NewTabletManagerClient(), collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion) + wr := wrangler.New(vtenv.NewTestEnv(), logutil.NewConsoleLogger(), ts, tmclient.NewTabletManagerClient()) // create shard and tablets _, err := ts.GetOrCreateShard(ctx, "test_keyspace", "0") diff --git a/go/vt/wrangler/testlib/shard_test.go b/go/vt/wrangler/testlib/shard_test.go index e7ef9c637a1..400071d9e3c 100644 --- a/go/vt/wrangler/testlib/shard_test.go +++ b/go/vt/wrangler/testlib/shard_test.go @@ -21,13 +21,11 @@ import ( "strings" "testing" - "vitess.io/vitess/go/mysql/collations" - "vitess.io/vitess/go/mysql/config" "vitess.io/vitess/go/vt/logutil" - "vitess.io/vitess/go/vt/sqlparser" "vitess.io/vitess/go/vt/topo" "vitess.io/vitess/go/vt/topo/memorytopo" "vitess.io/vitess/go/vt/topotools" + "vitess.io/vitess/go/vt/vtenv" "vitess.io/vitess/go/vt/vttablet/tmclient" "vitess.io/vitess/go/vt/wrangler" @@ -38,7 +36,7 @@ func TestDeleteShardCleanup(t *testing.T) { ctx, cancel := context.WithCancel(context.Background()) defer cancel() ts := memorytopo.NewServer(ctx, "cell1", "cell2") - wr := wrangler.New(logutil.NewConsoleLogger(), ts, tmclient.NewTabletManagerClient(), collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion) + wr := wrangler.New(vtenv.NewTestEnv(), logutil.NewConsoleLogger(), ts, tmclient.NewTabletManagerClient()) vp := NewVtctlPipe(t, ts) defer vp.Close() diff --git a/go/vt/wrangler/testlib/version_test.go b/go/vt/wrangler/testlib/version_test.go index b282f976cea..c0ea92a5b46 100644 --- a/go/vt/wrangler/testlib/version_test.go +++ b/go/vt/wrangler/testlib/version_test.go @@ -25,15 +25,13 @@ import ( "testing" "time" - "vitess.io/vitess/go/mysql/collations" - "vitess.io/vitess/go/mysql/config" "vitess.io/vitess/go/vt/discovery" - "vitess.io/vitess/go/vt/sqlparser" "vitess.io/vitess/go/vt/topo/topoproto" + "vitess.io/vitess/go/vt/vtenv" + "vitess.io/vitess/go/vt/vttablet/tmclient" "vitess.io/vitess/go/vt/logutil" "vitess.io/vitess/go/vt/topo/memorytopo" - "vitess.io/vitess/go/vt/vttablet/tmclient" "vitess.io/vitess/go/vt/wrangler" topodatapb "vitess.io/vitess/go/vt/proto/topodata" @@ -73,7 +71,7 @@ func TestVersion(t *testing.T) { ctx, cancel := context.WithCancel(context.Background()) defer cancel() ts := memorytopo.NewServer(ctx, "cell1", "cell2") - wr := wrangler.New(logutil.NewConsoleLogger(), ts, tmclient.NewTabletManagerClient(), collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion) + wr := wrangler.New(vtenv.NewTestEnv(), logutil.NewConsoleLogger(), ts, tmclient.NewTabletManagerClient()) vp := NewVtctlPipe(t, ts) defer vp.Close() diff --git a/go/vt/wrangler/testlib/vtctl_pipe.go b/go/vt/wrangler/testlib/vtctl_pipe.go index 37ac001ce98..44a6931870a 100644 --- a/go/vt/wrangler/testlib/vtctl_pipe.go +++ b/go/vt/wrangler/testlib/vtctl_pipe.go @@ -30,14 +30,12 @@ import ( "github.com/stretchr/testify/require" "google.golang.org/grpc" - "vitess.io/vitess/go/mysql/collations" - "vitess.io/vitess/go/mysql/config" "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/grpcvtctlserver" "vitess.io/vitess/go/vt/vtctl/vtctlclient" + "vitess.io/vitess/go/vt/vtenv" // we need to import the grpcvtctlclient library so the gRPC // vtctl client is registered and can be used. @@ -79,7 +77,7 @@ func NewVtctlPipe(t *testing.T, ts *topo.Server) *VtctlPipe { // Create a gRPC server and listen on the port server := grpc.NewServer() - grpcvtctlserver.StartServer(server, ts, collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion) + grpcvtctlserver.StartServer(server, vtenv.NewTestEnv(), ts) go server.Serve(listener) // Create a VtctlClient gRPC client to talk to the fake server diff --git a/go/vt/wrangler/traffic_switcher.go b/go/vt/wrangler/traffic_switcher.go index 9cc0eea554c..e25b94f99e2 100644 --- a/go/vt/wrangler/traffic_switcher.go +++ b/go/vt/wrangler/traffic_switcher.go @@ -29,8 +29,6 @@ import ( "golang.org/x/exp/maps" "golang.org/x/sync/errgroup" - "vitess.io/vitess/go/mysql/collations" - "vitess.io/vitess/go/json2" "vitess.io/vitess/go/sqlescape" "vitess.io/vitess/go/sqltypes" @@ -138,7 +136,6 @@ type trafficSwitcher struct { func (ts *trafficSwitcher) TopoServer() *topo.Server { return ts.wr.ts } func (ts *trafficSwitcher) TabletManagerClient() tmclient.TabletManagerClient { return ts.wr.tmc } -func (ts *trafficSwitcher) CollationEnv() *collations.Environment { return ts.wr.collationEnv } func (ts *trafficSwitcher) Logger() logutil.Logger { return ts.wr.logger } func (ts *trafficSwitcher) VReplicationExec(ctx context.Context, alias *topodatapb.TabletAlias, query string) (*querypb.QueryResult, error) { return ts.wr.VReplicationExec(ctx, alias, query) @@ -224,7 +221,7 @@ func (wr *Wrangler) getWorkflowState(ctx context.Context, targetKeyspace, workfl return nil, nil, err } - ws := workflow.NewServer(wr.ts, wr.tmc, wr.collationEnv, wr.parser, wr.mysqlVersion) + ws := workflow.NewServer(wr.env, wr.ts, wr.tmc) state := &workflow.State{ Workflow: workflowName, SourceKeyspace: ts.SourceKeyspaceName(), @@ -556,7 +553,7 @@ func (wr *Wrangler) SwitchWrites(ctx context.Context, targetKeyspace, workflowNa } if !journalsExist { ts.Logger().Infof("No previous journals were found. Proceeding normally.") - sm, err := workflow.BuildStreamMigrator(ctx, ts, cancel, wr.parser) + sm, err := workflow.BuildStreamMigrator(ctx, ts, cancel, wr.env.Parser()) if err != nil { return handleError("failed to migrate the workflow streams", err) } @@ -993,7 +990,7 @@ func (wr *Wrangler) buildTrafficSwitcher(ctx context.Context, targetKeyspace, wo if err != nil { return nil, err } - ts.sourceKSSchema, err = vindexes.BuildKeyspaceSchema(vs, ts.sourceKeyspace, wr.parser) + ts.sourceKSSchema, err = vindexes.BuildKeyspaceSchema(vs, ts.sourceKeyspace, wr.env.Parser()) if err != nil { return nil, err } @@ -1187,7 +1184,7 @@ func (ts *trafficSwitcher) switchShardReads(ctx context.Context, cells []string, // If so, it also returns the list of sourceWorkflows that need to be switched. func (ts *trafficSwitcher) checkJournals(ctx context.Context) (journalsExist bool, sourceWorkflows []string, err error) { var ( - ws = workflow.NewServer(ts.TopoServer(), ts.TabletManagerClient(), ts.wr.collationEnv, ts.wr.parser, ts.wr.mysqlVersion) + ws = workflow.NewServer(ts.wr.env, ts.TopoServer(), ts.TabletManagerClient()) mu sync.Mutex ) diff --git a/go/vt/wrangler/traffic_switcher_env_test.go b/go/vt/wrangler/traffic_switcher_env_test.go index 04c52e453ae..9134bb8917e 100644 --- a/go/vt/wrangler/traffic_switcher_env_test.go +++ b/go/vt/wrangler/traffic_switcher_env_test.go @@ -29,8 +29,6 @@ import ( "github.com/stretchr/testify/require" "golang.org/x/sync/semaphore" - "vitess.io/vitess/go/mysql/collations" - "vitess.io/vitess/go/mysql/config" "vitess.io/vitess/go/mysql/fakesqldb" "vitess.io/vitess/go/mysql/replication" "vitess.io/vitess/go/sqlescape" @@ -44,6 +42,7 @@ import ( "vitess.io/vitess/go/vt/topo" "vitess.io/vitess/go/vt/topo/memorytopo" "vitess.io/vitess/go/vt/topotools" + "vitess.io/vitess/go/vt/vtenv" "vitess.io/vitess/go/vt/vtgate/vindexes" "vitess.io/vitess/go/vt/vttablet/queryservice" "vitess.io/vitess/go/vt/vttablet/tabletconn" @@ -122,7 +121,7 @@ func newTestTableMigrater(ctx context.Context, t *testing.T) *testMigraterEnv { func newTestTableMigraterCustom(ctx context.Context, t *testing.T, sourceShards, targetShards []string, fmtQuery string) *testMigraterEnv { tme := &testMigraterEnv{} tme.ts = memorytopo.NewServer(ctx, "cell1", "cell2") - tme.wr = New(logutil.NewConsoleLogger(), tme.ts, tmclient.NewTabletManagerClient(), collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion) + tme.wr = New(vtenv.NewTestEnv(), logutil.NewConsoleLogger(), tme.ts, tmclient.NewTabletManagerClient()) tme.wr.sem = semaphore.NewWeighted(1) tme.sourceShards = sourceShards tme.targetShards = targetShards @@ -392,7 +391,7 @@ func newTestTablePartialMigrater(ctx context.Context, t *testing.T, shards, shar require.Greater(t, len(shards), 1, "shard by shard migrations can only be done on sharded keyspaces") tme := &testMigraterEnv{} tme.ts = memorytopo.NewServer(ctx, "cell1", "cell2") - tme.wr = New(logutil.NewConsoleLogger(), tme.ts, tmclient.NewTabletManagerClient(), collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion) + tme.wr = New(vtenv.NewTestEnv(), logutil.NewConsoleLogger(), tme.ts, tmclient.NewTabletManagerClient()) tme.wr.sem = semaphore.NewWeighted(1) tme.sourceShards = shards tme.targetShards = shards @@ -555,7 +554,7 @@ func newTestTablePartialMigrater(ctx context.Context, t *testing.T, shards, shar func newTestShardMigrater(ctx context.Context, t *testing.T, sourceShards, targetShards []string) *testShardMigraterEnv { tme := &testShardMigraterEnv{} tme.ts = memorytopo.NewServer(ctx, "cell1", "cell2") - tme.wr = New(logutil.NewConsoleLogger(), tme.ts, tmclient.NewTabletManagerClient(), collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion) + tme.wr = New(vtenv.NewTestEnv(), logutil.NewConsoleLogger(), tme.ts, tmclient.NewTabletManagerClient()) tme.sourceShards = sourceShards tme.targetShards = targetShards tme.tmeDB = fakesqldb.New(t) diff --git a/go/vt/wrangler/vdiff.go b/go/vt/wrangler/vdiff.go index e5e351d5e3b..70dee1261fc 100644 --- a/go/vt/wrangler/vdiff.go +++ b/go/vt/wrangler/vdiff.go @@ -31,6 +31,7 @@ import ( "vitess.io/vitess/go/mysql/replication" "vitess.io/vitess/go/mysql/sqlerror" + "vitess.io/vitess/go/vt/vtenv" "vitess.io/vitess/go/mysql/collations" "vitess.io/vitess/go/sqltypes" @@ -92,6 +93,7 @@ type RowDiff struct { // vdiff contains the metadata for performing vdiff for one workflow. type vdiff struct { + env *vtenv.Environment ts *trafficSwitcher sourceCell string targetCell string @@ -110,10 +112,6 @@ type vdiff struct { tables []string sourceTimeZone string targetTimeZone string - - collationEnv *collations.Environment - parser *sqlparser.Parser - mysqlVersion string } // compareColInfo contains the metadata for a column of the table being diffed @@ -214,6 +212,7 @@ func (wr *Wrangler) VDiff(ctx context.Context, targetKeyspace, workflowName, sou } // Initialize vdiff df := &vdiff{ + env: wr.env, ts: ts, sourceCell: sourceCell, targetCell: targetCell, @@ -225,9 +224,6 @@ func (wr *Wrangler) VDiff(ctx context.Context, targetKeyspace, workflowName, sou tables: includeTables, sourceTimeZone: ts.sourceTimeZone, targetTimeZone: ts.targetTimeZone, - collationEnv: wr.collationEnv, - parser: wr.parser, - mysqlVersion: wr.mysqlVersion, } for shard, source := range ts.Sources() { df.sources[shard] = &shardStreamer{ @@ -251,7 +247,7 @@ func (wr *Wrangler) VDiff(ctx context.Context, targetKeyspace, workflowName, sou if err != nil { return nil, vterrors.Wrap(err, "GetSchema") } - if err = df.buildVDiffPlan(ctx, oneFilter, schm, df.tables); err != nil { + if err = df.buildVDiffPlan(oneFilter, schm, df.tables); err != nil { return nil, vterrors.Wrap(err, "buildVDiffPlan") } @@ -379,11 +375,11 @@ func (wr *Wrangler) VDiff(ctx context.Context, targetKeyspace, workflowName, sou diffReports[table] = dr } if format == "json" { - json, err := json.MarshalIndent(diffReports, "", "") + j, err := json.MarshalIndent(diffReports, "", "") if err != nil { wr.Logger().Printf("Error converting report to json: %v", err.Error()) } - jsonOutput += string(json) + jsonOutput += string(j) wr.logger.Printf("%s", jsonOutput) } else { for table, dr := range diffReports { @@ -453,7 +449,7 @@ func (df *vdiff) diffTable(ctx context.Context, wr *Wrangler, table string, td * } // buildVDiffPlan builds all the differs. -func (df *vdiff) buildVDiffPlan(ctx context.Context, filter *binlogdatapb.Filter, schm *tabletmanagerdatapb.SchemaDefinition, tablesToInclude []string) error { +func (df *vdiff) buildVDiffPlan(filter *binlogdatapb.Filter, schm *tabletmanagerdatapb.SchemaDefinition, tablesToInclude []string) error { df.differs = make(map[string]*tableDiffer) for _, table := range schm.TableDefinitions { rule, err := vreplication.MatchTable(table.Name, filter) @@ -495,8 +491,8 @@ func (df *vdiff) buildVDiffPlan(ctx context.Context, filter *binlogdatapb.Filter // findPKs identifies PKs, determines any collations to be used for // them, and removes them from the columns used for data comparison. -func findPKs(table *tabletmanagerdatapb.TableDefinition, targetSelect *sqlparser.Select, td *tableDiffer, collationEnv *collations.Environment, parser *sqlparser.Parser, mysqlVersion string) (sqlparser.OrderBy, error) { - columnCollations, err := getColumnCollations(table, collationEnv, parser, mysqlVersion) +func findPKs(env *vtenv.Environment, table *tabletmanagerdatapb.TableDefinition, targetSelect *sqlparser.Select, td *tableDiffer) (sqlparser.OrderBy, error) { + columnCollations, err := getColumnCollations(env, table) if err != nil { return nil, err } @@ -540,8 +536,8 @@ func findPKs(table *tabletmanagerdatapb.TableDefinition, targetSelect *sqlparser // getColumnCollations determines the proper collation to use for each // column in the table definition leveraging MySQL's collation inheritance // rules. -func getColumnCollations(table *tabletmanagerdatapb.TableDefinition, collationEnv *collations.Environment, parser *sqlparser.Parser, mysqlVersion string) (map[string]collations.ID, error) { - createstmt, err := parser.Parse(table.Schema) +func getColumnCollations(venv *vtenv.Environment, table *tabletmanagerdatapb.TableDefinition) (map[string]collations.ID, error) { + createstmt, err := venv.Parser().Parse(table.Schema) if err != nil { return nil, err } @@ -549,7 +545,7 @@ func getColumnCollations(table *tabletmanagerdatapb.TableDefinition, collationEn if !ok { return nil, vterrors.Wrapf(err, "invalid table schema %s for table %s", table.Schema, table.Name) } - env := schemadiff.NewEnv(collationEnv, collationEnv.DefaultConnectionCharset(), parser, mysqlVersion) + env := schemadiff.NewEnv(venv, venv.CollationEnv().DefaultConnectionCharset()) tableschema, err := schemadiff.NewCreateTableEntity(env, createtable) if err != nil { return nil, vterrors.Wrapf(err, "invalid table schema %s for table %s", table.Schema, table.Name) @@ -561,25 +557,25 @@ func getColumnCollations(table *tabletmanagerdatapb.TableDefinition, collationEn getColumnCollation := func(column *sqlparser.ColumnDefinition) collations.ID { // If there's an explicit collation listed then use that. if column.Type.Options.Collate != "" { - return collationEnv.LookupByName(strings.ToLower(column.Type.Options.Collate)) + return env.CollationEnv().LookupByName(strings.ToLower(column.Type.Options.Collate)) } // If the column has a charset listed then the default collation // for that charset is used. if column.Type.Charset.Name != "" { - return collationEnv.DefaultCollationForCharset(strings.ToLower(column.Type.Charset.Name)) + return env.CollationEnv().DefaultCollationForCharset(strings.ToLower(column.Type.Charset.Name)) } // If the table has an explicit collation listed then use that. if tableCollation != "" { - return collationEnv.LookupByName(strings.ToLower(tableCollation)) + return env.CollationEnv().LookupByName(strings.ToLower(tableCollation)) } // If the table has a charset listed then use the default collation // for that charset. if tableCharset != "" { - return collationEnv.DefaultCollationForCharset(strings.ToLower(tableCharset)) + return env.CollationEnv().DefaultCollationForCharset(strings.ToLower(tableCharset)) } // The table is using the global default charset and collation and // we inherit that. - return collationEnv.DefaultConnectionCharset() + return env.CollationEnv().DefaultConnectionCharset() } columnCollations := make(map[string]collations.ID) @@ -656,7 +652,7 @@ func getColumnNameForSelectExpr(selectExpression sqlparser.SelectExpr) (string, // buildTablePlan builds one tableDiffer. func (df *vdiff) buildTablePlan(table *tabletmanagerdatapb.TableDefinition, query string) (*tableDiffer, error) { - statement, err := df.parser.Parse(query) + statement, err := df.env.Parser().Parse(query) if err != nil { return nil, err } @@ -666,8 +662,8 @@ func (df *vdiff) buildTablePlan(table *tabletmanagerdatapb.TableDefinition, quer } td := &tableDiffer{ targetTable: table.Name, - collationEnv: df.collationEnv, - parser: df.parser, + collationEnv: df.env.CollationEnv(), + parser: df.env.Parser(), } sourceSelect := &sqlparser.Select{} targetSelect := &sqlparser.Select{} @@ -708,7 +704,7 @@ func (df *vdiff) buildTablePlan(table *tabletmanagerdatapb.TableDefinition, quer aggregates = append(aggregates, engine.NewAggregateParam( /*opcode*/ opcode.AggregateSum, /*offset*/ len(sourceSelect.SelectExprs)-1, - /*alias*/ "", df.collationEnv)) + /*alias*/ "", df.env.CollationEnv())) } } default: @@ -747,7 +743,7 @@ func (df *vdiff) buildTablePlan(table *tabletmanagerdatapb.TableDefinition, quer }, } - orderby, err := findPKs(table, targetSelect, td, df.collationEnv, df.parser, df.mysqlVersion) + orderby, err := findPKs(df.env, table, targetSelect, td) if err != nil { return nil, err } @@ -763,8 +759,8 @@ func (df *vdiff) buildTablePlan(table *tabletmanagerdatapb.TableDefinition, quer td.sourceExpression = sqlparser.String(sourceSelect) td.targetExpression = sqlparser.String(targetSelect) - td.sourcePrimitive = newMergeSorter(df.sources, td.comparePKs, df.collationEnv) - td.targetPrimitive = newMergeSorter(df.targets, td.comparePKs, df.collationEnv) + td.sourcePrimitive = newMergeSorter(df.sources, td.comparePKs, df.env.CollationEnv()) + td.targetPrimitive = newMergeSorter(df.targets, td.comparePKs, df.env.CollationEnv()) // If there were aggregate expressions, we have to re-aggregate // the results, which engine.OrderedAggregate can do. if len(aggregates) != 0 { @@ -772,7 +768,7 @@ func (df *vdiff) buildTablePlan(table *tabletmanagerdatapb.TableDefinition, quer Aggregates: aggregates, GroupByKeys: pkColsToGroupByParams(td.pkCols, td.collationEnv), Input: td.sourcePrimitive, - CollationEnv: df.collationEnv, + CollationEnv: df.env.CollationEnv(), } } diff --git a/go/vt/wrangler/vdiff_env_test.go b/go/vt/wrangler/vdiff_env_test.go index 08dd96068b0..ff0f97db769 100644 --- a/go/vt/wrangler/vdiff_env_test.go +++ b/go/vt/wrangler/vdiff_env_test.go @@ -23,15 +23,13 @@ import ( "sync" "testing" - "vitess.io/vitess/go/mysql/collations" - "vitess.io/vitess/go/mysql/config" "vitess.io/vitess/go/mysql/fakesqldb" "vitess.io/vitess/go/sqltypes" "vitess.io/vitess/go/vt/grpcclient" "vitess.io/vitess/go/vt/logutil" - "vitess.io/vitess/go/vt/sqlparser" "vitess.io/vitess/go/vt/topo" "vitess.io/vitess/go/vt/topo/memorytopo" + "vitess.io/vitess/go/vt/vtenv" "vitess.io/vitess/go/vt/vttablet/queryservice" "vitess.io/vitess/go/vt/vttablet/queryservice/fakes" "vitess.io/vitess/go/vt/vttablet/tabletconn" @@ -80,7 +78,7 @@ func newTestVDiffEnv(t testing.TB, ctx context.Context, sourceShards, targetShar tabletType: topodatapb.TabletType_REPLICA, tmc: newTestVDiffTMClient(), } - env.wr = New(logutil.NewConsoleLogger(), env.topoServ, env.tmc, collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion) + env.wr = New(vtenv.NewTestEnv(), logutil.NewConsoleLogger(), env.topoServ, env.tmc) // Generate a unique dialer name. dialerName := fmt.Sprintf("VDiffTest-%s-%d", t.Name(), rand.Intn(1000000000)) diff --git a/go/vt/wrangler/vdiff_test.go b/go/vt/wrangler/vdiff_test.go index 33b809776db..1b0071ebed7 100644 --- a/go/vt/wrangler/vdiff_test.go +++ b/go/vt/wrangler/vdiff_test.go @@ -27,18 +27,19 @@ import ( "github.com/stretchr/testify/require" "vitess.io/vitess/go/mysql/collations" - "vitess.io/vitess/go/mysql/config" "vitess.io/vitess/go/sqltypes" binlogdatapb "vitess.io/vitess/go/vt/proto/binlogdata" tabletmanagerdatapb "vitess.io/vitess/go/vt/proto/tabletmanagerdata" "vitess.io/vitess/go/vt/sqlparser" "vitess.io/vitess/go/vt/topo" + "vitess.io/vitess/go/vt/vtenv" "vitess.io/vitess/go/vt/vtgate/engine" "vitess.io/vitess/go/vt/vtgate/engine/opcode" ) func TestVDiffPlanSuccess(t *testing.T) { collationEnv := collations.MySQL8() + parser := sqlparser.NewTestParser() schm := &tabletmanagerdatapb.SchemaDefinition{ TableDefinitions: []*tabletmanagerdatapb.TableDefinition{{ Name: "t1", @@ -100,6 +101,7 @@ func TestVDiffPlanSuccess(t *testing.T) { sourcePrimitive: newMergeSorter(nil, []compareColInfo{{0, collations.Unknown, true}}, collationEnv), targetPrimitive: newMergeSorter(nil, []compareColInfo{{0, collations.Unknown, true}}, collationEnv), collationEnv: collationEnv, + parser: parser, }, }, { input: &binlogdatapb.Rule{ @@ -118,6 +120,7 @@ func TestVDiffPlanSuccess(t *testing.T) { sourcePrimitive: newMergeSorter(nil, []compareColInfo{{0, collations.Unknown, true}}, collationEnv), targetPrimitive: newMergeSorter(nil, []compareColInfo{{0, collations.Unknown, true}}, collationEnv), collationEnv: collationEnv, + parser: parser, }, }, { input: &binlogdatapb.Rule{ @@ -136,6 +139,7 @@ func TestVDiffPlanSuccess(t *testing.T) { sourcePrimitive: newMergeSorter(nil, []compareColInfo{{0, collations.Unknown, true}}, collationEnv), targetPrimitive: newMergeSorter(nil, []compareColInfo{{0, collations.Unknown, true}}, collationEnv), collationEnv: collationEnv, + parser: parser, }, }, { input: &binlogdatapb.Rule{ @@ -154,6 +158,7 @@ func TestVDiffPlanSuccess(t *testing.T) { sourcePrimitive: newMergeSorter(nil, []compareColInfo{{1, collations.Unknown, true}}, collationEnv), targetPrimitive: newMergeSorter(nil, []compareColInfo{{1, collations.Unknown, true}}, collationEnv), collationEnv: collationEnv, + parser: parser, }, }, { input: &binlogdatapb.Rule{ @@ -172,6 +177,7 @@ func TestVDiffPlanSuccess(t *testing.T) { sourcePrimitive: newMergeSorter(nil, []compareColInfo{{0, collations.Unknown, true}}, collationEnv), targetPrimitive: newMergeSorter(nil, []compareColInfo{{0, collations.Unknown, true}}, collationEnv), collationEnv: collationEnv, + parser: parser, }, }, { // non-pk text column. @@ -191,6 +197,7 @@ func TestVDiffPlanSuccess(t *testing.T) { sourcePrimitive: newMergeSorter(nil, []compareColInfo{{0, collations.Unknown, true}}, collationEnv), targetPrimitive: newMergeSorter(nil, []compareColInfo{{0, collations.Unknown, true}}, collationEnv), collationEnv: collationEnv, + parser: parser, }, }, { // non-pk text column, different order. @@ -210,6 +217,7 @@ func TestVDiffPlanSuccess(t *testing.T) { sourcePrimitive: newMergeSorter(nil, []compareColInfo{{1, collations.Unknown, true}}, collationEnv), targetPrimitive: newMergeSorter(nil, []compareColInfo{{1, collations.Unknown, true}}, collationEnv), collationEnv: collationEnv, + parser: parser, }, }, { // pk text column. @@ -229,6 +237,7 @@ func TestVDiffPlanSuccess(t *testing.T) { sourcePrimitive: newMergeSorter(nil, []compareColInfo{{0, collationEnv.DefaultConnectionCharset(), false}}, collationEnv), targetPrimitive: newMergeSorter(nil, []compareColInfo{{0, collationEnv.DefaultConnectionCharset(), false}}, collationEnv), collationEnv: collationEnv, + parser: parser, }, }, { // pk text column, different order. @@ -248,6 +257,7 @@ func TestVDiffPlanSuccess(t *testing.T) { sourcePrimitive: newMergeSorter(nil, []compareColInfo{{1, collationEnv.DefaultConnectionCharset(), false}}, collationEnv), targetPrimitive: newMergeSorter(nil, []compareColInfo{{1, collationEnv.DefaultConnectionCharset(), false}}, collationEnv), collationEnv: collationEnv, + parser: parser, }, }, { // text column as expression. @@ -267,6 +277,7 @@ func TestVDiffPlanSuccess(t *testing.T) { sourcePrimitive: newMergeSorter(nil, []compareColInfo{{1, collationEnv.DefaultConnectionCharset(), false}}, collationEnv), targetPrimitive: newMergeSorter(nil, []compareColInfo{{1, collationEnv.DefaultConnectionCharset(), false}}, collationEnv), collationEnv: collationEnv, + parser: parser, }, }, { input: &binlogdatapb.Rule{ @@ -284,6 +295,7 @@ func TestVDiffPlanSuccess(t *testing.T) { sourcePrimitive: newMergeSorter(nil, []compareColInfo{{0, collations.Unknown, true}, {1, collations.Unknown, true}}, collationEnv), targetPrimitive: newMergeSorter(nil, []compareColInfo{{0, collations.Unknown, true}, {1, collations.Unknown, true}}, collationEnv), collationEnv: collationEnv, + parser: parser, }, }, { // in_keyrange @@ -303,6 +315,7 @@ func TestVDiffPlanSuccess(t *testing.T) { sourcePrimitive: newMergeSorter(nil, []compareColInfo{{0, collations.Unknown, true}}, collationEnv), targetPrimitive: newMergeSorter(nil, []compareColInfo{{0, collations.Unknown, true}}, collationEnv), collationEnv: collationEnv, + parser: parser, }, }, { // in_keyrange on RHS of AND. @@ -323,6 +336,7 @@ func TestVDiffPlanSuccess(t *testing.T) { sourcePrimitive: newMergeSorter(nil, []compareColInfo{{0, collations.Unknown, true}}, collationEnv), targetPrimitive: newMergeSorter(nil, []compareColInfo{{0, collations.Unknown, true}}, collationEnv), collationEnv: collationEnv, + parser: parser, }, }, { // in_keyrange on LHS of AND. @@ -342,7 +356,8 @@ func TestVDiffPlanSuccess(t *testing.T) { selectPks: []int{0}, sourcePrimitive: newMergeSorter(nil, []compareColInfo{{0, collations.Unknown, true}}, collationEnv), targetPrimitive: newMergeSorter(nil, []compareColInfo{{0, collations.Unknown, true}}, collationEnv), - collationEnv: collations.MySQL8(), + collationEnv: collationEnv, + parser: parser, }, }, { // in_keyrange on cascaded AND expression @@ -362,7 +377,8 @@ func TestVDiffPlanSuccess(t *testing.T) { selectPks: []int{0}, sourcePrimitive: newMergeSorter(nil, []compareColInfo{{0, collations.Unknown, true}}, collationEnv), targetPrimitive: newMergeSorter(nil, []compareColInfo{{0, collations.Unknown, true}}, collationEnv), - collationEnv: collations.MySQL8(), + collationEnv: collationEnv, + parser: parser, }, }, { // in_keyrange parenthesized @@ -382,7 +398,8 @@ func TestVDiffPlanSuccess(t *testing.T) { selectPks: []int{0}, sourcePrimitive: newMergeSorter(nil, []compareColInfo{{0, collations.Unknown, true}}, collationEnv), targetPrimitive: newMergeSorter(nil, []compareColInfo{{0, collations.Unknown, true}}, collationEnv), - collationEnv: collations.MySQL8(), + collationEnv: collationEnv, + parser: parser, }, }, { // group by @@ -401,7 +418,8 @@ func TestVDiffPlanSuccess(t *testing.T) { selectPks: []int{0}, sourcePrimitive: newMergeSorter(nil, []compareColInfo{{0, collations.Unknown, true}}, collationEnv), targetPrimitive: newMergeSorter(nil, []compareColInfo{{0, collations.Unknown, true}}, collationEnv), - collationEnv: collations.MySQL8(), + collationEnv: collationEnv, + parser: parser, }, }, { // aggregations @@ -425,10 +443,11 @@ func TestVDiffPlanSuccess(t *testing.T) { }, GroupByKeys: []*engine.GroupByParams{{KeyCol: 0, WeightStringCol: -1, CollationEnv: collations.MySQL8()}}, Input: newMergeSorter(nil, []compareColInfo{{0, collations.Unknown, true}}, collationEnv), - CollationEnv: collations.MySQL8(), + CollationEnv: collationEnv, }, targetPrimitive: newMergeSorter(nil, []compareColInfo{{0, collations.Unknown, true}}, collationEnv), - collationEnv: collations.MySQL8(), + collationEnv: collationEnv, + parser: parser, }, }, { input: &binlogdatapb.Rule{ @@ -446,15 +465,16 @@ func TestVDiffPlanSuccess(t *testing.T) { selectPks: []int{0}, sourcePrimitive: newMergeSorter(nil, []compareColInfo{{0, collations.Unknown, true}}, collationEnv), targetPrimitive: newMergeSorter(nil, []compareColInfo{{0, collations.Unknown, true}}, collationEnv), - collationEnv: collations.MySQL8(), + collationEnv: collationEnv, + parser: parser, }, }} for _, tcase := range testcases { t.Run(tcase.input.Filter, func(t *testing.T) { filter := &binlogdatapb.Filter{Rules: []*binlogdatapb.Rule{tcase.input}} - df := &vdiff{sourceTimeZone: tcase.sourceTimeZone, targetTimeZone: "UTC", collationEnv: collations.MySQL8()} - err := df.buildVDiffPlan(context.Background(), filter, schm, nil) + df := &vdiff{env: vtenv.NewTestEnv(), sourceTimeZone: tcase.sourceTimeZone, targetTimeZone: "UTC"} + err := df.buildVDiffPlan(filter, schm, nil) require.NoError(t, err, tcase.input) require.Equal(t, 1, len(df.differs), tcase.input) assert.Equal(t, tcase.td, df.differs[tcase.table], tcase.input) @@ -508,8 +528,8 @@ func TestVDiffPlanFailure(t *testing.T) { }} for _, tcase := range testcases { filter := &binlogdatapb.Filter{Rules: []*binlogdatapb.Rule{tcase.input}} - df := &vdiff{collationEnv: collations.MySQL8(), parser: sqlparser.NewTestParser(), mysqlVersion: config.DefaultMySQLVersion} - err := df.buildVDiffPlan(context.Background(), filter, schm, nil) + df := &vdiff{env: vtenv.NewTestEnv()} + err := df.buildVDiffPlan(filter, schm, nil) assert.EqualError(t, err, tcase.err, tcase.input) } } @@ -1099,9 +1119,10 @@ func TestVDiffFindPKs(t *testing.T) { }, } + env := vtenv.NewTestEnv() for _, tc := range testcases { t.Run(tc.name, func(t *testing.T) { - _, err := findPKs(tc.table, tc.targetSelect, tc.tdIn, collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion) + _, err := findPKs(env, tc.table, tc.targetSelect, tc.tdIn) require.NoError(t, err) require.EqualValues(t, tc.tdOut, tc.tdIn) }) @@ -1138,25 +1159,25 @@ func TestVDiffPlanInclude(t *testing.T) { }}, } - df := &vdiff{collationEnv: collations.MySQL8(), parser: sqlparser.NewTestParser(), mysqlVersion: config.DefaultMySQLVersion} + df := &vdiff{env: vtenv.NewTestEnv()} rule := &binlogdatapb.Rule{ Match: "/.*", } filter := &binlogdatapb.Filter{Rules: []*binlogdatapb.Rule{rule}} var err error - err = df.buildVDiffPlan(context.Background(), filter, schm, []string{"t2"}) + err = df.buildVDiffPlan(filter, schm, []string{"t2"}) require.NoError(t, err) require.Equal(t, 1, len(df.differs)) - err = df.buildVDiffPlan(context.Background(), filter, schm, []string{"t2", "t3"}) + err = df.buildVDiffPlan(filter, schm, []string{"t2", "t3"}) require.NoError(t, err) require.Equal(t, 2, len(df.differs)) - err = df.buildVDiffPlan(context.Background(), filter, schm, []string{"t1", "t2", "t3"}) + err = df.buildVDiffPlan(filter, schm, []string{"t1", "t2", "t3"}) require.NoError(t, err) require.Equal(t, 3, len(df.differs)) - err = df.buildVDiffPlan(context.Background(), filter, schm, []string{"t1", "t2", "t3", "t4"}) + err = df.buildVDiffPlan(filter, schm, []string{"t1", "t2", "t3", "t4"}) require.NoError(t, err) require.Equal(t, 4, len(df.differs)) - err = df.buildVDiffPlan(context.Background(), filter, schm, []string{"t1", "t2", "t3", "t5"}) + err = df.buildVDiffPlan(filter, schm, []string{"t1", "t2", "t3", "t5"}) require.Error(t, err) } @@ -1257,9 +1278,10 @@ func TestGetColumnCollations(t *testing.T) { }, }, } + env := vtenv.NewTestEnv() for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - got, err := getColumnCollations(tt.table, collationEnv, sqlparser.NewTestParser(), config.DefaultMySQLVersion) + got, err := getColumnCollations(env, tt.table) if (err != nil) != tt.wantErr { t.Errorf("getColumnCollations() error = %v, wantErr = %t", err, tt.wantErr) return diff --git a/go/vt/wrangler/vexec.go b/go/vt/wrangler/vexec.go index 0eab71e7c00..49b6c442f60 100644 --- a/go/vt/wrangler/vexec.go +++ b/go/vt/wrangler/vexec.go @@ -279,7 +279,7 @@ func (vx *vexec) execCallback(callback func(context.Context, *topo.TabletInfo) ( // parseQuery parses the input query func (vx *vexec) parseQuery() (err error) { - if vx.stmt, err = vx.wr.parser.Parse(vx.query); err != nil { + if vx.stmt, err = vx.wr.SQLParser().Parse(vx.query); err != nil { return err } if vx.tableName, err = extractTableName(vx.stmt); err != nil { @@ -857,7 +857,7 @@ func (wr *Wrangler) ListAllWorkflows(ctx context.Context, keyspace string, activ where = " where state <> 'Stopped'" } query := "select distinct workflow from _vt.vreplication" + where - vx := vtctldvexec.NewVExec(keyspace, "", wr.ts, wr.tmc, wr.parser) + vx := vtctldvexec.NewVExec(keyspace, "", wr.ts, wr.tmc, wr.SQLParser()) results, err := vx.QueryContext(ctx, query) if err != nil { return nil, err diff --git a/go/vt/wrangler/vexec_plan.go b/go/vt/wrangler/vexec_plan.go index 52dd2f8b6dd..382f8a1a134 100644 --- a/go/vt/wrangler/vexec_plan.go +++ b/go/vt/wrangler/vexec_plan.go @@ -260,7 +260,7 @@ func (vx *vexec) buildUpdatePlan(ctx context.Context, planner vexecPlanner, upd } } if templates := plannerParams.updateTemplates; len(templates) > 0 { - match, err := vx.wr.parser.QueryMatchesTemplates(vx.query, templates) + match, err := vx.wr.env.Parser().QueryMatchesTemplates(vx.query, templates) if err != nil { return nil, err } @@ -312,7 +312,7 @@ func (vx *vexec) buildInsertPlan(ctx context.Context, planner vexecPlanner, ins return nil, fmt.Errorf("query not supported by vexec: %s", sqlparser.String(ins)) } if len(templates) > 0 { - match, err := vx.wr.parser.QueryMatchesTemplates(vx.query, templates) + match, err := vx.wr.env.Parser().QueryMatchesTemplates(vx.query, templates) if err != nil { return nil, err } diff --git a/go/vt/wrangler/vexec_test.go b/go/vt/wrangler/vexec_test.go index 79ec7e6ed09..27efbe61a9f 100644 --- a/go/vt/wrangler/vexec_test.go +++ b/go/vt/wrangler/vexec_test.go @@ -28,15 +28,13 @@ import ( "github.com/stretchr/testify/require" - "vitess.io/vitess/go/mysql/collations" - "vitess.io/vitess/go/mysql/config" "vitess.io/vitess/go/sqltypes" "vitess.io/vitess/go/textutil" "vitess.io/vitess/go/vt/logutil" binlogdatapb "vitess.io/vitess/go/vt/proto/binlogdata" tabletmanagerdatapb "vitess.io/vitess/go/vt/proto/tabletmanagerdata" topodatapb "vitess.io/vitess/go/vt/proto/topodata" - "vitess.io/vitess/go/vt/sqlparser" + "vitess.io/vitess/go/vt/vtenv" ) var ( @@ -57,7 +55,7 @@ func TestVExec(t *testing.T) { env := newWranglerTestEnv(t, ctx, []string{"0"}, []string{"-80", "80-"}, nil, time.Now().Unix()) defer env.close() var logger = logutil.NewMemoryLogger() - wr := New(logger, env.topoServ, env.tmc, collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion) + wr := New(vtenv.NewTestEnv(), logger, env.topoServ, env.tmc) vx := newVExec(ctx, workflow, keyspace, query, wr) err := vx.getPrimaries(nil) @@ -204,7 +202,7 @@ func TestWorkflowListStreams(t *testing.T) { env := newWranglerTestEnv(t, ctx, []string{"0"}, []string{"-80", "80-"}, nil, 1234) defer env.close() logger := logutil.NewMemoryLogger() - wr := New(logger, env.topoServ, env.tmc, collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion) + wr := New(vtenv.NewTestEnv(), logger, env.topoServ, env.tmc) _, err := wr.WorkflowAction(ctx, workflow, keyspace, "listall", false, nil, nil) require.NoError(t, err) @@ -294,7 +292,7 @@ func TestWorkflowListAll(t *testing.T) { env := newWranglerTestEnv(t, ctx, []string{"0"}, []string{"-80", "80-"}, nil, 0) defer env.close() logger := logutil.NewMemoryLogger() - wr := New(logger, env.topoServ, env.tmc, collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion) + wr := New(vtenv.NewTestEnv(), logger, env.topoServ, env.tmc) workflows, err := wr.ListAllWorkflows(ctx, keyspace, true) require.Nil(t, err) @@ -315,7 +313,7 @@ func TestVExecValidations(t *testing.T) { env := newWranglerTestEnv(t, ctx, []string{"0"}, []string{"-80", "80-"}, nil, 0) defer env.close() - wr := New(logutil.NewConsoleLogger(), env.topoServ, env.tmc, collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion) + wr := New(vtenv.NewTestEnv(), logutil.NewConsoleLogger(), env.topoServ, env.tmc) vx := newVExec(ctx, workflow, keyspace, query, wr) @@ -401,7 +399,7 @@ func TestWorkflowUpdate(t *testing.T) { env := newWranglerTestEnv(t, ctx, []string{"0"}, []string{"-80", "80-"}, nil, 1234) defer env.close() logger := logutil.NewMemoryLogger() - wr := New(logger, env.topoServ, env.tmc, collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion) + wr := New(vtenv.NewTestEnv(), logger, env.topoServ, env.tmc) nullSlice := textutil.SimulatedNullStringSlice // Used to represent a non-provided value nullOnDDL := binlogdatapb.OnDDLAction(textutil.SimulatedNullInt) // Used to represent a non-provided value tests := []struct { diff --git a/go/vt/wrangler/wrangler.go b/go/vt/wrangler/wrangler.go index 8898fe39eb6..ee18643cc78 100644 --- a/go/vt/wrangler/wrangler.go +++ b/go/vt/wrangler/wrangler.go @@ -23,12 +23,12 @@ import ( "golang.org/x/sync/semaphore" - "vitess.io/vitess/go/mysql/collations" "vitess.io/vitess/go/sqltypes" "vitess.io/vitess/go/vt/logutil" "vitess.io/vitess/go/vt/sqlparser" "vitess.io/vitess/go/vt/topo" "vitess.io/vitess/go/vt/vtctl/grpcvtctldserver" + "vitess.io/vitess/go/vt/vtenv" "vitess.io/vitess/go/vt/vttablet/tmclient" vtctlservicepb "vitess.io/vitess/go/vt/proto/vtctlservice" @@ -49,6 +49,7 @@ var ( // Multiple go routines can use the same Wrangler at the same time, // provided they want to share the same logger / topo server / lock timeout. type Wrangler struct { + env *vtenv.Environment logger logutil.Logger ts *topo.Server tmc tmclient.TabletManagerClient @@ -59,23 +60,18 @@ type Wrangler struct { VExecFunc func(ctx context.Context, workflow, keyspace, query string, dryRun bool) (map[*topo.TabletInfo]*sqltypes.Result, error) // Limt the number of concurrent background goroutines if needed. sem *semaphore.Weighted - collationEnv *collations.Environment - parser *sqlparser.Parser - mysqlVersion string WorkflowParams *VReplicationWorkflowParams } // New creates a new Wrangler object. -func New(logger logutil.Logger, ts *topo.Server, tmc tmclient.TabletManagerClient, collationEnv *collations.Environment, parser *sqlparser.Parser, mysqlVersion string) *Wrangler { +func New(env *vtenv.Environment, logger logutil.Logger, ts *topo.Server, tmc tmclient.TabletManagerClient) *Wrangler { return &Wrangler{ - logger: logger, - ts: ts, - tmc: tmc, - vtctld: grpcvtctldserver.NewVtctldServer(ts, collationEnv, parser, mysqlVersion), - sourceTs: ts, - collationEnv: collationEnv, - parser: parser, - mysqlVersion: mysqlVersion, + env: env, + logger: logger, + ts: ts, + tmc: tmc, + vtctld: grpcvtctldserver.NewVtctldServer(env, ts), + sourceTs: ts, } } @@ -83,13 +79,12 @@ func New(logger logutil.Logger, ts *topo.Server, tmc tmclient.TabletManagerClien // in production. func NewTestWrangler(logger logutil.Logger, ts *topo.Server, tmc tmclient.TabletManagerClient) *Wrangler { return &Wrangler{ - logger: logger, - ts: ts, - tmc: tmc, - vtctld: grpcvtctldserver.NewTestVtctldServer(ts, tmc), - sourceTs: ts, - collationEnv: collations.MySQL8(), - parser: sqlparser.NewTestParser(), + env: vtenv.NewTestEnv(), + logger: logger, + ts: ts, + tmc: tmc, + vtctld: grpcvtctldserver.NewTestVtctldServer(ts, tmc), + sourceTs: ts, } } @@ -123,5 +118,5 @@ func (wr *Wrangler) Logger() logutil.Logger { // SQLParser returns the parser this wrangler is using. func (wr *Wrangler) SQLParser() *sqlparser.Parser { - return wr.parser + return wr.env.Parser() } diff --git a/go/vt/wrangler/wrangler_env_test.go b/go/vt/wrangler/wrangler_env_test.go index 453e2a88c06..4bc20c0aad0 100644 --- a/go/vt/wrangler/wrangler_env_test.go +++ b/go/vt/wrangler/wrangler_env_test.go @@ -23,15 +23,13 @@ import ( "sync" "testing" - "vitess.io/vitess/go/mysql/collations" - "vitess.io/vitess/go/mysql/config" "vitess.io/vitess/go/sqltypes" "vitess.io/vitess/go/vt/grpcclient" "vitess.io/vitess/go/vt/log" "vitess.io/vitess/go/vt/logutil" - "vitess.io/vitess/go/vt/sqlparser" "vitess.io/vitess/go/vt/topo" "vitess.io/vitess/go/vt/topo/memorytopo" + "vitess.io/vitess/go/vt/vtenv" "vitess.io/vitess/go/vt/vttablet/queryservice" "vitess.io/vitess/go/vt/vttablet/queryservice/fakes" "vitess.io/vitess/go/vt/vttablet/tabletconn" @@ -71,7 +69,7 @@ func newWranglerTestEnv(t testing.TB, ctx context.Context, sourceShards, targetS tabletType: topodatapb.TabletType_REPLICA, tmc: newTestWranglerTMClient(), } - env.wr = New(logutil.NewConsoleLogger(), env.topoServ, env.tmc, collations.MySQL8(), sqlparser.NewTestParser(), config.DefaultMySQLVersion) + env.wr = New(vtenv.NewTestEnv(), logutil.NewConsoleLogger(), env.topoServ, env.tmc) env.tmc.tablets = make(map[int]*testWranglerTablet) // Generate a unique dialer name.