#![cfg_attr(coverage_nightly, coverage(off))]
use crate::services::cache::advanced_strategies::{CacheTier, EvictionPolicy};
use anyhow::Result;
use parking_lot::RwLock;
use rustc_hash::FxHashMap;
use serde::{Deserialize, Serialize};
use std::sync::atomic::{AtomicU64, Ordering};
use std::time::{Duration, Instant};
use tracing::{debug, info, warn};
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct WorkloadProfile {
pub request_rate: f64,
pub working_set_size: u64,
pub temporal_locality: f64,
pub spatial_locality: f64,
pub read_write_ratio: f64,
pub target_hit_rate: f64,
pub latency_sensitivity: f64,
}
#[derive(Debug, Clone)]
pub struct StrategyRecommendation {
pub eviction_policy: EvictionPolicy,
pub tier_config: TierConfiguration,
pub expected_improvement: f64,
pub confidence: f64,
}
#[derive(Debug, Clone)]
pub struct TierConfiguration {
pub tier_allocations: FxHashMap<CacheTier, u64>,
pub enabled_tiers: FxHashMap<CacheTier, bool>,
pub promotion_thresholds: FxHashMap<CacheTier, f64>,
}
#[derive(Debug, Clone, Default)]
pub struct PerformanceMetrics {
pub hit_rate: f64,
pub avg_latency: Duration,
pub memory_utilization: f64,
pub throughput: f64,
pub effectiveness_score: f64,
}
pub struct CacheOrchestrator {
workload_profile: RwLock<WorkloadProfile>,
strategies: RwLock<FxHashMap<String, Box<dyn CacheStrategy + Send + Sync>>>,
metrics: RwLock<PerformanceMetrics>,
evaluation_history: RwLock<Vec<StrategyEvaluation>>,
config: OrchestratorConfig,
counters: PerformanceCounters,
}
#[derive(Debug, Clone)]
pub struct OrchestratorConfig {
pub auto_strategy_switching: bool,
pub evaluation_interval: Duration,
pub min_improvement_threshold: f64,
pub evaluation_window: usize,
pub enable_prediction: bool,
}
#[derive(Debug, Clone)]
#[allow(dead_code)]
struct StrategyEvaluation {
performance: PerformanceMetrics,
timestamp: Instant,
}
#[cfg(test)]
impl StrategyEvaluation {
pub(crate) fn score(&self) -> f64 {
self.performance.hit_rate * self.performance.throughput
}
pub(crate) fn is_valid(&self) -> bool {
self.timestamp.elapsed().as_secs() < 3600
}
}
#[derive(Debug)]
struct PerformanceCounters {
strategy_switches: AtomicU64,
evaluations_performed: AtomicU64,
recommendations_generated: AtomicU64,
performance_improvements: AtomicU64,
}
pub trait CacheStrategy {
fn strategy_id(&self) -> &str;
fn get_stats(&self) -> PerformanceMetrics;
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct StrategyConfig {
pub parameters: FxHashMap<String, serde_json::Value>,
pub resource_limits: ResourceLimits,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ResourceLimits {
pub max_memory: u64,
pub max_disk: u64,
pub max_cpu: f64,
}
include!("orchestrator_impls.rs");
include!("orchestrator_analysis.rs");
include!("orchestrator_tests.rs");