Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: auto config for vttablet #589

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion go/cmd/vtgate/vtgate.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"math/rand"
"strings"
"time"
"vitess.io/vitess/go/internal/global"

"vitess.io/vitess/go/viperutil"

Expand All @@ -42,7 +43,7 @@ import (
)

var (
cell = ""
cell = global.DefaultCell
tabletTypesToWait []topodatapb.TabletType
plannerName string
vtGateViperConfig = viperutil.NewViperConfig()
Expand Down
47 changes: 25 additions & 22 deletions go/cmd/vttablet/vttablet.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ package main

import (
"context"
"os"
"fmt"
"time"

"vitess.io/vitess/go/internal/global"
Expand Down Expand Up @@ -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()
Expand All @@ -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)
}

Expand Down Expand Up @@ -103,24 +102,37 @@ func main() {
if tableACLMode != global.TableACLModeSimple && tableACLMode != global.TableACLModeMysqlBased {
log.Exit("require table-acl-config-mode")
}
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)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

when *topodatapb.TabletAlias is nil , it may casue NullPointerException in 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)
}
tabletAlias, err = topoproto.ParseTabletAlias(fmt.Sprintf("%s-%010d", cell, server_id))
if err != nil {
log.Exitf("failed to parse --tablet-path: %v", err)
}
}

// 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.
Expand Down Expand Up @@ -169,17 +181,8 @@ 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
Expand Down
2 changes: 2 additions & 0 deletions go/internal/global/global.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import (
"time"
)

const DefaultCell = "zone1"

// Keyspace
const (
DefaultKeyspace = "mysql"
Expand Down
Loading