spiceai 4.0.0

SDK for Spice.ai, an open-source runtime and platform for building AI-driven software.
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
// Use Fibonacci backoff util from spiceai: https://github.com/spiceai/spiceai/tree/trunk/crates/util/src
pub use backoff::Error as RetryError;
pub use backoff::future::retry;

use std::time::Duration;

use backoff::backoff::Backoff;

// Fibonacci-based backoff delay intervals capped at 5 mins
const BACKOFF_INTERVALS_MS: [u64; 14] = [
    1000, 1000, 2000, 3000, 5000, 8000, 13000, 21000, 34000, 55000, 89000, 144_000, 233_000,
    300_000,
];

#[derive(Debug)]
pub struct FibonacciBackoff {
    num_retries: usize,
    pub randomization_factor: f64,
    pub max_retries: Option<usize>,
    pub max_duration: Option<Duration>,
}

impl Default for FibonacciBackoff {
    fn default() -> FibonacciBackoff {
        FibonacciBackoff {
            num_retries: 0,
            randomization_factor: 0.3,
            max_retries: None,
            max_duration: None,
        }
    }
}

impl Backoff for FibonacciBackoff {
    fn reset(&mut self) {
        self.num_retries = 0;
    }

    fn next_backoff(&mut self) -> Option<Duration> {
        self.num_retries += 1;

        if let Some(max_retries) = self.max_retries
            && self.num_retries > max_retries
        {
            return None;
        }

        let interval = if self.num_retries >= BACKOFF_INTERVALS_MS.len() {
            Duration::from_millis(BACKOFF_INTERVALS_MS[BACKOFF_INTERVALS_MS.len() - 1])
        } else {
            Duration::from_millis(BACKOFF_INTERVALS_MS[self.num_retries])
        };

        let randomized_interval = get_random_value_from_interval(
            self.randomization_factor,
            rand::random::<f64>(),
            interval,
        );

        let final_interval = if let Some(max_duration) = self.max_duration {
            if randomized_interval > max_duration {
                max_duration
            } else {
                randomized_interval
            }
        } else {
            randomized_interval
        };

        Some(final_interval)
    }
}

fn get_random_value_from_interval(
    randomization_factor: f64,
    random: f64,
    current_interval: Duration,
) -> Duration {
    let current_interval_nanos = duration_to_nanos(current_interval);

    let delta = randomization_factor * current_interval_nanos;
    let min_interval = current_interval_nanos - delta;
    let max_interval = current_interval_nanos + delta;
    // Get a random value from the range [minInterval, maxInterval].
    // The formula used below has a +1 because if the minInterval is 1 and the maxInterval is 3 then
    // we want a 33% chance for selecting either 1, 2 or 3.
    let diff = max_interval - min_interval;
    let nanos = min_interval + (random * (diff + 1.0));
    nanos_to_duration(nanos)
}

pub struct FibonacciBackoffBuilder {
    randomization_factor: f64,
    max_retries: Option<usize>,
    max_duration: Option<Duration>,
}

impl FibonacciBackoffBuilder {
    #[must_use]
    pub fn new() -> Self {
        Self {
            randomization_factor: 0.3,
            max_retries: None,
            max_duration: None,
        }
    }

    /// Set the maximum number of retries. None means no limit.
    #[must_use]
    pub fn max_retries(mut self, value: Option<usize>) -> Self {
        self.max_retries = value;
        self
    }

    #[must_use]
    pub fn build(self) -> FibonacciBackoff {
        FibonacciBackoff {
            randomization_factor: self.randomization_factor,
            max_retries: self.max_retries,
            max_duration: self.max_duration,
            ..FibonacciBackoff::default()
        }
    }
}

impl Default for FibonacciBackoffBuilder {
    fn default() -> Self {
        Self::new()
    }
}

#[allow(clippy::cast_precision_loss)]
fn duration_to_nanos(d: Duration) -> f64 {
    d.as_secs() as f64 * 1_000_000_000.0 + f64::from(d.subsec_nanos())
}

