ipfrs-storage 0.1.0

Storage backends and block management for IPFRS content-addressed system
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
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
//! Storage Pool Manager for multi-backend routing
//!
//! This module provides a storage pool that manages multiple backends with
//! intelligent routing strategies including:
//! - Load balancing across backends
//! - Size-based routing (small blocks to fast storage, large to cold)
//! - Cost-aware routing for cloud storage
//! - Automatic failover and redundancy
//! - Backend health monitoring

use crate::traits::BlockStore;
use async_trait::async_trait;
use dashmap::DashMap;
use ipfrs_core::{Block, Cid, Error, Result};
use serde::{Deserialize, Serialize};
use std::sync::atomic::{AtomicU64, AtomicUsize, Ordering};
use std::sync::Arc;
use std::time::{Duration, Instant};
use tracing::{debug, warn};

/// Backend identifier
pub type BackendId = String;

/// Routing strategy for selecting backends
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum RoutingStrategy {
    /// Round-robin load balancing
    RoundRobin,
    /// Route based on block size (small to fast, large to slow)
    SizeBased,
    /// Route to least loaded backend
    LeastLoaded,
    /// Route to lowest cost backend
    CostAware,
    /// Route to geographically closest backend
    LatencyAware,
    /// Replicate to all backends
    Replicated,
    /// Hash-based consistent hashing
    ConsistentHash,
}

/// Backend configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct BackendConfig {
    /// Unique backend identifier
    pub id: BackendId,
    /// Backend priority (higher = preferred)
    pub priority: u8,
    /// Maximum capacity in bytes (0 = unlimited)
    pub capacity: u64,
    /// Current used bytes
    pub used: u64,
    /// Cost per GB per month (for cost-aware routing)
    pub cost_per_gb: f64,
    /// Average read latency in milliseconds
    pub avg_latency_ms: f64,
    /// Block size threshold (route blocks larger than this)
    pub size_threshold: Option<u64>,
    /// Whether this backend is healthy
    pub healthy: bool,
    /// Whether to use for reads
    pub read_enabled: bool,
    /// Whether to use for writes
    pub write_enabled: bool,
}

impl Default for BackendConfig {
    fn default() -> Self {
        Self {
            id: "default".to_string(),
            priority: 100,
            capacity: 0,
            used: 0,
            cost_per_gb: 0.0,
            avg_latency_ms: 10.0,
            size_threshold: None,
            healthy: true,
            read_enabled: true,
            write_enabled: true,
        }
    }
}

/// Backend statistics
#[derive(Debug, Default)]
pub struct BackendStats {
    /// Total read operations
    pub reads: AtomicU64,
    /// Total write operations
    pub writes: AtomicU64,
    /// Total bytes read
    pub bytes_read: AtomicU64,
    /// Total bytes written
    pub bytes_written: AtomicU64,
    /// Total errors
    pub errors: AtomicU64,
    /// Last health check time
    pub last_health_check: parking_lot::Mutex<Option<Instant>>,
}

impl BackendStats {
    fn record_read(&self, bytes: u64) {
        self.reads.fetch_add(1, Ordering::Relaxed);
        self.bytes_read.fetch_add(bytes, Ordering::Relaxed);
    }

    fn record_write(&self, bytes: u64) {
        self.writes.fetch_add(1, Ordering::Relaxed);
        self.bytes_written.fetch_add(bytes, Ordering::Relaxed);
    }

    fn record_error(&self) {
        self.errors.fetch_add(1, Ordering::Relaxed);
    }

    fn update_health_check(&self) {
        *self.last_health_check.lock() = Some(Instant::now());
    }
}

/// Backend wrapper with metadata
struct Backend<S: BlockStore> {
    store: Arc<S>,
    config: parking_lot::RwLock<BackendConfig>,
    stats: BackendStats,
}

/// Storage pool configuration
#[derive(Debug, Clone)]
pub struct PoolConfig {
    /// Routing strategy
    pub strategy: RoutingStrategy,
    /// Replication factor (for replicated strategy)
    pub replication_factor: usize,
    /// Health check interval
    pub health_check_interval: Duration,
    /// Enable automatic failover
    pub auto_failover: bool,
    /// Minimum healthy backends required
    pub min_healthy_backends: usize,
}

impl Default for PoolConfig {
    fn default() -> Self {
        Self {
            strategy: RoutingStrategy::RoundRobin,
            replication_factor: 1,
            health_check_interval: Duration::from_secs(30),
            auto_failover: true,
            min_healthy_backends: 1,
        }
    }
}

/// Storage pool statistics
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PoolStats {
    /// Total backends
    pub total_backends: usize,
    /// Healthy backends
    pub healthy_backends: usize,
    /// Total capacity
    pub total_capacity: u64,
    /// Total used
    pub total_used: u64,
    /// Total reads
    pub total_reads: u64,
    /// Total writes
    pub total_writes: u64,
    /// Total errors
    pub total_errors: u64,
    /// Average cost per GB
    pub avg_cost_per_gb: f64,
    /// Average latency
    pub avg_latency_ms: f64,
}

/// Storage Pool Manager
///
/// Manages multiple storage backends with intelligent routing
pub struct StoragePool<S: BlockStore> {
    backends: DashMap<BackendId, Backend<S>>,
    config: parking_lot::RwLock<PoolConfig>,
    round_robin_counter: AtomicUsize,
    /// CID to backend mapping (for tracking where blocks are stored)
    cid_map: DashMap<Cid, Vec<BackendId>>,
}

impl<S: BlockStore> StoragePool<S> {
    /// Create a new storage pool
    pub fn new(config: PoolConfig) -> Self {
        Self {
            backends: DashMap::new(),
            config: parking_lot::RwLock::new(config),
            round_robin_counter: AtomicUsize::new(0),
            cid_map: DashMap::new(),
        }
    }

    /// Add a backend to the pool
    pub fn add_backend(&self, config: BackendConfig, store: Arc<S>) {
        let id = config.id.clone();
        let backend = Backend {
            store,
            config: parking_lot::RwLock::new(config),
            stats: BackendStats::default(),
        };
        self.backends.insert(id.clone(), backend);
        debug!("Added backend to pool: {}", id);
    }

    /// Remove a backend from the pool
    pub fn remove_backend(&self, id: &str) -> Option<Arc<S>> {
        self.backends.remove(id).map(|(_, backend)| backend.store)
    }

    /// Get backend configuration
    pub fn get_backend_config(&self, id: &str) -> Option<BackendConfig> {
        self.backends
            .get(id)
            .map(|backend| backend.config.read().clone())
    }

    /// Update backend configuration
    pub fn update_backend_config(&self, id: &str, config: BackendConfig) -> Result<()> {
        let backend = self
            .backends
            .get(id)
            .ok_or_else(|| Error::Storage(format!("Backend not found: {}", id)))?;
        *backend.config.write() = config;
        Ok(())
    }

    /// Mark backend as healthy or unhealthy
    pub fn set_backend_health(&self, id: &str, healthy: bool) -> Result<()> {
        let backend = self
            .backends
            .get(id)
            .ok_or_else(|| Error::Storage(format!("Backend not found: {}", id)))?;
        backend.config.write().healthy = healthy;
        backend.stats.update_health_check();
        debug!("Backend {} health set to: {}", id, healthy);
        Ok(())
    }

    /// Get pool statistics
    pub fn stats(&self) -> PoolStats {
        let mut total_capacity = 0u64;
        let mut total_used = 0u64;
        let mut total_reads = 0u64;
        let mut total_writes = 0u64;
        let mut total_errors = 0u64;
        let mut total_cost = 0.0;
        let mut total_latency = 0.0;
        let mut healthy_count = 0;
        let total_count = self.backends.len();

        for backend in self.backends.iter() {
            let config = backend.config.read();
            let stats = &backend.stats;

            if config.healthy {
                healthy_count += 1;
            }

            total_capacity += config.capacity;
            total_used += config.used;
            total_reads += stats.reads.load(Ordering::Relaxed);
            total_writes += stats.writes.load(Ordering::Relaxed);
            total_errors += stats.errors.load(Ordering::Relaxed);
            total_cost += config.cost_per_gb;
            total_latency += config.avg_latency_ms;
        }

        let avg_cost_per_gb = if total_count > 0 {
            total_cost / total_count as f64
        } else {
            0.0
        };

        let avg_latency_ms = if total_count > 0 {
            total_latency / total_count as f64
        } else {
            0.0
        };

        PoolStats {
            total_backends: total_count,
            healthy_backends: healthy_count,
            total_capacity,
            total_used,
            total_reads,
            total_writes,
            total_errors,
            avg_cost_per_gb,
            avg_latency_ms,
        }
    }

