From 4ecc95a04341d72cbd41508cd6ef3a0f17a63be3 Mon Sep 17 00:00:00 2001 From: 9547 Date: Mon, 22 Feb 2021 12:31:08 +0800 Subject: [PATCH] playground: set grafana topology in pd (#1142) * 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> --- components/playground/instance/drainer.go | 4 +-- components/playground/instance/instance.go | 7 ++-- components/playground/instance/pd.go | 10 +++--- components/playground/instance/pump.go | 4 +-- components/playground/instance/ticdc.go | 2 +- components/playground/instance/tidb.go | 2 +- components/playground/instance/tiflash.go | 4 +-- components/playground/instance/tikv.go | 4 +-- components/playground/playground.go | 38 +++++++++++++--------- 9 files changed, 42 insertions(+), 33 deletions(-) diff --git a/components/playground/instance/drainer.go b/components/playground/instance/drainer.go index 6989639300..fb02053854 100644 --- a/components/playground/instance/drainer.go +++ b/components/playground/instance/drainer.go @@ -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. @@ -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()), } diff --git a/components/playground/instance/instance.go b/components/playground/instance/instance.go index 7c1a533f7e..e546645ec6 100644 --- a/components/playground/instance/instance.go +++ b/components/playground/instance/instance.go @@ -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 { @@ -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 diff --git a/components/playground/instance/pd.go b/components/playground/instance/pd.go index 9128320138..b72f325b45 100644 --- a/components/playground/instance/pd.go +++ b/components/playground/instance/pd.go @@ -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 != "" { @@ -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: @@ -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) } diff --git a/components/playground/instance/pump.go b/components/playground/instance/pump.go index f05374f70d..44e7b8e196 100644 --- a/components/playground/instance/pump.go +++ b/components/playground/instance/pump.go @@ -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. @@ -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()), } diff --git a/components/playground/instance/ticdc.go b/components/playground/instance/ticdc.go index f752d5ffcb..177e54f313 100644 --- a/components/playground/instance/ticdc.go +++ b/components/playground/instance/ticdc.go @@ -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()), } diff --git a/components/playground/instance/tidb.go b/components/playground/instance/tidb.go index a754862057..0374a4f352 100644 --- a/components/playground/instance/tidb.go +++ b/components/playground/instance/tidb.go @@ -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) } diff --git a/components/playground/instance/tiflash.go b/components/playground/instance/tiflash.go index 40d168fe2a..c42c25fd88 100644 --- a/components/playground/instance/tiflash.go +++ b/components/playground/instance/tiflash.go @@ -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 { @@ -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 { diff --git a/components/playground/instance/tikv.go b/components/playground/instance/tikv.go index 339b1504db..13487c56a7 100644 --- a/components/playground/instance/tikv.go +++ b/components/playground/instance/tikv.go @@ -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), @@ -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 { diff --git a/components/playground/playground.go b/components/playground/playground.go index 6bcf22872d..63fc274cad 100644 --- a/components/playground/playground.go +++ b/components/playground/playground.go @@ -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) @@ -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 { @@ -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