-
Notifications
You must be signed in to change notification settings - Fork 764
resourcemanager: add metadata watcher scaffold #10259
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
Changes from all commits
d0e1aef
c766971
75b7550
9a4d049
98a1ded
4092c2e
e7a2d93
e5a7c7f
180747c
6909d07
3fcc9fc
a5f0a5b
a5e47c1
93d4d6e
1810868
52b345f
e73d960
561b8a0
64be26e
df1e2fc
66813f4
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 |
|---|---|---|
|
|
@@ -15,6 +15,7 @@ | |
| package server | ||
|
|
||
| import ( | ||
| "context" | ||
| "encoding/json" | ||
| "math" | ||
| "sort" | ||
|
|
@@ -23,11 +24,14 @@ import ( | |
| "github.com/gogo/protobuf/proto" | ||
| "go.uber.org/zap" | ||
|
|
||
| "github.com/pingcap/errors" | ||
| rmpb "github.com/pingcap/kvproto/pkg/resource_manager" | ||
| "github.com/pingcap/log" | ||
|
|
||
| "github.com/tikv/pd/pkg/errs" | ||
| "github.com/tikv/pd/pkg/storage/endpoint" | ||
| "github.com/tikv/pd/pkg/storage/kv" | ||
| "github.com/tikv/pd/pkg/utils/keypath" | ||
| "github.com/tikv/pd/pkg/utils/syncutil" | ||
| ) | ||
|
|
||
|
|
@@ -93,19 +97,87 @@ func newKeyspaceResourceGroupManager( | |
| } | ||
| } | ||
|
|
||
| func (krgm *keyspaceResourceGroupManager) addResourceGroupFromRaw(name string, rawValue string) error { | ||
| func (krgm *keyspaceResourceGroupManager) parseResourceGroupFromRaw(name, rawValue string) (*rmpb.ResourceGroup, error) { | ||
| group := &rmpb.ResourceGroup{} | ||
| if err := proto.Unmarshal([]byte(rawValue), group); err != nil { | ||
| log.Error("failed to parse the keyspace resource group meta info", | ||
| zap.Uint32("keyspace-id", krgm.keyspaceID), zap.String("name", name), zap.String("raw-value", rawValue), zap.Error(err)) | ||
| return nil, err | ||
| } | ||
| if group.Name != name { | ||
| err := errors.Errorf("resource group key name %s does not match payload name %s", name, group.Name) | ||
| log.Error("resource group name mismatch in storage payload", | ||
| zap.Uint32("keyspace-id", krgm.keyspaceID), | ||
| zap.String("raw-value", rawValue), | ||
| zap.Error(err)) | ||
| return nil, err | ||
| } | ||
| return group, nil | ||
| } | ||
|
|
||
| func validateResourceGroupProto(grouppb *rmpb.ResourceGroup) error { | ||
| if len(grouppb.Name) == 0 || len(grouppb.Name) > maxGroupNameLength { | ||
| return errs.ErrInvalidGroup.FastGenByArgs("the group name") | ||
| } | ||
| if grouppb.GetPriority() > maxPriority { | ||
| return errs.ErrInvalidGroup.FastGenByArgs("the group priority") | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| func (krgm *keyspaceResourceGroupManager) addResourceGroupFromRaw(name string, rawValue string) error { | ||
| group, err := krgm.parseResourceGroupFromRaw(name, rawValue) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| if err := validateResourceGroupProto(group); err != nil { | ||
| return err | ||
| } | ||
| resourceGroup := FromProtoResourceGroup(group) | ||
| krgm.Lock() | ||
| krgm.groups[group.Name] = resourceGroup | ||
| krgm.Unlock() | ||
| krgm.syncBurstabilityWithServiceLimit(resourceGroup) | ||
| return nil | ||
| } | ||
|
|
||
| func (krgm *keyspaceResourceGroupManager) upsertResourceGroupFromRaw(name string, rawValue string) error { | ||
| group, err := krgm.parseResourceGroupFromRaw(name, rawValue) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| if err := validateResourceGroupProto(group); err != nil { | ||
| return err | ||
| } | ||
|
|
||
| krgm.RLock() | ||
|
Contributor
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. It's better to wrap the curl for groups into two functions, such as groupExists and saveGroup? It makes the reader confused when to use the lock?
Member
Author
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. I kept this inline because the lock boundaries are the actual point here: we only need the manager map under Extracting helpers like |
||
| existing := krgm.groups[group.Name] | ||
| krgm.RUnlock() | ||
| if existing != nil { | ||
| if err := existing.ApplySettings(group); err != nil { | ||
| log.Error("failed to apply the keyspace resource group settings from raw value", | ||
| zap.Uint32("keyspace-id", krgm.keyspaceID), zap.String("name", name), zap.String("raw-value", rawValue), zap.Error(err)) | ||
| return err | ||
| } | ||
| krgm.syncBurstabilityWithServiceLimit(existing) | ||
| return nil | ||
| } | ||
|
|
||
| resourceGroup := FromProtoResourceGroup(group) | ||
| krgm.Lock() | ||
| krgm.groups[group.Name] = FromProtoResourceGroup(group) | ||
| krgm.groups[group.Name] = resourceGroup | ||
| krgm.Unlock() | ||
| krgm.syncBurstabilityWithServiceLimit(resourceGroup) | ||
| return nil | ||
| } | ||
|
|
||
| func (krgm *keyspaceResourceGroupManager) deleteResourceGroupFromCache(name string) { | ||
| krgm.Lock() | ||
| delete(krgm.groups, name) | ||
| delete(krgm.groupRUTrackers, name) | ||
| krgm.Unlock() | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
| } | ||
|
|
||
| func (krgm *keyspaceResourceGroupManager) setRawStatesIntoResourceGroup(name string, rawValue string) error { | ||
| tokens := &GroupStates{} | ||
| if err := json.Unmarshal([]byte(rawValue), tokens); err != nil { | ||
|
|
@@ -128,35 +200,60 @@ func (krgm *keyspaceResourceGroupManager) initDefaultResourceGroup() { | |
| if ok { | ||
| return | ||
| } | ||
| defaultGroup := &ResourceGroup{ | ||
| defaultGroup := newDefaultResourceGroup() | ||
| if err := krgm.addResourceGroup(defaultGroup.IntoProtoResourceGroup(krgm.keyspaceID)); err != nil { | ||
| log.Warn("init default group failed", zap.Uint32("keyspace-id", krgm.keyspaceID), zap.Error(err)) | ||
| } | ||
| } | ||
|
|
||
| func (krgm *keyspaceResourceGroupManager) ensureReservedDefaultGroupInCache() { | ||
| krgm.RLock() | ||
| _, ok := krgm.groups[DefaultResourceGroupName] | ||
| krgm.RUnlock() | ||
| if ok { | ||
| return | ||
| } | ||
| defaultGroup := newDefaultResourceGroup() | ||
| inserted := false | ||
| krgm.Lock() | ||
| if _, ok := krgm.groups[DefaultResourceGroupName]; !ok { | ||
| krgm.groups[DefaultResourceGroupName] = defaultGroup | ||
| inserted = true | ||
| } | ||
| krgm.Unlock() | ||
| if inserted { | ||
| krgm.syncBurstabilityWithServiceLimit(defaultGroup) | ||
| } | ||
| } | ||
|
|
||
| func newDefaultResourceGroup() *ResourceGroup { | ||
| return &ResourceGroup{ | ||
| Name: DefaultResourceGroupName, | ||
| Mode: rmpb.GroupMode_RUMode, | ||
| RUSettings: &RequestUnitSettings{ | ||
| RU: &GroupTokenBucket{ | ||
| Settings: &rmpb.TokenLimitSettings{ | ||
| FillRate: UnlimitedRate, | ||
| BurstLimit: UnlimitedBurstLimit, | ||
| }, | ||
| RUSettings: NewRequestUnitSettings(DefaultResourceGroupName, &rmpb.TokenBucket{ | ||
| Settings: &rmpb.TokenLimitSettings{ | ||
| FillRate: UnlimitedRate, | ||
| BurstLimit: UnlimitedBurstLimit, | ||
| }, | ||
| }, | ||
| Priority: middlePriority, | ||
| } | ||
| if err := krgm.addResourceGroup(defaultGroup.IntoProtoResourceGroup(krgm.keyspaceID)); err != nil { | ||
| log.Warn("init default group failed", zap.Uint32("keyspace-id", krgm.keyspaceID), zap.Error(err)) | ||
| }), | ||
| Priority: middlePriority, | ||
| RUConsumption: &rmpb.Consumption{}, | ||
| } | ||
| } | ||
|
|
||
| func (krgm *keyspaceResourceGroupManager) restoreDefaultResourceGroupFromReserved() { | ||
| defaultGroup := newDefaultResourceGroup() | ||
| krgm.Lock() | ||
| krgm.groups[DefaultResourceGroupName] = defaultGroup | ||
| krgm.Unlock() | ||
|
okJiang marked this conversation as resolved.
|
||
| krgm.syncBurstabilityWithServiceLimit(defaultGroup) | ||
| } | ||
|
|
||
| func (krgm *keyspaceResourceGroupManager) addResourceGroup(grouppb *rmpb.ResourceGroup) error { | ||
| if len(grouppb.Name) == 0 || len(grouppb.Name) > maxGroupNameLength { | ||
| return errs.ErrInvalidGroup | ||
| } | ||
| // Check the Priority. | ||
| if grouppb.GetPriority() > maxPriority { | ||
| return errs.ErrInvalidGroup | ||
| if err := validateResourceGroupProto(grouppb); err != nil { | ||
| return err | ||
| } | ||
| group := FromProtoResourceGroup(grouppb) | ||
| krgm.Lock() | ||
| defer krgm.Unlock() | ||
| if krgm.writeRole.AllowsMetadataWrite() { | ||
| if err := group.persistSettings(krgm.keyspaceID, krgm.storage); err != nil { | ||
| return err | ||
|
|
@@ -167,13 +264,16 @@ func (krgm *keyspaceResourceGroupManager) addResourceGroup(grouppb *rmpb.Resourc | |
| return err | ||
| } | ||
| } | ||
| krgm.Lock() | ||
| krgm.groups[group.Name] = group | ||
| krgm.Unlock() | ||
| krgm.syncBurstabilityWithServiceLimit(group) | ||
| return nil | ||
| } | ||
|
|
||
| func (krgm *keyspaceResourceGroupManager) modifyResourceGroup(group *rmpb.ResourceGroup) error { | ||
| if group == nil || group.Name == "" { | ||
| return errs.ErrInvalidGroup | ||
| return errs.ErrInvalidGroup.FastGenByArgs("the group name") | ||
| } | ||
| krgm.RLock() | ||
| curGroup, ok := krgm.groups[group.Name] | ||
|
|
@@ -204,12 +304,29 @@ func (krgm *keyspaceResourceGroupManager) deleteResourceGroup(name string) error | |
| if !krgm.writeRole.AllowsMetadataWrite() { | ||
| return errMetadataWriteDisabled | ||
| } | ||
| if err := krgm.storage.DeleteResourceGroupSetting(krgm.keyspaceID, name); err != nil { | ||
| return err | ||
| if txnStorage, ok := krgm.storage.(interface { | ||
| RunInTxn(context.Context, func(txn kv.Txn) error) error | ||
| }); ok { | ||
| if err := txnStorage.RunInTxn(context.Background(), func(txn kv.Txn) error { | ||
| if err := txn.Remove(keypath.KeyspaceResourceGroupSettingPath(krgm.keyspaceID, name)); err != nil { | ||
| return err | ||
| } | ||
| return txn.Remove(keypath.KeyspaceResourceGroupStatePath(krgm.keyspaceID, name)) | ||
|
Contributor
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. If this delete failed, do we need to revert the KeyspaceResourceGroupSettingPath?
Member
Author
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. I don't think reverting In the transactional path this cannot happen because both deletes are done in one txn. In the non-transactional fallback, once the settings key delete has succeeded the group is already gone from the authoritative metadata view; trying to recreate that settings key would be another non-transactional write and could repersist stale metadata instead of restoring the original state cleanly. |
||
| }); err != nil { | ||
| return err | ||
| } | ||
| } else { | ||
| if err := krgm.storage.DeleteResourceGroupSetting(krgm.keyspaceID, name); err != nil { | ||
| return err | ||
| } | ||
| if err := krgm.storage.DeleteResourceGroupStates(krgm.keyspaceID, name); err != nil { | ||
|
Contributor
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. If the error happened, don't return the error?
Member
Author
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. I intentionally don't return that states-delete error in the fallback path. At that point the settings key has already been deleted, so returning an error would make callers observe a failed delete even though the authoritative group metadata is already gone. The remaining states key is only orphaned cleanup data; logging a warning is useful, but surfacing it as the operation result would be misleading. |
||
| log.Warn("failed to delete resource group states after deleting settings", | ||
| zap.Uint32("keyspace-id", krgm.keyspaceID), | ||
| zap.String("name", name), | ||
| zap.Error(err)) | ||
| } | ||
| } | ||
| krgm.Lock() | ||
| delete(krgm.groups, name) | ||
| krgm.Unlock() | ||
| krgm.deleteResourceGroupFromCache(name) | ||
| return nil | ||
| } | ||
|
|
||
|
|
@@ -763,6 +880,19 @@ func (krgm *keyspaceResourceGroupManager) cleanupOverrides() { | |
| } | ||
| } | ||
|
|
||
| // Newly loaded groups can miss the initial service-limit replay, so apply the | ||
| // same baseline burst invalidation when they enter the cache. | ||
| func (krgm *keyspaceResourceGroupManager) syncBurstabilityWithServiceLimit(group *ResourceGroup) { | ||
| if group == nil || group.getBurstLimit(true) >= 0 || group.getOverrideBurstLimit() >= 0 { | ||
| return | ||
| } | ||
| serviceLimit, isSet := krgm.getServiceLimit() | ||
| if !isSet || serviceLimit <= 0 { | ||
| return | ||
| } | ||
| group.overrideBurstLimit(int64(serviceLimit)) | ||
| } | ||
|
|
||
| // Since the burstable resource groups won't require tokens from the server anymore, | ||
| // we have to override the burst limit of all the resource groups to the service limit. | ||
| // This ensures the burstability of the resource groups can be properly invalidated. | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.