heliosdb-proxy 0.4.2

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
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
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
//! Circuit Breaker Implementation
//!
//! Core circuit breaker logic implementing the Closed -> Open -> Half-Open -> Closed
//! state machine for protecting against cascading failures.

use std::sync::atomic::{AtomicU32, AtomicU64, AtomicU8, Ordering};
use std::sync::Arc;
use std::time::{Duration, Instant};

use parking_lot::RwLock;

use super::config::CircuitBreakerConfig;
use super::sliding_counter::SlidingWindowCounter;
use super::state::{CircuitBreakerListener, CircuitEvent, CircuitState, TransitionReason};

/// Error returned when circuit is open
#[derive(Debug, Clone)]
pub struct CircuitOpen {
    /// Node this circuit protects
    pub node_id: String,
    /// Time until circuit may transition to half-open
    pub retry_after: Duration,
    /// Current failure count
    pub failure_count: u32,
    /// Last error that caused circuit to open
    pub last_error: Option<String>,
}

impl CircuitOpen {
    /// Create a new CircuitOpen error
    pub fn new(node_id: impl Into<String>, retry_after: Duration) -> Self {
        Self {
            node_id: node_id.into(),
            retry_after,
            failure_count: 0,
            last_error: None,
        }
    }

    /// Add failure count
    pub fn with_failure_count(mut self, count: u32) -> Self {
        self.failure_count = count;
        self
    }

    /// Add last error
    pub fn with_last_error(mut self, error: impl Into<String>) -> Self {
        self.last_error = Some(error.into());
        self
    }

    /// Generate LLM-friendly error message
    pub fn to_llm_message(&self) -> String {
        format!(
            r#"{{"error":"circuit_breaker_open","node":"{}","retry_after_ms":{},"failure_count":{},"message":"Backend node is temporarily unavailable. Please retry after {} milliseconds."}}"#,
            self.node_id,
            self.retry_after.as_millis(),
            self.failure_count,
            self.retry_after.as_millis()
        )
    }
}

impl std::fmt::Display for CircuitOpen {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(
            f,
            "Circuit open for node '{}', retry after {:?}",
            self.node_id, self.retry_after
        )
    }
}

impl std::error::Error for CircuitOpen {}

/// Guard returned from allow_request() that tracks request success/failure
pub struct RequestGuard {
    breaker: Arc<CircuitBreakerInner>,
    is_probe: bool,
    start_time: Instant,
    completed: bool,
}

impl RequestGuard {
    fn new(breaker: Arc<CircuitBreakerInner>) -> Self {
        Self {
            breaker,
            is_probe: false,
            start_time: Instant::now(),
            completed: false,
        }
    }

    fn new_probe(breaker: Arc<CircuitBreakerInner>) -> Self {
        breaker.active_probes.fetch_add(1, Ordering::SeqCst);
        Self {
            breaker,
            is_probe: true,
            start_time: Instant::now(),
            completed: false,
        }
    }

    /// Mark request as successful
    pub fn success(mut self) {
        self.completed = true;
        let duration = self.start_time.elapsed();

        if self.is_probe {
            self.breaker.active_probes.fetch_sub(1, Ordering::SeqCst);
            self.breaker.notify_probe_result(true, duration);
        }

        self.breaker.record_success_internal();
    }

    /// Mark request as failed
    pub fn failure(mut self, error: &str) {
        self.completed = true;
        let duration = self.start_time.elapsed();

        if self.is_probe {
            self.breaker.active_probes.fetch_sub(1, Ordering::SeqCst);
            self.breaker.notify_probe_result(false, duration);
        }

        self.breaker.record_failure_internal(error);
    }

    /// Get elapsed time since request started
    pub fn elapsed(&self) -> Duration {
        self.start_time.elapsed()
    }

    /// Check if this is a probe request (half-open state)
    pub fn is_probe(&self) -> bool {
        self.is_probe
    }
}

impl Drop for RequestGuard {
    fn drop(&mut self) {
        // If not explicitly completed, count as failure
        if !self.completed {
            if self.is_probe {
                self.breaker.active_probes.fetch_sub(1, Ordering::SeqCst);
            }
            self.breaker.record_failure_internal("request abandoned");
        }
    }
}

/// Internal circuit breaker state
struct CircuitBreakerInner {
    /// Node this circuit protects
    node_id: String,

    /// Current state (atomic u8)
    state: AtomicU8,

    /// Failure counter (rolling window)
    failure_counter: SlidingWindowCounter,

    /// Success counter (for half-open validation)
    success_counter: AtomicU32,

    /// Time when circuit opened (nanos since start)
    opened_at: AtomicU64,

    /// Start time for relative timestamps
    start_time: Instant,

    /// Number of active probes in half-open
    active_probes: AtomicU32,

    /// Last error message
    last_error: RwLock<Option<String>>,

    /// Configuration
    config: RwLock<CircuitBreakerConfig>,

    /// Event listeners
    listeners: RwLock<Vec<Arc<dyn CircuitBreakerListener>>>,

    /// Transition history (limited size)
    history: RwLock<Vec<super::state::StateTransition>>,

    /// Total times circuit opened
    open_count: AtomicU64,

    /// Total failures recorded
    total_failures: AtomicU64,

    /// Total successes recorded
    total_successes: AtomicU64,
}

impl CircuitBreakerInner {
    fn get_state(&self) -> CircuitState {
        CircuitState::from_u8(self.state.load(Ordering::SeqCst)).unwrap_or(CircuitState::Closed)
    }

    fn now_nanos(&self) -> u64 {
        self.start_time.elapsed().as_nanos() as u64
    }

    fn should_try_half_open(&self) -> bool {
        let config = self.config.read();
        let opened_at = self.opened_at.load(Ordering::SeqCst);
        let elapsed_nanos = self.now_nanos().saturating_sub(opened_at);
        let elapsed = Duration::from_nanos(elapsed_nanos);
        elapsed >= config.cooldown
    }

    fn time_until_half_open(&self) -> Duration {
        let config = self.config.read();
        let opened_at = self.opened_at.load(Ordering::SeqCst);
        let elapsed_nanos = self.now_nanos().saturating_sub(opened_at);
        let elapsed = Duration::from_nanos(elapsed_nanos);
        config.cooldown.saturating_sub(elapsed)
    }

    fn can_probe(&self) -> bool {
        let config = self.config.read();
        self.active_probes.load(Ordering::SeqCst) < config.half_open_max_probes
    }

    fn transition_to_open(&self, reason: TransitionReason) {
        let prev = self
            .state
            .swap(CircuitState::Open as u8, Ordering::SeqCst);
        if prev != CircuitState::Open as u8 {
            self.opened_at.store(self.now_nanos(), Ordering::SeqCst);
            self.open_count.fetch_add(1, Ordering::SeqCst);

            let from = CircuitState::from_u8(prev).unwrap_or(CircuitState::Closed);
            self.record_transition(from, CircuitState::Open, reason.clone());

            self.notify_opened(reason);
        }
    }

    fn transition_to_half_open(&self) {
        let prev_state = self.get_state();
        self.state
            .store(CircuitState::HalfOpen as u8, Ordering::SeqCst);
        self.success_counter.store(0, Ordering::SeqCst);

        let config = self.config.read();
        let reason = TransitionReason::CooldownElapsed {
            cooldown: config.cooldown,
        };

        self.record_transition(prev_state, CircuitState::HalfOpen, reason);
        self.notify_half_opened();
    }

    fn transition_to_closed(&self, reason: TransitionReason) {
        let prev_state = self.get_state();
        self.state
            .store(CircuitState::Closed as u8, Ordering::SeqCst);
        self.failure_counter.reset();

        self.record_transition(prev_state, CircuitState::Closed, reason.clone());
        self.notify_closed(reason);
    }

