Skip to content

Commit

Permalink
playground: Add --dry-run for preparing binaries in CI
Browse files Browse the repository at this point in the history
Signed-off-by: Wish <[email protected]>
  • Loading branch information
breezewish committed Dec 4, 2024
1 parent f7d5cb8 commit 0aa2ba0
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 22 deletions.
3 changes: 3 additions & 0 deletions components/playground/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ type BootOptions struct {
Mode string `yaml:"mode"`
PDMode string `yaml:"pd_mode"`
Version string `yaml:"version"`
DryRun bool `yaml:"dry_run"`
PD instance.Config `yaml:"pd"` // will change to api when pd_mode == ms
TSO instance.Config `yaml:"tso"` // Only available when pd_mode == ms
Scheduling instance.Config `yaml:"scheduling"` // Only available when pd_mode == ms
Expand Down Expand Up @@ -285,6 +286,8 @@ Note: Version constraint [bold]%s[reset] is resolved to [green][bold]%s[reset].
rootCmd.Flags().IntVar(&options.GrafanaPort, "grafana.port", 3000, "grafana port. If not provided, grafana will use 3000 as its port.")
rootCmd.Flags().IntVar(&options.PortOffset, "port-offset", 0, "If specified, all components will use default_port+port_offset as the port. This argument is useful when you want to start multiple playgrounds on the same host. Recommend to set to 10000, 20000, etc.")

rootCmd.Flags().BoolVar(&options.DryRun, "dry-run", false, "Do not actually run any component. Useful to prepare binaries in CI environment, or checkout what will be started.")

// NOTE: Do not set default values if they may be changed in different modes.

rootCmd.Flags().IntVar(&options.TiDB.Num, "db", 0, "TiDB instance number")
Expand Down
70 changes: 48 additions & 22 deletions components/playground/playground.go
Original file line number Diff line number Diff line change
Expand Up @@ -518,10 +518,12 @@ func (p *Playground) startInstance(ctx context.Context, inst instance.Instance)
return err
}

if err = inst.Start(ctx); err != nil {
return err
if !p.bootOptions.DryRun {
if err = inst.Start(ctx); err != nil {
return err
}
p.addWaitInstance(inst)
}
p.addWaitInstance(inst)
return nil
}

Expand Down Expand Up @@ -871,6 +873,11 @@ func (p *Playground) addInstance(componentID string, pdRole instance.PDRole, tif
}

