Skip to content

Conversation

@lhy1024
Copy link
Contributor

@lhy1024 lhy1024 commented Dec 4, 2025

What problem does this PR solve?

Issue Number: Ref #9764

What is changed and how does it work?

  • Add base affinity models/state: group.go introduces Group/GroupState, availability/degraded/expired conditions, ID validation, and region affinity checks.
  • Manager skeleton: manager.go adds Manager with dual locking (metaMutex + RWMutex)

Check List

Tests

  • Unit test

Release note

None.

@ti-chi-bot ti-chi-bot bot added release-note-none Denotes a PR that doesn't merit a release note. dco-signoff: yes Indicates the PR's author has signed the dco. size/XXL Denotes a PR that changes 1000+ lines, ignoring generated files. labels Dec 4, 2025
Signed-off-by: lhy1024 <admin@liudos.us>
@codecov
Copy link

codecov bot commented Dec 4, 2025

Codecov Report

❌ Patch coverage is 14.89796% with 417 lines in your changes missing coverage. Please review.
✅ Project coverage is 78.07%. Comparing base (dadce5d) to head (b191733).
⚠️ Report is 7 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #9997      +/-   ##
==========================================
- Coverage   78.53%   78.07%   -0.47%     
==========================================
  Files         503      510       +7     
  Lines       67322    67900     +578     
==========================================
+ Hits        52874    53013     +139     
- Misses      10615    11045     +430     
- Partials     3833     3842       +9     
Flag Coverage Δ
unittests 78.07% <14.89%> (-0.47%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@lhy1024
Copy link
Contributor Author

lhy1024 commented Dec 5, 2025

@bufferflies @okJiang PTAL

}

