heliosdb-proxy 0.4.1

HeliosProxy - Intelligent connection router and failover manager for HeliosDB and PostgreSQL
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
//! Adaptive Circuit Breaker Thresholds
//!
//! Automatically adjusts failure thresholds based on historical data
//! to distinguish between normal failure rates and anomalies.

use std::collections::VecDeque;
use std::sync::atomic::{AtomicU32, Ordering};
use std::time::{Duration, Instant};

use parking_lot::RwLock;

/// Rolling statistics calculator
#[derive(Debug)]
pub struct RollingStats {
    /// Data points
    values: RwLock<VecDeque<(Instant, f64)>>,
    /// Window duration
    window: Duration,
    /// Maximum number of data points
    max_points: usize,
}

impl RollingStats {
    /// Create a new rolling statistics calculator
    pub fn new(window: Duration) -> Self {
        Self {
            values: RwLock::new(VecDeque::new()),
            window,
            max_points: 1000,
        }
    }

    /// Create with specific max points
    pub fn with_max_points(window: Duration, max_points: usize) -> Self {
        Self {
            values: RwLock::new(VecDeque::new()),
            window,
            max_points,
        }
    }

    /// Add a data point
    pub fn add(&self, value: f64) {
        let now = Instant::now();
        let mut values = self.values.write();

        // Remove expired entries
        let cutoff = now - self.window;
        while values.front().map(|(t, _)| *t < cutoff).unwrap_or(false) {
            values.pop_front();
        }

        // Enforce max points
        while values.len() >= self.max_points {
            values.pop_front();
        }

        values.push_back((now, value));
    }

    /// Get current average
    pub fn average(&self) -> Option<f64> {
        let now = Instant::now();
        let mut values = self.values.write();

        // Remove expired entries
        let cutoff = now - self.window;
        while values.front().map(|(t, _)| *t < cutoff).unwrap_or(false) {
            values.pop_front();
        }

        if values.is_empty() {
            return None;
        }

        let sum: f64 = values.iter().map(|(_, v)| v).sum();
        Some(sum / values.len() as f64)
    }

    /// Get current standard deviation
    pub fn std_dev(&self) -> Option<f64> {
        let avg = self.average()?;
        let values = self.values.read();

        if values.len() < 2 {
            return None;
        }

        let variance: f64 = values
            .iter()
            .map(|(_, v)| (v - avg).powi(2))
            .sum::<f64>()
            / (values.len() - 1) as f64;

        Some(variance.sqrt())
    }

    /// Get count of data points
    pub fn count(&self) -> usize {
        let now = Instant::now();
        let mut values = self.values.write();

        // Remove expired entries
        let cutoff = now - self.window;
        while values.front().map(|(t, _)| *t < cutoff).unwrap_or(false) {
            values.pop_front();
        }

        values.len()
    }

    /// Get minimum value
    pub fn min(&self) -> Option<f64> {
        let values = self.values.read();
        values.iter().map(|(_, v)| *v).fold(None, |min, v| {
            Some(min.map_or(v, |m: f64| m.min(v)))
        })
    }

    /// Get maximum value
    pub fn max(&self) -> Option<f64> {
        let values = self.values.read();
        values.iter().map(|(_, v)| *v).fold(None, |max, v| {
            Some(max.map_or(v, |m: f64| m.max(v)))
        })
    }

    /// Get percentile value (0.0 - 1.0)
    pub fn percentile(&self, p: f64) -> Option<f64> {
        let values = self.values.read();
        if values.is_empty() {
            return None;
        }

        let mut sorted: Vec<f64> = values.iter().map(|(_, v)| *v).collect();
        sorted.sort_by(|a, b| a.partial_cmp(b).unwrap_or(std::cmp::Ordering::Equal));

        let idx = ((sorted.len() - 1) as f64 * p.clamp(0.0, 1.0)) as usize;
        Some(sorted[idx])
    }

    /// Reset statistics
    pub fn reset(&self) {
        self.values.write().clear();
    }
}

impl Clone for RollingStats {
    fn clone(&self) -> Self {
        Self {
            values: RwLock::new(self.values.read().clone()),
            window: self.window,
            max_points: self.max_points,
        }
    }
}

