Repository navigation
fix(kv-router): scope KvRouter cancellation to a child token #11455
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -20,8 +20,9 @@ use dynamo_kv_router::{ | |
| pub(crate) use dynamo_kv_router::indexer::TieredMatchDetails; | ||
| #[allow(unused_imports)] | ||
| pub(crate) use dynamo_kv_router::indexer::WireTieredMatchDetails; | ||
| use dynamo_runtime::{component::Component, traits::DistributedRuntimeProvider}; | ||
| use dynamo_runtime::component::Component; | ||
| use tokio::sync::oneshot; | ||
| use tokio_util::sync::CancellationToken; | ||
|
|
||
| mod embedding_cache; | ||
| mod lookup; | ||
|
|
@@ -77,6 +78,7 @@ impl Indexer { | |
| kv_router_config: &KvRouterConfig, | ||
| block_size: u32, | ||
| model_name: Option<&str>, | ||
| cancellation_token: CancellationToken, | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Adding 🤖 AI FixIn |
||
| ) -> Result<Self> { | ||
| if kv_router_config.overlap_score_credit == 0.0 { | ||
| return Ok(Self::None); | ||
|
|
@@ -103,7 +105,12 @@ impl Indexer { | |
| ); | ||
| let remote = | ||
| RemoteIndexer::new(component, model_name, kv_router_config.use_kv_events).await?; | ||
| let approx = SideIndexer::new_predict_on_route(component, kv_router_config, block_size); | ||
| let approx = SideIndexer::new_predict_on_route( | ||
| component, | ||
| kv_router_config, | ||
| block_size, | ||
| cancellation_token.child_token(), | ||
| ); | ||
| return Ok(Self::Remote { | ||
| primary: Arc::new(remote), | ||
| approx, | ||
|
|
@@ -135,10 +142,9 @@ impl Indexer { | |
| }); | ||
| } | ||
|
|
||
| let cancellation_token = component.drt().primary_token(); | ||
| return Ok(Self::KvIndexer { | ||
| primary: KvIndexer::new_with_pruning( | ||
| cancellation_token, | ||
| cancellation_token.child_token(), | ||
| block_size, | ||
| kv_indexer_metrics.clone(), | ||
| prune_config, | ||
|
|
@@ -153,7 +159,12 @@ impl Indexer { | |
| }); | ||
| } | ||
|
|
||
| let approx = SideIndexer::new_predict_on_route(component, kv_router_config, block_size); | ||
| let approx = SideIndexer::new_predict_on_route( | ||
| component, | ||
| kv_router_config, | ||
| block_size, | ||
| cancellation_token.child_token(), | ||
| ); | ||
|
|
||
| if kv_router_config.router_event_threads > 1 { | ||
| let kv_indexer_metrics = KvIndexerMetrics::from_component(component); | ||
|
|
@@ -175,11 +186,9 @@ impl Indexer { | |
| } | ||
|
|
||
| let kv_indexer_metrics = KvIndexerMetrics::from_component(component); | ||
| let cancellation_token = component.drt().primary_token(); | ||
|
|
||
| Ok(Self::KvIndexer { | ||
| primary: KvIndexer::new_with_pruning( | ||
| cancellation_token, | ||
| cancellation_token.child_token(), | ||
| block_size, | ||
| kv_indexer_metrics.clone(), | ||
| None, | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -30,6 +30,7 @@ use dynamo_tokens::SequenceHash; | |
| use std::collections::{HashMap, HashSet}; | ||
| use std::sync::Arc; | ||
| use std::time::Duration; | ||
| use tokio_util::sync::CancellationToken; | ||
|
|
||
| pub struct KvScheduler<Sel = DefaultWorkerSelector, RF = NoopOverlapScoresRefresh> | ||
| where | ||
|
|
@@ -60,6 +61,7 @@ where | |
| overloaded_worker_provider: Option<OverloadedWorkerProvider>, | ||
| model_name: Option<&str>, | ||
| worker_type: &'static str, | ||
| cancellation_token: CancellationToken, | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Adding 🤖 AI FixIn |
||
| ) -> Result<Self, KvSchedulerError> { | ||
| let initial_workers: HashMap<WorkerId, ModelRuntimeConfig> = | ||
| workers_with_configs.borrow().clone(); | ||
|
|
@@ -72,6 +74,7 @@ where | |
| kv_router_config.router_replica_sync, | ||
| router_id, | ||
| worker_type, | ||
| cancellation_token.child_token(), | ||
| ) | ||
| .await | ||
| .map_err(|e| KvSchedulerError::InitFailed(e.to_string()))?; | ||
|
|
@@ -107,14 +110,14 @@ where | |
| overloaded_worker_provider, | ||
| kv_router_config.router_queue_recheck_interval(), | ||
| kv_router_config.router_track_prefill_tokens, | ||
| component.drt().child_token(), | ||
| cancellation_token.child_token(), | ||
| worker_type, | ||
| watch_worker_configs, | ||
| )); | ||
|
|
||
| let metrics_scheduler = Arc::clone(&inner); | ||
| let background_metrics = queue_metrics.clone(); | ||
| let metrics_cancel_token = component.drt().child_token(); | ||
| let metrics_cancel_token = cancellation_token.child_token(); | ||
| let mut queue_updates = inner.subscribe_queue_updates(); | ||
| tokio::spawn(async move { | ||
| let mut recheck_interval = tokio::time::interval(Duration::from_secs(60)); | ||
|
|
@@ -471,6 +474,7 @@ mod tests { | |
| router_track_active_blocks: false, | ||
| ..Default::default() | ||
| }; | ||
| let cancellation_token = CancellationToken::new(); | ||
|
|
||
| let scheduler = KvScheduler::start( | ||
| component.clone(), | ||
|
|
@@ -483,6 +487,7 @@ mod tests { | |
| None, | ||
| Some("test-model"), | ||
| "decode", | ||
| cancellation_token.clone(), | ||
| ) | ||
| .await | ||
| .unwrap(); | ||
|
|
@@ -514,6 +519,6 @@ mod tests { | |
| .await | ||
| .unwrap(); | ||
|
|
||
| component.drt().primary_token().cancel(); | ||
| cancellation_token.cancel(); | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When
Runtime::shutdown()is called while a KV-routed endpoint still has in-flight or queued requests, this token is cancelled in shutdown Phase 1 becauseRuntime::child_token()is a child ofendpoint_shutdown_token, and Phase 2 only waits for graceful endpoints afterward (lib/runtime/src/runtime.rs). That tears down the scheduler/indexer/sequence tasks before the active requests finish, regressing graceful shutdown behavior; the mocker explicitly avoids this by deriving fromprimary_token()so it only stops in Phase 3. Use an independently cancellable child of the primary token (or equivalent) soDropcan stop this router without cancelling the runtime, while runtime shutdown keeps router support alive until Phase 3.Useful? React with 👍 / 👎.