diff --git a/components/cluster/command/display.go b/components/cluster/command/display.go index bf55a42b7b..8fc1367cd8 100644 --- a/components/cluster/command/display.go +++ b/components/cluster/command/display.go @@ -272,14 +272,12 @@ func formatInstanceStatus(status string) string { } switch { - case startsWith("healthy|l"): // healthy|l, healthy|l|ui + case startsWith("up|l"): // up|l, up|l|ui return color.HiGreenString(status) - case startsWith("healthy"): // healthy, healthy|ui - return color.GreenString(status) - case startsWith("unhealthy", "down", "err"): // unhealthy/down/err, unhealthy/down/err|ui - return color.RedString(status) case startsWith("up"): return color.GreenString(status) + case startsWith("down", "err"): // down, down|ui + return color.RedString(status) case startsWith("tombstone", "disconnected"), strings.Contains(status, "offline"): return color.YellowString(status) default: diff --git a/components/dms/command/display.go b/components/dms/command/display.go index f84d798db8..812a3c2572 100644 --- a/components/dms/command/display.go +++ b/components/dms/command/display.go @@ -246,15 +246,26 @@ func displayClusterTopology(clusterName string, opt *operator.Options) error { } func formatInstanceStatus(status string) string { - switch strings.ToLower(status) { - case "up", "healthy", "free": - return color.GreenString(status) - case "healthy|l", "bound": // master leader + lowercaseStatus := strings.ToLower(status) + + startsWith := func(prefixs ...string) bool { + for _, prefix := range prefixs { + if strings.HasPrefix(lowercaseStatus, prefix) { + return true + } + } + return false + } + + switch { + case startsWith("up|l"): // up|l, up|l|ui return color.HiGreenString(status) - case "offline", "tombstone", "disconnected": - return color.YellowString(status) - case "down", "unhealthy", "err": + case startsWith("up"): + return color.GreenString(status) + case startsWith("down", "err"): // down, down|ui return color.RedString(status) + case startsWith("tombstone", "disconnected"), strings.Contains(status, "offline"): + return color.YellowString(status) default: return status } diff --git a/pkg/cluster/api/pdapi.go b/pkg/cluster/api/pdapi.go index 8117644276..c452619e03 100644 --- a/pkg/cluster/api/pdapi.go +++ b/pkg/cluster/api/pdapi.go @@ -64,7 +64,7 @@ func (pc *PDClient) GetURL(addr string) string { // nolint (some is unused now) var ( - pdHealthURI = "pd/health" + pdPingURI = "pd/ping" pdMembersURI = "pd/api/v1/members" pdStoresURI = "pd/api/v1/stores" pdStoreURI = "pd/api/v1/store" @@ -102,11 +102,6 @@ func tryURLs(endpoints []string, f func(endpoint string) ([]byte, error)) ([]byt return bytes, err } -// PDHealthInfo is the member health info from PD's API -type PDHealthInfo struct { - Healths []pdserverapi.Health -} - func (pc *PDClient) getEndpoints(cmd string) (endpoints []string) { for _, addr := range pc.addrs { endpoint := fmt.Sprintf("%s/%s", pc.GetURL(addr), cmd) @@ -116,11 +111,9 @@ func (pc *PDClient) getEndpoints(cmd string) (endpoints []string) { return } -// GetHealth queries the health info from PD server -func (pc *PDClient) GetHealth() (*PDHealthInfo, error) { - endpoints := pc.getEndpoints(pdHealthURI) - - healths := []pdserverapi.Health{} +// CheckHealth checks the health of PD node +func (pc *PDClient) CheckHealth() error { + endpoints := pc.getEndpoints(pdPingURI) _, err := tryURLs(endpoints, func(endpoint string) ([]byte, error) { body, err := pc.httpClient.Get(endpoint) @@ -128,14 +121,14 @@ func (pc *PDClient) GetHealth() (*PDHealthInfo, error) { return body, err } - return body, json.Unmarshal(body, &healths) + return body, nil }) if err != nil { - return nil, errors.AddStack(err) + return errors.AddStack(err) } - return &PDHealthInfo{healths}, nil + return nil } // GetStores queries the stores info from PD server diff --git a/pkg/cluster/meta/topology.go b/pkg/cluster/meta/topology.go index e3e4e03304..321604b71e 100644 --- a/pkg/cluster/meta/topology.go +++ b/pkg/cluster/meta/topology.go @@ -297,7 +297,8 @@ func (s PDSpec) Status(pdList ...string) string { suffix = "|UI" } - healths, err := curPdAPI.GetHealth() + // check health + err := curPdAPI.CheckHealth() if err != nil { return "Down" + suffix } @@ -307,20 +308,10 @@ func (s PDSpec) Status(pdList ...string) string { if err != nil { return "ERR" + suffix } - - for _, member := range healths.Healths { - if s.Name != member.Name { - continue - } - if s.Name == leader.Name { - suffix = "|L" + suffix - } - if member.Health { - return "Healthy" + suffix - } - return "Unhealthy" + suffix + if s.Name == leader.Name { + suffix = "|L" + suffix } - return "N/A" + suffix + return "Up" + suffix } // Role returns the component role of the instance