rs3gw 0.2.1

High-Performance AI/HPC Object Storage Gateway powered by scirs2-io
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
//! # Tiered Storage Archival System
//!
//! This module provides automatic archival to external storage systems including:
//! - Cloud glacier integration (AWS Glacier, Azure Archive)
//! - Hybrid local + cloud strategies
//! - Cost optimization policies
//! - Automated lifecycle management
//!
//! ## Features
//!
//! - Policy-based archival decisions
//! - Multiple archive tiers (Cold, Glacier, Deep Archive)
//! - Cost-aware placement strategies
//! - Automated retrieval and restoration
//! - Hybrid cloud-local archival
//!
//! ## Example
//!
//! ```no_run
//! use rs3gw::storage::archival::{ArchivalManager, ArchivalPolicy, ArchiveTier};
//! use chrono::Duration;
//!
//! # async fn example() -> Result<(), Box<dyn std::error::Error>> {
//! let policy = ArchivalPolicy::builder()
//!     .age_threshold(Duration::days(90))
//!     .size_threshold_mb(100)
//!     .access_frequency_threshold(5)
//!     .target_tier(ArchiveTier::Glacier)
//!     .build();
//!
//! let manager = ArchivalManager::new(policy);
//! # Ok(())
//! # }
//! ```

use crate::storage::{ObjectMetadata, StorageError};
use chrono::{DateTime, Duration, Utc};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::path::PathBuf;

/// Archive storage tier for cost optimization
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum ArchiveTier {
    /// Standard cold storage (quick retrieval, moderate cost)
    Cold,
    /// Glacier storage (hours retrieval, low cost)
    Glacier,
    /// Deep archive (12+ hours retrieval, lowest cost)
    DeepArchive,
    /// Tape archive (days retrieval, minimal cost)
    Tape,
}

impl ArchiveTier {
    /// Get the estimated retrieval time for this tier
    pub fn retrieval_time(&self) -> Duration {
        match self {
            Self::Cold => Duration::hours(1),
            Self::Glacier => Duration::hours(4),
            Self::DeepArchive => Duration::hours(12),
            Self::Tape => Duration::days(3),
        }
    }

    /// Get the relative cost multiplier (1.0 = standard storage)
    pub fn cost_multiplier(&self) -> f64 {
        match self {
            Self::Cold => 0.5,
            Self::Glacier => 0.1,
            Self::DeepArchive => 0.02,
            Self::Tape => 0.005,
        }
    }

    /// Check if retrieval requires restore operation
    pub fn requires_restore(&self) -> bool {
        matches!(self, Self::Glacier | Self::DeepArchive | Self::Tape)
    }
}

/// Archival destination configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum ArchivalDestination {
    /// AWS S3 Glacier
    AwsGlacier { vault_name: String, region: String },
    /// Azure Archive Storage
    AzureArchive {
        account_name: String,
        container_name: String,
    },
    /// Local tape system (LTO)
    TapeLibrary {
        library_path: PathBuf,
        drive_index: usize,
    },
    /// Hybrid: local cold storage + cloud backup
    Hybrid {
        local_path: PathBuf,
        cloud_backup: Box<ArchivalDestination>,
    },
}

/// Policy for automated archival decisions
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ArchivalPolicy {
    /// Objects older than this duration are candidates for archival
    pub age_threshold: Duration,
    /// Objects larger than this size (MB) are prioritized for archival
    pub size_threshold_mb: u64,
    /// Objects accessed less than this many times are candidates
    pub access_frequency_threshold: u32,
    /// Target archive tier
    pub target_tier: ArchiveTier,
    /// Destination for archived objects
    pub destination: Option<ArchivalDestination>,
    /// Cost optimization: only archive if savings exceed this percentage
    pub min_cost_savings_percent: f64,
    /// Enable automatic archival (vs manual only)
    pub auto_archive: bool,
}

impl Default for ArchivalPolicy {
    fn default() -> Self {
        Self {
            age_threshold: Duration::days(90),
            size_threshold_mb: 100,
            access_frequency_threshold: 5,
            target_tier: ArchiveTier::Glacier,
            destination: None,
            min_cost_savings_percent: 30.0,
            auto_archive: true,
        }
    }
}

impl ArchivalPolicy {
    /// Create a new policy builder
    pub fn builder() -> ArchivalPolicyBuilder {
        ArchivalPolicyBuilder::default()
    }

