Skip to content
This repository has been archived by the owner on Sep 21, 2022. It is now read-only.

Commit

Permalink
Merge pull request vitessio#4094 from sougou/nomycnf
Browse files Browse the repository at this point in the history
nomycnf: operate without mycnf more naturally
  • Loading branch information
sougou authored Jul 21, 2018
2 parents cf6afb6 + ac11628 commit 2e94e3e
Show file tree
Hide file tree
Showing 19 changed files with 246 additions and 224 deletions.
24 changes: 12 additions & 12 deletions go/cmd/mysqlctl/mysqlctl.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,12 @@ func initConfigCmd(subFlags *flag.FlagSet, args []string) error {
subFlags.Parse(args)

// Generate my.cnf from scratch and use it to find mysqld.
mysqld, err := mysqlctl.CreateMysqld(uint32(*tabletUID), *mysqlSocket, int32(*mysqlPort))
mysqld, cnf, err := mysqlctl.CreateMysqldAndMycnf(uint32(*tabletUID), *mysqlSocket, int32(*mysqlPort))
if err != nil {
return fmt.Errorf("failed to initialize mysql config: %v", err)
}
defer mysqld.Close()
if err := mysqld.InitConfig(); err != nil {
if err := mysqld.InitConfig(cnf); err != nil {
return fmt.Errorf("failed to init mysql config: %v", err)
}
return nil
Expand All @@ -65,29 +65,29 @@ func initCmd(subFlags *flag.FlagSet, args []string) error {
subFlags.Parse(args)

// Generate my.cnf from scratch and use it to find mysqld.
mysqld, err := mysqlctl.CreateMysqld(uint32(*tabletUID), *mysqlSocket, int32(*mysqlPort))
mysqld, cnf, err := mysqlctl.CreateMysqldAndMycnf(uint32(*tabletUID), *mysqlSocket, int32(*mysqlPort))
if err != nil {
return fmt.Errorf("failed to initialize mysql config: %v", err)
}
defer mysqld.Close()

ctx, cancel := context.WithTimeout(context.Background(), *waitTime)
defer cancel()
if err := mysqld.Init(ctx, *initDBSQLFile); err != nil {
if err := mysqld.Init(ctx, cnf, *initDBSQLFile); err != nil {
return fmt.Errorf("failed init mysql: %v", err)
}
return nil
}

func reinitConfigCmd(subFlags *flag.FlagSet, args []string) error {
// There ought to be an existing my.cnf, so use it to find mysqld.
mysqld, err := mysqlctl.OpenMysqld(uint32(*tabletUID))
mysqld, cnf, err := mysqlctl.OpenMysqldAndMycnf(uint32(*tabletUID))
if err != nil {
return fmt.Errorf("failed to find mysql config: %v", err)
}
defer mysqld.Close()

if err := mysqld.ReinitConfig(context.TODO()); err != nil {
if err := mysqld.ReinitConfig(context.TODO(), cnf); err != nil {
return fmt.Errorf("failed to reinit mysql config: %v", err)
}
return nil
Expand All @@ -98,15 +98,15 @@ func shutdownCmd(subFlags *flag.FlagSet, args []string) error {
subFlags.Parse(args)

// There ought to be an existing my.cnf, so use it to find mysqld.
mysqld, err := mysqlctl.OpenMysqld(uint32(*tabletUID))
mysqld, cnf, err := mysqlctl.OpenMysqldAndMycnf(uint32(*tabletUID))
if err != nil {
return fmt.Errorf("failed to find mysql config: %v", err)
}
defer mysqld.Close()

ctx, cancel := context.WithTimeout(context.Background(), *waitTime)
defer cancel()
if err := mysqld.Shutdown(ctx, true); err != nil {
if err := mysqld.Shutdown(ctx, cnf, true); err != nil {
return fmt.Errorf("failed shutdown mysql: %v", err)
}
return nil
Expand All @@ -119,15 +119,15 @@ func startCmd(subFlags *flag.FlagSet, args []string) error {
subFlags.Parse(args)

// There ought to be an existing my.cnf, so use it to find mysqld.
mysqld, err := mysqlctl.OpenMysqld(uint32(*tabletUID))
mysqld, cnf, err := mysqlctl.OpenMysqldAndMycnf(uint32(*tabletUID))
if err != nil {
return fmt.Errorf("failed to find mysql config: %v", err)
}
defer mysqld.Close()

ctx, cancel := context.WithTimeout(context.Background(), *waitTime)
defer cancel()
if err := mysqld.Start(ctx, mysqldArgs...); err != nil {
if err := mysqld.Start(ctx, cnf, mysqldArgs...); err != nil {
return fmt.Errorf("failed start mysql: %v", err)
}
return nil
Expand All @@ -139,15 +139,15 @@ func teardownCmd(subFlags *flag.FlagSet, args []string) error {
subFlags.Parse(args)

// There ought to be an existing my.cnf, so use it to find mysqld.
mysqld, err := mysqlctl.OpenMysqld(uint32(*tabletUID))
mysqld, cnf, err := mysqlctl.OpenMysqldAndMycnf(uint32(*tabletUID))
if err != nil {
return fmt.Errorf("failed to find mysql config: %v", err)
}
defer mysqld.Close()

ctx, cancel := context.WithTimeout(context.Background(), *waitTime)
defer cancel()
if err := mysqld.Teardown(ctx, *force); err != nil {
if err := mysqld.Teardown(ctx, cnf, *force); err != nil {
return fmt.Errorf("failed teardown mysql (forced? %v): %v", *force, err)
}
return nil
Expand Down
13 changes: 7 additions & 6 deletions go/cmd/mysqlctld/mysqlctld.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import (
var (
// mysqld is used by the rpc implementation plugin.
mysqld *mysqlctl.Mysqld
cnf *mysqlctl.Mycnf

mysqlPort = flag.Int("mysql_port", 3306, "mysql port")
tabletUID = flag.Uint("tablet_uid", 41983, "tablet uid")
Expand Down Expand Up @@ -74,14 +75,14 @@ func main() {
log.Infof("mycnf file (%s) doesn't exist, initializing", mycnfFile)

var err error
mysqld, err = mysqlctl.CreateMysqld(uint32(*tabletUID), *mysqlSocket, int32(*mysqlPort))
mysqld, cnf, err = mysqlctl.CreateMysqldAndMycnf(uint32(*tabletUID), *mysqlSocket, int32(*mysqlPort))
if err != nil {
log.Errorf("failed to initialize mysql config: %v", err)
exit.Return(1)
}
mysqld.OnTerm(onTermFunc)

if err := mysqld.Init(ctx, *initDBSQLFile); err != nil {
if err := mysqld.Init(ctx, cnf, *initDBSQLFile); err != nil {
log.Errorf("failed to initialize mysql data dir and start mysqld: %v", err)
exit.Return(1)
}
Expand All @@ -90,20 +91,20 @@ func main() {
log.Infof("mycnf file (%s) already exists, starting without init", mycnfFile)

var err error
mysqld, err = mysqlctl.OpenMysqld(uint32(*tabletUID))
mysqld, cnf, err = mysqlctl.OpenMysqldAndMycnf(uint32(*tabletUID))
if err != nil {
log.Errorf("failed to find mysql config: %v", err)
exit.Return(1)
}
mysqld.OnTerm(onTermFunc)

err = mysqld.RefreshConfig(ctx)
err = mysqld.RefreshConfig(ctx, cnf)
if err != nil {
log.Errorf("failed to refresh config: %v", err)
exit.Return(1)
}

if err := mysqld.Start(ctx); err != nil {
if err := mysqld.Start(ctx, cnf); err != nil {
log.Errorf("failed to start mysqld: %v", err)
exit.Return(1)
}
Expand All @@ -117,7 +118,7 @@ func main() {
servenv.OnTermSync(func() {
log.Infof("mysqlctl received SIGTERM, shutting down mysqld first")
ctx := context.Background()
mysqld.Shutdown(ctx, true)
mysqld.Shutdown(ctx, cnf, true)
})

// Start RPC server and wait for SIGTERM.
Expand Down
2 changes: 1 addition & 1 deletion go/cmd/mysqlctld/plugin_grpcmysqlctlserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func init() {
servenv.InitServiceMap("grpc", "mysqlctl")
servenv.OnRun(func() {
if servenv.GRPCCheckServiceMap("mysqlctl") {
grpcmysqlctlserver.StartServer(servenv.GRPCServer, mysqld)
grpcmysqlctlserver.StartServer(servenv.GRPCServer, cnf, mysqld)
}
})
}
15 changes: 5 additions & 10 deletions go/cmd/vtcombo/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,21 +94,16 @@ func main() {
servenv.Init()
tabletenv.Init()

// database configs
mycnf, err := mysqlctl.NewMycnfFromFlags(0)
if err != nil {
log.Errorf("mycnf read failed: %v", err)
exit.Return(1)
}
dbcfgs, err := dbconfigs.Init(mycnf.SocketFile)
dbcfgs, err := dbconfigs.Init("")
if err != nil {
log.Warning(err)
}
mysqld := mysqlctl.NewMysqld(mycnf, dbcfgs)
mysqld := mysqlctl.NewMysqld(dbcfgs)
servenv.OnClose(mysqld.Close)

// tablets configuration and init
if err := vtcombo.InitTabletMap(ts, tpb, mysqld, dbcfgs, *schemaDir, mycnf); err != nil {
// tablets configuration and init.
// Send mycnf as nil because vtcombo won't do backups and restores.
if err := vtcombo.InitTabletMap(ts, tpb, mysqld, dbcfgs, *schemaDir, nil); err != nil {
log.Errorf("initTabletMapProto failed: %v", err)
exit.Return(1)
}
Expand Down
27 changes: 22 additions & 5 deletions go/cmd/vttablet/vttablet.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,30 @@ func main() {
if err != nil {
log.Exitf("failed to parse -tablet-path: %v", err)
}
if tabletAlias.Uid < 0 {
log.Exitf("invalid tablet id: %d", tabletAlias.Uid)
}

mycnf, err := mysqlctl.NewMycnfFromFlags(tabletAlias.Uid)
if err != nil {
log.Exitf("mycnf read failed: %v", err)
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 !dbconfigs.HasConnectionParams() {
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.")
}

dbcfgs, err := dbconfigs.Init(mycnf.SocketFile)
// If connection parameters were specified, socketFile will be empty.
// Otherwise, the socketFile (read from mycnf) will be used to initialize
// dbconfigs.
dbcfgs, err := dbconfigs.Init(socketFile)
if err != nil {
log.Warning(err)
}
Expand Down Expand Up @@ -115,7 +132,7 @@ func main() {
// Create mysqld and register the health reporter (needs to be done
// before initializing the agent, so the initial health check
// done by the agent has the right reporter)
mysqld := mysqlctl.NewMysqld(mycnf, dbcfgs)
mysqld := mysqlctl.NewMysqld(dbcfgs)
servenv.OnClose(mysqld.Close)

// Depends on both query and updateStream.
Expand Down
26 changes: 16 additions & 10 deletions go/vt/dbconfigs/dbconfigs.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,14 @@ func (dbcfgs *DBConfigs) Copy() *DBConfigs {
return result
}

// HasConnectionParams returns true if connection parameters were
// specified in the command-line. This will allow the caller to
// search for alternate ways to connect, like looking in the my.cnf
// file.
func HasConnectionParams() bool {
return baseConfig.Host != "" || baseConfig.UnixSocket != ""
}

// Init will initialize all the necessary connection parameters.
// Precedence is as follows: if baseConfig command line options are
// set, they supersede all other settings.
Expand All @@ -222,17 +230,8 @@ func (dbcfgs *DBConfigs) Copy() *DBConfigs {
// If no per-user parameters are supplied, then the defaultSocketFile
// is used to initialize the per-user conn params.
func Init(defaultSocketFile string) (*DBConfigs, error) {
// This is to support legacy behavior: use supplied socket value
// if conn parameters are not specified.
// TODO(sougou): deprecate.
for _, uc := range dbConfigs.userConfigs {
if uc.param.UnixSocket == "" && uc.param.Host == "" {
uc.param.UnixSocket = defaultSocketFile
}
}

// The new base configs, if set, supersede legacy settings.
if baseConfig.Host != "" || baseConfig.UnixSocket != "" {
if HasConnectionParams() {
for _, uc := range dbConfigs.userConfigs {
uc.param.Host = baseConfig.Host
uc.param.Port = baseConfig.Port
Expand All @@ -246,6 +245,13 @@ func Init(defaultSocketFile string) (*DBConfigs, error) {
uc.param.SslKey = baseConfig.SslKey
}
}
} else {
// Use supplied socket value if conn parameters are not specified.
for _, uc := range dbConfigs.userConfigs {
if uc.param.UnixSocket == "" && uc.param.Host == "" {
uc.param.UnixSocket = defaultSocketFile
}
}
}

// See if the CredentialsServer is working. We do not use the
Expand Down
Loading

0 comments on commit 2e94e3e

Please sign in to comment.