Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 2 additions & 0 deletions lib/kv-router/src/indexer/branch_sharded.rs
Original file line number Diff line number Diff line change
Expand Up @@ -680,6 +680,7 @@ impl<S: AsyncShardHandle> BranchShardedIndexer<S> {
let shard_event = RouterEvent {
worker_id: event.worker_id,
state_source: event.state_source,
session_id: event.session_id.clone(),
storage_tier: event.storage_tier,
residency_domain: event.residency_domain.clone(),
event: KvCacheEvent {
Expand All @@ -701,6 +702,7 @@ impl<S: AsyncShardHandle> BranchShardedIndexer<S> {
let broadcast_event = RouterEvent {
worker_id: event.worker_id,
state_source: event.state_source,
session_id: event.session_id.clone(),
storage_tier: event.storage_tier,
residency_domain: event.residency_domain.clone(),
event: KvCacheEvent {
Expand Down
1 change: 1 addition & 0 deletions lib/kv-router/src/indexer/concurrent_radix_tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -516,6 +516,7 @@ impl ConcurrentRadixTree {
let event = RouterEvent {
worker_id: worker.worker_id,
state_source: None,
session_id: None,
storage_tier: crate::protocols::StorageTier::Device,
residency_domain: crate::protocols::WireResidencyDomain::explicit(
crate::protocols::ResidencyDomain::Worker,
Expand Down
1 change: 1 addition & 0 deletions lib/kv-router/src/indexer/lower_tier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1659,6 +1659,7 @@ mod tests {
.apply_event(RouterEvent {
worker_id: 7,
state_source: None,
session_id: None,
storage_tier: StorageTier::Device,
residency_domain: WireResidencyDomain::default(),
event: crate::protocols::KvCacheEvent {
Expand Down
1 change: 1 addition & 0 deletions lib/kv-router/src/indexer/positional.rs
Original file line number Diff line number Diff line change
Expand Up @@ -530,6 +530,7 @@ impl PositionalIndexer {
events.push(RouterEvent {
worker_id: worker.worker_id,
state_source: None,
session_id: None,
storage_tier: crate::protocols::StorageTier::Device,
residency_domain: crate::protocols::WireResidencyDomain::explicit(
crate::protocols::ResidencyDomain::Worker,
Expand Down
1 change: 1 addition & 0 deletions lib/kv-router/src/indexer/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2495,6 +2495,7 @@ mod local_indexer_tests {
RouterEvent {
worker_id: 0,
state_source: None,
session_id: None,
storage_tier: StorageTier::Device,
residency_domain: WireResidencyDomain::default(),
event: KvCacheEvent {
Expand Down
51 changes: 45 additions & 6 deletions lib/kv-router/src/protocols.rs
Original file line number Diff line number Diff line change
Expand Up @@ -938,11 +938,23 @@ impl Placement {
pub struct PlacementEvent {
pub placement: Placement,
pub event: KvCacheEvent,
/// Session that triggered this store or reuse report, if provided by the engine.
#[serde(default, skip_serializing_if = "Option::is_none")]
pub session_id: Option<String>,
}

impl PlacementEvent {
pub fn new(placement: Placement, event: KvCacheEvent) -> Self {
Self { placement, event }
Self {
placement,
event,
session_id: None,
}
}

pub fn with_session_id(mut self, session_id: impl Into<String>) -> Self {
self.session_id = Some(session_id.into());
self
}

pub fn local_gpu(worker_id: WorkerId, event: KvCacheEvent) -> Self {
Expand All @@ -953,12 +965,14 @@ impl PlacementEvent {
let PlacementOwner::LocalWorker(worker) = self.placement.owner else {
return None;
};
Some(RouterEvent::with_residency_domain(
let mut event = RouterEvent::with_residency_domain(
worker.worker_id,
self.event,
self.placement.tier,
self.placement.residency_domain,
))
);
event.session_id = self.session_id;
Some(event)
}
}

Expand Down Expand Up @@ -1458,6 +1472,9 @@ pub enum KvCacheEventError {
UnsupportedResidencyDomain,
}

/// Reserved session key for events that predate session attribution.
pub const UNATTRIBUTED_SESSION_ID: &str = "__dynamo_unattributed__";

/// A [`KvCacheEvent`] on a specific LLM worker denoted by [`WorkerId`].
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct RouterEvent {
Expand All @@ -1476,10 +1493,13 @@ pub struct RouterEvent {
///
/// This is absent on the legacy Worker-only wire. CacheOwner events are
/// valid only on a versioned, residency-aware source where this field is
/// present; they must never be sent to legacy consumers. Keep this field
/// last so legacy positional MessagePack remains prefix-compatible.
/// present; they must never be sent to legacy consumers. Optional extension
/// fields remain append-only so legacy positional MessagePack stays prefix-compatible.
#[serde(default, skip_serializing_if = "Option::is_none")]
pub state_source: Option<CacheOwnerId>,
/// Session that triggered this store or reuse report, if provided by the engine.
#[serde(default, skip_serializing_if = "Option::is_none")]
pub session_id: Option<String>,
}

impl RouterEvent {
Expand Down Expand Up @@ -1516,6 +1536,7 @@ impl RouterEvent {
Self {
worker_id,
state_source: None,
session_id: None,
storage_tier,
residency_domain: WireResidencyDomain::explicit(residency_domain),
event,
Expand All @@ -1539,6 +1560,18 @@ impl RouterEvent {
self
}

pub fn with_session_id(mut self, session_id: impl Into<String>) -> Self {
self.session_id = Some(session_id.into());
self
}

/// Return the reported session or the shared fallback for unattributed events.
pub fn session_id_or_unattributed(&self) -> &str {
self.session_id
.as_deref()
.unwrap_or(UNATTRIBUTED_SESSION_ID)
}

/// Resolve a storage mutation to its canonical logical domain.
pub fn resolved_residency_domain(&self) -> Result<ResidencyDomain, UnsupportedResidencyDomain> {
let domain = self.residency_domain.parse()?.unwrap_or_default();
Expand Down Expand Up @@ -1902,6 +1935,7 @@ mod tests {
];
let decoded: Vec<RouterEvent> =
rmp_serde::from_slice(&rmp_serde::to_vec_named(&legacy).unwrap()).unwrap();
assert_eq!(decoded[0].session_id, None);
assert_eq!(
decoded[0].resolved_residency_domain(),
Ok(ResidencyDomain::Worker)
Expand All @@ -1913,12 +1947,17 @@ mod tests {
stored(3),
StorageTier::Disk,
ResidencyDomain::Worker,
);
)
.with_session_id("session-1");
let old_reader: LegacyRouterEvent =
rmp_serde::from_slice(&rmp_serde::to_vec_named(&explicit_worker).unwrap()).unwrap();
Comment thread
karen-sy marked this conversation as resolved.
Resolved
assert_eq!(old_reader.event.event_id, 3);
assert_eq!(old_reader.storage_tier, StorageTier::Disk);

let round_trip: RouterEvent =
rmp_serde::from_slice(&rmp_serde::to_vec_named(&explicit_worker).unwrap()).unwrap();
assert_eq!(round_trip.session_id.as_deref(), Some("session-1"));

let mixed = vec![
ExplicitDomainEvent {
worker_id: 7,
Expand Down
13 changes: 11 additions & 2 deletions lib/kv-router/src/zmq_wire/convert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ pub fn convert_event(
kv_cache_spec_sliding_window: _,
locality: _,
ownership: _,
session_id,
} => {
// Reject self-referencing blocks: all block hashes (including parent) must be unique.
{
Expand Down Expand Up @@ -116,7 +117,7 @@ pub fn convert_event(
.into_iter()
.map(BlockHashValue::into_u64)
.collect();
KvCacheEvent {
let event = KvCacheEvent {
event_id,
data: KvCacheEventData::Stored(KvCacheStoreData {
parent_hash: parent_block_hash
Expand All @@ -138,7 +139,15 @@ pub fn convert_event(
),
}),
dp_rank,
}
};
let placement_event = PlacementEvent::new(
Placement::local_worker(worker.worker_id, worker.dp_rank, storage_tier),
event,
);
return Some(match session_id {
Some(session_id) => placement_event.with_session_id(session_id),
None => placement_event,
});
}
RawKvEvent::BlockRemoved { block_hashes, .. } => {
let hashes = block_hashes
Expand Down
8 changes: 8 additions & 0 deletions lib/kv-router/src/zmq_wire/deserialize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ impl<'de> Visitor<'de> for RawKvEventVisitor {
let mut block_mm_infos: Option<Option<Vec<Option<BlockExtraInfo>>>> = None;
let mut locality: Option<Option<Locality>> = None;
let mut ownership: Option<Option<String>> = None;
let mut session_id: Option<Option<String>> = None;
let mut metadata = KvCacheEventMetadata::default();

while let Some(key) = map.next_key::<String>()? {
Expand Down Expand Up @@ -105,6 +106,9 @@ impl<'de> Visitor<'de> for RawKvEventVisitor {
"ownership" => {
ownership = Some(map.next_value()?);
}
"session_id" => {
session_id = Some(map.next_value()?);
}
_ => {
map.next_value::<IgnoredAny>()?;
}
Expand Down Expand Up @@ -143,6 +147,7 @@ impl<'de> Visitor<'de> for RawKvEventVisitor {
kv_cache_spec_sliding_window: metadata.kv_cache_spec_sliding_window,
locality: locality.unwrap_or(None),
ownership: ownership.unwrap_or(None),
session_id: session_id.unwrap_or(None),
})
}
Some("BlockRemoved") => {
Expand Down Expand Up @@ -237,6 +242,9 @@ impl<'de> Visitor<'de> for RawKvEventVisitor {
kv_cache_spec_sliding_window: parsed.metadata.kv_cache_spec_sliding_window,
locality: parsed.locality,
ownership: parsed.ownership,
// vLLM emits session_id in its named-map format. Legacy
// positional layouts have no unambiguous session slot.
session_id: None,
})
}
"BlockRemoved" => {
Expand Down
Loading
Loading