Repository navigation
Each queued submission writes an envelope of its own - #2275
Conversation
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.
aaj3f
left a comment
There was a problem hiding this comment.
@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
Valuecopy as before. No performance-degradation risk. - Deployment targets: ✔
fluree-db-consensusbehindraftonly (Raft-clustered server or anEmbeddedRaftNodeembedder); 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, andQueuednot 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()); |
There was a problem hiding this comment.
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 { |
There was a problem hiding this comment.
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.
| // hashing the rest of the envelope is equivalent to hashing | ||
| // the canonical body. | ||
| QueuedRequest::Push(p) => serde_json::to_value(p)?, | ||
| QueuedRequest::PushWithMerges(p) => { |
There was a problem hiding this comment.
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.
| @@ -152,28 +152,18 @@ impl QueuedTransactor { | |||
| /// an idempotency key: without one, re-proposing would create a | |||
There was a problem hiding this comment.
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.)
|
Coordination with #2280 (retention and erasure, on
#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.
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`.
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:
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
IdempotencyHitfor a retry. If the other submission's entry was still queued, the worker failed to read the envelope and poisoned the entry:The change
Each envelope names its submission.
QueuedTransact,QueuedPush,QueuedRevert,QueuedMergeandQueuedRebasegain an optionalsubmissionfield.enqueue_and_awaitstamps a fresh ULID into it before writing the envelope, so every submission gets its own address.canonical_body_bytesleaves 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 asNone. A unit test decodes a stamped merge with the prior struct's shape.The transactor releases an envelope in one place.
submit_and_awaitnow wraps the propose loop (propose_and_park), which records what became of the envelope in anEnvelopeFate. The envelope is released only when nothing will read it:ForwardToLeader, membership change), orInFlight).Once the submission's own entry is queued, the queue owns the envelope. A
Fatalpropose leaves it, because whether the entry was queued is unknown.This closes two related holes:
ForwardToLeaderafter 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_ciddiffers.Tests
it_embedded_node.rs:an_identical_queued_request_keeps_its_envelopequeues a request behind a held write lock, queues an identical keyless request, and sends a keyed retry of the first. The retry is answered withIdempotencyHit, which used to release the shared envelope. It goes through aQueuedTransactorwithout 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 theStagingFailedabove.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.rscover 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. Aqueued_transactor.rsunit test covers the release rule.Merging into
nextThe commit applies cleanly to
next, which has the same envelope construction sites.