Skip to content

Speed up split-vote elections with the new FAILOVER_AUTH_NACK message - #3833

Merged
enjoy-binbin merged 15 commits into
valkey-io:unstablefrom
enjoy-binbin:nack
Sep 1, 2026
Merged

enjoy-binbin merged 15 commits into
valkey-io:unstablefrom
enjoy-binbin:nack

Conversation

@enjoy-binbin

@enjoy-binbin enjoy-binbin commented May 26, 2026 •

Copy link
Copy Markdown
Member

In the current failover protocol, a replica sends one AUTH_REQUEST per epoch and
each voter casts at most one vote per epoch. Despite the various delay heuristics
in clusterHandleReplicaFailover that try to stagger the replicas, concurrent
elections can still collide on the same epoch. When the vote is split and nobody
reaches the quorum, the losing replica cannot learn this in time: it must first
wait for the election to be declared expired after auth_timeout (2 ×
cluster-node-timeout), and then wait another auth_retry_time (2 × auth_timeout)
before it is allowed to start the next election with a higher epoch.

This PR introduces a new message type, FAILOVER_AUTH_NACK, that a voter replies
with in every rejection branch (NOT_SAFE, REQ_EPOCH_OLD, ALREADY_VOTED,
REQ_IS_PRIMARY, NO_PRIMARY, PRIMARY_UP, STALE_CONFIG). The replica counts the
NACKs it receives; since a voter never answers twice in the same epoch, the most
votes it could still gather is bounded by the voters that have not NACKed. Once
that bound drops below the quorum, the election is declared unwinnable and a new
one is started immediately with a higher epoch, shrinking recovery from the
auth_timeout + auth_retry_time window down to a few cron ticks.

A voting primary that is itself FAIL can never reply (it neither ACKs nor NACKs)
yet is still counted in cluster->size, so it is excluded from this bound: the
achievable votes are size - size_fail - nack_count, where size_fail is the
number of FAIL voting primaries maintained in clusterUpdateState(). Without this,
a split vote among the replicas of several primaries that went down together
could never push nack_count high enough, and the replica would fall back to the
slow auth_timeout path. The quorum itself is still computed from the full size,
since a failover must be authorized by a majority of the whole configuration.

Wire compatibility is preserved by gating NACK sending behind a new capability
flag, CLUSTER_NODE_FAILOVER_AUTH_NACK_SUPPORTED, advertised in the PING/PONG
flags via clusterUpdateMyselfFlags. Nodes that do not advertise this capability
will never see the new message type and fall back to the legacy auth_timeout path.

Granted (ACK) and rejected (NACK) votes are logged symmetrically per epoch, so
the vote tally of an election can be reconstructed from the logs.

Two DEBUG hooks are added for testing:

  • DEBUG CLUSTER-FAILOVER-DELAY : override the failover delay computed in
    clusterHandleReplicaFailover.
  • DEBUG CLUSTER-FAILOVER-EPOCH : force the next election started by this
    replica to run in the given epoch (consumed once, then back to currentEpoch+1),
    so several replicas can be made to contend in the same epoch and deterministically
    reproduce a split vote.

In the current failover protocol a replica sends AUTH_REQUEST exactly
once per epoch and each voter casts at most one vote per epoch. Despite
the various delay heuristics in clusterHandleReplicaFailover that try to
stagger replicas, concurrent elections can still collide on the same
epoch. When the votes split and nobody reaches the quorum, the losing
replica has no way to learn this in time and must first wait for the
election to be declared expired after auth_timeout (2*cluster-node-timeout)
and then wait another auth_retry_time (2*auth_timeout) before it is even
allowed to start the next election with a higher epoch.

Introduce a new message type, FAILOVER_AUTH_NACK, that voters reply with
from every refusal branch. The replica counts incoming NACKs; since a
voter never re-answers within the same epoch, once size - nack_count
drops below the quorum the election is declared unwinnable and a new
one is started with a higher epoch right away, shrinking recovery from
the auth_timeout + auth_retry_time window to a few cron ticks.

Wire compatibility is preserved by gating NACK emission on a new
CLUSTER_NODE_FAILOVER_AUTH_NACK_SUPPORTED capability flag advertised in
PING/PONG flags via clusterUpdateMyselfFlags. Peers that do not advertise
the capability never see the new message type and fall back to the
legacy auth_timeout path.

Adding a new DEBUG CLUSTER-FAILOVER-DELAY <ms> hook overrides the delay
computed in clusterHandleReplicaFailover for testing.

Signed-off-by: Binbin <[email protected]>
@enjoy-binbin

enjoy-binbin commented May 26, 2026 •

Copy link
Copy Markdown
Member Author

This is an example without the auth_time reset (just print the logs but not reset failover_auth_time):

one replica