//nolint:revive
func (m *Manager) UpdateAffinityGroupPeers(string, uint64, []uint64) (*GroupState, error) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
func (m *Manager) UpdateAffinityGroupPeers(string, uint64, []uint64) (*GroupState, error) {
func (*Manager) UpdateAffinityGroupPeers(string, uint64, []uint64) (*GroupState, error) {

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok, but it will be used really in another pr later.

if slice.HasDupInSorted(voterStoreIDs) {
return errs.ErrAffinityGroupContent.FastGenByArgs("duplicate voter store ID")
}
if !slices.Contains(voterStoreIDs, g.LeaderStoreID) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In generative speaking, the leader should belong to the voters. How about renaming voterStoreIDs as followerStoreIDs if you don't allow the leader to exist in this list.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, we now only return error when LeaderStoreID is not in voterStoreIDs

}
for _, storeID := range voterStoreIDs {
if m.storeSetInformer.GetStore(storeID) == nil {
return errs.ErrAffinityGroupContent.FastGenByArgs("store does not exist")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about informing the user that the store was not found?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok

// GroupKeyRanges represents key ranges with group id.
type GroupKeyRanges struct {
KeyRanges []keyutil.KeyRange
GroupID string
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about adding more to the format of this groupID?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or we add it in pd-ctl about group format, I have added it in pd-ctl. We don't limit the format in server, it should be a convention for pd-ctl and SQL.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The region label also does not conform to the server's specified format.

re.NoError(err)

group := &Group{
ID: "TEST-group_1",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

affinity_group_id = tidb_p_t

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it only for test

Signed-off-by: lhy1024 <admin@liudos.us>
@lhy1024 lhy1024 requested a review from bufferflies December 5, 2025 03:33
@ti-chi-bot ti-chi-bot bot added the needs-cherry-pick-release-8.5 Should cherry pick this PR to release-8.5 branch. label Dec 5, 2025
@okJiang
Copy link
Member

okJiang commented Dec 5, 2025

/cc @okJiang

@ti-chi-bot ti-chi-bot bot requested a review from okJiang December 5, 2025 07:34
)

// Phase is a status intended for API display
type Phase string
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moving Phase and condition to a new file is better for me.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it is not necessary for now. It will cause problems for subsequent code merges. Once all code has been merged, we can consider making further adjustments if necessary.

// AffinityVer initializes at 1 and increments by 1 each time the Group changes.
AffinityVer uint64
// AffinityRegionCount indicates how many Regions have all Voter and Leader peers in the correct stores. (AffinityVer equals).
AffinityRegionCount int
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is better.

Suggested change
AffinityRegionCount int
AffinitizedRegionCount int

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

affinity is also adj.

Comment on lines +215 to +218
// AffinityVer initializes at 1 and increments by 1 each time the Group changes.
AffinityVer uint64
// AffinityRegionCount indicates how many Regions have all Voter and Leader peers in the correct stores. (AffinityVer equals).
AffinityRegionCount int
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why exist two same fields? L171-L173

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

runtimeGroupInfo is struct for memory, GroupState is struct for api handler

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why was it designed this way?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about renaming to test_utils.go

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Files ending with _test will only be compiled during test.

Signed-off-by: lhy1024 <admin@liudos.us>
@ti-chi-bot ti-chi-bot bot added the needs-1-more-lgtm Indicates a PR needs 1 more LGTM. label Dec 8, 2025
@ti-chi-bot ti-chi-bot bot added lgtm approved and removed needs-1-more-lgtm Indicates a PR needs 1 more LGTM. labels Dec 8, 2025
@ti-chi-bot
Copy link
Contributor

ti-chi-bot bot commented Dec 8, 2025

[LGTM Timeline notifier]

Timeline:

  • 2025-12-08 02:09:40.88449881 +0000 UTC m=+834125.698276382: ☑️ agreed by okJiang.
  • 2025-12-08 04:44:02.344331637 +0000 UTC m=+843387.158109208: ☑️ agreed by bufferflies.

@lhy1024
Copy link
Contributor Author

lhy1024 commented Dec 8, 2025

@niubell PTAL

Copy link
Contributor

@niubell niubell left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The first PR to add the basic structures, will add the corresponding tests in next PRs.

@lhy1024
Copy link
Contributor Author

lhy1024 commented Dec 8, 2025

The first PR to add the basic structures, will add the corresponding tests in next PRs.

yes, they will be added in 9998 and 9999

@ti-chi-bot
Copy link
Contributor

ti-chi-bot bot commented Dec 8, 2025

@niubell: adding LGTM is restricted to approvers and reviewers in OWNERS files.

Details

In response to this:

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository.

@ti-chi-bot
Copy link
Contributor

ti-chi-bot bot commented Dec 8, 2025

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: bufferflies, niubell, okJiang

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@lhy1024
Copy link
Contributor Author

lhy1024 commented Dec 8, 2025

/test pull-error-log-review

@ti-chi-bot ti-chi-bot bot merged commit 23550eb into tikv:master Dec 8, 2025
38 of 42 checks passed
@ti-chi-bot
Copy link
Member

In response to a cherrypick label: new pull request created to branch release-8.5: #10015.

case storeDown:
return "down"
case storeRemovingOrRemoved:
return "removing-or-removed"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

prefer to split them

}

// IsAffinitySchedulingEnabled indicates whether affinity scheduling is allowed.
func (g *runtimeGroupInfo) IsAffinitySchedulingEnabled() bool {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The name is the same as IsAffinitySchedulingEnabled in the config, but the meaning is different.

HunDunDM pushed a commit to HunDunDM/pd that referenced this pull request Dec 11, 2025
ref tikv#9764

Signed-off-by: lhy1024 <admin@liudos.us>
lhy1024 added a commit to lhy1024/pd that referenced this pull request Dec 14, 2025
ref tikv#9764

Signed-off-by: lhy1024 <admin@liudos.us>
lhy1024 added a commit to lhy1024/pd that referenced this pull request Dec 16, 2025
ref tikv#9764

Signed-off-by: lhy1024 <admin@liudos.us>
lhy1024 added a commit to lhy1024/pd that referenced this pull request Dec 16, 2025
ref tikv#9764

Signed-off-by: lhy1024 <admin@liudos.us>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

approved dco-signoff: yes Indicates the PR's author has signed the dco. lgtm needs-cherry-pick-release-8.5 Should cherry pick this PR to release-8.5 branch. release-note-none Denotes a PR that doesn't merit a release note. size/XXL Denotes a PR that changes 1000+ lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants