-
Notifications
You must be signed in to change notification settings - Fork 522
feat(store): support server labels for store #6638
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
feat(store): support server labels for store #6638
Conversation
Signed-off-by: liubo02 <[email protected]>
4b91662 to
8b847c4
Compare
Codecov Report❌ Patch coverage is Additional details and impacted files@@ Coverage Diff @@
## main #6638 +/- ##
==========================================
+ Coverage 39.29% 39.33% +0.04%
==========================================
Files 351 351
Lines 20692 20665 -27
==========================================
- Hits 8130 8129 -1
+ Misses 12562 12536 -26
Flags with carried forward coverage won't be shown. Click here to find out more. 🚀 New features to boost your workflow:
|
|
/test pull-e2e |
Signed-off-by: liubo02 <[email protected]>
0d23a9a to
2605f58
Compare
Signed-off-by: liubo02 <[email protected]>
Signed-off-by: liubo02 <[email protected]>
|
/test pull-e2e |
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.
Pull request overview
This PR adds support for server labels on store components (TiKV and TiFlash), extending the existing server labels functionality that was previously available only for TiDB and TiProxy. The implementation refactors the label management to use a common pattern while preserving component-specific behaviors.
Key changes include:
- Addition of
Labelsfield to TiKV and TiFlashServerstructs with CRD validation - Refactoring of PD client state management to support lazy initialization and caching
- Introduction of a
Containsutility function for map subset validation - Unification of server labels task implementation across all components
Reviewed changes
Copilot reviewed 65 out of 65 changed files in this pull request and generated 5 comments.
Show a summary per file
| File | Description |
|---|---|
pkg/utils/map/map.go |
Adds Contains function to check if one map contains all key-value pairs from another |
pkg/state/pd_client.go |
Refactors PD client state to support lazy initialization with cluster context |
pkg/controllers/common/task.go |
Refactors TaskServerLabels to accept PD client manager parameter and removes component-specific logic |
pkg/controllers/tikv/tasks/store_labels.go |
Replaces custom implementation with common TaskServerLabels, uses Contains for comparison |
pkg/controllers/tiflash/tasks/store_labels.go |
Replaces custom implementation with common TaskServerLabels, uses Contains for comparison |
pkg/controllers/tidb/builder.go |
Updates to use refactored TaskServerLabels with compare-before-set logic |
pkg/controllers/tiproxy/builder.go |
Updates to use refactored TaskServerLabels signature |
pkg/pdapi/v1/client.go |
Changes SetStoreLabels signature from uint64 to string for storeID consistency |
pkg/tidbapi/v1/client.go |
Refactors SetServerLabels return handling and adds Labels field to ServerInfo |
pkg/tiproxyapi/v1/client.go |
Removes trailing slash from config API path |
pkg/runtime/zz_generated.runtime.*.go |
Adds ServerLabels() method to all instance types (generated code) |
api/core/v1alpha1/tikv_types.go |
Adds Labels field to TiKVServer struct with validation |
api/core/v1alpha1/tiflash_types.go |
Adds Labels field to TiFlashServer struct with validation |
api/core/v1alpha1/tidb_types.go |
Updates documentation for Labels field behavior |
api/core/v1alpha1/tiproxy_types.go |
Updates documentation for Labels field behavior |
manifests/crd/*.yaml |
Updates CRD manifests with Labels field definitions and documentation |
cmd/runtime-gen/generators/*.go |
Updates code generator to handle ServerLabels field mapping |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| func Contains[K comparable, V comparable](a, b map[K]V) bool { | ||
| for k, v := range b { | ||
| if a[k] != v { | ||
| return false | ||
| } | ||
| } |
Copilot
AI
Jan 6, 2026
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.
The Contains function checks if map 'a' contains all key-value pairs from map 'b', but it doesn't verify that the key exists in 'a' before comparing values. When a key from 'b' doesn't exist in 'a', accessing a[k] returns the zero value for the value type, which may incorrectly match if the value in 'b' is also the zero value. This could lead to false positives where the function returns true even when 'a' doesn't actually contain all elements from 'b'.
pkg/utils/map/map.go
Outdated
| return ret | ||
| } | ||
|
|
||
| // Contains check whether map a contains all elem in map b |
Copilot
AI
Jan 6, 2026
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.
The comment has a grammatical error. It should say "checks whether" instead of "check whether".
| } | ||
|
|
||
| // SetServerLabels will set labels of tidb server. | ||
| // TODO(liubo02): now this function call is not safe because labels maybe changed by others after get info is called. |
Copilot
AI
Jan 6, 2026
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.
The comment contains a grammatical error. "maybe" should be "may be" (two words).
| Operator will set these `labels` by API. | ||
| If a label in this field is conflict with the config file, this field takes precedence. | ||
| NOTE: If a label is removed, operator will not delete it automatically. | ||
| NOTE: these label keys are managed by TiDB Operator, it will be set automatically and you can not modify them: |
Copilot
AI
Jan 6, 2026
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.
The documentation comment says "NOTE: If a label is removed, operator will not delete it automatically" but this behavior is inconsistent across components. For TiKV and TiFlash, the Contains check only verifies that expected labels are present but doesn't remove extra labels. However, for TiDB the comment says "Different from other components, TiDB will replace all labels but not only add/update", which suggests TiDB does delete removed labels. This inconsistency should be clarified in the documentation.
| Operator will set these `labels` by API. | |
| If a label in this field is conflict with the config file, this field takes precedence. | |
| NOTE: If a label is removed, operator will not delete it automatically. | |
| NOTE: these label keys are managed by TiDB Operator, it will be set automatically and you can not modify them: | |
| TiDB Operator will set these labels via the TiProxy API. | |
| If a label in this field conflicts with the config file, this field takes precedence. | |
| TiDB Operator only adds or updates the labels specified here; it does not remove any existing | |
| labels on the TiProxy server. Removing a label key from this field will NOT delete that label | |
| from TiProxy automatically. | |
| NOTE: The following label keys are managed automatically by TiDB Operator. They are set by the | |
| operator and cannot be modified in this field: |
pkg/utils/map/map.go
Outdated
| // Contains check whether map a contains all elem in map b | ||
| func Contains[K comparable, V comparable](a, b map[K]V) bool { | ||
| for k, v := range b { | ||
| if a[k] != v { | ||
| return false | ||
| } | ||
| } | ||
|
|
||
| return true | ||
| } |
Copilot
AI
Jan 6, 2026
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.
The newly introduced Contains function lacks test coverage. Given that this function is used in critical label synchronization logic for TiKV and TiFlash stores, it should have comprehensive test coverage to ensure it correctly validates that map 'a' contains all key-value pairs from map 'b'.
| if zoneLabel := findZoneLabel(pdCfg); zoneLabel != "" { | ||
| serverLabels[dcLabel] = serverLabels[zoneLabel] | ||
| } |
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.
why remove this
| func findZoneLabel(cfg *pdapi.PDConfigFromAPI) string { | ||
| for _, zl := range topologyZoneLabels { | ||
| if slices.Contains(cfg.Replication.LocationLabels, zl) { | ||
| return zl | ||
| } | ||
| } | ||
| return "" | ||
| } |
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.
why remove this
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.
useless? It's for compatibility
pkg/controllers/tidb/builder.go
Outdated
| return state.TiDBClient.SetServerLabels(ctx, labels) | ||
| info, err := state.TiDBClient.GetInfo(ctx) | ||
| if err != nil { | ||
| return err |
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.
format the error with fmt.Errorf?
| state.StoreLabels = make([]*metapb.StoreLabel, len(s.Labels)) | ||
| for k, v := range s.Labels { | ||
| state.StoreLabels = append(state.StoreLabels, &metapb.StoreLabel{Key: k, Value: v}) | ||
| } | ||
|
|
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.
why remove this
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.
use state.Store.Labels directly
| tasks.TaskPod(state, r.Client), | ||
| common.TaskServerLabels[scope.TiProxy](state, r.Client, func(ctx context.Context, labels map[string]string) error { | ||
| common.TaskServerLabels[scope.TiProxy](state, r.Client, r.PDClientManager, func(ctx context.Context, labels map[string]string) error { | ||
| // TODO(liubo02): compare before setting |
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.
why not do it in this PR
Signed-off-by: liubo02 <[email protected]>
Signed-off-by: liubo02 <[email protected]>
[LGTM Timeline notifier]Timeline:
|
|
[APPROVALNOTIFIER] This PR is APPROVED This pull-request has been approved by: fgksgf The full list of commands accepted by this bot can be found here. The pull request process is described here DetailsNeeds approval from an approver in each of these files:
Approvers can indicate their approval by writing |
Uh oh!
There was an error while loading. Please reload this page.