Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions docs/prefix-cache-offload.md
Original file line number Diff line number Diff line change
Expand Up @@ -623,12 +623,11 @@ from their last put or restore; this is acceptable for a cold-tier copy because
an instance that keeps the prefix hot re-cascades it on eviction.

At most once a minute, the model-thread poll walks from the oldest entry,
skipping interior nodes and records that cannot be dropped, and stopping at
the first fresh leaf or 64 expired leaves. Expired leaves are dropped outright;
skipping interior nodes, fresh leaves, and records that cannot be dropped, and
stopping after 64 expired leaves. Expired leaves are dropped outright;
they are never sent down the cascade. Page-only ancestors are removed with their
last child; in-flight copies and exports retain their storage. LRU positions change only for the accessed key.
A fresh former interior node left as a leaf by pressure eviction can delay a
sweep by up to one max age. The manager sweeps every namespace on its store
The manager sweeps every namespace on its store
tick, using each index's own once-a-minute limit. Expiry does not apply to the
GPU tier.

Expand Down
5 changes: 5 additions & 0 deletions helios/src/server/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2114,6 +2114,11 @@ impl<'a> Worker<'a> {
incoming.prompt.tokens = tokens;
incoming.prompt.max_new_tokens = remaining;
incoming.readmitted = true;
incoming.blocked = None;
incoming.pool_lookup = None;
incoming.pool_lookup_started = None;
incoming.pool_plan = None;
incoming.restore_wait = RestoreWait::default();
self.begin_pool_lookups(std::slice::from_mut(&mut incoming));
let seed = active.carried.seed.unwrap_or(incoming.prompt.sampling.seed);
let generated_tokens = active.carried.generated_tokens + generated;
Expand Down
79 changes: 79 additions & 0 deletions helios/src/server/runtime/phase_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6847,3 +6847,82 @@ fn core_parameter_preemption_preserves_prefix_identity_and_packed_value_on_forwa
.unwrap();
assert_eq!(upload.bytes, 0.75f32.to_le_bytes());
}

