Skip to content

[fm] Introduce SitrepGuardedInsert.#10532

Closed
mergeconflict wants to merge 5 commits into
mergeconflict/impl-default-for-generationfrom
mergeconflict/fm-sitrepguardedinsert
Closed

[fm] Introduce SitrepGuardedInsert.#10532
mergeconflict wants to merge 5 commits into
mergeconflict/impl-default-for-generationfrom
mergeconflict/fm-sitrepguardedinsert

Conversation

@mergeconflict

@mergeconflict mergeconflict commented Jun 2, 2026

Copy link
Copy Markdown
Contributor

Add the SitrepGuardedInsert Diesel combinator and the SitrepGuardedResource trait: a generic primitive for FM rendezvous to insert a resource row idempotently and guarded against stale-sitrep execution.

The combinator wraps a caller-supplied resource INSERT in a single CTE statement that:

  • aborts (StaleSitrep) unless the executor's expected generation still equals the latest sitrep's generation column;
  • short-circuits (AlreadyExists) if a creation marker already exists for the resource id;
  • on a successful insert, atomically writes a creation marker.

The result is surfaced as a SitrepGuardedInsertOutcome of Created / AlreadyExists / StaleSitrep.

Context: #10248. This is used in #10533 and #10535 which are split out in hopes of making the review somewhat less miserable.

@mergeconflict mergeconflict self-assigned this Jun 2, 2026
@mergeconflict mergeconflict added the fault-management Everything related to the fault-management initiative (RFD480 and others) label Jun 2, 2026
Add the `SitrepGuardedInsert` Diesel combinator and the
`SitrepGuardedResource` trait: a generic primitive for FM rendezvous to
insert a resource row idempotently and guarded against stale-sitrep
execution.

The combinator wraps a caller-supplied resource INSERT in a single CTE
statement that:

  - aborts (StaleSitrep) unless the executor's expected generation still
    equals the latest sitrep's generation column;
  - short-circuits (AlreadyExists) if a creation marker already exists for
    the resource id;
  - on a successful insert, atomically writes a creation marker, gated by
    `WHERE EXISTS (SELECT 1 FROM new_resource)` so a marker is never
    fabricated for a pre-existing row.

All spliced SQL identifiers come from the trait's `&'static str` consts, so
the query is injection-safe. The result is surfaced as a
`SitrepGuardedInsertOutcome` of Created / AlreadyExists / StaleSitrep.
@mergeconflict mergeconflict force-pushed the mergeconflict/fm-sitrepguardedinsert branch from 6663e70 to c6e5904 Compare June 2, 2026 19:49
Comment thread nexus/db-queries/src/db/sitrep_guard.rs Outdated
/// 5. `new_marker`: the marker INSERT, emitted inline by the
/// combinator using R's `&'static str` consts. This only runs when
/// `new_resource` actually produced a row.
fn walk_ast<'b>(&'b self, mut out: AstPass<'_, 'b, Pg>) -> QueryResult<()> {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

It would be nice to have an expectorate test (and maybe also an EXPLAIN test) for the kind of queries that this generates.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I've added an expectorate test in 365af00. I didn't bother with an EXPLAIN test yet, but I'm pondering it...

Comment thread nexus/db-queries/src/db/sitrep_guard.rs Outdated
Comment thread nexus/db-queries/src/db/sitrep_guard.rs Outdated
@AlejandroME AlejandroME added this to the 21 milestone Jun 4, 2026
- generate table and column names from types, rather than hardcoding them as static strings
- add expectorate test
- use `Generation` type for generation values
- style nit
- don't tell lies
Comment thread nexus/db-queries/src/db/sitrep_guard.rs
Comment thread nexus/db-queries/src/db/sitrep_guard.rs Outdated
Comment on lines +198 to +201
// Column names injected into the CTEs below.
let marker_id_column = <R::MarkerIdColumn as Column>::NAME;
let generation_column = <R::GenerationColumn as Column>::NAME;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

you love to see it!

Comment thread nexus/db-queries/src/db/sitrep_guard.rs
@mergeconflict mergeconflict requested a review from hawkw June 5, 2026 16:40
@mergeconflict mergeconflict force-pushed the mergeconflict/fm-sitrepguardedinsert branch from c4925ea to 0aded13 Compare June 8, 2026 16:41
@mergeconflict mergeconflict changed the base branch from main to mergeconflict/impl-default-for-generation June 8, 2026 16:41
@mergeconflict mergeconflict deleted the branch mergeconflict/impl-default-for-generation June 8, 2026 17:57
@mergeconflict

Copy link
Copy Markdown
Contributor Author

what the fuck github

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

fault-management Everything related to the fault-management initiative (RFD480 and others)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants