From 3d65bc81b15de29c79639c5f4e9d2b56dc8a0876 Mon Sep 17 00:00:00 2001 From: Boyang Lyu Date: Wed, 7 Aug 2024 19:53:52 +0800 Subject: [PATCH 01/35] gc_safepoint HTTP SDK Signed-off-by: Boyang Lyu --- client/http/api.go | 1 + client/http/interface.go | 30 +++++++++++++++++++ client/http/request_info.go | 2 ++ client/http/types.go | 14 +++++++++ pkg/storage/endpoint/gc_safe_point.go | 1 + server/api/service_gc_safepoint.go | 1 + .../pdctl/command/gc_safepoint_command.go | 21 +++---------- 7 files changed, 53 insertions(+), 17 deletions(-) diff --git a/client/http/api.go b/client/http/api.go index 3376a48770d..3f20005879f 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..e947c59aeec 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,31 @@ func (c *client) GetKeyspaceMetaByName(ctx context.Context, keyspaceName string) } return &keyspaceMetaPB, nil } + +// GetGCSafePoint get the gc_safepoint +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 +} + +// DeleteGCSafePoint deletes a gc_safepoint +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..94f71c6186e 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) } From c307b893d10c8adf39b768506f0475e8127f610d Mon Sep 17 00:00:00 2001 From: Boyang Lyu Date: Wed, 7 Aug 2024 19:56:46 +0800 Subject: [PATCH 02/35] fix an indentation mismatch Signed-off-by: Boyang Lyu --- client/http/types.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/client/http/types.go b/client/http/types.go index bbc7a02f710..9014b2fb9a2 100644 --- a/client/http/types.go +++ b/client/http/types.go @@ -34,7 +34,7 @@ type ServiceSafePoint struct { // NOTE: This type is in sync with pd/server/spi/service_gc_safepoint.go type ListServiceGCSafepoint struct { - ServiceGCSafepoints []*ServiceSafePoint `json:"service_gc_safe_points"` + ServiceGCSafepoints []*ServiceSafePoint `json:"service_gc_safe_points"` MinServiceGcSafepoint uint64 `json:"min_service_gc_safe_point,omitempty"` GCSafePoint uint64 `json:"gc_safe_point"` } From 637c50c1a4bf7fb625d5f3caeef55a8ab7d83b8a Mon Sep 17 00:00:00 2001 From: Boyang Lyu Date: Thu, 8 Aug 2024 14:27:00 +0800 Subject: [PATCH 03/35] fix some coding style issues Signed-off-by: Boyang Lyu --- client/http/api.go | 2 +- client/http/interface.go | 16 ++++++++-------- .../pd-ctl/pdctl/command/gc_safepoint_command.go | 9 +-------- 3 files changed, 10 insertions(+), 17 deletions(-) diff --git a/client/http/api.go b/client/http/api.go index 3f20005879f..90fa128defd 100644 --- a/client/http/api.go +++ b/client/http/api.go @@ -79,7 +79,7 @@ const ( Status = "/pd/api/v1/status" Version = "/pd/api/v1/version" operators = "/pd/api/v1/operators" - safepoint = "pd/api/v1/gc/safepoint" + 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 e947c59aeec..0d223fc9ccd 100644 --- a/client/http/interface.go +++ b/client/http/interface.go @@ -1029,28 +1029,28 @@ func (c *client) GetKeyspaceMetaByName(ctx context.Context, keyspaceName string) // GetGCSafePoint get the gc_safepoint func (c *client) GetGCSafePoint(ctx context.Context) (ListServiceGCSafepoint, error) { - var gcsafepoint ListServiceGCSafepoint + var gcSafePoint ListServiceGCSafepoint err := c.request(ctx, newRequestInfo(). WithName(GetGCSafePointName). WithURI(safepoint). WithMethod(http.MethodGet). - WithResp(&gcsafepoint)) + WithResp(&gcSafePoint)) if err != nil { - return gcsafepoint, err + return gcSafePoint, err } - return gcsafepoint, nil + return gcSafePoint, nil } -// DeleteGCSafePoint deletes a gc_safepoint -func (c *client) DeleteGCSafePoint(ctx context.Context, service_id string) (string, error) { +// DeleteGCSafePoint deletes a gc safepoint +func (c *client) DeleteGCSafePoint(ctx context.Context, serviceId string) (string, error) { var msg string err := c.request(ctx, newRequestInfo(). WithName(DeleteGCSafePointName). - WithURI(safepoint+"/"+service_id). + WithURI(safepoint+"/"+serviceId). WithMethod(http.MethodDelete). WithResp(&msg)) if err != nil { return msg, err } return msg, nil -} \ No newline at end of file +} diff --git a/tools/pd-ctl/pdctl/command/gc_safepoint_command.go b/tools/pd-ctl/pdctl/command/gc_safepoint_command.go index 496c870d9e1..e50b356993b 100644 --- a/tools/pd-ctl/pdctl/command/gc_safepoint_command.go +++ b/tools/pd-ctl/pdctl/command/gc_safepoint_command.go @@ -15,9 +15,7 @@ package command import ( - "encoding/json" "sort" - "github.com/spf13/cobra" ) @@ -52,12 +50,7 @@ func showSSPs(cmd *cobra.Command, _ []string) { 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 - } - jsonPrint(cmd, string(data)) + jsonPrint(cmd, safepoint) } func deleteSSP(cmd *cobra.Command, args []string) { From 002862ce096ec4b9d039e876c402c434a01cf97f Mon Sep 17 00:00:00 2001 From: Boyang Lyu Date: Thu, 8 Aug 2024 14:30:52 +0800 Subject: [PATCH 04/35] format the code Signed-off-by: Boyang Lyu --- client/http/interface.go | 16 ++++++++-------- client/http/types.go | 8 ++++---- .../pd-ctl/pdctl/command/gc_safepoint_command.go | 2 +- 3 files changed, 13 insertions(+), 13 deletions(-) diff --git a/client/http/interface.go b/client/http/interface.go index 0d223fc9ccd..7fb0f981aaa 100644 --- a/client/http/interface.go +++ b/client/http/interface.go @@ -1031,10 +1031,10 @@ func (c *client) GetKeyspaceMetaByName(ctx context.Context, keyspaceName string) 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)) + WithName(GetGCSafePointName). + WithURI(safepoint). + WithMethod(http.MethodGet). + WithResp(&gcSafePoint)) if err != nil { return gcSafePoint, err } @@ -1045,10 +1045,10 @@ func (c *client) GetGCSafePoint(ctx context.Context) (ListServiceGCSafepoint, er func (c *client) DeleteGCSafePoint(ctx context.Context, serviceId string) (string, error) { var msg string err := c.request(ctx, newRequestInfo(). - WithName(DeleteGCSafePointName). - WithURI(safepoint+"/"+serviceId). - WithMethod(http.MethodDelete). - WithResp(&msg)) + WithName(DeleteGCSafePointName). + WithURI(safepoint+"/"+serviceId). + WithMethod(http.MethodDelete). + WithResp(&msg)) if err != nil { return msg, err } diff --git a/client/http/types.go b/client/http/types.go index 9014b2fb9a2..1b04603e8b5 100644 --- a/client/http/types.go +++ b/client/http/types.go @@ -32,11 +32,11 @@ type ServiceSafePoint struct { SafePoint uint64 `json:"safe_point"` } -// NOTE: This type is in sync with pd/server/spi/service_gc_safepoint.go +// 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"` + 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. diff --git a/tools/pd-ctl/pdctl/command/gc_safepoint_command.go b/tools/pd-ctl/pdctl/command/gc_safepoint_command.go index e50b356993b..8cbce189693 100644 --- a/tools/pd-ctl/pdctl/command/gc_safepoint_command.go +++ b/tools/pd-ctl/pdctl/command/gc_safepoint_command.go @@ -15,8 +15,8 @@ package command import ( - "sort" "github.com/spf13/cobra" + "sort" ) // NewServiceGCSafepointCommand return a service gc safepoint subcommand of rootCmd From 419b903a25eb605254d48b9ac3dc285fff0648f3 Mon Sep 17 00:00:00 2001 From: Boyang Lyu Date: Thu, 8 Aug 2024 14:38:48 +0800 Subject: [PATCH 05/35] fix a var-name issue Signed-off-by: Boyang Lyu --- client/http/interface.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/client/http/interface.go b/client/http/interface.go index 7fb0f981aaa..ab5064770ac 100644 --- a/client/http/interface.go +++ b/client/http/interface.go @@ -1042,11 +1042,11 @@ func (c *client) GetGCSafePoint(ctx context.Context) (ListServiceGCSafepoint, er } // DeleteGCSafePoint deletes a gc safepoint -func (c *client) DeleteGCSafePoint(ctx context.Context, serviceId string) (string, error) { +func (c *client) DeleteGCSafePoint(ctx context.Context, serviceID string) (string, error) { var msg string err := c.request(ctx, newRequestInfo(). WithName(DeleteGCSafePointName). - WithURI(safepoint+"/"+serviceId). + WithURI(safepoint+"/"+serviceID). WithMethod(http.MethodDelete). WithResp(&msg)) if err != nil { From 6ac2bc293d8dabf61c5e6d5ef479a6232df1ee1c Mon Sep 17 00:00:00 2001 From: Boyang Lyu Date: Thu, 8 Aug 2024 15:17:00 +0800 Subject: [PATCH 06/35] add test functions Signed-off-by: Boyang Lyu --- tests/integrations/client/http_client_test.go | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/tests/integrations/client/http_client_test.go b/tests/integrations/client/http_client_test.go index fd8b65f01ba..ec41fcdafd6 100644 --- a/tests/integrations/client/http_client_test.go +++ b/tests/integrations/client/http_client_test.go @@ -835,3 +835,11 @@ func (suite *httpClientTestSuite) TestRetryOnLeaderChange() { cancel() wg.Wait() } + +func (suite *httpClientTestSuite) TestGetSafePoint() { + +} + +func (suite *httpClientTestSuite) TestDeleteSafePoint() { + +} From 8a8a40f38b6858da55238f633619564f1d295826 Mon Sep 17 00:00:00 2001 From: Boyang Lyu Date: Thu, 8 Aug 2024 17:36:15 +0800 Subject: [PATCH 07/35] add test functions for gc safepoint call Signed-off-by: Boyang Lyu --- tests/integrations/client/http_client_test.go | 70 +++++++++++++++++++ 1 file changed, 70 insertions(+) diff --git a/tests/integrations/client/http_client_test.go b/tests/integrations/client/http_client_test.go index ec41fcdafd6..3aea6fce1c5 100644 --- a/tests/integrations/client/http_client_test.go +++ b/tests/integrations/client/http_client_test.go @@ -37,9 +37,11 @@ import ( sc "github.com/tikv/pd/pkg/schedule/config" "github.com/tikv/pd/pkg/schedule/labeler" "github.com/tikv/pd/pkg/schedule/placement" + "github.com/tikv/pd/pkg/storage/endpoint" "github.com/tikv/pd/pkg/utils/testutil" "github.com/tikv/pd/pkg/utils/tsoutil" "github.com/tikv/pd/pkg/versioninfo" + "github.com/tikv/pd/server/api" "github.com/tikv/pd/tests" ) @@ -111,6 +113,36 @@ func (suite *httpClientTestSuite) SetupSuite() { suite.endpoints = endpoints suite.cluster = cluster + list := &api.ListServiceGCSafepoint{ + ServiceGCSafepoints: []*endpoint.ServiceSafePoint{ + { + ServiceID: "AAA", + ExpiredAt: time.Now().Unix() + 10, + SafePoint: 10, + }, + { + ServiceID: "BBB", + ExpiredAt: time.Now().Unix() + 10, + SafePoint: 20, + }, + { + ServiceID: "CCC", + ExpiredAt: time.Now().Unix() + 10, + SafePoint: 30, + }, + }, + GCSafePoint: 1, + MinServiceGcSafepoint: 1, + } + for _, s := range testServers { + storage := s.GetServer().GetStorage() + for _, ssp := range list.ServiceGCSafepoints { + err := storage.SaveServiceGCSafePoint(ssp) + re.NoError(err) + } + storage.SaveGCSafePoint(1) + } + if suite.withServiceDiscovery { // Run test with specific service discovery. cli := setupCli(suite.ctx, re, suite.endpoints) @@ -837,9 +869,47 @@ func (suite *httpClientTestSuite) TestRetryOnLeaderChange() { } func (suite *httpClientTestSuite) TestGetSafePoint() { + re := suite.Require() + l, err := suite.client.GetGCSafePoint(suite.ctx) + re.NoError(err) + + re.Equal(l.GCSafePoint, uint64(1)) + re.Equal(l.MinServiceGcSafepoint, uint64(10)) + re.Equal(len(l.ServiceGCSafepoints), 3) + for i, val := range l.ServiceGCSafepoints { + if i == 0 { + re.Equal(val.ServiceID, "AAA") + re.Equal(val.SafePoint, uint64(10)) + } + + if i == 1 { + re.Equal(val.ServiceID, "BBB") + re.Equal(val.SafePoint, uint64(20)) + } + + if i == 2 { + re.Equal(val.ServiceID, "CCC") + re.Equal(val.SafePoint, uint64(30)) + + } + } } func (suite *httpClientTestSuite) TestDeleteSafePoint() { + re := suite.Require() + msg1, err1 := suite.client.DeleteGCSafePoint(suite.ctx, "AAA") + re.NoError(err1) + re.Equal(msg1, "Delete service GC safepoint successfully.") + + msg2, err2 := suite.client.DeleteGCSafePoint(suite.ctx, "BBB") + re.NoError(err2) + re.Equal(msg2, "Delete service GC safepoint successfully.") + + msg3, err3 := suite.client.DeleteGCSafePoint(suite.ctx, "DDD") + re.NoError(err3) + re.Equal(msg3, "Delete service GC safepoint successfully.") + _, err4 := suite.client.DeleteGCSafePoint(suite.ctx, "gc_worker") + re.True(err4 != nil) } From cc78799670892031ea2e15d171268a00e231e902 Mon Sep 17 00:00:00 2001 From: Boyang Lyu Date: Thu, 8 Aug 2024 18:54:26 +0800 Subject: [PATCH 08/35] fix coding style for testing suite Signed-off-by: Boyang Lyu --- tests/integrations/client/http_client_test.go | 25 +++++++++---------- 1 file changed, 12 insertions(+), 13 deletions(-) diff --git a/tests/integrations/client/http_client_test.go b/tests/integrations/client/http_client_test.go index 3aea6fce1c5..1a3c375e66d 100644 --- a/tests/integrations/client/http_client_test.go +++ b/tests/integrations/client/http_client_test.go @@ -873,25 +873,24 @@ func (suite *httpClientTestSuite) TestGetSafePoint() { l, err := suite.client.GetGCSafePoint(suite.ctx) re.NoError(err) - re.Equal(l.GCSafePoint, uint64(1)) - re.Equal(l.MinServiceGcSafepoint, uint64(10)) - re.Equal(len(l.ServiceGCSafepoints), 3) + re.Equal(uint64(1), l.GCSafePoint) + re.Equal(uint64(10), l.MinServiceGcSafepoint) + re.Equal(3, len(l.ServiceGCSafepoints)) for i, val := range l.ServiceGCSafepoints { if i == 0 { - re.Equal(val.ServiceID, "AAA") - re.Equal(val.SafePoint, uint64(10)) + re.Equal("AAA", val.ServiceID) + re.Equal(uint64(10), val.SafePoint) } if i == 1 { - re.Equal(val.ServiceID, "BBB") + re.Equal("BBB", val.ServiceID) re.Equal(val.SafePoint, uint64(20)) } if i == 2 { - re.Equal(val.ServiceID, "CCC") - re.Equal(val.SafePoint, uint64(30)) - + re.Equal("CCC", val.ServiceID) + re.Equal(uint64(30), val.SafePoint) } } } @@ -900,16 +899,16 @@ func (suite *httpClientTestSuite) TestDeleteSafePoint() { re := suite.Require() msg1, err1 := suite.client.DeleteGCSafePoint(suite.ctx, "AAA") re.NoError(err1) - re.Equal(msg1, "Delete service GC safepoint successfully.") + re.Equal("Delete service GC safepoint successfully.", msg1) msg2, err2 := suite.client.DeleteGCSafePoint(suite.ctx, "BBB") re.NoError(err2) - re.Equal(msg2, "Delete service GC safepoint successfully.") + re.Equal("Delete service GC safepoint successfully.", msg2) msg3, err3 := suite.client.DeleteGCSafePoint(suite.ctx, "DDD") re.NoError(err3) - re.Equal(msg3, "Delete service GC safepoint successfully.") + re.Equal("Delete service GC safepoint successfully.", msg3) _, err4 := suite.client.DeleteGCSafePoint(suite.ctx, "gc_worker") - re.True(err4 != nil) + re.NotEqual(err4, nil) } From c88639bb7c91e879f70f3ed9315b162ce84e8fbb Mon Sep 17 00:00:00 2001 From: Boyang Lyu Date: Thu, 8 Aug 2024 19:06:17 +0800 Subject: [PATCH 09/35] fix coding style for testing suite again Signed-off-by: Boyang Lyu --- tests/integrations/client/http_client_test.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/integrations/client/http_client_test.go b/tests/integrations/client/http_client_test.go index 1a3c375e66d..0378e7632a6 100644 --- a/tests/integrations/client/http_client_test.go +++ b/tests/integrations/client/http_client_test.go @@ -875,7 +875,7 @@ func (suite *httpClientTestSuite) TestGetSafePoint() { re.Equal(uint64(1), l.GCSafePoint) re.Equal(uint64(10), l.MinServiceGcSafepoint) - re.Equal(3, len(l.ServiceGCSafepoints)) + re.Len(l.ServiceGCSafepoints, 3) for i, val := range l.ServiceGCSafepoints { if i == 0 { @@ -885,7 +885,7 @@ func (suite *httpClientTestSuite) TestGetSafePoint() { if i == 1 { re.Equal("BBB", val.ServiceID) - re.Equal(val.SafePoint, uint64(20)) + re.Equal(uint64(20), val.SafePoint) } if i == 2 { @@ -910,5 +910,5 @@ func (suite *httpClientTestSuite) TestDeleteSafePoint() { re.Equal("Delete service GC safepoint successfully.", msg3) _, err4 := suite.client.DeleteGCSafePoint(suite.ctx, "gc_worker") - re.NotEqual(err4, nil) + re.Error(err4) } From 870f9cc88d1a6b3f43b718f8cef19c7b8574f4a1 Mon Sep 17 00:00:00 2001 From: Boyang Lyu Date: Thu, 8 Aug 2024 19:17:24 +0800 Subject: [PATCH 10/35] changed the test case for get SafePoint Signed-off-by: Boyang Lyu --- tests/integrations/client/http_client_test.go | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/tests/integrations/client/http_client_test.go b/tests/integrations/client/http_client_test.go index 0378e7632a6..eca1d369534 100644 --- a/tests/integrations/client/http_client_test.go +++ b/tests/integrations/client/http_client_test.go @@ -118,17 +118,17 @@ func (suite *httpClientTestSuite) SetupSuite() { { ServiceID: "AAA", ExpiredAt: time.Now().Unix() + 10, - SafePoint: 10, + SafePoint: 1, }, { ServiceID: "BBB", ExpiredAt: time.Now().Unix() + 10, - SafePoint: 20, + SafePoint: 2, }, { ServiceID: "CCC", ExpiredAt: time.Now().Unix() + 10, - SafePoint: 30, + SafePoint: 3, }, }, GCSafePoint: 1, @@ -874,23 +874,23 @@ func (suite *httpClientTestSuite) TestGetSafePoint() { re.NoError(err) re.Equal(uint64(1), l.GCSafePoint) - re.Equal(uint64(10), l.MinServiceGcSafepoint) + re.Equal(uint64(1), l.MinServiceGcSafepoint) re.Len(l.ServiceGCSafepoints, 3) for i, val := range l.ServiceGCSafepoints { if i == 0 { re.Equal("AAA", val.ServiceID) - re.Equal(uint64(10), val.SafePoint) + re.Equal(uint64(1), val.SafePoint) } if i == 1 { re.Equal("BBB", val.ServiceID) - re.Equal(uint64(20), val.SafePoint) + re.Equal(uint64(2), val.SafePoint) } if i == 2 { re.Equal("CCC", val.ServiceID) - re.Equal(uint64(30), val.SafePoint) + re.Equal(uint64(3), val.SafePoint) } } } From be5c4a18c7dc736a10e49bb5ce3d6eb04b40ad73 Mon Sep 17 00:00:00 2001 From: Boyang Lyu Date: Thu, 8 Aug 2024 19:42:57 +0800 Subject: [PATCH 11/35] edit test function Signed-off-by: Boyang Lyu --- tests/integrations/client/http_client_test.go | 40 +++++++++++++------ 1 file changed, 28 insertions(+), 12 deletions(-) diff --git a/tests/integrations/client/http_client_test.go b/tests/integrations/client/http_client_test.go index eca1d369534..8788f295290 100644 --- a/tests/integrations/client/http_client_test.go +++ b/tests/integrations/client/http_client_test.go @@ -134,14 +134,22 @@ func (suite *httpClientTestSuite) SetupSuite() { GCSafePoint: 1, MinServiceGcSafepoint: 1, } - for _, s := range testServers { - storage := s.GetServer().GetStorage() - for _, ssp := range list.ServiceGCSafepoints { - err := storage.SaveServiceGCSafePoint(ssp) - re.NoError(err) - } - storage.SaveGCSafePoint(1) + + storage := suite.cluster.GetLeaderServer().GetServer().GetStorage() + for _, ssp := range list.ServiceGCSafepoints { + err := storage.SaveServiceGCSafePoint(ssp) + re.NoError(err) } + storage.SaveGCSafePoint(1) + + // for _, s := range testServers { + // storage := s.GetServer().GetStorage() + // for _, ssp := range list.ServiceGCSafepoints { + // err := storage.SaveServiceGCSafePoint(ssp) + // re.NoError(err) + // } + // storage.SaveGCSafePoint(1) + // } if suite.withServiceDiscovery { // Run test with specific service discovery. @@ -870,7 +878,11 @@ func (suite *httpClientTestSuite) TestRetryOnLeaderChange() { func (suite *httpClientTestSuite) TestGetSafePoint() { re := suite.Require() - l, err := suite.client.GetGCSafePoint(suite.ctx) + client := suite.client + ctx, cancel := context.WithCancel(suite.ctx) + defer cancel() + + l, err := client.GetGCSafePoint(ctx) re.NoError(err) re.Equal(uint64(1), l.GCSafePoint) @@ -897,18 +909,22 @@ func (suite *httpClientTestSuite) TestGetSafePoint() { func (suite *httpClientTestSuite) TestDeleteSafePoint() { re := suite.Require() - msg1, err1 := suite.client.DeleteGCSafePoint(suite.ctx, "AAA") + client := suite.client + ctx, cancel := context.WithCancel(suite.ctx) + defer cancel() + + msg1, err1 := client.DeleteGCSafePoint(ctx, "AAA") re.NoError(err1) re.Equal("Delete service GC safepoint successfully.", msg1) - msg2, err2 := suite.client.DeleteGCSafePoint(suite.ctx, "BBB") + msg2, err2 := client.DeleteGCSafePoint(ctx, "BBB") re.NoError(err2) re.Equal("Delete service GC safepoint successfully.", msg2) - msg3, err3 := suite.client.DeleteGCSafePoint(suite.ctx, "DDD") + msg3, err3 := client.DeleteGCSafePoint(ctx, "DDD") re.NoError(err3) re.Equal("Delete service GC safepoint successfully.", msg3) - _, err4 := suite.client.DeleteGCSafePoint(suite.ctx, "gc_worker") + _, err4 := client.DeleteGCSafePoint(ctx, "gc_worker") re.Error(err4) } From d6ed9d8663501557c94b9b3d792cf191b7f9e4c8 Mon Sep 17 00:00:00 2001 From: Boyang Lyu Date: Thu, 8 Aug 2024 20:12:31 +0800 Subject: [PATCH 12/35] edited some testcases Signed-off-by: Boyang Lyu --- tests/integrations/client/http_client_test.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/integrations/client/http_client_test.go b/tests/integrations/client/http_client_test.go index 8788f295290..283cd40d4c6 100644 --- a/tests/integrations/client/http_client_test.go +++ b/tests/integrations/client/http_client_test.go @@ -885,9 +885,9 @@ func (suite *httpClientTestSuite) TestGetSafePoint() { l, err := client.GetGCSafePoint(ctx) re.NoError(err) - re.Equal(uint64(1), l.GCSafePoint) - re.Equal(uint64(1), l.MinServiceGcSafepoint) - re.Len(l.ServiceGCSafepoints, 3) + // re.Equal(uint64(1), l.GCSafePoint) + // re.Equal(uint64(1), l.MinServiceGcSafepoint) + // re.Len(l.ServiceGCSafepoints, 3) for i, val := range l.ServiceGCSafepoints { if i == 0 { From 2d278ca03084dc820e3dafb057e8b900932cdca9 Mon Sep 17 00:00:00 2001 From: Boyang Lyu Date: Thu, 8 Aug 2024 20:28:56 +0800 Subject: [PATCH 13/35] put get/delete safepoint test into a single function Signed-off-by: Boyang Lyu --- tests/integrations/client/http_client_test.go | 111 ++++++++++-------- 1 file changed, 63 insertions(+), 48 deletions(-) diff --git a/tests/integrations/client/http_client_test.go b/tests/integrations/client/http_client_test.go index 283cd40d4c6..29d06818fcb 100644 --- a/tests/integrations/client/http_client_test.go +++ b/tests/integrations/client/http_client_test.go @@ -113,44 +113,6 @@ func (suite *httpClientTestSuite) SetupSuite() { suite.endpoints = endpoints suite.cluster = cluster - list := &api.ListServiceGCSafepoint{ - ServiceGCSafepoints: []*endpoint.ServiceSafePoint{ - { - ServiceID: "AAA", - ExpiredAt: time.Now().Unix() + 10, - SafePoint: 1, - }, - { - ServiceID: "BBB", - ExpiredAt: time.Now().Unix() + 10, - SafePoint: 2, - }, - { - ServiceID: "CCC", - ExpiredAt: time.Now().Unix() + 10, - SafePoint: 3, - }, - }, - GCSafePoint: 1, - MinServiceGcSafepoint: 1, - } - - storage := suite.cluster.GetLeaderServer().GetServer().GetStorage() - for _, ssp := range list.ServiceGCSafepoints { - err := storage.SaveServiceGCSafePoint(ssp) - re.NoError(err) - } - storage.SaveGCSafePoint(1) - - // for _, s := range testServers { - // storage := s.GetServer().GetStorage() - // for _, ssp := range list.ServiceGCSafepoints { - // err := storage.SaveServiceGCSafePoint(ssp) - // re.NoError(err) - // } - // storage.SaveGCSafePoint(1) - // } - if suite.withServiceDiscovery { // Run test with specific service discovery. cli := setupCli(suite.ctx, re, suite.endpoints) @@ -882,12 +844,50 @@ func (suite *httpClientTestSuite) TestGetSafePoint() { ctx, cancel := context.WithCancel(suite.ctx) defer cancel() + list := &api.ListServiceGCSafepoint{ + ServiceGCSafepoints: []*endpoint.ServiceSafePoint{ + { + ServiceID: "AAA", + ExpiredAt: time.Now().Unix() + 10, + SafePoint: 1, + }, + { + ServiceID: "BBB", + ExpiredAt: time.Now().Unix() + 10, + SafePoint: 2, + }, + { + ServiceID: "CCC", + ExpiredAt: time.Now().Unix() + 10, + SafePoint: 3, + }, + }, + GCSafePoint: 1, + MinServiceGcSafepoint: 1, + } + + storage := suite.cluster.GetLeaderServer().GetServer().GetStorage() + for _, ssp := range list.ServiceGCSafepoints { + err := storage.SaveServiceGCSafePoint(ssp) + re.NoError(err) + } + storage.SaveGCSafePoint(1) + + // for _, s := range testServers { + // storage := s.GetServer().GetStorage() + // for _, ssp := range list.ServiceGCSafepoints { + // err := storage.SaveServiceGCSafePoint(ssp) + // re.NoError(err) + // } + // storage.SaveGCSafePoint(1) + // } + l, err := client.GetGCSafePoint(ctx) re.NoError(err) - // re.Equal(uint64(1), l.GCSafePoint) - // re.Equal(uint64(1), l.MinServiceGcSafepoint) - // re.Len(l.ServiceGCSafepoints, 3) + re.Equal(uint64(1), l.GCSafePoint) + re.Equal(uint64(1), l.MinServiceGcSafepoint) + re.Len(l.ServiceGCSafepoints, 3) for i, val := range l.ServiceGCSafepoints { if i == 0 { @@ -905,13 +905,6 @@ func (suite *httpClientTestSuite) TestGetSafePoint() { re.Equal(uint64(3), val.SafePoint) } } -} - -func (suite *httpClientTestSuite) TestDeleteSafePoint() { - re := suite.Require() - client := suite.client - ctx, cancel := context.WithCancel(suite.ctx) - defer cancel() msg1, err1 := client.DeleteGCSafePoint(ctx, "AAA") re.NoError(err1) @@ -928,3 +921,25 @@ func (suite *httpClientTestSuite) TestDeleteSafePoint() { _, err4 := client.DeleteGCSafePoint(ctx, "gc_worker") re.Error(err4) } + +// func (suite *httpClientTestSuite) TestDeleteSafePoint() { +// re := suite.Require() +// client := suite.client +// ctx, cancel := context.WithCancel(suite.ctx) +// defer cancel() + +// msg1, err1 := client.DeleteGCSafePoint(ctx, "AAA") +// re.NoError(err1) +// re.Equal("Delete service GC safepoint successfully.", msg1) + +// msg2, err2 := client.DeleteGCSafePoint(ctx, "BBB") +// re.NoError(err2) +// re.Equal("Delete service GC safepoint successfully.", msg2) + +// msg3, err3 := client.DeleteGCSafePoint(ctx, "DDD") +// re.NoError(err3) +// re.Equal("Delete service GC safepoint successfully.", msg3) + +// _, err4 := client.DeleteGCSafePoint(ctx, "gc_worker") +// re.Error(err4) +// } From 6c7de04603955f60e85e864ea8f652235332bc33 Mon Sep 17 00:00:00 2001 From: Boyang Lyu Date: Thu, 8 Aug 2024 20:38:11 +0800 Subject: [PATCH 14/35] removed unnecessary comments Signed-off-by: Boyang Lyu --- tests/integrations/client/http_client_test.go | 31 ------------------- 1 file changed, 31 deletions(-) diff --git a/tests/integrations/client/http_client_test.go b/tests/integrations/client/http_client_test.go index 29d06818fcb..650279536f9 100644 --- a/tests/integrations/client/http_client_test.go +++ b/tests/integrations/client/http_client_test.go @@ -873,15 +873,6 @@ func (suite *httpClientTestSuite) TestGetSafePoint() { } storage.SaveGCSafePoint(1) - // for _, s := range testServers { - // storage := s.GetServer().GetStorage() - // for _, ssp := range list.ServiceGCSafepoints { - // err := storage.SaveServiceGCSafePoint(ssp) - // re.NoError(err) - // } - // storage.SaveGCSafePoint(1) - // } - l, err := client.GetGCSafePoint(ctx) re.NoError(err) @@ -921,25 +912,3 @@ func (suite *httpClientTestSuite) TestGetSafePoint() { _, err4 := client.DeleteGCSafePoint(ctx, "gc_worker") re.Error(err4) } - -// func (suite *httpClientTestSuite) TestDeleteSafePoint() { -// re := suite.Require() -// client := suite.client -// ctx, cancel := context.WithCancel(suite.ctx) -// defer cancel() - -// msg1, err1 := client.DeleteGCSafePoint(ctx, "AAA") -// re.NoError(err1) -// re.Equal("Delete service GC safepoint successfully.", msg1) - -// msg2, err2 := client.DeleteGCSafePoint(ctx, "BBB") -// re.NoError(err2) -// re.Equal("Delete service GC safepoint successfully.", msg2) - -// msg3, err3 := client.DeleteGCSafePoint(ctx, "DDD") -// re.NoError(err3) -// re.Equal("Delete service GC safepoint successfully.", msg3) - -// _, err4 := client.DeleteGCSafePoint(ctx, "gc_worker") -// re.Error(err4) -// } From 11d5353ce62df3823074378a10778389563c9cc9 Mon Sep 17 00:00:00 2001 From: Boyang Lyu Date: Fri, 9 Aug 2024 13:57:21 +0800 Subject: [PATCH 15/35] made other modifications Signed-off-by: Boyang Lyu --- client/http/api.go | 5 +++ client/http/interface.go | 6 ++-- pkg/storage/endpoint/gc_safe_point.go | 2 +- server/api/service_gc_safepoint.go | 2 +- tests/integrations/client/http_client_test.go | 32 ++++--------------- .../pdctl/command/gc_safepoint_command.go | 3 +- 6 files changed, 19 insertions(+), 31 deletions(-) diff --git a/client/http/api.go b/client/http/api.go index 90fa128defd..97d155fe04a 100644 --- a/client/http/api.go +++ b/client/http/api.go @@ -216,3 +216,8 @@ func GetUpdateKeyspaceConfigURL(keyspaceName string) string { func GetKeyspaceMetaByNameURL(keyspaceName string) string { return fmt.Sprintf(GetKeyspaceMetaByName, keyspaceName) } + +// GetDeleteSafePointURI returns the URI for delete safepoint service +func GetDeleteSafePointURI(serviceID string) string { + return safepoint + "/" + serviceID +} diff --git a/client/http/interface.go b/client/http/interface.go index ab5064770ac..f5cd1a38211 100644 --- a/client/http/interface.go +++ b/client/http/interface.go @@ -1027,7 +1027,7 @@ func (c *client) GetKeyspaceMetaByName(ctx context.Context, keyspaceName string) return &keyspaceMetaPB, nil } -// GetGCSafePoint get the gc_safepoint +// GetGCSafePoint gets the GC safe point list. func (c *client) GetGCSafePoint(ctx context.Context) (ListServiceGCSafepoint, error) { var gcSafePoint ListServiceGCSafepoint err := c.request(ctx, newRequestInfo(). @@ -1041,12 +1041,12 @@ func (c *client) GetGCSafePoint(ctx context.Context) (ListServiceGCSafepoint, er return gcSafePoint, nil } -// DeleteGCSafePoint deletes a gc safepoint +// DeleteGCSafePoint deletes a GC safe point with the given service ID. func (c *client) DeleteGCSafePoint(ctx context.Context, serviceID string) (string, error) { var msg string err := c.request(ctx, newRequestInfo(). WithName(DeleteGCSafePointName). - WithURI(safepoint+"/"+serviceID). + WithURI(GetDeleteSafePointURI(serviceID)). WithMethod(http.MethodDelete). WithResp(&msg)) if err != nil { diff --git a/pkg/storage/endpoint/gc_safe_point.go b/pkg/storage/endpoint/gc_safe_point.go index 137d2abd680..8b6736fe0dc 100644 --- a/pkg/storage/endpoint/gc_safe_point.go +++ b/pkg/storage/endpoint/gc_safe_point.go @@ -28,7 +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 +// 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 06d7f174a5a..ca29f9c352f 100644 --- a/server/api/service_gc_safepoint.go +++ b/server/api/service_gc_safepoint.go @@ -38,7 +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 +// This type is in sync with `pd/client/http/types.go`. type ListServiceGCSafepoint struct { ServiceGCSafepoints []*endpoint.ServiceSafePoint `json:"service_gc_safe_points"` MinServiceGcSafepoint uint64 `json:"min_service_gc_safe_point,omitempty"` diff --git a/tests/integrations/client/http_client_test.go b/tests/integrations/client/http_client_test.go index 650279536f9..bc343bf6d72 100644 --- a/tests/integrations/client/http_client_test.go +++ b/tests/integrations/client/http_client_test.go @@ -881,33 +881,15 @@ func (suite *httpClientTestSuite) TestGetSafePoint() { re.Len(l.ServiceGCSafepoints, 3) for i, val := range l.ServiceGCSafepoints { - if i == 0 { - re.Equal("AAA", val.ServiceID) - re.Equal(uint64(1), val.SafePoint) - } - - if i == 1 { - re.Equal("BBB", val.ServiceID) - re.Equal(uint64(2), val.SafePoint) - } - - if i == 2 { - re.Equal("CCC", val.ServiceID) - re.Equal(uint64(3), val.SafePoint) - } + re.Equal(list.ServiceGCSafepoints[i].ServiceID, val.ServiceID) + re.Equal(list.ServiceGCSafepoints[i].SafePoint, val.SafePoint) } - msg1, err1 := client.DeleteGCSafePoint(ctx, "AAA") - re.NoError(err1) - re.Equal("Delete service GC safepoint successfully.", msg1) - - msg2, err2 := client.DeleteGCSafePoint(ctx, "BBB") - re.NoError(err2) - re.Equal("Delete service GC safepoint successfully.", msg2) - - msg3, err3 := client.DeleteGCSafePoint(ctx, "DDD") - re.NoError(err3) - re.Equal("Delete service GC safepoint successfully.", msg3) + for i := 0; i < 3; i++ { + msg, err := client.DeleteGCSafePoint(ctx, list.ServiceGCSafepoints[i].ServiceID) + re.NoError(err) + re.Equal("Delete service GC safepoint successfully.", msg) + } _, err4 := client.DeleteGCSafePoint(ctx, "gc_worker") re.Error(err4) diff --git a/tools/pd-ctl/pdctl/command/gc_safepoint_command.go b/tools/pd-ctl/pdctl/command/gc_safepoint_command.go index 8cbce189693..8849eb2c848 100644 --- a/tools/pd-ctl/pdctl/command/gc_safepoint_command.go +++ b/tools/pd-ctl/pdctl/command/gc_safepoint_command.go @@ -15,8 +15,9 @@ package command import ( - "github.com/spf13/cobra" "sort" + + "github.com/spf13/cobra" ) // NewServiceGCSafepointCommand return a service gc safepoint subcommand of rootCmd From acec910fec93ecc7f9c31e6ec82941c0e2201fb9 Mon Sep 17 00:00:00 2001 From: Boyang Lyu Date: Fri, 9 Aug 2024 14:17:39 +0800 Subject: [PATCH 16/35] changed test funciton name Signed-off-by: Boyang Lyu --- tests/integrations/client/http_client_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/integrations/client/http_client_test.go b/tests/integrations/client/http_client_test.go index bc343bf6d72..fae42b4d047 100644 --- a/tests/integrations/client/http_client_test.go +++ b/tests/integrations/client/http_client_test.go @@ -838,7 +838,7 @@ func (suite *httpClientTestSuite) TestRetryOnLeaderChange() { wg.Wait() } -func (suite *httpClientTestSuite) TestGetSafePoint() { +func (suite *httpClientTestSuite) TestGetGCSafePoint() { re := suite.Require() client := suite.client ctx, cancel := context.WithCancel(suite.ctx) From 1a1912aaf90fbe63c12a05dbddace0490a810c6a Mon Sep 17 00:00:00 2001 From: Boyang Lyu Date: Fri, 9 Aug 2024 14:29:22 +0800 Subject: [PATCH 17/35] do some experiments Signed-off-by: Boyang Lyu --- tests/integrations/client/http_client_test.go | 50 +++++++++++++------ 1 file changed, 36 insertions(+), 14 deletions(-) diff --git a/tests/integrations/client/http_client_test.go b/tests/integrations/client/http_client_test.go index fae42b4d047..3c1cc995318 100644 --- a/tests/integrations/client/http_client_test.go +++ b/tests/integrations/client/http_client_test.go @@ -113,6 +113,35 @@ func (suite *httpClientTestSuite) SetupSuite() { suite.endpoints = endpoints suite.cluster = cluster + list := &api.ListServiceGCSafepoint{ + ServiceGCSafepoints: []*endpoint.ServiceSafePoint{ + { + ServiceID: "AAA", + ExpiredAt: time.Now().Unix() + 10, + SafePoint: 1, + }, + { + ServiceID: "BBB", + ExpiredAt: time.Now().Unix() + 10, + SafePoint: 2, + }, + { + ServiceID: "CCC", + ExpiredAt: time.Now().Unix() + 10, + SafePoint: 3, + }, + }, + GCSafePoint: 1, + MinServiceGcSafepoint: 1, + } + + storage := suite.cluster.GetLeaderServer().GetServer().GetStorage() + for _, ssp := range list.ServiceGCSafepoints { + err := storage.SaveServiceGCSafePoint(ssp) + re.NoError(err) + } + storage.SaveGCSafePoint(1) + if suite.withServiceDiscovery { // Run test with specific service discovery. cli := setupCli(suite.ctx, re, suite.endpoints) @@ -844,6 +873,13 @@ func (suite *httpClientTestSuite) TestGetGCSafePoint() { ctx, cancel := context.WithCancel(suite.ctx) defer cancel() + l, err := client.GetGCSafePoint(ctx) + re.NoError(err) + + re.Equal(uint64(1), l.GCSafePoint) + re.Equal(uint64(1), l.MinServiceGcSafepoint) + re.Len(l.ServiceGCSafepoints, 3) + list := &api.ListServiceGCSafepoint{ ServiceGCSafepoints: []*endpoint.ServiceSafePoint{ { @@ -866,20 +902,6 @@ func (suite *httpClientTestSuite) TestGetGCSafePoint() { MinServiceGcSafepoint: 1, } - storage := suite.cluster.GetLeaderServer().GetServer().GetStorage() - for _, ssp := range list.ServiceGCSafepoints { - err := storage.SaveServiceGCSafePoint(ssp) - re.NoError(err) - } - storage.SaveGCSafePoint(1) - - l, err := client.GetGCSafePoint(ctx) - re.NoError(err) - - re.Equal(uint64(1), l.GCSafePoint) - re.Equal(uint64(1), l.MinServiceGcSafepoint) - re.Len(l.ServiceGCSafepoints, 3) - for i, val := range l.ServiceGCSafepoints { re.Equal(list.ServiceGCSafepoints[i].ServiceID, val.ServiceID) re.Equal(list.ServiceGCSafepoints[i].SafePoint, val.SafePoint) From 83de36f15d2ac7077eaa3650db2e35df82fdea9a Mon Sep 17 00:00:00 2001 From: Boyang Lyu Date: Fri, 9 Aug 2024 14:35:50 +0800 Subject: [PATCH 18/35] delete my test function Signed-off-by: Boyang Lyu --- tests/integrations/client/http_client_test.go | 98 +++++++++---------- 1 file changed, 49 insertions(+), 49 deletions(-) diff --git a/tests/integrations/client/http_client_test.go b/tests/integrations/client/http_client_test.go index 3c1cc995318..765ae11d5a1 100644 --- a/tests/integrations/client/http_client_test.go +++ b/tests/integrations/client/http_client_test.go @@ -867,52 +867,52 @@ func (suite *httpClientTestSuite) TestRetryOnLeaderChange() { wg.Wait() } -func (suite *httpClientTestSuite) TestGetGCSafePoint() { - re := suite.Require() - client := suite.client - ctx, cancel := context.WithCancel(suite.ctx) - defer cancel() - - l, err := client.GetGCSafePoint(ctx) - re.NoError(err) - - re.Equal(uint64(1), l.GCSafePoint) - re.Equal(uint64(1), l.MinServiceGcSafepoint) - re.Len(l.ServiceGCSafepoints, 3) - - list := &api.ListServiceGCSafepoint{ - ServiceGCSafepoints: []*endpoint.ServiceSafePoint{ - { - ServiceID: "AAA", - ExpiredAt: time.Now().Unix() + 10, - SafePoint: 1, - }, - { - ServiceID: "BBB", - ExpiredAt: time.Now().Unix() + 10, - SafePoint: 2, - }, - { - ServiceID: "CCC", - ExpiredAt: time.Now().Unix() + 10, - SafePoint: 3, - }, - }, - GCSafePoint: 1, - MinServiceGcSafepoint: 1, - } - - for i, val := range l.ServiceGCSafepoints { - re.Equal(list.ServiceGCSafepoints[i].ServiceID, val.ServiceID) - re.Equal(list.ServiceGCSafepoints[i].SafePoint, val.SafePoint) - } - - for i := 0; i < 3; i++ { - msg, err := client.DeleteGCSafePoint(ctx, list.ServiceGCSafepoints[i].ServiceID) - re.NoError(err) - re.Equal("Delete service GC safepoint successfully.", msg) - } - - _, err4 := client.DeleteGCSafePoint(ctx, "gc_worker") - re.Error(err4) -} +// func (suite *httpClientTestSuite) TestGetGCSafePoint() { +// re := suite.Require() +// client := suite.client +// ctx, cancel := context.WithCancel(suite.ctx) +// defer cancel() + +// l, err := client.GetGCSafePoint(ctx) +// re.NoError(err) + +// re.Equal(uint64(1), l.GCSafePoint) +// re.Equal(uint64(1), l.MinServiceGcSafepoint) +// re.Len(l.ServiceGCSafepoints, 3) + +// list := &api.ListServiceGCSafepoint{ +// ServiceGCSafepoints: []*endpoint.ServiceSafePoint{ +// { +// ServiceID: "AAA", +// ExpiredAt: time.Now().Unix() + 10, +// SafePoint: 1, +// }, +// { +// ServiceID: "BBB", +// ExpiredAt: time.Now().Unix() + 10, +// SafePoint: 2, +// }, +// { +// ServiceID: "CCC", +// ExpiredAt: time.Now().Unix() + 10, +// SafePoint: 3, +// }, +// }, +// GCSafePoint: 1, +// MinServiceGcSafepoint: 1, +// } + +// for i, val := range l.ServiceGCSafepoints { +// re.Equal(list.ServiceGCSafepoints[i].ServiceID, val.ServiceID) +// re.Equal(list.ServiceGCSafepoints[i].SafePoint, val.SafePoint) +// } + +// for i := 0; i < 3; i++ { +// msg, err := client.DeleteGCSafePoint(ctx, list.ServiceGCSafepoints[i].ServiceID) +// re.NoError(err) +// re.Equal("Delete service GC safepoint successfully.", msg) +// } + +// _, err4 := client.DeleteGCSafePoint(ctx, "gc_worker") +// re.Error(err4) +// } From cd0a7d530a46e115abdafb50feb398bdffa03b72 Mon Sep 17 00:00:00 2001 From: Boyang Lyu Date: Fri, 9 Aug 2024 14:37:03 +0800 Subject: [PATCH 19/35] add an empty test funciton Signed-off-by: Boyang Lyu --- tests/integrations/client/http_client_test.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/tests/integrations/client/http_client_test.go b/tests/integrations/client/http_client_test.go index 765ae11d5a1..8ddbfc5c340 100644 --- a/tests/integrations/client/http_client_test.go +++ b/tests/integrations/client/http_client_test.go @@ -867,6 +867,10 @@ func (suite *httpClientTestSuite) TestRetryOnLeaderChange() { wg.Wait() } +func (suite *httpClientTestSuite) TestGetGCSafePoint() { + +} + // func (suite *httpClientTestSuite) TestGetGCSafePoint() { // re := suite.Require() // client := suite.client From 3858cb3a11ec628a83d56f0b8839d6176f10b565 Mon Sep 17 00:00:00 2001 From: Boyang Lyu Date: Fri, 9 Aug 2024 14:44:56 +0800 Subject: [PATCH 20/35] remove the test delete logic from test funciton Signed-off-by: Boyang Lyu --- tests/integrations/client/http_client_test.go | 96 +++++++++---------- 1 file changed, 46 insertions(+), 50 deletions(-) diff --git a/tests/integrations/client/http_client_test.go b/tests/integrations/client/http_client_test.go index 8ddbfc5c340..830debff98c 100644 --- a/tests/integrations/client/http_client_test.go +++ b/tests/integrations/client/http_client_test.go @@ -868,55 +868,51 @@ func (suite *httpClientTestSuite) TestRetryOnLeaderChange() { } func (suite *httpClientTestSuite) TestGetGCSafePoint() { + re := suite.Require() + client := suite.client + ctx, cancel := context.WithCancel(suite.ctx) + defer cancel() -} + l, err := client.GetGCSafePoint(ctx) + re.NoError(err) + + re.Equal(uint64(1), l.GCSafePoint) + re.Equal(uint64(1), l.MinServiceGcSafepoint) + re.Len(l.ServiceGCSafepoints, 3) -// func (suite *httpClientTestSuite) TestGetGCSafePoint() { -// re := suite.Require() -// client := suite.client -// ctx, cancel := context.WithCancel(suite.ctx) -// defer cancel() - -// l, err := client.GetGCSafePoint(ctx) -// re.NoError(err) - -// re.Equal(uint64(1), l.GCSafePoint) -// re.Equal(uint64(1), l.MinServiceGcSafepoint) -// re.Len(l.ServiceGCSafepoints, 3) - -// list := &api.ListServiceGCSafepoint{ -// ServiceGCSafepoints: []*endpoint.ServiceSafePoint{ -// { -// ServiceID: "AAA", -// ExpiredAt: time.Now().Unix() + 10, -// SafePoint: 1, -// }, -// { -// ServiceID: "BBB", -// ExpiredAt: time.Now().Unix() + 10, -// SafePoint: 2, -// }, -// { -// ServiceID: "CCC", -// ExpiredAt: time.Now().Unix() + 10, -// SafePoint: 3, -// }, -// }, -// GCSafePoint: 1, -// MinServiceGcSafepoint: 1, -// } - -// for i, val := range l.ServiceGCSafepoints { -// re.Equal(list.ServiceGCSafepoints[i].ServiceID, val.ServiceID) -// re.Equal(list.ServiceGCSafepoints[i].SafePoint, val.SafePoint) -// } - -// for i := 0; i < 3; i++ { -// msg, err := client.DeleteGCSafePoint(ctx, list.ServiceGCSafepoints[i].ServiceID) -// re.NoError(err) -// re.Equal("Delete service GC safepoint successfully.", msg) -// } - -// _, err4 := client.DeleteGCSafePoint(ctx, "gc_worker") -// re.Error(err4) -// } + list := &api.ListServiceGCSafepoint{ + ServiceGCSafepoints: []*endpoint.ServiceSafePoint{ + { + ServiceID: "AAA", + ExpiredAt: time.Now().Unix() + 10, + SafePoint: 1, + }, + { + ServiceID: "BBB", + ExpiredAt: time.Now().Unix() + 10, + SafePoint: 2, + }, + { + ServiceID: "CCC", + ExpiredAt: time.Now().Unix() + 10, + SafePoint: 3, + }, + }, + GCSafePoint: 1, + MinServiceGcSafepoint: 1, + } + + for i, val := range l.ServiceGCSafepoints { + re.Equal(list.ServiceGCSafepoints[i].ServiceID, val.ServiceID) + re.Equal(list.ServiceGCSafepoints[i].SafePoint, val.SafePoint) + } + + // for i := 0; i < 3; i++ { + // msg, err := client.DeleteGCSafePoint(ctx, list.ServiceGCSafepoints[i].ServiceID) + // re.NoError(err) + // re.Equal("Delete service GC safepoint successfully.", msg) + // } + + _, err4 := client.DeleteGCSafePoint(ctx, "gc_worker") + re.Error(err4) +} From 11be503a0d310af5df3713576794edd619b929a0 Mon Sep 17 00:00:00 2001 From: Boyang Lyu Date: Fri, 9 Aug 2024 14:53:22 +0800 Subject: [PATCH 21/35] remove the test delete logic from test funciton, and move the test logic into local functions Signed-off-by: Boyang Lyu --- tests/integrations/client/http_client_test.go | 50 ++++++------------- 1 file changed, 14 insertions(+), 36 deletions(-) diff --git a/tests/integrations/client/http_client_test.go b/tests/integrations/client/http_client_test.go index 830debff98c..bb8049181e0 100644 --- a/tests/integrations/client/http_client_test.go +++ b/tests/integrations/client/http_client_test.go @@ -113,35 +113,6 @@ func (suite *httpClientTestSuite) SetupSuite() { suite.endpoints = endpoints suite.cluster = cluster - list := &api.ListServiceGCSafepoint{ - ServiceGCSafepoints: []*endpoint.ServiceSafePoint{ - { - ServiceID: "AAA", - ExpiredAt: time.Now().Unix() + 10, - SafePoint: 1, - }, - { - ServiceID: "BBB", - ExpiredAt: time.Now().Unix() + 10, - SafePoint: 2, - }, - { - ServiceID: "CCC", - ExpiredAt: time.Now().Unix() + 10, - SafePoint: 3, - }, - }, - GCSafePoint: 1, - MinServiceGcSafepoint: 1, - } - - storage := suite.cluster.GetLeaderServer().GetServer().GetStorage() - for _, ssp := range list.ServiceGCSafepoints { - err := storage.SaveServiceGCSafePoint(ssp) - re.NoError(err) - } - storage.SaveGCSafePoint(1) - if suite.withServiceDiscovery { // Run test with specific service discovery. cli := setupCli(suite.ctx, re, suite.endpoints) @@ -873,13 +844,6 @@ func (suite *httpClientTestSuite) TestGetGCSafePoint() { ctx, cancel := context.WithCancel(suite.ctx) defer cancel() - l, err := client.GetGCSafePoint(ctx) - re.NoError(err) - - re.Equal(uint64(1), l.GCSafePoint) - re.Equal(uint64(1), l.MinServiceGcSafepoint) - re.Len(l.ServiceGCSafepoints, 3) - list := &api.ListServiceGCSafepoint{ ServiceGCSafepoints: []*endpoint.ServiceSafePoint{ { @@ -902,6 +866,20 @@ func (suite *httpClientTestSuite) TestGetGCSafePoint() { MinServiceGcSafepoint: 1, } + storage := suite.cluster.GetLeaderServer().GetServer().GetStorage() + for _, ssp := range list.ServiceGCSafepoints { + err := storage.SaveServiceGCSafePoint(ssp) + re.NoError(err) + } + storage.SaveGCSafePoint(1) + + l, err := client.GetGCSafePoint(ctx) + re.NoError(err) + + re.Equal(uint64(1), l.GCSafePoint) + re.Equal(uint64(1), l.MinServiceGcSafepoint) + re.Len(l.ServiceGCSafepoints, 3) + for i, val := range l.ServiceGCSafepoints { re.Equal(list.ServiceGCSafepoints[i].ServiceID, val.ServiceID) re.Equal(list.ServiceGCSafepoints[i].SafePoint, val.SafePoint) From dc62cc289805c61142f28a46b6a9e840d7b6d5c3 Mon Sep 17 00:00:00 2001 From: Boyang Lyu Date: Fri, 9 Aug 2024 15:46:54 +0800 Subject: [PATCH 22/35] add requirePDClient to cobra command Signed-off-by: Boyang Lyu --- client/http/api.go | 2 +- tests/integrations/client/http_client_test.go | 3 +++ .../pd-ctl/pdctl/command/gc_safepoint_command.go | 16 +++++++++------- 3 files changed, 13 insertions(+), 8 deletions(-) diff --git a/client/http/api.go b/client/http/api.go index 97d155fe04a..d1bce99f4f9 100644 --- a/client/http/api.go +++ b/client/http/api.go @@ -219,5 +219,5 @@ func GetKeyspaceMetaByNameURL(keyspaceName string) string { // GetDeleteSafePointURI returns the URI for delete safepoint service func GetDeleteSafePointURI(serviceID string) string { - return safepoint + "/" + serviceID + return fmt.Sprintf("%s/%s", safepoint, serviceID) } diff --git a/tests/integrations/client/http_client_test.go b/tests/integrations/client/http_client_test.go index bb8049181e0..8e261e1555b 100644 --- a/tests/integrations/client/http_client_test.go +++ b/tests/integrations/client/http_client_test.go @@ -844,6 +844,7 @@ func (suite *httpClientTestSuite) TestGetGCSafePoint() { ctx, cancel := context.WithCancel(suite.ctx) defer cancel() + // adding some safepoints to the server list := &api.ListServiceGCSafepoint{ ServiceGCSafepoints: []*endpoint.ServiceSafePoint{ { @@ -873,6 +874,7 @@ func (suite *httpClientTestSuite) TestGetGCSafePoint() { } storage.SaveGCSafePoint(1) + // get the safepoints and start testing l, err := client.GetGCSafePoint(ctx) re.NoError(err) @@ -880,6 +882,7 @@ func (suite *httpClientTestSuite) TestGetGCSafePoint() { re.Equal(uint64(1), l.MinServiceGcSafepoint) re.Len(l.ServiceGCSafepoints, 3) + // TODO : add some sorting to preserve order for i, val := range l.ServiceGCSafepoints { re.Equal(list.ServiceGCSafepoints[i].ServiceID, val.ServiceID) re.Equal(list.ServiceGCSafepoints[i].SafePoint, val.SafePoint) diff --git a/tools/pd-ctl/pdctl/command/gc_safepoint_command.go b/tools/pd-ctl/pdctl/command/gc_safepoint_command.go index 8849eb2c848..f35677d5b34 100644 --- a/tools/pd-ctl/pdctl/command/gc_safepoint_command.go +++ b/tools/pd-ctl/pdctl/command/gc_safepoint_command.go @@ -23,9 +23,10 @@ import ( // NewServiceGCSafepointCommand return a service gc safepoint subcommand of rootCmd func NewServiceGCSafepointCommand() *cobra.Command { l := &cobra.Command{ - Use: "service-gc-safepoint", - Short: "show all service gc safepoint", - Run: showSSPs, + Use: "service-gc-safepoint", + Short: "show all service gc safepoint", + PersistentPreRunE: requirePDClient, + Run: showSSPs, } l.AddCommand(NewDeleteServiceGCSafepointCommand()) return l @@ -34,10 +35,11 @@ func NewServiceGCSafepointCommand() *cobra.Command { // NewDeleteServiceGCSafepointCommand return a subcommand to delete service gc safepoint func NewDeleteServiceGCSafepointCommand() *cobra.Command { l := &cobra.Command{ - Use: "delete ", - Short: "delete a service gc safepoint", - Run: deleteSSP, - Hidden: true, + Use: "delete ", + Short: "delete a service gc safepoint", + PersistentPreRunE: requirePDClient, + Run: deleteSSP, + Hidden: true, } return l } From 7adce308af3e98620e6a3bb7e43cf390c6acd8be Mon Sep 17 00:00:00 2001 From: Boyang Lyu Date: Fri, 9 Aug 2024 16:05:46 +0800 Subject: [PATCH 23/35] add the delete test back Signed-off-by: Boyang Lyu --- tests/integrations/client/http_client_test.go | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/tests/integrations/client/http_client_test.go b/tests/integrations/client/http_client_test.go index 8e261e1555b..c29d5fda400 100644 --- a/tests/integrations/client/http_client_test.go +++ b/tests/integrations/client/http_client_test.go @@ -888,11 +888,13 @@ func (suite *httpClientTestSuite) TestGetGCSafePoint() { re.Equal(list.ServiceGCSafepoints[i].SafePoint, val.SafePoint) } - // for i := 0; i < 3; i++ { - // msg, err := client.DeleteGCSafePoint(ctx, list.ServiceGCSafepoints[i].ServiceID) - // re.NoError(err) - // re.Equal("Delete service GC safepoint successfully.", msg) - // } + /* ************************** */ + for i := 0; i < 3; i++ { + msg, err := client.DeleteGCSafePoint(ctx, list.ServiceGCSafepoints[i].ServiceID) + re.NoError(err) + re.Equal("Delete service GC safepoint successfully.", msg) + } + /* ************************** */ _, err4 := client.DeleteGCSafePoint(ctx, "gc_worker") re.Error(err4) From 014ec40845b377d0c88d0f735ab11c52a074804b Mon Sep 17 00:00:00 2001 From: Boyang Lyu Date: Fri, 9 Aug 2024 16:13:23 +0800 Subject: [PATCH 24/35] try again Signed-off-by: Boyang Lyu --- tests/integrations/client/http_client_test.go | 2 -- 1 file changed, 2 deletions(-) diff --git a/tests/integrations/client/http_client_test.go b/tests/integrations/client/http_client_test.go index c29d5fda400..8bd82ed7f69 100644 --- a/tests/integrations/client/http_client_test.go +++ b/tests/integrations/client/http_client_test.go @@ -888,13 +888,11 @@ func (suite *httpClientTestSuite) TestGetGCSafePoint() { re.Equal(list.ServiceGCSafepoints[i].SafePoint, val.SafePoint) } - /* ************************** */ for i := 0; i < 3; i++ { msg, err := client.DeleteGCSafePoint(ctx, list.ServiceGCSafepoints[i].ServiceID) re.NoError(err) re.Equal("Delete service GC safepoint successfully.", msg) } - /* ************************** */ _, err4 := client.DeleteGCSafePoint(ctx, "gc_worker") re.Error(err4) From 4666fb553416675d0affcfbd3eb8259afe21f0b4 Mon Sep 17 00:00:00 2001 From: Boyang Lyu Date: Thu, 15 Aug 2024 14:57:58 +0800 Subject: [PATCH 25/35] add sorting logic to gc_safepoint test Signed-off-by: Boyang Lyu --- tests/integrations/client/http_client_test.go | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/tests/integrations/client/http_client_test.go b/tests/integrations/client/http_client_test.go index 8bd82ed7f69..11f6db8e883 100644 --- a/tests/integrations/client/http_client_test.go +++ b/tests/integrations/client/http_client_test.go @@ -882,7 +882,10 @@ func (suite *httpClientTestSuite) TestGetGCSafePoint() { re.Equal(uint64(1), l.MinServiceGcSafepoint) re.Len(l.ServiceGCSafepoints, 3) - // TODO : add some sorting to preserve order + sort.Slice(l.ServiceGCSafepoints, func(i, j int) bool { + return l.ServiceGCSafepoints[i].ServiceID < l.ServiceGCSafepoints[j].ServiceID + }) + for i, val := range l.ServiceGCSafepoints { re.Equal(list.ServiceGCSafepoints[i].ServiceID, val.ServiceID) re.Equal(list.ServiceGCSafepoints[i].SafePoint, val.SafePoint) From 670fe6a284801f8397b6ddf3437b91a41ed502e7 Mon Sep 17 00:00:00 2001 From: Boyang Lyu Date: Thu, 15 Aug 2024 15:16:54 +0800 Subject: [PATCH 26/35] add a second-round get check after safepoints are deleted Signed-off-by: Boyang Lyu --- tests/integrations/client/http_client_test.go | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/tests/integrations/client/http_client_test.go b/tests/integrations/client/http_client_test.go index 11f6db8e883..3d1d3428e2e 100644 --- a/tests/integrations/client/http_client_test.go +++ b/tests/integrations/client/http_client_test.go @@ -882,6 +882,7 @@ func (suite *httpClientTestSuite) TestGetGCSafePoint() { re.Equal(uint64(1), l.MinServiceGcSafepoint) re.Len(l.ServiceGCSafepoints, 3) + // sort the gc safepoints based on order of ServiceID sort.Slice(l.ServiceGCSafepoints, func(i, j int) bool { return l.ServiceGCSafepoints[i].ServiceID < l.ServiceGCSafepoints[j].ServiceID }) @@ -891,6 +892,7 @@ func (suite *httpClientTestSuite) TestGetGCSafePoint() { re.Equal(list.ServiceGCSafepoints[i].SafePoint, val.SafePoint) } + // delete the safepoints for i := 0; i < 3; i++ { msg, err := client.DeleteGCSafePoint(ctx, list.ServiceGCSafepoints[i].ServiceID) re.NoError(err) @@ -899,4 +901,12 @@ func (suite *httpClientTestSuite) TestGetGCSafePoint() { _, err4 := client.DeleteGCSafePoint(ctx, "gc_worker") re.Error(err4) + + // check that the safepoitns are indeed deleted + l, err = client.GetGCSafePoint(ctx) + re.NoError(err) + + re.Equal(uint64(1), l.GCSafePoint) + re.Equal(uint64(0), l.MinServiceGcSafepoint) + re.Len(l.ServiceGCSafepoints, 0) } From fb8a5b55e33cd0177a81b48155b451ee0ac691f8 Mon Sep 17 00:00:00 2001 From: Boyang Lyu Date: Thu, 15 Aug 2024 15:24:22 +0800 Subject: [PATCH 27/35] remove the requirePDClient in delete subcommand Signed-off-by: Boyang Lyu --- tests/integrations/client/http_client_test.go | 2 +- tools/pd-ctl/pdctl/command/gc_safepoint_command.go | 9 ++++----- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/tests/integrations/client/http_client_test.go b/tests/integrations/client/http_client_test.go index 3d1d3428e2e..982cb18f1d8 100644 --- a/tests/integrations/client/http_client_test.go +++ b/tests/integrations/client/http_client_test.go @@ -908,5 +908,5 @@ func (suite *httpClientTestSuite) TestGetGCSafePoint() { re.Equal(uint64(1), l.GCSafePoint) re.Equal(uint64(0), l.MinServiceGcSafepoint) - re.Len(l.ServiceGCSafepoints, 0) + re.Empty(l.ServiceGCSafepoints) } diff --git a/tools/pd-ctl/pdctl/command/gc_safepoint_command.go b/tools/pd-ctl/pdctl/command/gc_safepoint_command.go index f35677d5b34..9a07d92937f 100644 --- a/tools/pd-ctl/pdctl/command/gc_safepoint_command.go +++ b/tools/pd-ctl/pdctl/command/gc_safepoint_command.go @@ -35,11 +35,10 @@ func NewServiceGCSafepointCommand() *cobra.Command { // NewDeleteServiceGCSafepointCommand return a subcommand to delete service gc safepoint func NewDeleteServiceGCSafepointCommand() *cobra.Command { l := &cobra.Command{ - Use: "delete ", - Short: "delete a service gc safepoint", - PersistentPreRunE: requirePDClient, - Run: deleteSSP, - Hidden: true, + Use: "delete ", + Short: "delete a service gc safepoint", + Run: deleteSSP, + Hidden: true, } return l } From 7ef43f2a7feaf29d09f90df42e9ccef9f8d54497 Mon Sep 17 00:00:00 2001 From: Boyang Lyu Date: Fri, 16 Aug 2024 00:06:16 +0800 Subject: [PATCH 28/35] fix a typo Signed-off-by: Boyang Lyu --- client/http/types.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/client/http/types.go b/client/http/types.go index 1b04603e8b5..a262d9c672c 100644 --- a/client/http/types.go +++ b/client/http/types.go @@ -32,7 +32,7 @@ type ServiceSafePoint struct { SafePoint uint64 `json:"safe_point"` } -// NOTE: This type is in sync with pd/server/spi/service_gc_safepoint.go +// NOTE: This type is in sync with pd/server/api/service_gc_safepoint.go type ListServiceGCSafepoint struct { ServiceGCSafepoints []*ServiceSafePoint `json:"service_gc_safe_points"` MinServiceGcSafepoint uint64 `json:"min_service_gc_safe_point,omitempty"` From 98b09b7fde58998f4bc3b8254aedc1aa6853a507 Mon Sep 17 00:00:00 2001 From: Boyang Lyu Date: Fri, 16 Aug 2024 00:24:53 +0800 Subject: [PATCH 29/35] added tests Signed-off-by: Boyang Lyu --- .../pd-ctl/tests/safepoint/safepoint_test.go | 131 ++++++++++++++++++ 1 file changed, 131 insertions(+) create mode 100644 tools/pd-ctl/tests/safepoint/safepoint_test.go diff --git a/tools/pd-ctl/tests/safepoint/safepoint_test.go b/tools/pd-ctl/tests/safepoint/safepoint_test.go new file mode 100644 index 00000000000..4bff76cbcaf --- /dev/null +++ b/tools/pd-ctl/tests/safepoint/safepoint_test.go @@ -0,0 +1,131 @@ +// Copyright 2019 TiKV Project Authors. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package safepoint_test + +import ( + "context" + "encoding/json" + "sort" + "testing" + "time" + + "github.com/stretchr/testify/require" + "github.com/tikv/pd/pkg/storage/endpoint" + "github.com/tikv/pd/server/api" + pdTests "github.com/tikv/pd/tests" + ctl "github.com/tikv/pd/tools/pd-ctl/pdctl" + "github.com/tikv/pd/tools/pd-ctl/tests" +) + +func TestSafepoint(t *testing.T) { + re := require.New(t) + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() + tc, err := pdTests.NewTestCluster(ctx, 3) + re.NoError(err) + defer tc.Destroy() + err = tc.RunInitialServers() + re.NoError(err) + tc.WaitLeader() + leaderServer := tc.GetLeaderServer() + re.NoError(leaderServer.BootstrapCluster()) + pdAddr := tc.GetConfig().GetClientURL() + cmd := ctl.GetRootCmd() + + // add some gc_safepoint to the server + list := &api.ListServiceGCSafepoint{ + ServiceGCSafepoints: []*endpoint.ServiceSafePoint{ + { + ServiceID: "AAA", + ExpiredAt: time.Now().Unix() + 10, + SafePoint: 1, + }, + { + ServiceID: "BBB", + ExpiredAt: time.Now().Unix() + 10, + SafePoint: 2, + }, + { + ServiceID: "CCC", + ExpiredAt: time.Now().Unix() + 10, + SafePoint: 3, + }, + }, + GCSafePoint: 1, + MinServiceGcSafepoint: 1, + } + + storage := leaderServer.GetServer().GetStorage() + for _, ssp := range list.ServiceGCSafepoints { + err := storage.SaveServiceGCSafePoint(ssp) + re.NoError(err) + } + storage.SaveGCSafePoint(1) + + // get the safepoints + args := []string{"-u", pdAddr, "service-gc-safepoint"} + output, err := tests.ExecuteCommand(cmd, args...) + re.NoError(err) + + // create an container to hold the received values + var l api.ListServiceGCSafepoint + re.NoError(json.Unmarshal(output, &l)) + + // test if the points are what we expected + re.Equal(uint64(1), l.GCSafePoint) + re.Equal(uint64(1), l.MinServiceGcSafepoint) + re.Len(l.ServiceGCSafepoints, 3) + + // sort the gc safepoints based on order of ServiceID + sort.Slice(l.ServiceGCSafepoints, func(i, j int) bool { + return l.ServiceGCSafepoints[i].ServiceID < l.ServiceGCSafepoints[j].ServiceID + }) + + for i, val := range l.ServiceGCSafepoints { + re.Equal(list.ServiceGCSafepoints[i].ServiceID, val.ServiceID) + re.Equal(list.ServiceGCSafepoints[i].SafePoint, val.SafePoint) + } + + // delete the safepoints + for i := 0; i < 3; i++ { + args = []string{"-u", pdAddr, "service-gc-safepoint", "delete", list.ServiceGCSafepoints[i].ServiceID} + output, err = tests.ExecuteCommand(cmd, args...) + re.NoError(err) + var msg string + re.NoError(json.Unmarshal(output, &msg)) + re.Equal("Delete service GC safepoint successfully.", msg) + } + + // do a second round of get safepoints to ensure that the safe points are indeed deleted + args = []string{"-u", pdAddr, "service-gc-safepoint"} + output, err = tests.ExecuteCommand(cmd, args...) + re.NoError(err) + + var ll api.ListServiceGCSafepoint + re.NoError(json.Unmarshal(output, &ll)) + + re.Equal(uint64(1), ll.GCSafePoint) + re.Equal(uint64(0), ll.MinServiceGcSafepoint) + re.Empty(ll.ServiceGCSafepoints) + + // // try delete the "gc_worker", should get an error + // args = []string{"-u", pdAddr, "service-gc-safepoint", "delete", "gc_worker"} + // output, err = tests.ExecuteCommand(cmd, args...) + // // re.Error(err) + // re.NoError(err) + // var msg string + // re.NoError(json.Unmarshal(output, &msg)) + // re.Equal("Delete service GC safepoint successfully.", msg) +} From 785e975bd0cfeaec90bb345fc6a7f1bc3ebba8b9 Mon Sep 17 00:00:00 2001 From: Boyang Lyu Date: Fri, 16 Aug 2024 00:50:06 +0800 Subject: [PATCH 30/35] add the delete gc_worker check Signed-off-by: Boyang Lyu --- tools/pd-ctl/tests/safepoint/safepoint_test.go | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/tools/pd-ctl/tests/safepoint/safepoint_test.go b/tools/pd-ctl/tests/safepoint/safepoint_test.go index 4bff76cbcaf..27aaebdaee5 100644 --- a/tools/pd-ctl/tests/safepoint/safepoint_test.go +++ b/tools/pd-ctl/tests/safepoint/safepoint_test.go @@ -120,12 +120,12 @@ func TestSafepoint(t *testing.T) { re.Equal(uint64(0), ll.MinServiceGcSafepoint) re.Empty(ll.ServiceGCSafepoints) - // // try delete the "gc_worker", should get an error - // args = []string{"-u", pdAddr, "service-gc-safepoint", "delete", "gc_worker"} - // output, err = tests.ExecuteCommand(cmd, args...) - // // re.Error(err) - // re.NoError(err) - // var msg string - // re.NoError(json.Unmarshal(output, &msg)) - // re.Equal("Delete service GC safepoint successfully.", msg) + // try delete the "gc_worker", should get an error + args = []string{"-u", pdAddr, "service-gc-safepoint", "delete", "gc_worker"} + output, err = tests.ExecuteCommand(cmd, args...) + re.NoError(err) + + // the following byte array, when translating to ascii, is : Failed to delete service GC safepoint: request pd http api failed with status: '500 Internal Server Error', body: '"cannot remove service safepoint of gc_worker"' + exp := []byte{0x46, 0x61, 0x69, 0x6c, 0x65, 0x64, 0x20, 0x74, 0x6f, 0x20, 0x64, 0x65, 0x6c, 0x65, 0x74, 0x65, 0x20, 0x73, 0x65, 0x72, 0x76, 0x69, 0x63, 0x65, 0x20, 0x47, 0x43, 0x20, 0x73, 0x61, 0x66, 0x65, 0x70, 0x6f, 0x69, 0x6e, 0x74, 0x3a, 0x20, 0x72, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x20, 0x70, 0x64, 0x20, 0x68, 0x74, 0x74, 0x70, 0x20, 0x61, 0x70, 0x69, 0x20, 0x66, 0x61, 0x69, 0x6c, 0x65, 0x64, 0x20, 0x77, 0x69, 0x74, 0x68, 0x20, 0x73, 0x74, 0x61, 0x74, 0x75, 0x73, 0x3a, 0x20, 0x27, 0x35, 0x30, 0x30, 0x20, 0x49, 0x6e, 0x74, 0x65, 0x72, 0x6e, 0x61, 0x6c, 0x20, 0x53, 0x65, 0x72, 0x76, 0x65, 0x72, 0x20, 0x45, 0x72, 0x72, 0x6f, 0x72, 0x27, 0x2c, 0x20, 0x62, 0x6f, 0x64, 0x79, 0x3a, 0x20, 0x27, 0x22, 0x63, 0x61, 0x6e, 0x6e, 0x6f, 0x74, 0x20, 0x72, 0x65, 0x6d, 0x6f, 0x76, 0x65, 0x20, 0x73, 0x65, 0x72, 0x76, 0x69, 0x63, 0x65, 0x20, 0x73, 0x61, 0x66, 0x65, 0x20, 0x70, 0x6f, 0x69, 0x6e, 0x74, 0x20, 0x6f, 0x66, 0x20, 0x67, 0x63, 0x5f, 0x77, 0x6f, 0x72, 0x6b, 0x65, 0x72, 0x22, 0x27, 0xa} + re.Equal(exp, output) } From f522d4502eecbcf0fd1eb8a23a4fd2e5dcf13538 Mon Sep 17 00:00:00 2001 From: Boyang Lyu Date: Fri, 16 Aug 2024 01:04:01 +0800 Subject: [PATCH 31/35] add a comment Signed-off-by: Boyang Lyu --- tools/pd-ctl/tests/safepoint/safepoint_test.go | 1 + 1 file changed, 1 insertion(+) diff --git a/tools/pd-ctl/tests/safepoint/safepoint_test.go b/tools/pd-ctl/tests/safepoint/safepoint_test.go index 27aaebdaee5..c201a6daf69 100644 --- a/tools/pd-ctl/tests/safepoint/safepoint_test.go +++ b/tools/pd-ctl/tests/safepoint/safepoint_test.go @@ -126,6 +126,7 @@ func TestSafepoint(t *testing.T) { re.NoError(err) // the following byte array, when translating to ascii, is : Failed to delete service GC safepoint: request pd http api failed with status: '500 Internal Server Error', body: '"cannot remove service safepoint of gc_worker"' + // unmarshall this array to string will fail due to the single/double quotatoin mark exp := []byte{0x46, 0x61, 0x69, 0x6c, 0x65, 0x64, 0x20, 0x74, 0x6f, 0x20, 0x64, 0x65, 0x6c, 0x65, 0x74, 0x65, 0x20, 0x73, 0x65, 0x72, 0x76, 0x69, 0x63, 0x65, 0x20, 0x47, 0x43, 0x20, 0x73, 0x61, 0x66, 0x65, 0x70, 0x6f, 0x69, 0x6e, 0x74, 0x3a, 0x20, 0x72, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x20, 0x70, 0x64, 0x20, 0x68, 0x74, 0x74, 0x70, 0x20, 0x61, 0x70, 0x69, 0x20, 0x66, 0x61, 0x69, 0x6c, 0x65, 0x64, 0x20, 0x77, 0x69, 0x74, 0x68, 0x20, 0x73, 0x74, 0x61, 0x74, 0x75, 0x73, 0x3a, 0x20, 0x27, 0x35, 0x30, 0x30, 0x20, 0x49, 0x6e, 0x74, 0x65, 0x72, 0x6e, 0x61, 0x6c, 0x20, 0x53, 0x65, 0x72, 0x76, 0x65, 0x72, 0x20, 0x45, 0x72, 0x72, 0x6f, 0x72, 0x27, 0x2c, 0x20, 0x62, 0x6f, 0x64, 0x79, 0x3a, 0x20, 0x27, 0x22, 0x63, 0x61, 0x6e, 0x6e, 0x6f, 0x74, 0x20, 0x72, 0x65, 0x6d, 0x6f, 0x76, 0x65, 0x20, 0x73, 0x65, 0x72, 0x76, 0x69, 0x63, 0x65, 0x20, 0x73, 0x61, 0x66, 0x65, 0x20, 0x70, 0x6f, 0x69, 0x6e, 0x74, 0x20, 0x6f, 0x66, 0x20, 0x67, 0x63, 0x5f, 0x77, 0x6f, 0x72, 0x6b, 0x65, 0x72, 0x22, 0x27, 0xa} re.Equal(exp, output) } From 6f4d633c430a4c3319175c77080bbdaa2bf1f7fe Mon Sep 17 00:00:00 2001 From: Boyang Lyu Date: Fri, 16 Aug 2024 13:58:14 +0800 Subject: [PATCH 32/35] modified the delete gc_worker test Signed-off-by: Boyang Lyu --- tools/pd-ctl/tests/safepoint/safepoint_test.go | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/tools/pd-ctl/tests/safepoint/safepoint_test.go b/tools/pd-ctl/tests/safepoint/safepoint_test.go index c201a6daf69..8fbe62f3741 100644 --- a/tools/pd-ctl/tests/safepoint/safepoint_test.go +++ b/tools/pd-ctl/tests/safepoint/safepoint_test.go @@ -120,13 +120,10 @@ func TestSafepoint(t *testing.T) { re.Equal(uint64(0), ll.MinServiceGcSafepoint) re.Empty(ll.ServiceGCSafepoints) - // try delete the "gc_worker", should get an error + // try delete the "gc_worker", should get an error message args = []string{"-u", pdAddr, "service-gc-safepoint", "delete", "gc_worker"} output, err = tests.ExecuteCommand(cmd, args...) re.NoError(err) - // the following byte array, when translating to ascii, is : Failed to delete service GC safepoint: request pd http api failed with status: '500 Internal Server Error', body: '"cannot remove service safepoint of gc_worker"' - // unmarshall this array to string will fail due to the single/double quotatoin mark - exp := []byte{0x46, 0x61, 0x69, 0x6c, 0x65, 0x64, 0x20, 0x74, 0x6f, 0x20, 0x64, 0x65, 0x6c, 0x65, 0x74, 0x65, 0x20, 0x73, 0x65, 0x72, 0x76, 0x69, 0x63, 0x65, 0x20, 0x47, 0x43, 0x20, 0x73, 0x61, 0x66, 0x65, 0x70, 0x6f, 0x69, 0x6e, 0x74, 0x3a, 0x20, 0x72, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x20, 0x70, 0x64, 0x20, 0x68, 0x74, 0x74, 0x70, 0x20, 0x61, 0x70, 0x69, 0x20, 0x66, 0x61, 0x69, 0x6c, 0x65, 0x64, 0x20, 0x77, 0x69, 0x74, 0x68, 0x20, 0x73, 0x74, 0x61, 0x74, 0x75, 0x73, 0x3a, 0x20, 0x27, 0x35, 0x30, 0x30, 0x20, 0x49, 0x6e, 0x74, 0x65, 0x72, 0x6e, 0x61, 0x6c, 0x20, 0x53, 0x65, 0x72, 0x76, 0x65, 0x72, 0x20, 0x45, 0x72, 0x72, 0x6f, 0x72, 0x27, 0x2c, 0x20, 0x62, 0x6f, 0x64, 0x79, 0x3a, 0x20, 0x27, 0x22, 0x63, 0x61, 0x6e, 0x6e, 0x6f, 0x74, 0x20, 0x72, 0x65, 0x6d, 0x6f, 0x76, 0x65, 0x20, 0x73, 0x65, 0x72, 0x76, 0x69, 0x63, 0x65, 0x20, 0x73, 0x61, 0x66, 0x65, 0x20, 0x70, 0x6f, 0x69, 0x6e, 0x74, 0x20, 0x6f, 0x66, 0x20, 0x67, 0x63, 0x5f, 0x77, 0x6f, 0x72, 0x6b, 0x65, 0x72, 0x22, 0x27, 0xa} - re.Equal(exp, output) + re.Equal("Failed to delete service GC safepoint: request pd http api failed with status: '500 Internal Server Error', body: '\"cannot remove service safe point of gc_worker\"'\n", string(output)) } From e3517c891eb0804144caef9a306c1aaf18569095 Mon Sep 17 00:00:00 2001 From: Boyang Lyu Date: Fri, 16 Aug 2024 14:07:25 +0800 Subject: [PATCH 33/35] run the tests again Signed-off-by: Boyang Lyu --- tools/pd-ctl/tests/safepoint/safepoint_test.go | 1 + 1 file changed, 1 insertion(+) diff --git a/tools/pd-ctl/tests/safepoint/safepoint_test.go b/tools/pd-ctl/tests/safepoint/safepoint_test.go index 8fbe62f3741..3a46e326e60 100644 --- a/tools/pd-ctl/tests/safepoint/safepoint_test.go +++ b/tools/pd-ctl/tests/safepoint/safepoint_test.go @@ -125,5 +125,6 @@ func TestSafepoint(t *testing.T) { output, err = tests.ExecuteCommand(cmd, args...) re.NoError(err) + // try again re.Equal("Failed to delete service GC safepoint: request pd http api failed with status: '500 Internal Server Error', body: '\"cannot remove service safe point of gc_worker\"'\n", string(output)) } From d25953b09b76442bed50a4b131bcb274218b7ca3 Mon Sep 17 00:00:00 2001 From: Boyang Lyu Date: Fri, 16 Aug 2024 14:15:18 +0800 Subject: [PATCH 34/35] add a comment Signed-off-by: Boyang Lyu --- tests/integrations/client/http_client_test.go | 4 ++-- tools/pd-ctl/tests/safepoint/safepoint_test.go | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/integrations/client/http_client_test.go b/tests/integrations/client/http_client_test.go index 982cb18f1d8..e80679290ea 100644 --- a/tests/integrations/client/http_client_test.go +++ b/tests/integrations/client/http_client_test.go @@ -899,8 +899,8 @@ func (suite *httpClientTestSuite) TestGetGCSafePoint() { re.Equal("Delete service GC safepoint successfully.", msg) } - _, err4 := client.DeleteGCSafePoint(ctx, "gc_worker") - re.Error(err4) + _, err = client.DeleteGCSafePoint(ctx, "gc_worker") + re.Error(err) // check that the safepoitns are indeed deleted l, err = client.GetGCSafePoint(ctx) diff --git a/tools/pd-ctl/tests/safepoint/safepoint_test.go b/tools/pd-ctl/tests/safepoint/safepoint_test.go index 3a46e326e60..4538a226fea 100644 --- a/tools/pd-ctl/tests/safepoint/safepoint_test.go +++ b/tools/pd-ctl/tests/safepoint/safepoint_test.go @@ -125,6 +125,6 @@ func TestSafepoint(t *testing.T) { output, err = tests.ExecuteCommand(cmd, args...) re.NoError(err) - // try again + // output should be an error message re.Equal("Failed to delete service GC safepoint: request pd http api failed with status: '500 Internal Server Error', body: '\"cannot remove service safe point of gc_worker\"'\n", string(output)) } From dded6c8a5a2d9599312315d3a05f992a025e8a14 Mon Sep 17 00:00:00 2001 From: Boyang Lyu Date: Wed, 21 Aug 2024 12:16:29 +0800 Subject: [PATCH 35/35] add delete non-exist safepoints tests Signed-off-by: Boyang Lyu --- client/http/types.go | 2 ++ tests/integrations/client/http_client_test.go | 13 ++++++++++--- tools/pd-ctl/tests/safepoint/safepoint_test.go | 10 +++++++++- 3 files changed, 21 insertions(+), 4 deletions(-) diff --git a/client/http/types.go b/client/http/types.go index a262d9c672c..4bc60978a0e 100644 --- a/client/http/types.go +++ b/client/http/types.go @@ -25,6 +25,7 @@ import ( pd "github.com/tikv/pd/client" ) +// ServiceSafePoint is the safepoint for a specific service // NOTE: This type is in sync with pd/pkg/storage/endpoint/gc_safe_point.go type ServiceSafePoint struct { ServiceID string `json:"service_id"` @@ -32,6 +33,7 @@ type ServiceSafePoint struct { SafePoint uint64 `json:"safe_point"` } +// ListServiceGCSafepoint is the response for list service GC safepoint. // NOTE: This type is in sync with pd/server/api/service_gc_safepoint.go type ListServiceGCSafepoint struct { ServiceGCSafepoints []*ServiceSafePoint `json:"service_gc_safe_points"` diff --git a/tests/integrations/client/http_client_test.go b/tests/integrations/client/http_client_test.go index e80679290ea..fe0962012e6 100644 --- a/tests/integrations/client/http_client_test.go +++ b/tests/integrations/client/http_client_test.go @@ -899,9 +899,6 @@ func (suite *httpClientTestSuite) TestGetGCSafePoint() { re.Equal("Delete service GC safepoint successfully.", msg) } - _, err = client.DeleteGCSafePoint(ctx, "gc_worker") - re.Error(err) - // check that the safepoitns are indeed deleted l, err = client.GetGCSafePoint(ctx) re.NoError(err) @@ -909,4 +906,14 @@ func (suite *httpClientTestSuite) TestGetGCSafePoint() { re.Equal(uint64(1), l.GCSafePoint) re.Equal(uint64(0), l.MinServiceGcSafepoint) re.Empty(l.ServiceGCSafepoints) + + // try delete gc_worker, should get an error + _, err = client.DeleteGCSafePoint(ctx, "gc_worker") + re.Error(err) + + // try delete some non-exist safepoints, should return normally + var msg string + msg, err = client.DeleteGCSafePoint(ctx, "non_exist") + re.NoError(err) + re.Equal("Delete service GC safepoint successfully.", msg) } diff --git a/tools/pd-ctl/tests/safepoint/safepoint_test.go b/tools/pd-ctl/tests/safepoint/safepoint_test.go index 4538a226fea..5551cce1fff 100644 --- a/tools/pd-ctl/tests/safepoint/safepoint_test.go +++ b/tools/pd-ctl/tests/safepoint/safepoint_test.go @@ -1,4 +1,4 @@ -// Copyright 2019 TiKV Project Authors. +// Copyright 2024 TiKV Project Authors. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. @@ -127,4 +127,12 @@ func TestSafepoint(t *testing.T) { // output should be an error message re.Equal("Failed to delete service GC safepoint: request pd http api failed with status: '500 Internal Server Error', body: '\"cannot remove service safe point of gc_worker\"'\n", string(output)) + + // try delete a non-exist safepoint, should return normally + args = []string{"-u", pdAddr, "service-gc-safepoint", "delete", "non_exist"} + output, err = tests.ExecuteCommand(cmd, args...) + re.NoError(err) + var msg string + re.NoError(json.Unmarshal(output, &msg)) + re.Equal("Delete service GC safepoint successfully.", msg) }