Skip to content

Commit

Permalink
sort gc safepoint in pd-ctl
Browse files Browse the repository at this point in the history
Signed-off-by: bufferflies <1045931706@qq.com>
  • Loading branch information
bufferflies committed Oct 19, 2023
1 parent cb9c70c commit 179c607
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 6 deletions.
7 changes: 4 additions & 3 deletions server/api/service_gc_safepoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,16 +35,17 @@ func newServiceGCSafepointHandler(svr *server.Server, rd *render.Render) *servic
}
}

// ListServiceGCSafepoint is the response for list service GC safepoint.
// NOTE: This type is exported by HTTP API. Please pay more attention when modifying it.
type listServiceGCSafepoint struct {
type ListServiceGCSafepoint struct {
ServiceGCSafepoints []*endpoint.ServiceSafePoint `json:"service_gc_safe_points"`
GCSafePoint uint64 `json:"gc_safe_point"`
}

// @Tags service_gc_safepoint
// @Summary Get all service GC safepoint.
// @Produce json
// @Success 200 {array} listServiceGCSafepoint
// @Success 200 {array} ListServiceGCSafepoint
// @Failure 500 {string} string "PD server failed to proceed the request."
// @Router /gc/safepoint [get]
func (h *serviceGCSafepointHandler) GetGCSafePoint(w http.ResponseWriter, r *http.Request) {
Expand All @@ -59,7 +60,7 @@ func (h *serviceGCSafepointHandler) GetGCSafePoint(w http.ResponseWriter, r *htt
h.rd.JSON(w, http.StatusInternalServerError, err.Error())
return
}
list := listServiceGCSafepoint{
list := ListServiceGCSafepoint{
GCSafePoint: gcSafepoint,
ServiceGCSafepoints: ssps,
}
Expand Down
4 changes: 2 additions & 2 deletions server/api/service_gc_safepoint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func (suite *serviceGCSafepointTestSuite) TestServiceGCSafepoint() {
sspURL := suite.urlPrefix + "/gc/safepoint"

storage := suite.svr.GetStorage()
list := &listServiceGCSafepoint{
list := &ListServiceGCSafepoint{
ServiceGCSafepoints: []*endpoint.ServiceSafePoint{
{
ServiceID: "a",
Expand Down Expand Up @@ -87,7 +87,7 @@ func (suite *serviceGCSafepointTestSuite) TestServiceGCSafepoint() {
res, err := testDialClient.Get(sspURL)
suite.NoError(err)
defer res.Body.Close()
listResp := &listServiceGCSafepoint{}
listResp := &ListServiceGCSafepoint{}
err = apiutil.ReadJSON(res.Body, listResp)
suite.NoError(err)
suite.Equal(list, listResp)
Expand Down
18 changes: 17 additions & 1 deletion tools/pd-ctl/pdctl/command/gc_safepoint_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,12 @@
package command

import (
"encoding/json"
"net/http"
"sort"

"github.com/spf13/cobra"
"github.com/tikv/pd/server/api"
)

var (
Expand Down Expand Up @@ -52,7 +55,20 @@ func showSSPs(cmd *cobra.Command, args []string) {
cmd.Printf("Failed to get service GC safepoint: %s\n", err)
return
}
cmd.Println(r)
var safepoint api.ListServiceGCSafepoint
if err := json.Unmarshal([]byte(r), &safepoint); err != nil {
cmd.Printf("Failed to unmarshal service GC safepoint: %s\n", err)
return
}
sort.Slice(safepoint.ServiceGCSafepoints, func(i, j int) bool {
return safepoint.ServiceGCSafepoints[i].SafePoint < safepoint.ServiceGCSafepoints[j].SafePoint
})
data, err := json.MarshalIndent(safepoint, "", " ")
if err != nil {
cmd.Printf("Failed to marshal service GC safepoint: %s\n", err)
return
}
cmd.Println(string(data))
}

func deleteSSP(cmd *cobra.Command, args []string) {
Expand Down

0 comments on commit 179c607

Please sign in to comment.