mcs: reject registration reusing another instance's address - #11043
mcs: reject registration reusing another instance's address#11043bufferflies wants to merge 1 commit into
Conversation
Claim the service registry key with an atomic create-if-absent transaction so that an instance advertising a duplicate address cannot overwrite the registry entry of another live instance and further join the primary election with the same identity. A stale entry left by a crashed instance expires with its lease, so the registration retries within the lease TTL before giving up. Signed-off-by: tongjian <1045931706@qq.com>
|
[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 |
📝 WalkthroughWalkthroughService registration now uses lease-backed conditional etcd transactions, rejects live duplicate addresses, retries occupied registrations within a TTL-based deadline, and revokes failed leases. Tests cover conflict rejection, lease expiry, and subsequent successful registration. ChangesService registration conflict handling
Estimated code review effort: 4 (Complex) | ~45 minutes Sequence Diagram(s)sequenceDiagram
participant ServiceRegister
participant Etcd
participant ServiceContext
ServiceRegister->>Etcd: Grant lease and conditionally claim registry key
Etcd-->>ServiceRegister: Return success or occupied error
ServiceRegister->>ServiceContext: Wait for retry interval or cancellation
ServiceRegister->>Etcd: Retry claim before TTL deadline
Etcd-->>ServiceRegister: Register after previous lease expires
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✨ 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 |
|
@bufferflies: The following test failed, say
Full PR test history. Your PR dashboard. DetailsInstructions 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. I understand the commands that are listed here. |
There was a problem hiding this comment.
🧹 Nitpick comments (1)
pkg/mcs/discovery/register_test.go (1)
109-111: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick winPrefer
errors.Isover substring matching for the sentinel error check.Since this is an internal test (same package), asserting
errors.Is(err, errServiceAddrOccupied)directly ties the test to the actual sentinel error instead of its message wording, which is more robust against future message-copy changes.♻️ Proposed refactor
err := sr2.Register() re.Error(err) - re.Contains(err.Error(), "occupied") + re.ErrorIs(err, errServiceAddrOccupied)🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@pkg/mcs/discovery/register_test.go` around lines 109 - 111, Update the Register test’s error assertion after sr2.Register() to use errors.Is with the errServiceAddrOccupied sentinel instead of checking for the "occupied" message substring, while retaining the existing error assertion.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Nitpick comments:
In `@pkg/mcs/discovery/register_test.go`:
- Around line 109-111: Update the Register test’s error assertion after
sr2.Register() to use errors.Is with the errServiceAddrOccupied sentinel instead
of checking for the "occupied" message substring, while retaining the existing
error assertion.
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro Plus
Run ID: b278b836-d3c2-4210-9736-a876500f995b
📒 Files selected for processing (2)
pkg/mcs/discovery/register.gopkg/mcs/discovery/register_test.go
What problem does this PR solve?
Issue Number: Close #11001, Close #10998
A microservice instance (TSO / Scheduling) started with an
--advertise-listen-addralready used by another live instance could blindly overwrite that instance's etcd registry entry and then join the primary election with the same participant identity.What is changed and how does it work?
Validate the registry key at registration time in
pkg/mcs/discovery:putWithTTLnow claims the registry key with an atomic create-if-absent etcd transaction instead of an unconditionalPut, so a duplicate advertised address can no longer overwrite another live instance's entry.Registerretries the claim within the lease TTL window, so a stale entry left by a crashed instance with the same address expires and the restart succeeds without manual intervention, while a genuine duplicate live instance keeps its lease refreshed and the orphan fails startup before ever entering the primary election (all MCS servers callRegisterbeforestartServer).Check List
Tests
TestRegisterConflictverifies a duplicate live registration is rejected without overwriting the existing entry, and that a new instance can register after the stale lease expires.tests/integrations/mcs/discoverypasses.Release note
🤖 Generated with Claude Code
Summary by CodeRabbit