Skip to content

Add a bounded re-read and re-plan update for records - #536

Open
dhruv8sh wants to merge 4 commits into
feat/kv-record-contractfrom
feat/record-update-retry
Open

dhruv8sh wants to merge 4 commits into
feat/kv-record-contractfrom
feat/record-update-retry

Conversation

@dhruv8sh

@dhruv8sh dhruv8sh commented Nov 25, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

  • A record write that loses its Add/CAS precondition wrote nothing, and today each application hand-writes the reload-and-retry loop. Trusted Server's after-response identity write needs exactly one bounded re-read and re-plan (Retry an after-response write that loses its CAS trusted-server#1553), and review asked for it as an explicit EdgeZero operation that returns Unsupported where a store cannot do it.
  • RecordHandle::update(key, attempts, plan) reads the record, asks the caller's planner for a replacement or a reason to skip, and writes it with IfGenerationMatch on the generation read, or Add after a miss. After a definitive precondition rejection it reads and plans again, up to attempts writes, so at most one replacement lands.
  • A lost precondition proves the record exists under another generation, so a re-read that returns the rejected generation or misses ends as StaleRead instead of planning on an eventually consistent read. On celld, whose lookup hides expired rows that still block Add/CAS until its sweep, the same outcome repeats until the sweep, and the docs say so. Errors are not retried, MutationUncertain is never replayed, and the conflict count is reported even when the update fails. Application policy (what to re-plan, any fallback) stays with the caller.

Changes

Crate / File Change
crates/edgezero-core/src/record_store.rs New RecordHandle::update, RecordReplacement, RecordUpdateOutcome (Conflict, Skipped, StaleRead, Written) and RecordUpdate { conflicts, result }. Composed from lookup and insert, so a provider without record operations reports KvError::Unsupported. 13 unit tests over a scripted provider.
docs/guide/kv-records.md "Re-plan after a lost precondition" section with an example and the stale-read (including celld's unswept expired rows), error and Unsupported rules.
CHANGELOG.md Unreleased entry.

Closes

Closes #535

Stacked on #510 (feat/kv-record-contract). Parent: #468.

Test plan

  • cargo test --workspace --all-targets (Rust 1.98.1)
  • cargo clippy --workspace --all-targets --all-features -- -D warnings
  • cargo fmt --all -- --check
  • cargo check --workspace --all-targets --features "fastly cloudflare spin"
  • WASM builds: no adapter or target-specific code changed
  • examples/app-demo workspace: not affected
  • Docs build: cd docs && npm run lint && npm run format && npm run build
  • Manual testing via edgezero serve --adapter axum: no runtime wiring changed
  • Other: tests cover a first-attempt write, Add after a miss, a conflict then a successful re-plan, giving up at the attempt bound (2 and 1), a planner skip, stale re-reads (rejected generation, miss after lost CAS, miss after lost Add), a failed re-read keeping the conflict count, a store error and a MutationUncertain write not retried, and Unsupported from a provider without record operations
  • Other: mutation checks. Removing the stale-read stop fails 3 tests; an off-by-one in the attempt bound fails 2

Checklist

  • Changes follow CLAUDE.md conventions
  • No Tokio deps added to core or adapter crates
  • Route params use {id} syntax (not :id) (no routes changed)
  • Types imported from edgezero_core (not http crate)
  • Store wiring goes through KvRegistry / ConfigRegistry / SecretRegistry (no wiring changed)
  • New code has tests
  • No secrets or credentials committed

RecordHandle::update reads a record, plans its replacement and writes it with IfGenerationMatch, or Add after a miss. After a lost precondition it reads and plans again up to the given attempts, stops on a stale re-read, never replays an uncertain mutation, and reports the conflict count. Providers without record operations report Unsupported.

Closes #535

Signed-off-by: dhruv8sh <dhruv8sh@proton.me>
@dhruv8sh dhruv8sh self-assigned this Nov 25, 2026
dhruv8sh added a commit to IABTechLab/trusted-server that referenced this pull request Nov 25, 2026
EcKvStore gains update, which RecordEcKvStore implements with EdgeZero's RecordHandle::update (IABTechLab/edgezero#536). KvIdentityGraph keeps the permit and the identity plan and makes one bounded update; a store without it reports Unsupported and the write falls back to last-write-wins. EdgeZero is pinned to the rc/identity commit 9d02d7b3 with #536 merged.

Signed-off-by: dhruv8sh <dhruv8sh@proton.me>
@aram356 aram356 linked an issue Nov 25, 2026 that may be closed by this pull request
3 tasks
Bring in PR #510's current head. It carries main's Fastly SDK 0.13.3,
Fastly CLI 17.2.0 and fixture download retry, the stack's Fastly SDK
0.13.3 and url 2.5.9 pins, Viceroy 0.22.0 with the readiness repair and
its atomic conditional KV insert, the early-response upload drain, the
production hosting port reservation and respawn fixes, the AWS emulator
proofs' readiness markers and the batch cutoff test fix. It merges
without conflicts.
…ection

Moving the entry below the first existing Unreleased section avoids a
merge conflict with the after-response entries at the top of the
integration branch. The entry's content is unchanged.
aram356 added a commit that referenced this pull request Nov 26, 2026
Take PR #536's head 601ae8d, which adds RecordHandle::update, the
bounded re-read and re-plan update for records, on PR #510's current
head. It merges without conflicts.
@aram356

aram356 commented Nov 26, 2026

Copy link
Copy Markdown
Collaborator

For the identity integration branch (#495), I pushed 179f0f59 and 601ae8db, and this PR is now in rc/identity (459bd27d, a plain merge of 601ae8db), which IABTechLab/trusted-server#1660 needs for RecordHandle::update.

  • 179f0f59 merges Add portable conditional KV records across providers #510's current head a296a067. It brings EdgeZero main's Fastly SDK 0.13.3 upgrade, with the Fastly CLI 17.2.0 upgrade and the fixture download retry, the stack's Fastly SDK 0.13.3 and url 2.5.9 pins, Viceroy 0.22.0 with the readiness repair and its atomic conditional KV insert, the early-response upload drain on Axum, the production hosting port reservation and respawn fixes, the AWS emulator proofs' readiness markers and the batch cutoff test fix. It merges without conflicts.
  • 601ae8db moves this PR's ### Bounded record update (#535) CHANGELOG section below the Axum JSON section, the first existing Unreleased section, so rc/identity's merge of it is plain next to the after-response entries at the top. The entry's content is unchanged.

Checked on this head: the 13 update tests passed 50 of 50 runs, the core suite passed, and the Axum and Fastly record contracts passed, Fastly under the repaired Viceroy. A concurrent-update probe, 4 concurrent updates over 400 rounds, passed 400 of 400 with the atomic insert, against 62 of 400 lost updates without it.

@prk-Jr prk-Jr left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Approving. RecordHandle::update is a small composition of lookup and insert. It handles the cases I would expect:

  • it bounds writes rather than reads;
  • it never replays MutationUncertain;
  • it keeps the conflict count when the update fails;
  • it reports Unsupported for a provider without record operations.

The scripted tests cover each outcome. I swapped the write after a miss from Add to Overwrite, and two tests failed (update_adds_when_the_lookup_misses, update_stops_when_the_reread_misses_after_a_lost_add). They passed again with the code restored.

I also checked whether this changes the Fastly CAS-on-a-deleted-key concern from #510. It doesn't make it worse: update issues at most one acknowledged write and adds no new precondition path.

One non-blocking point is inline: the "a lost precondition proves the record exists" premise doesn't hold on celld for expired rows that haven't been swept yet.

Checks:

  • cargo +1.98.1 test -p edgezero-core --lib record_store: 13 passed.
  • Hosted checks at 601ae8db: 26 passed. The 3 failures look like infrastructure:
    • cli tests failed on a Docker registry "Data limit exceeded" while pulling the AppConfig agent image;
    • cargo test is the aggregate job reporting that failure;
    • cache-new-revision-smoke failed its compiler-cache reuse assertion.
    • None of them touch edgezero-core.

/// again; the update stops at the first acknowledged write, so at most one
/// replacement lands.
///
/// A lost precondition proves the record exists under another generation. A

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Non-blocking: this premise doesn't hold on every provider that supports records.

The celld README says "Lookup/list use native expiry filtering. Physical existence and Add/CAS use the physical value until sweep."

So for a celld record that has expired but not yet been swept:

  1. lookup returns None.
  2. update plans and writes Add, which is rejected because the physical row still exists.
  3. The re-read misses again, so the update ends as StaleRead. This happens on every attempt until the sweep runs.

Nothing is written either way, so this is not a data-integrity problem. But the outcome and this doc describe it as an eventually consistent read. A caller deciding between "retry soon" and "give up" would get the reason wrong.

Suggestion: soften the claim, e.g. "...proves the record exists, or on providers whose lookups hide expired rows that still block Add/CAS (celld until sweep), that an expired row remains". Make the same change to the matching paragraph in docs/guide/kv-records.md.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Thanks, fixed in c8318ce. The update doc, the StaleRead variant and the guide now say a lost precondition can also mean an expired row celld has not swept yet, and that StaleRead then repeats until the sweep, so it does not always mean a retry soon will succeed.

aram356 added a commit that referenced this pull request Nov 27, 2026
PR #524 (outbound mTLS client identities, #523) belongs to no identity
epic, and PR #522 (portable DynamoDB and celld record providers on
Fastly, #474) is still a draft. #524 is stacked on #522, so both leave
together.

This commit reverts their net changes. Its tree is rc/identity as it
was before #522 (654f546) merged with the current tips of #519, #515,
#502, #469 and #536, the branch's remaining lines. #514 and #517 are
already inside them. The rebuild merges without conflicts, and the
change touches only the 113 paths that #522 and #524 changed against
#517.

Three files differed from a plain reverse of the two PRs because of the
conflict resolutions in 7616c85 and b3a45df. Each keeps the other
lines' code and drops only #522's or #524's:

- crates/edgezero-adapter-axum/src/cli.rs keeps the capability matrix
  test's lint expectation and drops #524's client identity test.
- crates/edgezero-adapter-fastly/src/request.rs keeps the after-response
  handle and wrapper and drops #522's deferred portable KV registry.
- crates/edgezero-store-aws/src/dynamodb_records_tests.rs drops imports
  used only by #522's tests.

Merging a later tip of #522 or #524 will not restore this code, because
git treats their earlier commits as already merged. Bringing them back
needs a revert of this commit first.
…aleRead

celld's lookup hides expired rows that still block Add/CAS until its sweep, so a lost precondition there need not mean another generation exists and StaleRead can repeat until the sweep. Say so in the update docs, the StaleRead variant and the records guide.

Signed-off-by: dhruv8sh <dhruv8sh@proton.me>

@ChristianPavilonis ChristianPavilonis left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Approved. The bounded retry re-plans after a definitive rejection, respects the write limit, and does not replay an uncertain mutation. The EdgeZero core suite, Clippy, and Rust formatting checks passed. I also checked the successful re-plan and the deletion-before-CAS case against Axum/redb. Three non-blocking comments inline.

],
"should plan again against the re-read"
);
assert_eq!(store.modes(), [if_match(1), if_match(2)]);

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

