Skip to content

Raft: reject stale proposals against uncommitted shard epochs - #5593

Open
quanyeyang wants to merge 9 commits into
valkey-io:cluster-v2from
quanyeyang:raft-append-time-epoch-tracking
Open

quanyeyang wants to merge 9 commits into
valkey-io:cluster-v2from
quanyeyang:raft-append-time-epoch-tracking

Conversation

@quanyeyang

@quanyeyang quanyeyang commented Nov 25, 2026 •

Copy link
Copy Markdown
Member

Closes #3874.

#3899 checks proposals against the applied shard epoch. If two FAILOVER proposals for the same shard arrive before the first one applies, both can pass that check and enter the log.

The leader now accounts for unapplied FAILOVER, SET_REPLICA_OF, and NODE_FORGET entries when checking a proposal’s shard epoch. In the example above, it rejects the second FAILOVER before appending it. SLOT_CHANGE checks the epoch without advancing it. Apply-time validation against the applied epoch remains authoritative.

Shard-epoch conflicts now use the retry index introduced in #4456. A REJECT conflict tells the proposer which log index to apply before refreshing the epoch and retrying.

  • A reconnect before that index is applied can resend the original proposal without consuming an epoch retry. Waiting for an in-flight voter change also preserves the epoch retry budget.
  • Until a new leader applies its election no-op, it uses that entry’s index as the minimum retry index. This prevents a proposer that has already applied an inherited entry from exhausting its retries while the leader catches up.
  • A proposal can retry at the same epoch if the conflicting entry is skipped. A retried NODE_FORGET uses the target node’s current shard epoch, even if the node moved to a shard with a lower epoch.

Protocol and cluster tests cover the two-FAILOVER case, retries across reconnects and elections, and CLUSTER FORGET during voter changes and shard moves.

@coderabbitai

coderabbitai Bot commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

Important

Review skipped

Auto reviews are disabled on base/target branches other than the default branch.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Repository UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 327468ae-2762-4656-a6a1-449eb2cefce9

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • Trigger review
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

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.

Share

Comment thread src/cluster_raft.c Resolved
@valkey-review-bot

valkey-review-bot Bot commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

Review state

Not ready for human review yet.
The duplicate NODE_FORGET timeout is fixed: a deferred FORGET for a node that is already gone is now resent and answered as UNCHANGED. The new deferred skip in clusterRaftCompletePendingProposal also applies on apply. A FAILOVER that is resent across a leader change and deferred can then be reported as rejected after it succeeded.

Risk: Medium. Changes which raft proposals reach the log and how pending proposals get completed on cluster-v2. A mistake turns successful FORGET or FAILOVER into client errors or timeouts.

Open findings (1)

  • src/cluster_raft.c:1896 Skipping deferred proposals on apply (index != 0) keeps a deferred FAILOVER from being completed by its own applied entry after a leader-change resend. The refreshed retry is then rejected and the client gets an error for a failover that succeeded.

Resolved (2)

  • src/cluster_raft.c:1154 Leader-local proposals rejected by the new append-time check get an immediate error from clusterRaftPropose instead of being deferred and retried, which regresses the documented stale-epoch retry.
  • src/cluster_raft.c:4708 A second NODE_FORGET for the same node in one commit window is deferred, never resent after the node is gone, and times out instead of returning OK.

Backport: Not needed. Changes the raft cluster protocol, which exists only on the cluster-v2 feature branch and not on any release line.

Updated for 79d69ac at 2026-11-27 07:07 UTC. The bot rewrites this comment on every review.

Comment thread src/cluster_raft.c Resolved
@codecov

codecov Bot commented Nov 25, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 93.93939% with 8 lines in your changes missing coverage. Please review.
✅ Project coverage is 81.69%. Comparing base (a5f92d9) to head (256a5a8).

Files with missing lines Patch % Lines
src/cluster_raft.c 93.93% 8 Missing ⚠️
Additional details and impacted files
@@              Coverage Diff               @@
##           cluster-v2    #5593      +/-   ##
==============================================
+ Coverage       81.61%   81.69%   +0.07%     
==============================================
  Files             203      203              
  Lines          108494   108502       +8     
