use async_trait::async_trait;
use std::sync::Arc;
use super::{AnchorRef, AnchorTask, KvIndexerMetrics, KvRouterError, WorkerTask};
use crate::protocols::*;
#[async_trait]
pub trait SharedKvCache: Send + Sync {
async fn check_blocks(
&self,
tokens: &[u32],
block_size: u32,
) -> Result<SharedCacheHits, KvRouterError>;
}
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct ShardSizeSnapshot {
pub shard_idx: usize,
pub worker_count: usize,
pub block_count: usize,
pub node_count: usize,
}
#[async_trait]
pub trait KvIndexerInterface {
async fn find_matches(
&self,
sequence: Vec<LocalBlockHash>,
) -> Result<OverlapScores, KvRouterError>;
async fn find_matches_for_request(
&self,
tokens: &[u32],
lora_name: Option<&str>,
is_eagle: Option<bool>,
) -> Result<OverlapScores, KvRouterError>;
async fn apply_event(&self, event: RouterEvent);
async fn remove_worker(&self, worker: WorkerId);
async fn remove_worker_dp_rank(&self, worker: WorkerId, _dp_rank: DpRank) {
self.remove_worker(worker).await;
}
fn shutdown(&self);
async fn dump_events(&self) -> Result<Vec<RouterEvent>, KvRouterError>;
async fn process_routing_decision_for_request(
&self,
tokens_with_hashes: &mut TokensWithHashes,
worker: WorkerWithDpRank,
) -> Result<(), KvRouterError>;
async fn flush(&self) -> usize;
fn timing_report(&self) -> String {
String::new()
}
async fn shard_sizes(&self) -> Vec<ShardSizeSnapshot> {
vec![]
}
fn node_edge_lengths(&self) -> Vec<usize> {
vec![]
}
}
pub trait SyncIndexer: Send + Sync + 'static {
fn worker(
&self,
event_receiver: flume::Receiver<WorkerTask>,
metrics: Option<Arc<KvIndexerMetrics>>,
) -> anyhow::Result<()>;
fn find_matches(&self, sequence: &[LocalBlockHash], early_exit: bool) -> OverlapScores;
fn apply_anchor(
&self,
_worker: WorkerWithDpRank,
_anchor: AnchorTask,
) -> Result<(), KvCacheEventError> {
Err(KvCacheEventError::InvalidBlockSequence)
}
fn find_matches_from_anchor(
&self,
_anchor: AnchorRef,
_suffix: &[LocalBlockHash],
) -> Result<OverlapScores, KvRouterError> {
Err(KvRouterError::Unsupported(
"backend does not support anchored find_matches".to_string(),
))
}
fn try_schedule_cleanup(&self) -> bool {
false
}
fn cancel_scheduled_cleanup(&self) {}
fn run_cleanup_task(&self) {}
fn dump_events(&self) -> Option<Vec<RouterEvent>> {
None
}
fn timing_report(&self) -> String {
String::new()
}
fn node_count(&self) -> usize {
0
}
fn node_edge_lengths(&self) -> Vec<usize> {
vec![]
}
}
pub trait AnchorCapableSyncIndexer: SyncIndexer {}