/// Adaptive threshold calculator
#[derive(Debug)]
pub struct AdaptiveThreshold {
    /// Historical failure rates
    failure_stats: RollingStats,

    /// Base threshold (starting point)
    base_threshold: AtomicU32,

    /// Minimum threshold (floor)
    min_threshold: u32,

    /// Maximum threshold (ceiling)
    max_threshold: u32,

    /// Number of standard deviations for anomaly detection
    sigma_multiplier: f64,

    /// Minimum samples needed before adapting
    min_samples: usize,

    /// Last computed threshold
    cached_threshold: RwLock<Option<u32>>,
}

impl AdaptiveThreshold {
    /// Create a new adaptive threshold calculator
    pub fn new(base_threshold: u32) -> Self {
        Self {
            failure_stats: RollingStats::new(Duration::from_secs(3600)), // 1 hour window
            base_threshold: AtomicU32::new(base_threshold),
            min_threshold: 2,
            max_threshold: 100,
            sigma_multiplier: 3.0, // 3 sigma rule
            min_samples: 10,
            cached_threshold: RwLock::new(None),
        }
    }

    /// Create with custom configuration
    pub fn with_config(
        base_threshold: u32,
        window: Duration,
        min_threshold: u32,
        max_threshold: u32,
        sigma_multiplier: f64,
    ) -> Self {
        Self {
            failure_stats: RollingStats::new(window),
            base_threshold: AtomicU32::new(base_threshold),
            min_threshold,
            max_threshold,
            sigma_multiplier,
            min_samples: 10,
            cached_threshold: RwLock::new(None),
        }
    }

    /// Record a failure count observation
    pub fn record_failures(&self, failure_count: u32) {
        self.failure_stats.add(failure_count as f64);
        // Invalidate cached threshold
        *self.cached_threshold.write() = None;
    }

    /// Compute adaptive threshold
    ///
    /// Uses the formula: threshold = avg + (sigma_multiplier * std_dev)
    /// This means the circuit opens when failures exceed normal by 3 sigma (by default)
    pub fn compute_threshold(&self) -> u32 {
        // Return cached value if available
        if let Some(cached) = *self.cached_threshold.read() {
            return cached;
        }

        let base = self.base_threshold.load(Ordering::SeqCst);

        // Need minimum samples for statistical validity
        if self.failure_stats.count() < self.min_samples {
            return base;
        }

        let threshold = match (self.failure_stats.average(), self.failure_stats.std_dev()) {
            (Some(avg), Some(std)) => {
                let computed = avg + (self.sigma_multiplier * std);
                let computed = computed.round() as u32;

                // Clamp to configured bounds
                computed.clamp(self.min_threshold, self.max_threshold)
            }
            (Some(avg), None) => {
                // Not enough variance, use average + 50%
                let computed = (avg * 1.5).round() as u32;
                computed.clamp(self.min_threshold, self.max_threshold)
            }
            _ => base,
        };

        // Cache the result
        *self.cached_threshold.write() = Some(threshold);

        threshold
    }

    /// Get the base threshold
    pub fn base_threshold(&self) -> u32 {
        self.base_threshold.load(Ordering::SeqCst)
    }

    /// Update base threshold
    pub fn set_base_threshold(&self, threshold: u32) {
        self.base_threshold.store(threshold, Ordering::SeqCst);
        *self.cached_threshold.write() = None;
    }

    /// Get current statistics summary
    pub fn get_stats(&self) -> AdaptiveStats {
        AdaptiveStats {
            base_threshold: self.base_threshold(),
            computed_threshold: self.compute_threshold(),
            sample_count: self.failure_stats.count(),
            average_failures: self.failure_stats.average(),
            std_deviation: self.failure_stats.std_dev(),
            min_observed: self.failure_stats.min(),
            max_observed: self.failure_stats.max(),
        }
    }

    /// Reset statistics
    pub fn reset(&self) {
        self.failure_stats.reset();
        *self.cached_threshold.write() = None;
    }

    /// Check if we have enough data for reliable adaptation
    pub fn is_calibrated(&self) -> bool {
        self.failure_stats.count() >= self.min_samples
    }
}