==============================================
+ Hits            88548    88639      +91     
+ Misses          19946    19863      -83     
Files with missing lines Coverage Δ
src/cluster_raft.c 88.66% <93.93%> (+2.24%) ⬆️

... and 23 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Pre-validation compared only the applied epoch, so two FAILOVERs in
the same commit window both entered the log. Scan the uncommitted
suffix so the second is REJECTED before append.

Signed-off-by: quanyeyang <quanyemostima@gmail.com>
Pre-validation on the leader completed the client before the
proposal was pending, so a stale epoch was not retried. Track it
first so CompletePendingProposal can defer until the epoch advances.

Signed-off-by: quanyeyang <quanyemostima@gmail.com>
When two clients propose NODE_FORGET for the same node before the first
entry commits, the append-time epoch check defers one proposal. Once the
node is gone, allow that proposal to retry as an idempotent no-op. Skip
deferred proposals when matching results so each client gets a reply.

Add a regression test that holds the first entry uncommitted and verifies
that both clients receive OK.

Signed-off-by: quanyeyang <quanyemostima@gmail.com>
@quanyeyang
quanyeyang force-pushed the raft-append-time-epoch-tracking branch from 478936d to 79d69ac Compare November 27, 2026 06:59
Comment thread src/cluster_raft.c Resolved
Let an applied entry complete a deferred proposal when its resend was
rejected. This lets FAILOVER return OK when the replica was promoted.

Retry at the same shard epoch if the predicted bump was skipped on
apply or lost on a leader change. This prevents a valid ADDSLOTS from
timing out after a concurrent FORGET fails.

Add regression tests for both cases.

Signed-off-by: quanyeyang <quanyemostima@gmail.com>
@zuiderkwast

Copy link
Copy Markdown
Contributor

#4456 adds "retry-after index" which is a very nice mechanism. I think we should use it for shard-epoch conflicts too. I posted a comment on #4456. Maybe you can update this PR when that's merged?

@quanyeyang

Copy link
Copy Markdown
Member Author

#4456 adds "retry-after index" which is a very nice mechanism. I think we should use it for shard-epoch conflicts too. I posted a comment on #4456. Maybe you can update this PR when that's merged?

That's good, of course we can merge #4456 first.

…ime-epoch-tracking

Signed-off-by: quanyeyang <quanyemostima@gmail.com>
Comment thread src/cluster_raft.c Resolved
Comment thread src/cluster_raft.c Resolved
@bandalgomsu

Copy link
Copy Markdown
Contributor

While reviewing this, I found a pre-existing issue. I'll follow up on it separately in #5751 !

…ime-epoch-tracking

Signed-off-by: quanyeyang <quanyemostima@gmail.com>
Count an epoch retry only after the proposer applies the retry-after
index. Repeated reconnects could exhaust the retry budget and return
an error to the client even though the original FAILOVER later succeeded.

When retrying NODE_FORGET, use the epoch of the target node's current
shard. A node can move to a shard with a lower epoch, leaving the
original request stale on every retry.

Add regression coverage for both cases.

Signed-off-by: quanyeyang <quanyemostima@gmail.com>
A proposer may have already applied a conflicting entry that the new
leader has yet to apply. Returning that entry's index can exhaust the
proposal's retries before the leader catches up.

Use the election no-op's index as the minimum retry-after index until
the new leader applies it.

Signed-off-by: quanyeyang <quanyemostima@gmail.com>
A CLUSTER FORGET queued behind other voter changes could exhaust its
retry budget and report a stale shard epoch even when the epoch had
never changed.

Keep using the same retry index for both waits. Use the REJECT reason
to charge the retry budget only for stale epoch conflicts.

Signed-off-by: quanyeyang <quanyemostima@gmail.com>

@bandalgomsu bandalgomsu left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

lgtm! Could you update the description to cover the latest changes ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants