-
Notifications
You must be signed in to change notification settings - Fork 753
affinity: use isRegionPlacementRuleSatisfiedWithBestLocation instead of IsRegionReplicated #10192
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
base: master
Are you sure you want to change the base?
Conversation
|
Skipping CI for Draft Pull Request. |
|
[APPROVALNOTIFIER] This PR is NOT APPROVED This pull-request has been approved by: The full list of commands accepted by this bot can be found here. DetailsNeeds approval from an approver in each of these files:Approvers can indicate their approval by writing |
📝 WalkthroughWalkthroughAdds placement-rule awareness to affinity checking and replica strategy: new RuleManager usage, placement-rule satisfaction checks, better-location logic, witness-aware helpers, exported RuleFit Stores, RegionFit no-cache API, region-label isolation API changes, metric addition, and corresponding tests. Changes
Sequence DiagramsequenceDiagram
participant AC as AffinityChecker
participant RM as RuleManager
participant RF as RegionFit
participant RS as ReplicaStrategy
participant CL as Cluster
AC->>RM: FitRegion / FitRegionWithoutCache(region)
RM->>RF: build RegionFit (stores, rules)
RM-->>AC: return RegionFit
AC->>AC: isRegionPlacementRuleSatisfiedWithBestLocation(region, isReal)
AC->>RS: strategy(region, rule, fastFailover)
RS-->>AC: ReplicaStrategy
AC->>RS: getBetterLocation(ruleFit, region, fastFailover)
RS-->>AC: (oldStoreID, newStoreID, filteredByTempState)
AC->>CL: consult witness/cluster info
AC-->>AC: decide satisfied / block / schedule
Estimated Code Review Effort🎯 4 (Complex) | ⏱️ ~45 minutes Suggested Labels
Suggested Reviewers
Poem
🚥 Pre-merge checks | ✅ 2 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (2 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing touches
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
Codecov Report❌ Patch coverage is Additional details and impacted files@@ Coverage Diff @@
## master #10192 +/- ##
=======================================
Coverage 78.59% 78.59%
=======================================
Files 520 520
Lines 70014 70080 +66
=======================================
+ Hits 55028 55082 +54
- Misses 11008 11017 +9
- Partials 3978 3981 +3
Flags with carried forward coverage won't be shown. Click here to find out more. 🚀 New features to boost your workflow:
|
lhy1024
left a comment
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.
I think the way is OK, but we need some tests for it.
| isWitness := rf.Rule.IsWitness && isWitnessEnabled(c.cluster) | ||
| // If the peer to be moved is a witness, since no snapshot is needed, we also reuse the fast failover logic. | ||
| strategy := c.strategy(region, rf.Rule, isWitness) |
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.
maybe we need not to consider witness
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.
This is for compatibility reasons, as the original code was retained.
Signed-off-by: HunDunDM <hundundm@gmail.com>
Signed-off-by: HunDunDM <hundundm@gmail.com>
Signed-off-by: HunDunDM <hundundm@gmail.com>
8929d83 to
7243c75
Compare
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.
Actionable comments posted: 2
🤖 Fix all issues with AI agents
In `@pkg/schedule/checker/affinity_checker.go`:
- Around line 499-533: In isRegionPlacementRuleSatisfiedWithBestLocation, guard
against a nil RegionFit returned by ruleManager by checking if fit == nil
immediately after assigning it (both for FitRegion and FitRegionWithoutCache)
and return false if nil; this prevents dereferencing fit later in the function
(e.g., when calling fit.IsSatisfied() or iterating fit.RuleFits).
In `@pkg/schedule/placement/fit.go`:
- Around line 141-143: The exported field Stores in the RuleFit struct lacks a
JSON tag and should follow the existing kebab-case tagging pattern; update the
Stores field declaration in RuleFit to include json:"stores,omitempty" (i.e.,
add the explicit tag to the Stores []*core.StoreInfo field) so it matches other
exported fields like Rule, Peers, and IsolationScore.
| // Stores is the stores that the peers are placed in. | ||
| Stores []*core.StoreInfo | ||
| } |
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.
🧩 Analysis chain
🏁 Script executed:
# First, let's examine the struct in fit.go around lines 141-143
cat -n pkg/schedule/placement/fit.go | sed -n '130,160p'Repository: tikv/pd
Length of output: 1302
🏁 Script executed:
# Find the full struct definition
sed -n '130,170p' pkg/schedule/placement/fit.goRepository: tikv/pd
Length of output: 1394
🏁 Script executed:
# Search for where this struct is used in JSON marshaling/unmarshaling
rg -n "json\." pkg/schedule/placement/fit.goRepository: tikv/pd
Length of output: 33
🏁 Script executed:
# Check for JSON usage patterns with this struct across the codebase
rg -n "FitResult" --type go | head -20Repository: tikv/pd
Length of output: 33
🏁 Script executed:
# Check other structs in fit.go for JSON tag patterns
rg -B2 -A2 'json:' pkg/schedule/placement/fit.goRepository: tikv/pd
Length of output: 1032
🏁 Script executed:
# Search for where RuleFit is marshaled to JSON or used in API responses
rg -n "RuleFit" --type go -A 3 | grep -E "(marshal|json|response|http)" | head -20Repository: tikv/pd
Length of output: 346
🏁 Script executed:
# Check if there are any API endpoints that return RuleFit
rg -n "Marshal|json\.Marshal" pkg/schedule/placement/fit.goRepository: tikv/pd
Length of output: 33
🏁 Script executed:
# Search for API handlers or response functions that use RuleFit
rg -l "RuleFit" --type go | xargs grep -l "json\.Marshal\|api\|handler\|response" | head -10Repository: tikv/pd
Length of output: 205
🏁 Script executed:
# Check the API test to see how RuleFit is used in JSON responses
grep -n "RuleFit\|Stores" tests/server/api/rule_test.go | head -30Repository: tikv/pd
Length of output: 139
🏁 Script executed:
# Check the rule_manager to understand RuleFit usage
grep -n "RuleFit" pkg/schedule/placement/rule_manager.go -A 3 | head -40Repository: tikv/pd
Length of output: 660
🏁 Script executed:
# Search for any place where Stores field is actually used
rg -n "\.Stores" pkg/schedule/placement/ --type goRepository: tikv/pd
Length of output: 247
Add an explicit JSON tag for Stores to match struct export pattern.
The Stores field is exported but lacks a JSON tag, while all other exported fields in RuleFit have explicit tags following kebab-case convention (json:"rule", json:"peers", json:"isolation-score", etc.). Add json:"stores,omitempty" to maintain consistency.
🧩 Proposed change
// Stores is the stores that the peers are placed in.
Stores []*core.StoreInfo `json:"stores,omitempty"`📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
| // Stores is the stores that the peers are placed in. | |
| Stores []*core.StoreInfo | |
| } | |
| // Stores is the stores that the peers are placed in. | |
| Stores []*core.StoreInfo `json:"stores,omitempty"` | |
| } |
🤖 Prompt for AI Agents
In `@pkg/schedule/placement/fit.go` around lines 141 - 143, The exported field
Stores in the RuleFit struct lacks a JSON tag and should follow the existing
kebab-case tagging pattern; update the Stores field declaration in RuleFit to
include json:"stores,omitempty" (i.e., add the explicit tag to the Stores
[]*core.StoreInfo field) so it matches other exported fields like Rule, Peers,
and IsolationScore.
|
/retest-required |
What problem does this PR solve?
Issue Number: Ref #10149
What is changed and how does it work?
Check List
Tests
Related changes
Release note
Summary by CodeRabbit
New Features
Bug Fixes
Metrics
Tests
✏️ Tip: You can customize this high-level summary in your review settings.