    fn record_success_internal(&self) {
        self.total_successes.fetch_add(1, Ordering::SeqCst);

        match self.get_state() {
            CircuitState::Closed => {
                // Success in closed state - could reset failure counter
                // (optional behavior, we keep failures for window duration)
            }
            CircuitState::HalfOpen => {
                let config = self.config.read();
                let count = self.success_counter.fetch_add(1, Ordering::SeqCst) + 1;
                if count >= config.half_open_success_threshold {
                    drop(config);
                    self.transition_to_closed(TransitionReason::ProbeSucceeded {
                        success_count: count,
                        threshold: count,
                    });
                }
            }
            CircuitState::Open => {
                // Should not happen - requests blocked in open state
            }
        }
    }

    fn record_failure_internal(&self, error: &str) {
        self.total_failures.fetch_add(1, Ordering::SeqCst);
        *self.last_error.write() = Some(error.to_string());

        match self.get_state() {
            CircuitState::Closed => {
                let count = self.failure_counter.increment();
                let config = self.config.read();
                let threshold = config.failure_threshold;

                self.notify_failure_recorded(error, count);

                if count >= threshold {
                    drop(config);
                    self.transition_to_open(TransitionReason::FailureThresholdExceeded {
                        failure_count: count,
                        threshold,
                    });
                }
            }
            CircuitState::HalfOpen => {
                // Any failure in half-open goes back to open
                self.transition_to_open(TransitionReason::ProbeFailed {
                    error: error.to_string(),
                });
            }
            CircuitState::Open => {
                // Already open, just update timestamp
                self.opened_at.store(self.now_nanos(), Ordering::SeqCst);
            }
        }
    }

    fn record_transition(
        &self,
        from: CircuitState,
        to: CircuitState,
        reason: TransitionReason,
    ) {
        let transition = super::state::StateTransition::new(from, to, reason);
        let mut history = self.history.write();
        history.push(transition);
        // Keep last 100 transitions
        if history.len() > 100 {
            history.remove(0);
        }
    }

    fn notify_opened(&self, reason: TransitionReason) {
        let event = CircuitEvent::Opened {
            node_id: self.node_id.clone(),
            reason,
            failure_count: self.failure_counter.count(),
        };
        self.notify_listeners(event);
    }

    fn notify_half_opened(&self) {
        let config = self.config.read();
        let event = CircuitEvent::HalfOpened {
            node_id: self.node_id.clone(),
            cooldown_elapsed: config.cooldown,
        };
        drop(config);
        self.notify_listeners(event);
    }

    fn notify_closed(&self, reason: TransitionReason) {
        // Calculate recovery time from when circuit was opened
        let opened_at = self.opened_at.load(Ordering::SeqCst);
        let recovery_nanos = self.now_nanos().saturating_sub(opened_at);

        let event = CircuitEvent::Closed {
            node_id: self.node_id.clone(),
            reason,
            recovery_time: Duration::from_nanos(recovery_nanos),
        };
        self.notify_listeners(event);
    }

    fn notify_failure_recorded(&self, error: &str, count: u32) {
        let event = CircuitEvent::FailureRecorded {
            node_id: self.node_id.clone(),
            error: error.to_string(),
            failure_count: count,
        };
        self.notify_listeners(event);
    }

    fn notify_probe_result(&self, success: bool, duration: Duration) {
        let event = CircuitEvent::ProbeResult {
            node_id: self.node_id.clone(),
            success,
            duration,
        };
        self.notify_listeners(event);
    }

    fn notify_listeners(&self, event: CircuitEvent) {
        let listeners = self.listeners.read();
        for listener in listeners.iter() {
            listener.on_event(event.clone());
        }
    }
}

/// Circuit Breaker
///
/// Protects against cascading failures by tracking errors and temporarily
/// blocking requests to unhealthy nodes.
#[derive(Clone)]
pub struct CircuitBreaker {
    inner: Arc<CircuitBreakerInner>,
}