    /// Select backends for a write operation
    #[allow(dead_code)]
    fn select_backends_for_write(&self, cid: &Cid, data_size: usize) -> Vec<BackendId> {
        let config = self.config.read();
        let strategy = config.strategy;
        let replication_factor = config.replication_factor;

        match strategy {
            RoutingStrategy::RoundRobin => self.select_round_robin(replication_factor),
            RoutingStrategy::SizeBased => self.select_size_based(data_size, replication_factor),
            RoutingStrategy::LeastLoaded => self.select_least_loaded(replication_factor),
            RoutingStrategy::CostAware => self.select_cost_aware(replication_factor),
            RoutingStrategy::LatencyAware => self.select_latency_aware(replication_factor),
            RoutingStrategy::Replicated => self.select_all_healthy(),
            RoutingStrategy::ConsistentHash => self.select_consistent_hash(cid, replication_factor),
        }
    }

    /// Select backends using round-robin
    fn select_round_robin(&self, count: usize) -> Vec<BackendId> {
        let healthy: Vec<_> = self
            .backends
            .iter()
            .filter(|b| b.config.read().healthy && b.config.read().write_enabled)
            .map(|b| b.config.read().id.clone())
            .collect();

        if healthy.is_empty() {
            return Vec::new();
        }

        let mut selected = Vec::new();
        for _ in 0..count.min(healthy.len()) {
            let idx = self.round_robin_counter.fetch_add(1, Ordering::Relaxed) % healthy.len();
            selected.push(healthy[idx].clone());
        }
        selected
    }

    /// Select backends based on size
    fn select_size_based(&self, data_size: usize, count: usize) -> Vec<BackendId> {
        let mut candidates: Vec<_> = self
            .backends
            .iter()
            .filter_map(|b| {
                let config = b.config.read();
                if !config.healthy || !config.write_enabled {
                    return None;
                }

                let matches_size = if let Some(threshold) = config.size_threshold {
                    if data_size >= threshold as usize {
                        config.priority >= 50 // Low priority for large blocks
                    } else {
                        config.priority > 50 // High priority for small blocks
                    }
                } else {
                    true
                };

                if matches_size {
                    Some((config.id.clone(), config.priority))
                } else {
                    None
                }
            })
            .collect();

        candidates.sort_by(|a, b| b.1.cmp(&a.1));
        candidates
            .into_iter()
            .take(count)
            .map(|(id, _)| id)
            .collect()
    }

    /// Select least loaded backends
    fn select_least_loaded(&self, count: usize) -> Vec<BackendId> {
        let mut candidates: Vec<_> = self
            .backends
            .iter()
            .filter_map(|b| {
                let config = b.config.read();
                if !config.healthy || !config.write_enabled {
                    return None;
                }

                let load = if config.capacity > 0 {
                    (config.used as f64 / config.capacity as f64 * 100.0) as u64
                } else {
                    0
                };

                Some((config.id.clone(), load))
            })
            .collect();

        candidates.sort_by_key(|(_, load)| *load);
        candidates
            .into_iter()
            .take(count)
            .map(|(id, _)| id)
            .collect()
    }

    /// Select lowest cost backends
    fn select_cost_aware(&self, count: usize) -> Vec<BackendId> {
        let mut candidates: Vec<_> = self
            .backends
            .iter()
            .filter_map(|b| {
                let config = b.config.read();
                if !config.healthy || !config.write_enabled {
                    return None;
                }
                Some((config.id.clone(), (config.cost_per_gb * 1000.0) as u64))
            })
            .collect();

        candidates.sort_by_key(|(_, cost)| *cost);
        candidates
            .into_iter()
            .take(count)
            .map(|(id, _)| id)
            .collect()
    }

    /// Select lowest latency backends
    fn select_latency_aware(&self, count: usize) -> Vec<BackendId> {
        let mut candidates: Vec<_> = self
            .backends
            .iter()
            .filter_map(|b| {
                let config = b.config.read();
                if !config.healthy || !config.read_enabled {
                    return None;
                }
                Some((config.id.clone(), (config.avg_latency_ms * 1000.0) as u64))
            })
            .collect();

        candidates.sort_by_key(|(_, latency)| *latency);
        candidates
            .into_iter()
            .take(count)
            .map(|(id, _)| id)
            .collect()
    }

    /// Select all healthy backends
    fn select_all_healthy(&self) -> Vec<BackendId> {
        self.backends
            .iter()
            .filter_map(|b| {
                let config = b.config.read();
                if config.healthy && config.write_enabled {
                    Some(config.id.clone())
                } else {
                    None
                }
            })
            .collect()
    }

    /// Select backends using consistent hashing
    fn select_consistent_hash(&self, cid: &Cid, count: usize) -> Vec<BackendId> {
        let healthy: Vec<_> = self
            .backends
            .iter()
            .filter_map(|b| {
                let config = b.config.read();
                if config.healthy && config.write_enabled {
                    Some(config.id.clone())
                } else {
                    None
                }
            })
            .collect();

        if healthy.is_empty() {
            return Vec::new();
        }

        // Use CID hash to determine backend
        let cid_bytes = cid.to_bytes();
        let hash = cid_bytes
            .iter()
            .fold(0u64, |acc, &b| acc.wrapping_mul(31).wrapping_add(b as u64));

        let mut selected = Vec::new();
        for i in 0..count.min(healthy.len()) {
            let idx = ((hash + i as u64) % healthy.len() as u64) as usize;
            selected.push(healthy[idx].clone());
        }
        selected
    }

    /// Get backends where a CID is stored
    fn get_backends_for_cid(&self, cid: &Cid) -> Vec<BackendId> {
        self.cid_map
            .get(cid)
            .map(|backends| backends.clone())
            .unwrap_or_default()
    }

    /// Record that a CID is stored in a backend
    fn record_cid_location(&self, cid: Cid, backend_id: BackendId) {
        self.cid_map
            .entry(cid)
            .or_insert_with(Vec::new)
            .push(backend_id);
    }
}

