diff --git a/components/playground/main.go b/components/playground/main.go index 7de793e9b9..4ac764fe64 100644 --- a/components/playground/main.go +++ b/components/playground/main.go @@ -146,7 +146,7 @@ if you don't specified a version. Examples: $ tiup playground nightly # Start a TiDB nightly version local cluster $ tiup playground v5.0.1 --db 3 --pd 3 --kv 3 # Start a local cluster with 10 nodes - $ tiup playground nightly --monitor=false # Start a local cluster and disable monitor system + $ tiup playground nightly --without-monitor # Start a local cluster and disable 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 $ tiup playground --mode tikv-slim # Start a local tikv only cluster (No TiDB or TiFlash Available) diff --git a/doc/user/playground.md b/doc/user/playground.md index 98538f2c76..31acbd4b7e 100644 --- a/doc/user/playground.md +++ b/doc/user/playground.md @@ -22,23 +22,48 @@ The command line arguments for playground state: ```bash Usage: tiup playground [version] [flags] + tiup [command] + +Available Commands: + completion generate the autocompletion script for the specified shell + display + help Help about any command + scale-in + scale-out Flags: - --db int TiDB instance number (default 1) + --db int TiDB instance number + --db.host host Playground TiDB host. If not provided, TiDB will still use host flag as its host + --db.port int Playground TiDB port. If not provided, TiDB will use 4000 as its port --db.binpath string TiDB instance binary path --db.config string TiDB instance configuration file + --db.timeout int TiDB max wait time in seconds for starting, 0 means no limit + --drainer int Drainer instance number + --drainer.binpath string Drainer instance binary path + --drainer.config string Drainer instance configuration file -h, --help help for tiup - --host string Playground cluster host (default "127.0.0.1") - --kv int TiKV instance number (default 1) + --host string Playground cluster host + --kv int TiKV instance number --kv.binpath string TiKV instance binary path --kv.config string TiKV instance configuration file - --monitor Start prometheus component - --pd int PD instance number (default 1) + --mode string TiUP playground mode: 'tidb', 'tikv-slim' (default "tidb") + --pd int PD instance number + --pd.Host host Playground PD host. If not provided, PD will still use host flag as its host --pd.binpath string PD instance binary path --pd.config string PD instance configuration file + --pump int Pump instance number + --pump.binpath string Pump instance binary path + --pump.config string Pump instance configuration file + -T, --tag string Specify a tag for playground + --ticdc int TiCDC instance number + --ticdc.binpath string TiCDC instance binary path + --ticdc.config string TiCDC instance configuration file --tiflash int TiFlash instance number --tiflash.binpath string TiFlash instance binary path --tiflash.config string TiFlash instance configuration file + --tiflash.timeout int TiFlash max wait time in seconds for starting, 0 means no limit + -v, --version version for tiup + --without-monitor Don't start prometheus and grafana component ``` ## Example @@ -51,13 +76,19 @@ tiup playground nightly Nightly is the version number of this cluster, and similar ones can be `tiup playground v4.0.0-rc` etc. -### Start a cluster with monitoring. +### Start a cluster with or without monitoring. + +```shell +tiup playground nightly +``` + +This command launches Prometheus on port 9090 and Grafana on port 3000 for displaying timing data within the cluster. ```shell -tiup playground nightly --monitor +tiup playground nightly --without-monitor ``` -This command launches prometheus on port 9090 for displaying timing data within the cluster. +This won't launch Prometheus or Grafana. This can be used to save resources. ### Overrides the default configuration of the PD diff --git a/pkg/cluster/task/step.go b/pkg/cluster/task/step.go index fa34ec37ff..34b223b18e 100644 --- a/pkg/cluster/task/step.go +++ b/pkg/cluster/task/step.go @@ -96,7 +96,8 @@ func (s *StepDisplay) Execute(ctx context.Context) error { } switch s.DisplayMode { - case log.DisplayModeJSON: + case log.DisplayModeJSON, + log.DisplayModePlain: break default: if singleBar, ok := s.progressBar.(*progress.SingleBar); ok { @@ -126,7 +127,9 @@ func (s *StepDisplay) Execute(ctx context.Context) error { switch s.DisplayMode { case log.DisplayModeJSON: - _ = printDp(dp) + _ = printDpJSON(dp) + case log.DisplayModePlain: + printDpPlain(dp) default: s.progressBar.UpdateDisplay(dp) } @@ -153,7 +156,9 @@ func (s *StepDisplay) handleTaskBegin(task Task) { } switch s.DisplayMode { case log.DisplayModeJSON: - _ = printDp(dp) + _ = printDpJSON(dp) + case log.DisplayModePlain: + printDpPlain(dp) default: s.progressBar.UpdateDisplay(dp) } @@ -169,7 +174,9 @@ func (s *StepDisplay) handleTaskProgress(task Task, p string) { } switch s.DisplayMode { case log.DisplayModeJSON: - _ = printDp(dp) + _ = printDpJSON(dp) + case log.DisplayModePlain: + printDpPlain(dp) default: s.progressBar.UpdateDisplay(dp) } @@ -209,7 +216,8 @@ func (ps *ParallelStepDisplay) SetDisplayMode(m log.DisplayMode) *ParallelStepDi // Execute implements the Task interface func (ps *ParallelStepDisplay) Execute(ctx context.Context) error { switch ps.DisplayMode { - case log.DisplayModeJSON: + case log.DisplayModeJSON, + log.DisplayModePlain: break default: ps.progressBar.StartRenderLoop() @@ -229,7 +237,7 @@ func (ps *ParallelStepDisplay) String() string { return ps.inner.String() } -func printDp(dp *progress.DisplayProps) error { +func printDpJSON(dp *progress.DisplayProps) error { output, err := json.Marshal(dp) if err != nil { return err @@ -237,3 +245,12 @@ func printDp(dp *progress.DisplayProps) error { fmt.Println(string(output)) return nil } + +func printDpPlain(dp *progress.DisplayProps) { + switch dp.Mode { + case progress.ModeError: + log.Errorf("progress: %s", dp) + default: + log.Infof("progress: %s", dp) + } +} diff --git a/pkg/logger/log/log.go b/pkg/logger/log/log.go index 11c5805898..65f658e335 100644 --- a/pkg/logger/log/log.go +++ b/pkg/logger/log/log.go @@ -24,6 +24,9 @@ import ( var ( outputFmt DisplayMode = DisplayModeDefault // global output format of logger + + stdout io.Writer = os.Stdout + stderr io.Writer = os.Stderr ) // DisplayMode control the output format @@ -76,19 +79,29 @@ func Debugf(format string, args ...interface{}) { // Deprecated: Use zap.L().Info() instead func Infof(format string, args ...interface{}) { zap.L().Info(fmt.Sprintf(format, args...)) - printLog(os.Stdout, "info", format, args...) + printLog(stdout, "info", format, args...) } // Warnf output the warning message to console // Deprecated: Use zap.L().Warn() instead func Warnf(format string, args ...interface{}) { zap.L().Warn(fmt.Sprintf(format, args...)) - printLog(os.Stderr, "warn", format, args...) + printLog(stderr, "warn", format, args...) } // Errorf output the error message to console // Deprecated: Use zap.L().Error() instead func Errorf(format string, args ...interface{}) { zap.L().Error(fmt.Sprintf(format, args...)) - printLog(os.Stderr, "error", format, args...) + printLog(stderr, "error", format, args...) +} + +// SetStdout redirect stdout to a custom writer +func SetStdout(w io.Writer) { + stdout = w +} + +// SetStderr redirect stderr to a custom writer +func SetStderr(w io.Writer) { + stderr = w } diff --git a/pkg/logger/log/verbose.go b/pkg/logger/log/verbose.go index 1cd8c35b0b..a1d49e196b 100644 --- a/pkg/logger/log/verbose.go +++ b/pkg/logger/log/verbose.go @@ -31,5 +31,5 @@ func Verbose(format string, args ...interface{}) { if !verbose { return } - fmt.Fprintln(os.Stderr, "Verbose:", fmt.Sprintf(format, args...)) + fmt.Fprintln(stderr, "Verbose:", fmt.Sprintf(format, args...)) } diff --git a/pkg/tui/progress/display_props.go b/pkg/tui/progress/display_props.go index 8883f59ef3..65ead9d217 100644 --- a/pkg/tui/progress/display_props.go +++ b/pkg/tui/progress/display_props.go @@ -15,6 +15,7 @@ package progress import ( "encoding/json" + "fmt" "strings" ) @@ -72,9 +73,37 @@ func (m *Mode) UnmarshalJSON(b []byte) error { return nil } +// String implements string +func (m Mode) String() string { + var s string + switch m { + case ModeSpinner: + s = "spinner" + case ModeProgress: + s = "progress" + case ModeDone: + s = "done" + case ModeError: + s = "error" + default: + s = "unknown" + } + return s +} + // DisplayProps controls the display of the progress bar. type DisplayProps struct { Prefix string `json:"prefix,omitempty"` Suffix string `json:"suffix,omitempty"` // If `Mode == Done / Error`, Suffix is not printed Mode Mode `json:"mode,omitempty"` } + +// String implements string +func (dp *DisplayProps) String() string { + return fmt.Sprintf( + "(%s) %s: %s", + dp.Mode, + dp.Prefix, + dp.Suffix, + ) +}