Skip to content

Set command side-effect category for WithRetry - #540

Merged
mgravell merged 3 commits into
masterfrom
marc/command-categories
Jul 31, 2026
Merged

mgravell merged 3 commits into
masterfrom
marc/command-categories

Conversation

@mgravell

@mgravell mgravell commented Jul 31, 2026 •

Copy link
Copy Markdown
Collaborator

SE.Redis 3.1.0 adds "retry" support; this needs context on the operation being performed (see here), so: we add these.

Note that I have intentionally not added a 3.1.0 dependency yet; the flags work fine with or without that.


Note

Medium Risk
Wide behavioral change under failover/retry: mis-categorized commands could double-apply writes or skip safe retries; mitigated by exhaustive categorization and unit tests pinning argument-dependent cases.

Overview
Adds side-effect categories on every Redis Stack command so StackExchange.Redis 3.1+ WithRetry can decide whether a lost reply may be replayed. Categories are carried on SerializedCommand (CommandCategory / EffectiveFlags) via a new CommandCategories ladder that mirrors CommandFlags.CommandRetry* using cast constants so the library still builds against SE.Redis 3.0.x (extra bits are ignored there).

Dispatch now passes command.EffectiveFlags instead of a single global CommandFlags value; uncategorized command paths use SerializedCommand.Uncategorized, and the old SerializedCommand constructors are [Obsolete] with CS0618 treated as an error in the main project so all builders must categorize.

Per-module builders (Bloom, JSON, Search, TimeSeries, etc.) tag each command as read-only, conditional write, last-wins, accumulating, never-retry, server-specific, and so on. Notable argument-dependent rules include JSON.SET (NX/XX → WriteChecked), RediSearch cursor / WITHCURSOR → Never, blocking list/stream pops → WriteAccumulating, and TS.ADD category from TsAddParams.ResolveCategory (timestamp *, ON_DUPLICATE, etc.). ExecuteBroadcast gains overloads that accept an explicit category.

Tests pin category bit alignment with SE.Redis 3.1.0 and cover the tricky cases in CommandCategoryTests.

Reviewed by Cursor Bugbot for commit cc8dbe9. Bugbot is set up for automated code reviews on this repo. Configure here.

mgravell added 2 commits July 31, 2026 11:48
StackExchange.Redis 3.1.0 added WithRetry, which replays failed operations
according to a policy bounded by the command's declared side-effect category.
Commands it does not recognise fall back to CommandRetryNever, and it knows
nothing about module commands - so today *nothing* NRedisStack issues is
eligible for retry.

Categorize all 153 builder commands. SerializedCommand gains a CommandFlags
category as the first constructor argument, masked on construction to the
retry-category region (bits 13-17) plus the server-specific bit (18), so the
parameter cannot smuggle in a replica preference or FireAndForget. The
uncategorized constructors are [Obsolete] and CS0618 is an error inside the
library, so a new command cannot be added without declaring what a replay of
it would do. An internal EffectiveFlags property combines the category with
the library-wide defaults (absorbing the DEBUG-only NoRedirect from Auxiliary).

The library keeps its StackExchange.Redis 3.0.x floor: CommandCategories
declares the values as casts rather than referencing the 3.1.0 named members,
which are [Experimental] under SER007. On 3.0.x the bits fall outside that
version's user-selectable mask and are silently discarded, reproducing current
behaviour exactly; on 3.1.0+ they take effect. The test project overrides to
3.1.0 and asserts each rung against the named member, so an upstream
renumbering fails loudly instead of silently re-categorizing everything.

Notable calls:

- BF.ADD/MADD/INSERT are WriteChecked: bloom adds are monotone, so a replay is
  a state no-op that only changes the returned 0/1.
- CF.ADD/INSERT/DEL are WriteAccumulating: a cuckoo filter is a multiset, so a
  replay changes the counts.
- FT.CURSOR READ is WriteAccumulating, not read-only - reading advances the
  cursor, so a replay skips a page rather than repeating one. It and FT.CURSOR
  DEL, BF/CF.SCANDUMP and cursored FT.AGGREGATE carry ServerSpecific, which
  strips failover while still permitting a same-server retry.
- FT.CONFIG (GET as well as SET) is ServerAdmin|ServerSpecific, matching how
  SE.Redis treats plain CONFIG; a different member can be configured
  differently, so GET is not meaningfully safer.
- The blocking core pops are WriteAccumulating: they are destructive reads, so
  a replay pops a further element and the first one is already lost.
- TS.ADD/TS.MADD are uniformly WriteAccumulating for now; see the comment in
  TimeSeriesCommandsBuilder for why the overload taking TsAddParams cannot see
  which of three possible rungs applies.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 0e5ad774aa

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment thread src/NRedisStack/Search/SearchCommandBuilder.cs Outdated
Comment thread src/NRedisStack/Search/SearchCommandBuilder.cs Outdated
@mgravell
mgravell merged commit 87ea197 into master Jul 31, 2026
22 checks passed
@mgravell
mgravell deleted the marc/command-categories branch July 31, 2026 12:41
mgravell added a commit that referenced this pull request Aug 3, 2026
* Announce the library name without demanding IDatabase

Auxiliary.ExecuteAsync unconditionally cast its IDatabaseAsync to IDatabase in
order to send the one-time CLIENT SETINFO lib-name/lib-ver pair. A retry-wrapped
database (SE.Redis 3.1.0 WithRetry) is async-only - RetryDatabase implements
IDatabaseAsync but not IDatabase - so every async module command issued through
one threw InvalidCastException before the command was sent, making the retry
categories added in #540 unusable from the async API.

The announcement never needed the sync interface: it only needed it to create a
batch to group the two commands, which don't need grouping. It is now typed as
IDatabaseAsync and dispatched fire-and-forget via an internal ClientSetInfoAsync
overload that takes dispatch flags, so there is no cast, no batch, and no
round-trip on the caller's first command. In the expected case - fire-and-forget
completing synchronously - no state machine is entered on this path at all: the
overload hands back a cached completed task, and the caller only awaits (to
consume a fault) if a task somehow comes back not-yet-completed. A synchronous
failure in the version probe can no longer fail the caller's command either;
announcing who we are is best-effort.

Renamed SetInfoInPipeline -> SetLibraryInfoOnce (internal): there is no longer a
pipeline involved.

Task.IsCompletedSuccessfully only exists on modern .NET, so netstandard2.0/net481
get it as an extension property polyfill rather than open-coding the Status test
at each site.

Audited the rest of the library for the same shape: this was the only
unconditional cast to a sync SE.Redis interface; SearchCommandsAsync's cluster
cursor path was already type-tested.

Tests cover an async module command and the first-use announcement over
db.WithRetry(...) - both fail with InvalidCastException before this change - plus
the synchronous completion that the fire-and-forget dispatch relies on.

* - ref 3.1.3 to take the WithRetry()/ITransaction tweaks
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