Skip to content

Commit

Permalink
playground: set grafana topology in pd (pingcap#1142)
Browse files Browse the repository at this point in the history
* refact(playground): expose func advertiseHost

* feat(playground): set grafana topo into pd

Co-authored-by: Ti Chi Robot <71242396+ti-chi-bot@users.noreply.github.com>
  • Loading branch information
2 people authored and lucklove committed Mar 4, 2021
1 parent a2b2607 commit 4ecc95a
Show file tree
Hide file tree
Showing 9 changed files with 42 additions and 33 deletions.
4 changes: 2 additions & 2 deletions components/playground/instance/drainer.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func (d *Drainer) LogFile() string {

// Addr return the address of Drainer.
func (d *Drainer) Addr() string {
return fmt.Sprintf("%s:%d", advertiseHost(d.Host), d.Port)
return fmt.Sprintf("%s:%d", AdvertiseHost(d.Host), d.Port)
}

// NodeID return the node id of drainer.
Expand All @@ -76,7 +76,7 @@ func (d *Drainer) Start(ctx context.Context, version pkgver.Version) error {
args := []string{
fmt.Sprintf("--node-id=%s", d.NodeID()),
fmt.Sprintf("--addr=%s:%d", d.Host, d.Port),
fmt.Sprintf("--advertise-addr=%s:%d", advertiseHost(d.Host), d.Port),
fmt.Sprintf("--advertise-addr=%s:%d", AdvertiseHost(d.Host), d.Port),
fmt.Sprintf("--pd-urls=%s", strings.Join(endpoints, ",")),
fmt.Sprintf("--log-file=%s", d.LogFile()),
}
Expand Down
7 changes: 4 additions & 3 deletions components/playground/instance/instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,8 @@ func CompVersion(comp string, version pkgver.Version) string {
return fmt.Sprintf("%v:%v", comp, version)
}

func advertiseHost(listen string) string {
// AdvertiseHost returns the interface's ip addr if listen host is 0.0.0.0
func AdvertiseHost(listen string) string {
if listen == "0.0.0.0" {
addrs, err := net.InterfaceAddrs()
if err != nil || len(addrs) == 0 {
Expand Down Expand Up @@ -102,9 +103,9 @@ func pdEndpoints(pds []*PDInstance, isHTTP bool) []string {
var endpoints []string
for _, pd := range pds {
if isHTTP {
endpoints = append(endpoints, fmt.Sprintf("http://%s:%d", advertiseHost(pd.Host), pd.StatusPort))
endpoints = append(endpoints, fmt.Sprintf("http://%s:%d", AdvertiseHost(pd.Host), pd.StatusPort))
} else {
endpoints = append(endpoints, fmt.Sprintf("%s:%d", advertiseHost(pd.Host), pd.StatusPort))
endpoints = append(endpoints, fmt.Sprintf("%s:%d", AdvertiseHost(pd.Host), pd.StatusPort))
}
}
return endpoints
Expand Down
10 changes: 5 additions & 5 deletions components/playground/instance/pd.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,9 @@ func (inst *PDInstance) Start(ctx context.Context, version pkgver.Version) error
"--name=" + uid,
fmt.Sprintf("--data-dir=%s", filepath.Join(inst.Dir, "data")),
fmt.Sprintf("--peer-urls=http://%s:%d", inst.Host, inst.Port),
fmt.Sprintf("--advertise-peer-urls=http://%s:%d", advertiseHost(inst.Host), inst.Port),
fmt.Sprintf("--advertise-peer-urls=http://%s:%d", AdvertiseHost(inst.Host), inst.Port),
fmt.Sprintf("--client-urls=http://%s:%d", inst.Host, inst.StatusPort),
fmt.Sprintf("--advertise-client-urls=http://%s:%d", advertiseHost(inst.Host), inst.StatusPort),
fmt.Sprintf("--advertise-client-urls=http://%s:%d", AdvertiseHost(inst.Host), inst.StatusPort),
fmt.Sprintf("--log-file=%s", inst.LogFile()),
}
if inst.ConfigPath != "" {
Expand All @@ -85,13 +85,13 @@ func (inst *PDInstance) Start(ctx context.Context, version pkgver.Version) error
endpoints := make([]string, 0)
for _, pd := range inst.initEndpoints {
uid := fmt.Sprintf("pd-%d", pd.ID)
endpoints = append(endpoints, fmt.Sprintf("%s=http://%s:%d", uid, advertiseHost(inst.Host), pd.Port))
endpoints = append(endpoints, fmt.Sprintf("%s=http://%s:%d", uid, 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:%d", advertiseHost(inst.Host), pd.Port))
endpoints = append(endpoints, fmt.Sprintf("http://%s:%d", AdvertiseHost(inst.Host), pd.Port))
}
args = append(args, fmt.Sprintf("--join=%s", strings.Join(endpoints, ",")))
default:
Expand Down Expand Up @@ -119,5 +119,5 @@ func (inst *PDInstance) LogFile() string {

// Addr return the listen address of PD
func (inst *PDInstance) Addr() string {
return fmt.Sprintf("%s:%d", advertiseHost(inst.Host), inst.StatusPort)
return fmt.Sprintf("%s:%d", AdvertiseHost(inst.Host), inst.StatusPort)
}
4 changes: 2 additions & 2 deletions components/playground/instance/pump.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ func (p *Pump) Ready(ctx context.Context) error {

// Addr return the address of Pump.
func (p *Pump) Addr() string {
return fmt.Sprintf("%s:%d", advertiseHost(p.Host), p.Port)
return fmt.Sprintf("%s:%d", AdvertiseHost(p.Host), p.Port)
}

// Start implements Instance interface.
Expand All @@ -95,7 +95,7 @@ func (p *Pump) Start(ctx context.Context, version pkgver.Version) error {
args := []string{
fmt.Sprintf("--node-id=%s", p.NodeID()),
fmt.Sprintf("--addr=%s:%d", p.Host, p.Port),
fmt.Sprintf("--advertise-addr=%s:%d", advertiseHost(p.Host), p.Port),
fmt.Sprintf("--advertise-addr=%s:%d", AdvertiseHost(p.Host), p.Port),
fmt.Sprintf("--pd-urls=%s", strings.Join(endpoints, ",")),
fmt.Sprintf("--log-file=%s", p.LogFile()),
}
Expand Down
2 changes: 1 addition & 1 deletion components/playground/instance/ticdc.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func (c *TiCDC) Start(ctx context.Context, version pkgver.Version) error {
args := []string{
"server",
fmt.Sprintf("--addr=%s:%d", c.Host, c.Port),
fmt.Sprintf("--advertise-addr=%s:%d", advertiseHost(c.Host), c.Port),
fmt.Sprintf("--advertise-addr=%s:%d", AdvertiseHost(c.Host), c.Port),
fmt.Sprintf("--pd=%s", strings.Join(endpoints, ",")),
fmt.Sprintf("--log-file=%s", c.LogFile()),
}
Expand Down
2 changes: 1 addition & 1 deletion components/playground/instance/tidb.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,5 +89,5 @@ func (inst *TiDBInstance) LogFile() string {

// Addr return the listen address of TiDB
func (inst *TiDBInstance) Addr() string {
return fmt.Sprintf("%s:%d", advertiseHost(inst.Host), inst.Port)
return fmt.Sprintf("%s:%d", AdvertiseHost(inst.Host), inst.Port)
}
4 changes: 2 additions & 2 deletions components/playground/instance/tiflash.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ func (inst *TiFlashInstance) Start(ctx context.Context, version pkgver.Version)

tidbStatusAddrs := make([]string, 0, len(inst.dbs))
for _, db := range inst.dbs {
tidbStatusAddrs = append(tidbStatusAddrs, fmt.Sprintf("%s:%d", advertiseHost(db.Host), uint64(db.StatusPort)))
tidbStatusAddrs = append(tidbStatusAddrs, fmt.Sprintf("%s:%d", AdvertiseHost(db.Host), uint64(db.StatusPort)))
}
wd, err := filepath.Abs(inst.Dir)
if err != nil {
Expand Down Expand Up @@ -204,7 +204,7 @@ func (inst *TiFlashInstance) Cmd() *exec.Cmd {

// StoreAddr return the store address of TiFlash
func (inst *TiFlashInstance) StoreAddr() string {
return fmt.Sprintf("%s:%d", advertiseHost(inst.Host), inst.ServicePort)
return fmt.Sprintf("%s:%d", AdvertiseHost(inst.Host), inst.ServicePort)
}

func (inst *TiFlashInstance) checkConfig(deployDir, clusterManagerPath string, version pkgver.Version, tidbStatusAddrs, endpoints []string) error {
Expand Down
4 changes: 2 additions & 2 deletions components/playground/instance/tikv.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func (inst *TiKVInstance) Start(ctx context.Context, version pkgver.Version) err
endpoints := pdEndpoints(inst.pds, true)
args := []string{
fmt.Sprintf("--addr=%s:%d", inst.Host, inst.Port),
fmt.Sprintf("--advertise-addr=%s:%d", advertiseHost(inst.Host), inst.Port),
fmt.Sprintf("--advertise-addr=%s:%d", AdvertiseHost(inst.Host), inst.Port),
fmt.Sprintf("--status-addr=%s:%d", inst.Host, inst.StatusPort),
fmt.Sprintf("--pd=%s", strings.Join(endpoints, ",")),
fmt.Sprintf("--config=%s", inst.ConfigPath),
Expand Down Expand Up @@ -92,7 +92,7 @@ func (inst *TiKVInstance) LogFile() string {

// StoreAddr return the store address of TiKV
func (inst *TiKVInstance) StoreAddr() string {
return fmt.Sprintf("%s:%d", advertiseHost(inst.Host), inst.Port)
return fmt.Sprintf("%s:%d", AdvertiseHost(inst.Host), inst.Port)
}

func (inst *TiKVInstance) checkConfig() error {
Expand Down
38 changes: 23 additions & 15 deletions components/playground/playground.go
Original file line number Diff line number Diff line change
Expand Up @@ -880,18 +880,8 @@ func (p *Playground) bootCluster(ctx context.Context, env *environment.Environme
fmt.Println(color.GreenString("To view the dashboard: http://%s/dashboard", pdAddr))
}

if monitorInfo != nil && len(p.pds) != 0 {
client, err := newEtcdClient(p.pds[0].Addr())
if err == nil && client != nil {
promBinary, err := json.Marshal(monitorInfo)
if err == nil {
_, err = client.Put(context.TODO(), "/topology/prometheus", string(promBinary))
if err != nil {
fmt.Println("Set the PD metrics storage failed")
}
fmt.Print(color.GreenString("To view the Prometheus: http://%s:%d\n", monitorInfo.IP, monitorInfo.Port))
}
}
if monitorInfo != nil {
p.updateMonitorTopology("prometheus", *monitorInfo)
}

dumpDSN(filepath.Join(p.dataDir, "dsn"), p.tidbs)
Expand All @@ -906,13 +896,31 @@ func (p *Playground) bootCluster(ctx context.Context, env *environment.Environme

logIfErr(p.renderSDFile())

if p.grafana != nil {
fmt.Print(color.GreenString("To view the Grafana: http://%s:%d\n", p.grafana.host, p.grafana.port))
if g := p.grafana; g != nil {
p.updateMonitorTopology("grafana", MonitorInfo{g.host, g.port, g.cmd.Path})
}

return nil
}

func (p *Playground) updateMonitorTopology(componentID string, info MonitorInfo) {
info.IP = instance.AdvertiseHost(info.IP)
fmt.Print(color.GreenString("To view the %s: http://%s:%d\n", strings.Title(componentID), info.IP, info.Port))
if len(p.pds) == 0 {
return
}

client, err := newEtcdClient(p.pds[0].Addr())
if err == nil && client != nil {
if promBinary, err := json.Marshal(info); err == nil {
_, err = client.Put(context.TODO(), "/topology/"+componentID, string(promBinary))
if err != nil {
fmt.Println("Set the PD metrics storage failed")
}
}
}
}

// Wait all instance quit and return the first non-nil err.
// including p8s & grafana
func (p *Playground) wait() error {
Expand Down Expand Up @@ -995,7 +1003,7 @@ func (p *Playground) bootMonitor(ctx context.Context, env *environment.Environme
return nil, nil, err
}

monitorInfo.IP = options.host
monitorInfo.IP = instance.AdvertiseHost(options.host)
monitorInfo.BinaryPath = promDir
monitorInfo.Port = monitor.port

Expand Down

0 comments on commit 4ecc95a

Please sign in to comment.