Skip to content

Commit

Permalink
change gc_safepoint call to PD HTTP SDK
Browse files Browse the repository at this point in the history
Signed-off-by: Boyang Lyu <[email protected]>
  • Loading branch information
JackL9u committed Aug 7, 2024
1 parent 3c4c427 commit 4caa8d9
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 17 deletions.
1 change: 1 addition & 0 deletions client/http/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
28 changes: 28 additions & 0 deletions client/http/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
}
2 changes: 2 additions & 0 deletions client/http/request_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@ const (
deleteOperators = "DeleteOperators"
UpdateKeyspaceGCManagementTypeName = "UpdateKeyspaceGCManagementType"
GetKeyspaceMetaByNameName = "GetKeyspaceMetaByName"
GetGCSafePointName = "GetGCSafePoint"
DeleteGCSafePointName = "DeleteGCSafePoint"
)

type requestInfo struct {
Expand Down
14 changes: 14 additions & 0 deletions client/http/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
1 change: 1 addition & 0 deletions pkg/storage/endpoint/gc_safe_point.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down
1 change: 1 addition & 0 deletions server/api/service_gc_safepoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down
21 changes: 4 additions & 17 deletions tools/pd-ctl/pdctl/command/gc_safepoint_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
})
Expand All @@ -68,20 +57,18 @@ 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) {
if len(args) != 1 {
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)
}

0 comments on commit 4caa8d9

Please sign in to comment.