Skip to content

Restore: make the dedicated subscription connection lazy - #3310

Merged
mgravell merged 1 commit into
mainfrom
marc/lazy-subscription-connection
Oct 30, 2026
Merged

mgravell merged 1 commit into
mainfrom
marc/lazy-subscription-connection

Conversation

@mgravell

Copy link
Copy Markdown
Member

Problem

The dedicated pub/sub connection (always under RESP2; under RESP3 unless SharedSubscriptionConnection is enabled) was opened eagerly as part of activating every endpoint. A client that never subscribes to anything still paid for a second socket per node, and a second reconnect per node on every failover.

This surfaced on Azure Managed Redis, whose options provider disables the configuration channel: 3.0.x showed a single RESP3 connection, but 3.4.x showed two, with the second idle. The hasSubscriptions gate sketched in #3067 was left commented out.

Change

  • ActivateServer opens the subscription bridge only when ServerEndPoint.RequiresEagerSubscriptionConnection is true: subscriptions supported, not shared, and either a configuration channel is set or subscriptions already exist (and may need to move to this endpoint). Otherwise the existing GetBridge paths create it on first SUBSCRIBE; with the default backlog policy that first command queues until the bridge is up.
  • The "second leg" connect-monitor completion in OnFullyEstablished and the maintenance handoff deadline check used IsSubscriberConnected, which reads an absent bridge as disconnected and would have hung the connect wait. Both now use IsSubscriberConnectedOrNotRequired.
  • The RESP3-wanted-but-negotiated-RESP2 fallback in OnFullyEstablished applies the same gate.
  • The endpoint-state connect log prints Subscription: NotRequired for a bridge that has not been asked for, instead of Disconnected.

Observed via CLIENT LIST

Probe: .NET 10 console app, ConfigurationOptions.Parse, Defaults set to AzureManagedRedisOptionsProvider (TLS off for the local plaintext server), against Redis 8.10.3 in docker. "Default" rows use the stock provider, which sets the configuration channel.

Before (3.4.20):

Provider Protocol Sockets at connect Flags
AMR RESP3 2 N sub=0 / N sub=0 (idle)
Default RESP3 2 N sub=0 / P sub=1
Default RESP2 2 N sub=0 / P sub=1

After (this branch):

Provider Protocol Sockets at connect Flags After explicit Subscribe
AMR RESP3 1 N sub=0 2: N / P sub=1
Default RESP3 2 N sub=0 / P sub=1 (config channel) same, sub=2
Default RESP2 2 N sub=0 / P sub=1 (config channel) same, sub=2

For reference, 3.0.25 with the AMR provider was 1 socket, N sub=0 (RESP3 shared mode, and it did not subscribe to the config channel under RESP3, a known gap).

Tests

  • AzureManagedRedisConnectionCount now asserts one connection before any pub/sub (all four protocol/sharing cells) and the expected count after.
  • New ConfigurationChannelOpensSubscriptionConnectionEagerly covers the eager path under RESP2 and RESP3, including the P flag and sub=1 on the second connection.
  • HelloClusterModeUnitTests.CrossSlotSurvivesSubscriptionReconnect and ConnectionAttemptUnitTests.SuccessfulAttemptsAreReported relied on the eager bridge; both now subscribe first, and the latter asserts no subscription attempt is reported before that.

Filtered local run (pub/sub, subscription, connection, config, maintenance, failover, sentinel, moved, default-options; net8.0): 844 passed, 0 failed, 89 skipped (no cluster/sentinel servers).

Caveat

With BacklogPolicy.FailFast, the first Subscribe on a long-connected multiplexer can throw NoConnectionAvailable: the bridge is created and still connecting at selection time. The subscription is still registered and completes once the bridge is up, but the caller sees an exception where the eager bridge previously hid it. If that matters, server selection for subscription commands could treat "interactive connected, subscription bridge connecting" as selectable; not changed here.

The dedicated pub/sub connection (always under RESP2; under RESP3 unless
SharedSubscriptionConnection is enabled) was opened eagerly as part of
activating every endpoint, so a client that never subscribes to anything
still paid for a second socket per node, and a second reconnect per node
on every failover. Seen on AMR, whose options provider disables the
configuration channel: 3.4.x showed two RESP3 connections where 3.0.x
showed one.

ActivateServer now opens that connection only when there is something to
subscribe to straight away: a configuration channel, or subscriptions that
already exist (and may need to move to this endpoint). Otherwise it is
created on first use, via the existing GetBridge paths; with the default
backlog policy the first SUBSCRIBE queues until it is up.

The connect wait ("second leg") and the maintenance handoff deadline
treated an absent subscription bridge as a disconnected one, which would
have left connects hanging; they now consider a connection that has not
been asked for as not required. The endpoint-state connect log says
"NotRequired" for that case instead of "Disconnected".

