oxigdal-cloud 0.1.4

Advanced cloud storage backends for OxiGDAL - Pure Rust cloud integration
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
//! Retry logic with exponential backoff and circuit breaker pattern
//!
//! This module provides comprehensive retry mechanisms for cloud storage operations,
//! including exponential backoff, jitter, circuit breaker, retry budgets, and idempotency checks.

use std::time::Duration;

use crate::error::{CloudError, Result, RetryError};

/// Default maximum number of retry attempts
pub const DEFAULT_MAX_RETRIES: usize = 3;

/// Default initial backoff duration (100ms)
pub const DEFAULT_INITIAL_BACKOFF: Duration = Duration::from_millis(100);

/// Default maximum backoff duration (30 seconds)
pub const DEFAULT_MAX_BACKOFF: Duration = Duration::from_secs(30);

/// Default backoff multiplier
pub const DEFAULT_BACKOFF_MULTIPLIER: f64 = 2.0;

/// Retry strategy configuration
#[derive(Debug, Clone)]
pub struct RetryConfig {
    /// Maximum number of retry attempts
    pub max_retries: usize,
    /// Initial backoff duration
    pub initial_backoff: Duration,
    /// Maximum backoff duration
    pub max_backoff: Duration,
    /// Backoff multiplier for exponential backoff
    pub backoff_multiplier: f64,
    /// Whether to add jitter to backoff
    pub jitter: bool,
    /// Whether to enable circuit breaker
    pub circuit_breaker: bool,
    /// Circuit breaker failure threshold
    pub circuit_breaker_threshold: usize,
    /// Circuit breaker timeout duration
    pub circuit_breaker_timeout: Duration,
}

impl Default for RetryConfig {
    fn default() -> Self {
        Self {
            max_retries: DEFAULT_MAX_RETRIES,
            initial_backoff: DEFAULT_INITIAL_BACKOFF,
            max_backoff: DEFAULT_MAX_BACKOFF,
            backoff_multiplier: DEFAULT_BACKOFF_MULTIPLIER,
            jitter: true,
            circuit_breaker: true,
            circuit_breaker_threshold: 5,
            circuit_breaker_timeout: Duration::from_secs(60),
        }
    }
}

impl RetryConfig {
    /// Creates a new retry configuration
    #[must_use]
    pub fn new() -> Self {
        Self::default()
    }

    /// Sets the maximum number of retries
    #[must_use]
    pub fn with_max_retries(mut self, max_retries: usize) -> Self {
        self.max_retries = max_retries;
        self
    }

    /// Sets the initial backoff duration
    #[must_use]
    pub fn with_initial_backoff(mut self, duration: Duration) -> Self {
        self.initial_backoff = duration;
        self
    }

    /// Sets the maximum backoff duration
    #[must_use]
    pub fn with_max_backoff(mut self, duration: Duration) -> Self {
        self.max_backoff = duration;
        self
    }

    /// Sets the backoff multiplier
    #[must_use]
    pub fn with_backoff_multiplier(mut self, multiplier: f64) -> Self {
        self.backoff_multiplier = multiplier;
        self
    }

    /// Enables or disables jitter
    #[must_use]
    pub fn with_jitter(mut self, jitter: bool) -> Self {
        self.jitter = jitter;
        self
    }

    /// Enables or disables circuit breaker
    #[must_use]
    pub fn with_circuit_breaker(mut self, enabled: bool) -> Self {
        self.circuit_breaker = enabled;
        self
    }
}

/// Circuit breaker state
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum CircuitState {
    /// Circuit is closed (normal operation)
    Closed,
    /// Circuit is open (failures exceeded threshold)
    Open,
    /// Circuit is half-open (testing if service recovered)
    HalfOpen,
}

/// Circuit breaker for preventing cascading failures
#[derive(Debug)]
pub struct CircuitBreaker {
    /// Current state
    state: CircuitState,
    /// Consecutive failure count
    failure_count: usize,
    /// Failure threshold
    threshold: usize,
    /// Timeout duration
    timeout: Duration,
    /// Last failure time
    last_failure: Option<std::time::Instant>,
}

impl CircuitBreaker {
    /// Creates a new circuit breaker
    #[must_use]
    pub fn new(threshold: usize, timeout: Duration) -> Self {
        Self {
            state: CircuitState::Closed,
            failure_count: 0,
            threshold,
            timeout,
            last_failure: None,
        }
    }

