prefix cache: a reserved snapshot is a save target until its commit (#129)

A checkpoint reservation (Scheduler::reserve_ckpt, at planning) is written only after the
step (Engine::checkpoint_save), and nothing between the two checked that the slot was still
the reservation's. A later reservation in the same step that found the store full ran
retention over every session, and the policy, placing its ladder from each session's tip,
could release the very slot just handed out: a second request's reservation lands on a
snapshot a first session already holds (the same prefix, so the same key) below that
session's tip, or a lookup's take_over indexes a fresh reservation under a session whose
ladder does not keep it. The save then faulted in checkpoint_copy ("the store does not hold
that slot ... given back after its id was handed out"), the step loop read the negative as a
device fault, and the engine exited with every in-flight request (#129: every 25-50 min
under multi-session agentic load, the store not full at the fault, 12-15 of 16 in use --
the eviction itself had freed the rest).

A reservation now keeps its slot the way a handed-out snapshot does: retention, the LRU
eviction and expiry skip it from reserve_checkpoint until commit_checkpoint, or until the
next step begins when the save was skipped (its request gone), so a skipped save cannot hold
a slot for good. The engine issues every save before it plans the next step, so the window
covers the copy. A reservation that finds every slot handed out or awaiting a save is
refused as before (ckpt_refused) and the chunk goes without a snapshot.

mem_test: the two cases, the shared-prefix one at the position geometric backoff releases,
and a step boundary (begin_step) in the three cases that reserve several snapshots in a
row without committing, as the engine does one per step.
This commit is contained in:
baKon committed 2026-10-18 22:53:56 -05:00
1 parent 816e24bd46
commit d37639ef71
3 files changed
+129 -18

No files matched your search

+16 -5
View File
@@ -284,9 +284,10 @@ public:
void hold(uint64_t req, const std::vector<int32_t>& tokens, const std::vector<MMContentKey>& mm);
void release(uint64_t req);
/* Start of a step's admissions. Snapshots the previous step's lookups handed out have been
* read by its forward by now; until this call they keep their slots against retention,
* reserve_checkpoint's eviction and expiry. */
void begin_step() { handed_.clear(); }
* read by its forward by now, and the ones it reserved have been saved into (the engine
* issues the save before the next step is planned); until this call they keep their slots
* against retention, reserve_checkpoint's eviction and expiry. */
void begin_step() { handed_.clear(); reserved_.clear(); }
/* ---------------- expiry ----------------
*
@@ -307,8 +308,11 @@ public:
/* Reserve a snapshot slot for `session` at `pos`. When the store is full, applies the
* retention policy, then evicts the least recently used snapshot (a use is its reservation or
* a lookup resuming from it), a tip like any other. A snapshot handed out this step is never
* evicted: its admission restores from the slot ahead of the step. RAD_E_FULL when nothing
* can be given up. */
* released, by retention or eviction: its admission restores from the slot ahead of the step.
* Nor is one this call reserved and commit_checkpoint has not yet committed: the engine saves
* into the slot after the step, and a later reservation in the same step must not run
* retention over a save target (a second request reserving a snapshot a first session holds
* below its tip). RAD_E_FULL when nothing can be given up. */
int reserve_checkpoint(uint64_t session, int64_t pos, const BlockHash& prefix,
int32_t* slot_out);
/* The state for `prefix` is now in its slot; lookup() offers it only after this (Ckpt::valid). */
@@ -421,6 +425,13 @@ private:
bool handed(const BlockHash& h) const {
return std::find(handed_.begin(), handed_.end(), h) != handed_.end();
}
/* Snapshots reserve_checkpoint reserved and commit_checkpoint has not committed: the step's
* save targets, kept in their slots until the commit, or until the next step begins when the
* save was skipped (its request gone). */
mutable std::vector<BlockHash> reserved_;
bool reserved(const BlockHash& h) const {
return std::find(reserved_.begin(), reserved_.end(), h) != reserved_.end();
}
mutable uint64_t clock_ = 0;
std::unique_ptr<CheckpointPolicy> policy_;
mutable Stats stats_;