Skip to content

Commit

Permalink
support microservices mode for playground
Browse files Browse the repository at this point in the history
Signed-off-by: Ryan Leung <[email protected]>
  • Loading branch information
rleungx committed Jul 10, 2023
1 parent c2ae2f6 commit b020943
Show file tree
Hide file tree
Showing 6 changed files with 176 additions and 59 deletions.
1 change: 0 additions & 1 deletion components/playground/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,6 @@ func newScaleOut() *cobra.Command {
cmd.Flags().IntVarP(&opt.TiKVCDC.Num, "kvcdc", "", opt.TiKVCDC.Num, "TiKV-CDC instance number")
cmd.Flags().IntVarP(&opt.Pump.Num, "pump", "", opt.Pump.Num, "Pump instance number")
cmd.Flags().IntVarP(&opt.Drainer.Num, "drainer", "", opt.Pump.Num, "Drainer instance number")

cmd.Flags().StringVarP(&opt.TiDB.Host, "db.host", "", opt.TiDB.Host, "Playground TiDB host. If not provided, TiDB will still use `host` flag as its host")
cmd.Flags().StringVarP(&opt.PD.Host, "pd.host", "", opt.PD.Host, "Playground PD host. If not provided, PD will still use `host` flag as its host")

Expand Down
3 changes: 3 additions & 0 deletions components/playground/instance/instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,9 @@ func logIfErr(err error) {
func pdEndpoints(pds []*PDInstance, isHTTP bool) []string {
var endpoints []string
for _, pd := range pds {
if pd.Role == PDRoleTSO {
continue
}
if isHTTP {
endpoints = append(endpoints, "http://"+utils.JoinHostPort(AdvertiseHost(pd.Host), pd.StatusPort))
} else {
Expand Down
110 changes: 80 additions & 30 deletions components/playground/instance/pd.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,32 @@ import (
"github.com/pingcap/tiup/pkg/utils"
)

// PDRole is the role of PD.
type PDRole string

const (
// PDRoleNormal is the default role of PD
PDRoleNormal PDRole = "pd"
// PDRoleAPI is the role of PD API
PDRoleAPI PDRole = "api"
// PDRoleTSO is the role of PD TSO
PDRoleTSO PDRole = "tso"
// PDRoleResourceManager is the role of PD resource manager
PDRoleResourceManager PDRole = "resource manager"
)

// PDInstance represent a running pd-server
type PDInstance struct {
instance
Role PDRole
initEndpoints []*PDInstance
joinEndpoints []*PDInstance
pds []*PDInstance
Process
}

// NewPDInstance return a PDInstance
func NewPDInstance(binPath, dir, host, configPath string, id, port int) *PDInstance {
func NewPDInstance(role PDRole, binPath, dir, host, configPath string, id int, pds []*PDInstance, port int) *PDInstance {
if port <= 0 {
port = 2379
}
Expand All @@ -47,6 +63,8 @@ func NewPDInstance(binPath, dir, host, configPath string, id, port int) *PDInsta
StatusPort: utils.MustGetFreePort(host, port),
ConfigPath: configPath,
},
Role: role,
pds: pds,
}
}

Expand All @@ -70,35 +88,67 @@ func (inst *PDInstance) Name() string {
// Start calls set inst.cmd and Start
func (inst *PDInstance) Start(ctx context.Context, version utils.Version) error {
uid := inst.Name()
args := []string{
"--name=" + uid,
fmt.Sprintf("--data-dir=%s", filepath.Join(inst.Dir, "data")),
fmt.Sprintf("--peer-urls=http://%s", utils.JoinHostPort(inst.Host, inst.Port)),
fmt.Sprintf("--advertise-peer-urls=http://%s", utils.JoinHostPort(AdvertiseHost(inst.Host), inst.Port)),
fmt.Sprintf("--client-urls=http://%s", utils.JoinHostPort(inst.Host, inst.StatusPort)),
fmt.Sprintf("--advertise-client-urls=http://%s", utils.JoinHostPort(AdvertiseHost(inst.Host), inst.StatusPort)),
fmt.Sprintf("--log-file=%s", inst.LogFile()),
}
if inst.ConfigPath != "" {
args = append(args, fmt.Sprintf("--config=%s", inst.ConfigPath))
}

switch {
case len(inst.initEndpoints) > 0:
endpoints := make([]string, 0)
for _, pd := range inst.initEndpoints {
uid := fmt.Sprintf("pd-%d", pd.ID)
endpoints = append(endpoints, fmt.Sprintf("%s=http://%s", uid, utils.JoinHostPort(AdvertiseHost(inst.Host), pd.Port)))
var args []string
switch inst.Role {
case PDRoleNormal, PDRoleAPI:
if inst.Role == PDRoleAPI {
args = []string{"services", "api"}
}
args = append(args, []string{
"--name=" + uid,
fmt.Sprintf("--data-dir=%s", filepath.Join(inst.Dir, "data")),
fmt.Sprintf("--peer-urls=http://%s", utils.JoinHostPort(inst.Host, inst.Port)),
fmt.Sprintf("--advertise-peer-urls=http://%s", utils.JoinHostPort(AdvertiseHost(inst.Host), inst.Port)),
fmt.Sprintf("--client-urls=http://%s", utils.JoinHostPort(inst.Host, inst.StatusPort)),
fmt.Sprintf("--advertise-client-urls=http://%s", utils.JoinHostPort(AdvertiseHost(inst.Host), inst.StatusPort)),
fmt.Sprintf("--log-file=%s", inst.LogFile()),
}...)
if inst.ConfigPath != "" {
args = append(args, fmt.Sprintf("--config=%s", inst.ConfigPath))
}
switch {
case len(inst.initEndpoints) > 0:
endpoints := make([]string, 0)
for _, pd := range inst.initEndpoints {
uid := fmt.Sprintf("pd-%d", pd.ID)
endpoints = append(endpoints, fmt.Sprintf("%s=http://%s", uid, utils.JoinHostPort(AdvertiseHost(inst.Host), pd.Port)))
}
args = append(args, fmt.Sprintf("--initial-cluster=%s", strings.Join(endpoints, ",")))
case len(inst.joinEndpoints) > 0:
endpoints := make([]string, 0)
for _, pd := range inst.joinEndpoints {
endpoints = append(endpoints, fmt.Sprintf("http://%s", utils.JoinHostPort(AdvertiseHost(inst.Host), pd.Port)))
}
args = append(args, fmt.Sprintf("--join=%s", strings.Join(endpoints, ",")))
default:
return errors.Errorf("must set the init or join instances")
}
case PDRoleTSO:
endpoints := pdEndpoints(inst.pds, true)
args = []string{
"services",
"tso",
fmt.Sprintf("--listen-addr=http://%s", utils.JoinHostPort(inst.Host, inst.Port)),
fmt.Sprintf("--advertise-listen-addr=http://%s", utils.JoinHostPort(AdvertiseHost(inst.Host), inst.Port)),
fmt.Sprintf("--backend-endpoints=%s", strings.Join(endpoints, ",")),
fmt.Sprintf("--log-file=%s", inst.LogFile()),
}
if inst.ConfigPath != "" {
args = append(args, fmt.Sprintf("--config=%s", inst.ConfigPath))
}
case PDRoleResourceManager:
endpoints := pdEndpoints(inst.pds, true)
args = []string{
"services",
"resource-manager",
fmt.Sprintf("--listen-addr=http://%s", utils.JoinHostPort(inst.Host, inst.Port)),
fmt.Sprintf("--advertise-listen-addr=http://%s", utils.JoinHostPort(AdvertiseHost(inst.Host), inst.Port)),
fmt.Sprintf("--backend-endpoints=%s", strings.Join(endpoints, ",")),
fmt.Sprintf("--log-file=%s", inst.LogFile()),
}
args = append(args, fmt.Sprintf("--initial-cluster=%s", strings.Join(endpoints, ",")))
case len(inst.joinEndpoints) > 0:
endpoints := make([]string, 0)
for _, pd := range inst.joinEndpoints {
endpoints = append(endpoints, fmt.Sprintf("http://%s", utils.JoinHostPort(AdvertiseHost(inst.Host), pd.Port)))
if inst.ConfigPath != "" {
args = append(args, fmt.Sprintf("--config=%s", inst.ConfigPath))
}
args = append(args, fmt.Sprintf("--join=%s", strings.Join(endpoints, ",")))
default:
return errors.Errorf("must set the init or join instances")
}

var err error
Expand All @@ -113,12 +163,12 @@ func (inst *PDInstance) Start(ctx context.Context, version utils.Version) error

// Component return the component name.
func (inst *PDInstance) Component() string {
return "pd"
return string(inst.Role)
}

// LogFile return the log file.
func (inst *PDInstance) LogFile() string {
return filepath.Join(inst.Dir, "pd.log")
return filepath.Join(inst.Dir, fmt.Sprintf("%s.log", string(inst.Role)))
}

// Addr return the listen address of PD
Expand Down
32 changes: 30 additions & 2 deletions components/playground/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,10 @@ import (
type BootOptions struct {
Mode string `yaml:"mode"`
Version string `yaml:"version"`
PD instance.Config `yaml:"pd"`
PD instance.Config `yaml:"pd"` // ignored when mode == pd-ms
API instance.Config `yaml:"api"` // Only available when mode == pd-ms
TSO instance.Config `yaml:"tso"` // Only available when mode == pd-ms
RM instance.Config `yaml:"rc"` // Only available when mode == pd-ms
TiDB instance.Config `yaml:"tidb"`
TiKV instance.Config `yaml:"tikv"`
TiFlash instance.Config `yaml:"tiflash"` // ignored when mode == tidb-disagg
Expand Down Expand Up @@ -266,7 +269,7 @@ If you'd like to use a TiDB version other than %s, cancel and retry with the fol
},
}

rootCmd.Flags().StringVar(&options.Mode, "mode", "tidb", "TiUP playground mode: 'tidb', 'tidb-disagg', 'tikv-slim'")
rootCmd.Flags().StringVar(&options.Mode, "mode", "tidb", "TiUP playground mode: 'tidb', 'tidb-disagg', 'tikv-slim', 'pd-ms'")
rootCmd.PersistentFlags().StringVarP(&tag, "tag", "T", "", "Specify a tag for playground") // Use `PersistentFlags()` to make it available to subcommands.
rootCmd.Flags().Bool("without-monitor", false, "Don't start prometheus and grafana component")
rootCmd.Flags().BoolVar(&options.Monitor, "monitor", true, "Start prometheus and grafana component")
Expand All @@ -285,6 +288,10 @@ If you'd like to use a TiDB version other than %s, cancel and retry with the fol
rootCmd.Flags().IntVar(&options.Pump.Num, "pump", 0, "Pump instance number")
rootCmd.Flags().IntVar(&options.Drainer.Num, "drainer", 0, "Drainer instance number")

rootCmd.Flags().IntVar(&options.API.Num, "api", 0, "API instance number")
rootCmd.Flags().IntVar(&options.TSO.Num, "tso", 0, "TSO instance number")
rootCmd.Flags().IntVar(&options.RM.Num, "rc", 0, "Resource manager instance number")

rootCmd.Flags().IntVar(&options.TiDB.UpTimeout, "db.timeout", 60, "TiDB max wait time in seconds for starting, 0 means no limit")
rootCmd.Flags().IntVar(&options.TiFlash.UpTimeout, "tiflash.timeout", 120, "TiFlash max wait time in seconds for starting, 0 means no limit")

Expand All @@ -309,6 +316,10 @@ If you'd like to use a TiDB version other than %s, cancel and retry with the fol
rootCmd.Flags().StringVar(&options.TiCDC.ConfigPath, "ticdc.config", "", "TiCDC instance configuration file")
rootCmd.Flags().StringVar(&options.TiKVCDC.ConfigPath, "kvcdc.config", "", "TiKV-CDC instance configuration file")

rootCmd.Flags().StringVar(&options.API.ConfigPath, "api.config", "", "API instance configuration file")
rootCmd.Flags().StringVar(&options.TSO.ConfigPath, "tso.config", "", "TSO instance configuration file")
rootCmd.Flags().StringVar(&options.RM.ConfigPath, "rc.config", "", "Resource manager instance configuration file")

rootCmd.Flags().StringVar(&options.TiDB.BinPath, "db.binpath", "", "TiDB instance binary path")
rootCmd.Flags().StringVar(&options.TiKV.BinPath, "kv.binpath", "", "TiKV instance binary path")
rootCmd.Flags().StringVar(&options.PD.BinPath, "pd.binpath", "", "PD instance binary path")
Expand All @@ -320,6 +331,10 @@ If you'd like to use a TiDB version other than %s, cancel and retry with the fol
rootCmd.Flags().StringVar(&options.Pump.BinPath, "pump.binpath", "", "Pump instance binary path")
rootCmd.Flags().StringVar(&options.Drainer.BinPath, "drainer.binpath", "", "Drainer instance binary path")

rootCmd.Flags().StringVar(&options.API.BinPath, "api.binpath", "", "API instance binary path")
rootCmd.Flags().StringVar(&options.TSO.BinPath, "tso.binpath", "", "TSO instance binary path")
rootCmd.Flags().StringVar(&options.RM.BinPath, "rc.binpath", "", "Resource manager instance binary path")

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

rootCmd.Flags().StringVar(&options.DisaggOpts.S3Endpoint, "disagg.s3_endpoint", "127.0.0.1:9000", "Object store URL for the disaggregated TiFlash, available when --mode=tidb-disagg")
Expand Down Expand Up @@ -374,6 +389,19 @@ func populateDefaultOpt(flagSet *pflag.FlagSet) error {
defaultStr(&options.TiFlashCompute.BinPath, "tiflash.compute.binpath", options.TiFlash.BinPath)
defaultStr(&options.TiFlashCompute.ConfigPath, "tiflash.compute.config", options.TiFlash.ConfigPath)
options.TiFlashCompute.UpTimeout = options.TiFlash.UpTimeout
case "pd-ms":
defaultInt(&options.TiDB.Num, "db", 1)
defaultInt(&options.TiKV.Num, "kv", 1)
defaultInt(&options.API.Num, "api", 1)
defaultStr(&options.API.BinPath, "api.binpath", options.API.BinPath)
defaultStr(&options.API.ConfigPath, "api.config", options.API.ConfigPath)
defaultInt(&options.TSO.Num, "tso", 1)
defaultStr(&options.TSO.BinPath, "tso.binpath", options.TSO.BinPath)
defaultStr(&options.TSO.ConfigPath, "tso.config", options.TSO.ConfigPath)
defaultInt(&options.RM.Num, "rc", 1)
defaultStr(&options.RM.BinPath, "rc.binpath", options.RM.BinPath)
defaultStr(&options.RM.ConfigPath, "rc.config", options.RM.ConfigPath)
defaultInt(&options.TiFlash.Num, "tiflash", 1)
default:
return errors.Errorf("Unknown --mode %s", options.Mode)
}
Expand Down
Loading

0 comments on commit b020943

Please sign in to comment.