diff --git a/client/http/api.go b/client/http/api.go index 3376a48770d..38eb792d9b4 100644 --- a/client/http/api.go +++ b/client/http/api.go @@ -79,6 +79,7 @@ const ( Status = "/pd/api/v1/status" Version = "/pd/api/v1/version" operators = "/pd/api/v1/operators" + safepoint = "pd/api/v1/gc/safepoint" // Micro Service microServicePrefix = "/pd/api/v2/ms" // Keyspace diff --git a/client/http/interface.go b/client/http/interface.go index cd9fc22702e..9f2903525ae 100644 --- a/client/http/interface.go +++ b/client/http/interface.go @@ -100,6 +100,8 @@ type Client interface { /* Other interfaces */ GetMinResolvedTSByStoresIDs(context.Context, []uint64) (uint64, map[uint64]uint64, error) GetPDVersion(context.Context) (string, error) + GetGCSafePoint(context.Context) (ListServiceGCSafepoint, error) + DeleteGCSafePoint(context.Context, string) (string, error) /* Micro Service interfaces */ GetMicroServiceMembers(context.Context, string) ([]MicroServiceMember, error) GetMicroServicePrimary(context.Context, string) (string, error) @@ -1024,3 +1026,29 @@ func (c *client) GetKeyspaceMetaByName(ctx context.Context, keyspaceName string) } return &keyspaceMetaPB, nil } + +func (c *client) GetGCSafePoint(ctx context.Context) (ListServiceGCSafepoint, error) { + var gcsafepoint ListServiceGCSafepoint + err := c.request(ctx, newRequestInfo(). + WithName(GetGCSafePointName). + WithURI(safepoint). + WithMethod(http.MethodGet). + WithResp(&gcsafepoint)) + if err != nil { + return gcsafepoint, err + } + return gcsafepoint, nil +} + +func (c *client) DeleteGCSafePoint(ctx context.Context, service_id string) (string, error) { + var msg string + err := c.request(ctx, newRequestInfo(). + WithName(DeleteGCSafePointName). + WithURI(safepoint+"/"+service_id). + WithMethod(http.MethodDelete). + WithResp(&msg)) + if err != nil { + return msg, err + } + return msg, nil +} \ No newline at end of file diff --git a/client/http/request_info.go b/client/http/request_info.go index 783220bcc60..fea9bdd332f 100644 --- a/client/http/request_info.go +++ b/client/http/request_info.go @@ -85,6 +85,8 @@ const ( deleteOperators = "DeleteOperators" UpdateKeyspaceGCManagementTypeName = "UpdateKeyspaceGCManagementType" GetKeyspaceMetaByNameName = "GetKeyspaceMetaByName" + GetGCSafePointName = "GetGCSafePoint" + DeleteGCSafePointName = "DeleteGCSafePoint" ) type requestInfo struct { diff --git a/client/http/types.go b/client/http/types.go index 55f9b65caad..bbc7a02f710 100644 --- a/client/http/types.go +++ b/client/http/types.go @@ -25,6 +25,20 @@ import ( pd "github.com/tikv/pd/client" ) +// NOTE: This type is in sync with pd/pkg/storage/endpoint/gc_safe_point.go +type ServiceSafePoint struct { + ServiceID string `json:"service_id"` + ExpiredAt int64 `json:"expired_at"` + SafePoint uint64 `json:"safe_point"` +} + +// NOTE: This type is in sync with pd/server/spi/service_gc_safepoint.go +type ListServiceGCSafepoint struct { + ServiceGCSafepoints []*ServiceSafePoint `json:"service_gc_safe_points"` + MinServiceGcSafepoint uint64 `json:"min_service_gc_safe_point,omitempty"` + GCSafePoint uint64 `json:"gc_safe_point"` +} + // ClusterState saves some cluster state information. // NOTE: This type sync with https://github.com/tikv/pd/blob/5eae459c01a797cbd0c416054c6f0cad16b8740a/server/cluster/cluster.go#L173 type ClusterState struct { diff --git a/pkg/storage/endpoint/gc_safe_point.go b/pkg/storage/endpoint/gc_safe_point.go index 85b29e0b47e..137d2abd680 100644 --- a/pkg/storage/endpoint/gc_safe_point.go +++ b/pkg/storage/endpoint/gc_safe_point.go @@ -28,6 +28,7 @@ import ( // ServiceSafePoint is the safepoint for a specific service // NOTE: This type is exported by HTTP API. Please pay more attention when modifying it. +// this type is in sync with client/http/types.go type ServiceSafePoint struct { ServiceID string `json:"service_id"` ExpiredAt int64 `json:"expired_at"` diff --git a/server/api/service_gc_safepoint.go b/server/api/service_gc_safepoint.go index d6bb153eb6f..06d7f174a5a 100644 --- a/server/api/service_gc_safepoint.go +++ b/server/api/service_gc_safepoint.go @@ -38,6 +38,7 @@ 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. +// this type is now in sync with pd/client/http/types.go ListServiceGCSafepoint type ListServiceGCSafepoint struct { ServiceGCSafepoints []*endpoint.ServiceSafePoint `json:"service_gc_safe_points"` MinServiceGcSafepoint uint64 `json:"min_service_gc_safe_point,omitempty"` diff --git a/tools/pd-ctl/pdctl/command/gc_safepoint_command.go b/tools/pd-ctl/pdctl/command/gc_safepoint_command.go index f4a6b6fcfd0..496c870d9e1 100644 --- a/tools/pd-ctl/pdctl/command/gc_safepoint_command.go +++ b/tools/pd-ctl/pdctl/command/gc_safepoint_command.go @@ -16,15 +16,9 @@ package command import ( "encoding/json" - "net/http" "sort" "github.com/spf13/cobra" - "github.com/tikv/pd/server/api" -) - -var ( - serviceGCSafepointPrefix = "pd/api/v1/gc/safepoint" ) // NewServiceGCSafepointCommand return a service gc safepoint subcommand of rootCmd @@ -50,16 +44,11 @@ func NewDeleteServiceGCSafepointCommand() *cobra.Command { } func showSSPs(cmd *cobra.Command, _ []string) { - r, err := doRequest(cmd, serviceGCSafepointPrefix, http.MethodGet, http.Header{}) + safepoint, err := PDCli.GetGCSafePoint(cmd.Context()) if err != nil { cmd.Printf("Failed to get service GC safepoint: %s\n", err) return } - 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 }) @@ -68,7 +57,7 @@ func showSSPs(cmd *cobra.Command, _ []string) { cmd.Printf("Failed to marshal service GC safepoint: %s\n", err) return } - cmd.Println(string(data)) + jsonPrint(cmd, string(data)) } func deleteSSP(cmd *cobra.Command, args []string) { @@ -76,12 +65,10 @@ func deleteSSP(cmd *cobra.Command, args []string) { cmd.Usage() return } - serviceID := args[0] - deleteURL := serviceGCSafepointPrefix + "/" + serviceID - r, err := doRequest(cmd, deleteURL, http.MethodDelete, http.Header{}) + r, err := PDCli.DeleteGCSafePoint(cmd.Context(), args[0]) if err != nil { cmd.Printf("Failed to delete service GC safepoint: %s\n", err) return } - cmd.Println(r) + jsonPrint(cmd, r) }