OpenViking icon indicating copy to clipboard operation
OpenViking copied to clipboard

[RFC] Unify QueueFS delivery settlement: per-delivery receipts across Memory, SQLite and Redis backends

Open tanbopiao opened this issue 1 week ago • 8 comments

Background

QueueFS promises at-least-once delivery with serial-key ordering, but settlement (ACK/RETRY) on all three backends is keyed by bare message_id alone:

  • Memory backend (crates/ragfs/src/plugins/queuefs/backend.rs:430-447): ack locates the message in processing by id and deletes it.
  • SQLite backend (same file, 983-999): DELETE ... WHERE message_id = ? AND status = 'processing'.
  • Redis backend (cache_protocol.rs:217-240): ZREM processing by id, then releases the group_active serial slot if it still points at that id.

Under at-least-once semantics — retry, crash recovery, and Redis heartbeat recovery all re-deliver the same message_id — a late ACK from a stale delivery is indistinguishable from an ACK for the current delivery at settlement time. This is exactly the failure documented in #8199 (Memory/SQLite) and #8181 (Redis serial-key early release).

Prior art — thank you

  • #8184 (@Leeminjing) already introduces a delivery_receipt with Lua-side validation for Redis — a strong validation of this direction.
  • #8199 (@PoisonGodv) documents that #8184 leaves Memory/SQLite settling on raw message IDs.
  • #8183 (@Leeminjing) fixes the /api/v1/system/wait cross-queue snapshot for one call site.

Rather than three divergent per-backend fixes, this RFC proposes lifting the same idea into one contract at the QueueFS boundary shared by all three backends, so all backends converge instead of drifting.

Proposed contract — one boundary, three backends

The contract is defined at the QueueFS boundary shared by all three backends, not inside any single backend. Note that the current QueueBackend trait covers Memory and SQLite only; Redis uses a separate async implementation, so a trait-level change alone would not reach it.

  1. Reuse delivery_receipt from #8184 and add it to the existing /dequeue response. Each transition from pending to processing issues a fresh receipt atomically.
  2. ACK and RETRY validate the queue, message ID, and current receipt in the same operation that changes queue state; a mismatched receipt is a no-op returning false.
  3. During normal consumption, only an ACK for the current delivery unblocks the next message with the same serial_key. Native RETRY and recovery invalidate the old receipt while preserving the original message's priority over its same-key successors.
  4. Python's REQUEUED already publishes a replacement and follows a separate path. An invalidated worker may still be running; the queue must stay consistent regardless of what such a worker does.

Legacy compatibility and mixed-version deployment are separate problems

Bare-ID settlement remains accepted only for deliveries without a receipt — that check applies only to upgraded code. Older SQLite processes can still run their original SQL, and older Redis mounts can still run their original Lua. These are two distinct concerns:

  • Cutover. Coordinate the switch per SQLite file or Redis namespace. Stop old mounts and let in-flight requests finish before switching.
  • Legacy recovery. For SQLite, explicitly recover legacy processing rows with a NULL receipt so they receive a fresh receipt when claimed again. A positive recover_stale_sec leaves recent rows untouched during startup recovery.
  • Mixed-version operation. If it must be supported, the upgraded store needs a mechanism that actually blocks writes from old processes.

Acceptance criteria

A shared conformance test suite running identical delivery / retry / stale-ACK-injection scenarios against all three backends, asserting: (a) late ACKs are rejected no-ops; (b) serial ordering is preserved under late-ACK injection.

Out of scope: Work consistency

Work consistency is a separate design. Using the actual Python middleware and Task Store with a simulated QueueFS transport, @Leeminjing reproduced a case where A's receipt had been invalidated, yet A's late failure marked Work as failed before B registered; B's handler was then skipped even though B's ACK succeeded (openviking/service/task_queue_middleware.py:145-249). Returning the settlement result to Python would come too late to prevent that write, and ACKing first would leave a crash window in which the message is gone and the Work result is still unsaved.

This RFC therefore limits its acceptance criteria to QueueFS rejecting stale settlement. Ownership transfer, durable result recording, and recovery for interrupted operations will be specified in a separate design document, referencing that reproduction as the motivating case.

Migration (backward compatible)