impl CircuitBreaker {
    /// Create a new circuit breaker
    pub fn new(node_id: impl Into<String>, config: CircuitBreakerConfig) -> Self {
        let failure_window = config.failure_window;
        Self {
            inner: Arc::new(CircuitBreakerInner {
                node_id: node_id.into(),
                state: AtomicU8::new(CircuitState::Closed as u8),
                failure_counter: SlidingWindowCounter::new(failure_window),
                success_counter: AtomicU32::new(0),
                opened_at: AtomicU64::new(0),
                start_time: Instant::now(),
                active_probes: AtomicU32::new(0),
                last_error: RwLock::new(None),
                config: RwLock::new(config),
                listeners: RwLock::new(Vec::new()),
                history: RwLock::new(Vec::new()),
                open_count: AtomicU64::new(0),
                total_failures: AtomicU64::new(0),
                total_successes: AtomicU64::new(0),
            }),
        }
    }

    /// Get the node ID this circuit protects
    pub fn node_id(&self) -> &str {
        &self.inner.node_id
    }

    /// Get current circuit state
    pub fn get_state(&self) -> CircuitState {
        self.inner.get_state()
    }

    /// Try to get permission for a request
    pub fn allow_request(&self) -> Result<RequestGuard, CircuitOpen> {
        match self.inner.get_state() {
            CircuitState::Closed => Ok(RequestGuard::new(Arc::clone(&self.inner))),
            CircuitState::Open => {
                if self.inner.should_try_half_open() {
                    self.inner.transition_to_half_open();
                    Ok(RequestGuard::new_probe(Arc::clone(&self.inner)))
                } else {
                    Err(CircuitOpen::new(
                        &self.inner.node_id,
                        self.inner.time_until_half_open(),
                    )
                    .with_failure_count(self.inner.failure_counter.count())
                    .with_last_error(
                        self.inner
                            .last_error
                            .read()
                            .clone()
                            .unwrap_or_default(),
                    ))
                }
            }
            CircuitState::HalfOpen => {
                if self.inner.can_probe() {
                    Ok(RequestGuard::new_probe(Arc::clone(&self.inner)))
                } else {
                    Err(CircuitOpen::new(&self.inner.node_id, Duration::from_millis(100))
                        .with_failure_count(self.inner.failure_counter.count()))
                }
            }
        }
    }

    /// Record a success (called internally by RequestGuard)
    pub fn record_success(&self) {
        self.inner.record_success_internal();
    }

    /// Record a failure directly (bypassing guard pattern)
    pub fn record_failure_direct(&self) {
        self.inner.record_failure_internal("direct failure");
    }

    /// Record a failure with error message directly
    pub fn record_failure(&self, error: &str) {
        self.inner.record_failure_internal(error);
    }

    /// Force circuit to open state
    pub fn force_open(&self, admin: Option<&str>) {
        self.inner.transition_to_open(TransitionReason::ManualForce {
            admin: admin.map(String::from),
        });
    }

    /// Force circuit to closed state
    pub fn force_close(&self, admin: Option<&str>) {
        self.inner.transition_to_closed(TransitionReason::ManualForce {
            admin: admin.map(String::from),
        });
    }

    /// Reset circuit breaker (close and clear counters)
    pub fn reset(&self) {
        self.inner.failure_counter.reset();
        self.inner.success_counter.store(0, Ordering::SeqCst);
        self.inner.transition_to_closed(TransitionReason::Reset);
    }

    /// Update configuration
    pub fn update_config(&self, config: CircuitBreakerConfig) {
        *self.inner.config.write() = config;
    }

    /// Get current configuration
    pub fn config(&self) -> CircuitBreakerConfig {
        self.inner.config.read().clone()
    }

    /// Add event listener
    pub fn add_listener(&self, listener: Arc<dyn CircuitBreakerListener>) {
        self.inner.listeners.write().push(listener);
    }

