Skip to content

Commit

Permalink
api: improve health API (#2158)
Browse files Browse the repository at this point in the history
Signed-off-by: Ryan Leung <rleungx@gmail.com>
  • Loading branch information
rleungx authored Feb 26, 2020
1 parent 2a2b296 commit 904e833
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 12 deletions.
8 changes: 4 additions & 4 deletions server/api/health.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,17 +49,17 @@ func (h *healthHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
return
}

unhealthMembers := cluster.CheckHealth(members)
healthMembers := cluster.CheckHealth(members)
healths := []Health{}
for _, member := range members {
h := Health{
Name: member.Name,
MemberID: member.MemberId,
ClientUrls: member.ClientUrls,
Health: true,
Health: false,
}
if _, ok := unhealthMembers[member.GetMemberId()]; ok {
h.Health = false
if _, ok := healthMembers[member.GetMemberId()]; ok {
h.Health = true
}
healths = append(healths, h)
}
Expand Down
9 changes: 5 additions & 4 deletions server/cluster/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -1596,20 +1596,21 @@ var healthURL = "/pd/api/v1/ping"

// CheckHealth checks if members are healthy.
func CheckHealth(members []*pdpb.Member) map[uint64]*pdpb.Member {
unhealthMembers := make(map[uint64]*pdpb.Member)
healthMembers := make(map[uint64]*pdpb.Member)
for _, member := range members {
for _, cURL := range member.ClientUrls {
resp, err := DialClient.Get(fmt.Sprintf("%s%s", cURL, healthURL))
if resp != nil {
resp.Body.Close()
}
if err != nil || resp.StatusCode != http.StatusOK {
unhealthMembers[member.GetMemberId()] = member

if err == nil && resp.StatusCode == http.StatusOK {
healthMembers[member.GetMemberId()] = member
break
}
}
}
return unhealthMembers
return healthMembers
}

// GetMembers return a slice of Members.
Expand Down
8 changes: 4 additions & 4 deletions tests/pdctl/health/health_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,17 +55,17 @@ func (s *healthTestSuite) TestHealth(c *C) {
client := tc.GetEtcdClient()
members, err := cluster.GetMembers(client)
c.Assert(err, IsNil)
unhealthMembers := cluster.CheckHealth(members)
healthMembers := cluster.CheckHealth(members)
healths := []api.Health{}
for _, member := range members {
h := api.Health{
Name: member.Name,
MemberID: member.MemberId,
ClientUrls: member.ClientUrls,
Health: true,
Health: false,
}
if _, ok := unhealthMembers[member.GetMemberId()]; ok {
h.Health = false
if _, ok := healthMembers[member.GetMemberId()]; ok {
h.Health = true
}
healths = append(healths, h)
}
Expand Down

0 comments on commit 904e833

Please sign in to comment.