diff --git a/server/api/service_gc_safepoint.go b/server/api/service_gc_safepoint.go index b26edaba07d..270edca58bf 100644 --- a/server/api/service_gc_safepoint.go +++ b/server/api/service_gc_safepoint.go @@ -35,8 +35,9 @@ 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"` } @@ -44,7 +45,7 @@ type listServiceGCSafepoint struct { // @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) { @@ -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, } diff --git a/server/api/service_gc_safepoint_test.go b/server/api/service_gc_safepoint_test.go index 517a94c2e23..3df9102d116 100644 --- a/server/api/service_gc_safepoint_test.go +++ b/server/api/service_gc_safepoint_test.go @@ -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", @@ -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) diff --git a/tools/pd-ctl/pdctl/command/gc_safepoint_command.go b/tools/pd-ctl/pdctl/command/gc_safepoint_command.go index 619cf5a928b..80c6328e955 100644 --- a/tools/pd-ctl/pdctl/command/gc_safepoint_command.go +++ b/tools/pd-ctl/pdctl/command/gc_safepoint_command.go @@ -15,9 +15,12 @@ package command import ( + "encoding/json" "net/http" + "sort" "github.com/spf13/cobra" + "github.com/tikv/pd/server/api" ) var ( @@ -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) {