Skip to content

Re-read server roles promptly when a primary answers READONLY - #3384

Open
mgravell wants to merge 1 commit into
mainfrom
marc/readonly-role-check
Open

mgravell wants to merge 1 commit into
mainfrom
marc/readonly-role-check

Conversation

@mgravell

@mgravell mgravell commented Dec 1, 2026

Copy link
Copy Markdown
Member

Fixes #3383.

A primary demoted in place drops no connection. That covers a REPLICAOF switch, and a Sentinel failover whose +switch-master the client missed. The client only noticed at the next periodic INFO replication, so for up to ConfigCheckSeconds (60s by default) every write went to the old primary and was refused with READONLY. This happens without Sentinel too: a plain primary/replica configuration gets stuck the same way.

Change

When a READONLY reply comes from a server we treat as a primary, the client re-reads the role (INFO replication) of every connected server, at most once a second per multiplexer. The existing paths then elect the new primary: the reconfigure that a role change triggers, and for a Sentinel-managed connection, the demotion check on the Sentinel heartbeat.

  • Every server, not just the one that refused. If that node was demoted, another was presumably promoted, and we still think that one is a replica. A reconfigure doesn't re-read the role of a server it's already connected to, so checking only the refusing node leaves no primary at all. I tried that first, and the plain primary/replica case then failed with "no connection available" until the periodic check.
  • No effect when the refusing server is already known to be a replica (for example, a module write sent through IServer.Execute), or in cluster mode, where replicas answer MOVED rather than READONLY.
  • The re-reads run on the thread pool rather than the read loop, and the refused command still fails with READONLY as before. Re-issuing it to the new primary would be safe, because the server didn't run it, but that changes retry behaviour, so I've left it out.
  • New log event 143 (Information) when this fires.

Tests

ReadOnlyReplyUnitTests, against the in-process server, with a subclass that refuses writes on a replica as a real server does:

  • ReadOnlyFromThePrimaryIsNoticedPromptly, via Sentinel and with plain endpoints, ConfigCheckSeconds = 60: swap the roles with no dropped connection and no announcement, then keep writing. On main, writes are still refused 10s later (about 635 refusals). With this change, one write is refused and the next goes to the new primary.
  • RepeatedReadOnlyIsRateLimited: a server that refuses every write but still reports itself a primary. About 235,000 refusals in 2.7s cause 3 role re-reads.
  • ReadOnlyFromAKnownReplicaChangesNothing: a module-style write to a known replica causes no re-reads. This fails if the replica check is removed.

The library builds clean across all target frameworks. All *UnitTests pass on net10.0, with one unrelated flaky test, EndpointPruningUnitTests.NodeCarryingSubscriptionsIsNotPruned, which passed 5 of 5 runs alone. The new tests also pass on net481.

A primary demoted in place (REPLICAOF, or a sentinel failover whose +switch-master
was missed) drops no connection, so the client kept routing writes to it until the
next periodic INFO replication - up to ConfigCheckSeconds (60s by default) of
refused writes. A READONLY reply from a server we treat as a primary now re-reads
the role of every connected server, at most once a second per multiplexer; the
existing reconfigure and sentinel demotion paths then elect the new primary.

Fixes #3383.

Claude-Session: https://claude.ai/code/session_01DUpQ8dy8FLBwADei2VzLmZ

This branch has not been deployed

No deployments
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.

Sentinel: a READONLY reply from the primary does not trigger a reconfigure

1 participant