From 523f49547f58f195d72d5dfd32d6cb2d05c5e7e5 Mon Sep 17 00:00:00 2001 From: Andrew Mason Date: Thu, 7 Sep 2023 16:06:23 -0400 Subject: [PATCH 1/9] migrate vttablet to cobra Signed-off-by: Andrew Mason --- go/cmd/vttablet/cli/cli.go | 241 ++++++++++++++++++ .../{ => cli}/plugin_azblobbackupstorage.go | 2 +- .../{ => cli}/plugin_cephbackupstorage.go | 2 +- .../vttablet/{ => cli}/plugin_consultopo.go | 2 +- go/cmd/vttablet/{ => cli}/plugin_etcd2topo.go | 2 +- .../{ => cli}/plugin_filebackupstorage.go | 2 +- .../{ => cli}/plugin_filecustomrule.go | 2 +- .../vttablet/{ => cli}/plugin_filelogger.go | 2 +- .../{ => cli}/plugin_gcsbackupstorage.go | 2 +- .../{ => cli}/plugin_grpcbinlogplayer.go | 2 +- .../{ => cli}/plugin_grpcbinlogstreamer.go | 2 +- .../{ => cli}/plugin_grpcqueryservice.go | 2 +- .../{ => cli}/plugin_grpctabletconn.go | 2 +- .../{ => cli}/plugin_grpcthrottlerserver.go | 2 +- .../vttablet/{ => cli}/plugin_grpctmclient.go | 2 +- .../vttablet/{ => cli}/plugin_grpctmserver.go | 2 +- .../vttablet/{ => cli}/plugin_opentracing.go | 2 +- go/cmd/vttablet/{ => cli}/plugin_opentsdb.go | 2 +- .../{ => cli}/plugin_prometheusbackend.go | 2 +- .../{ => cli}/plugin_s3backupstorage.go | 2 +- go/cmd/vttablet/cli/plugin_statsd.go | 22 ++ .../vttablet/{ => cli}/plugin_sysloglogger.go | 2 +- .../{ => cli}/plugin_topocustomrule.go | 2 +- go/cmd/vttablet/{ => cli}/plugin_zk2topo.go | 2 +- go/cmd/vttablet/{ => cli}/status.go | 4 +- go/cmd/vttablet/docgen/main.go | 37 +++ go/cmd/vttablet/plugin_statsd.go | 7 - go/cmd/vttablet/vttablet.go | 200 +-------------- go/flags/endtoend/vttablet.txt | 9 +- 29 files changed, 332 insertions(+), 232 deletions(-) create mode 100644 go/cmd/vttablet/cli/cli.go rename go/cmd/vttablet/{ => cli}/plugin_azblobbackupstorage.go (97%) rename go/cmd/vttablet/{ => cli}/plugin_cephbackupstorage.go (97%) rename go/cmd/vttablet/{ => cli}/plugin_consultopo.go (98%) rename go/cmd/vttablet/{ => cli}/plugin_etcd2topo.go (98%) rename go/cmd/vttablet/{ => cli}/plugin_filebackupstorage.go (97%) rename go/cmd/vttablet/{ => cli}/plugin_filecustomrule.go (98%) rename go/cmd/vttablet/{ => cli}/plugin_filelogger.go (98%) rename go/cmd/vttablet/{ => cli}/plugin_gcsbackupstorage.go (97%) rename go/cmd/vttablet/{ => cli}/plugin_grpcbinlogplayer.go (98%) rename go/cmd/vttablet/{ => cli}/plugin_grpcbinlogstreamer.go (98%) rename go/cmd/vttablet/{ => cli}/plugin_grpcqueryservice.go (98%) rename go/cmd/vttablet/{ => cli}/plugin_grpctabletconn.go (98%) rename go/cmd/vttablet/{ => cli}/plugin_grpcthrottlerserver.go (98%) rename go/cmd/vttablet/{ => cli}/plugin_grpctmclient.go (98%) rename go/cmd/vttablet/{ => cli}/plugin_grpctmserver.go (98%) rename go/cmd/vttablet/{ => cli}/plugin_opentracing.go (98%) rename go/cmd/vttablet/{ => cli}/plugin_opentsdb.go (98%) rename go/cmd/vttablet/{ => cli}/plugin_prometheusbackend.go (98%) rename go/cmd/vttablet/{ => cli}/plugin_s3backupstorage.go (97%) create mode 100644 go/cmd/vttablet/cli/plugin_statsd.go rename go/cmd/vttablet/{ => cli}/plugin_sysloglogger.go (98%) rename go/cmd/vttablet/{ => cli}/plugin_topocustomrule.go (98%) rename go/cmd/vttablet/{ => cli}/plugin_zk2topo.go (98%) rename go/cmd/vttablet/{ => cli}/status.go (98%) create mode 100644 go/cmd/vttablet/docgen/main.go delete mode 100644 go/cmd/vttablet/plugin_statsd.go diff --git a/go/cmd/vttablet/cli/cli.go b/go/cmd/vttablet/cli/cli.go new file mode 100644 index 00000000000..f943f5b490d --- /dev/null +++ b/go/cmd/vttablet/cli/cli.go @@ -0,0 +1,241 @@ +/* +Copyright 2023 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 agreedto 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 cli + +import ( + "bytes" + "context" + "fmt" + "os" + "time" + + "github.com/spf13/cobra" + + "vitess.io/vitess/go/acl" + "vitess.io/vitess/go/vt/binlog" + "vitess.io/vitess/go/vt/dbconfigs" + "vitess.io/vitess/go/vt/log" + "vitess.io/vitess/go/vt/mysqlctl" + "vitess.io/vitess/go/vt/servenv" + "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/vttablet/onlineddl" + "vitess.io/vitess/go/vt/vttablet/tabletmanager" + "vitess.io/vitess/go/vt/vttablet/tabletmanager/vdiff" + "vitess.io/vitess/go/vt/vttablet/tabletmanager/vreplication" + "vitess.io/vitess/go/vt/vttablet/tabletserver" + "vitess.io/vitess/go/vt/vttablet/tabletserver/tabletenv" + "vitess.io/vitess/go/yaml2" + "vitess.io/vitess/resources" + + topodatapb "vitess.io/vitess/go/vt/proto/topodata" +) + +var ( + enforceTableACLConfig bool + tableACLConfig string + tableACLConfigReloadInterval time.Duration + tabletPath string + tabletConfig string + + tm *tabletmanager.TabletManager + + Main = &cobra.Command{ + Use: "vttablet", + Short: "", // TODO + Args: cobra.NoArgs, + Version: servenv.AppVersion.String(), + PreRunE: servenv.CobraPreRunE, + RunE: run, + } +) + +func run(cmd *cobra.Command, args []string) error { + servenv.Init() + defer servenv.Close() + + tabletAlias, err := topoproto.ParseTabletAlias(tabletPath) + if err != nil { + return fmt.Errorf("failed to parse --tablet-path: %w", err) + } + + // config and mycnf initializations are intertwined. + config, mycnf, err := initConfig(tabletAlias) + if err != nil { + return err + } + + ts := topo.Open() + qsc, err := createTabletServer(context.Background(), config, ts, tabletAlias) + if err != nil { + ts.Close() + return err + } + + mysqld := mysqlctl.NewMysqld(config.DB) + servenv.OnClose(mysqld.Close) + + if err := extractOnlineDDL(); err != nil { + ts.Close() + return fmt.Errorf("failed to extract online DDL binaries: %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) + if err != nil { + return fmt.Errorf("failed to parse --tablet-path: %w", err) + } + tm = &tabletmanager.TabletManager{ + BatchCtx: context.Background(), + TopoServer: ts, + Cnf: mycnf, + MysqlDaemon: mysqld, + DBConfigs: config.DB.Clone(), + QueryServiceControl: qsc, + UpdateStream: binlog.NewUpdateStream(ts, tablet.Keyspace, tabletAlias.Cell, qsc.SchemaEngine()), + VREngine: vreplication.NewEngine(config, ts, tabletAlias.Cell, mysqld, qsc.LagThrottler()), + VDiffEngine: vdiff.NewEngine(config, ts, tablet), + } + if err := tm.Start(tablet, config.Healthcheck.IntervalSeconds.Get()); err != nil { + ts.Close() + return fmt.Errorf("failed to parse --tablet-path or initialize DB credentials: %w", err) + } + servenv.OnClose(func() { + // Close the tm so that our topo entry gets pruned properly and any + // background goroutines that use the topo connection are stopped. + tm.Close() + + // tm uses ts. So, it should be closed after tm. + ts.Close() + }) + + servenv.RunDefault() + + return nil +} + +func initConfig(tabletAlias *topodatapb.TabletAlias) (*tabletenv.TabletConfig, *mysqlctl.Mycnf, error) { + tabletenv.Init() + // Load current config after tabletenv.Init, because it changes it. + config := tabletenv.NewCurrentConfig() + if err := config.Verify(); err != nil { + return nil, nil, fmt.Errorf("invalid config: %w", err) + } + + if tabletConfig != "" { + bytes, err := os.ReadFile(tabletConfig) + if err != nil { + return nil, nil, fmt.Errorf("error reading config file %s: %w", tabletConfig, err) + } + if err := yaml2.Unmarshal(bytes, config); err != nil { + return nil, nil, fmt.Errorf("error parsing config file %s: %w", bytes, err) + } + } + gotBytes, _ := yaml2.Marshal(config) + log.Infof("Loaded config file %s successfully:\n%s", tabletConfig, gotBytes) + + var ( + mycnf *mysqlctl.Mycnf + socketFile string + ) + // If no connection parameters were specified, load the mycnf file + // and use the socket from it. If connection parameters were specified, + // we assume that the mysql is not local, and we skip loading mycnf. + // This also means that backup and restore will not be allowed. + if !config.DB.HasGlobalSettings() { + var err error + if mycnf, err = mysqlctl.NewMycnfFromFlags(tabletAlias.Uid); err != nil { + return nil, nil, fmt.Errorf("mycnf read failed: %w", err) + } + + socketFile = mycnf.SocketFile + } else { + log.Info("connection parameters were specified. Not loading my.cnf.") + } + + // If connection parameters were specified, socketFile will be empty. + // Otherwise, the socketFile (read from mycnf) will be used to initialize + // dbconfigs. + config.DB.InitWithSocket(socketFile) + for _, cfg := range config.ExternalConnections { + cfg.InitWithSocket("") + } + return config, mycnf, nil +} + +// extractOnlineDDL extracts the gh-ost binary from this executable. gh-ost is appended +// to vttablet executable by `make build` with a go:embed +func extractOnlineDDL() error { + if binaryFileName, isOverride := onlineddl.GhostBinaryFileName(); !isOverride { + if err := os.WriteFile(binaryFileName, resources.GhostBinary, 0755); err != nil { + // One possibility of failure is that gh-ost is up and running. In that case, + // let's pause and check if the running gh-ost is exact same binary as the one we wish to extract. + foundBytes, _ := os.ReadFile(binaryFileName) + if bytes.Equal(resources.GhostBinary, foundBytes) { + // OK, it's the same binary, there is no need to extract the file anyway + return nil + } + return err + } + } + + return nil +} + +func createTabletServer(ctx context.Context, 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{}) + } else if enforceTableACLConfig { + return nil, fmt.Errorf("table acl config has to be specified with table-acl-config flag because enforce-tableacl-config is set.") + } + // creates and registers the query service + qsc := tabletserver.NewTabletServer(ctx, "", config, ts, tabletAlias) + servenv.OnRun(func() { + qsc.Register() + addStatusParts(qsc) + }) + servenv.OnClose(qsc.StopService) + qsc.InitACL(tableACLConfig, enforceTableACLConfig, tableACLConfigReloadInterval) + return qsc, nil +} + +func init() { + servenv.RegisterDefaultFlags() + servenv.RegisterFlags() + servenv.RegisterGRPCServerFlags() + servenv.RegisterGRPCServerAuthFlags() + servenv.RegisterServiceMapFlag() + + dbconfigs.RegisterFlags(dbconfigs.All...) + mysqlctl.RegisterFlags() + + servenv.MoveFlagsToCobraCommand(Main) + + acl.RegisterFlags(Main.Flags()) + Main.Flags().BoolVar(&enforceTableACLConfig, "enforce-tableacl-config", enforceTableACLConfig, "if this flag is true, vttablet will fail to start if a valid tableacl config does not exist") + Main.Flags().StringVar(&tableACLConfig, "table-acl-config", tableACLConfig, "path to table access checker config file; send SIGHUP to reload this file") + Main.Flags().DurationVar(&tableACLConfigReloadInterval, "table-acl-config-reload-interval", tableACLConfigReloadInterval, "Ticker to reload ACLs. Duration flag, format e.g.: 30s. Default: do not reload") + Main.Flags().StringVar(&tabletPath, "tablet-path", tabletPath, "tablet alias") + Main.Flags().StringVar(&tabletConfig, "tablet_config", tabletConfig, "YAML file config for tablet") +} diff --git a/go/cmd/vttablet/plugin_azblobbackupstorage.go b/go/cmd/vttablet/cli/plugin_azblobbackupstorage.go similarity index 97% rename from go/cmd/vttablet/plugin_azblobbackupstorage.go rename to go/cmd/vttablet/cli/plugin_azblobbackupstorage.go index a4ca64096a9..bdadc894aae 100644 --- a/go/cmd/vttablet/plugin_azblobbackupstorage.go +++ b/go/cmd/vttablet/cli/plugin_azblobbackupstorage.go @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and limitations under the License. */ -package main +package cli import ( _ "vitess.io/vitess/go/vt/mysqlctl/azblobbackupstorage" diff --git a/go/cmd/vttablet/plugin_cephbackupstorage.go b/go/cmd/vttablet/cli/plugin_cephbackupstorage.go similarity index 97% rename from go/cmd/vttablet/plugin_cephbackupstorage.go rename to go/cmd/vttablet/cli/plugin_cephbackupstorage.go index 6cd2d5619d0..171198f5e29 100644 --- a/go/cmd/vttablet/plugin_cephbackupstorage.go +++ b/go/cmd/vttablet/cli/plugin_cephbackupstorage.go @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and limitations under the License. */ -package main +package cli import ( _ "vitess.io/vitess/go/vt/mysqlctl/cephbackupstorage" diff --git a/go/cmd/vttablet/plugin_consultopo.go b/go/cmd/vttablet/cli/plugin_consultopo.go similarity index 98% rename from go/cmd/vttablet/plugin_consultopo.go rename to go/cmd/vttablet/cli/plugin_consultopo.go index 59d6774fdbc..a128f294a42 100644 --- a/go/cmd/vttablet/plugin_consultopo.go +++ b/go/cmd/vttablet/cli/plugin_consultopo.go @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and limitations under the License. */ -package main +package cli // This plugin imports consultopo to register the consul implementation of TopoServer. diff --git a/go/cmd/vttablet/plugin_etcd2topo.go b/go/cmd/vttablet/cli/plugin_etcd2topo.go similarity index 98% rename from go/cmd/vttablet/plugin_etcd2topo.go rename to go/cmd/vttablet/cli/plugin_etcd2topo.go index d99ef51d4af..5a51923cf00 100644 --- a/go/cmd/vttablet/plugin_etcd2topo.go +++ b/go/cmd/vttablet/cli/plugin_etcd2topo.go @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and limitations under the License. */ -package main +package cli // This plugin imports etcd2topo to register the etcd2 implementation of TopoServer. diff --git a/go/cmd/vttablet/plugin_filebackupstorage.go b/go/cmd/vttablet/cli/plugin_filebackupstorage.go similarity index 97% rename from go/cmd/vttablet/plugin_filebackupstorage.go rename to go/cmd/vttablet/cli/plugin_filebackupstorage.go index cf2ceb5150f..9edc82d6a1b 100644 --- a/go/cmd/vttablet/plugin_filebackupstorage.go +++ b/go/cmd/vttablet/cli/plugin_filebackupstorage.go @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and limitations under the License. */ -package main +package cli import ( _ "vitess.io/vitess/go/vt/mysqlctl/filebackupstorage" diff --git a/go/cmd/vttablet/plugin_filecustomrule.go b/go/cmd/vttablet/cli/plugin_filecustomrule.go similarity index 98% rename from go/cmd/vttablet/plugin_filecustomrule.go rename to go/cmd/vttablet/cli/plugin_filecustomrule.go index 854c484d3c1..1bf3c4297d5 100644 --- a/go/cmd/vttablet/plugin_filecustomrule.go +++ b/go/cmd/vttablet/cli/plugin_filecustomrule.go @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and limitations under the License. */ -package main +package cli // Imports and register the file custom rule source diff --git a/go/cmd/vttablet/plugin_filelogger.go b/go/cmd/vttablet/cli/plugin_filelogger.go similarity index 98% rename from go/cmd/vttablet/plugin_filelogger.go rename to go/cmd/vttablet/cli/plugin_filelogger.go index bc5d968d2f7..fd5104f69a8 100644 --- a/go/cmd/vttablet/plugin_filelogger.go +++ b/go/cmd/vttablet/cli/plugin_filelogger.go @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and limitations under the License. */ -package main +package cli // Imports and register the file-based query logger diff --git a/go/cmd/vttablet/plugin_gcsbackupstorage.go b/go/cmd/vttablet/cli/plugin_gcsbackupstorage.go similarity index 97% rename from go/cmd/vttablet/plugin_gcsbackupstorage.go rename to go/cmd/vttablet/cli/plugin_gcsbackupstorage.go index 82a22cef1da..655583c8ca2 100644 --- a/go/cmd/vttablet/plugin_gcsbackupstorage.go +++ b/go/cmd/vttablet/cli/plugin_gcsbackupstorage.go @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and limitations under the License. */ -package main +package cli import ( _ "vitess.io/vitess/go/vt/mysqlctl/gcsbackupstorage" diff --git a/go/cmd/vttablet/plugin_grpcbinlogplayer.go b/go/cmd/vttablet/cli/plugin_grpcbinlogplayer.go similarity index 98% rename from go/cmd/vttablet/plugin_grpcbinlogplayer.go rename to go/cmd/vttablet/cli/plugin_grpcbinlogplayer.go index f8b2380c7c7..31920b97fae 100644 --- a/go/cmd/vttablet/plugin_grpcbinlogplayer.go +++ b/go/cmd/vttablet/cli/plugin_grpcbinlogplayer.go @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and limitations under the License. */ -package main +package cli // Imports and register the gRPC binlog player diff --git a/go/cmd/vttablet/plugin_grpcbinlogstreamer.go b/go/cmd/vttablet/cli/plugin_grpcbinlogstreamer.go similarity index 98% rename from go/cmd/vttablet/plugin_grpcbinlogstreamer.go rename to go/cmd/vttablet/cli/plugin_grpcbinlogstreamer.go index 26683ea7ccf..716dd499785 100644 --- a/go/cmd/vttablet/plugin_grpcbinlogstreamer.go +++ b/go/cmd/vttablet/cli/plugin_grpcbinlogstreamer.go @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and limitations under the License. */ -package main +package cli // Imports and register the gRPC binlog streamer diff --git a/go/cmd/vttablet/plugin_grpcqueryservice.go b/go/cmd/vttablet/cli/plugin_grpcqueryservice.go similarity index 98% rename from go/cmd/vttablet/plugin_grpcqueryservice.go rename to go/cmd/vttablet/cli/plugin_grpcqueryservice.go index 073c2009151..a46701d16aa 100644 --- a/go/cmd/vttablet/plugin_grpcqueryservice.go +++ b/go/cmd/vttablet/cli/plugin_grpcqueryservice.go @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and limitations under the License. */ -package main +package cli // Imports and register the gRPC queryservice server diff --git a/go/cmd/vttablet/plugin_grpctabletconn.go b/go/cmd/vttablet/cli/plugin_grpctabletconn.go similarity index 98% rename from go/cmd/vttablet/plugin_grpctabletconn.go rename to go/cmd/vttablet/cli/plugin_grpctabletconn.go index 08291a7c916..4a97e36eec4 100644 --- a/go/cmd/vttablet/plugin_grpctabletconn.go +++ b/go/cmd/vttablet/cli/plugin_grpctabletconn.go @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and limitations under the License. */ -package main +package cli // Imports and register the gRPC tabletconn client diff --git a/go/cmd/vttablet/plugin_grpcthrottlerserver.go b/go/cmd/vttablet/cli/plugin_grpcthrottlerserver.go similarity index 98% rename from go/cmd/vttablet/plugin_grpcthrottlerserver.go rename to go/cmd/vttablet/cli/plugin_grpcthrottlerserver.go index 40cce4bd51c..f25fdb73df3 100644 --- a/go/cmd/vttablet/plugin_grpcthrottlerserver.go +++ b/go/cmd/vttablet/cli/plugin_grpcthrottlerserver.go @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and limitations under the License. */ -package main +package cli // Imports and register the gRPC throttler server. diff --git a/go/cmd/vttablet/plugin_grpctmclient.go b/go/cmd/vttablet/cli/plugin_grpctmclient.go similarity index 98% rename from go/cmd/vttablet/plugin_grpctmclient.go rename to go/cmd/vttablet/cli/plugin_grpctmclient.go index ce554da96df..8cd349c7f87 100644 --- a/go/cmd/vttablet/plugin_grpctmclient.go +++ b/go/cmd/vttablet/cli/plugin_grpctmclient.go @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and limitations under the License. */ -package main +package cli // Imports and register the gRPC tabletmanager client diff --git a/go/cmd/vttablet/plugin_grpctmserver.go b/go/cmd/vttablet/cli/plugin_grpctmserver.go similarity index 98% rename from go/cmd/vttablet/plugin_grpctmserver.go rename to go/cmd/vttablet/cli/plugin_grpctmserver.go index 094d273fe39..6dee0146c21 100644 --- a/go/cmd/vttablet/plugin_grpctmserver.go +++ b/go/cmd/vttablet/cli/plugin_grpctmserver.go @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and limitations under the License. */ -package main +package cli // Imports and register the gRPC tabletmanager server diff --git a/go/cmd/vttablet/plugin_opentracing.go b/go/cmd/vttablet/cli/plugin_opentracing.go similarity index 98% rename from go/cmd/vttablet/plugin_opentracing.go rename to go/cmd/vttablet/cli/plugin_opentracing.go index 942bb25c895..f836daf4036 100644 --- a/go/cmd/vttablet/plugin_opentracing.go +++ b/go/cmd/vttablet/cli/plugin_opentracing.go @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and limitations under the License. */ -package main +package cli import ( "vitess.io/vitess/go/trace" diff --git a/go/cmd/vttablet/plugin_opentsdb.go b/go/cmd/vttablet/cli/plugin_opentsdb.go similarity index 98% rename from go/cmd/vttablet/plugin_opentsdb.go rename to go/cmd/vttablet/cli/plugin_opentsdb.go index 494dbbee20d..328628c2a3d 100644 --- a/go/cmd/vttablet/plugin_opentsdb.go +++ b/go/cmd/vttablet/cli/plugin_opentsdb.go @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and limitations under the License. */ -package main +package cli // This plugin imports opentsdb to register the opentsdb stats backend. diff --git a/go/cmd/vttablet/plugin_prometheusbackend.go b/go/cmd/vttablet/cli/plugin_prometheusbackend.go similarity index 98% rename from go/cmd/vttablet/plugin_prometheusbackend.go rename to go/cmd/vttablet/cli/plugin_prometheusbackend.go index 4066b5ba6ec..a169c6d9777 100644 --- a/go/cmd/vttablet/plugin_prometheusbackend.go +++ b/go/cmd/vttablet/cli/plugin_prometheusbackend.go @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and limitations under the License. */ -package main +package cli // This plugin imports Prometheus to allow for instrumentation // with the Prometheus client library diff --git a/go/cmd/vttablet/plugin_s3backupstorage.go b/go/cmd/vttablet/cli/plugin_s3backupstorage.go similarity index 97% rename from go/cmd/vttablet/plugin_s3backupstorage.go rename to go/cmd/vttablet/cli/plugin_s3backupstorage.go index a5b5c671ebb..4b3ecb33edb 100644 --- a/go/cmd/vttablet/plugin_s3backupstorage.go +++ b/go/cmd/vttablet/cli/plugin_s3backupstorage.go @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and limitations under the License. */ -package main +package cli import ( _ "vitess.io/vitess/go/vt/mysqlctl/s3backupstorage" diff --git a/go/cmd/vttablet/cli/plugin_statsd.go b/go/cmd/vttablet/cli/plugin_statsd.go new file mode 100644 index 00000000000..189e0367eb0 --- /dev/null +++ b/go/cmd/vttablet/cli/plugin_statsd.go @@ -0,0 +1,22 @@ +/* +Copyright 2023 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 cli + +import "vitess.io/vitess/go/stats/statsd" + +func init() { + statsd.Init("vttablet") +} diff --git a/go/cmd/vttablet/plugin_sysloglogger.go b/go/cmd/vttablet/cli/plugin_sysloglogger.go similarity index 98% rename from go/cmd/vttablet/plugin_sysloglogger.go rename to go/cmd/vttablet/cli/plugin_sysloglogger.go index 4c57ad006c3..a7260d6f8cc 100644 --- a/go/cmd/vttablet/plugin_sysloglogger.go +++ b/go/cmd/vttablet/cli/plugin_sysloglogger.go @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and limitations under the License. */ -package main +package cli // Imports and register the syslog-based query logger diff --git a/go/cmd/vttablet/plugin_topocustomrule.go b/go/cmd/vttablet/cli/plugin_topocustomrule.go similarity index 98% rename from go/cmd/vttablet/plugin_topocustomrule.go rename to go/cmd/vttablet/cli/plugin_topocustomrule.go index cef81458155..9fce319558e 100644 --- a/go/cmd/vttablet/plugin_topocustomrule.go +++ b/go/cmd/vttablet/cli/plugin_topocustomrule.go @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and limitations under the License. */ -package main +package cli // Imports and register the topo custom rule source diff --git a/go/cmd/vttablet/plugin_zk2topo.go b/go/cmd/vttablet/cli/plugin_zk2topo.go similarity index 98% rename from go/cmd/vttablet/plugin_zk2topo.go rename to go/cmd/vttablet/cli/plugin_zk2topo.go index ebf385ec1af..d71a7e2e196 100644 --- a/go/cmd/vttablet/plugin_zk2topo.go +++ b/go/cmd/vttablet/cli/plugin_zk2topo.go @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and limitations under the License. */ -package main +package cli // Imports and register the zk2 TopologyServer diff --git a/go/cmd/vttablet/status.go b/go/cmd/vttablet/cli/status.go similarity index 98% rename from go/cmd/vttablet/status.go rename to go/cmd/vttablet/cli/status.go index ff3b65134c7..ec2460b0d19 100644 --- a/go/cmd/vttablet/status.go +++ b/go/cmd/vttablet/cli/status.go @@ -1,5 +1,5 @@ /* -Copyright 2019 The Vitess Authors. +Copyright 2023 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. @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and limitations under the License. */ -package main +package cli import ( "vitess.io/vitess/go/vt/servenv" diff --git a/go/cmd/vttablet/docgen/main.go b/go/cmd/vttablet/docgen/main.go new file mode 100644 index 00000000000..9915d641352 --- /dev/null +++ b/go/cmd/vttablet/docgen/main.go @@ -0,0 +1,37 @@ +/* +Copyright 2023 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 main + +import ( + "github.com/spf13/cobra" + + "vitess.io/vitess/go/cmd/internal/docgen" + "vitess.io/vitess/go/cmd/vttablet/cli" +) + +func main() { + var dir string + cmd := cobra.Command{ + Use: "docgen [-d ]", + RunE: func(cmd *cobra.Command, args []string) error { + return docgen.GenerateMarkdownTree(cli.Main, dir) + }, + } + + cmd.Flags().StringVarP(&dir, "dir", "d", "doc", "output directory to write documentation") + _ = cmd.Execute() +} diff --git a/go/cmd/vttablet/plugin_statsd.go b/go/cmd/vttablet/plugin_statsd.go deleted file mode 100644 index 51761e6c406..00000000000 --- a/go/cmd/vttablet/plugin_statsd.go +++ /dev/null @@ -1,7 +0,0 @@ -package main - -import "vitess.io/vitess/go/stats/statsd" - -func init() { - statsd.Init("vttablet") -} diff --git a/go/cmd/vttablet/vttablet.go b/go/cmd/vttablet/vttablet.go index 34f4ab77d64..0f91f48b649 100644 --- a/go/cmd/vttablet/vttablet.go +++ b/go/cmd/vttablet/vttablet.go @@ -18,206 +18,12 @@ limitations under the License. package main import ( - "bytes" - "context" - "os" - "time" - - "github.com/spf13/pflag" - - "vitess.io/vitess/go/acl" - "vitess.io/vitess/go/vt/binlog" - "vitess.io/vitess/go/vt/dbconfigs" + "vitess.io/vitess/go/cmd/vttablet/cli" "vitess.io/vitess/go/vt/log" - "vitess.io/vitess/go/vt/mysqlctl" - "vitess.io/vitess/go/vt/servenv" - "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/vttablet/onlineddl" - "vitess.io/vitess/go/vt/vttablet/tabletmanager" - "vitess.io/vitess/go/vt/vttablet/tabletmanager/vdiff" - "vitess.io/vitess/go/vt/vttablet/tabletmanager/vreplication" - "vitess.io/vitess/go/vt/vttablet/tabletserver" - "vitess.io/vitess/go/vt/vttablet/tabletserver/tabletenv" - "vitess.io/vitess/go/yaml2" - "vitess.io/vitess/resources" - - topodatapb "vitess.io/vitess/go/vt/proto/topodata" -) - -var ( - enforceTableACLConfig bool - tableACLConfig string - tableACLConfigReloadInterval time.Duration - tabletPath string - tabletConfig string - - tm *tabletmanager.TabletManager ) -func registerFlags(fs *pflag.FlagSet) { - fs.BoolVar(&enforceTableACLConfig, "enforce-tableacl-config", enforceTableACLConfig, "if this flag is true, vttablet will fail to start if a valid tableacl config does not exist") - fs.StringVar(&tableACLConfig, "table-acl-config", tableACLConfig, "path to table access checker config file; send SIGHUP to reload this file") - fs.DurationVar(&tableACLConfigReloadInterval, "table-acl-config-reload-interval", tableACLConfigReloadInterval, "Ticker to reload ACLs. Duration flag, format e.g.: 30s. Default: do not reload") - fs.StringVar(&tabletPath, "tablet-path", tabletPath, "tablet alias") - fs.StringVar(&tabletConfig, "tablet_config", tabletConfig, "YAML file config for tablet") - - acl.RegisterFlags(fs) -} - -func init() { - servenv.RegisterDefaultFlags() - servenv.RegisterFlags() - servenv.RegisterGRPCServerFlags() - servenv.RegisterGRPCServerAuthFlags() - servenv.RegisterServiceMapFlag() - servenv.OnParseFor("vttablet", registerFlags) -} - func main() { - dbconfigs.RegisterFlags(dbconfigs.All...) - mysqlctl.RegisterFlags() - - servenv.ParseFlags("vttablet") - servenv.Init() - - if tabletPath == "" { - log.Exit("--tablet-path required") - } - tabletAlias, err := topoproto.ParseTabletAlias(tabletPath) - if err != nil { - log.Exitf("failed to parse --tablet-path: %v", err) - } - - // config and mycnf initializations are intertwined. - config, mycnf := initConfig(tabletAlias) - - ts := topo.Open() - qsc := createTabletServer(context.Background(), config, ts, tabletAlias) - - mysqld := mysqlctl.NewMysqld(config.DB) - servenv.OnClose(mysqld.Close) - - if err := extractOnlineDDL(); err != nil { - log.Exitf("failed to extract online DDL binaries: %v", 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) - if err != nil { - log.Exitf("failed to parse --tablet-path: %v", err) - } - tm = &tabletmanager.TabletManager{ - BatchCtx: context.Background(), - TopoServer: ts, - Cnf: mycnf, - MysqlDaemon: mysqld, - DBConfigs: config.DB.Clone(), - QueryServiceControl: qsc, - UpdateStream: binlog.NewUpdateStream(ts, tablet.Keyspace, tabletAlias.Cell, qsc.SchemaEngine()), - VREngine: vreplication.NewEngine(config, ts, tabletAlias.Cell, mysqld, qsc.LagThrottler()), - VDiffEngine: vdiff.NewEngine(config, ts, tablet), - } - if err := tm.Start(tablet, config.Healthcheck.IntervalSeconds.Get()); err != nil { - log.Exitf("failed to parse --tablet-path or initialize DB credentials: %v", err) - } - servenv.OnClose(func() { - // Close the tm so that our topo entry gets pruned properly and any - // background goroutines that use the topo connection are stopped. - tm.Close() - - // tm uses ts. So, it should be closed after tm. - ts.Close() - }) - - servenv.RunDefault() -} - -func initConfig(tabletAlias *topodatapb.TabletAlias) (*tabletenv.TabletConfig, *mysqlctl.Mycnf) { - tabletenv.Init() - // Load current config after tabletenv.Init, because it changes it. - config := tabletenv.NewCurrentConfig() - if err := config.Verify(); err != nil { - log.Exitf("invalid config: %v", err) - } - - if tabletConfig != "" { - bytes, err := os.ReadFile(tabletConfig) - if err != nil { - log.Exitf("error reading config file %s: %v", tabletConfig, err) - } - if err := yaml2.Unmarshal(bytes, config); err != nil { - log.Exitf("error parsing config file %s: %v", bytes, err) - } - } - gotBytes, _ := yaml2.Marshal(config) - log.Infof("Loaded config file %s successfully:\n%s", tabletConfig, gotBytes) - - var mycnf *mysqlctl.Mycnf - var socketFile string - // If no connection parameters were specified, load the mycnf file - // and use the socket from it. If connection parameters were specified, - // we assume that the mysql is not local, and we skip loading mycnf. - // This also means that backup and restore will not be allowed. - if !config.DB.HasGlobalSettings() { - var err error - if mycnf, err = mysqlctl.NewMycnfFromFlags(tabletAlias.Uid); err != nil { - log.Exitf("mycnf read failed: %v", err) - } - socketFile = mycnf.SocketFile - } else { - log.Info("connection parameters were specified. Not loading my.cnf.") - } - - // If connection parameters were specified, socketFile will be empty. - // Otherwise, the socketFile (read from mycnf) will be used to initialize - // dbconfigs. - config.DB.InitWithSocket(socketFile) - for _, cfg := range config.ExternalConnections { - cfg.InitWithSocket("") - } - return config, mycnf -} - -// extractOnlineDDL extracts the gh-ost binary from this executable. gh-ost is appended -// to vttablet executable by `make build` with a go:embed -func extractOnlineDDL() error { - if binaryFileName, isOverride := onlineddl.GhostBinaryFileName(); !isOverride { - if err := os.WriteFile(binaryFileName, resources.GhostBinary, 0755); err != nil { - // One possibility of failure is that gh-ost is up and running. In that case, - // let's pause and check if the running gh-ost is exact same binary as the one we wish to extract. - foundBytes, _ := os.ReadFile(binaryFileName) - if bytes.Equal(resources.GhostBinary, foundBytes) { - // OK, it's the same binary, there is no need to extract the file anyway - return nil - } - return err - } - } - - return nil -} - -func createTabletServer(ctx context.Context, config *tabletenv.TabletConfig, ts *topo.Server, tabletAlias *topodatapb.TabletAlias) *tabletserver.TabletServer { - if tableACLConfig != "" { - // To override default simpleacl, other ACL plugins must set themselves to be default ACL factory - tableacl.Register("simpleacl", &simpleacl.Factory{}) - } else if enforceTableACLConfig { - log.Exit("table acl config has to be specified with table-acl-config flag because enforce-tableacl-config is set.") + if err := cli.Main.Execute(); err != nil { + log.Exit(err) } - // creates and registers the query service - qsc := tabletserver.NewTabletServer(ctx, "", config, ts, tabletAlias) - servenv.OnRun(func() { - qsc.Register() - addStatusParts(qsc) - }) - servenv.OnClose(qsc.StopService) - qsc.InitACL(tableACLConfig, enforceTableACLConfig, tableACLConfigReloadInterval) - return qsc } diff --git a/go/flags/endtoend/vttablet.txt b/go/flags/endtoend/vttablet.txt index 9b42ebb5644..62cc8de3d0f 100644 --- a/go/flags/endtoend/vttablet.txt +++ b/go/flags/endtoend/vttablet.txt @@ -1,4 +1,7 @@ -Usage of vttablet: +Usage: + vttablet [flags] + +Flags: --alsologtostderr log to standard error as well as files --app_idle_timeout duration Idle timeout for app connections (default 1m0s) --app_pool_size int Size of the connection pool for app connections (default 40) @@ -152,7 +155,7 @@ Usage of vttablet: --heartbeat_enable If true, vttablet records (if master) or checks (if replica) the current time of a replication heartbeat in the sidecar database's heartbeat table. The result is used to inform the serving state of the vttablet via healthchecks. --heartbeat_interval duration How frequently to read and write replication heartbeat. (default 1s) --heartbeat_on_demand_duration duration If non-zero, heartbeats are only written upon consumer request, and only run for up to given duration following the request. Frequent requests can keep the heartbeat running consistently; when requests are infrequent heartbeat may completely stop between requests - -h, --help display usage and exit + -h, --help help for vttablet --hot_row_protection_concurrent_transactions int Number of concurrent transactions let through to the txpool/MySQL for the same hot row. Should be > 1 to have enough 'ready' transactions in MySQL and benefit from a pipelining effect. (default 5) --hot_row_protection_max_global_queue_size int Global queue limit across all row (ranges). Useful to prevent that the queue can grow unbounded. (default 1000) --hot_row_protection_max_queue_size int Maximum number of BeginExecute RPCs which will be queued for the same row (range). (default 20) @@ -257,8 +260,6 @@ Usage of vttablet: --restore_from_backup (init restore parameter) will check BackupStorage for a recent backup at startup and start there --restore_from_backup_ts string (init restore parameter) if set, restore the latest backup taken at or before this timestamp. Example: '2021-04-29.133050' --retain_online_ddl_tables duration How long should vttablet keep an old migrated table before purging it (default 24h0m0s) - --s2a_enable_appengine_dialer If true, opportunistically use AppEngine-specific dialer to call S2A. - --s2a_timeout duration Timeout enforced on the connection to the S2A service for handshake. (default 3s) --s3_backup_aws_endpoint string endpoint of the S3 backend (region must be provided). --s3_backup_aws_region string AWS region to use. (default "us-east-1") --s3_backup_aws_retries int AWS request retries. (default -1) From e10710626bd32c371dfd5d1ada339cde9c3a0174 Mon Sep 17 00:00:00 2001 From: Andrew Mason Date: Thu, 7 Sep 2023 17:15:52 -0400 Subject: [PATCH 2/9] migrate vtgate to cobra Signed-off-by: Andrew Mason --- go/cmd/vtgate/cli/cli.go | 173 ++++++++++++++++++ .../{ => cli}/plugin_auth_clientcert.go | 2 +- go/cmd/vtgate/{ => cli}/plugin_auth_ldap.go | 2 +- go/cmd/vtgate/{ => cli}/plugin_auth_static.go | 2 +- go/cmd/vtgate/{ => cli}/plugin_auth_vault.go | 2 +- go/cmd/vtgate/{ => cli}/plugin_consultopo.go | 2 +- go/cmd/vtgate/{ => cli}/plugin_etcd2topo.go | 2 +- .../vtgate/{ => cli}/plugin_grpctabletconn.go | 2 +- .../{ => cli}/plugin_grpcvtgateservice.go | 2 +- go/cmd/vtgate/{ => cli}/plugin_opentracing.go | 2 +- go/cmd/vtgate/{ => cli}/plugin_opentsdb.go | 2 +- .../{ => cli}/plugin_prometheusbackend.go | 2 +- go/cmd/vtgate/cli/plugin_statsd.go | 23 +++ go/cmd/vtgate/{ => cli}/plugin_zk2topo.go | 2 +- go/cmd/vtgate/{ => cli}/status.go | 2 +- go/cmd/vtgate/docgen/main.go | 37 ++++ go/cmd/vtgate/plugin_statsd.go | 7 - go/cmd/vtgate/vtgate.go | 144 +-------------- go/flags/endtoend/vtgate.txt | 7 +- 19 files changed, 254 insertions(+), 163 deletions(-) create mode 100644 go/cmd/vtgate/cli/cli.go rename go/cmd/vtgate/{ => cli}/plugin_auth_clientcert.go (98%) rename go/cmd/vtgate/{ => cli}/plugin_auth_ldap.go (98%) rename go/cmd/vtgate/{ => cli}/plugin_auth_static.go (98%) rename go/cmd/vtgate/{ => cli}/plugin_auth_vault.go (98%) rename go/cmd/vtgate/{ => cli}/plugin_consultopo.go (98%) rename go/cmd/vtgate/{ => cli}/plugin_etcd2topo.go (98%) rename go/cmd/vtgate/{ => cli}/plugin_grpctabletconn.go (98%) rename go/cmd/vtgate/{ => cli}/plugin_grpcvtgateservice.go (98%) rename go/cmd/vtgate/{ => cli}/plugin_opentracing.go (98%) rename go/cmd/vtgate/{ => cli}/plugin_opentsdb.go (98%) rename go/cmd/vtgate/{ => cli}/plugin_prometheusbackend.go (98%) create mode 100644 go/cmd/vtgate/cli/plugin_statsd.go rename go/cmd/vtgate/{ => cli}/plugin_zk2topo.go (98%) rename go/cmd/vtgate/{ => cli}/status.go (99%) create mode 100644 go/cmd/vtgate/docgen/main.go delete mode 100644 go/cmd/vtgate/plugin_statsd.go diff --git a/go/cmd/vtgate/cli/cli.go b/go/cmd/vtgate/cli/cli.go new file mode 100644 index 00000000000..c3c7040385c --- /dev/null +++ b/go/cmd/vtgate/cli/cli.go @@ -0,0 +1,173 @@ +/* +Copyright 2023 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 agreedto 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 cli + +import ( + "context" + "fmt" + "strings" + + "github.com/spf13/cobra" + + "vitess.io/vitess/go/acl" + "vitess.io/vitess/go/exit" + "vitess.io/vitess/go/vt/discovery" + "vitess.io/vitess/go/vt/servenv" + "vitess.io/vitess/go/vt/srvtopo" + "vitess.io/vitess/go/vt/topo" + "vitess.io/vitess/go/vt/topo/topoproto" + "vitess.io/vitess/go/vt/vterrors" + "vitess.io/vitess/go/vt/vtgate" + "vitess.io/vitess/go/vt/vtgate/planbuilder/plancontext" + + topodatapb "vitess.io/vitess/go/vt/proto/topodata" + "vitess.io/vitess/go/vt/proto/vtrpc" +) + +var ( + cell string + tabletTypesToWait []topodatapb.TabletType + plannerName string + resilientServer *srvtopo.ResilientServer + + Main = &cobra.Command{ + Use: "vtgate", + Short: "", // TODO + Args: cobra.NoArgs, + Version: servenv.AppVersion.String(), + PreRunE: servenv.CobraPreRunE, + RunE: run, + } +) + +// CheckCellFlags will check validation of cell and cells_to_watch flag +// it will help to avoid strange behaviors when vtgate runs but actually does not work +func CheckCellFlags(ctx context.Context, serv srvtopo.Server, cell string, cellsToWatch string) error { + // topo check + var topoServer *topo.Server + if serv != nil { + var err error + topoServer, err = serv.GetTopoServer() + if err != nil { + return fmt.Errorf("Unable to create gateway: %w", err) + } + } else { + return fmt.Errorf("topo server cannot be nil") + } + cellsInTopo, err := topoServer.GetKnownCells(ctx) + if err != nil { + return err + } + if len(cellsInTopo) == 0 { + return vterrors.Errorf(vtrpc.Code_INVALID_ARGUMENT, "topo server should have at least one cell") + } + + // cell valid check + if cell == "" { + return vterrors.Errorf(vtrpc.Code_INVALID_ARGUMENT, "cell flag must be set") + } + hasCell := false + for _, v := range cellsInTopo { + if v == cell { + hasCell = true + break + } + } + if !hasCell { + return vterrors.Errorf(vtrpc.Code_INVALID_ARGUMENT, "cell:[%v] does not exist in topo", cell) + } + + // cells_to_watch valid check + cells := make([]string, 0, 1) + for _, c := range strings.Split(cellsToWatch, ",") { + if c == "" { + continue + } + // cell should contained in cellsInTopo + if exists := topo.InCellList(c, cellsInTopo); !exists { + return vterrors.Errorf(vtrpc.Code_INVALID_ARGUMENT, "cell: [%v] is not valid. Available cells: [%v]", c, strings.Join(cellsInTopo, ",")) + } + cells = append(cells, c) + } + if len(cells) == 0 { + return vterrors.Errorf(vtrpc.Code_INVALID_ARGUMENT, "cells_to_watch flag cannot be empty") + } + + return nil +} + +func run(cmd *cobra.Command, args []string) error { + defer exit.Recover() + + servenv.Init() + defer servenv.Close() + + ts := topo.Open() + defer ts.Close() + + resilientServer = srvtopo.NewResilientServer(context.Background(), ts, "ResilientSrvTopoServer") + + tabletTypes := make([]topodatapb.TabletType, 0, 1) + for _, tt := range tabletTypesToWait { + if topoproto.IsServingType(tt) { + tabletTypes = append(tabletTypes, tt) + } + } + + if len(tabletTypes) == 0 { + return fmt.Errorf("tablet_types_to_wait must contain at least one serving tablet type") + } + + err := CheckCellFlags(context.Background(), resilientServer, cell, vtgate.CellsToWatch) + if err != nil { + return fmt.Errorf("cells_to_watch validation failed: %v", err) + } + + plannerVersion, _ := plancontext.PlannerNameToVersion(plannerName) + + // pass nil for HealthCheck and it will be created + vtg := vtgate.Init(context.Background(), 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. + discovery.ParseTabletURLTemplateFromFlag() + addStatusParts(vtg) + }) + servenv.OnClose(func() { + _ = vtg.Gateway().Close(context.Background()) + }) + servenv.RunDefault() + + return nil +} + +func init() { + servenv.RegisterDefaultFlags() + servenv.RegisterFlags() + servenv.RegisterGRPCServerFlags() + servenv.RegisterGRPCServerAuthFlags() + servenv.RegisterServiceMapFlag() + + servenv.MoveFlagsToCobraCommand(Main) + + acl.RegisterFlags(Main.Flags()) + Main.Flags().StringVar(&cell, "cell", cell, "cell to use") + Main.Flags().Var((*topoproto.TabletTypeListFlag)(&tabletTypesToWait), "tablet_types_to_wait", "Wait till connected for specified tablet types during Gateway initialization. Should be provided as a comma-separated set of tablet types.") + Main.Flags().StringVar(&plannerName, "planner-version", plannerName, "Sets the default planner to use when the session has not changed it. Valid values are: Gen4, Gen4Greedy, Gen4Left2Right") + + Main.MarkFlagRequired("tablet_types_to_wait") +} diff --git a/go/cmd/vtgate/plugin_auth_clientcert.go b/go/cmd/vtgate/cli/plugin_auth_clientcert.go similarity index 98% rename from go/cmd/vtgate/plugin_auth_clientcert.go rename to go/cmd/vtgate/cli/plugin_auth_clientcert.go index 4f3d65ef626..1a1334e71ba 100644 --- a/go/cmd/vtgate/plugin_auth_clientcert.go +++ b/go/cmd/vtgate/cli/plugin_auth_clientcert.go @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and limitations under the License. */ -package main +package cli // This plugin imports clientcert to register the client certificate implementation of AuthServer. diff --git a/go/cmd/vtgate/plugin_auth_ldap.go b/go/cmd/vtgate/cli/plugin_auth_ldap.go similarity index 98% rename from go/cmd/vtgate/plugin_auth_ldap.go rename to go/cmd/vtgate/cli/plugin_auth_ldap.go index 257f0742733..7dc5b246f72 100644 --- a/go/cmd/vtgate/plugin_auth_ldap.go +++ b/go/cmd/vtgate/cli/plugin_auth_ldap.go @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and limitations under the License. */ -package main +package cli // This plugin imports ldapauthserver to register the LDAP implementation of AuthServer. diff --git a/go/cmd/vtgate/plugin_auth_static.go b/go/cmd/vtgate/cli/plugin_auth_static.go similarity index 98% rename from go/cmd/vtgate/plugin_auth_static.go rename to go/cmd/vtgate/cli/plugin_auth_static.go index 8e4a552cecf..9ffd60a79f2 100644 --- a/go/cmd/vtgate/plugin_auth_static.go +++ b/go/cmd/vtgate/cli/plugin_auth_static.go @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and limitations under the License. */ -package main +package cli // This plugin imports staticauthserver to register the flat-file implementation of AuthServer. diff --git a/go/cmd/vtgate/plugin_auth_vault.go b/go/cmd/vtgate/cli/plugin_auth_vault.go similarity index 98% rename from go/cmd/vtgate/plugin_auth_vault.go rename to go/cmd/vtgate/cli/plugin_auth_vault.go index ca271b496ca..2aee32e3940 100644 --- a/go/cmd/vtgate/plugin_auth_vault.go +++ b/go/cmd/vtgate/cli/plugin_auth_vault.go @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and limitations under the License. */ -package main +package cli // This plugin imports InitAuthServerVault to register the HashiCorp Vault implementation of AuthServer. diff --git a/go/cmd/vtgate/plugin_consultopo.go b/go/cmd/vtgate/cli/plugin_consultopo.go similarity index 98% rename from go/cmd/vtgate/plugin_consultopo.go rename to go/cmd/vtgate/cli/plugin_consultopo.go index 59d6774fdbc..a128f294a42 100644 --- a/go/cmd/vtgate/plugin_consultopo.go +++ b/go/cmd/vtgate/cli/plugin_consultopo.go @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and limitations under the License. */ -package main +package cli // This plugin imports consultopo to register the consul implementation of TopoServer. diff --git a/go/cmd/vtgate/plugin_etcd2topo.go b/go/cmd/vtgate/cli/plugin_etcd2topo.go similarity index 98% rename from go/cmd/vtgate/plugin_etcd2topo.go rename to go/cmd/vtgate/cli/plugin_etcd2topo.go index d99ef51d4af..5a51923cf00 100644 --- a/go/cmd/vtgate/plugin_etcd2topo.go +++ b/go/cmd/vtgate/cli/plugin_etcd2topo.go @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and limitations under the License. */ -package main +package cli // This plugin imports etcd2topo to register the etcd2 implementation of TopoServer. diff --git a/go/cmd/vtgate/plugin_grpctabletconn.go b/go/cmd/vtgate/cli/plugin_grpctabletconn.go similarity index 98% rename from go/cmd/vtgate/plugin_grpctabletconn.go rename to go/cmd/vtgate/cli/plugin_grpctabletconn.go index 08291a7c916..4a97e36eec4 100644 --- a/go/cmd/vtgate/plugin_grpctabletconn.go +++ b/go/cmd/vtgate/cli/plugin_grpctabletconn.go @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and limitations under the License. */ -package main +package cli // Imports and register the gRPC tabletconn client diff --git a/go/cmd/vtgate/plugin_grpcvtgateservice.go b/go/cmd/vtgate/cli/plugin_grpcvtgateservice.go similarity index 98% rename from go/cmd/vtgate/plugin_grpcvtgateservice.go rename to go/cmd/vtgate/cli/plugin_grpcvtgateservice.go index 4ee159710ca..bbbc6e3039e 100644 --- a/go/cmd/vtgate/plugin_grpcvtgateservice.go +++ b/go/cmd/vtgate/cli/plugin_grpcvtgateservice.go @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and limitations under the License. */ -package main +package cli // Imports and register the gRPC vtgateservice server diff --git a/go/cmd/vtgate/plugin_opentracing.go b/go/cmd/vtgate/cli/plugin_opentracing.go similarity index 98% rename from go/cmd/vtgate/plugin_opentracing.go rename to go/cmd/vtgate/cli/plugin_opentracing.go index 9a6786d3d64..7ec15423f5a 100644 --- a/go/cmd/vtgate/plugin_opentracing.go +++ b/go/cmd/vtgate/cli/plugin_opentracing.go @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and limitations under the License. */ -package main +package cli import ( "vitess.io/vitess/go/trace" diff --git a/go/cmd/vtgate/plugin_opentsdb.go b/go/cmd/vtgate/cli/plugin_opentsdb.go similarity index 98% rename from go/cmd/vtgate/plugin_opentsdb.go rename to go/cmd/vtgate/cli/plugin_opentsdb.go index 0988f3b9a64..37c81f271c9 100644 --- a/go/cmd/vtgate/plugin_opentsdb.go +++ b/go/cmd/vtgate/cli/plugin_opentsdb.go @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and limitations under the License. */ -package main +package cli // This plugin imports opentsdb to register the opentsdb stats backend. diff --git a/go/cmd/vtgate/plugin_prometheusbackend.go b/go/cmd/vtgate/cli/plugin_prometheusbackend.go similarity index 98% rename from go/cmd/vtgate/plugin_prometheusbackend.go rename to go/cmd/vtgate/cli/plugin_prometheusbackend.go index 6bffd133332..a1797abdcd1 100644 --- a/go/cmd/vtgate/plugin_prometheusbackend.go +++ b/go/cmd/vtgate/cli/plugin_prometheusbackend.go @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and limitations under the License. */ -package main +package cli // This plugin imports Prometheus to allow for instrumentation // with the Prometheus client library diff --git a/go/cmd/vtgate/cli/plugin_statsd.go b/go/cmd/vtgate/cli/plugin_statsd.go new file mode 100644 index 00000000000..fc42fa4f447 --- /dev/null +++ b/go/cmd/vtgate/cli/plugin_statsd.go @@ -0,0 +1,23 @@ +/* +Copyright 2023 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 cli + +import "vitess.io/vitess/go/stats/statsd" + +func init() { + statsd.Init("vtgate") +} diff --git a/go/cmd/vtgate/plugin_zk2topo.go b/go/cmd/vtgate/cli/plugin_zk2topo.go similarity index 98% rename from go/cmd/vtgate/plugin_zk2topo.go rename to go/cmd/vtgate/cli/plugin_zk2topo.go index d75a1c6bcb4..1870a3b2bb3 100644 --- a/go/cmd/vtgate/plugin_zk2topo.go +++ b/go/cmd/vtgate/cli/plugin_zk2topo.go @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and limitations under the License. */ -package main +package cli import ( // Imports and register the zk2 TopologyServer diff --git a/go/cmd/vtgate/status.go b/go/cmd/vtgate/cli/status.go similarity index 99% rename from go/cmd/vtgate/status.go rename to go/cmd/vtgate/cli/status.go index 436a1301438..6efbca51dc7 100644 --- a/go/cmd/vtgate/status.go +++ b/go/cmd/vtgate/cli/status.go @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and limitations under the License. */ -package main +package cli import ( "vitess.io/vitess/go/vt/discovery" diff --git a/go/cmd/vtgate/docgen/main.go b/go/cmd/vtgate/docgen/main.go new file mode 100644 index 00000000000..cd1add2ca39 --- /dev/null +++ b/go/cmd/vtgate/docgen/main.go @@ -0,0 +1,37 @@ +/* +Copyright 2023 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 main + +import ( + "github.com/spf13/cobra" + + "vitess.io/vitess/go/cmd/internal/docgen" + "vitess.io/vitess/go/cmd/vtgate/cli" +) + +func main() { + var dir string + cmd := cobra.Command{ + Use: "docgen [-d ]", + RunE: func(cmd *cobra.Command, args []string) error { + return docgen.GenerateMarkdownTree(cli.Main, dir) + }, + } + + cmd.Flags().StringVarP(&dir, "dir", "d", "doc", "output directory to write documentation") + _ = cmd.Execute() +} diff --git a/go/cmd/vtgate/plugin_statsd.go b/go/cmd/vtgate/plugin_statsd.go deleted file mode 100644 index ae2ecb5b2e0..00000000000 --- a/go/cmd/vtgate/plugin_statsd.go +++ /dev/null @@ -1,7 +0,0 @@ -package main - -import "vitess.io/vitess/go/stats/statsd" - -func init() { - statsd.Init("vtgate") -} diff --git a/go/cmd/vtgate/vtgate.go b/go/cmd/vtgate/vtgate.go index fe2be59d116..fd81fe85a68 100644 --- a/go/cmd/vtgate/vtgate.go +++ b/go/cmd/vtgate/vtgate.go @@ -17,150 +17,12 @@ limitations under the License. package main import ( - "context" - "strings" - - "github.com/spf13/pflag" - - "vitess.io/vitess/go/acl" - "vitess.io/vitess/go/exit" - "vitess.io/vitess/go/vt/discovery" + "vitess.io/vitess/go/cmd/vtgate/cli" "vitess.io/vitess/go/vt/log" - topodatapb "vitess.io/vitess/go/vt/proto/topodata" - "vitess.io/vitess/go/vt/proto/vtrpc" - "vitess.io/vitess/go/vt/servenv" - "vitess.io/vitess/go/vt/srvtopo" - "vitess.io/vitess/go/vt/topo" - "vitess.io/vitess/go/vt/topo/topoproto" - "vitess.io/vitess/go/vt/vterrors" - "vitess.io/vitess/go/vt/vtgate" - "vitess.io/vitess/go/vt/vtgate/planbuilder/plancontext" ) -var ( - cell = "" - tabletTypesToWait []topodatapb.TabletType - plannerName string -) - -func registerFlags(fs *pflag.FlagSet) { - fs.StringVar(&cell, "cell", cell, "cell to use") - fs.Var((*topoproto.TabletTypeListFlag)(&tabletTypesToWait), "tablet_types_to_wait", "Wait till connected for specified tablet types during Gateway initialization. Should be provided as a comma-separated set of tablet types.") - fs.StringVar(&plannerName, "planner-version", plannerName, "Sets the default planner to use when the session has not changed it. Valid values are: Gen4, Gen4Greedy, Gen4Left2Right") - - acl.RegisterFlags(fs) -} - -var resilientServer *srvtopo.ResilientServer - -func init() { - servenv.RegisterDefaultFlags() - servenv.RegisterFlags() - servenv.RegisterGRPCServerFlags() - servenv.RegisterGRPCServerAuthFlags() - servenv.RegisterServiceMapFlag() - servenv.OnParse(registerFlags) -} - -// CheckCellFlags will check validation of cell and cells_to_watch flag -// it will help to avoid strange behaviors when vtgate runs but actually does not work -func CheckCellFlags(ctx context.Context, serv srvtopo.Server, cell string, cellsToWatch string) error { - // topo check - var topoServer *topo.Server - if serv != nil { - var err error - topoServer, err = serv.GetTopoServer() - if err != nil { - log.Exitf("Unable to create gateway: %v", err) - } - } else { - log.Exitf("topo server cannot be nil") - } - cellsInTopo, err := topoServer.GetKnownCells(ctx) - if err != nil { - return err - } - if len(cellsInTopo) == 0 { - return vterrors.Errorf(vtrpc.Code_INVALID_ARGUMENT, "topo server should have at least one cell") - } - - // cell valid check - if cell == "" { - return vterrors.Errorf(vtrpc.Code_INVALID_ARGUMENT, "cell flag must be set") - } - hasCell := false - for _, v := range cellsInTopo { - if v == cell { - hasCell = true - break - } - } - if !hasCell { - return vterrors.Errorf(vtrpc.Code_INVALID_ARGUMENT, "cell:[%v] does not exist in topo", cell) - } - - // cells_to_watch valid check - cells := make([]string, 0, 1) - for _, c := range strings.Split(cellsToWatch, ",") { - if c == "" { - continue - } - // cell should contained in cellsInTopo - if exists := topo.InCellList(c, cellsInTopo); !exists { - return vterrors.Errorf(vtrpc.Code_INVALID_ARGUMENT, "cell: [%v] is not valid. Available cells: [%v]", c, strings.Join(cellsInTopo, ",")) - } - cells = append(cells, c) - } - if len(cells) == 0 { - return vterrors.Errorf(vtrpc.Code_INVALID_ARGUMENT, "cells_to_watch flag cannot be empty") - } - - return nil -} - func main() { - defer exit.Recover() - - servenv.ParseFlags("vtgate") - servenv.Init() - - ts := topo.Open() - defer ts.Close() - - resilientServer = srvtopo.NewResilientServer(context.Background(), ts, "ResilientSrvTopoServer") - - tabletTypes := make([]topodatapb.TabletType, 0, 1) - if len(tabletTypesToWait) != 0 { - for _, tt := range tabletTypesToWait { - if topoproto.IsServingType(tt) { - tabletTypes = append(tabletTypes, tt) - } - } - } else { - log.Exitf("tablet_types_to_wait flag must be set") - } - - if len(tabletTypes) == 0 { - log.Exitf("tablet_types_to_wait should contain at least one serving tablet type") - } - - err := CheckCellFlags(context.Background(), resilientServer, cell, vtgate.CellsToWatch) - if err != nil { - log.Exitf("cells_to_watch validation failed: %v", err) + if err := cli.Main.Execute(); err != nil { + log.Exit(err) } - - plannerVersion, _ := plancontext.PlannerNameToVersion(plannerName) - - // pass nil for HealthCheck and it will be created - vtg := vtgate.Init(context.Background(), 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. - discovery.ParseTabletURLTemplateFromFlag() - addStatusParts(vtg) - }) - servenv.OnClose(func() { - _ = vtg.Gateway().Close(context.Background()) - }) - servenv.RunDefault() } diff --git a/go/flags/endtoend/vtgate.txt b/go/flags/endtoend/vtgate.txt index 3f2752be084..c7e5782d07d 100644 --- a/go/flags/endtoend/vtgate.txt +++ b/go/flags/endtoend/vtgate.txt @@ -1,4 +1,7 @@ -Usage of vtgate: +Usage: + vtgate [flags] + +Flags: --allow-kill-statement Allows the execution of kill statement --allowed_tablet_types strings Specifies the tablet types this vtgate is allowed to route queries to. Should be provided as a comma-separated set of tablet types. --alsologtostderr log to standard error as well as files @@ -69,7 +72,7 @@ Usage of vtgate: --grpc_use_effective_callerid If set, and SSL is not used, will set the immediate caller id from the effective caller id's principal. --healthcheck_retry_delay duration health check retry delay (default 2ms) --healthcheck_timeout duration the health check timeout period (default 1m0s) - -h, --help display usage and exit + -h, --help help for vtgate --jaeger-agent-host string host and port to send spans to. if empty, no tracing will be done --keep_logs duration keep logs for this long (using ctime) (zero to keep forever) --keep_logs_by_mtime duration keep logs for this long (using mtime) (zero to keep forever) From 1e90bfa90f0203956dc1571820ec2e86ddc04d21 Mon Sep 17 00:00:00 2001 From: Andrew Mason Date: Fri, 8 Sep 2023 06:27:04 -0400 Subject: [PATCH 3/9] wip help text Signed-off-by: Andrew Mason --- go/cmd/vttablet/cli/cli.go | 11 +++++++++-- go/flags/endtoend/vttablet.txt | 7 +++++++ 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/go/cmd/vttablet/cli/cli.go b/go/cmd/vttablet/cli/cli.go index f943f5b490d..84eaefad90a 100644 --- a/go/cmd/vttablet/cli/cli.go +++ b/go/cmd/vttablet/cli/cli.go @@ -57,8 +57,15 @@ var ( tm *tabletmanager.TabletManager Main = &cobra.Command{ - Use: "vttablet", - Short: "", // TODO + Use: "vttablet", + Short: "The VTTablet server controls a running MySQL server.", + Long: fmt.Sprintf(`The VTTablet server _controls_ a running MySQL server. VTTablet supports two primary types of deployments: + +* Managed MySQL (most common) +* External MySQL + +In addition to these deployment types, a partially managed VTTablet is also possible by setting %s.`, "`--disable_active_reparents`"), + Example: "", // TODO: Args: cobra.NoArgs, Version: servenv.AppVersion.String(), PreRunE: servenv.CobraPreRunE, diff --git a/go/flags/endtoend/vttablet.txt b/go/flags/endtoend/vttablet.txt index 62cc8de3d0f..12dafdc7427 100644 --- a/go/flags/endtoend/vttablet.txt +++ b/go/flags/endtoend/vttablet.txt @@ -1,3 +1,10 @@ +The VTTablet server _controls_ a running MySQL server. VTTablet supports two primary types of deployments: + +* Managed MySQL (most common) +* External MySQL + +In addition to these deployment types, a partially managed VTTablet is also possible by setting `--disable_active_reparents`. + Usage: vttablet [flags] From dd90b235d552001df1cf1711f51828afede4ea2c Mon Sep 17 00:00:00 2001 From: Andrew Mason Date: Fri, 8 Sep 2023 17:11:26 -0400 Subject: [PATCH 4/9] add help text and examples Signed-off-by: Andrew Mason --- go/cmd/vtgate/cli/cli.go | 19 +++++++++++++++++-- go/cmd/vttablet/cli/cli.go | 13 ++++++++++++- go/flags/endtoend/vtgate.txt | 19 +++++++++++++++++++ go/flags/endtoend/vttablet.txt | 16 ++++++++++++++++ 4 files changed, 64 insertions(+), 3 deletions(-) diff --git a/go/cmd/vtgate/cli/cli.go b/go/cmd/vtgate/cli/cli.go index c3c7040385c..62b51945da2 100644 --- a/go/cmd/vtgate/cli/cli.go +++ b/go/cmd/vtgate/cli/cli.go @@ -45,8 +45,23 @@ var ( resilientServer *srvtopo.ResilientServer Main = &cobra.Command{ - Use: "vtgate", - Short: "", // TODO + Use: "vtgate", + Short: "VTGate is a stateless proxy responsible for accepting requests from applications and routing them to the appropriate tablet server(s) for query execution. It speaks both the MySQL Protocol and a gRPC protocol.", + Example: ` +vtgate \ + --topo_implementation etcd2 \ + --topo_global_server_address localhost:2379 \ + --topo_global_root /vitess/global \ + --log_dir $VTDATAROOT/tmp \ + --port 15001 \ + --grpc_port 15991 \ + --mysql_server_port 15306 \ + --cell test \ + --cells_to_watch test \ + --tablet_types_to_wait PRIMARY,REPLICA \ + --service_map 'grpc-vtgateservice' \ + --pid_file $VTDATAROOT/tmp/vtgate.pid \ + --mysql_auth_server_impl none`, Args: cobra.NoArgs, Version: servenv.AppVersion.String(), PreRunE: servenv.CobraPreRunE, diff --git a/go/cmd/vttablet/cli/cli.go b/go/cmd/vttablet/cli/cli.go index 84eaefad90a..c51cd4de33c 100644 --- a/go/cmd/vttablet/cli/cli.go +++ b/go/cmd/vttablet/cli/cli.go @@ -65,7 +65,18 @@ var ( * External MySQL In addition to these deployment types, a partially managed VTTablet is also possible by setting %s.`, "`--disable_active_reparents`"), - Example: "", // TODO: + Example: ` +vttablet \ + --topo_implementation etcd2 \ + --topo_global_server_address localhost:2379 \ + --topo_global_root /vitess/ \ + --tablet-path $alias \ + --init_keyspace $keyspace \ + --init_shard $shard \ + --init_tablet_type $tablet_type \ + --port $port \ + --grpc_port $grpc_port \ + --service_map 'grpc-queryservice,grpc-tabletmanager,grpc-updatestream'` + "\n\n`$alias` needs to be of the form: `-id`, and the cell should match one of the local cells that was created in the topology. The id can be left padded with zeroes: `cell-100` and `cell-000000100` are synonymous.", Args: cobra.NoArgs, Version: servenv.AppVersion.String(), PreRunE: servenv.CobraPreRunE, diff --git a/go/flags/endtoend/vtgate.txt b/go/flags/endtoend/vtgate.txt index c7e5782d07d..dad6548a506 100644 --- a/go/flags/endtoend/vtgate.txt +++ b/go/flags/endtoend/vtgate.txt @@ -1,6 +1,25 @@ +VTGate is a stateless proxy responsible for accepting requests from applications and routing them to the appropriate tablet server(s) for query execution. It speaks both the MySQL Protocol and a gRPC protocol. + Usage: vtgate [flags] +Examples: + +vtgate \ + --topo_implementation etcd2 \ + --topo_global_server_address localhost:2379 \ + --topo_global_root /vitess/global \ + --log_dir $VTDATAROOT/tmp \ + --port 15001 \ + --grpc_port 15991 \ + --mysql_server_port 15306 \ + --cell test \ + --cells_to_watch test \ + --tablet_types_to_wait PRIMARY,REPLICA \ + --service_map 'grpc-vtgateservice' \ + --pid_file $VTDATAROOT/tmp/vtgate.pid \ + --mysql_auth_server_impl none + Flags: --allow-kill-statement Allows the execution of kill statement --allowed_tablet_types strings Specifies the tablet types this vtgate is allowed to route queries to. Should be provided as a comma-separated set of tablet types. diff --git a/go/flags/endtoend/vttablet.txt b/go/flags/endtoend/vttablet.txt index 12dafdc7427..c4cf2116e50 100644 --- a/go/flags/endtoend/vttablet.txt +++ b/go/flags/endtoend/vttablet.txt @@ -8,6 +8,22 @@ In addition to these deployment types, a partially managed VTTablet is also poss Usage: vttablet [flags] +Examples: + +vttablet \ + --topo_implementation etcd2 \ + --topo_global_server_address localhost:2379 \ + --topo_global_root /vitess/ \ + --tablet-path $alias \ + --init_keyspace $keyspace \ + --init_shard $shard \ + --init_tablet_type $tablet_type \ + --port $port \ + --grpc_port $grpc_port \ + --service_map 'grpc-queryservice,grpc-tabletmanager,grpc-updatestream' + +`$alias` needs to be of the form: `-id`, and the cell should match one of the local cells that was created in the topology. The id can be left padded with zeroes: `cell-100` and `cell-000000100` are synonymous. + Flags: --alsologtostderr log to standard error as well as files --app_idle_timeout duration Idle timeout for app connections (default 1m0s) From 78ba047e064c32ba8790601e6f735bf6de4baa73 Mon Sep 17 00:00:00 2001 From: Andrew Mason Date: Fri, 8 Sep 2023 18:37:57 -0400 Subject: [PATCH 5/9] ensure the global commandline is updated Signed-off-by: Andrew Mason --- go/vt/servenv/servenv.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/go/vt/servenv/servenv.go b/go/vt/servenv/servenv.go index 06942d4f4b9..aa6c6265913 100644 --- a/go/vt/servenv/servenv.go +++ b/go/vt/servenv/servenv.go @@ -378,6 +378,8 @@ func MoveFlagsToCobraCommand(cmd *cobra.Command) { cmd.Flags().AddGoFlag(flag.Lookup("stderrthreshold")) cmd.Flags().AddGoFlag(flag.Lookup("log_dir")) cmd.Flags().AddGoFlag(flag.Lookup("vmodule")) + + pflag.CommandLine = cmd.Flags() } // CobraPreRunE returns the common function that commands will need to load From 3b422215f79493e37dfc62991bc8d4dd14789054 Mon Sep 17 00:00:00 2001 From: Andrew Mason Date: Wed, 13 Sep 2023 10:04:19 -0400 Subject: [PATCH 6/9] update longhelp for vttablet Signed-off-by: Andrew Mason --- go/cmd/vttablet/cli/cli.go | 22 ++++++++++++++++++++-- go/flags/endtoend/vttablet.txt | 17 +++++++++++++++++ 2 files changed, 37 insertions(+), 2 deletions(-) diff --git a/go/cmd/vttablet/cli/cli.go b/go/cmd/vttablet/cli/cli.go index c51cd4de33c..e967aafc164 100644 --- a/go/cmd/vttablet/cli/cli.go +++ b/go/cmd/vttablet/cli/cli.go @@ -59,12 +59,30 @@ var ( Main = &cobra.Command{ Use: "vttablet", Short: "The VTTablet server controls a running MySQL server.", - Long: fmt.Sprintf(`The VTTablet server _controls_ a running MySQL server. VTTablet supports two primary types of deployments: + Long: `The VTTablet server _controls_ a running MySQL server. VTTablet supports two primary types of deployments: * Managed MySQL (most common) * External MySQL -In addition to these deployment types, a partially managed VTTablet is also possible by setting %s.`, "`--disable_active_reparents`"), +In addition to these deployment types, a partially managed VTTablet is also possible by setting ` + "`--disable_active_reparents`." + ` + +### Managed MySQL + +In this mode, Vitess actively manages MySQL. + +### External MySQL. + +In this mode, an external MySQL can be used such as AWS RDS, AWS Aurora, Google CloudSQL; or just an existing (vanilla) MySQL installation. + +See "Unmanaged Tablet" for the full guide. + +Even if a MySQL is external, you can still make vttablet perform some management functions. They are as follows: + +` + + "* `--disable_active_reparents`: If this flag is set, then any reparent or replica commands will not be allowed. These are InitShardPrimary, PlannedReparentShard, EmergencyReparentShard, and ReparentTablet. In this mode, you should use the TabletExternallyReparented command to inform vitess of the current primary.\n" + + "* `--replication_connect_retry`: This value is give to mysql when it connects a replica to the primary as the retry duration parameter.\n" + + "* `--enable_replication_reporter`: If this flag is set, then vttablet will transmit replica lag related information to the vtgates, which will allow it to balance load better. Additionally, enabling this will also cause vttablet to restart replication if it was stopped. However, it will do this only if `--disable_active_reparents` was not turned on.\n" + + "* `--heartbeat_enable` and `--heartbeat_interval duration`: cause vttablet to write heartbeats to the sidecar database. This information is also used by the replication reporter to assess replica lag.\n", Example: ` vttablet \ --topo_implementation etcd2 \ diff --git a/go/flags/endtoend/vttablet.txt b/go/flags/endtoend/vttablet.txt index c4cf2116e50..afd1492d182 100644 --- a/go/flags/endtoend/vttablet.txt +++ b/go/flags/endtoend/vttablet.txt @@ -5,6 +5,23 @@ The VTTablet server _controls_ a running MySQL server. VTTablet supports two pri In addition to these deployment types, a partially managed VTTablet is also possible by setting `--disable_active_reparents`. +### Managed MySQL + +In this mode, Vitess actively manages MySQL. + +### External MySQL. + +In this mode, an external MySQL can be used such as AWS RDS, AWS Aurora, Google CloudSQL; or just an existing (vanilla) MySQL installation. + +See "Unmanaged Tablet" for the full guide. + +Even if a MySQL is external, you can still make vttablet perform some management functions. They are as follows: + +* `--disable_active_reparents`: If this flag is set, then any reparent or replica commands will not be allowed. These are InitShardPrimary, PlannedReparentShard, EmergencyReparentShard, and ReparentTablet. In this mode, you should use the TabletExternallyReparented command to inform vitess of the current primary. +* `--replication_connect_retry`: This value is give to mysql when it connects a replica to the primary as the retry duration parameter. +* `--enable_replication_reporter`: If this flag is set, then vttablet will transmit replica lag related information to the vtgates, which will allow it to balance load better. Additionally, enabling this will also cause vttablet to restart replication if it was stopped. However, it will do this only if `--disable_active_reparents` was not turned on. +* `--heartbeat_enable` and `--heartbeat_interval duration`: cause vttablet to write heartbeats to the sidecar database. This information is also used by the replication reporter to assess replica lag. + Usage: vttablet [flags] From c7da66d644ff367dfdc2ab496633f4c819fccf17 Mon Sep 17 00:00:00 2001 From: Andrew Mason Date: Wed, 13 Sep 2023 10:06:53 -0400 Subject: [PATCH 7/9] update longhelp for vtgate Signed-off-by: Andrew Mason --- go/cmd/vtgate/cli/cli.go | 9 +++++++-- go/flags/endtoend/vtgate.txt | 8 +++++++- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/go/cmd/vtgate/cli/cli.go b/go/cmd/vtgate/cli/cli.go index 62b51945da2..954fad14212 100644 --- a/go/cmd/vtgate/cli/cli.go +++ b/go/cmd/vtgate/cli/cli.go @@ -47,8 +47,13 @@ var ( Main = &cobra.Command{ Use: "vtgate", Short: "VTGate is a stateless proxy responsible for accepting requests from applications and routing them to the appropriate tablet server(s) for query execution. It speaks both the MySQL Protocol and a gRPC protocol.", - Example: ` -vtgate \ + Long: `VTGate is a stateless proxy responsible for accepting requests from applications and routing them to the appropriate tablet server(s) for query execution. It speaks both the MySQL Protocol and a gRPC protocol. + +### Key Options +` + + "`--srv_topo_cache_ttl`: There may be instances where you will need to increase the cached TTL from the default of 1 second to a higher number:\n" + + `* You may want to increase this option if you see that your topo leader goes down and keeps your queries waiting for a few seconds.`, + Example: `vtgate \ --topo_implementation etcd2 \ --topo_global_server_address localhost:2379 \ --topo_global_root /vitess/global \ diff --git a/go/flags/endtoend/vtgate.txt b/go/flags/endtoend/vtgate.txt index dad6548a506..ade86f94592 100644 --- a/go/flags/endtoend/vtgate.txt +++ b/go/flags/endtoend/vtgate.txt @@ -1,4 +1,10 @@ -VTGate is a stateless proxy responsible for accepting requests from applications and routing them to the appropriate tablet server(s) for query execution. It speaks both the MySQL Protocol and a gRPC protocol. +VTGate is a stateless proxy responsible for accepting requests from applications and routing them to the appropriate tablet server(s) for query execution. +It speaks both the MySQL Protocol and a gRPC protocol. + +### Key Options + +* `--srv_topo_cache_ttl`: There may be instances where you will need to increase the cached TTL from the default of 1 second to a higher number: + * You may want to increase this option if you see that your topo leader goes down and keeps your queries waiting for a few seconds. Usage: vtgate [flags] From 4d5200360b26f9af76fcf872d540a3b41692882e Mon Sep 17 00:00:00 2001 From: Andrew Mason Date: Wed, 13 Sep 2023 12:30:13 -0400 Subject: [PATCH 8/9] lmfao bugfix Signed-off-by: Andrew Mason --- go/cmd/vtgate/docgen/main.go | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/go/cmd/vtgate/docgen/main.go b/go/cmd/vtgate/docgen/main.go index cd1add2ca39..763d38b7e7b 100644 --- a/go/cmd/vtgate/docgen/main.go +++ b/go/cmd/vtgate/docgen/main.go @@ -18,6 +18,7 @@ package main import ( "github.com/spf13/cobra" + "github.com/spf13/pflag" "vitess.io/vitess/go/cmd/internal/docgen" "vitess.io/vitess/go/cmd/vtgate/cli" @@ -32,6 +33,10 @@ func main() { }, } + // Here because we inadvertently transfer the required "tablet-types-to-wait" + // flag during vtgate/cli's init func. + pflag.CommandLine = cmd.Flags() + cmd.Flags().StringVarP(&dir, "dir", "d", "doc", "output directory to write documentation") _ = cmd.Execute() } From 3aa60d9f80f975a31edae0e4cfff1dd75ee940c8 Mon Sep 17 00:00:00 2001 From: Andrew Mason Date: Thu, 14 Sep 2023 07:43:27 -0400 Subject: [PATCH 9/9] remember to build before generating flag data dumbdumb Signed-off-by: Andrew Mason --- go/cmd/vtgate/cli/cli.go | 4 ++-- go/flags/endtoend/vtgate.txt | 4 +--- 2 files changed, 3 insertions(+), 5 deletions(-) diff --git a/go/cmd/vtgate/cli/cli.go b/go/cmd/vtgate/cli/cli.go index 954fad14212..0ba24162f41 100644 --- a/go/cmd/vtgate/cli/cli.go +++ b/go/cmd/vtgate/cli/cli.go @@ -51,8 +51,8 @@ var ( ### Key Options ` + - "`--srv_topo_cache_ttl`: There may be instances where you will need to increase the cached TTL from the default of 1 second to a higher number:\n" + - `* You may want to increase this option if you see that your topo leader goes down and keeps your queries waiting for a few seconds.`, + "\n* `--srv_topo_cache_ttl`: There may be instances where you will need to increase the cached TTL from the default of 1 second to a higher number:\n" + + ` * You may want to increase this option if you see that your topo leader goes down and keeps your queries waiting for a few seconds.`, Example: `vtgate \ --topo_implementation etcd2 \ --topo_global_server_address localhost:2379 \ diff --git a/go/flags/endtoend/vtgate.txt b/go/flags/endtoend/vtgate.txt index ade86f94592..a9ca01edaee 100644 --- a/go/flags/endtoend/vtgate.txt +++ b/go/flags/endtoend/vtgate.txt @@ -1,5 +1,4 @@ -VTGate is a stateless proxy responsible for accepting requests from applications and routing them to the appropriate tablet server(s) for query execution. -It speaks both the MySQL Protocol and a gRPC protocol. +VTGate is a stateless proxy responsible for accepting requests from applications and routing them to the appropriate tablet server(s) for query execution. It speaks both the MySQL Protocol and a gRPC protocol. ### Key Options @@ -10,7 +9,6 @@ Usage: vtgate [flags] Examples: - vtgate \ --topo_implementation etcd2 \ --topo_global_server_address localhost:2379 \