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
//! Retry logic with exponential backoff and jitter
//!
//! Provides sophisticated retry strategies for handling transient failures:
//! - Exponential backoff to prevent overwhelming failing services
//! - Jitter to prevent thundering herd problems
//! - Configurable max attempts and timeouts
//! - Retry condition predicates
//!
//! ## Example
//! ```no_run
//! use ipfrs_storage::RetryPolicy;
//! use std::time::Duration;
//!
//! async fn flaky_operation() -> Result<String, std::io::Error> {
//!     // Your operation that might fail transiently
//!     Ok("success".to_string())
//! }
//!
//! #[tokio::main]
//! async fn main() {
//!     let policy = RetryPolicy::exponential(
//!         Duration::from_millis(100),
//!         3
//!     );
//!
//!     let result = policy.retry(|| flaky_operation()).await;
//!     println!("Result: {:?}", result);
//! }
//! ```

use anyhow::{anyhow, Result};
use serde::{Deserialize, Serialize};
use std::future::Future;
use std::time::Duration;
use tokio::time::sleep;

/// Backoff strategy for retries
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum BackoffStrategy {
    /// Fixed delay between retries
    Fixed,
    /// Exponential backoff (delay doubles each retry)
    Exponential,
    /// Linear backoff (delay increases linearly)
    Linear,
}

/// Jitter type to add randomness to backoff
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum JitterType {
    /// No jitter
    None,
    /// Full jitter (0 to computed delay)
    Full,
    /// Equal jitter (half computed delay + random half)
    Equal,
    /// Decorrelated jitter (AWS recommended)
    Decorrelated,
}

/// Retry policy configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RetryPolicy {
    /// Maximum number of attempts (including initial attempt)
    pub max_attempts: u32,
    /// Base delay for backoff
    pub base_delay: Duration,
    /// Maximum delay between retries
    pub max_delay: Duration,
    /// Backoff strategy
    pub strategy: BackoffStrategy,
    /// Jitter type
    pub jitter: JitterType,
    /// Multiplier for exponential backoff (default: 2.0)
    pub backoff_multiplier: f64,
    /// Overall timeout for all retry attempts
    pub total_timeout: Option<Duration>,
}

impl RetryPolicy {
    /// Create a new retry policy with exponential backoff
    ///
    /// # Arguments
    /// * `base_delay` - Initial delay between retries
    /// * `max_attempts` - Maximum number of attempts
    pub fn exponential(base_delay: Duration, max_attempts: u32) -> Self {
        Self {
            max_attempts,
            base_delay,
            max_delay: Duration::from_secs(60),
            strategy: BackoffStrategy::Exponential,
            jitter: JitterType::Equal,
            backoff_multiplier: 2.0,
            total_timeout: None,
        }
    }

    /// Create a retry policy with fixed delays
    pub fn fixed(delay: Duration, max_attempts: u32) -> Self {
        Self {
            max_attempts,
            base_delay: delay,
            max_delay: delay,
            strategy: BackoffStrategy::Fixed,
            jitter: JitterType::None,
            backoff_multiplier: 1.0,
            total_timeout: None,
        }
    }

    /// Create a retry policy with linear backoff
    pub fn linear(base_delay: Duration, max_attempts: u32) -> Self {
        Self {
            max_attempts,
            base_delay,
            max_delay: Duration::from_secs(60),
            strategy: BackoffStrategy::Linear,
            jitter: JitterType::Equal,
            backoff_multiplier: 1.0,
            total_timeout: None,
        }
    }

    /// Set maximum delay
    pub fn with_max_delay(mut self, max_delay: Duration) -> Self {
        self.max_delay = max_delay;
        self
    }

    /// Set jitter type
    pub fn with_jitter(mut self, jitter: JitterType) -> Self {
        self.jitter = jitter;
        self
    }

    /// Set total timeout
    pub fn with_timeout(mut self, timeout: Duration) -> Self {
        self.total_timeout = Some(timeout);
        self
    }

    /// Set backoff multiplier
    pub fn with_multiplier(mut self, multiplier: f64) -> Self {
        self.backoff_multiplier = multiplier;
        self
    }

