Skip to content

Each queued submission writes an envelope of its own - #2275

Merged
bplatz merged 3 commits into
mainfrom
fix/queued-envelope-shared-address
Nov 11, 2026
Merged

bplatz merged 3 commits into
mainfrom
fix/queued-envelope-shared-address

Conversation

@bplatz

@bplatz bplatz commented Nov 7, 2026

Copy link
Copy Markdown
Contributor

What was wrong

A queued request's envelope is stored in CAS under the hash of its bytes. Identical requests wrote identical envelopes, so they shared one blob:

  • merge, rebase, revert and push envelopes hold only stable fields;
  • a transact envelope holds a timestamp only when the caller backdates it with an event time, so two identical keyless inserts or upserts collide too.

Whichever submission released the envelope first deleted it from under the other. A releasing submission can be a keyless entry being applied, an idempotency eviction, or a submit-path rejection such as an IdempotencyHit for a retry. If the other submission's entry was still queued, the worker failed to read the envelope and poisoned the entry:

submission poisoned: StagingFailed { CAS read of request_cid failed: Not found ... }

The change

Each envelope names its submission. QueuedTransact, QueuedPush, QueuedRevert, QueuedMerge and QueuedRebase gain an optional submission field. enqueue_and_await stamps a fresh ULID into it before writing the envelope, so every submission gets its own address. canonical_body_bytes leaves the field out, so a keyed retry still matches the original request's idempotency body.

Mixed-version clusters keep working. The field is #[serde(default, skip_serializing_if = "Option::is_none")], so a node that predates it ignores it, and an envelope without it decodes as None. A unit test decodes a stamped merge with the prior struct's shape.

The transactor releases an envelope in one place. submit_and_await now wraps the propose loop (propose_and_park), which records what became of the envelope in an EnvelopeFate. The envelope is released only when nothing will read it:

  • every propose was turned away (rejected, ForwardToLeader, membership change), or
  • the submission only joined another submission's in-flight entry under the same key (InFlight).

Once the submission's own entry is queued, the queue owns the envelope. A Fatal propose leaves it, because whether the entry was queued is unknown.