R1 · P2 test gap: Assert what the retry writes. The scripted store records only the write mode, and both planner calls return the same replacement. A regression could call the planner twice, then write the first plan's bytes under the second generation and still pass this test. Give each plan a distinct payload and assert that the second insert receives the second plan's payload. The current implementation passed that check against Axum/redb; this is about protecting it in the committed tests.

Comment thread docs/guide/kv-records.md
Comment on lines +164 to +171
- A lost precondition proves the record exists under another generation, or,
on a provider whose lookup hides expired rows that still block Add/CAS
(celld until its sweep), that an expired row remains. A re-read that returns
the rejected generation or misses is stale, as an eventually consistent
lookup can be, or sees past such an expired row, and ends with `StaleRead`
instead of planning on it. On celld that repeats until the sweep removes the
row, so `StaleRead` does not always mean a retry soon will succeed. A record
deleted after the rejection reads the same way.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

R2 · P3 docs: StaleRead can be a fresh miss. Axum/redb rejects CAS if the row was deleted or expired before the write. I reproduced deletion before CAS: the re-read correctly missed and update returned StaleRead with one conflict and no write. Keep that conservative stop, but describe what it observes rather than what it proves. The rustdoc for RecordHandle::update and RecordUpdateOutcome::StaleRead should make the same distinction.

Suggested change
- A lost precondition proves the record exists under another generation, or,
on a provider whose lookup hides expired rows that still block Add/CAS
(celld until its sweep), that an expired row remains. A re-read that returns
the rejected generation or misses is stale, as an eventually consistent
lookup can be, or sees past such an expired row, and ends with `StaleRead`
instead of planning on it. On celld that repeats until the sweep removes the
row, so `StaleRead` does not always mean a retry soon will succeed. A record
deleted after the rejection reads the same way.
- After a precondition rejection, a miss or a re-read of the rejected
generation ends the update as `StaleRead` without running the planner again.
A miss can be current if the row was deleted or expired before or after the
rejection. It can also come from an eventually consistent lookup. On celld,
lookup hides expired rows that still block Add/CAS until the sweep, so
`StaleRead` can repeat until that row is swept.

Comment thread docs/guide/kv-records.md
use std::num::NonZeroU8;

use bytes::Bytes;
use edgezero_core::record_store::{RecordReplacement, RecordUpdateOutcome};

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

R3 · API consistency: Re-export the update types at the crate root. crates/edgezero-core/src/lib.rs already re-exports the other record types, including RecordHandle, RecordWrite, and RecordWriteOutcome. Please add RecordReplacement, RecordUpdate, and RecordUpdateOutcome to that list, then use the crate-root import here. Existing module imports would still work.

This branch has not been deployed

No deployments
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.

Add a bounded read, re-plan and conditional write operation for records

4 participants