    /// Checks if the circuit breaker allows the operation
    pub fn allow_request(&mut self) -> Result<()> {
        match self.state {
            CircuitState::Closed => Ok(()),
            CircuitState::Open => {
                // Check if timeout has elapsed
                if let Some(last_failure) = self.last_failure {
                    if last_failure.elapsed() >= self.timeout {
                        tracing::info!("Circuit breaker transitioning to half-open state");
                        self.state = CircuitState::HalfOpen;
                        Ok(())
                    } else {
                        Err(CloudError::Retry(RetryError::CircuitBreakerOpen {
                            message: "Circuit breaker is open".to_string(),
                        }))
                    }
                } else {
                    Ok(())
                }
            }
            CircuitState::HalfOpen => Ok(()),
        }
    }

    /// Records a successful operation
    pub fn record_success(&mut self) {
        match self.state {
            CircuitState::Closed => {
                self.failure_count = 0;
            }
            CircuitState::HalfOpen => {
                tracing::info!("Circuit breaker transitioning to closed state");
                self.state = CircuitState::Closed;
                self.failure_count = 0;
            }
            CircuitState::Open => {}
        }
    }

    /// Records a failed operation
    pub fn record_failure(&mut self) {
        self.failure_count += 1;
        self.last_failure = Some(std::time::Instant::now());

        if self.failure_count >= self.threshold && self.state != CircuitState::Open {
            tracing::warn!(
                "Circuit breaker opening after {} failures",
                self.failure_count
            );
            self.state = CircuitState::Open;
        }
    }

    /// Returns the current state of the circuit breaker
    #[must_use]
    pub fn state(&self) -> CircuitState {
        self.state
    }
}

/// Retry budget for limiting retry overhead
#[derive(Debug)]
pub struct RetryBudget {
    /// Available retry tokens
    tokens: usize,
    /// Maximum tokens
    max_tokens: usize,
    /// Token refill rate (tokens per second)
    refill_rate: f64,
    /// Last refill time
    last_refill: std::time::Instant,
}

impl RetryBudget {
    /// Creates a new retry budget
    #[must_use]
    pub fn new(max_tokens: usize, refill_rate: f64) -> Self {
        Self {
            tokens: max_tokens,
            max_tokens,
            refill_rate,
            last_refill: std::time::Instant::now(),
        }
    }

    /// Tries to consume a retry token
    pub fn try_consume(&mut self) -> Result<()> {
        self.refill();

        if self.tokens > 0 {
            self.tokens -= 1;
            Ok(())
        } else {
            Err(CloudError::Retry(RetryError::BudgetExhausted {
                message: "Retry budget exhausted".to_string(),
            }))
        }
    }

    /// Refills tokens based on elapsed time
    fn refill(&mut self) {
        let elapsed = self.last_refill.elapsed();
        let tokens_to_add = (elapsed.as_secs_f64() * self.refill_rate) as usize;

        if tokens_to_add > 0 {
            self.tokens = (self.tokens + tokens_to_add).min(self.max_tokens);
            self.last_refill = std::time::Instant::now();
        }
    }
}

/// Backoff calculator for exponential backoff with jitter
#[derive(Debug)]
pub struct Backoff {
    /// Configuration
    config: RetryConfig,
    /// Current attempt number
    attempt: usize,
}

impl Backoff {
    /// Creates a new backoff calculator
    #[must_use]
    pub fn new(config: RetryConfig) -> Self {
        Self { config, attempt: 0 }
    }

    /// Calculates the next backoff duration
    #[must_use]
    pub fn next(&mut self) -> Duration {
        let base = self.config.initial_backoff.as_secs_f64().mul_add(
            self.config.backoff_multiplier.powi(self.attempt as i32),
            0.0,
        );

        let backoff = if self.config.jitter {
            // Add jitter (0% to 50% of base)
            let jitter_factor = 1.0 + (rand() * 0.5);
            base * jitter_factor
        } else {
            base
        };

        self.attempt += 1;

        Duration::from_secs_f64(backoff.min(self.config.max_backoff.as_secs_f64()))
    }

    /// Resets the backoff state
    pub fn reset(&mut self) {
        self.attempt = 0;
    }
}

/// Simple pseudo-random number generator for jitter
/// Uses a basic LCG (Linear Congruential Generator)
fn rand() -> f64 {
    use std::sync::atomic::{AtomicU64, Ordering};
    static SEED: AtomicU64 = AtomicU64::new(0);

    let seed = SEED.load(Ordering::Relaxed);
    let next = seed.wrapping_mul(1664525).wrapping_add(1013904223);
    SEED.store(next, Ordering::Relaxed);

    (next >> 32) as f64 / u32::MAX as f64
}

