From 098b802fcda75b5ae0527b0b50b95ba8b31403cf Mon Sep 17 00:00:00 2001 From: ystaticy Date: Sat, 14 Sep 2024 14:10:40 +0800 Subject: [PATCH] keyspace level gc: forbid update to ks level gc (#8062) ref tikv/pd#8061 Disables updating gc_management_type to keyspace_level_gc Signed-off-by: y_static_y@sina.com --- pkg/keyspace/keyspace.go | 7 +++++++ pkg/keyspace/util.go | 3 +++ server/apiv2/handlers/keyspace.go | 11 +++++++++++ tests/integrations/client/http_client_test.go | 14 ++++++++++++-- 4 files changed, 33 insertions(+), 2 deletions(-) diff --git a/pkg/keyspace/keyspace.go b/pkg/keyspace/keyspace.go index a338e929eb6..afb60d7bb3f 100644 --- a/pkg/keyspace/keyspace.go +++ b/pkg/keyspace/keyspace.go @@ -54,6 +54,13 @@ const ( // Note: Config[TSOKeyspaceGroupIDKey] is only used to judge whether there is keyspace group id. // It will not update the keyspace group id when merging or splitting. TSOKeyspaceGroupIDKey = "tso_keyspace_group_id" + + // If `gc_management_type` is `global_gc`, it means the current keyspace requires a tidb without 'keyspace-name' + // configured to run a global gc worker to calculate a global gc safe point. + // If `gc_management_type` is `keyspace_level_gc` it means the current keyspace can calculate gc safe point by its own. + GCManagementType = "gc_management_type" + // KeyspaceLevelGC is a type of gc_management_type used to indicate that this keyspace independently advances its own gc safe point. + KeyspaceLevelGC = "keyspace_level_gc" ) // Config is the interface for keyspace config. diff --git a/pkg/keyspace/util.go b/pkg/keyspace/util.go index ac7d7b20398..91d07676205 100644 --- a/pkg/keyspace/util.go +++ b/pkg/keyspace/util.go @@ -88,6 +88,9 @@ var ( ErrModifyDefaultKeyspace = errors.New("cannot modify default keyspace's state") errIllegalOperation = errors.New("unknown operation") + // ErrUnsupportedOperationInKeyspace is used to indicate this is an unsupported operation. + ErrUnsupportedOperationInKeyspace = errors.New("it's a unsupported operation") + // stateTransitionTable lists all allowed next state for the given current state. // Note that transit from any state to itself is allowed for idempotence. stateTransitionTable = map[keyspacepb.KeyspaceState][]keyspacepb.KeyspaceState{ diff --git a/server/apiv2/handlers/keyspace.go b/server/apiv2/handlers/keyspace.go index c2802bb939d..89d0634ce8f 100644 --- a/server/apiv2/handlers/keyspace.go +++ b/server/apiv2/handlers/keyspace.go @@ -285,6 +285,7 @@ func UpdateKeyspaceConfig(c *gin.Context) { c.AbortWithStatusJSON(http.StatusInternalServerError, managerUninitializedErr) return } + name := c.Param("name") configParams := &UpdateConfigParams{} err := c.BindJSON(configParams) @@ -293,6 +294,16 @@ func UpdateKeyspaceConfig(c *gin.Context) { return } mutations := getMutations(configParams.Config) + + // Check if the update is supported. + for _, mutation := range mutations { + if mutation.Key == keyspace.GCManagementType && mutation.Value == keyspace.KeyspaceLevelGC { + err = keyspace.ErrUnsupportedOperationInKeyspace + c.AbortWithStatusJSON(http.StatusInternalServerError, err.Error()) + return + } + } + meta, err := manager.UpdateKeyspaceConfig(name, mutations) if err != nil { c.AbortWithStatusJSON(http.StatusInternalServerError, err.Error()) diff --git a/tests/integrations/client/http_client_test.go b/tests/integrations/client/http_client_test.go index 68643d5f0c8..e0e09dd6bc5 100644 --- a/tests/integrations/client/http_client_test.go +++ b/tests/integrations/client/http_client_test.go @@ -35,6 +35,7 @@ import ( pd "github.com/tikv/pd/client/http" "github.com/tikv/pd/client/retry" "github.com/tikv/pd/pkg/core" + "github.com/tikv/pd/pkg/keyspace" sc "github.com/tikv/pd/pkg/schedule/config" "github.com/tikv/pd/pkg/schedule/labeler" "github.com/tikv/pd/pkg/schedule/placement" @@ -770,7 +771,7 @@ func (suite *httpClientTestSuite) TestUpdateKeyspaceGCManagementType() { defer cancel() keyspaceName := "DEFAULT" - expectGCManagementType := "keyspace_level_gc" + expectGCManagementType := "test-type" keyspaceSafePointVersionConfig := pd.KeyspaceGCManagementTypeConfig{ Config: pd.KeyspaceGCManagementType{ @@ -782,11 +783,20 @@ func (suite *httpClientTestSuite) TestUpdateKeyspaceGCManagementType() { keyspaceMetaRes, err := client.GetKeyspaceMetaByName(ctx, keyspaceName) re.NoError(err) - val, ok := keyspaceMetaRes.Config["gc_management_type"] + val, ok := keyspaceMetaRes.Config[keyspace.GCManagementType] // Check it can get expect key and value in keyspace meta config. re.True(ok) re.Equal(expectGCManagementType, val) + + // Check it doesn't support update config to keyspace.KeyspaceLevelGC now. + keyspaceSafePointVersionConfig = pd.KeyspaceGCManagementTypeConfig{ + Config: pd.KeyspaceGCManagementType{ + GCManagementType: keyspace.KeyspaceLevelGC, + }, + } + err = client.UpdateKeyspaceGCManagementType(suite.ctx, keyspaceName, &keyspaceSafePointVersionConfig) + re.Error(err) } func (suite *httpClientTestSuite) TestGetHealthStatus() {