#[async_trait]
impl<S: BlockStore + Send + Sync + 'static> BlockStore for StoragePool<S> {
    async fn get(&self, cid: &Cid) -> Result<Option<Block>> {
        // Try to get from known backends first
        let known_backends = self.get_backends_for_cid(cid);

        for backend_id in known_backends {
            if let Some(backend) = self.backends.get(&backend_id) {
                let (healthy, read_enabled) = {
                    let config = backend.config.read();
                    (config.healthy, config.read_enabled)
                };

                if !healthy || !read_enabled {
                    continue;
                }

                match backend.store.get(cid).await {
                    Ok(Some(block)) => {
                        backend.stats.record_read(block.data().len() as u64);
                        return Ok(Some(block));
                    }
                    Ok(None) => continue,
                    Err(e) => {
                        warn!("Backend {} failed to get CID: {}", backend_id, e);
                        backend.stats.record_error();
                    }
                }
            }
        }

        // Fallback: try all healthy backends
        for backend in self.backends.iter() {
            let (healthy, read_enabled, backend_id) = {
                let config = backend.config.read();
                (config.healthy, config.read_enabled, config.id.clone())
            };

            if !healthy || !read_enabled {
                continue;
            }

            match backend.store.get(cid).await {
                Ok(Some(block)) => {
                    backend.stats.record_read(block.data().len() as u64);
                    // Update mapping
                    self.record_cid_location(*cid, backend_id);
                    return Ok(Some(block));
                }
                Ok(None) => continue,
                Err(_) => {
                    backend.stats.record_error();
                }
            }
        }

        Ok(None)
    }

    async fn put(&self, block: &Block) -> Result<()> {
        let cid = block.cid();
        let data_size = block.data().len();
        let backends = self.select_backends_for_write(cid, data_size);

        if backends.is_empty() {
            return Err(Error::Storage(
                "No healthy backends available for write".to_string(),
            ));
        }

        let mut errors = Vec::new();
        let mut success_count = 0;

        for backend_id in &backends {
            if let Some(backend) = self.backends.get(backend_id) {
                match backend.store.put(block).await {
                    Ok(()) => {
                        backend.stats.record_write(data_size as u64);
                        self.record_cid_location(*cid, backend_id.clone());
                        success_count += 1;
                    }
                    Err(e) => {
                        backend.stats.record_error();
                        errors.push((backend_id.clone(), e));
                    }
                }
            }
        }

        if success_count == 0 {
            return Err(Error::Storage(format!(
                "Failed to write to any backend: {} errors",
                errors.len()
            )));
        }

        Ok(())
    }

    async fn has(&self, cid: &Cid) -> Result<bool> {
        // Check known backends first
        let known_backends = self.get_backends_for_cid(cid);

        for backend_id in known_backends {
            if let Some(backend) = self.backends.get(&backend_id) {
                if !backend.config.read().healthy {
                    continue;
                }

                if let Ok(true) = backend.store.has(cid).await {
                    return Ok(true);
                }
            }
        }

        // Fallback: check all healthy backends
        for backend in self.backends.iter() {
            if !backend.config.read().healthy {
                continue;
            }

            if let Ok(true) = backend.store.has(cid).await {
                // Update mapping
                self.record_cid_location(*cid, backend.config.read().id.clone());
                return Ok(true);
            }
        }

        Ok(false)
    }

    async fn delete(&self, cid: &Cid) -> Result<()> {
        let backends = self.get_backends_for_cid(cid);

        if backends.is_empty() {
            // Try all backends if we don't know where it's stored
            for backend in self.backends.iter() {
                let _ = backend.store.delete(cid).await;
            }
        } else {
            for backend_id in &backends {
                if let Some(backend) = self.backends.get(backend_id) {
                    let _ = backend.store.delete(cid).await;
                }
            }
        }

        // Remove from mapping
        self.cid_map.remove(cid);
        Ok(())
    }

    fn list_cids(&self) -> Result<Vec<Cid>> {
        // Get unique CIDs from the mapping
        let cids: Vec<Cid> = self.cid_map.iter().map(|entry| *entry.key()).collect();
        Ok(cids)
    }

    fn len(&self) -> usize {
        self.cid_map.len()
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::memory::MemoryBlockStore;
    use bytes::Bytes;

    #[tokio::test]
    async fn test_pool_basic() {
        let pool = StoragePool::new(PoolConfig::default());

        let backend1 = Arc::new(MemoryBlockStore::new());
        let config1 = BackendConfig {
            id: "backend1".to_string(),
            ..Default::default()
        };

        pool.add_backend(config1, backend1);

        let data = Bytes::from_static(b"test data");
        let block = Block::new(data).unwrap();
        let cid = block.cid();

        pool.put(&block).await.unwrap();
        assert!(pool.has(cid).await.unwrap());

        let retrieved = pool.get(cid).await.unwrap();
        assert!(retrieved.is_some());
        assert_eq!(retrieved.unwrap().data(), block.data());
    }

    #[tokio::test]
    async fn test_pool_replicated() {
        let config = PoolConfig {
            strategy: RoutingStrategy::Replicated,
            ..Default::default()
        };
        let pool = StoragePool::new(config);

        let backend1 = Arc::new(MemoryBlockStore::new());
        let backend2 = Arc::new(MemoryBlockStore::new());

        pool.add_backend(
            BackendConfig {
                id: "backend1".to_string(),
                ..Default::default()
            },
            backend1.clone(),
        );

        pool.add_backend(
            BackendConfig {
                id: "backend2".to_string(),
                ..Default::default()
            },
            backend2.clone(),
        );

        let data = Bytes::from_static(b"test data");
        let block = Block::new(data).unwrap();
        let cid = block.cid();

        pool.put(&block).await.unwrap();

        // Should be in both backends
        assert!(backend1.has(cid).await.unwrap());
        assert!(backend2.has(cid).await.unwrap());
    }

    #[tokio::test]
    async fn test_pool_stats() {
        let pool = StoragePool::new(PoolConfig::default());

        let backend1 = Arc::new(MemoryBlockStore::new());
        pool.add_backend(
            BackendConfig {
                id: "backend1".to_string(),
                capacity: 1000,
                cost_per_gb: 0.023,
                ..Default::default()
            },
            backend1,
        );

        let stats = pool.stats();
        assert_eq!(stats.total_backends, 1);
        assert_eq!(stats.healthy_backends, 1);
        assert_eq!(stats.total_capacity, 1000);
    }
}