Tests: AzureManagedRedisConnectionCount now asserts one connection before
pub/sub and the expected count after; a new test covers the eager case
when a configuration channel is set; two tests that relied on the eager
subscription connection now subscribe first.
@mgravell mgravell changed the title Make the dedicated subscription connection lazy Restore: make the dedicated subscription connection lazy Oct 30, 2026
@mgravell
mgravell merged commit a4400f6 into main Oct 30, 2026
7 of 8 checks passed
@mgravell
mgravell deleted the marc/lazy-subscription-connection branch October 30, 2026 10:15
mgravell added a commit that referenced this pull request Oct 31, 2026
…, batch profiling, API promotion (#3322)

* Make the dedicated subscription connection lazy (#3310)

The dedicated pub/sub connection (always under RESP2; under RESP3 unless
SharedSubscriptionConnection is enabled) was opened eagerly as part of
activating every endpoint, so a client that never subscribes to anything
still paid for a second socket per node, and a second reconnect per node
on every failover. Seen on AMR, whose options provider disables the
configuration channel: 3.4.x showed two RESP3 connections where 3.0.x
showed one.

ActivateServer now opens that connection only when there is something to
subscribe to straight away: a configuration channel, or subscriptions that
already exist (and may need to move to this endpoint). Otherwise it is
created on first use, via the existing GetBridge paths; with the default
backlog policy the first SUBSCRIBE queues until it is up.

The connect wait ("second leg") and the maintenance handoff deadline
treated an absent subscription bridge as a disconnected one, which would
have left connects hanging; they now consider a connection that has not
been asked for as not required. The endpoint-state connect log says
"NotRequired" for that case instead of "Disconnected".

Tests: AzureManagedRedisConnectionCount now asserts one connection before
pub/sub and the expected count after; a new test covers the eager case
when a configuration channel is set; two tests that relied on the eager
subscription connection now subscribe first.

* Command coverage ledger and audit skill (#3311)

* Command coverage ledger and audit skill

Adds docs/CommandCoverage.md: every upstream Redis command the library does
not expose as a typed API, grouped and with a recorded decision (policy,
not applicable, deferred, todo, deprecated, enum-only, implemented). Seeded
from a diff of redis/redis src/commands (477 specs, @6a5fee67) against the
RedisCommand enum and source; it carries forward the dispositions from
#2055 (blocking is policy, Functions and ACL are deferred) and replaces
that hand-maintained list.

Adds the audit-command-coverage skill and its script, which re-runs the
diff on request and reports only what changed: new upstream commands with
no decision, ledger rows the code now covers, rows the spec no longer has,
and enum members the spec does not know. The script exits non-zero when
the ledger needs editing.

Closes #2055.

* Ledger: CLIENT TRACKING is v4, cluster/COMMAND introspection is administration, CLUSTER SHARDS note corrected

* Ledger: CLIENT PAUSE/UNPAUSE are operator tooling, not applicable

* Update RedisType.Bitmap and the keyspace-event tokens to reflect current server values (#3312)

* Object-type metadata for native bitmaps (redis/redis#15331), plus the tokens that diff turned up

RedisType gains Bitmap ("bitmap", the TYPE reply for a native roaring
bitmap, 8.12) and Gcra ("gcra", already in the server's type-name table).
KeyNotificationType gains SetBit (emitted under class $ for string bitmaps
and class b for native ones) and the other event names the server emits
that the enum lacked: arinsert, armset, arring, arseek, gcra, pfadd,
sexpire, spersist, sinterstore, zrangestore, and keymiss (class m, side
effect group).

Fixes the Copy token: the server has only ever emitted "copy_to", so the
"copy" token never parsed and a RedisChannel.KeyEvent(KeyNotificationType.Copy)
subscription never fired. The member and its value are unchanged.

Both parsers are generated from [AsciiHash]; tests extend the token tables.

* Drop Gcra: the type was withdrawn before release, so there is nothing for the token to match

* Add sunionstore/sdiffstore events (from the keyspace-notifications docs); drop the history comment on Copy

* Fix ConnectionGroupMember string-ctor dropping the name (#3314)

* Forward the name argument in ConnectionGroupMember's string constructor

The string overload chained to the ConfigurationOptions constructor without passing `name`, so every
member built from a configuration string (the idiom used throughout docs/Failover.md) was named after
its first endpoint by Init() instead of the name the caller gave it. Regression test added.

* Parenthesize a Debug-only assertion that trips SA1407

`endSegmentLength == 0 & endSegmentCommitted == 0` in DebugAssertValid sits inside `#if DEBUG`, so CI
(Release) never compiles it and never saw the StyleCop error; any local Debug build of RESPite failed
with TreatWarningsAsErrors. Introduced by ee47e53 after the StyleCop bump in aa1e5c0.

* Parse unbracketed IPv6 ip:port from the server (#1422) (#3307)

* Parse unbracketed IPv6 ip:port from the server (#1422)

CLUSTER NODES, MOVED/ASK and CLIENT LIST write IPv6 addresses without
brackets, i.e. "2a06:4e80:3:a::a:7000". The RFC 3986 parser used for
configuration strings reads that as a (valid!) eight-group address with
no port, so cluster nodes ended up as "[2a06:4e80:3:a::a:7000]:0" and
redirections and client addresses were equally wrong.

Add Format.TryParseServerEndPoint for the server's "ip:port" form: when
there is no ']' and more than one ':', split at the last colon and
require both halves to parse; anything else goes to the existing parser
unchanged, so configuration parsing is not touched. Use it at the three
sites that receive server-emitted text.

The sentinel +switch-master handler gets host and port as separate
tokens and was gluing them back together with ':' before parsing; pass
them to the (host, port) overload instead.

* Parse unbracketed IPv6 in MOVING/SMIGRATED endpoints

ParseMigrationEndPoint used the configuration-string parser, so an unbracketed IPv6 address parsed as an address with port 0, which the placeholder check then discarded. Use TryParseServerEndPoint, as the other server-emitted sites do; tests for the MOVING successor and SMIGRATED source/target.

* Profile commands sent in a batch (#2175) (#3313)

Co-authored-by: mohsinaliwork686-rgb <229973101+mohsinaliwork686-rgb@users.noreply.github.com>

* public-api.py: apply *REMOVED* at promotion; promote the API surface since 3.4.0 (#3316)

* public-api.py: apply *REMOVED* entries at promotion instead of copying them

The analyzer records a vanished shipped signature as a *REMOVED* line in
the unshipped file. Promotion moved every unshipped line across verbatim,
so a promotion after any such change would have left the shipped file
with both the dead signature and a *REMOVED* copy of it, and the analyzer
would then fail the build on the dead one (RS0017).

Promotion now consumes the marker: the named signature is dropped from the
shipped file, the marker is not carried over, and a marker that names
nothing shipped is an error. A *REMOVED* line already sitting in a shipped
file is also reported. The "+N promoted" note gains "-N removed", and an
unshipped file holding only removals is still emptied.

* Promote the shipped API surface: everything since 3.4.0

eng/public-api.py --promote. Fifty-two StackExchange.Redis entries (the
lag-aware availability surface, FailbackHealthCheck, HealthCheckProbeScope,
the 8.12-independent RedisType and KeyNotificationType tokens) and the
net6.0 SetUserPemCertificate overload move to shipped; the three parameter
renames from #3296 (muxer -> multiplexer on the Azure providers'
AfterConnectAsync, other -> obj on RESPite AsciiHash.Equals) are applied
rather than copied. Every unshipped file is now empty.

* Split third-party notices out of LICENSE so GitHub detects MIT (#3317)

* Split third-party notices out of LICENSE so GitHub detects MIT

The LICENSE file carried ~1.1KB of third-party notes after the MIT text,
which drops it below licensee's similarity threshold; GitHub reports the
repo license as "Other" (SPDX NOASSERTION). Keep LICENSE as the bare MIT
text and move the notes, verbatim, to THIRD-PARTY-NOTICES.md.

* Correct the third-party notices

- Redis: BSD-3 only through 7.2; RSALv2/SSPLv1 from 7.4, AGPLv3 added in
  8.0; point at redis.io/legal/licenses (the old /topics/license URL now
  redirects to the docs landing page).
- redis-doc: repo is archived under redis/redis-doc; note that redis/docs,
  its successor, is CC BY-NC-SA for newer content.
- Drop the Redis-64 and BookSleeve notes: neither is part of the
  development solution any more.
- Add the code actually incorporated from other projects (dotnet/runtime,
  dotnet/corefx, aspnet/BasicMiddleware) and the test-only redis-sharp copy,
  none of which the old notes mentioned.

---------

Co-authored-by: Mohsin Ali <mohsin.ali.work686@gmail.com>
Co-authored-by: mohsinaliwork686-rgb <229973101+mohsinaliwork686-rgb@users.noreply.github.com>
mgravell added a commit that referenced this pull request Oct 31, 2026
A configuration channel makes the dedicated subscription connection part
of connecting (#3310): ActivateServer dials it eagerly, but the dial is
fire-and-forget by design, and the reconfigure connect wait covered the
interactive leg alone - so on a loaded machine ConnectAsync returned
while the library's own broadcast channel was still dialling.
ConfigurationChannelOpensSubscriptionConnectionEagerly (merged with the
3.4.29 sync) fails exactly there on CI, on both OSes, while fast local
machines win the race.

The wait itself broadens: when RequiresEagerSubscriptionConnection, the
endpoint's availability task also awaits the subscription executor's
ConnectNowAsync (short-circuits when up, joins the dial in flight,
declines onto a completed task under backoff, so the wait stays bounded
by the pass's budget), with a subscription-leg failure deliberately not
faulting the endpoint's availability - the interactive leg decides
selectability, and a refused subscription dial redials on demand.

Deliberately NOT by gating the connect monitors on the second leg, which
is how the v3 core does it (OnFullyEstablished): those monitors complete
on every reconnect, and gating them there re-times subscription
re-placement against one-shot publishes - Resp3DowngradeTests'
SubscribeQueuedBeforeDowngradeDoesNotPoisonInteractive fails
deterministically with the PUBLISH answered :0, the re-SUBSCRIBE a beat
behind it. The monitors keep their single-leg meaning; only the
reconfigure wait, which is what ConnectAsync means by connected, grows
the second leg.

The new test pins the race deterministically: an in-process server that
is slow to accept only the subscription leg (OnAcceptClientAsync now
has an overload seeing the declared connection type) must delay
ConnectAsync, not be escaped by it. Red on unfixed source with the CI
failure's exact shape; green with the fix, both protocols.
mgravell added a commit that referenced this pull request Oct 31, 2026
…3325)

A configuration channel makes the dedicated subscription connection part
of connecting (#3310): ActivateServer dials it eagerly, but the dial is
fire-and-forget by design, and the reconfigure connect wait covered the
interactive leg alone - so on a loaded machine ConnectAsync returned
while the library's own broadcast channel was still dialling.
ConfigurationChannelOpensSubscriptionConnectionEagerly (merged with the
3.4.29 sync) fails exactly there on CI, on both OSes, while fast local
machines win the race.

The wait itself broadens: when RequiresEagerSubscriptionConnection, the
endpoint's availability task also awaits the subscription executor's
ConnectNowAsync (short-circuits when up, joins the dial in flight,
declines onto a completed task under backoff, so the wait stays bounded
by the pass's budget), with a subscription-leg failure deliberately not
faulting the endpoint's availability - the interactive leg decides
selectability, and a refused subscription dial redials on demand.

Deliberately NOT by gating the connect monitors on the second leg, which
is how the v3 core does it (OnFullyEstablished): those monitors complete
on every reconnect, and gating them there re-times subscription
re-placement against one-shot publishes - Resp3DowngradeTests'
SubscribeQueuedBeforeDowngradeDoesNotPoisonInteractive fails
deterministically with the PUBLISH answered :0, the re-SUBSCRIBE a beat
behind it. The monitors keep their single-leg meaning; only the
reconfigure wait, which is what ConnectAsync means by connected, grows
the second leg.

The new test pins the race deterministically: an in-process server that
is slow to accept only the subscription leg (OnAcceptClientAsync now
has an overload seeing the declared connection type) must delay
ConnectAsync, not be escaped by it. Red on unfixed source with the CI
failure's exact shape; green with the fix, both protocols.
rebaseandpanic added a commit to rebaseandpanic/StackExchange.Redis that referenced this pull request Nov 3, 2026
… on demand

ConcurrentManagedConnectReturnsOneChildAndDisposesTheOther waited for
exactly two connections on the data node, the winner's interactive and
subscription ones. Since seredis#3310 the dedicated subscription connection is
opened only when there is something to subscribe to, and this test
configuration has no configuration channel, so the winner holds one
connection and the wait timed out although the losing multiplexer was
disposed. The test now waits for the data node to hold exactly the
connections the winner reports.

SentinelAddressIsRetainedWhileReconfigureIsBusy polled GetServer, which
throws while the peer is not yet part of the connection; it now treats
an unknown endpoint as not connected yet.
mgravell added a commit that referenced this pull request Nov 13, 2026
…eyed routing tests, SER1xx experiment block (#3355)

* Make the dedicated subscription connection lazy (#3310)

The dedicated pub/sub connection (always under RESP2; under RESP3 unless
SharedSubscriptionConnection is enabled) was opened eagerly as part of
activating every endpoint, so a client that never subscribes to anything
still paid for a second socket per node, and a second reconnect per node
on every failover. Seen on AMR, whose options provider disables the
configuration channel: 3.4.x showed two RESP3 connections where 3.0.x
showed one.

ActivateServer now opens that connection only when there is something to
subscribe to straight away: a configuration channel, or subscriptions that
already exist (and may need to move to this endpoint). Otherwise it is
created on first use, via the existing GetBridge paths; with the default
backlog policy the first SUBSCRIBE queues until it is up.

The connect wait ("second leg") and the maintenance handoff deadline
treated an absent subscription bridge as a disconnected one, which would
have left connects hanging; they now consider a connection that has not
been asked for as not required. The endpoint-state connect log says
"NotRequired" for that case instead of "Disconnected".

Tests: AzureManagedRedisConnectionCount now asserts one connection before
pub/sub and the expected count after; a new test covers the eager case
when a configuration channel is set; two tests that relied on the eager
subscription connection now subscribe first.

* Command coverage ledger and audit skill (#3311)

* Command coverage ledger and audit skill

Adds docs/CommandCoverage.md: every upstream Redis command the library does
not expose as a typed API, grouped and with a recorded decision (policy,
not applicable, deferred, todo, deprecated, enum-only, implemented). Seeded
from a diff of redis/redis src/commands (477 specs, @6a5fee67) against the
RedisCommand enum and source; it carries forward the dispositions from
#2055 (blocking is policy, Functions and ACL are deferred) and replaces
that hand-maintained list.

Adds the audit-command-coverage skill and its script, which re-runs the
diff on request and reports only what changed: new upstream commands with
no decision, ledger rows the code now covers, rows the spec no longer has,
and enum members the spec does not know. The script exits non-zero when
the ledger needs editing.

Closes #2055.

* Ledger: CLIENT TRACKING is v4, cluster/COMMAND introspection is administration, CLUSTER SHARDS note corrected

* Ledger: CLIENT PAUSE/UNPAUSE are operator tooling, not applicable

* Update RedisType.Bitmap and the keyspace-event tokens to reflect current server values (#3312)

* Object-type metadata for native bitmaps (redis/redis#15331), plus the tokens that diff turned up

RedisType gains Bitmap ("bitmap", the TYPE reply for a native roaring
bitmap, 8.12) and Gcra ("gcra", already in the server's type-name table).
KeyNotificationType gains SetBit (emitted under class $ for string bitmaps
and class b for native ones) and the other event names the server emits
that the enum lacked: arinsert, armset, arring, arseek, gcra, pfadd,
sexpire, spersist, sinterstore, zrangestore, and keymiss (class m, side
effect group).

Fixes the Copy token: the server has only ever emitted "copy_to", so the
"copy" token never parsed and a RedisChannel.KeyEvent(KeyNotificationType.Copy)
subscription never fired. The member and its value are unchanged.

Both parsers are generated from [AsciiHash]; tests extend the token tables.

* Drop Gcra: the type was withdrawn before release, so there is nothing for the token to match

* Add sunionstore/sdiffstore events (from the keyspace-notifications docs); drop the history comment on Copy

* Fix ConnectionGroupMember string-ctor dropping the name (#3314)

* Forward the name argument in ConnectionGroupMember's string constructor

The string overload chained to the ConfigurationOptions constructor without passing `name`, so every
member built from a configuration string (the idiom used throughout docs/Failover.md) was named after
its first endpoint by Init() instead of the name the caller gave it. Regression test added.

* Parenthesize a Debug-only assertion that trips SA1407

`endSegmentLength == 0 & endSegmentCommitted == 0` in DebugAssertValid sits inside `#if DEBUG`, so CI
(Release) never compiles it and never saw the StyleCop error; any local Debug build of RESPite failed
with TreatWarningsAsErrors. Introduced by ee47e53 after the StyleCop bump in aa1e5c0.

* Parse unbracketed IPv6 ip:port from the server (#1422) (#3307)

* Parse unbracketed IPv6 ip:port from the server (#1422)

CLUSTER NODES, MOVED/ASK and CLIENT LIST write IPv6 addresses without
brackets, i.e. "2a06:4e80:3:a::a:7000". The RFC 3986 parser used for
configuration strings reads that as a (valid!) eight-group address with
no port, so cluster nodes ended up as "[2a06:4e80:3:a::a:7000]:0" and
redirections and client addresses were equally wrong.

Add Format.TryParseServerEndPoint for the server's "ip:port" form: when
there is no ']' and more than one ':', split at the last colon and
require both halves to parse; anything else goes to the existing parser
unchanged, so configuration parsing is not touched. Use it at the three
sites that receive server-emitted text.

The sentinel +switch-master handler gets host and port as separate
tokens and was gluing them back together with ':' before parsing; pass
them to the (host, port) overload instead.

* Parse unbracketed IPv6 in MOVING/SMIGRATED endpoints

ParseMigrationEndPoint used the configuration-string parser, so an unbracketed IPv6 address parsed as an address with port 0, which the placeholder check then discarded. Use TryParseServerEndPoint, as the other server-emitted sites do; tests for the MOVING successor and SMIGRATED source/target.

* Profile commands sent in a batch (#2175) (#3313)

Co-authored-by: mohsinaliwork686-rgb <229973101+mohsinaliwork686-rgb@users.noreply.github.com>

* public-api.py: apply *REMOVED* at promotion; promote the API surface since 3.4.0 (#3316)

* public-api.py: apply *REMOVED* entries at promotion instead of copying them

The analyzer records a vanished shipped signature as a *REMOVED* line in
the unshipped file. Promotion moved every unshipped line across verbatim,
so a promotion after any such change would have left the shipped file
with both the dead signature and a *REMOVED* copy of it, and the analyzer
would then fail the build on the dead one (RS0017).

Promotion now consumes the marker: the named signature is dropped from the
shipped file, the marker is not carried over, and a marker that names
nothing shipped is an error. A *REMOVED* line already sitting in a shipped
file is also reported. The "+N promoted" note gains "-N removed", and an
unshipped file holding only removals is still emptied.

* Promote the shipped API surface: everything since 3.4.0

eng/public-api.py --promote. Fifty-two StackExchange.Redis entries (the
lag-aware availability surface, FailbackHealthCheck, HealthCheckProbeScope,
the 8.12-independent RedisType and KeyNotificationType tokens) and the
net6.0 SetUserPemCertificate overload move to shipped; the three parameter
renames from #3296 (muxer -> multiplexer on the Azure providers'
AfterConnectAsync, other -> obj on RESPite AsciiHash.Equals) are applied
rather than copied. Every unshipped file is now empty.

* Split third-party notices out of LICENSE so GitHub detects MIT (#3317)

* Split third-party notices out of LICENSE so GitHub detects MIT

The LICENSE file carried ~1.1KB of third-party notes after the MIT text,
which drops it below licensee's similarity threshold; GitHub reports the
repo license as "Other" (SPDX NOASSERTION). Keep LICENSE as the bare MIT
text and move the notes, verbatim, to THIRD-PARTY-NOTICES.md.

* Correct the third-party notices

- Redis: BSD-3 only through 7.2; RSALv2/SSPLv1 from 7.4, AGPLv3 added in
  8.0; point at redis.io/legal/licenses (the old /topics/license URL now
  redirects to the docs landing page).
- redis-doc: repo is archived under redis/redis-doc; note that redis/docs,
  its successor, is CC BY-NC-SA for newer content.
- Drop the Redis-64 and BookSleeve notes: neither is part of the
  development solution any more.
- Add the code actually incorporated from other projects (dotnet/runtime,
  dotnet/corefx, aspnet/BasicMiddleware) and the test-only redis-sharp copy,
  none of which the old notes mentioned.

* The read loop stops matching replies once teardown owns the response queue; a close with commands in flight no longer completes a call with the previous call's reply (#3330)

RecordConnectionFailed drains _writtenAwaitingResponse one message at a
time, completing each outside the lock, while the read loop can still be
matching replies that were already in the receive buffer. Each reply the
loop matched after the drain had taken a message went to the message
*after* the one it answered: GET key N completed, successfully, with key
N-1's value, and every reply behind it was off by one. Reproduced on
main with the reporter's loop: 616 wrong replies in 20,000 calls.

_isShutdown was already set under the queue's lock before the drain, and
the loop already consulted it - but only once the queue was empty.
Checking it before the dequeue closes the race: a reply arriving after
teardown has begun is dropped with the connection, and the message it
answered is failed by the drain as before.

The test re-creates the scenario against the real server (20 rounds of
500 in-flight GETs closed with allowCommandsToComplete: false) and
asserts that no call completes with another call's value; without the
fix it sees ~200 in a run.

Fixes #3329

* CLUSTER SLOT-STATS (8.2): per-slot usage statistics on IServer, experimental SER012 (#3341)

* CLUSTER SLOT-STATS (8.2): per-slot usage statistics on IServer, experimental SER012

IServer.ClusterSlotStatsBySlotRange(Async) and ClusterSlotStatsOrderBy(Async), modelled
on the CLUSTER SLOTS file layout and the HOTKEYS field handling: node-local read, lenient
nominal parsing, config-gated metrics as nullable properties (absent != zero), unknown
metrics preserved as Metadata, cpu-usec surfaced as TimeSpan. RedisFeatures.ClusterSlotStats
gates on 8.2 RC1. Promotes the CommandCoverage 'deferred' row.

* The storage-kind tripwire acknowledges SLOT-STATS and SLOTSRANGE: two new >8-byte literals, 28 -> 30

* Check the maximum operation count in HeartbeatConsistencyCheckPingsAsync (#3342)

* In-repo links follow the org move to seredis (#3326): github.com/StackExchange/StackExchange.Redis -> github.com/seredis/StackExchange.Redis everywhere, including the nupkg RepositoryUrl/PackageReleaseNotes metadata (#3344)

* MOVING handoff: hold new work in the backlog while the in-flight commands drain, so the recycle happens within a round trip instead of at the window end (#3347)

* MOVING handoff: hold new work in the backlog while the in-flight commands drain, so the recycle happens within a round trip instead of at the window end

The drain waited for "no caller work", which under steady traffic never comes: there is always a command in flight, so the recycle waited out the whole announced window and failed whatever was in flight at that instant. Measured live (endpoint rebind, single proxy, ~900 ops/s): recycle at +20.0s against a MOVING at +6.0s, 16 failed commands, missed-deadline warning.

While a handoff drains, the bridge holds a caller's work in the backlog instead of writing it to the connection about to be replaced; the backlog processor does not flush while the hold stands; the drain waits only for what is already written, capped at half the relaxed timeout (bounded by half the remaining window, floor one second); the connection is then recycled as before, the hold cleared, and the held work flushed to the replacement by the ordinary reconnect path. Timeouts are relaxed for the hold and the reconnect. A FailFast policy does not abandon held work for the handoff's own disconnect. Same scenario on this branch: recycle at +6.2s against a MOVING at +5.9s, 0 failed of 97k.

Deliberately a flag and nothing more: no per-message timeout cover (with relaxation off and a timeout shorter than the hold, held commands can time out in the backlog, as on any reconnect), and no retire under the write lock (a writer that reaches the lock after the recycle gets a write fault, as before). Also: the drain budget is recomputed after the DNS poll, a second MOVING during a handoff updates the successor pin rather than being dropped, and the pin lasts the window so the second bridge dials the same successor.

Tests: steady load (with and without relaxation), slow command inside the cap, blocking command past the cap; plus a fault-injector tier measurement that records the live timeline.

* OneLogicalEventFromEveryNodeRaisesOneEvent: wait for the one event rather than counting at the instant the second node relaxed; the callback is dispatched after the relaxation, so the count could be read as zero (flaked on main at ~1 in 6)

* INCREX and ARGREP are routed by key on a cluster (#3352)

IncrexMessageBase (#3065) and ArrayGrepMessage (#3076) derived from Message
directly, held a key, and never overrode GetHashSlot, so they reported
NoSlot. On a cluster that sends the command to an arbitrary node and leans
on -MOVED: an extra round trip for most calls, and a RedisServerException
under CommandFlags.NoRedirect.

Both now derive from Message.CommandKeyBase, as their single-key siblings
do, which supplies the slot (and the key in CommandAndKey).

Affects the StringIncrement overloads that take an Expiration (INCREX) and
ArrayGrep. No public API change.

---------

Co-authored-by: Mohsin Ali <mohsin.ali.work686@gmail.com>
Co-authored-by: mohsinaliwork686-rgb <229973101+mohsinaliwork686-rgb@users.noreply.github.com>
Co-authored-by: Nikolay Zdravkov <unsafeptr@gmail.com>
mgravell added a commit that referenced this pull request Dec 2, 2026
…3301)

* fix(sentinel): discover and recover without blocking thread-pool workers

A sentinel-managed connection that loses its primary arms a reconnect timer
that fires every second until OnManagedConnectionRestored stops it. Each tick
ran SwitchPrimary, which asked every sentinel through a PLINQ query of
synchronous commands: one blocked pool worker per sentinel endpoint, waiting
on socket completions that are themselves pool work items. With a handful of
sentinel endpoints this takes the whole pool, and the process stalls until
the pool injects threads - every second, for as long as the timer runs. The
timer can also run forever: a node that never returns at its old address, or
a Restored delivered before its Failed, leaves nothing to stop it.

- Sentinel queries use the async API, asking every sentinel at once with one
  deadline for the whole query; the first answer wins and the rest are only
  observed. No worker waits on a sentinel.
- SwitchPrimary becomes SwitchPrimaryAsync, with a per-connection gate around
  discovery and reconfiguration. A switch requested while one is running is
  recorded and replayed instead of overlapping it.
- The reconnect timer is one-shot: the next attempt is armed one second after
  the previous one finished. It stops on state - the sentinel-reported primary
  is connected, is not a replica, and no other node is a connected primary -
  rather than on the order Failed/Restored arrive in.
- The +switch-master, +sentinel, lost-sentinel, restore and demotion paths
  start async work instead of blocking their caller; demotion recovery sends
  INFO through TryWriteAsync, and a write that did not go out is not taken as
  a role check.
- GetSentinelMasterConnection and the async connect share one async core. The
  public synchronous Connect keeps its soft ConnectTimeout behaviour with
  AbortOnConnectFail=false, and is the only remaining synchronous wait.
- Two concurrent GetSentinelMasterConnection calls for one service return the
  same child; the losing connection is disposed.
- Restored no longer runs a switch unconditionally: a connection already on
  the sentinel-reported primary, with no stale view and no recovery running,
  is left alone; anything else schedules the same recovery as Failed.
- The sentinel address list is refreshed after the first switch of a managed
  connection as well, not only after a switch that reconfigured it.

Which sentinel answer the client accepts, and how it reconciles replicas, is
unchanged here.

Spec:
https://redis.io/docs/latest/develop/reference/sentinel-clients/#step-1-connect-to-the-first-sentinel
https://redis.io/docs/latest/develop/reference/sentinel-clients/#step-2-ask-for-the-master-address
https://redis.io/docs/latest/develop/reference/sentinel-clients/#handle-reconnections
https://valkey.io/topics/sentinel-clients/#step-1-connect-to-the-first-sentinel
https://valkey.io/topics/sentinel-clients/#step-2-ask-for-primary-address
https://valkey.io/topics/sentinel-clients/#handling-reconnections

* fix(sentinel): take only a healthy primary answer, and keep the replica view consistent

With every sentinel asked at once, the first answer wins - so which answer is
taken matters more than when the sentinels were asked one by one.

- The primary is read with SENTINEL MASTER and accepted only from a sentinel
  that holds it as a healthy master (flags contain master, no s_down/o_down).
  A sentinel still repeating a failed primary is passed over for one that
  already knows the new one. get-master-addr-by-name, which carries no flags,
  is the fallback only when SENTINEL MASTER itself is not supported; any other
  error is not a reason to trust a flagless answer.
- Replicas are read from the sentinel whose primary answer was accepted, not
  from a second race that could combine two unrelated views.
- Every switch refreshes the replica set, also for a primary the connection
  already knows; before, only an unknown primary did, so a replica that came
  back at a new address left the old one in the snapshot for good.
- Sentinel keeps reporting failed replicas (it never forgets them until
  SENTINEL RESET). Down flags only decide whether a node not yet held is added;
  a node this connection holds and is connected to is kept whatever one
  sentinel's momentary view says. Only a disconnected node missing from a
  nonempty reply is retired, and an empty or failed replica query retires
  nothing.

Spec:
https://redis.io/docs/latest/develop/reference/sentinel-clients/#step-2-ask-for-the-master-address
https://redis.io/docs/latest/develop/reference/sentinel-clients/#connect-to-replicas
https://redis.io/docs/latest/develop/reference/sentinel-clients/#sentinel-failover-disconnection
https://valkey.io/topics/sentinel-clients/#step-2-ask-for-primary-address
https://valkey.io/topics/sentinel-clients/#connecting-to-replicas
https://valkey.io/topics/sentinel-clients/#sentinel-failover-disconnection

* fix(sentinel): keep every sentinel route, parse +sentinel, ask reported-down routes last

- The +sentinel handler passed messageParts[0] - the literal instance type
  "sentinel" - as the service name, so the address refresh it triggers never
  matched a service and the announcement was never acted on. The service is
  read after the "@" separator.
- The sentinel list stays append-only, as before: seeds and routes found
  earlier are kept, and a Service address and the pod address the same
  sentinel advertises are both kept without being matched to each other.
  What changes is how a refresh runs: new routes are added before the
  reconfigure is claimed, so a busy reconfigure cannot drop what was
  discovered, and a refresh requested while one is running is replayed
  rather than lost.
- Every route is still asked at once, but connected routes are started first
  and routes sentinel itself reports down last.

Spec:
https://redis.io/docs/latest/develop/reference/sentinel-clients/#step-1-connect-to-the-first-sentinel
https://redis.io/docs/latest/develop/reference/sentinel-clients/#sentinels-list-automatic-refresh
https://redis.io/docs/latest/develop/reference/sentinel-clients/#subscribe-to-sentinel-events-to-improve-responsiveness
https://valkey.io/topics/sentinel-clients/#step-1-connect-to-the-first-sentinel
https://valkey.io/topics/sentinel-clients/#sentinels-list-automatic-refresh
https://valkey.io/topics/sentinel-clients/#subscribe-to-sentinel-events-to-improve-responsiveness

* fix(sentinel): a READONLY reply marks a managed node as a replica

A primary demoted in place answers writes with READONLY long before the next
INFO replication poll (ConfigCheckSeconds, 60s by default) notices the role
change, and until then commands keep failing against it. On a
sentinel-managed connection a READONLY reply now marks the node as a replica
right away - locally, without starting a reconfigure on the IO path - and
the demotion heartbeat then asks sentinel and switches, as it does for a
demotion seen by INFO.

Standalone and cluster connections keep their existing READONLY behaviour.

Spec:
https://redis.io/docs/latest/develop/reference/sentinel-clients/#step-3-call-the-role-command-in-the-target-instance
https://redis.io/docs/latest/develop/reference/sentinel-clients/#sentinel-failover-disconnection
https://valkey.io/topics/sentinel-clients/#step-3-call-the-role-command-in-the-target-instance
https://valkey.io/topics/sentinel-clients/#sentinel-failover-disconnection

* fix(sentinel): ask get-master-addr-by-name when SENTINEL MASTER gives no usable answer

Before this change only an "unknown command/subcommand" error took the
get-master-addr-by-name path. Any other error (NOPERM for an ACL user
allowed only what discovery needed before) or a reply without flags was
treated as no answer, so such a sentinel could no longer be used.

Now any server error, and any reply without the master flag or an
address, asks get-master-addr-by-name at the same sentinel - which is
what every sentinel was asked before SENTINEL MASTER was used. Only a
sentinel that explicitly holds the node down (s_down/o_down) is passed
over. A stale address from that path is caught by the ROLE check, as it
always was.

Spec:
https://redis.io/docs/latest/develop/reference/sentinel-clients/#step-2-ask-for-the-master-address
https://redis.io/docs/latest/develop/reference/sentinel-clients/#step-3-call-the-role-command-in-the-target-instance
https://valkey.io/topics/sentinel-clients/#step-2-ask-for-primary-address
https://valkey.io/topics/sentinel-clients/#step-3-call-the-role-command-in-the-target-instance

* fix(sentinel): add every replica sentinel lists, as before

A replica that sentinel reported down (s_down/o_down/disconnected) was
skipped at connect and in the switch. Recovery stops once the primary is
healthy, so nothing asked sentinel again: a replica that was down while
the client started - a rolling restart - was never used.

Every listed replica is added again, whatever its flags, and its bridge
keeps reconnecting until it answers, as it did before. This does not
bring back the reconnect storm: a dead replica reports its connection
failure once per connection, not on every retry, and the switch that
follows finds a healthy primary and stops the timer.

The cost is the old one too: sentinel keeps listing a replica until
SENTINEL RESET, so a replica that came back at a new address leaves its
old address retried for the life of the connection.

Spec:
https://redis.io/docs/latest/develop/reference/sentinel-clients/#connect-to-replicas
https://valkey.io/topics/sentinel-clients/#connecting-to-replicas

* fix(sentinel): rebuild the server list only for a new primary, as before

The switch re-read the replicas and reshaped the endpoint set on every
switch, also around a primary the connection already knew, and kept any
connected node sentinel no longer named. Two things went wrong with that:

- An old primary left out of the service that stays reachable and keeps
  answering role:master was never retired. The connection then saw a
  second primary, the switch never counted as healthy, and the reconnect
  timer asked sentinel and reconfigured every node once a second for as
  long as that node lived.
- A replica that was briefly disconnected while one sentinel returned an
  incomplete list was retired, and not used again after it came back.

The endpoint set is changed again only when sentinel names a primary the
connection has never seen: it is rebuilt from that primary and every
listed replica, and every other node is retired, connected or not. A
failed replica query keeps the old nodes. A known primary keeps its set;
a stale role view still gets a full reconfigure.

Replica addresses are again asked of every sentinel at once, the first
answer winning, instead of only the sentinel whose primary answer won:
that one may refuse the replica query (an ACL), or drop its connection
after answering, while another sentinel can answer.

Spec:
https://redis.io/docs/latest/develop/reference/sentinel-clients/#handle-reconnections
https://redis.io/docs/latest/develop/reference/sentinel-clients/#connect-to-replicas
https://valkey.io/topics/sentinel-clients/#handling-reconnections
https://valkey.io/topics/sentinel-clients/#connecting-to-replicas

* refactor(sentinel): ask every sentinel route at once without ordering them

The sentinel queries were launched connected routes first and routes
sentinel reports down last, which meant parsing the peer flags out of
SENTINEL SENTINELS and keeping them between refreshes. Every route is
asked at once and the first usable answer wins, so the launch order
never chose the answer; it is dropped, and SENTINEL SENTINELS is read
for addresses only, as before.

Spec:
https://redis.io/docs/latest/develop/reference/sentinel-clients/#step-1-connect-to-the-first-sentinel
https://redis.io/docs/latest/develop/reference/sentinel-clients/#sentinels-list-automatic-refresh
https://valkey.io/topics/sentinel-clients/#step-1-connect-to-the-first-sentinel
https://valkey.io/topics/sentinel-clients/#sentinels-list-automatic-refresh

* docs(sentinel): sentinel queries wait up to AsyncTimeout, also from Connect

Sentinel discovery is asynchronous on every path, so a sentinel that has
stopped answering is waited on for AsyncTimeout, not SyncTimeout, even
when the connection is made through the synchronous Connect. With the
two timeouts equal nothing changes.

The paragraph on which sentinel answer is followed is reworded to say
what the client does now: a sentinel holding the primary down is passed
over, and one without a usable SENTINEL MASTER reply is asked
get-master-addr-by-name.

Spec:
https://redis.io/docs/latest/develop/reference/sentinel-clients/#step-2-ask-for-the-master-address
https://valkey.io/topics/sentinel-clients/#step-2-ask-for-primary-address

* test(sentinel): do not count a subscription connection that is opened on demand

ConcurrentManagedConnectReturnsOneChildAndDisposesTheOther waited for
exactly two connections on the data node, the winner's interactive and
subscription ones. Since #3310 the dedicated subscription connection is
opened only when there is something to subscribe to, and this test
configuration has no configuration channel, so the winner holds one
connection and the wait timed out although the losing multiplexer was
disposed. The test now waits for the data node to hold exactly the
connections the winner reports.

SentinelAddressIsRetainedWhileReconfigureIsBusy polled GetServer, which
throws while the peer is not yet part of the connection; it now treats
an unknown endpoint as not connected yet.

* test(sentinel): follow a primary that sentinel corrects without a failover

Sentinel first names an unreachable primary, the client connects with
AbortOnConnectFail=false, then sentinel names the real primary with no
+switch-master and no connection dropped. Covers Connect and ConnectAsync,
including a primary held down (s_down) at connect: the client moves to the
corrected primary, serves commands and pub/sub there, a handler subscribed
while stuck receives messages, and a command queued while stuck does not
wait out its timeout.

* fix(sentinel): wait at the address sentinel names when it holds the primary down

When every sentinel answers SENTINEL MASTER with s_down/o_down, discovery
found no address and the connect fell through to seeding with the
configured endpoints. A sentinel host given without a port then got the
default data port, and whatever Redis listened there served the client.

Connect now asks get-master-addr-by-name in a second pass once the healthy
pass has come back empty, so a slow healthy answer still wins and, during a
failover, the replica being promoted is taken rather than the old primary.
The initial switch asks for a healthy primary first, as every switch does,
and takes the held-down address only when there is none: AbortOnConnectFail=true
connects to a reachable primary sentinel holds down, as before, and a
sentinel put right while the client was connecting is followed at once.

A connection seeded because no sentinel answered at all takes the
held-down address in its switch too, but only while it is still on that
seed; everywhere else no healthy primary still means "ask again later". The
seed flag is cleared only once the switch holds the reconfigure gate and has
rebuilt the endpoint set, so an attempt that lost the gate is repeated on the
next tick, and the seed nodes are retired even when the replica query fails.

---------

Co-authored-by: Marc Gravell <marc.gravell@gmail.com>
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.

1 participant