use dashmap::DashMap;
use dynamo_kv_router::protocols::WorkerWithDpRank;
use std::sync::Arc;
use std::time::Instant;
#[derive(Debug, Clone)]
pub struct LoraReplicaConfig {
pub lora_name: String,
pub replica_factor: usize,
pub replica_set: Vec<WorkerWithDpRank>,
pub updated_at: Instant,
pub is_active: bool,
}
#[derive(Clone)]
pub struct LoraRoutingTable {
allocations: Arc<DashMap<String, LoraReplicaConfig>>,
}
impl LoraRoutingTable {
pub fn new() -> Self {
Self {
allocations: Arc::new(DashMap::new()),
}
}
pub fn get_replica_set(&self, lora_name: &str) -> Option<Vec<WorkerWithDpRank>> {
self.allocations
.get(lora_name)
.map(|entry| entry.replica_set.clone())
}
pub fn get_config(&self, lora_name: &str) -> Option<LoraReplicaConfig> {
self.allocations.get(lora_name).map(|entry| entry.clone())
}
pub fn update_allocation(&self, lora_name: String, config: LoraReplicaConfig) {
self.allocations.insert(lora_name, config);
}
pub fn remove_lora(&self, lora_name: &str) -> Option<LoraReplicaConfig> {
self.allocations.remove(lora_name).map(|(_, v)| v)
}
pub fn list_loras(&self) -> Vec<String> {
self.allocations
.iter()
.map(|entry| entry.key().clone())
.collect()
}
pub fn snapshot_configs(&self) -> Vec<(String, LoraReplicaConfig)> {
self.allocations
.iter()
.map(|entry| (entry.key().clone(), entry.value().clone()))
.collect()
}
pub fn len(&self) -> usize {
self.allocations.len()
}
pub fn is_empty(&self) -> bool {
self.allocations.is_empty()
}
pub fn clear(&self) {
self.allocations.clear();
}
}
impl Default for LoraRoutingTable {
fn default() -> Self {
Self::new()
}
}