    /// Get current failure count
    pub fn failure_count(&self) -> u32 {
        self.inner.failure_counter.count()
    }

    /// Get total times circuit has opened
    pub fn open_count(&self) -> u64 {
        self.inner.open_count.load(Ordering::SeqCst)
    }

    /// Get total failures recorded
    pub fn total_failures(&self) -> u64 {
        self.inner.total_failures.load(Ordering::SeqCst)
    }

    /// Get total successes recorded
    pub fn total_successes(&self) -> u64 {
        self.inner.total_successes.load(Ordering::SeqCst)
    }

    /// Get last error message
    pub fn last_error(&self) -> Option<String> {
        self.inner.last_error.read().clone()
    }

    /// Get time until circuit may transition to half-open (if open)
    pub fn time_until_half_open(&self) -> Option<Duration> {
        if self.get_state() == CircuitState::Open {
            Some(self.inner.time_until_half_open())
        } else {
            None
        }
    }

    /// Get transition history
    pub fn get_history(&self) -> Vec<super::state::StateTransition> {
        self.inner.history.read().clone()
    }
}

impl std::fmt::Debug for CircuitBreaker {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("CircuitBreaker")
            .field("node_id", &self.inner.node_id)
            .field("state", &self.get_state())
            .field("failure_count", &self.failure_count())
            .finish()
    }
}

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

    #[test]
    fn test_circuit_breaker_new() {
        let config = CircuitBreakerConfig::default();
        let breaker = CircuitBreaker::new("test-node", config);

        assert_eq!(breaker.node_id(), "test-node");
        assert_eq!(breaker.get_state(), CircuitState::Closed);
        assert_eq!(breaker.failure_count(), 0);
    }

    #[test]
    fn test_circuit_breaker_allow_request_closed() {
        let config = CircuitBreakerConfig::default();
        let breaker = CircuitBreaker::new("test-node", config);

        let guard = breaker.allow_request().expect("should allow");
        assert!(!guard.is_probe());
        guard.success();

        assert_eq!(breaker.total_successes(), 1);
    }

    #[test]
    fn test_circuit_breaker_open_on_failures() {
        let config = CircuitBreakerConfig::builder()
            .failure_threshold(3)
            .build();
        let breaker = CircuitBreaker::new("test-node", config);

        breaker.record_failure("error 1");
        breaker.record_failure("error 2");
        assert_eq!(breaker.get_state(), CircuitState::Closed);

        breaker.record_failure("error 3");
        assert_eq!(breaker.get_state(), CircuitState::Open);
    }

    #[test]
    fn test_circuit_open_error() {
        let err = CircuitOpen::new("node-1", Duration::from_secs(10))
            .with_failure_count(5)
            .with_last_error("connection timeout");

        assert_eq!(err.node_id, "node-1");
        assert_eq!(err.failure_count, 5);
        assert_eq!(err.last_error, Some("connection timeout".to_string()));

        let msg = err.to_llm_message();
        assert!(msg.contains("circuit_breaker_open"));
        assert!(msg.contains("node-1"));
    }

    #[test]
    fn test_force_open_close() {
        let config = CircuitBreakerConfig::default();
        let breaker = CircuitBreaker::new("test-node", config);

        breaker.force_open(Some("admin"));
        assert_eq!(breaker.get_state(), CircuitState::Open);

        breaker.force_close(Some("admin"));
        assert_eq!(breaker.get_state(), CircuitState::Closed);
    }

    #[test]
    fn test_reset() {
        let config = CircuitBreakerConfig::builder()
            .failure_threshold(2)
            .build();
        let breaker = CircuitBreaker::new("test-node", config);

        breaker.record_failure("error 1");
        breaker.record_failure("error 2");
        assert_eq!(breaker.get_state(), CircuitState::Open);

        breaker.reset();
        assert_eq!(breaker.get_state(), CircuitState::Closed);
        assert_eq!(breaker.failure_count(), 0);
    }
}