-
Notifications
You must be signed in to change notification settings - Fork 727
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
*: define user kind for keyspace group #6241
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||
---|---|---|---|---|
|
@@ -46,6 +46,10 @@ const ( | |||
regionLabelIDPrefix = "keyspaces/" | ||||
// regionLabelKey is the key for keyspace id in keyspace region label. | ||||
regionLabelKey = "id" | ||||
// UserKindKey is the key for user kind in keyspace config. | ||||
UserKindKey = "user_kind" | ||||
// TSOKeyspaceGroupIDKey is the key for tso keyspace group id in keyspace config. | ||||
TSOKeyspaceGroupIDKey = "tso_keyspace_group_id" | ||||
) | ||||
|
||||
// Config is the interface for keyspace config. | ||||
|
@@ -68,6 +72,7 @@ type Manager struct { | |||
ctx context.Context | ||||
// config is the configurations of the manager. | ||||
config Config | ||||
kgm *GroupManager | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This manager has the same name as: pd/pkg/tso/keyspace_group_manager.go Line 57 in 9fcde12
I strongly feel like we should unify them or give this keyspace group storage manager another name. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. They are two different things. And any suggestion for the name? |
||||
} | ||||
|
||||
// CreateKeyspaceRequest represents necessary arguments to create a keyspace. | ||||
|
@@ -85,6 +90,7 @@ func NewKeyspaceManager(store endpoint.KeyspaceStorage, | |||
cluster schedule.Cluster, | ||||
idAllocator id.Allocator, | ||||
config Config, | ||||
kgm *GroupManager, | ||||
) *Manager { | ||||
return &Manager{ | ||||
metaLock: syncutil.NewLockGroup(syncutil.WithHash(keyspaceIDHash)), | ||||
|
@@ -93,6 +99,7 @@ func NewKeyspaceManager(store endpoint.KeyspaceStorage, | |||
cluster: cluster, | ||||
ctx: context.TODO(), | ||||
config: config, | ||||
kgm: kgm, | ||||
} | ||||
} | ||||
|
||||
|
@@ -103,14 +110,22 @@ func (manager *Manager) Bootstrap() error { | |||
return err | ||||
} | ||||
now := time.Now().Unix() | ||||
id, err := manager.kgm.GetAvailableKeyspaceGroupIDByKind(endpoint.Basic) | ||||
if err != nil { | ||||
return err | ||||
} | ||||
defaultKeyspace := &keyspacepb.KeyspaceMeta{ | ||||
Id: DefaultKeyspaceID, | ||||
Name: DefaultKeyspaceName, | ||||
State: keyspacepb.KeyspaceState_ENABLED, | ||||
CreatedAt: now, | ||||
StateChangedAt: now, | ||||
Config: map[string]string{ | ||||
UserKindKey: endpoint.Basic.String(), | ||||
TSOKeyspaceGroupIDKey: id, | ||||
}, | ||||
} | ||||
err := manager.saveNewKeyspace(defaultKeyspace) | ||||
err = manager.saveNewKeyspace(defaultKeyspace) | ||||
// It's possible that default keyspace already exists in the storage (e.g. PD restart/recover), | ||||
// so we ignore the keyspaceExists error. | ||||
if err != nil && err != ErrKeyspaceExists { | ||||
|
@@ -120,9 +135,17 @@ func (manager *Manager) Bootstrap() error { | |||
// Initialize pre-alloc keyspace. | ||||
preAlloc := manager.config.GetPreAlloc() | ||||
for _, keyspaceName := range preAlloc { | ||||
id, err := manager.kgm.GetAvailableKeyspaceGroupIDByKind(endpoint.Basic) | ||||
if err != nil { | ||||
return err | ||||
} | ||||
_, err = manager.CreateKeyspace(&CreateKeyspaceRequest{ | ||||
Name: keyspaceName, | ||||
Now: now, | ||||
Config: map[string]string{ | ||||
UserKindKey: endpoint.Basic.String(), | ||||
TSOKeyspaceGroupIDKey: id, | ||||
}, | ||||
}) | ||||
// Ignore the keyspaceExists error for the same reason as saving default keyspace. | ||||
if err != nil && err != ErrKeyspaceExists { | ||||
|
@@ -148,6 +171,16 @@ func (manager *Manager) CreateKeyspace(request *CreateKeyspaceRequest) (*keyspac | |||
if err != nil { | ||||
return nil, err | ||||
} | ||||
userKind := endpoint.StringUserKind(request.Config[UserKindKey]) | ||||
id, err := manager.kgm.GetAvailableKeyspaceGroupIDByKind(userKind) | ||||
if err != nil { | ||||
return nil, err | ||||
} | ||||
if request.Config == nil { | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why do we do the nil check right after we try to get a key from it at line 174? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The nil map can be accessed but cannot be assigned. |
||||
request.Config = make(map[string]string) | ||||
} | ||||
request.Config[TSOKeyspaceGroupIDKey] = id | ||||
request.Config[UserKindKey] = userKind.String() | ||||
// Create and save keyspace metadata. | ||||
keyspace := &keyspacepb.KeyspaceMeta{ | ||||
Id: newID, | ||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,6 +22,44 @@ import ( | |
"go.etcd.io/etcd/clientv3" | ||
) | ||
|
||
// UserKind represents the user kind. | ||
type UserKind int | ||
|
||
// Different user kinds. | ||
const ( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. shall we define it as enum in keyspace.proto then we get all the following functions for free? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good question, we need a discussion about it. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. From my perspective, it's ok to keep it for now since we don't know if the current definition meets the future requirement. It's easier to modify compared with using proto. |
||
Basic UserKind = iota | ||
Standard | ||
Enterprise | ||
|
||
UserKindCount | ||
) | ||
|
||
// StringUserKind creates a UserKind with string. | ||
func StringUserKind(input string) UserKind { | ||
switch input { | ||
case Basic.String(): | ||
return Basic | ||
case Standard.String(): | ||
return Standard | ||
case Enterprise.String(): | ||
return Enterprise | ||
default: | ||
return Basic | ||
} | ||
} | ||
|
||
func (k UserKind) String() string { | ||
switch k { | ||
case Basic: | ||
return "basic" | ||
case Standard: | ||
return "standard" | ||
case Enterprise: | ||
return "enterprise" | ||
} | ||
return "unknown UserKind" | ||
} | ||
|
||
// KeyspaceGroupMember defines an election member which campaigns for the primary of the keyspace group. | ||
type KeyspaceGroupMember struct { | ||
Address string `json:"address"` | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Suggest reflecting the hierarchy from the naming, so maybe
KeyspaceGroupIDTSOKey
is a better name.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
But it sounds strange. lol