Skip to content

GH-4570: let a deduplication claim ride the Polecat transaction - #4573

Merged
jeremydmiller merged 1 commit into
mainfrom
gh-4570-polecat-transactional-deduplication
Sep 23, 2026
Merged

jeremydmiller merged 1 commit into
mainfrom
gh-4570-polecat-transactional-deduplication

Conversation

@jeremydmiller

Copy link
Copy Markdown
Member

Closes #4570. The Polecat half of #4505; Marten shipped in #4568, Fisher is #4571.

Same guarantee, different mechanism

The claim commits with the handler's work and rolls back with it, so a chain that throws or answers a
ProblemDetails never claimed at all — no compensating release, no status-code test, nothing to race
against the response.

But it could not be the same mechanism. Marten works by queuing an IStorageOperation onto the session,
which is safe because Wolverine's message store there is built from Marten's own NpgsqlDataSource.
Polecat's is not:

// SqlServerMessageStore, via WolverineOptionsPolecatExtensions.BuildSqlServerMessageStore
: base(database, SqlClientFactory.Instance.CreateDataSource(database.ConnectionString!), ...)

Same SQL Server database, different connection pool. So the claim is enlisted as an
ITransactionParticipant, which is handed the live SqlConnection and SqlTransaction — the pattern
StoreIncomingEnvelopeParticipant already uses for inbox rows.

The check does not share a round trip here

Polecat.Batching.IBatchedQuery has no AddItem<T>(IQueryHandler<T>):

IBatchedQuery members
Marten AddItem · CheckExists · Execute · Load · LoadMany · Query · QueryByPlan
Polecat CheckExists · EventsExist · Execute · FetchForWritingByTags · QueryByPlan

There is nothing to enlist a raw-SQL existence check into, and QueryByPlan composes other batch
operations rather than injecting SQL. So the check runs standalone through the session's AdvancedSql.

That is a missed optimisation, not a correctness gap — the guarantee comes from the claim riding the
transaction, not from where the check ran. An optional id still costs no round trip at all. An upstream
ask for an AddItem equivalent would close the gap later; it is recorded on #4570.

Scoping the classifier matters more on SQL Server

SqlException carries no table name. A blanket Number == 2627 || 2601 test — which is exactly what
PolecatIntegration's existing discard rule does (#4565, a live bug) — matches a duplicate-key failure
raised by the application's own documents just as readily, and would report it to the caller as
"already handled". The table name is only in the message text, which is where PolecatDeduplicationFailures
looks. The negative control below shows it is there:

Violation of PRIMARY KEY constraint 'pkey_wolverine_deduplication_deduplication_id'.
Cannot insert duplicate key in object 'tx_dedup.wolverine_deduplication'.

The guard is not CanApply

Same as Marten, and worth re-flagging because getting it wrong is silent and total: CanApply is true for
any chain that merely takes an IDocumentSession, and a claim enlisted in a transaction nothing commits is
never written — the endpoint reports itself as deduplicated while every replay runs. The guard requires the
CreateDocumentSessionFrame and the DocumentSessionSaveChanges postprocessor.

Multi-database tenancy falls through to claim-and-release, and so does an unreadable tenancy — a null
Options.Tenancy is treated as "not single" rather than assumed safe.

Also in this commit: the wrapper moves to core

RefuseDuplicateClaimAtCommitFrame was byte-identical to the Marten copy apart from the classifier, and
Fisher would have made a third. It now lives in Wolverine.Persistence.Codegen, parameterised by a
MethodInfo, and Marten takes it from there. Marten's deduplication tests are unchanged and still pass.

Verification

Clean pinned dotnet build wolverine.slnx -c Release -f net9.0 --no-incremental — 0 errors, 0 warnings.

PolecatTests 327/329 (2 skipped), which is the existing baseline plus the 7 new tests. Marten's
deduplication suites re-run after the wrapper refactor: 8/8 and 2/2.

