diff --git a/core/mem/prefix.cpp b/core/mem/prefix.cpp index 574889e4..179e9e86 100644 --- a/core/mem/prefix.cpp +++ b/core/mem/prefix.cpp @@ -711,9 +711,9 @@ int64_t PrefixCache::retain(uint64_t session) { int64_t freed = 0; for (const auto& a : all) { if (std::binary_search(keep.begin(), keep.end(), a.first)) { kept.push_back(a.second); continue; } - /* Keep one handed out this step regardless of policy: its admission restores from the - * slot ahead of the step. */ - if (handed(a.second)) { kept.push_back(a.second); continue; } + /* Keep one handed out this step or reserved and not yet committed regardless of policy: + * its admission restores from the slot ahead of the step, or the step's save lands in it. */ + if (handed(a.second) || reserved(a.second)) { kept.push_back(a.second); continue; } auto e = ck_by_prefix_.find(a.second); if (e == ck_by_prefix_.end()) continue; /* And one a lookup resumed from since the session's tip was written. The policy places @@ -754,6 +754,10 @@ int PrefixCache::reserve_checkpoint(uint64_t session, int64_t pos, const BlockHa if (existing != ck_by_prefix_.end()) { existing->second.used = ++clock_; existing->second.used_ns = rad_mono_ns(); + /* Another session's snapshot, this step's save target: retention run by a later + * reservation in this step places its ladder from THAT session's tip and may not keep + * this position. Held until the save is committed. */ + if (!reserved(prefix)) reserved_.push_back(prefix); *slot_out = existing->second.slot; return RAD_OK; } @@ -768,20 +772,21 @@ int PrefixCache::reserve_checkpoint(uint64_t session, int64_t pos, const BlockHa for (uint64_t s : sessions) retain(s); if (kv_->alloc_checkpoint(&slot) < 0) { /* Evict the least recently used snapshot, where a use is its reservation or a lookup - * resuming from it, and never one handed out this step (its admission restores from - * the slot ahead of the step). A tip gets no protection beyond its recency: sessions + * resuming from it, and never one handed out or reserved this step (its admission + * restores from the slot ahead of the step; the save lands in it after). A tip gets + * no protection beyond its recency: sessions * are request ids, and a client that rewrites its last message leaves one tip per * turn that no later turn reaches, while the anchor every turn resumes from is used * each turn. */ const Ckpt* victim = nullptr; for (const auto& kvp : ck_by_prefix_) { - if (handed(kvp.first)) continue; + if (handed(kvp.first) || reserved(kvp.first)) continue; if (!victim || kvp.second.used < victim->used) victim = &kvp.second; } if (!victim) { ++stats_.ckpt_refused; RAD_DEBUG("prefix cache: no snapshot at %lld for session %llu: every one of the " - "%lld slots was handed out this step", (long long)pos, + "%lld slots was handed out this step or awaits its save", (long long)pos, (unsigned long long)session, (long long)ck_by_prefix_.size()); return RAD_E_FULL; } @@ -805,11 +810,17 @@ int PrefixCache::reserve_checkpoint(uint64_t session, int64_t pos, const BlockHa c.used_ns = rad_mono_ns(); ck_by_prefix_.emplace(prefix, c); ck_by_session_[session].push_back(prefix); + /* A save target until committed: a lookup's take_over may index it under another session + * before the save, where retention run by a later reservation in this step would weigh it by + * that session's ladder. */ + reserved_.push_back(prefix); *slot_out = slot; return RAD_OK; } void PrefixCache::commit_checkpoint(const BlockHash& prefix) { + /* The save has landed: the slot is a snapshot like any other from here. */ + reserved_.erase(std::remove(reserved_.begin(), reserved_.end(), prefix), reserved_.end()); auto it = ck_by_prefix_.find(prefix); if (it == ck_by_prefix_.end()) return; /* One that lost its slot since the reservation holds nothing to serve. */ @@ -915,12 +926,12 @@ int64_t PrefixCache::expire(uint64_t now_ns, uint64_t ttl_ns) { } } - /* ---- snapshots: skip uncommitted reservations and ones handed out this step. At most - * --checkpoint-slots of them, so walk the whole index. */ + /* ---- snapshots: skip uncommitted reservations, ones handed out this step and ones a save + * is pending on. At most --checkpoint-slots of them, so walk the whole index. */ std::vector gone; for (const auto& kvp : ck_by_prefix_) { const Ckpt& c = kvp.second; - if (!c.valid || handed(kvp.first)) continue; + if (!c.valid || handed(kvp.first) || reserved(kvp.first)) continue; auto e = map_.find(kvp.first); const uint64_t used = std::max(c.used_ns, e == map_.end() ? 0 : e->second.used_ns); if (used < cutoff) gone.push_back(kvp.first); diff --git a/core/mem/prefix.h b/core/mem/prefix.h index 934d8350..2f171fb1 100644 --- a/core/mem/prefix.h +++ b/core/mem/prefix.h @@ -284,9 +284,10 @@ public: void hold(uint64_t req, const std::vector& tokens, const std::vector& 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 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 policy_; mutable Stats stats_; diff --git a/tests/mem_test.cpp b/tests/mem_test.cpp index 8b46aa55..e2eeb1bf 100644 --- a/tests/mem_test.cpp +++ b/tests/mem_test.cpp @@ -1611,8 +1611,9 @@ TEST(checkpoint_retention_is_budgeted_and_keeps_the_tip) { auto prompt = tok_run(4242, 16 * 64); const std::vector hs = chain_of(prompt); - /* Eight checkpoints at 64, 128, ... 512 fill the pool exactly. */ + /* Eight checkpoints at 64, 128, ... 512 fill the pool exactly, one step each. */ for (int i = 1; i <= 8; ++i) { + f.pc.begin_step(); int32_t s = -1; CHECK_OK(f.pc.reserve_checkpoint(1, i * 64, hs[i * 4 - 1], &s)); } @@ -1621,6 +1622,7 @@ TEST(checkpoint_retention_is_budgeted_and_keeps_the_tip) { /* The ninth forces retention. Geometric backoff over {64..512} keeps 512, 448, 384, 256 and * 64 and releases the rest; the tip survives, as an appending agent transcript needs. */ + f.pc.begin_step(); int32_t s9 = -1; CHECK_OK(f.pc.reserve_checkpoint(1, 9 * 64, hs[9 * 4 - 1], &s9)); auto held = f.pc.checkpoints(1); @@ -1768,6 +1770,89 @@ TEST(a_snapshot_handed_out_this_step_is_not_evicted) { CHECK_OK(f.pc.reserve_checkpoint(1, 128, hs[7], &s2)); } +/* A reservation made this step is a save target: the engine copies the state into its slot + * after the step, and nothing checks the slot again before that copy. A later reservation in the + * same step that finds the store full runs retention over every session, and the policy, placing + * its ladder from each session's tip, may 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), and that snapshot is below the first session's tip. Geometric backoff over + * {64..512} releases 192, so the shared reservation is made to land there. The save then faults + * ("the store does not hold that slot") and the step loop stops the engine. A reservation keeps + * its slot until its commit, or until the next step begins, as a handed-out snapshot does. */ +TEST(a_reservation_this_step_survives_retention) { + Fixture f; + CHECK_OK(f.build(/*with_linear=*/true, /*ckpt_slots=*/8)); + + const auto a = tok_run(4242, 16 * 33); + publish(f, 1, a, 16 * 32); + const std::vector hs = chain_of(a); + std::vector slot_at(9, -1); + for (int i = 1; i <= 8; ++i) slot_at[(size_t)i] = ckpt_at(f, 1, hs, i * 64); + CHECK_EQ(f.kv.free_checkpoint_slots(), 0ll); + + /* Session 2 prefilling the same prompt reserves at 192: the same key, so session 1's slot. */ + f.pc.begin_step(); + int32_t s2 = -1; + CHECK_OK(f.pc.reserve_checkpoint(2, 192, hs[11], &s2)); + CHECK_EQ(s2, slot_at[3]); + + /* The same step's next reservation finds the store full: retention runs over session 1, + * whose ladder does not keep 192. */ + BlockHash other = hs[3]; + other.lo ^= 0x5eedull; + int32_t s3 = -1; + CHECK_OK(f.pc.reserve_checkpoint(3, 64, other, &s3)); + CHECK(s3 >= 0); + CHECK(s3 != s2); + CHECK(f.kv.checkpoint_held(s2)); + bool kept_192 = false; + for (int64_t p : f.pc.checkpoints(1)) if (p == 192) kept_192 = true; + CHECK(kept_192); + CHECK(f.pc.stats().ckpt_retired >= 1ll); + + /* The next step may release it again: an uncommitted reservation whose save was skipped (its + * request gone) must not hold a slot for good. (A committed one is a snapshot like any other + * from the commit on, tested below.) */ + f.pc.begin_step(); + for (int i = 0; i < 3; ++i) { + BlockHash fresh = hs[3]; + fresh.lo ^= 0x1000ull + (uint64_t)i; + int32_t s = -1; + CHECK_OK(f.pc.reserve_checkpoint(10 + (uint64_t)i, 64, fresh, &s)); + } + bool still_192 = false; + for (int64_t p : f.pc.checkpoints(1)) if (p == 192) still_192 = true; + CHECK(!still_192); +} + +/* Eviction after retention skips it too. With one slot, the reservation is the only candidate, + * so a second reservation in the same step is refused instead of taking it; once committed, it + * is a snapshot like any other and the next step's reservation may take the slot. */ +TEST(a_reservation_this_step_is_not_evicted) { + Fixture f; + CHECK_OK(f.build(/*with_linear=*/true, /*ckpt_slots=*/1)); + const auto a = tok_run(5151, 16 * 9); + const std::vector hs = chain_of(a); + + f.pc.begin_step(); + int32_t s1 = -1; + CHECK_OK(f.pc.reserve_checkpoint(1, 64, hs[3], &s1)); + CHECK(s1 >= 0); + + BlockHash other = hs[3]; + other.lo ^= 0x5eedull; + int32_t s2 = -1; + CHECK_EQ(f.pc.reserve_checkpoint(2, 64, other, &s2), RAD_E_FULL); + CHECK(f.kv.checkpoint_held(s1)); + CHECK_EQ(f.pc.stats().ckpt_evictions, 0ll); + CHECK_EQ(f.pc.stats().ckpt_refused, 1ll); + + f.pc.commit_checkpoint(hs[3]); + f.pc.begin_step(); + CHECK_OK(f.pc.reserve_checkpoint(2, 64, other, &s2)); + CHECK_EQ(s2, s1); +} + TEST(a_dead_sessions_tip_is_evictable_so_the_slot_pool_cannot_wedge) { /* One checkpoint each from more distinct sessions than slots, each its session's tip. If tips * were unevictable the pool would wedge and every later request get RAD_E_FULL. A session is a @@ -1790,7 +1875,8 @@ TEST(a_dead_sessions_tip_is_evictable_so_the_slot_pool_cannot_wedge) { } CHECK_EQ(f.kv.free_checkpoint_slots(), 0ll); - /* The ninth session is served by retiring the least recently used tip. */ + /* The ninth session, a step later, is served by retiring the least recently used tip. */ + f.pc.begin_step(); BlockHash fresh = hs[3]; fresh.lo ^= 0x5eedull; int32_t s9 = -1; @@ -1813,6 +1899,7 @@ TEST(a_sessions_index_entry_does_not_outlive_its_last_snapshot) { auto prompt = tok_run(313, 16 * 32); const std::vector hs = chain_of(prompt); for (int i = 1; i <= 5; ++i) { + f.pc.begin_step(); int32_t s = -1; CHECK_OK(f.pc.reserve_checkpoint(7, i * 64, hs[i * 4 - 1], &s)); } @@ -1821,8 +1908,10 @@ TEST(a_sessions_index_entry_does_not_outlive_its_last_snapshot) { CHECK_EQ((long long)f.pc.checkpoints(7).size(), 2ll); CHECK_EQ(f.pc.sessions_tracked(), 1ll); - /* Four more sessions, one snapshot each: session 7's tip is the LRU and is evicted. */ + /* Four more sessions, one snapshot each, a step apart: session 7's tip is the LRU and is + * evicted. */ for (uint64_t sess = 20; sess < 24; ++sess) { + f.pc.begin_step(); BlockHash h = hs[9]; h.lo ^= sess; int32_t s = -1;