Skip to main content

heliosdb_proxy/distribcache/
config.rs

1//! DistribCache configuration types
2//!
3//! Configuration for multi-tier distributed caching with workload-specific settings.
4
5use std::net::SocketAddr;
6use std::path::PathBuf;
7use std::time::Duration;
8
9use super::tiers::{EvictionPolicy, CompressionType};
10
11/// Main configuration for Helios-DistribCache
12#[derive(Debug, Clone)]
13pub struct DistribCacheConfig {
14    /// Whether caching is enabled
15    pub enabled: bool,
16
17    // L1 (Hot Cache) configuration
18    /// L1 cache size in megabytes
19    pub l1_size_mb: usize,
20    /// Maximum entry size in L1
21    pub l1_max_entry_size: usize,
22    /// L1 eviction policy
23    pub l1_eviction_policy: EvictionPolicy,
24
25    // L2 (Warm Cache) configuration
26    /// Whether L2 is enabled
27    pub l2_enabled: bool,
28    /// L2 cache size in gigabytes
29    pub l2_size_gb: u64,
30    /// L2 storage path
31    pub l2_path: PathBuf,
32    /// L2 compression type
33    pub l2_compression: CompressionType,
34
35    // L3 (Distributed Cache) configuration
36    /// Whether L3 is enabled
37    pub l3_enabled: bool,
38    /// Replication factor for distributed cache
39    pub l3_replication_factor: u32,
40    /// Peer addresses for distributed cache mesh
41    pub l3_peers: Vec<SocketAddr>,
42
43    // Workload-specific TTLs
44    /// OLTP query cache TTL
45    pub oltp_cache_ttl: Duration,
46    /// OLAP query cache TTL
47    pub olap_cache_ttl: Duration,
48    /// Vector search cache TTL
49    pub vector_cache_ttl: Duration,
50    /// AI agent cache TTL
51    pub ai_agent_cache_ttl: Duration,
52    /// RAG pipeline cache TTL
53    pub rag_cache_ttl: Duration,
54    /// Default cache TTL
55    pub default_cache_ttl: Duration,
56
57    // Prefetching
58    /// Whether prefetching is enabled
59    pub prefetch_enabled: bool,
60    /// Number of queries to look ahead for prefetching
61    pub prefetch_lookahead: u32,
62    /// Confidence threshold for prefetch predictions
63    pub prefetch_confidence_threshold: f32,
64    /// Maximum prefetch queue size
65    pub max_prefetch_queue: usize,
66
67    // Invalidation
68    /// Invalidation mode
69    pub invalidation_mode: InvalidationMode,
70    /// WAL endpoint for streaming invalidation
71    pub wal_endpoint: Option<String>,
72    /// WAL lag tolerance
73    pub wal_lag_tolerance: Duration,
74
75    // Scheduling
76    /// Workload scheduling policy
77    pub scheduling_policy: SchedulingPolicy,
78    /// OLTP priority weight
79    pub oltp_priority: f64,
80    /// OLAP priority weight
81    pub olap_priority: f64,
82    /// Vector priority weight
83    pub vector_priority: f64,
84    /// AI agent priority weight
85    pub ai_agent_priority: f64,
86    /// Maximum concurrent queries per workload type
87    pub max_concurrent_oltp: u32,
88    pub max_concurrent_olap: u32,
89    pub max_concurrent_vector: u32,
90    pub max_concurrent_ai: u32,
91
92    // Heatmap
93    /// Whether heatmap analytics is enabled
94    pub heatmap_enabled: bool,
95    /// Heatmap time bucket size
96    pub heatmap_bucket_size: Duration,
97    /// Heatmap data retention
98    pub heatmap_retention: Duration,
99
100    // AI-specific settings
101    /// Maximum conversation turns to cache
102    pub max_conversation_turns: usize,
103    /// Semantic similarity threshold
104    pub semantic_similarity_threshold: f32,
105}
106
107impl Default for DistribCacheConfig {
108    fn default() -> Self {
109        Self {
110            enabled: true,
111
112            // L1 defaults
113            l1_size_mb: 256,
114            l1_max_entry_size: 1024 * 1024, // 1MB
115            l1_eviction_policy: EvictionPolicy::LFU,
116
117            // L2 defaults
118            l2_enabled: true,
119            l2_size_gb: 5,
120            l2_path: PathBuf::from("/var/lib/heliosproxy/cache"),
121            l2_compression: CompressionType::Lz4,
122
123            // L3 defaults
124            l3_enabled: false,
125            l3_replication_factor: 2,
126            l3_peers: Vec::new(),
127
128            // TTL defaults
129            oltp_cache_ttl: Duration::from_secs(60),
130            olap_cache_ttl: Duration::from_secs(30 * 60),
131            vector_cache_ttl: Duration::from_secs(5 * 60),
132            ai_agent_cache_ttl: Duration::from_secs(10 * 60),
133            rag_cache_ttl: Duration::from_secs(15 * 60),
134            default_cache_ttl: Duration::from_secs(5 * 60),
135
136            // Prefetch defaults
137            prefetch_enabled: true,
138            prefetch_lookahead: 3,
139            prefetch_confidence_threshold: 0.3,
140            max_prefetch_queue: 100,
141
142            // Invalidation defaults
143            invalidation_mode: InvalidationMode::Hybrid,
144            wal_endpoint: None,
145            wal_lag_tolerance: Duration::from_millis(100),
146
147            // Scheduling defaults
148            scheduling_policy: SchedulingPolicy::WeightedFair,
149            oltp_priority: 1.0,
150            olap_priority: 0.3,
151            vector_priority: 0.5,
152            ai_agent_priority: 0.7,
153            max_concurrent_oltp: 500,
154            max_concurrent_olap: 50,
155            max_concurrent_vector: 100,
156            max_concurrent_ai: 200,
157
158            // Heatmap defaults
159            heatmap_enabled: true,
160            heatmap_bucket_size: Duration::from_secs(5 * 60),
161            heatmap_retention: Duration::from_secs(7 * 24 * 60 * 60),
162
163            // AI defaults
164            max_conversation_turns: 50,
165            semantic_similarity_threshold: 0.85,
166        }
167    }
168}
169
170impl DistribCacheConfig {
171    /// Create a new configuration builder
172    pub fn builder() -> DistribCacheConfigBuilder {
173        DistribCacheConfigBuilder::default()
174    }
175}
176
177/// Invalidation mode
178#[derive(Debug, Clone, Copy, PartialEq, Eq)]
179pub enum InvalidationMode {
180    /// TTL-based expiration only
181    TTL,
182    /// WAL-based invalidation only
183    WAL,
184    /// Both TTL and WAL
185    Hybrid,
186}
187
188/// Workload scheduling policy
189#[derive(Debug, Clone, Copy, PartialEq, Eq)]
190pub enum SchedulingPolicy {
191    /// Strict priority (OLTP always first)
192    StrictPriority,
193    /// Weighted fair queuing
194    WeightedFair,
195    /// Time-based (OLAP during off-hours)
196    TimeBased,
197    /// Adaptive (learn optimal distribution)
198    Adaptive,
199}
200
201/// Builder for DistribCacheConfig
202#[derive(Debug, Default)]
203pub struct DistribCacheConfigBuilder {
204    config: DistribCacheConfig,
205}
206
207impl DistribCacheConfigBuilder {
208    /// Set whether caching is enabled
209    pub fn enabled(mut self, enabled: bool) -> Self {
210        self.config.enabled = enabled;
211        self
212    }
213
214    /// Set L1 cache size in MB
215    pub fn l1_size_mb(mut self, size: usize) -> Self {
216        self.config.l1_size_mb = size;
217        self
218    }
219
220    /// Set L1 maximum entry size
221    pub fn l1_max_entry_size(mut self, size: usize) -> Self {
222        self.config.l1_max_entry_size = size;
223        self
224    }
225
226    /// Set L1 eviction policy
227    pub fn l1_eviction_policy(mut self, policy: EvictionPolicy) -> Self {
228        self.config.l1_eviction_policy = policy;
229        self
230    }
231
232    /// Enable L2 cache
233    pub fn l2_enabled(mut self, enabled: bool) -> Self {
234        self.config.l2_enabled = enabled;
235        self
236    }
237
238    /// Set L2 cache size in GB
239    pub fn l2_size_gb(mut self, size: u64) -> Self {
240        self.config.l2_size_gb = size;
241        self
242    }
243
244    /// Set L2 storage path
245    pub fn l2_path(mut self, path: impl Into<PathBuf>) -> Self {
246        self.config.l2_path = path.into();
247        self
248    }
249
250    /// Set L2 compression type
251    pub fn l2_compression(mut self, compression: CompressionType) -> Self {
252        self.config.l2_compression = compression;
253        self
254    }
255
256    /// Enable L3 distributed cache
257    pub fn l3_enabled(mut self, enabled: bool) -> Self {
258        self.config.l3_enabled = enabled;
259        self
260    }
261
262    /// Set L3 replication factor
263    pub fn l3_replication_factor(mut self, factor: u32) -> Self {
264        self.config.l3_replication_factor = factor;
265        self
266    }
267
268    /// Set L3 peer addresses
269    pub fn l3_peers(mut self, peers: Vec<SocketAddr>) -> Self {
270        self.config.l3_peers = peers;
271        self
272    }
273
274    /// Set OLTP cache TTL
275    pub fn oltp_cache_ttl(mut self, ttl: Duration) -> Self {
276        self.config.oltp_cache_ttl = ttl;
277        self
278    }
279
280    /// Set OLAP cache TTL
281    pub fn olap_cache_ttl(mut self, ttl: Duration) -> Self {
282        self.config.olap_cache_ttl = ttl;
283        self
284    }
285
286    /// Set vector cache TTL
287    pub fn vector_cache_ttl(mut self, ttl: Duration) -> Self {
288        self.config.vector_cache_ttl = ttl;
289        self
290    }
291
292    /// Set AI agent cache TTL
293    pub fn ai_agent_cache_ttl(mut self, ttl: Duration) -> Self {
294        self.config.ai_agent_cache_ttl = ttl;
295        self
296    }
297
298    /// Set RAG cache TTL
299    pub fn rag_cache_ttl(mut self, ttl: Duration) -> Self {
300        self.config.rag_cache_ttl = ttl;
301        self
302    }
303
304    /// Enable prefetching
305    pub fn prefetch_enabled(mut self, enabled: bool) -> Self {
306        self.config.prefetch_enabled = enabled;
307        self
308    }
309
310    /// Set prefetch lookahead
311    pub fn prefetch_lookahead(mut self, lookahead: u32) -> Self {
312        self.config.prefetch_lookahead = lookahead;
313        self
314    }
315
316    /// Set prefetch confidence threshold
317    pub fn prefetch_confidence_threshold(mut self, threshold: f32) -> Self {
318        self.config.prefetch_confidence_threshold = threshold;
319        self
320    }
321
322    /// Set invalidation mode
323    pub fn invalidation_mode(mut self, mode: InvalidationMode) -> Self {
324        self.config.invalidation_mode = mode;
325        self
326    }
327
328    /// Set WAL endpoint
329    pub fn wal_endpoint(mut self, endpoint: impl Into<String>) -> Self {
330        self.config.wal_endpoint = Some(endpoint.into());
331        self
332    }
333
334    /// Set scheduling policy
335    pub fn scheduling_policy(mut self, policy: SchedulingPolicy) -> Self {
336        self.config.scheduling_policy = policy;
337        self
338    }
339
340    /// Enable heatmap analytics
341    pub fn heatmap_enabled(mut self, enabled: bool) -> Self {
342        self.config.heatmap_enabled = enabled;
343        self
344    }
345
346    /// Set maximum conversation turns to cache
347    pub fn max_conversation_turns(mut self, turns: usize) -> Self {
348        self.config.max_conversation_turns = turns;
349        self
350    }
351
352    /// Set semantic similarity threshold
353    pub fn semantic_similarity_threshold(mut self, threshold: f32) -> Self {
354        self.config.semantic_similarity_threshold = threshold;
355        self
356    }
357
358    /// Build the configuration
359    pub fn build(self) -> DistribCacheConfig {
360        self.config
361    }
362}
363
364/// Per-workload resource limits
365#[derive(Debug, Clone)]
366pub struct WorkloadLimits {
367    /// Maximum concurrent queries
368    pub max_concurrent: u32,
369    /// Maximum cache memory allocation (MB)
370    pub max_cache_mb: usize,
371    /// Priority weight (0.0 - 1.0)
372    pub priority_weight: f64,
373    /// Cache TTL
374    pub cache_ttl: Duration,
375}
376
377impl Default for WorkloadLimits {
378    fn default() -> Self {
379        Self {
380            max_concurrent: 100,
381            max_cache_mb: 64,
382            priority_weight: 0.5,
383            cache_ttl: Duration::from_secs(300),
384        }
385    }
386}
387
388#[cfg(test)]
389mod tests {
390    use super::*;
391
392    #[test]
393    fn test_default_config() {
394        let config = DistribCacheConfig::default();
395        assert!(config.enabled);
396        assert_eq!(config.l1_size_mb, 256);
397        assert!(config.l2_enabled);
398        assert!(!config.l3_enabled);
399        assert!(config.prefetch_enabled);
400        assert!(config.heatmap_enabled);
401    }
402
403    #[test]
404    fn test_config_builder() {
405        let config = DistribCacheConfig::builder()
406            .l1_size_mb(512)
407            .l2_enabled(false)
408            .l3_enabled(true)
409            .l3_replication_factor(3)
410            .prefetch_enabled(false)
411            .build();
412
413        assert_eq!(config.l1_size_mb, 512);
414        assert!(!config.l2_enabled);
415        assert!(config.l3_enabled);
416        assert_eq!(config.l3_replication_factor, 3);
417        assert!(!config.prefetch_enabled);
418    }
419
420    #[test]
421    fn test_workload_limits_default() {
422        let limits = WorkloadLimits::default();
423        assert_eq!(limits.max_concurrent, 100);
424        assert_eq!(limits.priority_weight, 0.5);
425    }
426}