impl Clone for AdaptiveThreshold {
    fn clone(&self) -> Self {
        Self {
            failure_stats: self.failure_stats.clone(),
            base_threshold: AtomicU32::new(self.base_threshold.load(Ordering::SeqCst)),
            min_threshold: self.min_threshold,
            max_threshold: self.max_threshold,
            sigma_multiplier: self.sigma_multiplier,
            min_samples: self.min_samples,
            cached_threshold: RwLock::new(*self.cached_threshold.read()),
        }
    }
}

/// Statistics about adaptive threshold
#[derive(Debug, Clone)]
pub struct AdaptiveStats {
    pub base_threshold: u32,
    pub computed_threshold: u32,
    pub sample_count: usize,
    pub average_failures: Option<f64>,
    pub std_deviation: Option<f64>,
    pub min_observed: Option<f64>,
    pub max_observed: Option<f64>,
}

impl AdaptiveStats {
    /// Check if threshold was adjusted from base
    pub fn is_adjusted(&self) -> bool {
        self.computed_threshold != self.base_threshold
    }

    /// Get adjustment percentage
    pub fn adjustment_percentage(&self) -> f64 {
        if self.base_threshold == 0 {
            return 0.0;
        }

        ((self.computed_threshold as f64 - self.base_threshold as f64)
            / self.base_threshold as f64)
            * 100.0
    }
}

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

    #[test]
    fn test_rolling_stats_basic() {
        let stats = RollingStats::new(Duration::from_secs(60));

        stats.add(10.0);
        stats.add(20.0);
        stats.add(30.0);

        assert_eq!(stats.count(), 3);
        assert!((stats.average().unwrap() - 20.0).abs() < 0.01);
    }

    #[test]
    fn test_rolling_stats_std_dev() {
        let stats = RollingStats::new(Duration::from_secs(60));

        // Values: 2, 4, 4, 4, 5, 5, 7, 9
        // Mean: 5, StdDev: ~2.14
        for v in [2.0, 4.0, 4.0, 4.0, 5.0, 5.0, 7.0, 9.0] {
            stats.add(v);
        }

        assert!((stats.average().unwrap() - 5.0).abs() < 0.01);
        assert!((stats.std_dev().unwrap() - 2.14).abs() < 0.1);
    }

    #[test]
    fn test_rolling_stats_percentile() {
        let stats = RollingStats::new(Duration::from_secs(60));

        for i in 1..=100 {
            stats.add(i as f64);
        }

        let p50 = stats.percentile(0.5).unwrap();
        assert!((p50 - 50.0).abs() < 1.0);

        let p99 = stats.percentile(0.99).unwrap();
        assert!((p99 - 99.0).abs() < 1.0);
    }

    #[test]
    fn test_adaptive_threshold_basic() {
        let adaptive = AdaptiveThreshold::new(5);

        // Not enough samples, should return base
        assert_eq!(adaptive.compute_threshold(), 5);
        assert!(!adaptive.is_calibrated());
    }

    #[test]
    fn test_adaptive_threshold_with_data() {
        let adaptive = AdaptiveThreshold::with_config(
            5,
            Duration::from_secs(3600),
            2,
            100,
            3.0,
        );

        // Add consistent failure counts
        for _ in 0..20 {
            adaptive.record_failures(3);
        }

        assert!(adaptive.is_calibrated());

        // With low variance, threshold should be close to average
        let threshold = adaptive.compute_threshold();
        assert!(threshold >= 3);
        assert!(threshold <= 10);
    }

    #[test]
    fn test_adaptive_threshold_high_variance() {
        let adaptive = AdaptiveThreshold::with_config(
            5,
            Duration::from_secs(3600),
            2,
            100,
            3.0,
        );

        // Add highly variable failure counts
        for i in 0..20 {
            adaptive.record_failures(i % 10);
        }

        let threshold = adaptive.compute_threshold();
        let stats = adaptive.get_stats();

        // Higher variance should increase threshold
        assert!(threshold > stats.average_failures.unwrap_or(0.0) as u32);
    }

    #[test]
    fn test_adaptive_stats() {
        let adaptive = AdaptiveThreshold::new(10);

        for _ in 0..15 {
            adaptive.record_failures(5);
        }

        let stats = adaptive.get_stats();
        assert_eq!(stats.base_threshold, 10);
        assert_eq!(stats.sample_count, 15);
        assert!(stats.average_failures.is_some());
    }
}