Skip to content
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: 3 additions & 5 deletions components/cluster/command/display.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
25 changes: 18 additions & 7 deletions components/dms/command/display.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
21 changes: 7 additions & 14 deletions pkg/cluster/api/pdapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
Expand All @@ -116,26 +111,24 @@ 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)
if err != nil {
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
Expand Down
19 changes: 5 additions & 14 deletions pkg/cluster/meta/topology.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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
Expand Down