delivery_receipt is optional; when absent, settlement falls back to message_id (current behavior). Backends can adopt incrementally; Redis can reuse #8184's Lua receipt validation as its implementation of the same contract.

Revision (2026-12-02). Per the acceptance review, the fallback above is scoped: it applies only to deliveries whose current receipt is absent (a legacy NULL-receipt row, modeled explicitly in the reference implementation via dequeue(with_receipt=False); legacy mode survives retry and crash recovery). When a receipt exists, ack(message_id) / retry(message_id) must present the matching receipt or be rejected — no settlement either way. This closes the stale-ACK path the unrestricted fallback reopened (see #8199). Three consistency test groups cover the fallback with a NULL receipt, the rejection with a live receipt, and the #8199 stale-ACK regression; the reference implementation revision is in tanbopiao/open-source-gap-fixes#1. The serial-slot scope question (RFC delimitation vs. a dequeue-time slot check) remains open pending @PoisonGodv's preference between the two options proposed in the acceptance reply.

Why a boundary-level contract instead of three PRs

#8184 proves the fix works on Redis; #8199 proves the gap remains on Memory/SQLite with no owner. A boundary-level contract plus a conformance test matrix prevents the three backends from drifting apart again — the same class of bug (#4303, #4745, #4751) has recurred across backends precisely because settlement semantics live in three independent implementations.

Happy to draft the boundary contract and the conformance test matrix if maintainers are open to this direction — and glad to be corrected on anything I've misread.

Cross-referenced in #8199 and #8181.

tanbopiao avatar Nov 23 '26 04:11 tanbopiao

The optional fallback looks like it can reopen the stale-ACK path during a mixed rollout. After recovery creates a new delivery, a legacy consumer calling ACK/RETRY with only message_id could still settle that delivery if the fallback applies unconditionally. Could receipt-less settlement be limited to deliveries that have no current receipt, with an explicit rule for retiring that compatibility path?

For SQLite, the transaction boundary also seems worth specifying: persist the fresh receipt in the same transaction as pending -> processing, invalidate the old receipt on recovery, and include the current receipt in the conditional ACK/RETRY statements. run_migrations() should also define the behavior of existing rows with NULL receipts.

A shared matrix covering retry, recovery, duplicate settlement, and serial successor release would then give all three backends the same observable contract.

PoisonGodv avatar Nov 23 '26 07:11 PoisonGodv

Thank you for such a careful review — these are exactly the weak spots I was worried about in the fallback section, and your suggestions are better than what I had.

  • Agreed on limiting receipt-less settlement to deliveries with no current receipt, with an explicit retirement path — I'll fold that into the migration section.
  • Agreed on all four SQLite points; I'll spell them out in the revision.
  • Agreed on extending the matrix to recovery and duplicate settlement — that coverage was indeed too thin as written.

I'll update the RFC accordingly — and if anything still looks off after the revision, I'd genuinely appreciate you flagging it. Your earlier offer to take the Memory/SQLite side would be very welcome whenever it lands, but no pressure either way.

tanbopiao avatar Nov 23 '26 09:11 tanbopiao

I have a few follow-up suggestions. Define the contract at the QueueFS boundary shared by all three backends. The current QueueBackend trait covers Memory and SQLite. Redis uses a separate async implementation. Reuse delivery_receipt from #8184 and add it to the existing /dequeue response. Each transition from pending to processing should issue a fresh receipt atomically. ACK and RETRY should validate the queue, message ID, and current receipt in the same operation that changes queue state. During normal consumption, only an ACK for the current delivery should unblock the next message with the same serial_key. Native RETRY and recovery should invalidate the old receipt while preserving the original message’s priority over its same-key successors. Python’s REQUEUED already publishes a replacement and follows a separate path. An invalidated worker may still be running. [QueueFS backend dispatch](https://github.com/volcengine/OpenViking/blob/ae3952e953e60f0609a2dd5a15cb87419ce2d401/crates/ragfs/src/plugins/queuefs/mod.rs#L134-L195) · [#8184](https://github.com/volcengine/OpenViking/pull/8184)

Specify legacy-client compatibility and mixed-version deployments separately. The author already agreed to limit bare-ID settlement to deliveries without a receipt. That check applies only to upgraded code. Older SQLite processes can still run their original SQL, and older Redis mounts can still run their original Lua. Coordinate the cutover per SQLite file or Redis namespace. Stop old mounts and let in-flight requests finish before switching. For SQLite, explicitly recover legacy processing rows with a NULL receipt so they receive a fresh receipt when claimed again. A positive recover_stale_sec can leave recent rows untouched during startup recovery. If mixed-version operation is required, the upgraded store needs a mechanism that actually blocks writes from old processes. [Author’s reply](https://github.com/volcengine/OpenViking/issues/8232#issuecomment-7096240545) · [SQLite startup recovery](https://github.com/volcengine/OpenViking/blob/ae3952e953e60f0609a2dd5a15cb87419ce2d401/crates/ragfs/src/plugins/queuefs/backend.rs#L618-L635) · [#8184 rollout requirements](https://github.com/volcengine/OpenViking/pull/8184)

Treat Work consistency as a separate design. Python currently persists Work before requesting queue settlement. Using the actual Python middleware and Task Store with a simulated QueueFS transport, we reproduced a case where A’s receipt had been invalidated, yet A’s late failure marked Work as failed before B registered. B’s handler was then skipped, even though B’s ACK succeeded. Returning the settlement result to Python would come too late to prevent that write. ACKing first would leave a crash window in which the message is gone and the Work result is still unsaved. I’d limit this RFC’s acceptance criteria to QueueFS rejecting stale settlement. If the RFC retains the Work guarantee, it should define when ownership transfers, how the result is durably recorded, and how interrupted operations recover. [Work persistence path](https://github.com/volcengine/OpenViking/blob/ae3952e953e60f0609a2dd5a15cb87419ce2d401/openviking/service/task_queue_middleware.py#L145-L249) · [QueueFS settlement interface](https://github.com/volcengine/OpenViking/blob/ae3952e953e60f0609a2dd5a15cb87419ce2d401/crates/ragfs/src/plugins/queuefs/mod.rs#L488-L499)

Leeminjing avatar Nov 23 '26 11:11 Leeminjing

Hi @Leeminjing,

Thank you for the thorough review — and especially for taking the time to reproduce the failure case with the actual Python middleware and Task Store. That is exactly the kind of evidence this RFC needs.

All three suggestions are accepted:

  1. QueueFS boundary contract. Agreed. We will define the receipt contract at the boundary shared by all three backends rather than per-backend, reuse delivery_receipt from #8184 in the /dequeue response, and require atomic issuance on every pending → processing transition. We will also adopt the invariant that ACK/RETRY validates queue, message ID, and current receipt in the same operation that changes queue state, and that only an ACK for the current delivery unblocks the next same-serial_key message.

  2. Legacy compatibility vs. mixed-version deployment. Agreed that these are two separate problems. We will specify per-store cutover (per SQLite file / Redis namespace), stop old mounts and drain in-flight requests before switching, and explicitly recover legacy processing rows with NULL receipts so they receive a fresh receipt when re-claimed. We will also incorporate the recover_stale_sec nuance so recent rows are left untouched during startup recovery.

  3. Work consistency as a separate design. Accepted — thank you for the reproduction. The case you describe (A's receipt invalidated, A's late failure marking Work failed before B registers, B's handler skipped despite a successful ACK) is beyond what this RFC can guarantee. We will limit this RFC's acceptance criteria to QueueFS rejecting stale settlement, and file the Work ownership-transfer, durable-result and recovery questions as a separate design document, referencing your reproduction as the motivating case.

I will update the RFC text accordingly. Thanks again.

tanbopiao avatar Nov 23 '26 12:11 tanbopiao

Follow-up on the delivery-receipt reference implementation: we ran a red/blue adversarial validation pass against it.

Red team: 16 attack vectors — the original defect PoCs plus new ones (poisoned rows, double-apply, idempotency-key collisions, malformed JSONL, crash-stall, stale ACK, clock rollback) — hit 11 on the original copy. Blue team: after fixing 4 kernel-level defects plus a clock-rollback gap, the hardened version defends all 16/16, and the original test suite passes with zero regressions.

Two additions that speak directly to the mixed-deployment concern raised above (@PoisonGodv): the hardened version adds an explicit reclaim_expired path for deliveries whose owner died mid-flight, and stale-ACK rejection — an ACK carrying a delivery_id that no longer matches the current in-flight delivery is refused instead of settling. Both are covered by new regression tests.

Trait change surface stays small: 1 trait + 3 backend implementations. Upstream status re-checked today: the bare msg_id-based settlement is still present on current main, so the contract remains landable.

Hardened code (this commit): https://github.com/tanbopiao/open-source-gap-fixes/commit/fed2441

tanbopiao avatar Nov 26 '26 11:11 tanbopiao

Thanks for posting the hardened version. Reading delivery_receipt.py at fed2441, I couldn't find the narrowing of receipt-less settlement we discussed on the 23rd. In _resolve, a delivery_id of None skips the comparison, so ack(message_id) / retry(message_id) still settle the current delivery even though it has a receipt. The module docstring's item 4 and test_backward_compat_ack_by_message_id both pin that behavior, and every delivery in this model gets a receipt. So a legacy caller's late ACK after a redelivery would still settle the new delivery. Were you planning to restrict that fallback to rows that have no current receipt, and to model legacy NULL-receipt rows explicitly so that case can be tested?

A smaller point: as far as I can tell dequeue() never consults the serial slot and retry() appends to the tail of _pending, so a same-key successor can be delivered while the head is in flight, or ahead of the retried message. The serial tests then only exercise the slot bookkeeping, not the blocking or priority discussed in the thread, which is worth keeping in mind if this is meant to stand in for the three backends.

PoisonGodv avatar Dec 01 '26 09:12 PoisonGodv

Hi @PoisonGodv,

Thanks for the careful read — both points land exactly where the gaps are.

On point 1 (receipt-less settlement). You're right, and the discrepancy is real. The RFC promises that the bare-ID fallback only applies to deliveries that have no receipt, but the implementation doesn't honor that: _resolve (delivery_receipt.py L88–96) only compares delivery_id when it is not None, and since every dequeue issues a fresh receipt (L66–85), there is no "receipt-less delivery" in the model at all. In practice the fallback reopens exactly the stale-ACK path the RFC was meant to close — ack(message_id) / retry(message_id) can settle the current delivery even while its receipt is still live, and docstring item 4 plus test_backward_compat_ack_by_message_id pin that behavior in.

Plan for the fix:

  • Restrict the bare-ID fallback to deliveries whose current receipt is absent, and explicitly model the legacy NULL-receipt row for backward compatibility;
  • When a receipt exists, ack(message_id) / retry(message_id) must either match the active receipt or be rejected — no settlement either way;
  • Add consistency tests covering: fallback with a NULL receipt, rejection with a live receipt, and the stale-ACK regression case from #8199.

On point 2 (serial slot / queue-jumping). Also right. dequeue() never consults _serial_slot, and retry() appends to the tail of _pending, so a same-key successor can jump ahead of an in-flight delivery; the serial tests only assert accounting, not blocking or priority.

I see two ways to close this and lean toward (a):

  • (a) State explicitly in the RFC that this reference implementation covers the settlement contract only, and that serial-blocking / priority semantics are delegated to the backend queue implementation;
  • (b) Alternatively, add a slot check in dequeue() so a same-key message is held back while a delivery for that key is in flight.

If you have a preference between the two, happy to follow it — (b) is the stronger guarantee but widens the patch scope.

I'll push the revision to the branch and update the RFC within the next day or two. Thanks again for holding this to a high bar.

tanbopiao avatar Dec 01 '26 13:12 tanbopiao

Thanks — the narrowed fallback reads right to me now: a bare-ID settlement only lands on a NULL-receipt delivery, and a live receipt has to be presented, which closes the path I was worried about. On the serial question I'd lean toward (a): stating in the RFC that this reference covers the settlement contract and delegating serial blocking / priority to the backend implementations, since reimplementing slot scheduling in the Python model wouldn't demonstrate anything about the Rust/SQL/Lua backends. Either way, happy to defer to what the maintainers prefer.

PoisonGodv avatar Dec 02 '26 08:12 PoisonGodv