Skip to content

Commit

Permalink
command: replace doRequest of HTTP client to get health status
Browse files Browse the repository at this point in the history
Signed-off-by: okJiang <[email protected]>
  • Loading branch information
okJiang committed May 23, 2024
1 parent 0056569 commit 48422ea
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 10 deletions.
1 change: 1 addition & 0 deletions client/http/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ const (
membersPrefix = "/pd/api/v1/members"
leaderPrefix = "/pd/api/v1/leader"
transferLeader = "/pd/api/v1/leader/transfer"
health = "/pd/api/v1/health"
// Config
Config = "/pd/api/v1/config"
ClusterVersion = "/pd/api/v1/config/cluster-version"
Expand Down
14 changes: 14 additions & 0 deletions client/http/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ type Client interface {
GetStores(context.Context) (*StoresInfo, error)
GetStore(context.Context, uint64) (*StoreInfo, error)
SetStoreLabels(context.Context, int64, map[string]string) error
GetHealthStatus(context.Context) ([]Health, error)
/* Config-related interfaces */
GetConfig(context.Context) (map[string]any, error)
SetConfig(context.Context, map[string]any, ...float64) error
Expand Down Expand Up @@ -337,6 +338,19 @@ func (c *client) SetStoreLabels(ctx context.Context, storeID int64, storeLabels
WithBody(jsonInput))
}

func (c *client) GetHealthStatus(ctx context.Context) ([]Health, error) {
var healths []Health
err := c.request(ctx, newRequestInfo().
WithName(getHealthName).
WithURI(health).
WithMethod(http.MethodGet).
WithResp(&healths))
if err != nil {
return nil, err
}
return healths, nil
}

// GetConfig gets the configurations.
func (c *client) GetConfig(ctx context.Context) (map[string]any, error) {
var config map[string]any
Expand Down
1 change: 1 addition & 0 deletions client/http/request_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ const (
getStoresName = "GetStores"
getStoreName = "GetStore"
setStoreLabelsName = "SetStoreLabels"
getHealthName = "GetHealth"
getConfigName = "GetConfig"
setConfigName = "SetConfig"
getScheduleConfigName = "GetScheduleConfig"
Expand Down
9 changes: 9 additions & 0 deletions client/http/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -661,3 +661,12 @@ func stringToKeyspaceState(str string) (keyspacepb.KeyspaceState, error) {
return keyspacepb.KeyspaceState(0), fmt.Errorf("invalid KeyspaceState string: %s", str)
}
}

// Health reflects the cluster's health.
// NOTE: This type is moved from `server/api/health.go`, maybe move them to the same place later.
type Health struct {
Name string `json:"name"`
MemberID uint64 `json:"member_id"`
ClientUrls []string `json:"client_urls"`
Health bool `json:"health"`
}
17 changes: 7 additions & 10 deletions tools/pd-ctl/pdctl/command/health_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,30 +15,27 @@
package command

import (
"net/http"
"context"

"github.com/spf13/cobra"
)

var (
healthPrefix = "pd/api/v1/health"
)

// NewHealthCommand return a health subcommand of rootCmd
func NewHealthCommand() *cobra.Command {
m := &cobra.Command{
Use: "health",
Short: "show all node's health information of the pd cluster",
Run: showHealthCommandFunc,
Use: "health",
Short: "show all node's health information of the pd cluster",
PersistentPreRunE: requirePDClient,
Run: showHealthCommandFunc,
}
return m
}

func showHealthCommandFunc(cmd *cobra.Command, _ []string) {
r, err := doRequest(cmd, healthPrefix, http.MethodGet, http.Header{})
health, err := PDCli.GetHealthStatus(context.Background())
if err != nil {
cmd.Println(err)
return
}
cmd.Println(r)
jsonPrint(cmd, health)
}

0 comments on commit 48422ea

Please sign in to comment.