sockudo-cache 4.0.0

Cache manager implementations for Sockudo
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
use crate::memory_cache_manager::MemoryCacheManager;
use async_trait::async_trait;
use sockudo_core::cache::CacheManager;
use sockudo_core::error::Result;
use sockudo_core::options::MemoryCacheOptions;
use std::sync::atomic::{AtomicBool, AtomicU64, Ordering};
use std::time::{Duration, Instant};
use tokio::sync::{Mutex, RwLock};
use tracing::{debug, info, warn};

const RECOVERY_CHECK_INTERVAL_SECS: u64 = 30;

/// Cache manager that wraps a primary (Redis/Redis Cluster) cache and automatically
/// falls back to in-memory cache when the primary becomes unavailable.
///
/// When the primary cache recovers, data from the fallback cache is synchronized
/// back to the primary before switching over to prevent data loss.
pub struct FallbackCacheManager {
    primary: Mutex<Box<dyn CacheManager + Send + Sync>>,
    fallback: Mutex<MemoryCacheManager>,
    using_fallback: AtomicBool,
    last_failure_time: AtomicU64,
    start_time: Instant,
    /// Guards state transitions to prevent race conditions during recovery
    recovery_lock: RwLock<()>,
}

impl FallbackCacheManager {
    /// Creates a new FallbackCacheManager without performing an initial health check.
    /// If Redis is unavailable at startup, the first cache operation will experience
    /// higher latency due to the failed attempt and retry.
    pub fn new(
        primary: Box<dyn CacheManager + Send + Sync>,
        fallback_options: MemoryCacheOptions,
    ) -> Self {
        let fallback = MemoryCacheManager::new("fallback_cache".to_string(), fallback_options);

        Self {
            primary: Mutex::new(primary),
            fallback: Mutex::new(fallback),
            using_fallback: AtomicBool::new(false),
            last_failure_time: AtomicU64::new(0),
            start_time: Instant::now(),
            recovery_lock: RwLock::new(()),
        }
    }

    /// Creates a new FallbackCacheManager and performs an initial health check on the
    /// primary cache. If the primary cache is unavailable at startup, immediately switches
    /// to fallback mode, avoiding initial latency on the first cache operation.
    pub async fn new_with_health_check(
        primary: Box<dyn CacheManager + Send + Sync>,
        fallback_options: MemoryCacheOptions,
    ) -> Self {
        let fallback = MemoryCacheManager::new("fallback_cache".to_string(), fallback_options);
        let start_time = Instant::now();

        let using_fallback = match primary.check_health().await {
            Ok(()) => {
                debug!("Primary cache is healthy at startup");
                false
            }
            Err(e) => {
                warn!(
                    "Primary cache unavailable at startup, starting in fallback mode. Error: {}",
                    e
                );
                true
            }
        };

        let last_failure_time = if using_fallback {
            start_time.elapsed().as_secs()
        } else {
            0
        };

        Self {
            primary: Mutex::new(primary),
            fallback: Mutex::new(fallback),
            using_fallback: AtomicBool::new(using_fallback),
            last_failure_time: AtomicU64::new(last_failure_time),
            start_time,
            recovery_lock: RwLock::new(()),
        }
    }

    fn is_using_fallback(&self) -> bool {
        self.using_fallback.load(Ordering::SeqCst)
    }

    fn switch_to_fallback(&self, error: &str) {
        if !self.using_fallback.swap(true, Ordering::SeqCst) {
            warn!(
                "Redis cache unavailable, switching to in-memory fallback. Error: {}",
                error
            );
            self.last_failure_time
                .store(self.start_time.elapsed().as_secs(), Ordering::SeqCst);
        }
    }

    fn should_attempt_recovery(&self) -> bool {
        if !self.is_using_fallback() {
            return false;
        }

        let last_failure = self.last_failure_time.load(Ordering::SeqCst);
        let current_time = self.start_time.elapsed().as_secs();

        current_time.saturating_sub(last_failure) >= RECOVERY_CHECK_INTERVAL_SECS
    }

    async fn try_recover(&self) -> bool {
        if !self.should_attempt_recovery() {
            return false;
        }

        // Acquire write lock to prevent concurrent recovery attempts and operations
        let _recovery_guard = self.recovery_lock.write().await;

        // Double-check after acquiring lock - another thread may have already recovered
        if !self.is_using_fallback() {
            return false;
        }

        debug!("Attempting to recover Redis cache connection...");

        let mut primary = self.primary.lock().await;
        match primary.check_health().await {
            Ok(()) => {
                info!("Redis cache connection recovered, synchronizing fallback data...");

                if let Err(e) = self.sync_fallback_to_primary(&mut primary).await {
                    warn!(
                        "Failed to sync fallback data to primary during recovery: {}",
                        e
                    );
                }

                self.using_fallback.store(false, Ordering::SeqCst);
                info!("Successfully switched back to primary cache after recovery");
                true
            }
            Err(e) => {
                debug!("Redis cache still unavailable: {}", e);
                self.last_failure_time
                    .store(self.start_time.elapsed().as_secs(), Ordering::SeqCst);
                false
            }
        }
    }