This closes two related holes:

  • A keyed retry that joins an in-flight entry now writes an envelope of its own (it used to share the original's). Nothing reads it, so it is released instead of leaked.
  • Before, a re-propose that met ForwardToLeader after the submission's own entry was already queued released that entry's envelope.

Side effect: two identical submissions on one node no longer share a waiter key, since request_cid differs.

Tests

it_embedded_node.rs:

  • an_identical_queued_request_keeps_its_envelope queues a request behind a held write lock, queues an identical keyless request, and sends a keyed retry of the first. The retry is answered with IdempotencyHit, which used to release the shared envelope. It goes through a QueuedTransactor without the per-node cache, as a retry arriving at another node would. Then the lock is dropped and the identical request must succeed. Without the stamp, it fails with the StagingFailed above.
  • a_retry_that_joins_a_queued_entry_releases_its_own_envelope: a keyed retry joins a queued entry, and afterwards exactly the two queued entries' envelopes remain. Without the stamp it finds 1; without the joined-release rule it finds 3.

Unit tests in lib.rs cover all six request variants: the stamp separates envelope bytes but not the canonical body, it round-trips, and a prior node's shape decodes a stamped envelope. A queued_transactor.rs unit test covers the release rule.

Merging into next

The commit applies cleanly to next, which has the same envelope construction sites.

An envelope's address is the hash of its bytes. Identical requests wrote
identical envelopes (a merge's, rebase's, revert's or push's holds only
stable fields, and a transact's holds a timestamp only when the caller
backdates it), so they shared one blob, and a release for one deleted it
from under another. A second identical request still queued then failed
reading it: StagingFailed, "CAS read of request_cid failed".

Every envelope now names the submission it belongs to. The idempotency
body leaves the name out, so a retry is still the same request, and a node
that predates it ignores it.

The transactor releases a submission's envelope in one place, when nothing
will read it: every propose was turned away, or the submission only joined
another's in-flight entry under the same key. Once its own entry is queued
it leaves the envelope to the queue. Before, a re-propose that met
ForwardToLeader released the envelope of the entry it had already queued.
@bplatz bplatz added the bug Something isn't working as expected label Nov 7, 2026
@bplatz
bplatz requested a review from a team November 7, 2026 21:02

@aaj3f aaj3f left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

@bplatz nice find, and at a design level no pushback from me: a per-submission id inside each request struct is the minimal fix the wire format allows (an outer wrapper would break an older node mid-upgrade), and moving the release decision into one place also closes the ForwardToLeader-after-queued deletion. Claude reproduced the poisoned submission at the base with your test and confirmed nothing in existing ledgers needs repair, since envelopes are transient. One coordination note: the #2279/#2280 stack on next still has the per-arm release_envelope calls in queued_transactor.rs, and #2280 adds an early return after the envelope is written. Whichever lands second should keep this PR's single release point and have that return mark the envelope turned away. Good to merge whenever you're ready.

Full review below:


✅ Approve with nits.

This is a really clean fix, @bplatz. The bug is real and reproducible: identical keyless writes, or a retry answered from the cache, deleted an envelope another queued entry still had to read. Your test fails at the base with exactly that poison and passes here. The cost is close to nothing: the write count is unchanged, each envelope grows by 42 bytes, and an identical request's envelope lives only while it's queued (or until its key is evicted), like any distinct request's. The two notes below are optional: one decision for where the stamp lives, and a clause in the queue design doc.

Adherence to repo commitments:

  • Patterns/abstractions: ✔ extends the existing request structs and the existing content-store release; one optional note on deciding the stamp's location in one place.
  • Performance (speed first, memory second): ✔ no hot path. One ULID and 42 bytes per submission; the canonical body makes the same single Value copy as before. No performance-degradation risk.
  • Deployment targets: ✔ fluree-db-consensus behind raft only (Raft-clustered server or an EmbeddedRaftNode embedder); no new background task or process-lifetime state; mixed-version clusters keep decoding and matching idempotency bodies.
  • Testing: ✔ two embedded-node integration tests and two unit tests, present and passing in CI's test job on 86ebb9fe2. Mutation-checked: no stamp, a joined envelope kept, and Queued not sticky each turn their test red; the identical-request test is red at the base.
  • Conventions: ✔ multi-line commit body that explains the mechanism and the release rule; the stale "unique timestamps per attempt" comment is gone.

Verified at branch HEAD 86ebb9fe2: CI run 42413903523 green, with the four new tests by name in the test job. On an EC2 box, Claude ran fluree-db-consensus --features raft. At the head, it_embedded_node passed 9/9 and the lib suite passed. At the base eccd062a1, with the PR's tests copied in, an_identical_queued_request_keeps_its_envelope FAILED with the StagingFailed poison. Three mutations were each red, with git status clean afterwards.

Approving so you can merge when ready, but maybe worth folding in the two small notes first.

let idempotency_cache_key =
idempotency_key.map(|k| IdempotencyCacheKey::new(full_ledger_id.clone(), k.clone()));

envelope.stamp_submission(ulid::Ulid::new().to_string());

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

praise. One line that fixes the shared address, and Claude confirmed it's the load-bearing one. With the PR's it_embedded_node.rs additions copied onto eccd062a1, an_identical_queued_request_keeps_its_envelope fails with exactly the StagingFailed { "CAS read of request_cid failed: Not found …" } poison from the body. At the head it passes, and with only this line removed it fails the same way again.

It also survives a rolling upgrade cleanly. A stamped envelope can't share an address with an unstamped one. An older worker ignores the field, since none of these structs is deny_unknown_fields. An unstamped envelope's canonical body is byte-identical to before, so a keyed retry that lands on a node of the other version still matches. And since envelopes are transient queue blobs that nothing references after their entry resolves, there's nothing in existing ledgers to repair.

Unknown,
}