/// Determines if an error is retryable
#[must_use]
pub fn is_retryable(error: &CloudError) -> bool {
    match error {
        CloudError::Timeout { .. } => true,
        CloudError::RateLimitExceeded { .. } => true,
        CloudError::Http(http_error) => match http_error {
            crate::error::HttpError::Network { .. } => true,
            crate::error::HttpError::Status { status, .. } => {
                // Retry on 5xx errors and some 4xx errors
                matches!(
                    *status,
                    500 | 502 | 503 | 504 | 408 | 429 // Server errors and rate limiting
                )
            }
            _ => false,
        },
        CloudError::S3(s3_error) => match s3_error {
            crate::error::S3Error::Sdk { .. } => true,
            _ => false,
        },
        CloudError::Azure(azure_error) => match azure_error {
            crate::error::AzureError::Sdk { .. } => true,
            _ => false,
        },
        CloudError::Gcs(gcs_error) => match gcs_error {
            crate::error::GcsError::Sdk { .. } => true,
            _ => false,
        },
        CloudError::Io(_) => true,
        _ => false,
    }
}

/// Retry executor with exponential backoff
#[cfg(feature = "async")]
pub struct RetryExecutor {
    /// Configuration
    config: RetryConfig,
    /// Circuit breaker
    circuit_breaker: Option<CircuitBreaker>,
    /// Retry budget
    retry_budget: Option<RetryBudget>,
}

#[cfg(feature = "async")]
impl RetryExecutor {
    /// Creates a new retry executor
    #[must_use]
    pub fn new(config: RetryConfig) -> Self {
        let circuit_breaker = if config.circuit_breaker {
            Some(CircuitBreaker::new(
                config.circuit_breaker_threshold,
                config.circuit_breaker_timeout,
            ))
        } else {
            None
        };

        let retry_budget = Some(RetryBudget::new(100, 10.0)); // 100 tokens, refill at 10/sec

        Self {
            config,
            circuit_breaker,
            retry_budget,
        }
    }