    /// Synchronizes data from fallback cache to primary cache during recovery.
    /// This prevents data loss for entries written during the outage.
    async fn sync_fallback_to_primary(
        &self,
        primary: &mut Box<dyn CacheManager + Send + Sync>,
    ) -> Result<()> {
        let fallback = self.fallback.lock().await;

        let entries = fallback.get_all_entries().await;

        if entries.is_empty() {
            debug!("No entries in fallback cache to sync");
            return Ok(());
        }

        debug!(
            "Syncing {} entries from fallback to primary cache",
            entries.len()
        );

        let mut synced = 0;
        let mut failed = 0;

        for (key, value, ttl) in entries {
            let ttl_seconds = ttl.map(|d| d.as_secs()).unwrap_or(0);
            match primary.set(&key, &value, ttl_seconds).await {
                Ok(()) => synced += 1,
                Err(e) => {
                    warn!("Failed to sync key '{}' to primary cache: {}", key, e);
                    failed += 1;
                }
            }
        }

        if failed > 0 {
            warn!(
                "Synced {}/{} entries from fallback to primary ({} failed)",
                synced,
                synced + failed,
                failed
            );
        } else {
            info!(
                "Successfully synced {} entries from fallback to primary cache",
                synced
            );
        }

        Ok(())
    }
}

#[async_trait]
impl CacheManager for FallbackCacheManager {
    async fn has(&self, key: &str) -> Result<bool> {
        self.try_recover().await;

        let _guard = self.recovery_lock.read().await;

        if self.is_using_fallback() {
            return self.fallback.lock().await.has(key).await;
        }

        let primary = self.primary.lock().await;
        match primary.has(key).await {
            Ok(result) => Ok(result),
            Err(e) => {
                self.switch_to_fallback(&e.to_string());
                self.fallback.lock().await.has(key).await
            }
        }
    }

    async fn get(&self, key: &str) -> Result<Option<String>> {
        self.try_recover().await;

        let _guard = self.recovery_lock.read().await;

        if self.is_using_fallback() {
            return self.fallback.lock().await.get(key).await;
        }

        let primary = self.primary.lock().await;
        match primary.get(key).await {
            Ok(result) => Ok(result),
            Err(e) => {
                self.switch_to_fallback(&e.to_string());
                self.fallback.lock().await.get(key).await
            }
        }
    }

    async fn set(&self, key: &str, value: &str, ttl_seconds: u64) -> Result<()> {
        self.try_recover().await;

        let _guard = self.recovery_lock.read().await;

        if self.is_using_fallback() {
            return self
                .fallback
                .lock()
                .await
                .set(key, value, ttl_seconds)
                .await;
        }

        let primary = self.primary.lock().await;
        match primary.set(key, value, ttl_seconds).await {
            Ok(()) => Ok(()),
            Err(e) => {
                self.switch_to_fallback(&e.to_string());
                self.fallback
                    .lock()
                    .await
                    .set(key, value, ttl_seconds)
                    .await
            }
        }
    }

    async fn remove(&self, key: &str) -> Result<()> {
        self.try_recover().await;

        let _guard = self.recovery_lock.read().await;

        if self.is_using_fallback() {
            return self.fallback.lock().await.remove(key).await;
        }

        let primary = self.primary.lock().await;
        match primary.remove(key).await {
            Ok(()) => Ok(()),
            Err(e) => {
                self.switch_to_fallback(&e.to_string());
                self.fallback.lock().await.remove(key).await
            }
        }
    }

    async fn disconnect(&self) -> Result<()> {
        let primary_result = self.primary.lock().await.disconnect().await;
        if let Err(ref e) = primary_result {
            warn!(error = ?e, "Failed to disconnect primary cache");
        }

        let fallback_result = self.fallback.lock().await.disconnect().await;
        if let Err(ref e) = fallback_result {
            warn!(error = ?e, "Failed to disconnect fallback cache");
        }

        match (primary_result, fallback_result) {
            (Ok(_), Ok(_)) => Ok(()),
            (Err(e), _) => Err(e),
            (_, Err(e)) => Err(e),
        }
    }