#[allow(clippy::cast_possible_truncation)]
#[allow(clippy::cast_sign_loss)]
fn nanos_to_duration(nanos: f64) -> Duration {
    let secs = nanos / 1_000_000_000.0;
    let nanos = nanos as u64 % 1_000_000_000;
    Duration::new(secs as u64, nanos as u32)
}

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

    #[test]
    fn test_fibonacci_backoff_default() {
        let backoff = FibonacciBackoff::default();
        assert_eq!(backoff.num_retries, 0);
        assert!((backoff.randomization_factor - 0.3).abs() < f64::EPSILON);
        assert!(backoff.max_retries.is_none());
        assert!(backoff.max_duration.is_none());
    }

    #[test]
    fn test_fibonacci_backoff_reset() {
        let mut backoff = FibonacciBackoff::default();
        // Advance some retries
        backoff.next_backoff();
        backoff.next_backoff();
        assert_eq!(backoff.num_retries, 2);

        // Reset should set num_retries back to 0
        backoff.reset();
        assert_eq!(backoff.num_retries, 0);
    }

    #[test]
    fn test_fibonacci_backoff_next_returns_some() {
        let mut backoff = FibonacciBackoff::default();

        // First several calls should return Some
        for _ in 0..10 {
            let duration = backoff.next_backoff();
            assert!(duration.is_some());
        }
    }

    #[test]
    fn test_fibonacci_backoff_max_retries() {
        let mut backoff = FibonacciBackoff {
            max_retries: Some(3),
            ..FibonacciBackoff::default()
        };

        // First 3 should succeed
        assert!(backoff.next_backoff().is_some());
        assert!(backoff.next_backoff().is_some());
        assert!(backoff.next_backoff().is_some());

        // 4th should fail (exceeds max_retries)
        assert!(backoff.next_backoff().is_none());
    }

    #[test]
    fn test_fibonacci_backoff_max_duration() {
        let max_dur = Duration::from_millis(500);
        let mut backoff = FibonacciBackoff {
            max_duration: Some(max_dur),
            randomization_factor: 0.0, // No randomization for predictable testing
            ..FibonacciBackoff::default()
        };

        // All durations should be capped at max_duration
        for _ in 0..14 {
            let duration = backoff.next_backoff();
            assert!(duration.is_some());
            assert!(duration.expect("should have duration") <= max_dur);
        }
    }

    #[test]
    fn test_fibonacci_backoff_intervals_increase() {
        let mut backoff = FibonacciBackoff {
            randomization_factor: 0.0, // No randomization for predictable testing
            ..FibonacciBackoff::default()
        };

        let first = backoff.next_backoff().expect("should have duration");
        let second = backoff.next_backoff().expect("should have duration");

        // Intervals should match the Fibonacci sequence (1s, 1s, 2s, 3s, ...)
        assert_eq!(first, Duration::from_millis(1000));
        assert_eq!(second, Duration::from_millis(2000));
    }

    #[test]
    fn test_fibonacci_backoff_caps_at_max_interval() {
        let mut backoff = FibonacciBackoff {
            randomization_factor: 0.0,
            num_retries: 20, // Beyond the BACKOFF_INTERVALS_MS array
            ..FibonacciBackoff::default()
        };

        let duration = backoff.next_backoff().expect("should have duration");
        // Should be capped at the last value in BACKOFF_INTERVALS_MS (300_000 ms = 5 mins)
        assert_eq!(duration, Duration::from_millis(300_000));
    }

    #[test]
    fn test_fibonacci_backoff_builder_default() {
        let builder = FibonacciBackoffBuilder::default();
        let backoff = builder.build();

        assert_eq!(backoff.num_retries, 0);
        assert!((backoff.randomization_factor - 0.3).abs() < f64::EPSILON);
        assert!(backoff.max_retries.is_none());
    }

    #[test]
    fn test_fibonacci_backoff_builder_max_retries() {
        let backoff = FibonacciBackoffBuilder::new().max_retries(Some(5)).build();

        assert_eq!(backoff.max_retries, Some(5));
    }

    #[test]
    fn test_fibonacci_backoff_builder_chaining() {
        let backoff = FibonacciBackoffBuilder::new().max_retries(Some(10)).build();

        assert_eq!(backoff.max_retries, Some(10));
    }

    #[test]
    fn test_duration_to_nanos() {
        let dur = Duration::new(1, 500_000_000); // 1.5 seconds
        let nanos = duration_to_nanos(dur);
        assert!((nanos - 1_500_000_000.0).abs() < 1.0);
    }

    #[test]
    fn test_nanos_to_duration() {
        let nanos = 1_500_000_000.0; // 1.5 seconds
        let dur = nanos_to_duration(nanos);
        assert_eq!(dur.as_secs(), 1);
        assert!(dur.subsec_nanos() >= 499_000_000 && dur.subsec_nanos() <= 501_000_000);
    }

    #[test]
    fn test_duration_roundtrip() {
        let original = Duration::new(2, 250_000_000);
        let nanos = duration_to_nanos(original);
        let restored = nanos_to_duration(nanos);

        // Should be very close (within a few nanoseconds due to floating point)
        let diff = original.abs_diff(restored);
        assert!(diff < Duration::from_nanos(1000));
    }

    #[test]
    fn test_get_random_value_from_interval_zero_factor() {
        let interval = Duration::from_secs(1);
        let result = get_random_value_from_interval(0.0, 0.5, interval);

        // With zero randomization, should return the original interval
        assert_eq!(result, interval);
    }

    #[test]
    fn test_get_random_value_from_interval_bounds() {
        let interval = Duration::from_secs(10);
        let factor = 0.5; // 50% randomization

        // With random = 0, should get min bound
        let min_result = get_random_value_from_interval(factor, 0.0, interval);
        // Min should be around 5 seconds (10 - 50%)
        assert!(min_result >= Duration::from_secs(4) && min_result <= Duration::from_secs(6));

        // With random = 1, should get max bound
        let max_result = get_random_value_from_interval(factor, 1.0, interval);
        // Max should be around 15 seconds (10 + 50%)
        assert!(max_result >= Duration::from_secs(14) && max_result <= Duration::from_secs(16));
    }

    // Edge case tests

    #[test]
    fn test_fibonacci_backoff_max_retries_zero() {
        let mut backoff = FibonacciBackoff {
            max_retries: Some(0),
            ..FibonacciBackoff::default()
        };

        // With max_retries = 0, first call should return None
        assert!(backoff.next_backoff().is_none());
    }

    #[test]
    fn test_fibonacci_backoff_max_retries_one() {
        let mut backoff = FibonacciBackoff {
            max_retries: Some(1),
            ..FibonacciBackoff::default()
        };

        // First should succeed
        assert!(backoff.next_backoff().is_some());
        // Second should fail
        assert!(backoff.next_backoff().is_none());
    }

    #[test]
    fn test_fibonacci_backoff_max_duration_zero() {
        let max_dur = Duration::ZERO;
        let mut backoff = FibonacciBackoff {
            max_duration: Some(max_dur),
            randomization_factor: 0.0,
            ..FibonacciBackoff::default()
        };

        let duration = backoff.next_backoff();
        assert!(matches!(duration, Some(d) if d == Duration::ZERO));
    }

    #[test]
    fn test_fibonacci_backoff_reset_after_max_retries() {
        let mut backoff = FibonacciBackoff {
            max_retries: Some(2),
            ..FibonacciBackoff::default()
        };

        assert!(backoff.next_backoff().is_some());
        assert!(backoff.next_backoff().is_some());
        assert!(backoff.next_backoff().is_none());

        // After reset, should work again
        backoff.reset();
        assert!(backoff.next_backoff().is_some());
    }

    #[test]
    fn test_duration_to_nanos_zero() {
        let dur = Duration::ZERO;
        let nanos = duration_to_nanos(dur);
        assert!((nanos - 0.0).abs() < f64::EPSILON);
    }

    #[test]
    fn test_nanos_to_duration_zero() {
        let nanos = 0.0;
        let dur = nanos_to_duration(nanos);
        assert_eq!(dur, Duration::ZERO);
    }

    #[test]
    fn test_duration_to_nanos_large_value() {
        // Test with a very large duration (1 year in seconds)
        let dur = Duration::from_secs(365 * 24 * 60 * 60);
        let nanos = duration_to_nanos(dur);
        let expected = 365.0 * 24.0 * 60.0 * 60.0 * 1_000_000_000.0;
        assert!((nanos - expected).abs() < 1_000_000.0); // Allow small floating point error
    }

    #[test]
    fn test_get_random_value_from_interval_zero_interval() {
        let interval = Duration::ZERO;
        let result = get_random_value_from_interval(0.5, 0.5, interval);
        // With zero interval, result should be zero or very small
        assert!(result <= Duration::from_nanos(100));
    }

    #[test]
    fn test_fibonacci_backoff_builder_none_max_retries() {
        let backoff = FibonacciBackoffBuilder::new().max_retries(None).build();
        assert!(backoff.max_retries.is_none());
    }

    #[test]
    fn test_fibonacci_backoff_many_retries_beyond_array() {
        let mut backoff = FibonacciBackoff {
            randomization_factor: 0.0,
            ..FibonacciBackoff::default()
        };

        // Exhaust all the intervals and then some
        for _ in 0..20 {
            let _ = backoff.next_backoff();
        }

        // Should still return the max interval
        let duration = backoff.next_backoff();
        assert!(matches!(duration, Some(d) if d == Duration::from_millis(300_000)));
    }

    #[test]
    fn test_get_random_value_from_interval_full_randomization() {
        let interval = Duration::from_secs(10);
        let factor = 1.0; // 100% randomization

        // With random = 0, should get 0 (10 - 100%)
        let min_result = get_random_value_from_interval(factor, 0.0, interval);
        assert!(min_result <= Duration::from_secs(1));

        // With random = 1, should get 20 (10 + 100%)
        let max_result = get_random_value_from_interval(factor, 1.0, interval);
        assert!(max_result >= Duration::from_secs(19) && max_result <= Duration::from_secs(21));
    }
}