    /// Calculate delay for a given attempt number
    fn calculate_delay(&self, attempt: u32) -> Duration {
        if attempt == 0 {
            return Duration::from_secs(0);
        }

        let base_ms = self.base_delay.as_millis() as f64;

        let computed_delay_ms = match self.strategy {
            BackoffStrategy::Fixed => base_ms,
            BackoffStrategy::Exponential => {
                base_ms * self.backoff_multiplier.powi(attempt as i32 - 1)
            }
            BackoffStrategy::Linear => base_ms * attempt as f64,
        };

        // Cap at max delay
        let capped_ms = computed_delay_ms.min(self.max_delay.as_millis() as f64);

        // Apply jitter
        let final_ms = match self.jitter {
            JitterType::None => capped_ms,
            JitterType::Full => {
                // Random value between 0 and computed delay
                fastrand::f64() * capped_ms
            }
            JitterType::Equal => {
                // Half of computed delay + random half
                capped_ms / 2.0 + (fastrand::f64() * capped_ms / 2.0)
            }
            JitterType::Decorrelated => {
                // AWS recommended: min(max_delay, random(base, last_delay * 3))
                let last_delay = if attempt > 1 {
                    self.calculate_delay(attempt - 1).as_millis() as f64
                } else {
                    base_ms
                };
                let random_delay = base_ms + (fastrand::f64() * (last_delay * 3.0 - base_ms));
                random_delay.min(self.max_delay.as_millis() as f64)
            }
        };

        Duration::from_millis(final_ms as u64)
    }

    /// Execute a function with retry logic
    ///
    /// # Arguments
    /// * `f` - Function to retry
    ///
    /// # Returns
    /// Result of the function or last error
    pub async fn retry<F, Fut, T, E>(&self, mut f: F) -> Result<T>
    where
        F: FnMut() -> Fut,
        Fut: Future<Output = Result<T, E>>,
        E: std::error::Error + Send + Sync + 'static,
    {
        let start_time = std::time::Instant::now();
        let mut last_error = None;

        for attempt in 0..self.max_attempts {
            // Check total timeout
            if let Some(timeout) = self.total_timeout {
                if start_time.elapsed() >= timeout {
                    return Err(anyhow!("Retry timeout exceeded after {attempt} attempts"));
                }
            }

            // Try the operation
            match f().await {
                Ok(result) => return Ok(result),
                Err(e) => {
                    last_error = Some(e);

                    // Don't sleep after the last attempt
                    if attempt + 1 < self.max_attempts {
                        let delay = self.calculate_delay(attempt + 1);
                        sleep(delay).await;
                    }
                }
            }
        }

        // All attempts failed
        if let Some(e) = last_error {
            Err(anyhow!(
                "Operation failed after {} attempts: {}",
                self.max_attempts,
                e
            ))
        } else {
            Err(anyhow!(
                "Operation failed after {} attempts",
                self.max_attempts
            ))
        }
    }
}

impl Default for RetryPolicy {
    fn default() -> Self {
        Self::exponential(Duration::from_millis(100), 3)
    }
}

/// Trait for retryable operations
pub trait Retryable<T, E> {
    /// Execute with retry policy
    fn with_retry(self, policy: RetryPolicy) -> impl Future<Output = Result<T>>;
}