    async fn check_health(&self) -> Result<()> {
        if self.is_using_fallback() {
            let fallback = self.fallback.lock().await;
            return fallback.check_health().await;
        }

        let primary = self.primary.lock().await;
        primary.check_health().await
    }

    async fn ttl(&self, key: &str) -> Result<Option<Duration>> {
        self.try_recover().await;

        let _guard = self.recovery_lock.read().await;

        if self.is_using_fallback() {
            return self.fallback.lock().await.ttl(key).await;
        }

        let primary = self.primary.lock().await;
        match primary.ttl(key).await {
            Ok(result) => Ok(result),
            Err(e) => {
                self.switch_to_fallback(&e.to_string());
                self.fallback.lock().await.ttl(key).await
            }
        }
    }

    async fn set_if_not_exists(&self, key: &str, value: &str, ttl_seconds: u64) -> Result<bool> {
        self.try_recover().await;

        let _guard = self.recovery_lock.read().await;

        if self.is_using_fallback() {
            return self
                .fallback
                .lock()
                .await
                .set_if_not_exists(key, value, ttl_seconds)
                .await;
        }

        let primary = self.primary.lock().await;
        match primary.set_if_not_exists(key, value, ttl_seconds).await {
            Ok(result) => Ok(result),
            Err(e) => {
                self.switch_to_fallback(&e.to_string());
                self.fallback
                    .lock()
                    .await
                    .set_if_not_exists(key, value, ttl_seconds)
                    .await
            }
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::memory_cache_manager::MemoryCacheManager;
    use sockudo_core::error::Error;
    use std::sync::Arc;
    use tokio::sync::Mutex as TokioMutex;

    /// Mock cache that can be configured to fail on demand
    struct MockCache {
        should_fail: Arc<TokioMutex<bool>>,
        data: Arc<TokioMutex<std::collections::HashMap<String, String>>>,
    }

    impl MockCache {
        fn new() -> Self {
            Self {
                should_fail: Arc::new(TokioMutex::new(false)),
                data: Arc::new(TokioMutex::new(std::collections::HashMap::new())),
            }
        }

        #[allow(dead_code)]
        async fn set_should_fail(&self, should_fail: bool) {
            *self.should_fail.lock().await = should_fail;
        }
    }

    #[async_trait]
    impl CacheManager for MockCache {
        async fn has(&self, key: &str) -> Result<bool> {
            if *self.should_fail.lock().await {
                return Err(Error::Cache("Mock failure".to_string()));
            }
            Ok(self.data.lock().await.contains_key(key))
        }

        async fn get(&self, key: &str) -> Result<Option<String>> {
            if *self.should_fail.lock().await {
                return Err(Error::Cache("Mock failure".to_string()));
            }
            Ok(self.data.lock().await.get(key).cloned())
        }

        async fn set(&self, key: &str, value: &str, _ttl_seconds: u64) -> Result<()> {
            if *self.should_fail.lock().await {
                return Err(Error::Cache("Mock failure".to_string()));
            }
            self.data
                .lock()
                .await
                .insert(key.to_string(), value.to_string());
            Ok(())
        }

        async fn remove(&self, key: &str) -> Result<()> {
            if *self.should_fail.lock().await {
                return Err(Error::Cache("Mock failure".to_string()));
            }
            self.data.lock().await.remove(key);
            Ok(())
        }

        async fn disconnect(&self) -> Result<()> {
            Ok(())
        }

        async fn check_health(&self) -> Result<()> {
            if *self.should_fail.lock().await {
                return Err(Error::Cache("Mock failure".to_string()));
            }
            Ok(())
        }

        async fn ttl(&self, _key: &str) -> Result<Option<Duration>> {
            if *self.should_fail.lock().await {
                return Err(Error::Cache("Mock failure".to_string()));
            }
            Ok(Some(Duration::from_secs(60)))
        }
    }

    #[tokio::test]
    async fn test_fallback_on_primary_failure() {
        let mock = MockCache::new();
        let should_fail = mock.should_fail.clone();

        let options = MemoryCacheOptions {
            ttl: 60,
            cleanup_interval: 60,
            max_capacity: 100,
        };

        let manager = FallbackCacheManager::new(Box::new(mock), options);

        assert!(!manager.is_using_fallback());

        *should_fail.lock().await = true;

        let result = manager.set("test_key", "test_value", 60).await;
        assert!(result.is_ok());

        assert!(manager.is_using_fallback());

        let value = manager.get("test_key").await.unwrap();
        assert_eq!(value, Some("test_value".to_string()));
    }

    #[tokio::test]
    async fn test_recovery_with_data_sync() {
        let mock = MockCache::new();
        let should_fail = mock.should_fail.clone();

        let options = MemoryCacheOptions {
            ttl: 60,
            cleanup_interval: 60,
            max_capacity: 100,
        };

        let manager = FallbackCacheManager::new(Box::new(mock), options);

        *should_fail.lock().await = true;
        manager.set("key1", "value1", 60).await.unwrap();
        manager.set("key2", "value2", 60).await.unwrap();

        assert!(manager.is_using_fallback());

        let value1_fallback = manager.get("key1").await.unwrap();
        assert_eq!(value1_fallback, Some("value1".to_string()));

        *should_fail.lock().await = false;

        manager.using_fallback.store(true, Ordering::SeqCst);
        manager.last_failure_time.store(0, Ordering::SeqCst);

        assert!(manager.is_using_fallback());
        let value2_fallback = manager.get("key2").await.unwrap();
        assert_eq!(value2_fallback, Some("value2".to_string()));
    }

    #[tokio::test]
    async fn test_no_race_condition_during_operations() {
        let mock = MockCache::new();
        let should_fail = mock.should_fail.clone();

        let options = MemoryCacheOptions {
            ttl: 60,
            cleanup_interval: 60,
            max_capacity: 100,
        };

        let manager = Arc::new(TokioMutex::new(FallbackCacheManager::new(
            Box::new(mock),
            options,
        )));

        *should_fail.lock().await = true;
        manager.lock().await.set("test", "value", 60).await.unwrap();

        assert!(manager.lock().await.is_using_fallback());

        let handles: Vec<_> = (0..10)
            .map(|i| {
                let manager_clone = manager.clone();
                tokio::spawn(async move {
                    let mgr = manager_clone.lock().await;
                    mgr.set(&format!("key{}", i), &format!("value{}", i), 60)
                        .await
                })
            })
            .collect();

        for handle in handles {
            assert!(handle.await.unwrap().is_ok());
        }

        assert!(manager.lock().await.is_using_fallback());

        for i in 0..10 {
            let mgr = manager.lock().await;
            let value = mgr.get(&format!("key{}", i)).await.unwrap();
            assert_eq!(value, Some(format!("value{}", i)));
        }
    }

    #[tokio::test]
    async fn test_memory_cache_get_all_entries() {
        let options = MemoryCacheOptions {
            ttl: 60,
            cleanup_interval: 60,
            max_capacity: 100,
        };

        let cache = MemoryCacheManager::new("test_prefix".to_string(), options);

        cache.set("key1", "value1", 60).await.unwrap();
        cache.set("key2", "value2", 60).await.unwrap();
        cache.set("key3", "value3", 60).await.unwrap();

        let entries = cache.get_all_entries().await;

        assert_eq!(entries.len(), 3);

        let keys: Vec<String> = entries.iter().map(|(k, _, _)| k.clone()).collect();
        assert!(keys.contains(&"key1".to_string()));
        assert!(keys.contains(&"key2".to_string()));
        assert!(keys.contains(&"key3".to_string()));

        for (key, value, ttl) in entries {
            match key.as_str() {
                "key1" => assert_eq!(value, "value1"),
                "key2" => assert_eq!(value, "value2"),
                "key3" => assert_eq!(value, "value3"),
                _ => panic!("Unexpected key: {}", key),
            }
            assert_eq!(ttl, Some(Duration::from_secs(60)));
        }
    }

    #[tokio::test]
    async fn test_new_with_health_check_healthy_primary() {
        let mock = MockCache::new();
        let options = MemoryCacheOptions {
            ttl: 60,
            cleanup_interval: 60,
            max_capacity: 100,
        };

        let manager = FallbackCacheManager::new_with_health_check(Box::new(mock), options).await;

        assert!(!manager.is_using_fallback());
    }

    #[tokio::test]
    async fn test_new_with_health_check_unhealthy_primary() {
        let mock = MockCache::new();
        let should_fail = mock.should_fail.clone();
        *should_fail.lock().await = true;

        let options = MemoryCacheOptions {
            ttl: 60,
            cleanup_interval: 60,
            max_capacity: 100,
        };

        let manager = FallbackCacheManager::new_with_health_check(Box::new(mock), options).await;

        assert!(manager.is_using_fallback());
    }

    #[tokio::test]
    async fn test_new_without_health_check() {
        let mock = MockCache::new();
        let should_fail = mock.should_fail.clone();
        *should_fail.lock().await = true;

        let options = MemoryCacheOptions {
            ttl: 60,
            cleanup_interval: 60,
            max_capacity: 100,
        };

        let manager = FallbackCacheManager::new(Box::new(mock), options);

        assert!(!manager.is_using_fallback());
    }
}