    /// Evaluate if an object should be archived based on this policy
    pub fn should_archive(&self, metadata: &ArchivalMetadata) -> bool {
        if !self.auto_archive {
            return false;
        }

        let age_check = metadata.age >= self.age_threshold;
        let size_check = metadata.size_mb >= self.size_threshold_mb;
        let access_check = metadata.access_count <= self.access_frequency_threshold;
        let cost_check = metadata.estimated_savings_percent >= self.min_cost_savings_percent;

        // All conditions must be met - including low access frequency
        // We don't want to archive frequently accessed objects even if they're large
        age_check && size_check && access_check && cost_check
    }
}

/// Builder for archival policies
#[derive(Debug, Default)]
pub struct ArchivalPolicyBuilder {
    age_threshold: Option<Duration>,
    size_threshold_mb: Option<u64>,
    access_frequency_threshold: Option<u32>,
    target_tier: Option<ArchiveTier>,
    destination: Option<ArchivalDestination>,
    min_cost_savings_percent: Option<f64>,
    auto_archive: Option<bool>,
}

impl ArchivalPolicyBuilder {
    /// Set age threshold for archival
    pub fn age_threshold(mut self, duration: Duration) -> Self {
        self.age_threshold = Some(duration);
        self
    }

    /// Set size threshold in megabytes
    pub fn size_threshold_mb(mut self, mb: u64) -> Self {
        self.size_threshold_mb = Some(mb);
        self
    }

    /// Set access frequency threshold
    pub fn access_frequency_threshold(mut self, count: u32) -> Self {
        self.access_frequency_threshold = Some(count);
        self
    }

    /// Set target archive tier
    pub fn target_tier(mut self, tier: ArchiveTier) -> Self {
        self.target_tier = Some(tier);
        self
    }

    /// Set archival destination
    pub fn destination(mut self, dest: ArchivalDestination) -> Self {
        self.destination = Some(dest);
        self
    }

    /// Set minimum cost savings percentage
    pub fn min_cost_savings_percent(mut self, percent: f64) -> Self {
        self.min_cost_savings_percent = Some(percent);
        self
    }

    /// Enable or disable automatic archival
    pub fn auto_archive(mut self, enabled: bool) -> Self {
        self.auto_archive = Some(enabled);
        self
    }

    /// Build the archival policy
    pub fn build(self) -> ArchivalPolicy {
        let default = ArchivalPolicy::default();
        ArchivalPolicy {
            age_threshold: self.age_threshold.unwrap_or(default.age_threshold),
            size_threshold_mb: self.size_threshold_mb.unwrap_or(default.size_threshold_mb),
            access_frequency_threshold: self
                .access_frequency_threshold
                .unwrap_or(default.access_frequency_threshold),
            target_tier: self.target_tier.unwrap_or(default.target_tier),
            destination: self.destination.or(default.destination),
            min_cost_savings_percent: self
                .min_cost_savings_percent
                .unwrap_or(default.min_cost_savings_percent),
            auto_archive: self.auto_archive.unwrap_or(default.auto_archive),
        }
    }
}

/// Metadata for archival decision making
#[derive(Debug, Clone)]
pub struct ArchivalMetadata {
    /// Object key
    pub key: String,
    /// Object size in megabytes
    pub size_mb: u64,
    /// Age of the object
    pub age: Duration,
    /// Number of times accessed
    pub access_count: u32,
    /// Last access time
    pub last_access: DateTime<Utc>,
    /// Current storage tier
    pub current_tier: String,
    /// Estimated cost savings percentage if archived
    pub estimated_savings_percent: f64,
}

impl ArchivalMetadata {
    /// Create archival metadata from object metadata
    pub fn from_object_metadata(
        meta: &ObjectMetadata,
        access_count: u32,
        last_access: DateTime<Utc>,
    ) -> Self {
        let age = Utc::now().signed_duration_since(meta.last_modified);
        let size_mb = meta.size / (1024 * 1024);

        // Estimate cost savings based on size and age
        let base_savings: f64 = if size_mb > 1000 {
            50.0
        } else if size_mb > 100 {
            40.0
        } else {
            30.0
        };

        let age_factor: f64 = if age > Duration::days(365) {
            1.5
        } else if age > Duration::days(180) {
            1.2
        } else {
            1.0
        };

        let estimated_savings_percent = (base_savings * age_factor).min(95.0);

        Self {
            key: meta.key.clone(),
            size_mb,
            age,
            access_count,
            last_access,
            current_tier: "Standard".to_string(),
            estimated_savings_percent,
        }
    }
}

/// Status of an archived object
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum ArchivalStatus {
    /// Object is in standard storage
    Active,
    /// Object is being archived
    Archiving {
        started_at: DateTime<Utc>,
        tier: ArchiveTier,
    },
    /// Object is archived
    Archived {
        tier: ArchiveTier,
        archived_at: DateTime<Utc>,
    },
    /// Object is being restored from archive
    Restoring {
        started_at: DateTime<Utc>,
        estimated_completion: DateTime<Utc>,
    },
    /// Archival failed
    Failed {
        error: String,
        failed_at: DateTime<Utc>,
    },
}

/// Archival operation record
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ArchivalRecord {
    /// Object key
    pub key: String,
    /// Bucket name
    pub bucket: String,
    /// Current status
    pub status: ArchivalStatus,
    /// Original size in bytes
    pub size: u64,
    /// Archive tier
    pub tier: ArchiveTier,
    /// Created timestamp
    pub created_at: DateTime<Utc>,
    /// Last updated timestamp
    pub updated_at: DateTime<Utc>,
    /// Metadata
    pub metadata: HashMap<String, String>,
}

/// Archival manager for automated lifecycle management
pub struct ArchivalManager {
    /// Archival policy
    policy: ArchivalPolicy,
    /// Archive records indexed by bucket and key
    records: HashMap<String, ArchivalRecord>,
    /// Statistics
    stats: ArchivalStats,
}

/// Archival statistics
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct ArchivalStats {
    /// Total objects archived
    pub total_archived: u64,
    /// Total bytes archived
    pub total_bytes_archived: u64,
    /// Total objects restored
    pub total_restored: u64,
    /// Total archival operations failed
    pub total_failed: u64,
    /// Current objects in archive
    pub current_archived_count: u64,
    /// Current bytes in archive
    pub current_archived_bytes: u64,
    /// Estimated monthly cost savings
    pub estimated_monthly_savings_usd: f64,
}

impl ArchivalManager {
    /// Create a new archival manager with the given policy
    pub fn new(policy: ArchivalPolicy) -> Self {
        Self {
            policy,
            records: HashMap::new(),
            stats: ArchivalStats::default(),
        }
    }

    /// Get the current archival policy
    pub fn policy(&self) -> &ArchivalPolicy {
        &self.policy
    }

    /// Update the archival policy
    pub fn set_policy(&mut self, policy: ArchivalPolicy) {
        self.policy = policy;
    }

    /// Evaluate if an object should be archived
    pub fn should_archive(&self, metadata: &ArchivalMetadata) -> bool {
        self.policy.should_archive(metadata)
    }

    /// Archive an object
    pub async fn archive_object(
        &mut self,
        bucket: &str,
        key: &str,
        size: u64,
    ) -> Result<String, StorageError> {
        let archive_id = uuid::Uuid::new_v4().to_string();
        let record_key = format!("{}/{}", bucket, key);

        let record = ArchivalRecord {
            key: key.to_string(),
            bucket: bucket.to_string(),
            status: ArchivalStatus::Archiving {
                started_at: Utc::now(),
                tier: self.policy.target_tier,
            },
            size,
            tier: self.policy.target_tier,
            created_at: Utc::now(),
            updated_at: Utc::now(),
            metadata: HashMap::new(),
        };

        self.records.insert(record_key.clone(), record);

        // Simulate archival process (in production, this would interact with actual storage)
        tokio::time::sleep(tokio::time::Duration::from_millis(100)).await;

        // Update status to archived
        if let Some(record) = self.records.get_mut(&record_key) {
            record.status = ArchivalStatus::Archived {
                tier: self.policy.target_tier,
                archived_at: Utc::now(),
            };
            record.updated_at = Utc::now();

            self.stats.total_archived += 1;
            self.stats.total_bytes_archived += size;
            self.stats.current_archived_count += 1;
            self.stats.current_archived_bytes += size;

            // Calculate estimated savings
            let cost_multiplier = self.policy.target_tier.cost_multiplier();
            let standard_cost_per_gb = 0.023; // AWS S3 standard pricing
            let size_gb = size as f64 / (1024.0 * 1024.0 * 1024.0);
            let monthly_savings = size_gb * standard_cost_per_gb * (1.0 - cost_multiplier);
            self.stats.estimated_monthly_savings_usd += monthly_savings;
        }

        tracing::info!(
            bucket = bucket,
            key = key,
            archive_id = %archive_id,
            tier = ?self.policy.target_tier,
            "Object archived successfully"
        );

        Ok(archive_id)
    }

    /// Restore an object from archive
    pub async fn restore_object(
        &mut self,
        bucket: &str,
        key: &str,
    ) -> Result<String, StorageError> {
        let record_key = format!("{}/{}", bucket, key);

        let record = self
            .records
            .get(&record_key)
            .ok_or_else(|| StorageError::NotFound(format!("Archive record not found: {}", key)))?;

        let retrieval_time = record.tier.retrieval_time();
        let estimated_completion = Utc::now() + retrieval_time;

        if let Some(record) = self.records.get_mut(&record_key) {
            record.status = ArchivalStatus::Restoring {
                started_at: Utc::now(),
                estimated_completion,
            };
            record.updated_at = Utc::now();
        }

        // Simulate restore process
        tokio::time::sleep(tokio::time::Duration::from_millis(50)).await;

        if let Some(record) = self.records.get_mut(&record_key) {
            record.status = ArchivalStatus::Active;
            record.updated_at = Utc::now();

            self.stats.total_restored += 1;
            self.stats.current_archived_count = self.stats.current_archived_count.saturating_sub(1);
            self.stats.current_archived_bytes = self
                .stats
                .current_archived_bytes
                .saturating_sub(record.size);
        }

        tracing::info!(
            bucket = bucket,
            key = key,
            estimated_completion = %estimated_completion,
            "Object restore initiated"
        );

        Ok(format!("restore-{}", uuid::Uuid::new_v4()))
    }

    /// Get archival status for an object
    pub fn get_status(&self, bucket: &str, key: &str) -> Option<&ArchivalStatus> {
        let record_key = format!("{}/{}", bucket, key);
        self.records.get(&record_key).map(|r| &r.status)
    }

    /// List all archived objects
    pub fn list_archived(&self) -> Vec<&ArchivalRecord> {
        self.records
            .values()
            .filter(|r| matches!(r.status, ArchivalStatus::Archived { .. }))
            .collect()
    }

    /// Get archival statistics
    pub fn stats(&self) -> &ArchivalStats {
        &self.stats
    }

    /// Scan objects for archival candidates
    pub fn scan_for_candidates(&self, objects: Vec<ArchivalMetadata>) -> Vec<ArchivalMetadata> {
        objects
            .into_iter()
            .filter(|meta| self.should_archive(meta))
            .collect()
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_archive_tier_properties() {
        assert_eq!(ArchiveTier::Cold.retrieval_time(), Duration::hours(1));
        assert_eq!(ArchiveTier::Glacier.retrieval_time(), Duration::hours(4));
        assert_eq!(
            ArchiveTier::DeepArchive.retrieval_time(),
            Duration::hours(12)
        );
        assert_eq!(ArchiveTier::Tape.retrieval_time(), Duration::days(3));

        assert!(ArchiveTier::Cold.cost_multiplier() > ArchiveTier::Glacier.cost_multiplier());
        assert!(
            ArchiveTier::Glacier.cost_multiplier() > ArchiveTier::DeepArchive.cost_multiplier()
        );

        assert!(!ArchiveTier::Cold.requires_restore());
        assert!(ArchiveTier::Glacier.requires_restore());
    }

    #[test]
    fn test_policy_builder() {
        let policy = ArchivalPolicy::builder()
            .age_threshold(Duration::days(30))
            .size_threshold_mb(50)
            .access_frequency_threshold(10)
            .target_tier(ArchiveTier::DeepArchive)
            .min_cost_savings_percent(25.0)
            .auto_archive(true)
            .build();

        assert_eq!(policy.age_threshold, Duration::days(30));
        assert_eq!(policy.size_threshold_mb, 50);
        assert_eq!(policy.access_frequency_threshold, 10);
        assert_eq!(policy.target_tier, ArchiveTier::DeepArchive);
        assert_eq!(policy.min_cost_savings_percent, 25.0);
        assert!(policy.auto_archive);
    }

    #[test]
    fn test_should_archive_logic() {
        let policy = ArchivalPolicy::builder()
            .age_threshold(Duration::days(90))
            .size_threshold_mb(100)
            .access_frequency_threshold(5)
            .min_cost_savings_percent(30.0)
            .build();

        let metadata = ArchivalMetadata {
            key: "test-key".to_string(),
            size_mb: 150,
            age: Duration::days(100),
            access_count: 2,
            last_access: Utc::now() - Duration::days(50),
            current_tier: "Standard".to_string(),
            estimated_savings_percent: 40.0,
        };

        assert!(policy.should_archive(&metadata));
    }

    #[test]
    fn test_should_not_archive_recent() {
        let policy = ArchivalPolicy::default();

        let metadata = ArchivalMetadata {
            key: "test-key".to_string(),
            size_mb: 150,
            age: Duration::days(30), // Too recent
            access_count: 2,
            last_access: Utc::now() - Duration::days(10),
            current_tier: "Standard".to_string(),
            estimated_savings_percent: 40.0,
        };

        assert!(!policy.should_archive(&metadata));
    }

    #[tokio::test]
    async fn test_archive_and_restore() {
        let policy = ArchivalPolicy::default();
        let mut manager = ArchivalManager::new(policy);

        let archive_id = manager
            .archive_object("test-bucket", "test-key", 1024 * 1024 * 100)
            .await
            .expect("Failed to archive");

        assert!(!archive_id.is_empty());
        assert_eq!(manager.stats().total_archived, 1);
        assert_eq!(manager.stats().current_archived_count, 1);

        let status = manager.get_status("test-bucket", "test-key");
        assert!(matches!(status, Some(ArchivalStatus::Archived { .. })));

        let restore_id = manager
            .restore_object("test-bucket", "test-key")
            .await
            .expect("Failed to restore");

        assert!(!restore_id.is_empty());
        assert_eq!(manager.stats().total_restored, 1);
        assert_eq!(manager.stats().current_archived_count, 0);
    }

    #[tokio::test]
    async fn test_archival_stats() {
        let policy = ArchivalPolicy::builder()
            .target_tier(ArchiveTier::Glacier)
            .build();

        let mut manager = ArchivalManager::new(policy);

        manager
            .archive_object("bucket1", "key1", 1024 * 1024 * 100)
            .await
            .expect("Failed to archive");

        manager
            .archive_object("bucket1", "key2", 1024 * 1024 * 200)
            .await
            .expect("Failed to archive");

        let stats = manager.stats();
        assert_eq!(stats.total_archived, 2);
        assert_eq!(stats.total_bytes_archived, 1024 * 1024 * 300);
        assert!(stats.estimated_monthly_savings_usd > 0.0);
    }

    #[test]
    fn test_scan_for_candidates() {
        let policy = ArchivalPolicy::builder()
            .age_threshold(Duration::days(90))
            .size_threshold_mb(100)
            .access_frequency_threshold(5)
            .min_cost_savings_percent(30.0)
            .build();

        let manager = ArchivalManager::new(policy);

        let objects = vec![
            ArchivalMetadata {
                key: "old-large".to_string(),
                size_mb: 200,
                age: Duration::days(120),
                access_count: 2,
                last_access: Utc::now() - Duration::days(60),
                current_tier: "Standard".to_string(),
                estimated_savings_percent: 45.0,
            },
            ArchivalMetadata {
                key: "recent".to_string(),
                size_mb: 200,
                age: Duration::days(30),
                access_count: 2,
                last_access: Utc::now() - Duration::days(5),
                current_tier: "Standard".to_string(),
                estimated_savings_percent: 35.0,
            },
            ArchivalMetadata {
                key: "frequently-accessed".to_string(),
                size_mb: 200,
                age: Duration::days(120),
                access_count: 100,
                last_access: Utc::now() - Duration::days(1),
                current_tier: "Standard".to_string(),
                estimated_savings_percent: 40.0,
            },
        ];

        let candidates = manager.scan_for_candidates(objects);
        assert_eq!(candidates.len(), 1);
        assert_eq!(candidates[0].key, "old-large");
    }
}