impl EnvelopeFate {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

praise. I think EnvelopeFate is the better half of this PR. Before, nine arms released the envelope on their own, and the ForwardToLeader arm did so even when an earlier attempt had already queued this submission's entry, which deleted the envelope that entry would read. Deciding once, with Queued sticky and Fatal leaving it, closes that too. Claude walked every return path in propose_and_park and found none that leaves a read envelope released or an unread one kept (max_retries is clamped to 1, so nothing returns Unanswered after a propose). M2 (a joined envelope kept) and M3 (Queued not sticky) each turn their test red.

Comment thread fluree-db-consensus/src/lib.rs Outdated
// hashing the rest of the envelope is equivalent to hashing
// the canonical body.
QueuedRequest::Push(p) => serde_json::to_value(p)?,
QueuedRequest::PushWithMerges(p) => {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

optional. This is more of a question than a suggestion. Where the stamp lives is decided in two places: stamp_submission is an exhaustive match, so a new variant won't compile without choosing its slot. canonical_body_bytes, though, strips a "submission" JSON key, with a hand-written case for PushWithMerges's nested push. The unit test enumerates all six variants today, but the next variant that nests a request would have to remember the strip on its own.

We may want a fn submission_slot(&mut self) -> &mut Option<String> with the one exhaustive match, used by stamp_submission. canonical_body_bytes would then clone, set the slot to None, and hash as it did before the PR. The clone costs about what today's to_value copy does, and the compiler would hold the invariant. Minor and non-blocking, but if you agree it's right, I'd rather see it folded in now than lost in the backlog.

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.

Addressed in ff5cce0

@@ -152,28 +152,18 @@ impl QueuedTransactor {
/// an idempotency key: without one, re-proposing would create a

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

optional. docs/design/raft-command-queue.md:53 still describes step 1 as "write QueuedRequest envelope to CAS → request_cid", which now reads as if identical requests dedup there. Perhaps one clause there saying each envelope carries its submission's id (so request_cid is unique per submission), plus the release rule this doc comment now states. Minor, but if you agree, I'd rather see it in this PR. (Commenting here because docs/design/raft-command-queue.md is not in this diff.)

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.

Addressed in ff5cce0

@bplatz

bplatz commented Nov 11, 2026

Copy link
Copy Markdown
Contributor Author

Coordination with #2280 (retention and erasure, on next): whichever of the two lands second needs to reconcile envelope release.

  • Collecting clusters. feat: TTL, history retention and proof-verified permanent erasure #2280 has a member leave a released envelope to cluster collection instead of deleting it (leave_for_collection, 7cbe8c8). The reason was the shared envelope this PR fixes: a release could delete an envelope another queued request still read. The second to land must carry that hand-off into this PR's single release point (EnvelopeFate). Otherwise the shared-envelope deletion comes back on collecting clusters. With per-submission envelopes, check whether leaving them to collection is still needed at all.
  • Non-collecting clusters. A cluster collects only after its first erasure. Until then it releases envelopes directly, so it keeps the shared-envelope bug until this PR lands.

#2280's body records the same requirement.

`stamp_submission` and `canonical_body_bytes` each knew where the
submission stamp lives: the first as an exhaustive match, the second
by stripping a "submission" JSON key, with a hand-written case for
the push nested in `PushWithMerges`. A new variant that nests a
request would compile without the strip and hash its stamp into the
idempotency body.

`submission_slot` is now the one exhaustive match. Stamping writes
through it, and the canonical body clears it on a copy of the
envelope before hashing as it did before the stamp existed. Transact
bodies never hold the stamp and are still hashed in place.

The queue design doc records that each envelope carries its
submission id and the rule for when the envelope is released.
@bplatz
bplatz merged commit b09d453 into main Nov 11, 2026
19 checks passed
@bplatz
bplatz deleted the fix/queued-envelope-shared-address branch November 11, 2026 19:39
bplatz added a commit that referenced this pull request Nov 11, 2026
Reconciles the per-submission envelopes of #2275 with cluster
collection. The transactor releases an envelope in one place
(`EnvelopeFate`), and that release leaves it to the collector where
the cluster collects. The write-scope refusal marks the envelope
turned away, so it is released rather than left behind.

The test that predicted a rebase envelope's address now pushes a
commit, the one submission whose stored addresses are known ahead.
The test of identical requests sharing an envelope, which cannot
happen now, gives way to one that a turned-away submission's
envelope is left to the collector, recorded as garbage. `next`'s new
graph-import test passes the record's `WriteBasis`.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working as expected

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants