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
//! Self-Healing Infrastructure
//!
//! Provides automatic corruption detection, repair, and replica rebuilding
//! for production reliability and data integrity.
//!
//! Features:
//! - Checksum verification for all stored objects
//! - Automatic corruption detection and repair
//! - Replica health monitoring and rebuilding
//! - Quota enforcement with automatic cleanup
//! - Background integrity checking
//!
//! # Example
//!
//! ```no_run
//! use rs3gw::storage::self_healing::{SelfHealingManager, SelfHealingConfig};
//! use std::path::PathBuf;
//! use std::time::Duration;
//!
//! # async fn example() -> Result<(), Box<dyn std::error::Error>> {
//! let config = SelfHealingConfig {
//!     check_interval: Duration::from_secs(3600), // Check every hour
//!     repair_enabled: true,
//!     max_concurrent_repairs: 5,
//!     corruption_threshold: 0.01, // Alert if >1% corrupted
//!     auto_cleanup_enabled: true,
//!     retention_days: 30,
//! };
//!
//! let manager = SelfHealingManager::new(PathBuf::from("./data"), config);
//! manager.start_background_checks().await?;
//! # Ok(())
//! # }
//! ```

use crate::storage::StorageError;
use chrono::{DateTime, Duration as ChronoDuration, Utc};
use serde::{Deserialize, Serialize};
use sha2::{Digest, Sha256};
use std::collections::HashMap;
use std::path::{Path, PathBuf};
use std::sync::Arc;
use std::time::Duration;
use tokio::fs;
use tokio::sync::RwLock;
use tokio::time::interval;
use tracing::{debug, error, info, warn};

/// Configuration for self-healing behavior
#[derive(Debug, Clone)]
pub struct SelfHealingConfig {
    /// Interval between integrity checks
    pub check_interval: Duration,
    /// Enable automatic repair of corrupted objects
    pub repair_enabled: bool,
    /// Maximum number of concurrent repair operations
    pub max_concurrent_repairs: usize,
    /// Corruption threshold for alerting (percentage)
    pub corruption_threshold: f64,
    /// Enable automatic cleanup of old data
    pub auto_cleanup_enabled: bool,
    /// Retention period in days for automatic cleanup
    pub retention_days: i64,
}

impl Default for SelfHealingConfig {
    fn default() -> Self {
        Self {
            check_interval: Duration::from_secs(3600), // 1 hour
            repair_enabled: true,
            max_concurrent_repairs: 5,
            corruption_threshold: 0.01, // 1%
            auto_cleanup_enabled: false,
            retention_days: 30,
        }
    }
}

/// Result of an integrity check
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct IntegrityCheckResult {
    /// Bucket name
    pub bucket: String,
    /// Object key
    pub key: String,
    /// Check timestamp
    pub checked_at: DateTime<Utc>,
    /// Whether the object is corrupted
    pub is_corrupted: bool,
    /// Expected checksum
    pub expected_checksum: String,
    /// Actual checksum (if different)
    pub actual_checksum: Option<String>,
    /// Error message if check failed
    pub error: Option<String>,
}

/// Statistics for self-healing operations
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct SelfHealingStats {
    /// Total objects checked
    pub total_checked: u64,
    /// Number of corrupted objects detected
    pub corrupted_found: u64,
    /// Number of objects successfully repaired
    pub repaired: u64,
    /// Number of repair failures
    pub repair_failures: u64,
    /// Number of objects cleaned up
    pub cleaned_up: u64,
    /// Last check timestamp
    pub last_check: Option<DateTime<Utc>>,
    /// Current corruption rate
    pub corruption_rate: f64,
}

/// Self-healing manager for automatic data integrity
pub struct SelfHealingManager {
    storage_root: PathBuf,
    config: SelfHealingConfig,
    stats: Arc<RwLock<SelfHealingStats>>,
    check_results: Arc<RwLock<Vec<IntegrityCheckResult>>>,
}

impl SelfHealingManager {
    /// Create a new self-healing manager
    pub fn new(storage_root: PathBuf, config: SelfHealingConfig) -> Self {
        Self {
            storage_root,
            config,
            stats: Arc::new(RwLock::new(SelfHealingStats::default())),
            check_results: Arc::new(RwLock::new(Vec::new())),
        }
    }

    /// Start background integrity checks
    pub async fn start_background_checks(&self) -> Result<(), StorageError> {
        let storage_root = self.storage_root.clone();
        let config = self.config.clone();
        let stats = Arc::clone(&self.stats);
        let check_results = Arc::clone(&self.check_results);

        tokio::spawn(async move {
            let mut check_interval = interval(config.check_interval);

            loop {
                check_interval.tick().await;
                info!("Starting background integrity check");

                match Self::run_integrity_check(
                    &storage_root,
                    &config,
                    Arc::clone(&stats),
                    Arc::clone(&check_results),
                )
                .await
                {
                    Ok(_) => {
                        info!("Background integrity check completed successfully");
                    }
                    Err(e) => {
                        error!("Background integrity check failed: {}", e);
                    }
                }

                // Run cleanup if enabled
                if config.auto_cleanup_enabled {
                    if let Err(e) = Self::run_cleanup(&storage_root, config.retention_days).await {
                        error!("Automatic cleanup failed: {}", e);
                    }
                }
            }
        });

        Ok(())
    }

    /// Run a full integrity check on all objects
    async fn run_integrity_check(
        storage_root: &Path,
        config: &SelfHealingConfig,
        stats: Arc<RwLock<SelfHealingStats>>,
        check_results: Arc<RwLock<Vec<IntegrityCheckResult>>>,
    ) -> Result<(), StorageError> {
        let mut total_checked = 0;
        let mut corrupted_found = 0;
        let mut repaired = 0;
        let mut repair_failures = 0;
        let mut results = Vec::new();

        // Scan all buckets
        let mut bucket_entries = fs::read_dir(storage_root).await?;

        while let Some(bucket_entry) = bucket_entries.next_entry().await? {
            let bucket_path = bucket_entry.path();
            if !bucket_path.is_dir() {
                continue;
            }

            let bucket_name = bucket_path
                .file_name()
                .and_then(|n| n.to_str())
                .unwrap_or("")
                .to_string();

            // Skip metadata directories
            if bucket_name.starts_with('.') {
                continue;
            }

            // Check all objects in bucket
            if let Ok(object_results) =
                Self::check_bucket_integrity(&bucket_path, &bucket_name, config).await
            {
                for result in object_results {
                    total_checked += 1;

                    if result.is_corrupted {
                        corrupted_found += 1;
                        warn!(
                            "Corruption detected: {}/{} - {}",
                            result.bucket,
                            result.key,
                            result
                                .error
                                .as_ref()
                                .unwrap_or(&"checksum mismatch".to_string())
                        );

                        // Attempt repair if enabled
                        if config.repair_enabled {
                            match Self::attempt_repair(storage_root, &result).await {
                                Ok(()) => {
                                    repaired += 1;
                                    info!(
                                        "Successfully repaired: {}/{}",
                                        result.bucket, result.key
                                    );
                                }
                                Err(e) => {
                                    repair_failures += 1;
                                    error!(
                                        "Failed to repair {}/{}: {}",
                                        result.bucket, result.key, e
                                    );
                                }
                            }
                        }
                    }

                    results.push(result);
                }
            }
        }

        // Update statistics
        let corruption_rate = if total_checked > 0 {
            (corrupted_found as f64) / (total_checked as f64)
        } else {
            0.0
        };

        let mut stats_lock = stats.write().await;
        stats_lock.total_checked += total_checked;
        stats_lock.corrupted_found += corrupted_found;
        stats_lock.repaired += repaired;
        stats_lock.repair_failures += repair_failures;
        stats_lock.last_check = Some(Utc::now());
        stats_lock.corruption_rate = corruption_rate;
        drop(stats_lock);

        // Store check results
        let mut results_lock = check_results.write().await;
        results_lock.clear();
        results_lock.extend(results);
        drop(results_lock);

        // Alert if corruption rate exceeds threshold
        if corruption_rate > config.corruption_threshold {
            error!(
                "ALERT: Corruption rate ({:.2}%) exceeds threshold ({:.2}%)",
                corruption_rate * 100.0,
                config.corruption_threshold * 100.0
            );
        }

        info!(
            "Integrity check complete: {} checked, {} corrupted, {} repaired, {} failed",
            total_checked, corrupted_found, repaired, repair_failures
        );

        Ok(())
    }

    /// Check integrity of all objects in a bucket
    async fn check_bucket_integrity(
        bucket_path: &Path,
        bucket_name: &str,
        _config: &SelfHealingConfig,
    ) -> Result<Vec<IntegrityCheckResult>, StorageError> {
        let mut results = Vec::new();

        // Walk through all object files
        let walker = walkdir::WalkDir::new(bucket_path)
            .follow_links(false)
            .into_iter()
            .filter_map(|e| e.ok())
            .filter(|e| e.file_type().is_file())
            .filter(|e| !e.path().to_str().unwrap_or("").ends_with(".meta"));

        for entry in walker {
            let object_path = entry.path();

            // Get object key from path
            let key = object_path
                .strip_prefix(bucket_path)
                .ok()
                .and_then(|p| p.to_str())
                .unwrap_or("")
                .to_string();

            if key.is_empty() {
                continue;
            }

            // Read metadata file
            let meta_path = object_path.with_extension("meta");
            let result = match fs::read(&meta_path).await {
                Ok(meta_data) => {
                    match serde_json::from_slice::<HashMap<String, serde_json::Value>>(&meta_data) {
                        Ok(metadata) => {
                            let expected_etag = metadata
                                .get("etag")
                                .and_then(|v| v.as_str())
                                .unwrap_or("")
                                .to_string();

                            // Calculate actual checksum
                            match Self::calculate_checksum(object_path).await {
                                Ok(actual_checksum) => {
                                    let is_corrupted = expected_etag != actual_checksum;

                                    IntegrityCheckResult {
                                        bucket: bucket_name.to_string(),
                                        key: key.clone(),
                                        checked_at: Utc::now(),
                                        is_corrupted,
                                        expected_checksum: expected_etag,
                                        actual_checksum: if is_corrupted {
                                            Some(actual_checksum)
                                        } else {
                                            None
                                        },
                                        error: if is_corrupted {
                                            Some("Checksum mismatch".to_string())
                                        } else {
                                            None
                                        },
                                    }
                                }
                                Err(e) => IntegrityCheckResult {
                                    bucket: bucket_name.to_string(),
                                    key: key.clone(),
                                    checked_at: Utc::now(),
                                    is_corrupted: true,
                                    expected_checksum: expected_etag,
                                    actual_checksum: None,
                                    error: Some(format!("Failed to calculate checksum: {}", e)),
                                },
                            }
                        }
                        Err(e) => IntegrityCheckResult {
                            bucket: bucket_name.to_string(),
                            key: key.clone(),
                            checked_at: Utc::now(),
                            is_corrupted: true,
                            expected_checksum: String::new(),
                            actual_checksum: None,
                            error: Some(format!("Failed to parse metadata: {}", e)),
                        },
                    }
                }
                Err(e) => IntegrityCheckResult {
                    bucket: bucket_name.to_string(),
                    key: key.clone(),
                    checked_at: Utc::now(),
                    is_corrupted: true,
                    expected_checksum: String::new(),
                    actual_checksum: None,
                    error: Some(format!("Failed to read metadata: {}", e)),
                },
            };

            results.push(result);
        }

        Ok(results)
    }

    /// Calculate SHA256 checksum of a file
    async fn calculate_checksum(path: &Path) -> Result<String, StorageError> {
        let data = fs::read(path).await?;
        let mut hasher = Sha256::new();
        hasher.update(&data);
        let result = hasher.finalize();
        Ok(hex::encode(result))
    }

    /// Attempt to repair a corrupted object
    async fn attempt_repair(
        _storage_root: &Path,
        result: &IntegrityCheckResult,
    ) -> Result<(), StorageError> {
        // In a single-node setup, we can't repair without replicas
        // This would be implemented in cluster mode by fetching from healthy replicas
        debug!(
            "Repair attempted for {}/{} (not implemented for single-node)",
            result.bucket, result.key
        );

        // For now, just log that repair would happen in cluster mode
        Err(StorageError::Internal(
            "Repair not available in single-node mode".to_string(),
        ))
    }

    /// Run automatic cleanup of old data
    async fn run_cleanup(storage_root: &Path, retention_days: i64) -> Result<u64, StorageError> {
        let mut cleaned_up = 0;
        let cutoff_time = Utc::now() - ChronoDuration::days(retention_days);

        info!(
            "Running automatic cleanup for objects older than {} days",
            retention_days
        );

        // Scan all buckets
        let mut bucket_entries = fs::read_dir(storage_root).await?;

        while let Some(bucket_entry) = bucket_entries.next_entry().await? {
            let bucket_path = bucket_entry.path();
            if !bucket_path.is_dir() {
                continue;
            }

            let bucket_name = bucket_path
                .file_name()
                .and_then(|n| n.to_str())
                .unwrap_or("")
                .to_string();

            // Skip metadata directories
            if bucket_name.starts_with('.') {
                continue;
            }

            // Check all objects in bucket
            let walker = walkdir::WalkDir::new(&bucket_path)
                .follow_links(false)
                .into_iter()
                .filter_map(|e| e.ok())
                .filter(|e| e.file_type().is_file())
                .filter(|e| !e.path().to_str().unwrap_or("").ends_with(".meta"));

            for entry in walker {
                let object_path = entry.path();

                // Check last modified time
                if let Ok(metadata) = entry.metadata() {
                    if let Ok(modified) = metadata.modified() {
                        let modified_time: DateTime<Utc> = modified.into();

                        if modified_time < cutoff_time {
                            // Delete old object and its metadata
                            match fs::remove_file(object_path).await {
                                Ok(_) => {
                                    cleaned_up += 1;
                                    debug!("Cleaned up old object: {:?}", object_path);

                                    // Also remove metadata file
                                    let meta_path = object_path.with_extension("meta");
                                    let _ = fs::remove_file(meta_path).await;
                                }
                                Err(e) => {
                                    warn!("Failed to cleanup {:?}: {}", object_path, e);
                                }
                            }
                        }
                    }
                }
            }
        }

        info!(
            "Automatic cleanup completed: {} objects removed",
            cleaned_up
        );
        Ok(cleaned_up)
    }

    /// Get current self-healing statistics
    pub async fn get_stats(&self) -> SelfHealingStats {
        self.stats.read().await.clone()
    }

    /// Get recent integrity check results
    pub async fn get_check_results(&self) -> Vec<IntegrityCheckResult> {
        self.check_results.read().await.clone()
    }

    /// Run an immediate integrity check (on-demand)
    pub async fn run_immediate_check(&self) -> Result<SelfHealingStats, StorageError> {
        Self::run_integrity_check(
            &self.storage_root,
            &self.config,
            Arc::clone(&self.stats),
            Arc::clone(&self.check_results),
        )
        .await?;

        Ok(self.get_stats().await)
    }

    /// Manually trigger cleanup
    pub async fn trigger_cleanup(&self) -> Result<u64, StorageError> {
        let cleaned = Self::run_cleanup(&self.storage_root, self.config.retention_days).await?;

        let mut stats = self.stats.write().await;
        stats.cleaned_up += cleaned;
        drop(stats);

        Ok(cleaned)
    }
}

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

    #[tokio::test]
    async fn test_self_healing_manager_creation() {
        let temp_dir = TempDir::new().expect("Failed to create temp dir");
        let config = SelfHealingConfig::default();
        let manager = SelfHealingManager::new(temp_dir.path().to_path_buf(), config);

        let stats = manager.get_stats().await;
        assert_eq!(stats.total_checked, 0);
        assert_eq!(stats.corrupted_found, 0);
    }

    #[tokio::test]
    async fn test_checksum_calculation() {
        let temp_dir = TempDir::new().expect("Failed to create temp dir");
        let test_file = temp_dir.path().join("test.dat");

        fs::write(&test_file, b"test data")
            .await
            .expect("Failed to write test file");

        let checksum = SelfHealingManager::calculate_checksum(&test_file)
            .await
            .expect("Failed to calculate checksum");

        assert!(!checksum.is_empty());
        assert_eq!(checksum.len(), 64); // SHA256 hex string length
    }

    #[tokio::test]
    async fn test_integrity_check_empty_storage() {
        let temp_dir = TempDir::new().expect("Failed to create temp dir");
        let config = SelfHealingConfig::default();
        let manager = SelfHealingManager::new(temp_dir.path().to_path_buf(), config);

        let stats = manager.run_immediate_check().await.expect("Check failed");

        assert_eq!(stats.total_checked, 0);
        assert_eq!(stats.corrupted_found, 0);
    }

    #[tokio::test]
    async fn test_cleanup_old_files() {
        let temp_dir = TempDir::new().expect("Failed to create temp dir");

        // Create a bucket directory
        let bucket_path = temp_dir.path().join("test-bucket");
        fs::create_dir(&bucket_path)
            .await
            .expect("Failed to create bucket");

        // Create a test file
        let test_file = bucket_path.join("old-object.dat");
        fs::write(&test_file, b"old data")
            .await
            .expect("Failed to write file");

        // Run cleanup with 0 retention days (clean everything)
        let cleaned = SelfHealingManager::run_cleanup(temp_dir.path(), 0)
            .await
            .expect("Cleanup failed");

        // Should have cleaned up 1 file
        assert_eq!(cleaned, 1);
    }

    #[tokio::test]
    async fn test_stats_tracking() {
        let temp_dir = TempDir::new().expect("Failed to create temp dir");
        let config = SelfHealingConfig::default();
        let manager = SelfHealingManager::new(temp_dir.path().to_path_buf(), config);

        // Run check
        let _ = manager.run_immediate_check().await;

        let stats = manager.get_stats().await;
        assert!(stats.last_check.is_some());
    }

    #[tokio::test]
    async fn test_config_default() {
        let config = SelfHealingConfig::default();

        assert_eq!(config.check_interval, Duration::from_secs(3600));
        assert!(config.repair_enabled);
        assert_eq!(config.max_concurrent_repairs, 5);
        assert_eq!(config.corruption_threshold, 0.01);
        assert!(!config.auto_cleanup_enabled);
        assert_eq!(config.retention_days, 30);
    }
}