/// Retry statistics
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct RetryStats {
    /// Total retry attempts made
    pub total_attempts: u64,
    /// Successful operations
    pub successful_ops: u64,
    /// Failed operations (after all retries)
    pub failed_ops: u64,
    /// Total delay time spent in retries
    pub total_delay_ms: u64,
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::sync::atomic::{AtomicU32, Ordering};
    use std::sync::Arc;

    #[tokio::test]
    async fn test_retry_success_first_attempt() {
        let policy = RetryPolicy::exponential(Duration::from_millis(10), 3);

        let result = policy
            .retry(|| async { Ok::<_, std::io::Error>("success") })
            .await;

        assert!(result.is_ok());
        assert_eq!(result.unwrap(), "success");
    }

    #[tokio::test]
    async fn test_retry_success_after_failures() {
        let counter = Arc::new(AtomicU32::new(0));
        let policy = RetryPolicy::exponential(Duration::from_millis(10), 3);

        let counter_clone = counter.clone();
        let result = policy
            .retry(|| {
                let c = counter_clone.clone();
                async move {
                    let count = c.fetch_add(1, Ordering::SeqCst);
                    if count < 2 {
                        Err(std::io::Error::new(
                            std::io::ErrorKind::Other,
                            "Transient failure",
                        ))
                    } else {
                        Ok("success")
                    }
                }
            })
            .await;

        assert!(result.is_ok());
        assert_eq!(result.unwrap(), "success");
        assert_eq!(counter.load(Ordering::SeqCst), 3);
    }

    #[tokio::test]
    async fn test_retry_all_attempts_fail() {
        let policy = RetryPolicy::exponential(Duration::from_millis(10), 3);

        let result = policy
            .retry(|| async {
                Err::<&str, std::io::Error>(std::io::Error::new(
                    std::io::ErrorKind::Other,
                    "Always fails",
                ))
            })
            .await;

        assert!(result.is_err());
    }

    #[tokio::test]
    async fn test_fixed_backoff() {
        let policy = RetryPolicy::fixed(Duration::from_millis(50), 3);

        for i in 1..=3 {
            let delay = policy.calculate_delay(i);
            assert_eq!(delay.as_millis(), 50);
        }
    }

    #[tokio::test]
    async fn test_exponential_backoff() {
        let policy = RetryPolicy::exponential(Duration::from_millis(100), 4);

        // Exponential growth (without jitter for this test)
        let policy_no_jitter = policy.with_jitter(JitterType::None);
        let d1 = policy_no_jitter.calculate_delay(1).as_millis();
        let d2 = policy_no_jitter.calculate_delay(2).as_millis();
        let d3 = policy_no_jitter.calculate_delay(3).as_millis();

        assert_eq!(d1, 100);
        assert_eq!(d2, 200);
        assert_eq!(d3, 400);
    }

    #[tokio::test]
    async fn test_linear_backoff() {
        let policy =
            RetryPolicy::linear(Duration::from_millis(100), 4).with_jitter(JitterType::None);

        let d1 = policy.calculate_delay(1).as_millis();
        let d2 = policy.calculate_delay(2).as_millis();
        let d3 = policy.calculate_delay(3).as_millis();

        assert_eq!(d1, 100);
        assert_eq!(d2, 200);
        assert_eq!(d3, 300);
    }

    #[tokio::test]
    async fn test_max_delay_cap() {
        let policy = RetryPolicy::exponential(Duration::from_millis(100), 10)
            .with_max_delay(Duration::from_millis(500))
            .with_jitter(JitterType::None);

        let delay = policy.calculate_delay(5);
        assert!(delay.as_millis() <= 500);
    }

    #[tokio::test]
    async fn test_jitter_full() {
        let policy =
            RetryPolicy::exponential(Duration::from_millis(100), 3).with_jitter(JitterType::Full);

        // With full jitter, delay should be between 0 and computed delay
        for _ in 0..10 {
            let delay = policy.calculate_delay(1);
            assert!(delay.as_millis() <= 100);
        }
    }

    #[tokio::test]
    async fn test_jitter_equal() {
        let policy =
            RetryPolicy::exponential(Duration::from_millis(100), 3).with_jitter(JitterType::Equal);

        // With equal jitter, delay should be between 50 and 100
        for _ in 0..10 {
            let delay = policy.calculate_delay(1);
            let ms = delay.as_millis();
            assert!(ms >= 50 && ms <= 100);
        }
    }

    #[tokio::test]
    async fn test_timeout() {
        let policy = RetryPolicy::exponential(Duration::from_millis(50), 10)
            .with_timeout(Duration::from_millis(150));

        let start = std::time::Instant::now();
        let result = policy
            .retry(|| async {
                Err::<&str, std::io::Error>(std::io::Error::new(
                    std::io::ErrorKind::Other,
                    "Always fails",
                ))
            })
            .await;

        let elapsed = start.elapsed();
        assert!(result.is_err());
        // Should timeout before all retries complete, but allow some margin
        assert!(elapsed < Duration::from_millis(500));
        assert!(elapsed >= Duration::from_millis(150));
    }
}