    /// Executes an async operation with retry logic
    pub async fn execute<F, Fut, T>(&mut self, mut operation: F) -> Result<T>
    where
        F: FnMut() -> Fut,
        Fut: std::future::Future<Output = Result<T>>,
    {
        // Check circuit breaker
        if let Some(ref mut cb) = self.circuit_breaker {
            cb.allow_request()?;
        }

        let mut backoff = Backoff::new(self.config.clone());
        let mut attempts = 0;

        loop {
            match operation().await {
                Ok(result) => {
                    // Success - record and return
                    if let Some(ref mut cb) = self.circuit_breaker {
                        cb.record_success();
                    }
                    return Ok(result);
                }
                Err(error) => {
                    attempts += 1;

                    // Check if error is retryable
                    if !is_retryable(&error) {
                        tracing::warn!("Non-retryable error: {}", error);
                        return Err(error);
                    }

                    // Check if we've exceeded max retries
                    if attempts > self.config.max_retries {
                        tracing::error!("Max retries ({}) exceeded", self.config.max_retries);
                        if let Some(ref mut cb) = self.circuit_breaker {
                            cb.record_failure();
                        }
                        return Err(CloudError::Retry(RetryError::MaxRetriesExceeded {
                            attempts,
                        }));
                    }

                    // Check retry budget
                    if let Some(ref mut budget) = self.retry_budget {
                        budget.try_consume()?;
                    }

                    // Calculate backoff and wait
                    let delay = backoff.next();
                    tracing::warn!(
                        "Retry attempt {}/{} after {:?}: {}",
                        attempts,
                        self.config.max_retries,
                        delay,
                        error
                    );

                    tokio::time::sleep(delay).await;
                }
            }
        }
    }
}

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

    #[test]
    fn test_retry_config_builder() {
        let config = RetryConfig::new()
            .with_max_retries(5)
            .with_initial_backoff(Duration::from_millis(50))
            .with_backoff_multiplier(3.0)
            .with_jitter(false);

        assert_eq!(config.max_retries, 5);
        assert_eq!(config.initial_backoff, Duration::from_millis(50));
        assert_eq!(config.backoff_multiplier, 3.0);
        assert!(!config.jitter);
    }

    #[test]
    fn test_circuit_breaker_closed() {
        let mut cb = CircuitBreaker::new(3, Duration::from_secs(60));
        assert_eq!(cb.state, CircuitState::Closed);
        assert!(cb.allow_request().is_ok());
    }

    #[test]
    fn test_circuit_breaker_opens() {
        let mut cb = CircuitBreaker::new(3, Duration::from_secs(60));

        // Record 3 failures
        cb.record_failure();
        cb.record_failure();
        cb.record_failure();

        assert_eq!(cb.state, CircuitState::Open);
        assert!(cb.allow_request().is_err());
    }

    #[test]
    fn test_circuit_breaker_half_open() {
        let mut cb = CircuitBreaker::new(3, Duration::from_millis(10));

        // Open the circuit
        cb.record_failure();
        cb.record_failure();
        cb.record_failure();
        assert_eq!(cb.state, CircuitState::Open);

        // Wait for timeout
        std::thread::sleep(Duration::from_millis(20));

        // Should transition to half-open
        assert!(cb.allow_request().is_ok());
        assert_eq!(cb.state, CircuitState::HalfOpen);

        // Success should close the circuit
        cb.record_success();
        assert_eq!(cb.state, CircuitState::Closed);
    }

    #[test]
    fn test_retry_budget() {
        // Use a high refill rate (100 tokens/sec) so we can sleep only 50ms
        // instead of seconds. At 100 tokens/sec, 50ms yields ~5 tokens.
        let mut budget = RetryBudget::new(10, 100.0);

        // Consume 10 tokens
        for _ in 0..10 {
            assert!(budget.try_consume().is_ok());
        }

        // 11th should fail
        assert!(budget.try_consume().is_err());

        // Wait for refill (50ms at 100 tokens/sec = ~5 tokens)
        std::thread::sleep(Duration::from_millis(50));

        // Should have refilled
        assert!(budget.try_consume().is_ok());
    }

    #[test]
    fn test_backoff_exponential() {
        let config = RetryConfig::new()
            .with_initial_backoff(Duration::from_millis(100))
            .with_backoff_multiplier(2.0)
            .with_jitter(false);

        let mut backoff = Backoff::new(config);

        let d1 = backoff.next();
        let d2 = backoff.next();
        let d3 = backoff.next();

        assert!(d1 < d2);
        assert!(d2 < d3);
    }

    #[test]
    fn test_is_retryable() {
        let timeout_error = CloudError::Timeout {
            message: "timeout".to_string(),
        };
        assert!(is_retryable(&timeout_error));

        let rate_limit_error = CloudError::RateLimitExceeded {
            message: "rate limit".to_string(),
        };
        assert!(is_retryable(&rate_limit_error));

        let not_found_error = CloudError::NotFound {
            key: "test".to_string(),
        };
        assert!(!is_retryable(&not_found_error));
    }

    #[cfg(feature = "async")]
    #[tokio::test]
    async fn test_retry_executor_success() {
        use std::sync::atomic::{AtomicUsize, Ordering};

        let config = RetryConfig::new().with_max_retries(3);
        let mut executor = RetryExecutor::new(config);

        let attempt = std::sync::Arc::new(AtomicUsize::new(0));
        let attempt_clone = attempt.clone();
        let result = executor
            .execute(|| {
                let attempt = attempt_clone.clone();
                async move {
                    let current = attempt.fetch_add(1, Ordering::SeqCst) + 1;
                    if current < 2 {
                        Err(CloudError::Timeout {
                            message: "timeout".to_string(),
                        })
                    } else {
                        Ok(42)
                    }
                }
            })
            .await;

        assert!(result.is_ok());
        assert_eq!(result.ok(), Some(42));
        assert_eq!(attempt.load(Ordering::SeqCst), 2);
    }

    #[cfg(feature = "async")]
    #[tokio::test]
    async fn test_retry_executor_max_retries() {
        use std::sync::atomic::{AtomicUsize, Ordering};

        let config = RetryConfig::new().with_max_retries(2);
        let mut executor = RetryExecutor::new(config);

        let attempt = std::sync::Arc::new(AtomicUsize::new(0));
        let attempt_clone = attempt.clone();
        let result: Result<i32> = executor
            .execute(|| {
                let attempt = attempt_clone.clone();
                async move {
                    attempt.fetch_add(1, Ordering::SeqCst);
                    Err(CloudError::Timeout {
                        message: "timeout".to_string(),
                    })
                }
            })
            .await;

        assert!(result.is_err());
        assert_eq!(attempt.load(Ordering::SeqCst), 3); // Initial + 2 retries
    }
}