#[test]
fn worker_park_clears_stale_pool_plan_and_lookup() {
let fixture = Fixture::new();
let (_sender, receiver) = mpsc::channel();
let mut worker = fixture.worker(receiver);
let mut store = FakeStore::new(None);
store.pooled = true;
store.pending_tickets = true;
let withdrawn = store.withdrawn.clone();
worker.attach_store(Some(Box::new(store)));

let (mut incoming, _generation) = fixture.incoming(vec![1, 2], 1);
incoming.prompt.reasoning_cut = Some(budget_cut(4, 2));
worker.enqueue(vec![incoming]);
assert_eq!(worker.pending.len(), 1);
assert!(worker.pending[0].pool_plan.is_some());
assert!(worker.pending[0].pool_lookup.is_some());
assert_eq!(
worker.pending[0]
.pool_plan
.as_ref()
.unwrap()
.history()
.token_count(),
2
);

// Expire the lookup grace window so the request admits while its pending ticket stays attached.
worker.pending[0].pool_lookup_started =
Some(std::time::Instant::now() - std::time::Duration::from_secs(60));
assert_eq!(worker.admit().unwrap(), ControlFlow::Continue(true));
let slot = worker
.active
.iter()
.position(Option::is_some)
.expect("request admitted");
assert!(
worker.active[slot]
.as_ref()
.unwrap()
.incoming
.pool_plan
.is_some(),
"precondition: pending pool plan survives admission"
);
assert!(
worker.active[slot]
.as_ref()
.unwrap()
.incoming
.pool_lookup
.is_some(),
"precondition: pending pool lookup survives admission"
);

// Drive the single-token reasoning pass so publish() triggers a forced reasoning cut and
// calls Worker::park while the pre-admission pending pool_plan/pool_lookup are still attached.
let mut sequence = 0;
budget_pass(&mut worker, &mut sequence, &[3]);
assert_eq!(worker.parked.len(), 1);
let parked_incoming = &worker.parked[0].0;
assert_eq!(parked_incoming.prompt.tokens, vec![1, 2, 3, 4]);
assert_eq!(
parked_incoming
.pool_plan
.as_ref()
.expect("fresh pool plan for extended prompt")
.history()
.token_count(),
4,
"parked request must not retain the pre-park 2-token PrefixHistory"
);
assert_eq!(
withdrawn.load(Ordering::Relaxed),
1,
"stale pre-park lookup ticket must be dropped on park"
);
}
69 changes: 68 additions & 1 deletion selene-manager/src/pool/claims.rs
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,8 @@ pub struct Channel {

/// What a claim on the manager's store tracks beyond the common state.
pub struct ManagerGet {
/// Manager-allocated transfer ID in the store's per-connection namespace.
pub transfer: u64,
/// Persisted page and row checksums by record, from the store's metadata.
pub checks: std::collections::HashMap<u32, (Vec<u64>, u64)>,
pub remaining: usize,
Expand Down Expand Up @@ -278,6 +280,16 @@ impl Claim {
}
}

#[cfg(test)]
pub(super) fn new_for_test(
key: ClaimKey,
sid: u64,
mp: MpState,
limits: &flare_selene::shared::Limits,
) -> Self {
Self::new(key, sid, &v2::Claim::default(), mp, limits, 0)
}

pub(super) fn channel(&self) -> Option<&Channel> {
root(&self.legs, false)
}
Expand Down Expand Up @@ -865,6 +877,7 @@ impl Core {
claim.total_pages = walk.pages;
claim.row = m.want_row;
claim.manager = Some(ManagerGet {
transfer: 0,
checks: Default::default(),
remaining: 0,
pending: 0,
Expand Down Expand Up @@ -1497,6 +1510,10 @@ impl Core {
let Some(claim) = self.claims.get_mut(&key) else {
return Ok(());
};
if claim.channel().is_some() {
// A data-channel requester's windows are released when its handler reports them sent.
return Ok(());
}
let Some(window) = claim.windows.get_mut(m.index as usize) else {
return Err(Violation::new(
"WindowReleased names a window that was never issued",
Expand Down Expand Up @@ -2184,9 +2201,10 @@ impl Core {
rec.refused = true;
rec.pending_windows = 0;
let connection = rec.link.connection;
let transfer = rec.transfer;
self.store_send(crate::store::StoreCommand::Cancel {
connection,
transfer: key.2,
transfer,
});
}
self.puts_settle(key);
Expand Down Expand Up @@ -2388,6 +2406,7 @@ mod tests {
},
];
claim.manager = Some(ManagerGet {
transfer: 0,
checks: Default::default(),
remaining: 0,
pending: 0,
Expand Down Expand Up @@ -2473,6 +2492,7 @@ mod tests {
PutRec {
phase: PutPhase::Open,
link,
transfer: 2,
pending_windows: 0,
remaining: 0,
windows: [(0, std::sync::Arc::new(core.staging.allocate(4096).unwrap()))].into(),
Expand Down Expand Up @@ -2504,6 +2524,7 @@ mod tests {
core.store = Some(StoreHandle {
commands,
next_connection: 7,
next_transfer: 0,
});
core.tick();
assert!(!core.procs[&process].exited);
Expand Down Expand Up @@ -2998,4 +3019,50 @@ mod tests {
core.export_end(1, complete_export()).unwrap();
assert_eq!(core.claims[&key].reason, v2::ClaimReason::CopyError);
}

#[tokio::test(flavor = "multi_thread")]
async fn window_released_from_channel_requester_does_not_drop_extent() {
let (mut core, _out) = core();
let key = delivered(&mut core);
let extent = std::sync::Arc::new(core.staging.allocate(4096).unwrap());
{
let claim = core.claims.get_mut(&key).unwrap();
claim.legs = vec![Leg {
rank: 0,
source: false,
ch: Channel {
transfer: 77,
token: [0; flare_selene::shared::frame::TOKEN_BYTES],
},
}];
claim.windows[0].phase = Phase::RequesterRead;
claim.windows[0].readers = 1;
claim.windows[0].extent = Some(extent);
}
core.channels.insert(77, ChanOwner::Claim(key));

core.window_released(
1,
v2::WindowReleased {
claim_id: key.2,
index: 0,
},
)
.unwrap();

let window = &core.claims[&key].windows[0];
assert_eq!(window.phase, Phase::RequesterRead);
assert_eq!(window.readers, 1);
assert!(window.extent.is_some());
assert_eq!(core.staging.used(), 4096);

core.channel_event(channel::Event::Sent {
transfer: 77,
index: 0,
});
let window = &core.claims[&key].windows[0];
assert_eq!(window.phase, Phase::Free);
assert!(window.extent.is_none());
assert_eq!(core.staging.used(), 0);
}
}
1 change: 1 addition & 0 deletions selene-manager/src/pool/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -424,6 +424,7 @@ impl Core {
store: store.map(|commands| StoreHandle {
commands,
next_connection: 1 << 40,
next_transfer: 0,
}),
self_tx: events.clone(),
puts: HashMap::new(),
Expand Down
37 changes: 36 additions & 1 deletion selene-manager/src/pool/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ pub fn walk_chain(
start_page: u64,
max_nodes: usize,
) -> Option<ChainWalk> {
if page_tokens == 0 {
if page_tokens == 0 || max_nodes == 0 {
return None;
}
let end_of = |key: Key| key.consumed_tokens / page_tokens;
Expand All @@ -181,13 +181,20 @@ pub fn walk_chain(
}];
let endpoint_end = end_of(endpoint.key);
let mut current = endpoint.clone();
let mut steps = 1usize;
loop {
let end = end_of(current.key);
let first = end.checked_sub(u64::from(current.tail_pages))?;
if first <= start_page {
break;
}
let parent_key = current.parent?;
if parent_key.consumed_tokens == 0
|| parent_key.consumed_tokens >= current.key.consumed_tokens
|| steps >= max_nodes
{
return None;
}
let parent = index.get(source, parent_key, endpoint.tier).or_else(|| {
// The manager reads a chain record by record from its own memory or disk.
if source != SourceId::Manager {
Expand All @@ -204,6 +211,7 @@ pub fn walk_chain(
if end_of(parent.key) != first || nodes.len() >= max_nodes {
return None;
}
steps += 1;
// A checkpoint that ends in the page its parent ended in holds no page of its
// own, and a record with no page and no row is not part of a claim.
if parent.tail_pages > 0 {
Expand Down Expand Up @@ -391,4 +399,31 @@ mod tests {
index.upsert(SOURCE, endpoint.clone());
assert!(walk_chain(&index, SOURCE, &endpoint, 64, 0, 100).is_none());
}

#[test]
fn walk_chain_rejects_self_parent_cycles_and_zero_page_chains_beyond_max_nodes() {
// Self-parenting zero-tail entry must terminate and return None.
let mut index = ScopeIndex::default();
let self_parent = entry(128, 1, Some((128, 1)), 0, Tier::Dram);
index.upsert(SOURCE, self_parent.clone());
assert!(walk_chain(&index, SOURCE, &self_parent, 64, 0, 100).is_none());

// Two-node cycle with tail_pages == 0 must terminate and return None.
let mut index = ScopeIndex::default();
let a = entry(128, 1, Some((128, 2)), 0, Tier::Dram);
let b = entry(128, 2, Some((128, 1)), 0, Tier::Dram);
index.upsert(SOURCE, a.clone());
index.upsert(SOURCE, b);
assert!(walk_chain(&index, SOURCE, &a, 64, 0, 100).is_none());

// Strictly decreasing zero-tail chain longer than max_nodes must be bounded.
let mut index = ScopeIndex::default();
index.upsert(SOURCE, entry(128, 0, None, 2, Tier::Dram));
index.upsert(SOURCE, entry(129, 1, Some((128, 0)), 0, Tier::Dram));
index.upsert(SOURCE, entry(130, 2, Some((129, 1)), 0, Tier::Dram));
let tip = entry(131, 3, Some((130, 2)), 0, Tier::Dram);
index.upsert(SOURCE, tip.clone());
assert!(walk_chain(&index, SOURCE, &tip, 64, 0, 3).is_none());
assert!(walk_chain(&index, SOURCE, &tip, 64, 0, 4).is_some());
}
}
41 changes: 41 additions & 0 deletions selene-manager/src/pool/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,11 @@ pub fn entry_from(entry: &v2::IndexEntry) -> Checked<Entry> {
if key.consumed_tokens == 0 {
return Err(Violation::new("zero consumed_tokens"));
}
if let Some(parent_key) = parent
&& (parent_key.consumed_tokens == 0 || parent_key.consumed_tokens >= key.consumed_tokens)
{
return Err(Violation::new("parent consumed_tokens must precede entry"));
}
if entry.required_profiles.len() > 64
|| entry.required_profiles.iter().any(|p| p.len() != 32)
|| (!entry.packed && !entry.required_profiles.is_empty())
Expand Down Expand Up @@ -255,3 +260,39 @@ pub fn validate_scope(scope: Option<&v2::Scope>, pool_name: &str) -> Option<Vali
descriptor,
})
}

#[cfg(test)]
mod tests {
use super::*;

fn wire_entry(tokens: u64, tag: u8, parent: Option<(u64, u8)>) -> v2::IndexEntry {
v2::IndexEntry {
key: Some(v2::PrefixKey {
consumed_tokens: tokens,
digest: vec![tag; 16],
}),
has_parent: parent.is_some(),
parent: parent.map(|(t, d)| v2::PrefixKey {
consumed_tokens: t,
digest: vec![d; 16],
}),
bucket: vec![9; 16],
tail_pages: 0,
row: true,
tier: v2::Tier::Dram as i32,
bytes: 4096,
packed: false,
required_profiles: Vec::new(),
}
}

#[test]
fn entry_from_rejects_self_parent_and_non_monotonic_parent() {
assert!(entry_from(&wire_entry(128, 1, Some((128, 1)))).is_err());
assert!(entry_from(&wire_entry(128, 1, Some((128, 2)))).is_err());
assert!(entry_from(&wire_entry(128, 1, Some((256, 2)))).is_err());
assert!(entry_from(&wire_entry(128, 1, Some((0, 2)))).is_err());
assert!(entry_from(&wire_entry(128, 1, Some((64, 2)))).is_ok());
assert!(entry_from(&wire_entry(128, 1, None)).is_ok());
}
}
Loading
Loading