heliosdb_proxy/distribcache/
config.rs1use std::net::SocketAddr;
6use std::path::PathBuf;
7use std::time::Duration;
8
9use super::tiers::{EvictionPolicy, CompressionType};
10
11#[derive(Debug, Clone)]
13pub struct DistribCacheConfig {
14 pub enabled: bool,
16
17 pub l1_size_mb: usize,
20 pub l1_max_entry_size: usize,
22 pub l1_eviction_policy: EvictionPolicy,
24
25 pub l2_enabled: bool,
28 pub l2_size_gb: u64,
30 pub l2_path: PathBuf,
32 pub l2_compression: CompressionType,
34
35 pub l3_enabled: bool,
38 pub l3_replication_factor: u32,
40 pub l3_peers: Vec<SocketAddr>,
42
43 pub oltp_cache_ttl: Duration,
46 pub olap_cache_ttl: Duration,
48 pub vector_cache_ttl: Duration,
50 pub ai_agent_cache_ttl: Duration,
52 pub rag_cache_ttl: Duration,
54 pub default_cache_ttl: Duration,
56
57 pub prefetch_enabled: bool,
60 pub prefetch_lookahead: u32,
62 pub prefetch_confidence_threshold: f32,
64 pub max_prefetch_queue: usize,
66
67 pub invalidation_mode: InvalidationMode,
70 pub wal_endpoint: Option<String>,
72 pub wal_lag_tolerance: Duration,
74
75 pub scheduling_policy: SchedulingPolicy,
78 pub oltp_priority: f64,
80 pub olap_priority: f64,
82 pub vector_priority: f64,
84 pub ai_agent_priority: f64,
86 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 pub heatmap_enabled: bool,
95 pub heatmap_bucket_size: Duration,
97 pub heatmap_retention: Duration,
99
100 pub max_conversation_turns: usize,
103 pub semantic_similarity_threshold: f32,
105}
106
107impl Default for DistribCacheConfig {
108 fn default() -> Self {
109 Self {
110 enabled: true,
111
112 l1_size_mb: 256,
114 l1_max_entry_size: 1024 * 1024, l1_eviction_policy: EvictionPolicy::LFU,
116
117 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_enabled: false,
125 l3_replication_factor: 2,
126 l3_peers: Vec::new(),
127
128 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_enabled: true,
138 prefetch_lookahead: 3,
139 prefetch_confidence_threshold: 0.3,
140 max_prefetch_queue: 100,
141
142 invalidation_mode: InvalidationMode::Hybrid,
144 wal_endpoint: None,
145 wal_lag_tolerance: Duration::from_millis(100),
146
147 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_enabled: true,
160 heatmap_bucket_size: Duration::from_secs(5 * 60),
161 heatmap_retention: Duration::from_secs(7 * 24 * 60 * 60),
162
163 max_conversation_turns: 50,
165 semantic_similarity_threshold: 0.85,
166 }
167 }
168}
169
170impl DistribCacheConfig {
171 pub fn builder() -> DistribCacheConfigBuilder {
173 DistribCacheConfigBuilder::default()
174 }
175}
176
177#[derive(Debug, Clone, Copy, PartialEq, Eq)]
179pub enum InvalidationMode {
180 TTL,
182 WAL,
184 Hybrid,
186}
187
188#[derive(Debug, Clone, Copy, PartialEq, Eq)]
190pub enum SchedulingPolicy {
191 StrictPriority,
193 WeightedFair,
195 TimeBased,
197 Adaptive,
199}
200
201#[derive(Debug, Default)]
203pub struct DistribCacheConfigBuilder {
204 config: DistribCacheConfig,
205}
206
207impl DistribCacheConfigBuilder {
208 pub fn enabled(mut self, enabled: bool) -> Self {
210 self.config.enabled = enabled;
211 self
212 }
213
214 pub fn l1_size_mb(mut self, size: usize) -> Self {
216 self.config.l1_size_mb = size;
217 self
218 }
219
220 pub fn l1_max_entry_size(mut self, size: usize) -> Self {
222 self.config.l1_max_entry_size = size;
223 self
224 }
225
226 pub fn l1_eviction_policy(mut self, policy: EvictionPolicy) -> Self {
228 self.config.l1_eviction_policy = policy;
229 self
230 }
231
232 pub fn l2_enabled(mut self, enabled: bool) -> Self {
234 self.config.l2_enabled = enabled;
235 self
236 }
237
238 pub fn l2_size_gb(mut self, size: u64) -> Self {
240 self.config.l2_size_gb = size;
241 self
242 }
243
244 pub fn l2_path(mut self, path: impl Into<PathBuf>) -> Self {
246 self.config.l2_path = path.into();
247 self
248 }
249
250 pub fn l2_compression(mut self, compression: CompressionType) -> Self {
252 self.config.l2_compression = compression;
253 self
254 }
255
256 pub fn l3_enabled(mut self, enabled: bool) -> Self {
258 self.config.l3_enabled = enabled;
259 self
260 }
261
262 pub fn l3_replication_factor(mut self, factor: u32) -> Self {
264 self.config.l3_replication_factor = factor;
265 self
266 }
267
268 pub fn l3_peers(mut self, peers: Vec<SocketAddr>) -> Self {
270 self.config.l3_peers = peers;
271 self
272 }
273
274 pub fn oltp_cache_ttl(mut self, ttl: Duration) -> Self {
276 self.config.oltp_cache_ttl = ttl;
277 self
278 }
279
280 pub fn olap_cache_ttl(mut self, ttl: Duration) -> Self {
282 self.config.olap_cache_ttl = ttl;
283 self
284 }
285
286 pub fn vector_cache_ttl(mut self, ttl: Duration) -> Self {
288 self.config.vector_cache_ttl = ttl;
289 self
290 }
291
292 pub fn ai_agent_cache_ttl(mut self, ttl: Duration) -> Self {
294 self.config.ai_agent_cache_ttl = ttl;
295 self
296 }
297
298 pub fn rag_cache_ttl(mut self, ttl: Duration) -> Self {
300 self.config.rag_cache_ttl = ttl;
301 self
302 }
303
304 pub fn prefetch_enabled(mut self, enabled: bool) -> Self {
306 self.config.prefetch_enabled = enabled;
307 self
308 }
309
310 pub fn prefetch_lookahead(mut self, lookahead: u32) -> Self {
312 self.config.prefetch_lookahead = lookahead;
313 self
314 }
315
316 pub fn prefetch_confidence_threshold(mut self, threshold: f32) -> Self {
318 self.config.prefetch_confidence_threshold = threshold;
319 self
320 }
321
322 pub fn invalidation_mode(mut self, mode: InvalidationMode) -> Self {
324 self.config.invalidation_mode = mode;
325 self
326 }
327
328 pub fn wal_endpoint(mut self, endpoint: impl Into<String>) -> Self {
330 self.config.wal_endpoint = Some(endpoint.into());
331 self
332 }
333
334 pub fn scheduling_policy(mut self, policy: SchedulingPolicy) -> Self {
336 self.config.scheduling_policy = policy;
337 self
338 }
339
340 pub fn heatmap_enabled(mut self, enabled: bool) -> Self {
342 self.config.heatmap_enabled = enabled;
343 self
344 }
345
346 pub fn max_conversation_turns(mut self, turns: usize) -> Self {
348 self.config.max_conversation_turns = turns;
349 self
350 }
351
352 pub fn semantic_similarity_threshold(mut self, threshold: f32) -> Self {
354 self.config.semantic_similarity_threshold = threshold;
355 self
356 }
357
358 pub fn build(self) -> DistribCacheConfig {
360 self.config
361 }
362}
363
364#[derive(Debug, Clone)]
366pub struct WorkloadLimits {
367 pub max_concurrent: u32,
369 pub max_cache_mb: usize,
371 pub priority_weight: f64,
373 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}