-
Notifications
You must be signed in to change notification settings - Fork 774
server: clean up microservice metadata in PD mode #10996
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
Open
rleungx
wants to merge
5
commits into
tikv:master
Choose a base branch
from
rleungx:fix-pd-mode-switch-cleanup
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+883
−0
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
256f11d
server: clean up microservice metadata in PD mode
rleungx e3fe646
server: make mode cleanup safe and non-blocking
rleungx 3509b60
server: simplify mode cleanup flow
rleungx ac16bb4
server: skip cleanup outside mode transitions
rleungx ee70648
server: protect microservice metadata cleanup
rleungx File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,300 @@ | ||
| // Copyright 2026 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 server | ||
|
|
||
| import ( | ||
| "context" | ||
| goerrors "errors" | ||
| "fmt" | ||
| "strconv" | ||
| "time" | ||
|
|
||
| clientv3 "go.etcd.io/etcd/client/v3" | ||
| "go.uber.org/zap" | ||
|
|
||
| "github.com/pingcap/errors" | ||
| "github.com/pingcap/failpoint" | ||
| "github.com/pingcap/kvproto/pkg/keyspacepb" | ||
| "github.com/pingcap/log" | ||
|
|
||
| "github.com/tikv/pd/pkg/errs" | ||
| "github.com/tikv/pd/pkg/keyspace" | ||
| "github.com/tikv/pd/pkg/keyspace/constant" | ||
| mcs "github.com/tikv/pd/pkg/mcs/utils/constant" | ||
| "github.com/tikv/pd/pkg/storage/kv" | ||
| "github.com/tikv/pd/pkg/utils/etcdutil" | ||
| "github.com/tikv/pd/pkg/utils/keypath" | ||
| "github.com/tikv/pd/pkg/utils/logutil" | ||
| ) | ||
|
|
||
| const ( | ||
| microserviceMetadataCleanupRetryInterval = 5 * time.Second | ||
| // Each loaded key adds one comparison and each updated key adds one write to | ||
| // the etcd transaction. Use half of the operation limit so a full batch stays | ||
| // within the transaction limit. | ||
| microserviceMetadataCleanupBatchSize = etcdutil.MaxEtcdTxnOps / 2 | ||
| ) | ||
|
|
||
| var errMicroserviceMetadataCleanupRejected = errors.New("microservice metadata cleanup rejected") | ||
|
|
||
| func rejectMicroserviceMetadataCleanup(format string, args ...any) error { | ||
| return errors.Wrapf(errMicroserviceMetadataCleanupRejected, format, args...) | ||
| } | ||
|
|
||
| func (s *Server) scheduleMicroserviceMetadataCleanup(ctx context.Context) { | ||
| if s.IsKeyspaceGroupEnabled() { | ||
| return | ||
| } | ||
| s.serverLoopWg.Add(1) | ||
| go func() { | ||
| defer logutil.LogPanic() | ||
| defer s.serverLoopWg.Done() | ||
| for { | ||
| if s.member != nil && !s.member.IsServing() { | ||
| return | ||
| } | ||
| err := s.cleanupMicroserviceMetadataInPDMode(ctx) | ||
| if err == nil { | ||
| return | ||
| } | ||
| if goerrors.Is(err, errMicroserviceMetadataCleanupRejected) { | ||
| log.Warn("cannot safely clean up microservice metadata in PD mode", | ||
| errs.ZapError(err)) | ||
| return | ||
| } | ||
| log.Warn("failed to clean up microservice metadata in PD mode, retry later", | ||
| errs.ZapError(err)) | ||
| timer := time.NewTimer(microserviceMetadataCleanupRetryInterval) | ||
| select { | ||
| case <-ctx.Done(): | ||
| timer.Stop() | ||
| return | ||
| case <-timer.C: | ||
| } | ||
| } | ||
| }() | ||
| } | ||
|
|
||
| func (s *Server) cleanupMicroserviceMetadataInPDMode(ctx context.Context) error { | ||
| if s.IsKeyspaceGroupEnabled() { | ||
| return nil | ||
| } | ||
| start := time.Now() | ||
|
|
||
| needsCleanup, err := s.validateMicroserviceMetadataCleanup(ctx) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| if !needsCleanup { | ||
| return nil | ||
| } | ||
| cleanedKeyspaces, err := s.cleanupDefaultTSOKeyspaceGroupConfig(ctx) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| deletedDefaultGroup, err := s.deleteDefaultTSOKeyspaceGroup(ctx) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| failpoint.InjectCall("beforeDeleteMicroserviceEtcdKeys") | ||
| if _, err := s.checkMicroserviceEtcdKeysNotLeased(ctx); err != nil { | ||
| return err | ||
| } | ||
| deletedMicroserviceKeys, err := s.deleteMicroserviceEtcdKeys(ctx) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| if deletedDefaultGroup || cleanedKeyspaces > 0 || deletedMicroserviceKeys > 0 { | ||
| log.Info("cleaned up microservice metadata in PD mode", | ||
| zap.Bool("deleted-default-keyspace-group", deletedDefaultGroup), | ||
| zap.Int("cleaned-keyspace-configs", cleanedKeyspaces), | ||
| zap.Int64("deleted-microservice-keys", deletedMicroserviceKeys), | ||
| zap.Duration("cost", time.Since(start))) | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| func (s *Server) checkMicroserviceEtcdKeysNotLeased(ctx context.Context) (bool, error) { | ||
| if s.client == nil { | ||
| return false, nil | ||
| } | ||
| getCtx, cancel := context.WithTimeout(ctx, etcdutil.DefaultRequestTimeout) | ||
| defer cancel() | ||
| resp, err := s.client.Get(getCtx, microserviceEtcdPrefix(), clientv3.WithPrefix(), clientv3.WithKeysOnly()) | ||
| if err != nil { | ||
| return false, errs.ErrEtcdKVGet.Wrap(err).GenWithStackByCause() | ||
| } | ||
| for _, item := range resp.Kvs { | ||
| if item.Lease != 0 { | ||
| return true, errors.Errorf("microservice key %q is still leased", string(item.Key)) | ||
| } | ||
| } | ||
| return len(resp.Kvs) > 0, nil | ||
| } | ||
|
|
||
| func (s *Server) validateMicroserviceMetadataCleanup(ctx context.Context) (bool, error) { | ||
| microserviceKeysExist, err := s.checkMicroserviceEtcdKeysNotLeased(ctx) | ||
| if err != nil { | ||
| return false, err | ||
| } | ||
| groups, err := s.storage.LoadKeyspaceGroups(constant.DefaultKeyspaceGroupID, 0) | ||
| if err != nil { | ||
| return false, err | ||
| } | ||
| needsCleanup := microserviceKeysExist | ||
| for _, group := range groups { | ||
| if group == nil { | ||
| continue | ||
| } | ||
| needsCleanup = true | ||
| if group.ID != constant.DefaultKeyspaceGroupID { | ||
| return false, rejectMicroserviceMetadataCleanup("found non-default TSO keyspace group %d when cleaning up PD mode microservice metadata", group.ID) | ||
| } | ||
| if group.IsSplitting() { | ||
|
Member
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. nit: the splitting/merging rejection paths (both here and in |
||
| return false, rejectMicroserviceMetadataCleanup("default TSO keyspace group is splitting when cleaning up PD mode microservice metadata") | ||
| } | ||
| if group.IsMerging() { | ||
| return false, rejectMicroserviceMetadataCleanup("default TSO keyspace group is merging when cleaning up PD mode microservice metadata") | ||
| } | ||
| } | ||
| if !needsCleanup { | ||
| return false, nil | ||
| } | ||
| err = s.scanKeyspaceMetadata(ctx, etcdutil.MaxEtcdTxnOps, func(_ kv.Txn, metas []*keyspacepb.KeyspaceMeta) error { | ||
| for _, meta := range metas { | ||
| if meta == nil { | ||
| continue | ||
| } | ||
| if err := validateDefaultTSOKeyspaceGroupAssignment(meta.GetId(), meta.GetConfig()); err != nil { | ||
| return err | ||
| } | ||
| } | ||
| return nil | ||
| }) | ||
| return true, err | ||
| } | ||
|
|
||
| func (s *Server) cleanupDefaultTSOKeyspaceGroupConfig(ctx context.Context) (int, error) { | ||
| cleaned := 0 | ||
| err := s.scanKeyspaceMetadata(ctx, microserviceMetadataCleanupBatchSize, func(txn kv.Txn, metas []*keyspacepb.KeyspaceMeta) error { | ||
| for _, meta := range metas { | ||
| if meta == nil || meta.Config == nil { | ||
| continue | ||
| } | ||
| if _, ok := meta.Config[keyspace.TSOKeyspaceGroupIDKey]; !ok { | ||
| continue | ||
| } | ||
| if err := validateDefaultTSOKeyspaceGroupAssignment(meta.GetId(), meta.GetConfig()); err != nil { | ||
| return err | ||
| } | ||
| delete(meta.Config, keyspace.TSOKeyspaceGroupIDKey) | ||
| if err := s.storage.SaveKeyspaceMeta(txn, meta); err != nil { | ||
| return err | ||
| } | ||
| cleaned++ | ||
| } | ||
| return nil | ||
| }) | ||
| return cleaned, err | ||
| } | ||
|
|
||
| func (s *Server) scanKeyspaceMetadata( | ||
| ctx context.Context, | ||
| batchSize int, | ||
| process func(kv.Txn, []*keyspacepb.KeyspaceMeta) error, | ||
| ) error { | ||
| startID := constant.StartKeyspaceID | ||
| for { | ||
| var metas []*keyspacepb.KeyspaceMeta | ||
| err := s.storage.RunInTxn(ctx, func(txn kv.Txn) error { | ||
| var err error | ||
| metas, err = s.storage.LoadRangeKeyspace(txn, startID, batchSize) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| return process(txn, metas) | ||
| }) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| if len(metas) < batchSize { | ||
| return nil | ||
| } | ||
| lastMeta := metas[len(metas)-1] | ||
| if lastMeta == nil { | ||
| return nil | ||
| } | ||
| lastID := lastMeta.GetId() | ||
| if lastID == ^uint32(0) { | ||
| return nil | ||
| } | ||
| startID = lastID + 1 | ||
| } | ||
| } | ||
|
|
||
| func validateDefaultTSOKeyspaceGroupAssignment(keyspaceID uint32, config map[string]string) error { | ||
| groupIDText, ok := config[keyspace.TSOKeyspaceGroupIDKey] | ||
| if !ok { | ||
| return nil | ||
| } | ||
| groupID, err := strconv.ParseUint(groupIDText, 10, 32) | ||
| if err != nil { | ||
| return rejectMicroserviceMetadataCleanup("keyspace %d has invalid TSO keyspace group ID %q", keyspaceID, groupIDText) | ||
| } | ||
| if groupID != uint64(constant.DefaultKeyspaceGroupID) { | ||
| return rejectMicroserviceMetadataCleanup("keyspace %d is assigned to non-default TSO keyspace group %d", keyspaceID, groupID) | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| func (s *Server) deleteDefaultTSOKeyspaceGroup(ctx context.Context) (bool, error) { | ||
| deleted := false | ||
| err := s.storage.RunInTxn(ctx, func(txn kv.Txn) error { | ||
| group, err := s.storage.LoadKeyspaceGroup(txn, constant.DefaultKeyspaceGroupID) | ||
| if err != nil || group == nil { | ||
| return err | ||
| } | ||
| if group.IsSplitting() { | ||
| return rejectMicroserviceMetadataCleanup("default TSO keyspace group is splitting when deleting PD mode microservice metadata") | ||
| } | ||
| if group.IsMerging() { | ||
| return rejectMicroserviceMetadataCleanup("default TSO keyspace group is merging when deleting PD mode microservice metadata") | ||
| } | ||
| if err := s.storage.DeleteKeyspaceGroup(txn, constant.DefaultKeyspaceGroupID); err != nil { | ||
| return err | ||
| } | ||
| deleted = true | ||
| return nil | ||
| }) | ||
| return deleted, err | ||
| } | ||
|
|
||
| func (s *Server) deleteMicroserviceEtcdKeys(ctx context.Context) (int64, error) { | ||
| if s.client == nil { | ||
| return 0, nil | ||
| } | ||
| ctx, cancel := context.WithTimeout(ctx, etcdutil.DefaultRequestTimeout) | ||
| defer cancel() | ||
| resp, err := s.client.Delete(ctx, microserviceEtcdPrefix(), clientv3.WithPrefix()) | ||
| if err != nil { | ||
| return 0, errs.ErrEtcdKVDelete.Wrap(err).GenWithStackByCause() | ||
| } | ||
| return resp.Deleted, nil | ||
| } | ||
|
|
||
| func microserviceEtcdPrefix() string { | ||
| return fmt.Sprintf("%s/%d/", mcs.MicroserviceRootPath, keypath.ClusterID()) | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
[blocking] The lease recheck and prefix deletion still have a TOCTOU window.
checkMicroserviceEtcdKeysNotLeasedanddeleteMicroserviceEtcdKeysare separate etcd requests. A TSO can register a leased key after this check returns and before the unconditionalWithPrefixdelete executes. The delete removes that new registry key without revoking its lease;ServiceRegisterkeeps receiving KeepAlive responses and therefore does not recreate the key, leaving a live service undiscoverable until restart.Please avoid deleting the whole prefix based on an earlier observation. One safe shape is to list the unleased keys, then delete only those exact keys in bounded transactions with revision/lease comparisons. A key created after the list must not be included.
The existing
beforeDeleteMicroserviceEtcdKeystest injects registration before the second check, so it does not cover this remaining window. Add a hook immediately after the check and verify that a newly registered leased key survives:This test fails with the current prefix delete even though the two existing lease tests pass.