From e2ec28b8bfc0d97fc67c29a775d6875d3aa8ae27 Mon Sep 17 00:00:00 2001 From: earayu Date: Thu, 21 Nov 2024 22:43:22 +0800 Subject: [PATCH 1/5] feat: auto config for vttablet --- go/cmd/vtgate/vtgate.go | 3 ++- go/cmd/vttablet/vttablet.go | 46 ++++++++++++------------------------ go/internal/global/global.go | 2 ++ 3 files changed, 19 insertions(+), 32 deletions(-) diff --git a/go/cmd/vtgate/vtgate.go b/go/cmd/vtgate/vtgate.go index 35f11536b3..0c82d1a6cd 100644 --- a/go/cmd/vtgate/vtgate.go +++ b/go/cmd/vtgate/vtgate.go @@ -21,6 +21,7 @@ import ( "math/rand" "strings" "time" + "vitess.io/vitess/go/internal/global" "vitess.io/vitess/go/viperutil" @@ -42,7 +43,7 @@ import ( ) var ( - cell = "" + cell = global.DefaultCell tabletTypesToWait []topodatapb.TabletType plannerName string vtGateViperConfig = viperutil.NewViperConfig() diff --git a/go/cmd/vttablet/vttablet.go b/go/cmd/vttablet/vttablet.go index 85ce11673b..b12ef07775 100644 --- a/go/cmd/vttablet/vttablet.go +++ b/go/cmd/vttablet/vttablet.go @@ -24,7 +24,7 @@ package main import ( "context" - "os" + "fmt" "time" "vitess.io/vitess/go/internal/global" @@ -53,12 +53,12 @@ import ( ) var ( + cell = global.DefaultCell enforceTableACLConfig bool tableACLConfig string tableACLMode string tableACLConfigReloadInterval time.Duration tabletPath string - tabletConfig string tm *tabletmanager.TabletManager vtTabletViperConfig = viperutil.NewViperConfig() @@ -70,7 +70,6 @@ func registerFlags(fs *pflag.FlagSet) { fs.StringVar(&tableACLMode, "table-acl-config-mode", global.TableACLModeSimple, "table acl config mode (simple or mysqlbased)") fs.DurationVar(&tableACLConfigReloadInterval, "table-acl-config-reload-interval", global.DefaultACLReloadInterval, "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) } @@ -99,10 +98,17 @@ func main() { servenv.ParseFlags("vttablet") servenv.Init() vtTabletViperConfig.LoadAndWatchConfigFile() - if tableACLMode != global.TableACLModeSimple && tableACLMode != global.TableACLModeMysqlBased { log.Exit("require table-acl-config-mode") } + + config := initConfig() + mysqld := mysqlctl.NewMysqld(config.DB) + server_id, err := mysqld.GetServerID(context.Background()) + if err != nil { + log.Exitf("failed to get server id: %v", err) + } + tabletPath = fmt.Sprintf("%s-%010d", cell, server_id) if tabletPath == "" { log.Exit("--tablet-path required") } @@ -113,14 +119,10 @@ func main() { // validate query server pool auto scale config tabletserver.ValidateQueryServerPoolAutoScaleConfig(true) - // config and mycnf initializations are intertwined. - config, mycnf := initConfig(tabletAlias) - ts := topo.Open() qsc := createTabletServer(config, ts, tabletAlias) viperutil.RegisterReloadHandlersForVtTablet(vtTabletViperConfig, qsc) - mysqld := mysqlctl.NewMysqld(config.DB) servenv.OnClose(mysqld.Close) // Initialize and start tm. @@ -135,7 +137,7 @@ func main() { tm = &tabletmanager.TabletManager{ BatchCtx: context.Background(), TopoServer: ts, - Cnf: mycnf, + Cnf: nil, // we don't host mysql-server like vitess, so we don't need mycnf MysqlDaemon: mysqld, DBConfigs: config.DB.Clone(), QueryServiceControl: qsc, @@ -161,7 +163,7 @@ func main() { servenv.RunDefault() } -func initConfig(tabletAlias *topodatapb.TabletAlias) (*tabletenv.TabletConfig, *mysqlctl.Mycnf) { +func initConfig() *tabletenv.TabletConfig { tabletenv.Init() // Load current config after tabletenv.Init, because it changes it. config := tabletenv.NewCurrentConfig() @@ -169,30 +171,12 @@ func initConfig(tabletAlias *topodatapb.TabletAlias) (*tabletenv.TabletConfig, * 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) + log.Infof("Loaded config file successfully:\n%s", 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 + log.Exitf("no db_host or db_socket file specified") } else { log.Info("connection parameters were specified. Not loading my.cnf.") } @@ -204,7 +188,7 @@ func initConfig(tabletAlias *topodatapb.TabletAlias) (*tabletenv.TabletConfig, * for _, cfg := range config.ExternalConnections { cfg.InitWithSocket("") } - return config, mycnf + return config } func createTabletServer(config *tabletenv.TabletConfig, ts *topo.Server, tabletAlias *topodatapb.TabletAlias) *tabletserver.TabletServer { diff --git a/go/internal/global/global.go b/go/internal/global/global.go index a939db4eed..2c91e77417 100644 --- a/go/internal/global/global.go +++ b/go/internal/global/global.go @@ -9,6 +9,8 @@ import ( "time" ) +const DefaultCell = "zone1" + // Keyspace const ( DefaultKeyspace = "mysql" From 5aa606f9bd6e7156921efe7cff7874cafac7a1d5 Mon Sep 17 00:00:00 2001 From: earayu Date: Mon, 25 Nov 2024 01:00:27 +0800 Subject: [PATCH 2/5] feat: auto config for vttablet --- go/cmd/vttablet/vttablet.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/go/cmd/vttablet/vttablet.go b/go/cmd/vttablet/vttablet.go index b12ef07775..53c3e56620 100644 --- a/go/cmd/vttablet/vttablet.go +++ b/go/cmd/vttablet/vttablet.go @@ -106,7 +106,7 @@ func main() { mysqld := mysqlctl.NewMysqld(config.DB) server_id, err := mysqld.GetServerID(context.Background()) if err != nil { - log.Exitf("failed to get server id: %v", err) + log.Warningf("failed to get server id: %v", err) } tabletPath = fmt.Sprintf("%s-%010d", cell, server_id) if tabletPath == "" { From 53441373fb9a84d5f66883d2e311662f5081b02f Mon Sep 17 00:00:00 2001 From: earayu Date: Mon, 25 Nov 2024 01:06:46 +0800 Subject: [PATCH 3/5] feat: auto config for vttablet --- go/cmd/vttablet/vttablet.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/go/cmd/vttablet/vttablet.go b/go/cmd/vttablet/vttablet.go index 53c3e56620..e8513a1c40 100644 --- a/go/cmd/vttablet/vttablet.go +++ b/go/cmd/vttablet/vttablet.go @@ -106,7 +106,7 @@ func main() { mysqld := mysqlctl.NewMysqld(config.DB) server_id, err := mysqld.GetServerID(context.Background()) if err != nil { - log.Warningf("failed to get server id: %v", err) + log.Exitf("failed to get server id: %v", err) } tabletPath = fmt.Sprintf("%s-%010d", cell, server_id) if tabletPath == "" { @@ -176,7 +176,7 @@ func initConfig() *tabletenv.TabletConfig { var socketFile string if !config.DB.HasGlobalSettings() { - log.Exitf("no db_host or db_socket file specified") + log.Error("no db_host or db_socket file specified") } else { log.Info("connection parameters were specified. Not loading my.cnf.") } From b8da307ed2f6abf8bee5cf9ad7f3a9c25c2e204b Mon Sep 17 00:00:00 2001 From: earayu Date: Wed, 18 Dec 2024 19:41:11 +0800 Subject: [PATCH 4/5] feat: init tabletAlias --- go/cmd/vttablet/vttablet.go | 52 +++++++++++++++++++++++++------------ 1 file changed, 36 insertions(+), 16 deletions(-) diff --git a/go/cmd/vttablet/vttablet.go b/go/cmd/vttablet/vttablet.go index e8513a1c40..e9eac4c373 100644 --- a/go/cmd/vttablet/vttablet.go +++ b/go/cmd/vttablet/vttablet.go @@ -98,24 +98,35 @@ func main() { servenv.ParseFlags("vttablet") servenv.Init() vtTabletViperConfig.LoadAndWatchConfigFile() + if tableACLMode != global.TableACLModeSimple && tableACLMode != global.TableACLModeMysqlBased { log.Exit("require table-acl-config-mode") } - config := initConfig() - mysqld := mysqlctl.NewMysqld(config.DB) - server_id, err := mysqld.GetServerID(context.Background()) - if err != nil { - log.Exitf("failed to get server id: %v", err) - } - tabletPath = fmt.Sprintf("%s-%010d", cell, server_id) - if tabletPath == "" { - log.Exit("--tablet-path required") + var tabletAlias *topodatapb.TabletAlias = nil + if tabletPath != "" { + parsedTabletAlias, err := topoproto.ParseTabletAlias(tabletPath) + if err != nil { + log.Exitf("failed to parse --tablet-path: %v", err) + } + tabletAlias = parsedTabletAlias } - tabletAlias, err := topoproto.ParseTabletAlias(tabletPath) - if err != nil { - log.Exitf("failed to parse --tablet-path: %v", err) + + config, mycnf := initConfig(tabletAlias) + mysqld := mysqlctl.NewMysqld(config.DB) + + if tabletAlias == nil { + server_id, err := mysqld.GetServerID(context.Background()) + if err != nil { + log.Exitf("failed to get server id: %v", err) + } + tabletPath = fmt.Sprintf("%s-%010d", cell, server_id) + tabletAlias, err = topoproto.ParseTabletAlias(tabletPath) + if err != nil { + log.Exitf("failed to parse --tablet-path: %v", err) + } } + // validate query server pool auto scale config tabletserver.ValidateQueryServerPoolAutoScaleConfig(true) @@ -137,7 +148,7 @@ func main() { tm = &tabletmanager.TabletManager{ BatchCtx: context.Background(), TopoServer: ts, - Cnf: nil, // we don't host mysql-server like vitess, so we don't need mycnf + Cnf: mycnf, MysqlDaemon: mysqld, DBConfigs: config.DB.Clone(), QueryServiceControl: qsc, @@ -163,7 +174,7 @@ func main() { servenv.RunDefault() } -func initConfig() *tabletenv.TabletConfig { +func initConfig(tabletAlias *topodatapb.TabletAlias) (*tabletenv.TabletConfig, *mysqlctl.Mycnf) { tabletenv.Init() // Load current config after tabletenv.Init, because it changes it. config := tabletenv.NewCurrentConfig() @@ -174,9 +185,18 @@ func initConfig() *tabletenv.TabletConfig { gotBytes, _ := yaml2.Marshal(config) log.Infof("Loaded config file successfully:\n%s", 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() { - log.Error("no db_host or db_socket file specified") + 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.") } @@ -188,7 +208,7 @@ func initConfig() *tabletenv.TabletConfig { for _, cfg := range config.ExternalConnections { cfg.InitWithSocket("") } - return config + return config, mycnf } func createTabletServer(config *tabletenv.TabletConfig, ts *topo.Server, tabletAlias *topodatapb.TabletAlias) *tabletserver.TabletServer { From a97ca87655f65f8aa3915363fb423ed6c2763103 Mon Sep 17 00:00:00 2001 From: earayu Date: Wed, 18 Dec 2024 19:41:43 +0800 Subject: [PATCH 5/5] feat: init tabletAlias --- go/cmd/vttablet/vttablet.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/go/cmd/vttablet/vttablet.go b/go/cmd/vttablet/vttablet.go index e9eac4c373..a787455b1b 100644 --- a/go/cmd/vttablet/vttablet.go +++ b/go/cmd/vttablet/vttablet.go @@ -120,8 +120,7 @@ func main() { if err != nil { log.Exitf("failed to get server id: %v", err) } - tabletPath = fmt.Sprintf("%s-%010d", cell, server_id) - tabletAlias, err = topoproto.ParseTabletAlias(tabletPath) + tabletAlias, err = topoproto.ParseTabletAlias(fmt.Sprintf("%s-%010d", cell, server_id)) if err != nil { log.Exitf("failed to parse --tablet-path: %v", err) }