Skip to content

Commit

Permalink
keyspace level gc: forbid update to ks level gc (#8062)
Browse files Browse the repository at this point in the history
ref #8061

Disables updating gc_management_type to keyspace_level_gc

Signed-off-by: [email protected] <[email protected]>
  • Loading branch information
ystaticy committed Sep 14, 2024
1 parent eb03382 commit 098b802
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 2 deletions.
7 changes: 7 additions & 0 deletions pkg/keyspace/keyspace.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
3 changes: 3 additions & 0 deletions pkg/keyspace/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down
11 changes: 11 additions & 0 deletions server/apiv2/handlers/keyspace.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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())
Expand Down
14 changes: 12 additions & 2 deletions tests/integrations/client/http_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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{
Expand All @@ -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() {
Expand Down

0 comments on commit 098b802

Please sign in to comment.