Skip to content
Merged
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
1 change: 1 addition & 0 deletions components/playground/instance/instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ type instance struct {
Host string
Port int
StatusPort int // client port for PD
ConfigPath string
}

// Instance represent running component
Expand Down
6 changes: 5 additions & 1 deletion components/playground/instance/pd.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,15 @@ type PDInstance struct {
}

// NewPDInstance return a PDInstance
func NewPDInstance(dir, host string, id int) *PDInstance {
func NewPDInstance(dir, host, configPath string, id int) *PDInstance {
return &PDInstance{
instance: instance{
ID: id,
Dir: dir,
Host: host,
Port: utils.MustGetFreePort(host, 2380),
StatusPort: utils.MustGetFreePort(host, 2379),
ConfigPath: configPath,
},
}
}
Expand Down Expand Up @@ -69,6 +70,9 @@ func (inst *PDInstance) Start(ctx context.Context, version repository.Version, b
fmt.Sprintf("--advertise-client-urls=http://%s:%d", inst.Host, inst.StatusPort),
fmt.Sprintf("--log-file=%s", filepath.Join(inst.Dir, "pd.log")),
}
if inst.ConfigPath != "" {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

We can remove other flags even if we specify a configuration file. We should keep the original flags and append --config if the config flag exists.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Thank you. Fixed.

args = append(args, fmt.Sprintf("--config=%s", inst.ConfigPath))
}
endpoints := make([]string, 0, len(inst.endpoints))
for _, pd := range inst.endpoints {
uid := fmt.Sprintf("pd-%d", pd.ID)
Expand Down
6 changes: 5 additions & 1 deletion components/playground/instance/tidb.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,15 @@ type TiDBInstance struct {
}

// NewTiDBInstance return a TiDBInstance
func NewTiDBInstance(dir, host string, id int, pds []*PDInstance) *TiDBInstance {
func NewTiDBInstance(dir, host, configPath string, id int, pds []*PDInstance) *TiDBInstance {
return &TiDBInstance{
instance: instance{
ID: id,
Dir: dir,
Host: host,
Port: utils.MustGetFreePort(host, 4000),
StatusPort: utils.MustGetFreePort("0.0.0.0", 10080),
ConfigPath: configPath,
},
pds: pds,
}
Expand All @@ -67,6 +68,9 @@ func (inst *TiDBInstance) Start(ctx context.Context, version repository.Version,
fmt.Sprintf("--path=%s", strings.Join(endpoints, ",")),
fmt.Sprintf("--log-file=%s", filepath.Join(inst.Dir, "tidb.log")),
}
if inst.ConfigPath != "" {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

ditto

args = append(args, fmt.Sprintf("--config=%s", inst.ConfigPath))
}
inst.cmd = exec.CommandContext(ctx, args[0], args[1:]...)
inst.cmd.Env = append(
os.Environ(),
Expand Down
6 changes: 5 additions & 1 deletion components/playground/instance/tikv.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,15 @@ type TiKVInstance struct {
}

// NewTiKVInstance return a TiKVInstance
func NewTiKVInstance(dir, host string, id int, pds []*PDInstance) *TiKVInstance {
func NewTiKVInstance(dir, host, configPath string, id int, pds []*PDInstance) *TiKVInstance {
return &TiKVInstance{
instance: instance{
ID: id,
Dir: dir,
Host: host,
Port: utils.MustGetFreePort(host, 20160),
StatusPort: utils.MustGetFreePort(host, 20180),
ConfigPath: configPath,
},
pds: pds,
}
Expand All @@ -55,6 +56,9 @@ func (inst *TiKVInstance) Start(ctx context.Context, version repository.Version,
return err
}
configPath := path.Join(inst.Dir, "tikv.toml")
if inst.ConfigPath != "" {
configPath = inst.ConfigPath
}
cf, err := os.Create(configPath)
if err != nil {
return errors.Trace(err)
Expand Down
96 changes: 71 additions & 25 deletions components/playground/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,21 @@ import (
"go.uber.org/zap"
)

type bootOptions struct {
version string
pdConfigPath string
tidbConfigPath string
tikvConfigPath string
pdBinPath string
tidbBinPath string
tikvBinPath string
pdNum int
tidbNum int
tikvNum int
host string
monitor bool
}

func installIfMissing(profile *localdata.Profile, component, version string) error {
versions, err := meta.Profile().InstalledVersions(component)
if err != nil {
Expand Down Expand Up @@ -73,6 +88,9 @@ func execute() error {
pdNum := 1
host := "127.0.0.1"
monitor := false
tidbConfigPath := ""
tikvConfigPath := ""
pdConfigPath := ""
tidbBinPath := ""
tikvBinPath := ""
pdBinPath := ""
Expand All @@ -86,14 +104,29 @@ Examples:
$ tiup playground nightly # Start a TiDB nightly version local cluster
$ tiup playground v3.0.10 --db 3 --pd 3 --kv 3 # Start a local cluster with 10 nodes
$ tiup playground nightly --monitor # Start a local cluster with monitor system
$ tiup playground --pd.config ~/config/pd.toml # Start a local cluster with specified configuration file,
$ tiup playground --db.binpath /xx/tidb-server # Start a local cluster with component binary path`,
SilenceUsage: true,
RunE: func(cmd *cobra.Command, args []string) error {
version := ""
if len(args) > 0 {
version = args[0]
}
return bootCluster(version, pdNum, tidbNum, tikvNum, host, monitor, tidbBinPath, tikvBinPath, pdBinPath)
options := &bootOptions{
version: version,
pdConfigPath: pdConfigPath,
tidbConfigPath: tidbConfigPath,
tikvConfigPath: tikvConfigPath,
pdBinPath: pdBinPath,
tidbBinPath: tidbBinPath,
tikvBinPath: tikvBinPath,
pdNum: pdNum,
tidbNum: tidbNum,
tikvNum: tikvNum,
host: host,
monitor: monitor,
}
return bootCluster(options)
},
}

Expand All @@ -102,6 +135,9 @@ Examples:
rootCmd.Flags().IntVarP(&pdNum, "pd", "", 1, "PD instance number")
rootCmd.Flags().StringVarP(&host, "host", "", host, "Playground cluster host")
rootCmd.Flags().BoolVar(&monitor, "monitor", false, "Start prometheus component")
rootCmd.Flags().StringVarP(&tidbConfigPath, "db.config", "", tidbConfigPath, "TiDB instance configuration file")
rootCmd.Flags().StringVarP(&tikvConfigPath, "kv.config", "", tikvConfigPath, "TiKV instance configuration file")
rootCmd.Flags().StringVarP(&pdConfigPath, "pd.config", "", pdConfigPath, "PD instance configuration file")
rootCmd.Flags().StringVarP(&tidbBinPath, "db.binpath", "", tidbBinPath, "TiDB instance binary path")
rootCmd.Flags().StringVarP(&tikvBinPath, "kv.binpath", "", tikvBinPath, "TiKV instance binary path")
rootCmd.Flags().StringVarP(&pdBinPath, "pd.binpath", "", pdBinPath, "PD instance binary path")
Expand Down Expand Up @@ -161,21 +197,31 @@ func getAbsolutePath(binPath string) string {
return binPath
}

func bootCluster(version string, pdNum, tidbNum, tikvNum int, host string, monitor bool, tidbBinPath, tikvBinPath, pdBinPath string) error {
if pdNum < 1 || tidbNum < 1 || tikvNum < 1 {
func bootCluster(options *bootOptions) error {
if options.pdNum < 1 || options.tidbNum < 1 || options.tikvNum < 1 {
return fmt.Errorf("all components count must be great than 0 (tidb=%v, tikv=%v, pd=%v)",
tidbNum, tikvNum, pdNum)
options.tidbNum, options.tikvNum, options.pdNum)
}

var pathMap = make(map[string]string)
if tidbBinPath != "" {
pathMap["tidb"] = getAbsolutePath(tidbBinPath)
if options.tidbBinPath != "" {
pathMap["tidb"] = getAbsolutePath(options.tidbBinPath)
}
if options.tikvBinPath != "" {
pathMap["tikv"] = getAbsolutePath(options.tikvBinPath)
}
if options.pdBinPath != "" {
pathMap["pd"] = getAbsolutePath(options.pdBinPath)
}

if options.tidbConfigPath != "" {
options.tidbConfigPath = getAbsolutePath(options.tidbConfigPath)
}
if tikvBinPath != "" {
pathMap["tikv"] = getAbsolutePath(tikvBinPath)
if options.tikvConfigPath != "" {
options.tikvConfigPath = getAbsolutePath(options.tikvConfigPath)
}
if pdBinPath != "" {
pathMap["pd"] = getAbsolutePath(pdBinPath)
if options.pdConfigPath != "" {
options.pdConfigPath = getAbsolutePath(options.pdConfigPath)
}

// Initialize the profile
Expand All @@ -188,7 +234,7 @@ func bootCluster(version string, pdNum, tidbNum, tikvNum int, host string, monit
if pathMap[comp] != "" {
continue
}
if err := installIfMissing(profile, comp, version); err != nil {
if err := installIfMissing(profile, comp, options.version); err != nil {
return err
}
}
Expand All @@ -197,18 +243,18 @@ func bootCluster(version string, pdNum, tidbNum, tikvNum int, host string, monit
return fmt.Errorf("cannot read environment variable %s", localdata.EnvNameInstanceDataDir)
}

all := make([]instance.Instance, 0, pdNum+tikvNum+tidbNum)
allRole := make([]string, 0, pdNum+tikvNum+tidbNum)
pds := make([]*instance.PDInstance, 0, pdNum)
kvs := make([]*instance.TiKVInstance, 0, tikvNum)
dbs := make([]*instance.TiDBInstance, 0, tidbNum)
all := make([]instance.Instance, 0, options.pdNum+options.tikvNum+options.tidbNum)
allRole := make([]string, 0, options.pdNum+options.tikvNum+options.tidbNum)
pds := make([]*instance.PDInstance, 0, options.pdNum)
kvs := make([]*instance.TiKVInstance, 0, options.tikvNum)
dbs := make([]*instance.TiDBInstance, 0, options.tidbNum)

ctx, cancel := context.WithCancel(context.Background())
defer cancel()

for i := 0; i < pdNum; i++ {
for i := 0; i < options.pdNum; i++ {
dir := filepath.Join(dataDir, fmt.Sprintf("pd-%d", i))
inst := instance.NewPDInstance(dir, host, i)
inst := instance.NewPDInstance(dir, options.host, options.pdConfigPath, i)
pds = append(pds, inst)
all = append(all, inst)
allRole = append(allRole, "pd")
Expand All @@ -217,17 +263,17 @@ func bootCluster(version string, pdNum, tidbNum, tikvNum int, host string, monit
pd.Join(pds)
}

for i := 0; i < tikvNum; i++ {
for i := 0; i < options.tikvNum; i++ {
dir := filepath.Join(dataDir, fmt.Sprintf("tikv-%d", i))
inst := instance.NewTiKVInstance(dir, host, i, pds)
inst := instance.NewTiKVInstance(dir, options.host, options.tikvConfigPath, i, pds)
kvs = append(kvs, inst)
all = append(all, inst)
allRole = append(allRole, "tikv")
}

for i := 0; i < tidbNum; i++ {
for i := 0; i < options.tidbNum; i++ {
dir := filepath.Join(dataDir, fmt.Sprintf("tidb-%d", i))
inst := instance.NewTiDBInstance(dir, host, i, pds)
inst := instance.NewTiDBInstance(dir, options.host, options.tidbConfigPath, i, pds)
dbs = append(dbs, inst)
all = append(all, inst)
allRole = append(allRole, "tidb")
Expand All @@ -236,7 +282,7 @@ func bootCluster(version string, pdNum, tidbNum, tikvNum int, host string, monit
fmt.Println("Playground Bootstrapping...")

var monitorAddr string
if monitor {
if options.monitor {
if err := installIfMissing(profile, "prometheus", ""); err != nil {
return err
}
Expand All @@ -252,7 +298,7 @@ func bootCluster(version string, pdNum, tidbNum, tikvNum int, host string, monit
}

promDir := filepath.Join(dataDir, "prometheus")
addr, cmd, err := startMonitor(ctx, host, promDir, tidbAddrs, tikvAddrs, pdAddrs)
addr, cmd, err := startMonitor(ctx, options.host, promDir, tidbAddrs, tikvAddrs, pdAddrs)
if err != nil {
return err
}
Expand Down Expand Up @@ -280,7 +326,7 @@ func bootCluster(version string, pdNum, tidbNum, tikvNum int, host string, monit
}

for i, inst := range all {
if err := inst.Start(ctx, repository.Version(version), pathMap[allRole[i]]); err != nil {
if err := inst.Start(ctx, repository.Version(options.version), pathMap[allRole[i]]); err != nil {
return err
}
}
Expand Down