Skip to content

Commit

Permalink
client: add status http interface (#7903)
Browse files Browse the repository at this point in the history
ref #7300

Signed-off-by: Ryan Leung <rleungx@gmail.com>

Co-authored-by: ti-chi-bot[bot] <108142056+ti-chi-bot[bot]@users.noreply.github.com>
  • Loading branch information
rleungx and ti-chi-bot[bot] committed Mar 12, 2024
1 parent aaf9c95 commit efffcc5
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 0 deletions.
15 changes: 15 additions & 0 deletions client/http/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ type Client interface {
GetClusterVersion(context.Context) (string, error)
GetCluster(context.Context) (*metapb.Cluster, error)
GetClusterStatus(context.Context) (*ClusterState, error)
GetStatus(context.Context) (*State, error)
GetReplicateConfig(context.Context) (map[string]any, error)
/* Scheduler-related interfaces */
GetSchedulers(context.Context) ([]string, error)
Expand Down Expand Up @@ -459,6 +460,20 @@ func (c *client) GetClusterStatus(ctx context.Context) (*ClusterState, error) {
return clusterStatus, nil
}

// GetStatus gets the status of PD.
func (c *client) GetStatus(ctx context.Context) (*State, error) {
var status *State
err := c.request(ctx, newRequestInfo().
WithName(getStatusName).
WithURI(Status).
WithMethod(http.MethodGet).
WithResp(&status))
if err != nil {
return nil, err
}
return status, nil
}

// GetReplicateConfig gets the replication configurations.
func (c *client) GetReplicateConfig(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 @@ -46,6 +46,7 @@ const (
getClusterVersionName = "GetClusterVersion"
getClusterName = "GetCluster"
getClusterStatusName = "GetClusterStatus"
getStatusName = "GetStatus"
getReplicateConfigName = "GetReplicateConfig"
getSchedulersName = "GetSchedulers"
createSchedulerName = "CreateScheduler"
Expand Down
9 changes: 9 additions & 0 deletions client/http/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,15 @@ type ClusterState struct {
ReplicationStatus string `json:"replication_status"`
}

// State is the status of PD server.
// NOTE: This type sync with https://github.com/tikv/pd/blob/1d77b25656bc18e1f5aa82337d4ab62a34b10087/pkg/versioninfo/versioninfo.go#L29
type State struct {
BuildTS string `json:"build_ts"`
Version string `json:"version"`
GitHash string `json:"git_hash"`
StartTimestamp int64 `json:"start_timestamp"`
}

// KeyRange defines a range of keys in bytes.
type KeyRange struct {
startKey []byte
Expand Down
16 changes: 16 additions & 0 deletions tests/integrations/client/http_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -651,6 +651,22 @@ func (suite *httpClientTestSuite) checkVersion(mode mode, client pd.Client) {
re.Equal(versioninfo.PDReleaseVersion, ver)
}

func (suite *httpClientTestSuite) TestStatus() {
suite.RunTestInTwoModes(suite.checkStatus)
}

func (suite *httpClientTestSuite) checkStatus(mode mode, client pd.Client) {
re := suite.Require()
env := suite.env[mode]

status, err := client.GetStatus(env.ctx)
re.NoError(err)
re.Equal(versioninfo.PDReleaseVersion, status.Version)
re.Equal(versioninfo.PDGitHash, status.GitHash)
re.Equal(versioninfo.PDBuildTS, status.BuildTS)
re.GreaterOrEqual(time.Now().Unix(), status.StartTimestamp)
}

func (suite *httpClientTestSuite) TestAdmin() {
suite.RunTestInTwoModes(suite.checkAdmin)
}
Expand Down

0 comments on commit efffcc5

Please sign in to comment.