func (p *Playground) waitAllDBUp() ([]string, []string) {
if p.bootOptions.DryRun {
// No wait, return immediately.
return nil, nil
}

var tidbSucc []string
var tiproxySucc []string
if len(p.tidbs) > 0 {
Expand Down Expand Up @@ -938,6 +945,11 @@ func (p *Playground) waitAllDBUp() ([]string, []string) {
}

func (p *Playground) waitAllTiFlashUp() {
if p.bootOptions.DryRun {
// No wait, return immediately.
return
}

if len(p.tiflashs) > 0 {
var endpoints []string
for _, pd := range p.pds {
Expand Down Expand Up @@ -1238,7 +1250,7 @@ func (p *Playground) bootCluster(ctx context.Context, env *environment.Environme

colorCmd := color.New(color.FgHiCyan, color.Bold)

if len(tidbSucc) > 0 {
if len(tidbSucc) > 0 || p.bootOptions.DryRun {
// start TiFlash after at least one TiDB is up.
var started []*instance.TiFlashInstance
for _, flash := range p.tiflashs {
Expand All @@ -1251,23 +1263,25 @@ func (p *Playground) bootCluster(ctx context.Context, env *environment.Environme
p.tiflashs = started
p.waitAllTiFlashUp()

fmt.Println()
color.New(color.FgGreen, color.Bold).Println("🎉 TiDB Playground Cluster is started, enjoy!")
fmt.Println()
mysql := mysqlCommand()
for _, dbAddr := range tidbSucc {
ss := strings.Split(dbAddr, ":")
fmt.Printf("Connect TiDB: ")
colorCmd.Printf("%s --host %s --port %s -u root\n", mysql, ss[0], ss[1])
}
for _, dbAddr := range tiproxySucc {
ss := strings.Split(dbAddr, ":")
fmt.Printf("Connect TiProxy: ")
colorCmd.Printf("%s --host %s --port %s -u root\n", mysql, ss[0], ss[1])
if !p.bootOptions.DryRun {
fmt.Println()
color.New(color.FgGreen, color.Bold).Println("🎉 TiDB Playground Cluster is started, enjoy!")
fmt.Println()
mysql := mysqlCommand()
for _, dbAddr := range tidbSucc {
ss := strings.Split(dbAddr, ":")
fmt.Printf("Connect TiDB: ")
colorCmd.Printf("%s --host %s --port %s -u root\n", mysql, ss[0], ss[1])
}
for _, dbAddr := range tiproxySucc {
ss := strings.Split(dbAddr, ":")
fmt.Printf("Connect TiProxy: ")
colorCmd.Printf("%s --host %s --port %s -u root\n", mysql, ss[0], ss[1])
}
}
}

if len(p.dmMasters) > 0 {
if len(p.dmMasters) > 0 && !p.bootOptions.DryRun {
fmt.Printf("Connect DM: ")
endpoints := make([]string, 0, len(p.dmMasters))
for _, dmMaster := range p.dmMasters {
Expand All @@ -1276,7 +1290,7 @@ func (p *Playground) bootCluster(ctx context.Context, env *environment.Environme
colorCmd.Printf("tiup dmctl --master-addr %s\n", strings.Join(endpoints, ","))
}

if len(p.pds) > 0 {
if len(p.pds) > 0 && !p.bootOptions.DryRun {
if pdAddr := p.pds[0].Addr(); len(p.tidbs) > 0 && hasDashboard(pdAddr) {
fmt.Printf("TiDB Dashboard: ")
colorCmd.Printf("http://%s/dashboard\n", pdAddr)
Expand Down Expand Up @@ -1316,7 +1330,7 @@ func (p *Playground) bootCluster(ctx context.Context, env *environment.Environme
}
}

if monitorInfo != nil {
if monitorInfo != nil && !p.bootOptions.DryRun {
p.updateMonitorTopology(spec.ComponentPrometheus, *monitorInfo)
}

Expand All @@ -1332,7 +1346,7 @@ func (p *Playground) bootCluster(ctx context.Context, env *environment.Environme

logIfErr(p.renderSDFile())

if g := p.grafana; g != nil {
if g := p.grafana; g != nil && !p.bootOptions.DryRun {
p.updateMonitorTopology(spec.ComponentGrafana, MonitorInfo{g.host, g.port, g.cmd.Path})
fmt.Printf("Grafana: ")
colorCmd.Printf("http://%s\n", utils.JoinHostPort(g.host, g.port))
Expand Down Expand Up @@ -1522,6 +1536,10 @@ func (p *Playground) bootMonitor(ctx context.Context, env *environment.Environme
monitor.cmd.Stderr = log
monitor.cmd.Stdout = os.Stdout

if p.bootOptions.DryRun {
return nil, nil, nil
}

if err := monitor.cmd.Start(); err != nil {
return nil, nil, err
}
Expand Down Expand Up @@ -1558,6 +1576,10 @@ func (p *Playground) bootNGMonitoring(ctx context.Context, env *environment.Envi
return nil, nil
}

if p.bootOptions.DryRun {
return nil, nil
}

if err := ngm.cmd.Start(); err != nil {
return nil, err
}
Expand Down Expand Up @@ -1625,7 +1647,11 @@ func (p *Playground) bootGrafana(ctx context.Context, env *environment.Environme
}

grafana := newGrafana(options.Version, options.Host, options.GrafanaPort)
// fmt.Println("Start Grafana instance...")

if p.bootOptions.DryRun {
return nil, nil
}

err = grafana.start(ctx, grafanaDir, options.PortOffset, "http://"+utils.JoinHostPort(monitorInfo.IP, monitorInfo.Port))
if err != nil {
return nil, err
Expand Down

0 comments on commit 0aa2ba0

Please sign in to comment.