Archived
server: DELETE /sessions forgets one stored conversation
A client that deleted a conversation, or compacted it past a prefix, knows the
rest is dead before --prefix-cache-ttl does. DELETE /sessions/<label> (or
?label=, ?id=<engine id>) walks the session's chain from the tip inwards like
expiry: VRAM blocks to the pool, the host copy with them, the session's
snapshots to the store. ?keep_tokens=N keeps the chain's start in whole blocks.
Blocks another stored session shares (the system prompt) and blocks a live
request reads stay and are counted apart; the row is cut at the deepest block
that stayed, so a later release reaches it. A request of the same conversation
in flight is 409. IScheduler::release_session beside reset_prefix_cache,
PrefixCache::release_session over the tiers' session table (find_sessions,
shared_prefix_blocks, cut_session). Counters released{entries,snapshots} on
/sessions and /stats, prefix_cache_released_total{what} on /metrics.
This commit is contained in:
1 parent
123c439684
commit
3aded1bbf8
20 files changed
+722
-2
No files matched your search
@@ -56,6 +56,23 @@ struct StatsScheduler : FakeScheduler {
|
||||
occ->host_cap_bytes = 64 << 20;
|
||||
return 1;
|
||||
}
|
||||
/* The one stored conversation is "conv-7" (engine id 7); "busy" has a request in flight. */
|
||||
std::vector<std::pair<std::string, int64_t>> released;
|
||||
int release_session(const std::string& label, uint64_t id, int64_t keep_tokens,
|
||||
SessionReleased* out) override {
|
||||
if (label == "busy") return RAD_E_STATE;
|
||||
if (label != "conv-7" && id != 7) return RAD_E_NOTFOUND;
|
||||
released.push_back({ label.empty() ? "id" : label, keep_tokens });
|
||||
out->sessions = 1;
|
||||
out->entries = 48 - keep_tokens / 16;
|
||||
out->tokens = out->entries * 16;
|
||||
out->device_bytes = out->entries * 4096;
|
||||
out->host_bytes = out->entries * 2048;
|
||||
out->snapshots = 2;
|
||||
out->kept_tokens = keep_tokens;
|
||||
out->kept_shared = 2;
|
||||
return RAD_OK;
|
||||
}
|
||||
TierCounters tier_counters() const override {
|
||||
TierCounters c;
|
||||
c.to_host = 3; c.dropped = 2; c.promoted = 1; c.host_drops = 4; c.failures = 0;
|
||||
@@ -246,6 +263,69 @@ TEST(sessions_lists_each_conversation_and_what_the_tiers_did) {
|
||||
CHECK_EQ(s["linear"].value("hit_cut_tokens", (int64_t)0), (int64_t)9000);
|
||||
}
|
||||
|
||||
/* DELETE /sessions forgets one stored conversation by its label or engine id and answers with
|
||||
* what went and what stayed; an unknown session is 404, one with a request in flight 409, a
|
||||
* malformed count 400, and an engine without a prefix cache 501. */
|
||||
TEST(a_session_is_released_by_label_or_id_and_the_answer_says_what_went) {
|
||||
Dash d(false);
|
||||
HttpResponse r = d.srv->router().call("DELETE", "/sessions/conv-7");
|
||||
CHECK_EQ(r.status, 200);
|
||||
json o = json::parse(r.body, nullptr, false);
|
||||
CHECK_EQ(o.value("label", ""), std::string("conv-7"));
|
||||
CHECK_EQ(o.value("entries", (int64_t)0), (int64_t)48);
|
||||
CHECK_EQ(o.value("tokens", (int64_t)0), (int64_t)768);
|
||||
CHECK_EQ(o.value("snapshots", (int64_t)0), (int64_t)2);
|
||||
CHECK_EQ(o["kept"].value("shared", (int64_t)0), (int64_t)2);
|
||||
CHECK_EQ(o["kept"].value("tokens", (int64_t)0), (int64_t)0);
|
||||
REQUIRE_EQ(d.sched.released.size(), 1u);
|
||||
CHECK_EQ(d.sched.released[0].second, (int64_t)0);
|
||||
|
||||
/* keep_tokens reaches the scheduler and comes back in the answer. */
|
||||
r = d.srv->router().call("DELETE", "/sessions/conv-7?keep_tokens=160");
|
||||
CHECK_EQ(r.status, 200);
|
||||
o = json::parse(r.body, nullptr, false);
|
||||
CHECK_EQ(o.value("keep_tokens", (int64_t)0), (int64_t)160);
|
||||
CHECK_EQ(o.value("entries", (int64_t)0), (int64_t)38);
|
||||
CHECK_EQ(o["kept"].value("tokens", (int64_t)0), (int64_t)160);
|
||||
REQUIRE_EQ(d.sched.released.size(), 2u);
|
||||
CHECK_EQ(d.sched.released[1].second, (int64_t)160);
|
||||
|
||||
/* By engine id, and by a label the path cannot carry. */
|
||||
r = d.srv->router().call("DELETE", "/sessions?id=7");
|
||||
CHECK_EQ(r.status, 200);
|
||||
o = json::parse(r.body, nullptr, false);
|
||||
CHECK_EQ(o.value("id", (int64_t)0), (int64_t)7);
|
||||
CHECK(!o.contains("label"));
|
||||
r = d.srv->router().call("DELETE", "/sessions?label=conv-7");
|
||||
CHECK_EQ(r.status, 200);
|
||||
|
||||
CHECK_EQ(d.srv->router().call("DELETE", "/sessions/conv-8").status, 404);
|
||||
CHECK_EQ(d.srv->router().call("DELETE", "/sessions?id=8").status, 404);
|
||||
CHECK_EQ(d.srv->router().call("DELETE", "/sessions/busy").status, 409);
|
||||
CHECK_EQ(d.srv->router().call("DELETE", "/sessions").status, 400);
|
||||
CHECK_EQ(d.srv->router().call("DELETE", "/sessions?id=seven").status, 400);
|
||||
CHECK_EQ(d.srv->router().call("DELETE", "/sessions/conv-7?keep_tokens=-1").status, 400);
|
||||
CHECK_EQ(d.sched.released.size(), 4u);
|
||||
|
||||
/* The stored-sessions view carries the counters beside the expiry ones. */
|
||||
const json s = d.get("/sessions");
|
||||
CHECK(s.contains("released"));
|
||||
CHECK(s["released"].contains("entries"));
|
||||
CHECK(s["released"].contains("snapshots"));
|
||||
}
|
||||
|
||||
TEST(a_session_release_without_a_prefix_cache_is_not_implemented) {
|
||||
FakeScheduler sched; /* the base: no release_session */
|
||||
FakeTokenizer tok;
|
||||
Deps dd;
|
||||
dd.sched = &sched;
|
||||
dd.tok = &tok;
|
||||
ServerOptions o;
|
||||
o.model_id = "dash";
|
||||
Server srv(dd, o);
|
||||
CHECK_EQ(srv.router().call("DELETE", "/sessions/conv-7").status, 501);
|
||||
}
|
||||
|
||||
/* ================================================================== the page's field contract */
|
||||
|
||||
/* Every field the page's script reads from /stats (in its update and the request table) and from
|
||||
|
||||
@@ -3234,6 +3234,188 @@ TEST(the_tiers_are_told_the_cache_s_block_ids_and_not_the_requests) {
|
||||
}
|
||||
}
|
||||
|
||||
/* ================================================================== release on request
|
||||
*
|
||||
* A client that deleted a conversation, or kept a prefix of it and replaced the rest, forgets it
|
||||
* through the session table: by label, from the tip inwards, host copy and snapshots with it.
|
||||
* What another conversation also walks stays, as does what a live sequence holds.
|
||||
*/
|
||||
|
||||
/* Publishes `tokens` under `id` with a session label, as the scheduler does at a turn's close,
|
||||
* and frees the sequence, as it does right after. */
|
||||
static void publish_turn(Fixture& f, uint64_t id, const std::vector<int32_t>& tokens,
|
||||
const std::string& label) {
|
||||
CHECK_OK(f.kv.add_sequence(id));
|
||||
CHECK_OK(f.kv.ensure(id, (int64_t)tokens.size()));
|
||||
std::vector<std::vector<int32_t>> bt;
|
||||
for (int32_t g : f.pc.cached_groups()) bt.push_back(f.kv.block_table(id, g));
|
||||
CHECK_OK(f.pc.insert(id, tokens, {}, bt, (int64_t)tokens.size(), label));
|
||||
f.kv.free_sequence(id);
|
||||
}
|
||||
|
||||
/* The label's rows in the session table. */
|
||||
static std::vector<IdleTiers::SessionInfo> rows_of(IdleTiers& t, const std::string& label) {
|
||||
std::vector<IdleTiers::SessionInfo> out;
|
||||
for (const IdleTiers::SessionInfo& s : t.sessions())
|
||||
if (s.label == label) out.push_back(s);
|
||||
return out;
|
||||
}
|
||||
|
||||
TEST(a_released_conversation_goes_from_the_tip_down_to_what_another_one_shares) {
|
||||
Fixture f;
|
||||
CHECK_OK(f.build(/*with_linear=*/false, /*ckpt_slots=*/0));
|
||||
IdleTiers t;
|
||||
CHECK_OK(t.configure(tier_config(1 << 20, 1024)));
|
||||
f.pc.set_tiers(&t);
|
||||
|
||||
/* Conversation A over three turns under three request ids: each turn extends the last. The
|
||||
* first two blocks are a system prompt conversation B opens with too. */
|
||||
const auto a1 = tok_run(100, 64); /* 4 blocks */
|
||||
auto a2 = a1; for (int i = 0; i < 32; ++i) a2.push_back(700 + i); /* 6 */
|
||||
auto a3 = a2; for (int i = 0; i < 32; ++i) a3.push_back(800 + i); /* 8 */
|
||||
publish_turn(f, 1, a1, "chat-a");
|
||||
publish_turn(f, 2, a2, "chat-a");
|
||||
publish_turn(f, 3, a3, "chat-a");
|
||||
auto b = tok_run(100, 32); for (int i = 0; i < 64; ++i) b.push_back(900 + i); /* 6 */
|
||||
publish_turn(f, 4, b, "chat-b");
|
||||
CHECK_EQ(f.pc.size(), 12ll); /* 8 + 4 own blocks of B */
|
||||
REQUIRE_EQ(rows_of(t, "chat-a").size(), 1u); /* one row per conversation */
|
||||
CHECK_EQ(rows_of(t, "chat-a")[0].n_tokens, 128ll);
|
||||
/* Every entry has its host copy. */
|
||||
copy_now(t);
|
||||
CHECK_EQ(t.arena().used_slots(), 12);
|
||||
const int64_t pool_before = f.kv.free_blocks(G_FULL);
|
||||
|
||||
SessionRelease r;
|
||||
CHECK_EQ(f.pc.release_session("chat-c", 0, 0, &r), RAD_E_NOTFOUND);
|
||||
CHECK_EQ(f.pc.release_session("", 0, 0, &r), RAD_E_INVAL);
|
||||
|
||||
CHECK_OK(f.pc.release_session("chat-a", 0, 0, &r));
|
||||
CHECK_EQ(r.sessions, 1ll);
|
||||
CHECK_EQ(r.entries, 6ll); /* 8 less the 2 B walks too */
|
||||
CHECK_EQ(r.tokens, 96ll);
|
||||
CHECK_EQ(r.kept_shared, 2ll);
|
||||
CHECK_EQ(r.kept_live, 0ll);
|
||||
CHECK_EQ(r.kept_tokens, 0ll);
|
||||
CHECK(r.device_bytes > 0);
|
||||
CHECK_EQ(r.host_bytes, 6 * 1024ll);
|
||||
/* Pool and arena got the six back; the index holds B's chain and the shared start. */
|
||||
CHECK_EQ(f.kv.free_blocks(G_FULL), pool_before + 6);
|
||||
CHECK_EQ(t.arena().used_slots(), 6);
|
||||
CHECK_EQ(f.pc.size(), 6ll);
|
||||
CHECK_EQ(f.pc.stats().released, 6ll);
|
||||
/* A's next turn hits the shared system prompt and nothing more; B is whole. */
|
||||
CHECK_EQ(f.pc.lookup(a3, {}).n_attn_tokens, 32);
|
||||
CHECK_EQ(f.pc.lookup(b, {}).n_attn_tokens, 80);
|
||||
CHECK_EQ(rows_of(t, "chat-a").size(), 0u);
|
||||
REQUIRE_EQ(rows_of(t, "chat-b").size(), 1u);
|
||||
CHECK_EQ(rows_of(t, "chat-b")[0].n_tokens, 96ll);
|
||||
|
||||
/* Released twice is not found the second time. */
|
||||
CHECK_EQ(f.pc.release_session("chat-a", 0, 0, &r), RAD_E_NOTFOUND);
|
||||
}
|
||||
|
||||
TEST(keep_tokens_leaves_the_chains_start_standing_in_whole_blocks) {
|
||||
Fixture f;
|
||||
CHECK_OK(f.build(/*with_linear=*/false, /*ckpt_slots=*/0));
|
||||
IdleTiers t;
|
||||
CHECK_OK(t.configure(tier_config(1 << 20, 1024)));
|
||||
f.pc.set_tiers(&t);
|
||||
|
||||
auto b = tok_run(100, 32); for (int i = 0; i < 64; ++i) b.push_back(900 + i); /* 6 blocks */
|
||||
publish_turn(f, 4, b, "chat-b");
|
||||
|
||||
/* 40 tokens keep three blocks: the one holding token 40 goes with them. */
|
||||
SessionRelease r;
|
||||
CHECK_OK(f.pc.release_session("chat-b", 0, 40, &r));
|
||||
CHECK_EQ(r.entries, 3ll);
|
||||
CHECK_EQ(r.kept_tokens, 48ll);
|
||||
CHECK_EQ(r.kept_shared, 0ll);
|
||||
CHECK_EQ(f.pc.lookup(b, {}).n_attn_tokens, 48);
|
||||
/* The row stays, cut to the kept prefix: the client extends it again. */
|
||||
REQUIRE_EQ(rows_of(t, "chat-b").size(), 1u);
|
||||
CHECK_EQ(rows_of(t, "chat-b")[0].n_tokens, 48ll);
|
||||
const uint64_t id = rows_of(t, "chat-b")[0].id;
|
||||
|
||||
/* The same by engine id, keeping more than is left: nothing goes, nothing breaks. */
|
||||
CHECK_OK(f.pc.release_session("", id, 200, &r));
|
||||
CHECK_EQ(r.entries, 0ll);
|
||||
CHECK_EQ(r.kept_tokens, 48ll);
|
||||
CHECK_EQ(f.pc.lookup(b, {}).n_attn_tokens, 48);
|
||||
|
||||
/* A later turn extends the stub into a full row again. */
|
||||
publish_turn(f, 5, b, "chat-b");
|
||||
REQUIRE_EQ(rows_of(t, "chat-b").size(), 1u);
|
||||
CHECK_EQ(rows_of(t, "chat-b")[0].n_tokens, 96ll);
|
||||
CHECK_EQ(f.pc.lookup(b, {}).n_attn_tokens, 80);
|
||||
}
|
||||
|
||||
TEST(a_release_takes_the_sessions_snapshots_past_the_kept_prefix_and_leaves_live_blocks) {
|
||||
Fixture f;
|
||||
CHECK_OK(f.build(/*with_linear=*/true, /*ckpt_slots=*/4));
|
||||
IdleTiers t;
|
||||
IdleTiers::Config tc = tier_config(1 << 20, 1024);
|
||||
tc.ckpt_bytes = f.kv.checkpoint_bytes();
|
||||
tc.ckpt_slots = 4;
|
||||
CHECK_OK(t.configure(tc));
|
||||
f.pc.set_tiers(&t);
|
||||
|
||||
const auto a = tok_run(100, 128); /* 8 blocks, checkpoints at 64, 128 */
|
||||
const std::vector<BlockHash> hs = chain_of(a);
|
||||
CHECK_OK(f.kv.add_sequence(3));
|
||||
CHECK_OK(f.kv.ensure(3, 128));
|
||||
ckpt_at(f, 3, hs, 64);
|
||||
ckpt_at(f, 3, hs, 128);
|
||||
f.pc.begin_step();
|
||||
{
|
||||
std::vector<std::vector<int32_t>> bt;
|
||||
for (int32_t g : f.pc.cached_groups()) bt.push_back(f.kv.block_table(3, g));
|
||||
CHECK_OK(f.pc.insert(3, a, {}, bt, 128, "chat-a"));
|
||||
}
|
||||
f.kv.free_sequence(3);
|
||||
CHECK_EQ(f.kv.free_checkpoint_slots(), 2ll);
|
||||
CHECK_EQ(rows_of(t, "chat-a")[0].snapshots, 2ll);
|
||||
|
||||
/* A waiting request restored the chain and holds it; a running one adopted the first
|
||||
* five blocks. Both keep their blocks through a release. */
|
||||
std::vector<std::vector<int32_t>> found;
|
||||
const CacheHit h = f.pc.lookup(a, {}, &found);
|
||||
CHECK_EQ(h.n_attn_tokens, 112);
|
||||
CHECK_OK(f.kv.add_sequence(9));
|
||||
std::vector<int32_t> five(found[0].begin(), found[0].begin() + 5);
|
||||
CHECK_OK(f.kv.adopt(9, f.pc.cached_groups()[0], five, 80));
|
||||
f.pc.begin_step();
|
||||
|
||||
SessionRelease r;
|
||||
CHECK_OK(f.pc.release_session("chat-a", 0, 64, &r));
|
||||
/* Blocks 4..7 are past the kept 64 tokens; block 4 is live, 5..7 go. */
|
||||
CHECK_EQ(r.entries, 3ll);
|
||||
CHECK_EQ(r.kept_live, 1ll);
|
||||
CHECK_EQ(r.kept_tokens, 64ll);
|
||||
/* The snapshot at 128 sat on block 7 and went with it; the one at 64 is on the kept prefix. */
|
||||
CHECK_EQ(r.snapshots, 1ll);
|
||||
CHECK_EQ(f.kv.free_checkpoint_slots(), 3ll);
|
||||
const std::vector<int64_t> left = f.pc.checkpoints(3);
|
||||
REQUIRE_EQ(left.size(), 1u);
|
||||
CHECK_EQ(left[0], 64ll);
|
||||
CHECK_EQ(f.pc.stats().ckpt_released, 1ll);
|
||||
/* The live block is still indexed and the sequence still reads it; the row reaches it. */
|
||||
CHECK_EQ(f.kv.block_refcount(G_FULL, five[4]), 2);
|
||||
REQUIRE_EQ(rows_of(t, "chat-a").size(), 1u);
|
||||
CHECK_EQ(rows_of(t, "chat-a")[0].n_tokens, 80ll);
|
||||
CHECK_EQ(f.pc.lookup(a, {}).n_attn_tokens, 80);
|
||||
CHECK_EQ(f.pc.lookup(a, {}).n_linear_tokens, 64);
|
||||
|
||||
/* The sequence is gone: the block it held goes on the next release. */
|
||||
f.kv.free_sequence(9);
|
||||
CHECK_OK(f.pc.release_session("chat-a", 0, 64, &r));
|
||||
CHECK_EQ(r.entries, 1ll);
|
||||
CHECK_EQ(r.kept_live, 0ll);
|
||||
CHECK_EQ(f.pc.lookup(a, {}).n_attn_tokens, 64);
|
||||
CHECK_EQ(f.pc.lookup(a, {}).n_linear_tokens, 64);
|
||||
CHECK_EQ(rows_of(t, "chat-a")[0].n_tokens, 64ll);
|
||||
}
|
||||
|
||||
/* ================================================================== the pool in bytes
|
||||
*
|
||||
* Blocks and tokens cannot be summed across KV groups: a recurrent group has no token axis and
|
||||
|
||||
@@ -424,6 +424,16 @@ struct FakePC : IPrefixCache {
|
||||
std::vector<uint64_t> holds, releases;
|
||||
void hold(const Request& r) override { holds.push_back(r.id); }
|
||||
void release(const Request& r) override { releases.push_back(r.id); }
|
||||
/* What the scheduler asked to be released, recorded; the answer is canned. */
|
||||
struct Released { std::string label; uint64_t id; int64_t keep_tokens; };
|
||||
std::vector<Released> released_calls;
|
||||
int release_rc = RAD_OK;
|
||||
int release_session(const std::string& label, uint64_t id, int64_t keep_tokens,
|
||||
SessionRelease* out) override {
|
||||
released_calls.push_back({ label, id, keep_tokens });
|
||||
if (out) { *out = SessionRelease{}; out->sessions = 1; out->entries = 5; out->tokens = 80; }
|
||||
return release_rc;
|
||||
}
|
||||
/* What the reserve asked for, call by call. */
|
||||
std::vector<int64_t> trims;
|
||||
int64_t trim(int64_t n) override {
|
||||
|
||||
@@ -1265,6 +1265,49 @@ TEST(cancellation_frees_blocks_immediately) {
|
||||
CHECK_EQ((int)f.sch.find(b)->state, (int)ReqState::Cancelled);
|
||||
}
|
||||
|
||||
/* A release of a conversation with a request of it in flight is refused, whether that request
|
||||
* is queued or running: the cache would skip its blocks as live and the request's close would
|
||||
* publish the whole chain again, so the client would have been told of a release that did not
|
||||
* happen. Once the request is gone the release reaches the cache with what the client asked. */
|
||||
TEST(a_session_release_waits_for_the_conversations_own_request) {
|
||||
Fixture f;
|
||||
f.build(/*blocks=*/40, /*states=*/64);
|
||||
CHECK_OK(f.start());
|
||||
|
||||
auto r = f.request(160, 64);
|
||||
r->session_label = "chat-7";
|
||||
const uint64_t a = f.add(std::move(r));
|
||||
CHECK(a != 0);
|
||||
|
||||
SessionRelease out;
|
||||
/* Queued, not yet admitted. */
|
||||
CHECK_EQ(f.sch.release_session("chat-7", 0, 0, &out), RAD_E_STATE);
|
||||
CHECK_EQ(f.sch.release_session("", a, 0, &out), RAD_E_STATE);
|
||||
CHECK_EQ(f.pc.released_calls.size(), 0u);
|
||||
|
||||
/* Running. */
|
||||
CHECK(f.sch.step() != nullptr);
|
||||
f.commit_plain();
|
||||
CHECK_EQ(f.sch.release_session("chat-7", 0, 32, &out), RAD_E_STATE);
|
||||
/* Another conversation is not in its way. */
|
||||
CHECK_OK(f.sch.release_session("chat-8", 0, 0, &out));
|
||||
REQUIRE_EQ(f.pc.released_calls.size(), 1u);
|
||||
CHECK(f.pc.released_calls[0].label == "chat-8");
|
||||
|
||||
/* Finished: the release goes through with label, id and the kept prefix as given. */
|
||||
CHECK_OK(f.sch.cancel(a));
|
||||
CHECK_OK(f.sch.reap(a));
|
||||
CHECK_OK(f.sch.release_session("chat-7", 0, 48, &out));
|
||||
REQUIRE_EQ(f.pc.released_calls.size(), 2u);
|
||||
CHECK(f.pc.released_calls[1].label == "chat-7");
|
||||
CHECK_EQ(f.pc.released_calls[1].keep_tokens, 48ll);
|
||||
CHECK_EQ(out.entries, 5ll);
|
||||
|
||||
/* Nothing named is a bad request, not a cache call. */
|
||||
CHECK_EQ(f.sch.release_session("", 0, 0, &out), RAD_E_INVAL);
|
||||
CHECK_EQ(f.pc.released_calls.size(), 2u);
|
||||
}
|
||||
|
||||
/* A reap while the step is still on the device. Common: the server reaps as soon as a sink
|
||||
* finishes, and cancelling finishes it, so every mid-decode disconnect lands here. Releasing the
|
||||
* Request now would destroy it under commit(); returning its slot now would let a newly admitted
|
||||
|
||||
Reference in new issue
Block a user