81935:S 26 May 2026 20:46:15.061 * Start of election delayed for 0 milliseconds (rank #0, primary rank #2, offset 14).
81935:S 26 May 2026 20:46:15.061 * Starting a failover election for epoch 11, node config epoch is 1
81935:S 26 May 2026 20:46:15.084 * Currently unable to failover: Waiting for votes, but majority still not reached.
81935:S 26 May 2026 20:46:15.089 * Needed quorum: 4. Number of votes received so far: 0
81935:S 26 May 2026 20:46:15.102 # Failover auth NACK [already-voted] from 168a74f5255a32672cb114838466aae7c284ace1 (R4) for epoch 11 (NACKs 1/7)
81935:S 26 May 2026 20:46:15.103 # Failover auth NACK [already-voted] from ee52b4158d5a1e80165ba6e0eff0a1ef2ce7bfb3 (R3) for epoch 11 (NACKs 2/7)
81935:S 26 May 2026 20:46:15.103 # Failover auth NACK [already-voted] from 584c617a6cf7d4ba3d7842732d107b2c0fdcfcb8 (R5) for epoch 11 (NACKs 3/7)
81935:S 26 May 2026 20:46:15.113 # Failover auth NACK [already-voted] from f2166e8b732feb4a59a6db075247692e4b6b8d4a (R6) for epoch 11 (NACKs 4/7)
81935:S 26 May 2026 20:46:15.113 # Failover election for epoch 11 cannot reach quorum 4 (NACKs 4/7). Resetting the election since we cannot win an election without quorum.
81935:S 26 May 2026 20:46:16.002 * Currently unable to failover: Waiting for votes, but majority still not reached.
81935:S 26 May 2026 20:46:16.002 * Needed quorum: 4. Number of votes received so far: 0
81935:S 26 May 2026 20:46:17.010 * Currently unable to failover: Waiting for votes, but majority still not reached.
81935:S 26 May 2026 20:46:17.010 * Needed quorum: 4. Number of votes received so far: 0
81935:S 26 May 2026 20:46:18.021 * Currently unable to failover: Waiting for votes, but majority still not reached.
81935:S 26 May 2026 20:46:18.021 * Needed quorum: 4. Number of votes received so far: 0
81935:S 26 May 2026 20:46:19.032 * Currently unable to failover: Waiting for votes, but majority still not reached.
81935:S 26 May 2026 20:46:19.032 * Needed quorum: 4. Number of votes received so far: 0
81935:S 26 May 2026 20:46:20.043 * Currently unable to failover: Waiting for votes, but majority still not reached.
81935:S 26 May 2026 20:46:20.043 * Needed quorum: 4. Number of votes received so far: 0
81935:S 26 May 2026 20:46:21.053 * Currently unable to failover: Waiting for votes, but majority still not reached.
81935:S 26 May 2026 20:46:21.053 * Needed quorum: 4. Number of votes received so far: 0
81935:S 26 May 2026 20:46:21.155 * Currently unable to failover: Failover attempt expired.
81935:S 26 May 2026 20:46:21.155 * Needed quorum: 4. Number of votes received so far: 0
81935:S 26 May 2026 20:46:22.064 * Currently unable to failover: Failover attempt expired.
81935:S 26 May 2026 20:46:22.064 * Needed quorum: 4. Number of votes received so far: 0
81935:S 26 May 2026 20:46:23.074 * Currently unable to failover: Failover attempt expired.
81935:S 26 May 2026 20:46:23.074 * Needed quorum: 4. Number of votes received so far: 0
81935:S 26 May 2026 20:46:24.085 * Currently unable to failover: Failover attempt expired.
81935:S 26 May 2026 20:46:24.085 * Needed quorum: 4. Number of votes received so far: 0
81935:S 26 May 2026 20:46:25.093 * Currently unable to failover: Failover attempt expired.
81935:S 26 May 2026 20:46:25.093 * Needed quorum: 4. Number of votes received so far: 0
81935:S 26 May 2026 20:46:26.007 * Currently unable to failover: Failover attempt expired.
81935:S 26 May 2026 20:46:26.008 * Needed quorum: 4. Number of votes received so far: 0
81935:S 26 May 2026 20:46:27.017 * Currently unable to failover: Failover attempt expired.
81935:S 26 May 2026 20:46:27.018 * Needed quorum: 4. Number of votes received so far: 0
81935:S 26 May 2026 20:46:27.119 * Start of election delayed for 0 milliseconds (rank #0, primary rank #2, offset 14).
81935:S 26 May 2026 20:46:27.120 * Starting a failover election for epoch 13, node config epoch is 1
81935:S 26 May 2026 20:46:27.155 * Reconfiguring node 960f1889400efd7256c34b27a69db68a0e37e3ea (R9) as primary for shard 6d42ca63697f3e23acff992eced550081767c2f3
81935:S 26 May 2026 20:46:27.159 * Mismatch in topology information for sender node 960f1889400efd7256c34b27a69db68a0e37e3ea (R9) in shard 6d42ca63697f3e23acff992eced550081767c2f3
81935:S 26 May 2026 20:46:27.196 * Failover election won: I'm the new primary.

The other replica:

81924:S 26 May 2026 20:46:15.056 * Start of election delayed for 0 milliseconds (rank #0, primary rank #1, offset 14).
81924:S 26 May 2026 20:46:15.056 * Starting a failover election for epoch 11, node config epoch is 2
81924:S 26 May 2026 20:46:15.098 # Failover auth NACK [already-voted] from 168a74f5255a32672cb114838466aae7c284ace1 (R4) for epoch 11 (NACKs 1/7)
81924:S 26 May 2026 20:46:15.103 # Failover auth NACK [already-voted] from ee52b4158d5a1e80165ba6e0eff0a1ef2ce7bfb3 (R3) for epoch 11 (NACKs 2/7)
81924:S 26 May 2026 20:46:15.103 # Failover auth NACK [already-voted] from 584c617a6cf7d4ba3d7842732d107b2c0fdcfcb8 (R5) for epoch 11 (NACKs 3/7)
81924:S 26 May 2026 20:46:15.113 * Currently unable to failover: Waiting for votes, but majority still not reached.
81924:S 26 May 2026 20:46:15.113 * Needed quorum: 4. Number of votes received so far: 1
81924:S 26 May 2026 20:46:16.053 * Currently unable to failover: Waiting for votes, but majority still not reached.
81924:S 26 May 2026 20:46:16.053 * Needed quorum: 4. Number of votes received so far: 1
81924:S 26 May 2026 20:46:17.064 * Currently unable to failover: Waiting for votes, but majority still not reached.
81924:S 26 May 2026 20:46:17.064 * Needed quorum: 4. Number of votes received so far: 1
81924:S 26 May 2026 20:46:18.073 * Currently unable to failover: Waiting for votes, but majority still not reached.
81924:S 26 May 2026 20:46:18.073 * Needed quorum: 4. Number of votes received so far: 1
81924:S 26 May 2026 20:46:19.084 * Currently unable to failover: Waiting for votes, but majority still not reached.
81924:S 26 May 2026 20:46:19.084 * Needed quorum: 4. Number of votes received so far: 1
81924:S 26 May 2026 20:46:20.093 * Currently unable to failover: Waiting for votes, but majority still not reached.
81924:S 26 May 2026 20:46:20.093 * Needed quorum: 4. Number of votes received so far: 1
81924:S 26 May 2026 20:46:21.003 * Currently unable to failover: Waiting for votes, but majority still not reached.
81924:S 26 May 2026 20:46:21.004 * Needed quorum: 4. Number of votes received so far: 1
81924:S 26 May 2026 20:46:21.105 * Currently unable to failover: Failover attempt expired.
81924:S 26 May 2026 20:46:21.105 * Needed quorum: 4. Number of votes received so far: 1
81924:S 26 May 2026 20:46:22.014 * Currently unable to failover: Failover attempt expired.
81924:S 26 May 2026 20:46:22.014 * Needed quorum: 4. Number of votes received so far: 1
81924:S 26 May 2026 20:46:23.025 * Currently unable to failover: Failover attempt expired.
81924:S 26 May 2026 20:46:23.025 * Needed quorum: 4. Number of votes received so far: 1
81924:S 26 May 2026 20:46:24.034 * Currently unable to failover: Failover attempt expired.
81924:S 26 May 2026 20:46:24.034 * Needed quorum: 4. Number of votes received so far: 1
81924:S 26 May 2026 20:46:25.044 * Currently unable to failover: Failover attempt expired.
81924:S 26 May 2026 20:46:25.044 * Needed quorum: 4. Number of votes received so far: 1
81924:S 26 May 2026 20:46:26.056 * Currently unable to failover: Failover attempt expired.
81924:S 26 May 2026 20:46:26.056 * Needed quorum: 4. Number of votes received so far: 1
81924:S 26 May 2026 20:46:27.067 * Start of election delayed for 0 milliseconds (rank #0, primary rank #1, offset 14).
81924:S 26 May 2026 20:46:27.067 * Starting a failover election for epoch 12, node config epoch is 2
81924:S 26 May 2026 20:46:27.113 # Failover auth NACK [already-voted] from 168a74f5255a32672cb114838466aae7c284ace1 (R4) for epoch 12 (NACKs 1/7)
81924:S 26 May 2026 20:46:27.113 # Failover auth NACK [already-voted] from ee52b4158d5a1e80165ba6e0eff0a1ef2ce7bfb3 (R3) for epoch 12 (NACKs 2/7)
81924:S 26 May 2026 20:46:27.117 # Failover auth NACK [already-voted] from 584c617a6cf7d4ba3d7842732d107b2c0fdcfcb8 (R5) for epoch 12 (NACKs 3/7)
81924:S 26 May 2026 20:46:27.121 # Failover auth NACK [already-voted] from f2166e8b732feb4a59a6db075247692e4b6b8d4a (R6) for epoch 12 (NACKs 4/7)
81924:S 26 May 2026 20:46:27.121 # Failover election for epoch 12 cannot reach quorum 4 (NACKs 4/7). Resetting the election since we cannot win an election without quorum.
81924:S 26 May 2026 20:46:27.121 * Currently unable to failover: Waiting for votes, but majority still not reached.
81924:S 26 May 2026 20:46:27.121 * Needed quorum: 4. Number of votes received so far: 0
81924:S 26 May 2026 20:46:27.159 # Failover election in progress for epoch 12, but received a claim from node 960f1889400efd7256c34b27a69db68a0e37e3ea (R9) with an equal or higher epoch 12. Resetting the election since we cannot win an election in the past.
81924:S 26 May 2026 20:46:27.163 * Reconfiguring node 960f1889400efd7256c34b27a69db68a0e37e3ea (R9) as primary for shard 6d42ca63697f3e23acff992eced550081767c2f3
81924:S 26 May 2026 20:46:27.163 * Mismatch in topology information for sender node 960f1889400efd7256c34b27a69db68a0e37e3ea (R9) in shard 6d42ca63697f3e23acff992eced550081767c2f3
81924:S 26 May 2026 20:46:27.163 * This is the best ranked replica and can initiate the election immediately.
81924:S 26 May 2026 20:46:27.167 * Start of election delayed for 0 milliseconds (rank #0, primary rank #0, offset 14).
81924:S 26 May 2026 20:46:27.172 * Starting a failover election for epoch 14, node config epoch is 2
81924:S 26 May 2026 20:46:27.276 * Reconfiguring node d51d892710dd8818b886c8e0e7ccdb51a4020ddb (R7) as primary for shard aeac04a66b927d48c8102f6a85030c756dd33f46
81924:S 26 May 2026 20:46:27.276 * Mismatch in topology information for sender node d51d892710dd8818b886c8e0e7ccdb51a4020ddb (R7) in shard aeac04a66b927d48c8102f6a85030c756dd33f46
81924:S 26 May 2026 20:46:27.276 * Failover election won: I'm the new primary.

@coderabbitai

coderabbitai Bot commented May 26, 2026 •

Copy link
Copy Markdown
Contributor

Review Change Stack

Note

Reviews paused

It looks like this branch is under active development. To avoid overwhelming you with review comments due to an influx of new commits, CodeRabbit has automatically paused this review. You can configure this behavior by changing the reviews.auto_review.auto_pause_after_reviewed_commits setting.

Use the following commands to manage reviews:

  • @coderabbitai resume to resume automatic reviews.
  • @coderabbitai review to trigger a single review.

Use the checkboxes below for quick actions:

  • ▶️ Resume reviews
  • 🔍 Trigger review
📝 Walkthrough

Walkthrough

Adds explicit FAILOVER_AUTH_NACK messaging and handling: wire contracts and capability flag, send/receive helpers, denial-path NACKs, NACK counting with election fast-fail, debug-configurable failover delay, and expanded parametrized tests.

Changes

Cluster Failover Authorization NACK Support

Layer / File(s) Summary
Wire protocol and capability contracts
src/cluster_legacy.h, src/cluster_legacy.c
New message type CLUSTERMSG_TYPE_FAILOVER_AUTH_NACK, clusterMsgDataFailoverNack payload and union member, reason codes, CLUSTER_NODE_FAILOVER_AUTH_NACK_SUPPORTED flag and accessor, init failover_auth_nack_count, and message-name mapping.
Debug configuration and command
src/server.h, src/server.c, src/debug.c
Adds server.debug_cluster_failover_delay (initialized to -1) and DEBUG CLUSTER-FAILOVER-DELAY <delay-ms> command with help text and validation.
Packet validation, capability tracking, send helper
src/cluster_legacy.c
Validate FAILOVER_AUTH_NACK packets, update peer capability from header flags, route NACK packets to handler, add clusterNackReasonString() and clusterSendFailoverNack() that only send to capable peers.
Failover auth rejection with NACKs
src/cluster_legacy.c
clusterSendFailoverAuthIfNeeded() now sends explicit NACKs for denial reasons (NOT_SAFE, REQ_EPOCH_OLD, ALREADY_VOTED, PRIMARY_UP, STALE_CONFIG); stale-config path sends UPDATE then STALE_CONFIG NACK.
NACK processing and election flow
src/cluster_legacy.c
clusterProcessFailoverAuthNack() increments failover_auth_nack_count, logs reason, fast-fails elections when quorum is unreachable; retry scheduling resets NACK count and debug delay overrides computed election delay.
Parametrized failover test
tests/unit/cluster/failover2.tcl
Refactors into test_same_epoch {delay} and runs scenario with delays 500 and 0; extends test_replica_config_epoch_failover with drop_nack to exercise legacy timeout vs fast-fail paths and expands the test matrix.

Sequence Diagram(s)

sequenceDiagram
  participant ComponentA
  participant ComponentB
  ComponentA->>ComponentB: observable interaction
Loading

Estimated code review effort

🎯 4 (Complex) | ⏱️ ~45 minutes

Possibly related issues

Suggested reviewers

  • zuiderkwast
  • lucasyonge
  • ranshid
🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 64.29% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title directly and clearly describes the main change: introducing a new FAILOVER_AUTH_NACK message to speed up split-vote elections, which is the core feature of this PR.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
Description check ✅ Passed The PR description comprehensively explains the FAILOVER_AUTH_NACK feature, its motivation (faster split-vote detection), implementation details, backward compatibility, and debug hooks introduced.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.


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 @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot 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.

🧹 Nitpick comments (3)
src/cluster_legacy.h (1)

147-150: ⚡ Quick win

Pin the new wire payload size with a static assert.

This struct is now part of the cluster wire contract. A compile-time size check here would catch accidental padding or field changes before they silently alter the on-the-wire format.

♻️ Proposed fix
 typedef struct {
     uint8_t reason;
     char notused1[24];
 } clusterMsgDataFailoverNack;
+static_assert(sizeof(clusterMsgDataFailoverNack) == 25, "unexpected FAILOVER_AUTH_NACK payload size");
🤖 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 `@src/cluster_legacy.h` around lines 147 - 150, The new cluster wire struct
clusterMsgDataFailoverNack must have its on-the-wire size pinned; add a
compile-time assertion (e.g., static_assert or _Static_assert depending on
project C standard) that sizeof(clusterMsgDataFailoverNack) == 25 (1 byte for
reason + 24 bytes for notused1) with a clear message indicating this pins the
wire format, so any accidental padding/field changes will fail to compile; place
the assertion next to the struct definition and use the same symbol name in the
assertion.
src/cluster_legacy.c (1)

70-71: ⚡ Quick win

Make the new NACK helpers file-local.

clusterSendFailoverNack() and clusterProcessFailoverAuthNack() are only used inside this translation unit, so keeping them non-static needlessly widens the symbol surface.

Proposed change
-void clusterProcessFailoverAuthNack(clusterNode *sender, clusterMsg *request);
-void clusterSendFailoverNack(clusterNode *node, uint8_t reason);
+static void clusterProcessFailoverAuthNack(clusterNode *sender, clusterMsg *request);
+static void clusterSendFailoverNack(clusterNode *node, uint8_t reason);
...
-void clusterSendFailoverNack(clusterNode *node, uint8_t reason) {
+static void clusterSendFailoverNack(clusterNode *node, uint8_t reason) {
...
-void clusterProcessFailoverAuthNack(clusterNode *sender, clusterMsg *request) {
+static void clusterProcessFailoverAuthNack(clusterNode *sender, clusterMsg *request) {

As per coding guidelines "Use static keyword for file-local functions in C code".

Also applies to: 5312-5324, 5443-5471

🤖 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 `@src/cluster_legacy.c` around lines 70 - 71, The two helper functions
clusterSendFailoverNack and clusterProcessFailoverAuthNack are only used within
this translation unit and should be made file-local: add the static qualifier to
their forward declarations and their function definitions (and to any other
internally-used helpers referenced in the same areas noted, e.g., the helpers
around the 5312-5324 and 5443-5471 regions) so their symbols are not exported
from the object file; ensure prototypes and definitions match (both marked
static).
tests/unit/cluster/failover2.tcl (1)

79-79: ⚡ Quick win

Consider using the pause_process helper for consistency.

Line 79 uses exec kill -SIGSTOP directly, while the rest of the file uses the pause_process helper (lines 15, 34, 120, 182). If atomic pausing is intentional for same-epoch testing, consider documenting the rationale; otherwise, prefer the framework helper:

♻️ Align with test framework pattern
-        exec kill -SIGSTOP [srv 0 pid] [srv -1 pid] [srv -2 pid]
+        pause_process [srv 0 pid]
+        pause_process [srv -1 pid]
+        pause_process [srv -2 pid]
🤖 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 `@tests/unit/cluster/failover2.tcl` at line 79, Replace the direct call to exec
kill -SIGSTOP used on the test processes with the existing pause_process helper
for consistency with this test suite (the helper is used at lines where
pause_process is referenced); locate the line containing exec kill -SIGSTOP [srv
0 pid] [srv -1 pid] [srv -2 pid] and change it to call pause_process for each
target process (or document in a short comment why an atomic SIGSTOP across
multiple pids is required if you intentionally need simultaneous pausing),
ensuring you use the same helper signature and import/context as other uses in
this file.
🤖 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 `@src/cluster_legacy.c`:
- Around line 70-71: The two helper functions clusterSendFailoverNack and
clusterProcessFailoverAuthNack are only used within this translation unit and
should be made file-local: add the static qualifier to their forward
declarations and their function definitions (and to any other internally-used
helpers referenced in the same areas noted, e.g., the helpers around the
5312-5324 and 5443-5471 regions) so their symbols are not exported from the
object file; ensure prototypes and definitions match (both marked static).

In `@src/cluster_legacy.h`:
- Around line 147-150: The new cluster wire struct clusterMsgDataFailoverNack
must have its on-the-wire size pinned; add a compile-time assertion (e.g.,
static_assert or _Static_assert depending on project C standard) that
sizeof(clusterMsgDataFailoverNack) == 25 (1 byte for reason + 24 bytes for
notused1) with a clear message indicating this pins the wire format, so any
accidental padding/field changes will fail to compile; place the assertion next
to the struct definition and use the same symbol name in the assertion.

In `@tests/unit/cluster/failover2.tcl`:
- Line 79: Replace the direct call to exec kill -SIGSTOP used on the test
processes with the existing pause_process helper for consistency with this test
suite (the helper is used at lines where pause_process is referenced); locate
the line containing exec kill -SIGSTOP [srv 0 pid] [srv -1 pid] [srv -2 pid] and
change it to call pause_process for each target process (or document in a short
comment why an atomic SIGSTOP across multiple pids is required if you
intentionally need simultaneous pausing), ensuring you use the same helper
signature and import/context as other uses in this file.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 5e5ebad9-589c-475d-8218-e3d48f92cb1b

📥 Commits

Reviewing files that changed from the base of the PR and between d9ba5ab and 887fccb.

📒 Files selected for processing (6)
  • src/cluster_legacy.c
  • src/cluster_legacy.h
  • src/debug.c
  • src/server.c
  • src/server.h
  • tests/unit/cluster/failover2.tcl

@enjoy-binbin
enjoy-binbin requested a review from zuiderkwast June 5, 2026 07:45

@zuiderkwast zuiderkwast 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.

Looks good in general.

Have you consider using the light header for NACK? If we use it from the start, we don't need to support the non-light variant.

Comment thread src/cluster_legacy.c Resolved
Comment thread src/debug.c Resolved
Comment thread src/cluster_legacy.c Resolved
Comment thread src/cluster_legacy.c Resolved

@sarthakaggarwal97 sarthakaggarwal97 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.

I like this idea, and the code looks mostly good. I would like to run it with valkey-fuzzer as well if it can find any issues. Shared one concern (not sure if it's valid.)

@enjoy-binbin

Copy link
Copy Markdown
Member Author

Have you consider using the light header for NACK? If we use it from the start, we don't need to support the non-light variant.

Yes, i did consider using a light header, but we do need the configEpoch information (which is currently in clusterMsg). And of course, we could also include the epoch information in the data payload. There are some conditional checks i don't want to touch:

    if (sender && !nodeInHandshake(sender)) {
        /* Update our currentEpoch if we see a newer epoch in the cluster. */
        sender_claimed_current_epoch = ntohu64(msg->currentEpoch);
        sender_claimed_config_epoch = ntohu64(msg->configEpoch);

And another reason, i want to keep the AUTH_REQUEST / AUTH_ACK / AUTH_NACK handling logic in one place. Keeping symmetry across these three election messages. NACK is point-to-point and only emitted during a failed elections, so i think i can accept it.

BUT of course, we can use light header for this type.

enjoy-binbin and others added 2 commits June 9, 2026 10:33
Co-authored-by: Viktor Söderqvist <[email protected]>
Signed-off-by: Binbin <[email protected]>
@enjoy-binbin enjoy-binbin added the run-extra-tests Run extra tests on this PR (Runs all tests from daily except valgrind and RESP) label Jun 9, 2026

@coderabbitai coderabbitai Bot 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.

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (2)
tests/unit/cluster/failover2.tcl (2)

78-78: ⚠️ Potential issue | 🟡 Minor | ⚡ Quick win

Typo in comment.

"Killing there primary nodes" should be "Killing three primary nodes".

-        # Killing there primary nodes.
+        # Killing three primary nodes.
🤖 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 `@tests/unit/cluster/failover2.tcl` at line 78, Typo in test comment: update
the comment string "Killing there primary nodes." in
tests/unit/cluster/failover2.tcl to read "Killing three primary nodes." so the
intent is clear; locate the commented line containing that exact phrase and
replace "there" with "three".

70-70: ⚠️ Potential issue | 🟡 Minor | ⚡ Quick win

Grammar issue in test name.

The test name contains "then they are elected" which should be "when they are elected" for grammatical correctness.

-    test "Primaries will not time out then they are elected in the same epoch - delay $delay" {
+    test "Primaries will not time out when they are elected in the same epoch - delay $delay" {
🤖 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 `@tests/unit/cluster/failover2.tcl` at line 70, The test name string "Primaries
will not time out then they are elected in the same epoch - delay $delay" has a
grammar mistake; update the test declaration to replace "then" with "when" so it
reads "Primaries will not time out when they are elected in the same epoch -
delay $delay" (locate the test block that begins with the same quoted title and
change the text there).

Source: Coding guidelines

🤖 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.

Outside diff comments:
In `@tests/unit/cluster/failover2.tcl`:
- Line 78: Typo in test comment: update the comment string "Killing there
primary nodes." in tests/unit/cluster/failover2.tcl to read "Killing three
primary nodes." so the intent is clear; locate the commented line containing
that exact phrase and replace "there" with "three".
- Line 70: The test name string "Primaries will not time out then they are
elected in the same epoch - delay $delay" has a grammar mistake; update the test
declaration to replace "then" with "when" so it reads "Primaries will not time
out when they are elected in the same epoch - delay $delay" (locate the test
block that begins with the same quoted title and change the text there).

ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 2f09e5f2-a421-48f6-843d-4f1f753114f9

📥 Commits

Reviewing files that changed from the base of the PR and between 6a9ddad and 64fac40.

📒 Files selected for processing (2)
  • src/cluster_legacy.c
  • tests/unit/cluster/failover2.tcl
🚧 Files skipped from review as they are similar to previous changes (1)
  • src/cluster_legacy.c

@codecov

codecov Bot commented Jun 9, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 85.33333% with 11 lines in your changes missing coverage. Please review.
✅ Project coverage is 77.02%. Comparing base (b713c6a) to head (8d57e24).
⚠️ Report is 62 commits behind head on unstable.

Files with missing lines Patch % Lines
src/cluster_legacy.c 87.71% 7 Missing ⚠️
src/debug.c 75.00% 4 Missing ⚠️
Additional details and impacted files
@@             Coverage Diff              @@
##           unstable    #3833      +/-   ##
============================================
+ Coverage     76.89%   77.02%   +0.12%     
============================================
  Files           162      162              
  Lines         81843    81917      +74     
============================================
+ Hits          62937    63099     +162     
+ Misses        18906    18818      -88     
Files with missing lines Coverage Δ
src/server.c 89.57% <100.00%> (+<0.01%) ⬆️
src/server.h 100.00% <ø> (ø)
src/debug.c 56.13% <75.00%> (+0.25%) ⬆️
src/cluster_legacy.c 88.24% <87.71%> (-0.14%) ⬇️

... and 18 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.

@zuiderkwast

Copy link
Copy Markdown
Contributor

And another reason, i want to keep the AUTH_REQUEST / AUTH_ACK / AUTH_NACK handling logic in one place. Keeping symmetry across these three election messages. NACK is point-to-point and only emitted during a failed elections, so i think i can accept it.

BUT of course, we can use light header for this type.

Let's use the normal header for NACK now. In the future, we can add light variant of all of these.

@zuiderkwast zuiderkwast 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. Some comments/questions. You can check if you agree or not, or just merge.

Comment thread src/cluster_legacy.c Resolved
Comment thread src/cluster_legacy.c Resolved
Comment thread src/cluster_legacy.c Resolved
Comment thread src/cluster_legacy.c Resolved
@zuiderkwast zuiderkwast added the needs-doc-pr This change needs to update a documentation page. Remove label once doc PR is open. label Jun 9, 2026

@sarthakaggarwal97 sarthakaggarwal97 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.

Comment thread src/cluster_legacy.h Resolved
zuiderkwast added a commit to tartelettie/valkey that referenced this pull request Sep 15, 2026
…valkey-io#3833)

In the current failover protocol, a replica sends one AUTH_REQUEST per epoch and
each voter casts at most one vote per epoch. Despite the various delay heuristics
in `clusterHandleReplicaFailover` that try to stagger the replicas, concurrent
elections can still collide on the same epoch. When the vote is split and nobody
reaches the quorum, the losing replica cannot learn this in time: it must first
wait for the election to be declared expired after auth_timeout (2 ×
cluster-node-timeout), and then wait another auth_retry_time (2 × auth_timeout)
before it is allowed to start the next election with a higher epoch.

This PR introduces a new message type, FAILOVER_AUTH_NACK, that a voter replies
with in every rejection branch (NOT_SAFE, REQ_EPOCH_OLD, ALREADY_VOTED,
REQ_IS_PRIMARY, NO_PRIMARY, PRIMARY_UP, STALE_CONFIG). The replica counts the
NACKs it receives; since a voter never answers twice in the same epoch, the most
votes it could still gather is bounded by the voters that have not NACKed. Once
that bound drops below the quorum, the election is declared unwinnable and a new
one is started immediately with a higher epoch, shrinking recovery from the
auth_timeout + auth_retry_time window down to a few cron ticks.

A voting primary that is itself FAIL can never reply (it neither ACKs nor NACKs)
yet is still counted in cluster->size, so it is excluded from this bound: the
achievable votes are `size - size_fail - nack_count`, where size_fail is the
number of FAIL voting primaries maintained in clusterUpdateState(). Without this,
a split vote among the replicas of several primaries that went down together
could never push nack_count high enough, and the replica would fall back to the
slow auth_timeout path. The quorum itself is still computed from the full size,
since a failover must be authorized by a majority of the whole configuration.

Wire compatibility is preserved by gating NACK sending behind a new capability
flag, CLUSTER_NODE_FAILOVER_AUTH_NACK_SUPPORTED, advertised in the PING/PONG
flags via `clusterUpdateMyselfFlags`. Nodes that do not advertise this capability
will never see the new message type and fall back to the legacy auth_timeout path.

Granted (ACK) and rejected (NACK) votes are logged symmetrically per epoch, so
the vote tally of an election can be reconstructed from the logs.

Two DEBUG hooks are added for testing:
- DEBUG CLUSTER-FAILOVER-DELAY <ms>: override the failover delay computed in
  clusterHandleReplicaFailover.
- DEBUG CLUSTER-FAILOVER-EPOCH <epoch>: force the next election started by this
  replica to run in the given epoch (consumed once, then back to currentEpoch+1),
  so several replicas can be made to contend in the same epoch and deterministically
  reproduce a split vote.

Signed-off-by: Binbin <[email protected]>
Signed-off-by: Viktor Söderqvist <[email protected]>
Co-authored-by: Viktor Söderqvist <[email protected]>
zuiderkwast pushed a commit that referenced this pull request Sep 16, 2026
A NACK for an old election could be counted against the current one,
since the receiver only checked the sender's header epoch. Carry the
rejected request's epoch in the NACK and only count exact matches. Fixes
#4627.

**Wire format change.** `clusterMsgDataFailoverNack` grows from 1 byte
(`reason`) to 16 bytes (`epoch`, `reason`, 7 reserved).
`CLUSTER_NODE_FAILOVER_AUTH_NACK_SUPPORTED` is unchanged, and
`clusterIsValidPacket` requires an exact length for this type, so a node
with this change and a node without it will drop each other's NACKs (one
`LL_WARNING` per dropped packet) and fall back to the timeout-based
election retry that predates #3833. Nothing released sets that
capability bit (#3833 is only on `unstable`), so this only affects mixed
`unstable` builds; it is not a rolling-upgrade concern for any released
version.

Signed-off-by: Dante Knowles <[email protected]>
JimB123 pushed a commit that referenced this pull request Sep 22, 2026
A NACK for an old election could be counted against the current one,
since the receiver only checked the sender's header epoch. Carry the
rejected request's epoch in the NACK and only count exact matches. Fixes
#4627.

**Wire format change.** `clusterMsgDataFailoverNack` grows from 1 byte
(`reason`) to 16 bytes (`epoch`, `reason`, 7 reserved).
`CLUSTER_NODE_FAILOVER_AUTH_NACK_SUPPORTED` is unchanged, and
`clusterIsValidPacket` requires an exact length for this type, so a node
with this change and a node without it will drop each other's NACKs (one
`LL_WARNING` per dropped packet) and fall back to the timeout-based
election retry that predates #3833. Nothing released sets that
capability bit (#3833 is only on `unstable`), so this only affects mixed
`unstable` builds; it is not a rolling-upgrade concern for any released
version.

Signed-off-by: Dante Knowles <[email protected]>
Backport-Source-PR: 4654
enjoy-binbin added a commit that referenced this pull request Oct 13, 2026
FAILOVER_AUTH_NACK (#3833) lets a candidate abandon an election as soon as
the votes it can still collect drop below quorum, instead of waiting for
auth_timeout. Every NACK is counted, but most rejection reasons (NO_PRIMARY,
PRIMARY_UP, NOT_SAFE, REQ_IS_PRIMARY) describe state that a new epoch leaves
unchanged: the voters answer the next request identically, so resetting only
restarts the election at event-loop frequency, bumping the epoch and
re-broadcasting a request that cannot win on every round.

Count only the reasons that carry split-vote information, i.e. that a fresh
epoch can actually change: ALREADY_VOTED (the vote is freed in the next
epoch), REQ_EPOCH_OLD (the next request carries a higher epoch) and
STALE_CONFIG (the voter sends an UPDATE just before the NACK). The rest are
ignored, leaving those elections to expire and be rescheduled on the
auth_retry_time cadence.

Add a regression test where the voters CLUSTER FORGET the failed primary so
that every request is answered with NO_PRIMARY: the candidate must keep one
election per auth_retry_time instead of retrying once per NACK.

Signed-off-by: Binbin <[email protected]>
valkeyrie-ops Bot pushed a commit that referenced this pull request Oct 13, 2026
FAILOVER_AUTH_NACK (#3833) lets a candidate abandon an election as soon as
the votes it can still collect drop below quorum, instead of waiting for
auth_timeout. Every NACK is counted, but most rejection reasons (NO_PRIMARY,
PRIMARY_UP, NOT_SAFE, REQ_IS_PRIMARY) describe state that a new epoch leaves
unchanged: the voters answer the next request identically, so resetting only
restarts the election at event-loop frequency, bumping the epoch and
re-broadcasting a request that cannot win on every round.

Count only the reasons that carry split-vote information, i.e. that a fresh
epoch can actually change: ALREADY_VOTED (the vote is freed in the next
epoch), REQ_EPOCH_OLD (the next request carries a higher epoch) and
STALE_CONFIG (the voter sends an UPDATE just before the NACK). The rest are
ignored, leaving those elections to expire and be rescheduled on the
auth_retry_time cadence.

Add a regression test where the voters CLUSTER FORGET the failed primary so
that every request is answered with NO_PRIMARY: the candidate must keep one
election per auth_retry_time instead of retrying once per NACK.

Signed-off-by: Binbin <[email protected]>
Backport-Source-PR: 4700
JimB123 pushed a commit that referenced this pull request Oct 14, 2026
FAILOVER_AUTH_NACK (#3833) lets a candidate abandon an election as soon as
the votes it can still collect drop below quorum, instead of waiting for
auth_timeout. Every NACK is counted, but most rejection reasons (NO_PRIMARY,
PRIMARY_UP, NOT_SAFE, REQ_IS_PRIMARY) describe state that a new epoch leaves
unchanged: the voters answer the next request identically, so resetting only
restarts the election at event-loop frequency, bumping the epoch and
re-broadcasting a request that cannot win on every round.

Count only the reasons that carry split-vote information, i.e. that a fresh
epoch can actually change: ALREADY_VOTED (the vote is freed in the next
epoch), REQ_EPOCH_OLD (the next request carries a higher epoch) and
STALE_CONFIG (the voter sends an UPDATE just before the NACK). The rest are
ignored, leaving those elections to expire and be rescheduled on the
auth_retry_time cadence.

Add a regression test where the voters CLUSTER FORGET the failed primary so
that every request is answered with NO_PRIMARY: the candidate must keep one
election per auth_retry_time instead of retrying once per NACK.

Signed-off-by: Binbin <[email protected]>
Backport-Source-PR: 4700
valkeyrie-ops Bot pushed a commit that referenced this pull request Nov 11, 2026
The test pauses three primaries at once and assumes each replica ranks
its failed primary correctly, but a replica ranks it only among the
failed primaries it already knows about. In the job log the replica of
the third primary received the FAIL for its own primary at 08:40:02.674
and the FAIL for the first primary only at 08:40:02.676. It therefore
logged primary rank #0, took the best ranked replica path and started
its election at once for epoch 12, the same epoch the replica of the
first primary (correctly rank 0) had just started. With five voting
primaries left and a quorum of 4, the votes split 2 and 3. Nobody won
epoch 12, so neither replica saw a newer config epoch to reset on, and
both logged Failover attempt expired from 08:40:32 (30 s auth timeout at
cluster-node-timeout 15000). The retry comes after 60 s, later than the
test's 50 s wait, which failed at 08:40:37.

The server recovers on its own after the retry, so this is a test that
depends on a FAIL propagation order that 9.1 does not guarantee, not a
broken promise. On 9.1 a same-epoch split vote can only end by timeout,
so the test's no-expiry assertion holds only when the replicas rank
themselves from complete information. Holding the elections until all
seven live nodes see all three primaries as fail gives ranks 0, 1 and 2,
so the elections are staggered by the per-rank delay and run in distinct
epochs. The patch uses only what this branch has: the
cluster-replica-no-failover config and the existing
wait_node_marked_fail polling helper, with no fixed sleeps. The default
branch fixed split votes with a new product feature (FAILOVER_AUTH_NACK,
f2c1c5e, #3833), which touches
cluster_legacy.c, debug.c and server.c and is not suitable to port to a
release branch as a CI fix. The already ported #3946 (5cf0ff9) only
lengthens the per-rank delay and cannot help when the rank itself is
computed as 0.
valkeyrie-ops Bot pushed a commit that referenced this pull request Nov 12, 2026
The log shows a split vote. Replica 7 won epoch 11 at 07:36:49.175.
Replicas 8 and 9 then both started an election for epoch 12 within 17 ms
of each other (49.274 and 49.291) and got 3 and 2 votes against a quorum
of 4, so neither could win. They waited out the 30 s auth timeout
(cluster-node-timeout 15000 times 2) and logged 'Failover attempt
expired' at 07:37:19, and the retry only comes after 60 s, so the test's
50 s wait ended first. They collided because each replica starts its
election delay when it learns that its own primary is FAIL, and those
moments differed: replica 8 marked its primary failed at 47.477 and
scheduled at 47.577, while replica 9 scheduled at 48.156. That 579 ms
difference cancelled the 500 ms per-rank spacing, leaving the rank 2 and
rank 1 replicas due at 49.572 and 49.642. When replica 7 won, both were
moved 500 ms earlier, both were already overdue, and both fired on their
next cron tick with the same currentEpoch.

The server on 8.1 has no way to recover quickly from a split vote. It
only resets an election when another node claims an equal or higher
config epoch, and here nobody won epoch 12. The default branch fixed
that in product code with the FAILOVER_AUTH_NACK feature (f2c1c5e,
#3833), which is a new protocol message and not a backport candidate. So
the fix belongs in the test. Holding the replicas with
cluster-replica-no-failover until all live nodes (voters 3 to 6 and
replicas 7 to 9) see all three primaries as FAIL makes the replicas
compute their delays at nearly the same moment with the full failed
primary rank, so the rank spacing the server relies on is intact. The
test still fails over three shards at once and keeps both assertions (no
epoch 0 election, no 'Failover attempt expired'), so nothing is
weakened. It uses only helpers and a config this branch already has
(wait_node_marked_fail, cluster-replica-no-failover).
sarthakaggarwal97 pushed a commit that referenced this pull request Nov 18, 2026
The test pauses three primaries at once and assumes each replica ranks
its failed primary correctly, but a replica ranks it only among the
failed primaries it already knows about. In the job log the replica of
the third primary received the FAIL for its own primary at 08:40:02.674
and the FAIL for the first primary only at 08:40:02.676. It therefore
logged primary rank #0, took the best ranked replica path and started
its election at once for epoch 12, the same epoch the replica of the
first primary (correctly rank 0) had just started. With five voting
primaries left and a quorum of 4, the votes split 2 and 3. Nobody won
epoch 12, so neither replica saw a newer config epoch to reset on, and
both logged Failover attempt expired from 08:40:32 (30 s auth timeout at
cluster-node-timeout 15000). The retry comes after 60 s, later than the
test's 50 s wait, which failed at 08:40:37.

The server recovers on its own after the retry, so this is a test that
depends on a FAIL propagation order that 9.1 does not guarantee, not a
broken promise. On 9.1 a same-epoch split vote can only end by timeout,
so the test's no-expiry assertion holds only when the replicas rank
themselves from complete information. Holding the elections until all
seven live nodes see all three primaries as fail gives ranks 0, 1 and 2,
so the elections are staggered by the per-rank delay and run in distinct
epochs. The patch uses only what this branch has: the
cluster-replica-no-failover config and the existing
wait_node_marked_fail polling helper, with no fixed sleeps. The default
branch fixed split votes with a new product feature (FAILOVER_AUTH_NACK,
f2c1c5e, #3833), which touches
cluster_legacy.c, debug.c and server.c and is not suitable to port to a
release branch as a CI fix. The already ported #3946 (5cf0ff9) only
lengthens the per-rank delay and cannot help when the rank itself is
computed as 0.
sarthakaggarwal97 pushed a commit that referenced this pull request Nov 18, 2026
The test pauses three primaries at once and assumes each replica ranks
its failed primary correctly, but a replica ranks it only among the
failed primaries it already knows about. In the job log the replica of
the third primary received the FAIL for its own primary at 08:40:02.674
and the FAIL for the first primary only at 08:40:02.676. It therefore
logged primary rank #0, took the best ranked replica path and started
its election at once for epoch 12, the same epoch the replica of the
first primary (correctly rank 0) had just started. With five voting
primaries left and a quorum of 4, the votes split 2 and 3. Nobody won
epoch 12, so neither replica saw a newer config epoch to reset on, and
both logged Failover attempt expired from 08:40:32 (30 s auth timeout at
cluster-node-timeout 15000). The retry comes after 60 s, later than the
test's 50 s wait, which failed at 08:40:37.

The server recovers on its own after the retry, so this is a test that
depends on a FAIL propagation order that 9.1 does not guarantee, not a
broken promise. On 9.1 a same-epoch split vote can only end by timeout,
so the test's no-expiry assertion holds only when the replicas rank
themselves from complete information. Holding the elections until all
seven live nodes see all three primaries as fail gives ranks 0, 1 and 2,
so the elections are staggered by the per-rank delay and run in distinct
epochs. The patch uses only what this branch has: the
cluster-replica-no-failover config and the existing
wait_node_marked_fail polling helper, with no fixed sleeps. The default
branch fixed split votes with a new product feature (FAILOVER_AUTH_NACK,
f2c1c5e, #3833), which touches
cluster_legacy.c, debug.c and server.c and is not suitable to port to a
release branch as a CI fix. The already ported #3946 (5cf0ff9) only
lengthens the per-rank delay and cannot help when the rank itself is
computed as 0.
sarthakaggarwal97 pushed a commit that referenced this pull request Nov 18, 2026
The log shows a split vote. Replica 7 won epoch 11 at 07:36:49.175.
Replicas 8 and 9 then both started an election for epoch 12 within 17 ms
of each other (49.274 and 49.291) and got 3 and 2 votes against a quorum
of 4, so neither could win. They waited out the 30 s auth timeout
(cluster-node-timeout 15000 times 2) and logged 'Failover attempt
expired' at 07:37:19, and the retry only comes after 60 s, so the test's
50 s wait ended first. They collided because each replica starts its
election delay when it learns that its own primary is FAIL, and those
moments differed: replica 8 marked its primary failed at 47.477 and
scheduled at 47.577, while replica 9 scheduled at 48.156. That 579 ms
difference cancelled the 500 ms per-rank spacing, leaving the rank 2 and
rank 1 replicas due at 49.572 and 49.642. When replica 7 won, both were
moved 500 ms earlier, both were already overdue, and both fired on their
next cron tick with the same currentEpoch.

The server on 8.1 has no way to recover quickly from a split vote. It
only resets an election when another node claims an equal or higher
config epoch, and here nobody won epoch 12. The default branch fixed
that in product code with the FAILOVER_AUTH_NACK feature (f2c1c5e,
#3833), which is a new protocol message and not a backport candidate. So
the fix belongs in the test. Holding the replicas with
cluster-replica-no-failover until all live nodes (voters 3 to 6 and
replicas 7 to 9) see all three primaries as FAIL makes the replicas
compute their delays at nearly the same moment with the full failed
primary rank, so the rank spacing the server relies on is intact. The
test still fails over three shards at once and keeps both assertions (no
epoch 0 election, no 'Failover attempt expired'), so nothing is
weakened. It uses only helpers and a config this branch already has
(wait_node_marked_fail, cluster-replica-no-failover).
valkeyrie-ops Bot added a commit that referenced this pull request Dec 2, 2026
The test rejects a split vote, which this branch's server can only
resolve by letting the election expire. In the log replica 9 won epoch 8
at 09:10:11.797, then replicas 7 and 8 both started an election for
epoch 9 at 09:10:12.143 and 09:10:12.145. Primaries 3 and 6 and the new
primary 9 voted for replica 7, primaries 4 and 5 voted for replica 8, so
the votes were 3 to 2 with a needed quorum of 4 and no one won the
epoch. Because no node claimed an epoch equal to or higher than 9,
neither election was reset, and both replicas logged "Failover attempt
expired" from 09:10:42. On 8.0 the retry comes after four times the node
timeout, 60 s with cluster-node-timeout 15000, while the test waits 50 s
and failed at 09:10:46. The election delay on 8.0 is a fixed 500 +
random() % 500 ms with no ranking across failed shards, so the larger
node timeout ported earlier does not space the shards' elections here
and only makes the retry slower.

A split vote in which no candidate reaches the majority, followed by a
timeout and a retry, is correct server behaviour on 8.0, so the test
should accept it rather than the server changing. The exception is
narrow: a replica may time out only if it started an election in an
epoch in which another replica also started one and which no replica
won, as read from the "Starting a failover election for epoch" and
"configEpoch set to N after successful failover" log lines. A replica
that times out although another node won its epoch, which is what the
test exists to catch, still fails. I kept the natural timing of the
three elections instead of releasing the replicas one by one, because
serialising them would stop the test from ever exercising same-epoch
elections or the false epoch 0 case. No product change is made: upstream
avoids or resolves split votes with features (failed primary ranking in
#1018, the FAILOVER_AUTH_NACK message in #3833) that are not minimal
backports for this branch.

CI-Fix-Job: test-ubuntu-latest
CI-Fix-Run: https://github.com/valkey-io/valkey/actions/runs/46245707417
Signed-off-by: valkeyrie-ops[bot] <284117741+valkeyrie-ops[bot]@users.noreply.github.com>
valkeyrie-ops Bot added a commit that referenced this pull request Dec 2, 2026
While R 8 drops every packet with CLOSE-CLUSTER-LINK-ON-PACKET-DROP on,
each dropped packet closes its link. R 8 closed all six outbound links
at 09:29:53.720, and the filter was lifted at 53.868. R 8 saw R 7's
claim of epoch 11 at 53.871, reset its election and started a new one
for epoch 12 at 54.426. clusterNodeCronHandleReconnect only retries a
node with no inbound link after node_timeout/2/10, which is 750 ms with
this test's 15000 ms node timeout, and 54.426 is 706 ms after the last
attempt. Primaries 3 and 4 accepted a connection only at 54.527 and
logged neither a grant nor a denial for epoch 12. The vote request is
broadcast once, only over links that exist, so R 8 got 3 votes
(primaries 5 and 6, and R 7) of the 4 it needed. It logged 'Failover
attempt expired' at 09:30:24 and the test's 50 second wait ran out.

The test assumed R 8's links were back by the time its new election
started, which the server does not guarantee: the new election starts
500 to 1000 ms after the reset and a reconnect can take up to 750 ms
plus a cron tick. The fix holds the moving part still instead of waiting
longer. It sets cluster-replica-no-failover yes on R 8 just before
lifting the filter, waits until R 8 and each voter (R 3 to R 7) report
each other's linkstate as connected, then sets it back to no. Both
directions matter because the request goes over the candidate's outbound
link and the vote comes back over the voter's outbound link. The hold
does not weaken the test: the election reset happens in packet
processing regardless of the config, and if it did not happen R 8 would
still sit in its epoch 11 election after release and fail both the role
wait and the 'Failover attempt expired' check. The same linkstate wait
is what the default branch uses for the same kind of race in the config
epoch test of this file (from #3833).

CI-Fix-Job: test-ubuntu-tls-no-tls
CI-Fix-Run: https://github.com/valkey-io/valkey/actions/runs/46616296383
Signed-off-by: valkeyrie-ops[bot] <284117741+valkeyrie-ops[bot]@users.noreply.github.com>
valkeyrie-ops Bot added a commit that referenced this pull request Dec 3, 2026
The test rejects a split vote, which this branch's server can only
resolve by letting the election expire. In the log replica 9 won epoch 8
at 09:10:11.797, then replicas 7 and 8 both started an election for
epoch 9 at 09:10:12.143 and 09:10:12.145. Primaries 3 and 6 and the new
primary 9 voted for replica 7, primaries 4 and 5 voted for replica 8, so
the votes were 3 to 2 with a needed quorum of 4 and no one won the
epoch. Because no node claimed an epoch equal to or higher than 9,
neither election was reset, and both replicas logged "Failover attempt
expired" from 09:10:42. On 8.0 the retry comes after four times the node
timeout, 60 s with cluster-node-timeout 15000, while the test waits 50 s
and failed at 09:10:46. The election delay on 8.0 is a fixed 500 +
random() % 500 ms with no ranking across failed shards, so the larger
node timeout ported earlier does not space the shards' elections here
and only makes the retry slower.

A split vote in which no candidate reaches the majority, followed by a
timeout and a retry, is correct server behaviour on 8.0, so the test
should accept it rather than the server changing. The exception is
narrow: a replica may time out only if it started an election in an
epoch in which another replica also started one and which no replica
won, as read from the "Starting a failover election for epoch" and
"configEpoch set to N after successful failover" log lines. A replica
that times out although another node won its epoch, which is what the
test exists to catch, still fails. I kept the natural timing of the
three elections instead of releasing the replicas one by one, because
serialising them would stop the test from ever exercising same-epoch
elections or the false epoch 0 case. No product change is made: upstream
avoids or resolves split votes with features (failed primary ranking in
#1018, the FAILOVER_AUTH_NACK message in #3833) that are not minimal
backports for this branch.

CI-Fix-Job: test-ubuntu-latest
CI-Fix-Run: https://github.com/valkey-io/valkey/actions/runs/46245707417
Signed-off-by: valkeyrie-ops[bot] <284117741+valkeyrie-ops[bot]@users.noreply.github.com>
valkeyrie-ops Bot added a commit that referenced this pull request Dec 3, 2026
While R 8 drops every packet with CLOSE-CLUSTER-LINK-ON-PACKET-DROP on,
each dropped packet closes its link. R 8 closed all six outbound links
at 09:29:53.720, and the filter was lifted at 53.868. R 8 saw R 7's
claim of epoch 11 at 53.871, reset its election and started a new one
for epoch 12 at 54.426. clusterNodeCronHandleReconnect only retries a
node with no inbound link after node_timeout/2/10, which is 750 ms with
this test's 15000 ms node timeout, and 54.426 is 706 ms after the last
attempt. Primaries 3 and 4 accepted a connection only at 54.527 and
logged neither a grant nor a denial for epoch 12. The vote request is
broadcast once, only over links that exist, so R 8 got 3 votes
(primaries 5 and 6, and R 7) of the 4 it needed. It logged 'Failover
attempt expired' at 09:30:24 and the test's 50 second wait ran out.

The test assumed R 8's links were back by the time its new election
started, which the server does not guarantee: the new election starts
500 to 1000 ms after the reset and a reconnect can take up to 750 ms
plus a cron tick. The fix holds the moving part still instead of waiting
longer. It sets cluster-replica-no-failover yes on R 8 just before
lifting the filter, waits until R 8 and each voter (R 3 to R 7) report
each other's linkstate as connected, then sets it back to no. Both
directions matter because the request goes over the candidate's outbound
link and the vote comes back over the voter's outbound link. The hold
does not weaken the test: the election reset happens in packet
processing regardless of the config, and if it did not happen R 8 would
still sit in its epoch 11 election after release and fail both the role
wait and the 'Failover attempt expired' check. The same linkstate wait
is what the default branch uses for the same kind of race in the config
epoch test of this file (from #3833).

CI-Fix-Job: test-ubuntu-tls-no-tls
CI-Fix-Run: https://github.com/valkey-io/valkey/actions/runs/46616296383
Signed-off-by: valkeyrie-ops[bot] <284117741+valkeyrie-ops[bot]@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

cluster needs-doc-pr This change needs to update a documentation page. Remove label once doc PR is open. release-notes This issue should get a line item in the release notes run-extra-tests Run extra tests on this PR (Runs all tests from daily except valgrind and RESP)

Projects

Status: Merged

Development

Successfully merging this pull request may close these issues.

3 participants