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

playground: Allow specifying port offset #2453

Merged
merged 1 commit into from
Aug 20, 2024
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
8 changes: 2 additions & 6 deletions components/playground/grafana.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,12 +151,8 @@ var clusterName = "Test-Cluster"

// dir should contains files untar the grafana.
// return not error iff the Cmd is started successfully.
func (g *grafana) start(ctx context.Context, dir string, p8sURL string) (err error) {
g.port, err = utils.GetFreePort(g.host, g.port)
if err != nil {
return err
}

func (g *grafana) start(ctx context.Context, dir string, portOffset int, p8sURL string) (err error) {
g.port = utils.MustGetFreePort(g.host, g.port, portOffset)
fname := filepath.Join(dir, "conf", "provisioning", "dashboards", "dashboard.yml")
err = writeDashboardConfig(fname, clusterName, filepath.Join(dir, "dashboards"))
if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions components/playground/instance/drainer.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,14 @@ type Drainer struct {
var _ Instance = &Drainer{}

// NewDrainer create a Drainer instance.
func NewDrainer(binPath string, dir, host, configPath string, id int, pds []*PDInstance) *Drainer {
func NewDrainer(binPath string, dir, host, configPath string, portOffset int, id int, pds []*PDInstance) *Drainer {
d := &Drainer{
instance: instance{
BinPath: binPath,
ID: id,
Dir: dir,
Host: host,
Port: utils.MustGetFreePort(host, 8250),
Port: utils.MustGetFreePort(host, 8250, portOffset),
ConfigPath: configPath,
},
pds: pds,
Expand Down
5 changes: 3 additions & 2 deletions components/playground/instance/instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"github.com/pingcap/errors"
"github.com/pingcap/tiup/pkg/cluster/spec"
tiupexec "github.com/pingcap/tiup/pkg/exec"
"github.com/pingcap/tiup/pkg/tui/colorstr"
"github.com/pingcap/tiup/pkg/utils"
)

Expand Down Expand Up @@ -98,9 +99,9 @@ func (inst *instance) PrepareBinary(componentName string, version utils.Version)
}
// distinguish whether the instance is started by specific binary path.
if inst.BinPath == "" {
fmt.Printf("Start %s instance:%s\n", componentName, version)
colorstr.Printf("[dark_gray]Start %s instance: %s[reset]\n", componentName, version)
} else {
fmt.Printf("Start %s instance:%s\n", componentName, instanceBinPath)
colorstr.Printf("[dark_gray]Start %s instance: %s[reset]\n", componentName, instanceBinPath)
}
inst.Version = version
inst.BinPath = instanceBinPath
Expand Down
6 changes: 3 additions & 3 deletions components/playground/instance/pd.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ type PDInstance struct {
}

// NewPDInstance return a PDInstance
func NewPDInstance(role PDRole, binPath, dir, host, configPath string, id int, pds []*PDInstance, port int, isCSEMode bool) *PDInstance {
func NewPDInstance(role PDRole, binPath, dir, host, configPath string, portOffset int, id int, pds []*PDInstance, port int, isCSEMode bool) *PDInstance {
if port <= 0 {
port = 2379
}
Expand All @@ -60,8 +60,8 @@ func NewPDInstance(role PDRole, binPath, dir, host, configPath string, id int, p
ID: id,
Dir: dir,
Host: host,
Port: utils.MustGetFreePort(host, 2380),
StatusPort: utils.MustGetFreePort(host, port),
Port: utils.MustGetFreePort(host, 2380, portOffset),
StatusPort: utils.MustGetFreePort(host, port, portOffset),
ConfigPath: configPath,
},
Role: role,
Expand Down
4 changes: 2 additions & 2 deletions components/playground/instance/pump.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,14 @@ type Pump struct {
var _ Instance = &Pump{}

// NewPump create a Pump instance.
func NewPump(binPath string, dir, host, configPath string, id int, pds []*PDInstance) *Pump {
func NewPump(binPath string, dir, host, configPath string, portOffset int, id int, pds []*PDInstance) *Pump {
pump := &Pump{
instance: instance{
BinPath: binPath,
ID: id,
Dir: dir,
Host: host,
Port: utils.MustGetFreePort(host, 8249),
Port: utils.MustGetFreePort(host, 8249, portOffset),
ConfigPath: configPath,
},
pds: pds,
Expand Down
4 changes: 2 additions & 2 deletions components/playground/instance/ticdc.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ type TiCDC struct {
var _ Instance = &TiCDC{}

// NewTiCDC create a TiCDC instance.
func NewTiCDC(binPath string, dir, host, configPath string, id int, port int, pds []*PDInstance) *TiCDC {
func NewTiCDC(binPath string, dir, host, configPath string, portOffset int, id int, port int, pds []*PDInstance) *TiCDC {
if port <= 0 {
port = 8300
}
Expand All @@ -43,7 +43,7 @@ func NewTiCDC(binPath string, dir, host, configPath string, id int, port int, pd
ID: id,
Dir: dir,
Host: host,
Port: utils.MustGetFreePort(host, port),
Port: utils.MustGetFreePort(host, port, portOffset),
ConfigPath: configPath,
},
pds: pds,
Expand Down
6 changes: 3 additions & 3 deletions components/playground/instance/tidb.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ type TiDBInstance struct {
}

// NewTiDBInstance return a TiDBInstance
func NewTiDBInstance(binPath string, dir, host, configPath string, id, port int, pds []*PDInstance, tiproxyCertDir string, enableBinlog bool, isCSEMode bool) *TiDBInstance {
func NewTiDBInstance(binPath string, dir, host, configPath string, portOffset int, id, port int, pds []*PDInstance, tiproxyCertDir string, enableBinlog bool, isCSEMode bool) *TiDBInstance {
if port <= 0 {
port = 4000
}
Expand All @@ -44,8 +44,8 @@ func NewTiDBInstance(binPath string, dir, host, configPath string, id, port int,
ID: id,
Dir: dir,
Host: host,
Port: utils.MustGetFreePort(host, port),
StatusPort: utils.MustGetFreePort("0.0.0.0", 10080),
Port: utils.MustGetFreePort(host, port, portOffset),
StatusPort: utils.MustGetFreePort("0.0.0.0", 10080, portOffset),
ConfigPath: configPath,
},
tiproxyCertDir: tiproxyCertDir,
Expand Down
14 changes: 7 additions & 7 deletions components/playground/instance/tiflash.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,14 @@ type TiFlashInstance struct {
}

// NewTiFlashInstance return a TiFlashInstance
func NewTiFlashInstance(role TiFlashRole, cseOptions CSEOptions, binPath, dir, host, configPath string, id int, pds []*PDInstance, dbs []*TiDBInstance, version string) *TiFlashInstance {
func NewTiFlashInstance(role TiFlashRole, cseOptions CSEOptions, binPath, dir, host, configPath string, portOffset int, id int, pds []*PDInstance, dbs []*TiDBInstance, version string) *TiFlashInstance {
if role != TiFlashRoleNormal && role != TiFlashRoleDisaggWrite && role != TiFlashRoleDisaggCompute {
panic(fmt.Sprintf("Unknown TiFlash role %s", role))
}

httpPort := 8123
if !tidbver.TiFlashNotNeedHTTPPortConfig(version) {
httpPort = utils.MustGetFreePort(host, httpPort)
httpPort = utils.MustGetFreePort(host, httpPort, portOffset)
}
return &TiFlashInstance{
instance: instance{
Expand All @@ -69,15 +69,15 @@ func NewTiFlashInstance(role TiFlashRole, cseOptions CSEOptions, binPath, dir, h
Dir: dir,
Host: host,
Port: httpPort,
StatusPort: utils.MustGetFreePort(host, 8234),
StatusPort: utils.MustGetFreePort(host, 8234, portOffset),
ConfigPath: configPath,
},
Role: role,
cseOpts: cseOptions,
TCPPort: utils.MustGetFreePort(host, 9100), // 9000 for default object store port
ServicePort: utils.MustGetFreePort(host, 3930),
ProxyPort: utils.MustGetFreePort(host, 20170),
ProxyStatusPort: utils.MustGetFreePort(host, 20292),
TCPPort: utils.MustGetFreePort(host, 9100, portOffset), // 9000 for default object store port
ServicePort: utils.MustGetFreePort(host, 3930, portOffset),
ProxyPort: utils.MustGetFreePort(host, 20170, portOffset),
ProxyStatusPort: utils.MustGetFreePort(host, 20292, portOffset),
pds: pds,
dbs: dbs,
}
Expand Down
6 changes: 3 additions & 3 deletions components/playground/instance/tikv.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ type TiKVInstance struct {
}

// NewTiKVInstance return a TiKVInstance
func NewTiKVInstance(binPath string, dir, host, configPath string, id int, port int, pds []*PDInstance, tsos []*PDInstance, isCSEMode bool, cseOptions CSEOptions, isPDMSMode bool) *TiKVInstance {
func NewTiKVInstance(binPath string, dir, host, configPath string, portOffset int, id int, port int, pds []*PDInstance, tsos []*PDInstance, isCSEMode bool, cseOptions CSEOptions, isPDMSMode bool) *TiKVInstance {
if port <= 0 {
port = 20160
}
Expand All @@ -46,8 +46,8 @@ func NewTiKVInstance(binPath string, dir, host, configPath string, id int, port
ID: id,
Dir: dir,
Host: host,
Port: utils.MustGetFreePort(host, port),
StatusPort: utils.MustGetFreePort(host, 20180),
Port: utils.MustGetFreePort(host, port, portOffset),
StatusPort: utils.MustGetFreePort(host, 20180, portOffset),
ConfigPath: configPath,
},
pds: pds,
Expand Down
4 changes: 2 additions & 2 deletions components/playground/instance/tikv_cdc.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,14 @@ type TiKVCDC struct {
var _ Instance = &TiKVCDC{}

// NewTiKVCDC create a TiKVCDC instance.
func NewTiKVCDC(binPath string, dir, host, configPath string, id int, pds []*PDInstance) *TiKVCDC {
func NewTiKVCDC(binPath string, dir, host, configPath string, portOffset int, id int, pds []*PDInstance) *TiKVCDC {
tikvCdc := &TiKVCDC{
instance: instance{
BinPath: binPath,
ID: id,
Dir: dir,
Host: host,
Port: utils.MustGetFreePort(host, 8600),
Port: utils.MustGetFreePort(host, 8600, portOffset),
ConfigPath: configPath,
},
pds: pds,
Expand Down
6 changes: 3 additions & 3 deletions components/playground/instance/tiproxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ func GenTiProxySessionCerts(dir string) error {
}

// NewTiProxy create a TiProxy instance.
func NewTiProxy(binPath string, dir, host, configPath string, id int, port int, pds []*PDInstance) *TiProxy {
func NewTiProxy(binPath string, dir, host, configPath string, portOffset int, id int, port int, pds []*PDInstance) *TiProxy {
if port <= 0 {
port = 6000
}
Expand All @@ -78,8 +78,8 @@ func NewTiProxy(binPath string, dir, host, configPath string, id int, port int,
ID: id,
Dir: dir,
Host: host,
Port: utils.MustGetFreePort(host, port),
StatusPort: utils.MustGetFreePort(host, 3080),
Port: utils.MustGetFreePort(host, port, portOffset),
StatusPort: utils.MustGetFreePort(host, 3080, portOffset),
ConfigPath: configPath,
},
pds: pds,
Expand Down
19 changes: 9 additions & 10 deletions components/playground/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ type BootOptions struct {
Monitor bool `yaml:"monitor"`
CSEOpts instance.CSEOptions `yaml:"cse"` // Only available when mode == tidb-cse
GrafanaPort int `yaml:"grafana_port"`
PortOffset int `yaml:"port_offset"`
}

var (
Expand Down Expand Up @@ -175,11 +176,8 @@ Examples:
return err
}

port, err := utils.GetFreePort("0.0.0.0", 9527)
if err != nil {
return err
}
err = dumpPort(filepath.Join(dataDir, "port"), port)
port := utils.MustGetFreePort("0.0.0.0", 9527, options.PortOffset)
err := dumpPort(filepath.Join(dataDir, "port"), port)
p := NewPlayground(dataDir, port)
if err != nil {
return err
Expand Down Expand Up @@ -207,7 +205,7 @@ Examples:

sig := (<-sc).(syscall.Signal)
atomic.StoreInt32(&p.curSig, int32(sig))
fmt.Println("Playground receive signal: ", sig)
colorstr.Printf("\n[red][bold]Playground receive signal: %s[reset]\n", sig)

// if bootCluster is not done we just cancel context to make it
// clean up and return ASAP and exit directly after timeout.
Expand Down Expand Up @@ -283,6 +281,7 @@ Note: Version constraint [bold]%s[reset] is resolved to [green][bold]%s[reset].
rootCmd.Flags().BoolVar(&options.Monitor, "monitor", true, "Start prometheus and grafana component")
_ = rootCmd.Flags().MarkDeprecated("monitor", "Please use --without-monitor to control whether to disable monitor.")
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.")

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

Expand Down Expand Up @@ -347,10 +346,10 @@ Note: Version constraint [bold]%s[reset] is resolved to [green][bold]%s[reset].

rootCmd.Flags().StringVar(&options.TiKVCDC.Version, "kvcdc.version", "", "TiKV-CDC instance version")

rootCmd.Flags().StringVar(&options.CSEOpts.S3Endpoint, "cse.s3_endpoint", "http://127.0.0.1:9000", "Object store URL for the disaggregated TiFlash, available when --mode=tidb-cse")
rootCmd.Flags().StringVar(&options.CSEOpts.Bucket, "cse.bucket", "tiflash", "Object store bucket for the disaggregated TiFlash, available when --mode=tidb-cse")
rootCmd.Flags().StringVar(&options.CSEOpts.AccessKey, "cse.access_key", "minioadmin", "Object store access key, available when --mode=tidb-cse")
rootCmd.Flags().StringVar(&options.CSEOpts.SecretKey, "cse.secret_key", "minioadmin", "Object store secret key, available when --mode=tidb-cse")
rootCmd.Flags().StringVar(&options.CSEOpts.S3Endpoint, "cse.s3_endpoint", "http://127.0.0.1:9000", "Object store URL for --mode=tidb-cse")
rootCmd.Flags().StringVar(&options.CSEOpts.Bucket, "cse.bucket", "tiflash", "Object store bucket for --mode=tidb-cse")
rootCmd.Flags().StringVar(&options.CSEOpts.AccessKey, "cse.access_key", "minioadmin", "Object store access key for --mode=tidb-cse")
rootCmd.Flags().StringVar(&options.CSEOpts.SecretKey, "cse.secret_key", "minioadmin", "Object store secret key for --mode=tidb-cse")

rootCmd.AddCommand(newDisplay())
rootCmd.AddCommand(newScaleOut())
Expand Down
8 changes: 3 additions & 5 deletions components/playground/monitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,15 +78,12 @@ func (m *monitor) wait() error {
}

// the cmd is not started after return
func newMonitor(ctx context.Context, version string, host, dir string) (*monitor, error) {
func newMonitor(ctx context.Context, version string, host, dir string, portOffset int) (*monitor, error) {
if err := utils.MkdirAll(dir, 0755); err != nil {
return nil, errors.AddStack(err)
}

port, err := utils.GetFreePort(host, 9090)
if err != nil {
return nil, err
}
port := utils.MustGetFreePort(host, 9090, portOffset)
addr := utils.JoinHostPort(host, port)

tmpl := `
Expand Down Expand Up @@ -132,6 +129,7 @@ scrape_configs:
}

var binPath string
var err error
if binPath, err = tiupexec.PrepareBinary("prometheus", utils.Version(version), binPath); err != nil {
return nil, err
}
Expand Down
8 changes: 2 additions & 6 deletions components/playground/ngmonitoring.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,16 +45,12 @@ func (m *ngMonitoring) wait() error {
}

// the cmd is not started after return
func newNGMonitoring(ctx context.Context, version string, host, dir string, pds []*instance.PDInstance) (*ngMonitoring, error) {
func newNGMonitoring(ctx context.Context, version string, host, dir string, portOffset int, pds []*instance.PDInstance) (*ngMonitoring, error) {
if err := utils.MkdirAll(dir, 0755); err != nil {
return nil, errors.AddStack(err)
}

port, err := utils.GetFreePort(host, 12020)
if err != nil {
return nil, err
}

port := utils.MustGetFreePort(host, 12020, portOffset)
m := new(ngMonitoring)
var endpoints []string
for _, pd := range pds {
Expand Down
Loading
Loading