The concurrency test forces the collision with a barrier holding both executions past their check before
either commits — two plain concurrent sends pass against a build with no commit-race handling at all,
because the optimistic check refuses the second whenever the first has already committed. Verified as a
negative control: with CommitRaceWrapper nulled out, the loser escapes as the raw SqlException quoted
above; restored, green. Re-run twice in a row to confirm it is not order-dependent.

🤖 Generated with Claude Code

https://claude.ai/code/session_01VDUrBeB4tTnKj4AExCS1nj

The Polecat half of GH-4505. Same guarantee as the Marten implementation --
the claim commits with the handler's work and rolls back with it, so a chain
that throws or answers a ProblemDetails never claimed at all and there is no
compensating release to order against the response -- but it cannot be the same
mechanism.

Wolverine's message store on Polecat is built from
SqlClientFactory.CreateDataSource(connectionString): the same SQL Server
database as the document store, but a different connection pool. Queuing a
storage operation onto the session, the way the Marten twin does, would still
write on a connection of its own. The claim is enlisted as an
ITransactionParticipant instead, which is handed the live connection and
transaction -- the pattern StoreIncomingEnvelopeParticipant already uses.

The optimistic check does NOT share a round trip here.
Polecat.Batching.IBatchedQuery has no AddItem<T>(IQueryHandler<T>) equivalent to
enlist a raw-SQL existence check into, so the check runs standalone through the
session's AdvancedSql. That is a missed optimisation rather than a correctness
gap: the guarantee comes from the claim riding the transaction, not from where
the check ran. An optional id still costs no round trip at all.

Scoping the commit-race classifier matters more here than on PostgreSQL.
SqlException carries no table name, so a blanket Number == 2627 || 2601 test --
which is what PolecatIntegration's existing discard rule does, GH-4565 -- matches
a duplicate-key failure from the application's own documents just as readily and
would report it to the caller as "already handled". The table name is only in the
message text, which is where this looks.

Also lifts RefuseDuplicateClaimAtCommitFrame out of Wolverine.Marten into
Wolverine.Persistence.Codegen. It was byte-identical apart from the classifier,
and Fisher would have made a third copy. Marten now takes it from core.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01VDUrBeB4tTnKj4AExCS1nj
@jeremydmiller
jeremydmiller merged commit b9ab1cc into main Sep 23, 2026
44 checks passed
jeremydmiller added a commit that referenced this pull request Sep 24, 2026
The Fisher half of GH-4505, after Marten (#4568) and Polecat (#4573). Same guarantee:
the claim commits with the handler's work and rolls back with it, so a chain that throws
or answers a ProblemDetails never claimed at all and there is no compensating release to
order against the response.

On SQLite the mechanism is a correctness requirement rather than a preference. Wolverine's
message store here is built from new WolverineSqliteDataSource(store.Options.ConnectionString)
-- the same FILE, a different pooled connection. Two connections to one file are two writers,
and the second blocks on the first from inside the first one's transaction, which hangs rather
than failing. So the claim is enlisted as an ITransactionParticipant, handed the live
connection and transaction, and even the optimistic existence check goes through the session's
own AdvancedSql rather than the store's data source.

Fisher has no database-per-tenant composition, so unlike the Marten and Polecat twins there is
no tenancy cardinality to guard.

The commit-race classifier is scoped to the deduplication table, sharing the table-name parsing
GH-4565 added for the inbox: a bare SQLITE_CONSTRAINT test would match a duplicate key raised by
the application's own documents in the same transaction and report it to the caller as "already
handled". It is exposed through FisherDeduplicationFailures rather than off SqliteMessageStore
directly, because the frame renders the declaring type into generated source and that type is
internal -- InternalsVisibleTo does not reach generated code.

The commit race IS reachable on SQLite, which #4571 left open. One writer means the two commits
serialize on the file lock rather than racing inside a transaction, but both executions still
hold a claim the other cannot see, so the loser still trips the primary key. The single-writer
model changes when it finds out, not whether it does.


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

Co-authored-by: Claude Opus 5 (1M context) <noreply@anthropic.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.

Recreate GH-4505 transactional deduplication for Polecat

1 participant