tasker-orchestration 0.1.4

Orchestration system for tasker workflow coordination
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
//! # Backpressure Checker
//!
//! TAS-75 Phase 5: Public API for checking backpressure status.
//!
//! This module provides the public `try_*` API for API handlers to check
//! backpressure conditions. All methods are synchronous and read from caches
//! updated by the background `StatusEvaluator` task.
//!
//! ## Design Principles
//!
//! 1. **Non-blocking**: All methods read from thread-safe caches
//! 2. **No database queries**: Cache-only reads in API hot path
//! 3. **Consistent interface**: Similar to the original `state.rs` methods
//! 4. **Detailed metrics**: Full backpressure metrics for health endpoints

use std::time::Duration;
use tasker_shared::types::web::ApiError;
use tracing::warn;

use super::caches::HealthStatusCaches;
use super::types::{
    BackpressureMetrics, BackpressureStatus, ChannelHealthStatus, QueueDepthStatus,
};

/// Public backpressure checking API
///
/// This struct provides synchronous access to cached health status data.
/// All methods read from caches that are updated by the background `StatusEvaluator`.
///
/// ## Usage
///
/// ```ignore
/// // In API handler
/// let checker = state.backpressure_checker();
///
/// // Check if backpressure is active (for gating API requests)
/// if let Some(error) = checker.try_check_backpressure() {
///     return Err(error);
/// }
///
/// // Get detailed metrics for health endpoint
/// let metrics = checker.get_backpressure_metrics().await;
/// ```
#[derive(Debug, Clone)]
pub struct BackpressureChecker {
    caches: HealthStatusCaches,
    stale_threshold: Duration,
}

impl BackpressureChecker {
    /// Create a new backpressure checker
    ///
    /// # Arguments
    /// * `caches` - Shared health status caches
    /// * `stale_threshold` - Duration after which cached data is considered stale
    pub fn new(caches: HealthStatusCaches, stale_threshold: Duration) -> Self {
        Self {
            caches,
            stale_threshold,
        }
    }

    /// Create a new backpressure checker with default stale threshold (30 seconds)
    pub fn with_default_threshold(caches: HealthStatusCaches) -> Self {
        Self::new(caches, Duration::from_secs(30))
    }

    // =========================================================================
    // Synchronous try_* API (for API hot path)
    // =========================================================================

    /// Check if backpressure is active (synchronous, cache-only)
    ///
    /// Returns `Some(ApiError)` if backpressure is active, `None` if healthy.
    /// This is the primary method for API handlers to check before accepting requests.
    ///
    /// # Returns
    /// - `Some(ApiError::Backpressure { ... })` if backpressure is active
    /// - `None` if the system is healthy and can accept requests
    ///
    /// # Example
    /// ```ignore
    /// if let Some(error) = checker.try_check_backpressure() {
    ///     // Return 503 with Retry-After header
    ///     return Err(error);
    /// }
    /// ```
    pub fn try_check_backpressure(&self) -> Option<ApiError> {
        // Try to get the backpressure status from cache
        // Note: This uses blocking_read since we want synchronous access in the hot path
        let status = match self.caches.backpressure().try_read() {
            Ok(guard) => guard.clone(),
            Err(_) => {
                // Lock contention - return healthy to avoid false positives
                return None;
            }
        };

        if status.active {
            let reason = status.reason.clone().unwrap_or_default();
            let retry_after_secs = status.retry_after_secs.unwrap_or(30);

            Some(ApiError::backpressure(reason, retry_after_secs))
        } else {
            None
        }
    }

    /// Get current queue depth status (synchronous, cache-only)
    ///
    /// Returns the cached queue depth status. If cache access fails,
    /// returns default (healthy) status.
    ///
    /// # Returns
    /// `QueueDepthStatus` with current queue depth information
    pub fn try_get_queue_depth_status(&self) -> QueueDepthStatus {
        match self.caches.queue_status().try_read() {
            Ok(guard) => guard.clone(),
            Err(_) => {
                // Lock contention - return default
                QueueDepthStatus::default()
            }
        }
    }

    /// Get current channel status (synchronous, cache-only)
    ///
    /// Returns the cached channel health status. If cache access fails,
    /// returns default (healthy) status.
    ///
    /// # Returns
    /// `ChannelHealthStatus` with current channel saturation information
    pub fn try_get_channel_status(&self) -> ChannelHealthStatus {
        match self.caches.channel_status().try_read() {
            Ok(guard) => guard.clone(),
            Err(_) => {
                // Lock contention - return default
                ChannelHealthStatus::default()
            }
        }
    }

    /// Check if cached data is stale (synchronous)
    ///
    /// Returns true if the cache hasn't been updated recently.
    /// Useful for health endpoints to report degraded status.
    pub fn try_is_stale(&self) -> bool {
        match self.caches.last_evaluated().try_read() {
            Ok(guard) => match *guard {
                None => true, // Never evaluated = stale
                Some(instant) => instant.elapsed() > self.stale_threshold,
            },
            Err(_) => {
                // Lock contention - assume not stale
                false
            }
        }
    }

    // =========================================================================
    // Async API (for health endpoints and monitoring)
    // =========================================================================

    /// Get detailed backpressure metrics for health endpoint
    ///
    /// This method is async because it acquires read locks on multiple caches.
    /// It's designed for health endpoints, not for the API hot path.
    ///
    /// # Returns
    /// `BackpressureMetrics` with comprehensive health information
    pub async fn get_backpressure_metrics(&self) -> BackpressureMetrics {
        let db = self.caches.get_db_status().await;
        let channel = self.caches.get_channel_status().await;
        let queue = self.caches.get_queue_status().await;
        let bp = self.caches.get_backpressure().await;

        // Check for stale data
        if self.caches.is_stale(self.stale_threshold).await {
            warn!(
                threshold_ms = self.stale_threshold.as_millis(),
                "Health cache data is stale"
            );
        }

        BackpressureMetrics {
            circuit_breaker_open: db.circuit_breaker_open,
            circuit_breaker_failures: db.circuit_breaker_failures,
            command_channel_saturation_percent: channel.command_saturation_percent,
            command_channel_available_capacity: channel.command_available_capacity,
            command_channel_messages_sent: channel.command_messages_sent,
            command_channel_overflow_events: channel.command_overflow_events,
            backpressure_active: bp.active,
            queue_depth_tier: format!("{:?}", queue.tier),
            queue_depth_max: queue.max_depth,
            queue_depth_worst_queue: queue.worst_queue.clone(),
            queue_depths: queue.queue_depths.clone(),
        }
    }

    /// Get the current backpressure status (async)
    ///
    /// Returns the full backpressure status including reason and retry time.
    pub async fn get_backpressure_status(&self) -> BackpressureStatus {
        self.caches.get_backpressure().await
    }

    /// Check if the system is healthy (async)
    ///
    /// Returns true if no backpressure condition is active and data is not stale.
    pub async fn is_healthy(&self) -> bool {
        let bp = self.caches.get_backpressure().await;
        let stale = self.caches.is_stale(self.stale_threshold).await;

        !bp.active && !stale
    }

    /// Get reference to the underlying caches (for advanced use cases)
    pub fn caches(&self) -> &HealthStatusCaches {
        &self.caches
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::health::types::QueueDepthTier;
    use std::collections::HashMap;

    #[tokio::test]
    async fn test_try_check_backpressure_healthy() {
        let caches = HealthStatusCaches::new();
        let checker = BackpressureChecker::with_default_threshold(caches);

        // Default status is healthy (no backpressure)
        let result = checker.try_check_backpressure();
        assert!(result.is_none());
    }

    #[tokio::test]
    async fn test_try_check_backpressure_active() {
        let caches = HealthStatusCaches::new();

        // Set backpressure active
        let bp_status = BackpressureStatus {
            active: true,
            reason: Some("Test backpressure".to_string()),
            retry_after_secs: Some(30),
            source: None,
        };
        caches.set_backpressure(bp_status).await;

        let checker = BackpressureChecker::with_default_threshold(caches);

        let result = checker.try_check_backpressure();
        assert!(result.is_some());

        if let Some(ApiError::Backpressure { reason, .. }) = result {
            assert_eq!(reason, "Test backpressure");
        } else {
            panic!("Expected Backpressure error");
        }
    }

    #[tokio::test]
    async fn test_try_get_queue_depth_status() {
        let caches = HealthStatusCaches::new();

        let mut depths = HashMap::new();
        depths.insert("test_queue".to_string(), 2500);

        let queue_status = QueueDepthStatus {
            tier: QueueDepthTier::Warning,
            max_depth: 2500,
            worst_queue: "test_queue".to_string(),
            queue_depths: depths,
        };
        caches.set_queue_status(queue_status).await;

        let checker = BackpressureChecker::with_default_threshold(caches);

        let result = checker.try_get_queue_depth_status();
        assert_eq!(result.tier, QueueDepthTier::Warning);
        assert_eq!(result.max_depth, 2500);
        assert_eq!(result.worst_queue, "test_queue");
    }

    #[tokio::test]
    async fn test_try_get_channel_status() {
        let caches = HealthStatusCaches::new();

        let channel_status = ChannelHealthStatus {
            evaluated: true,
            command_saturation_percent: 75.0,
            command_available_capacity: 250,
            command_messages_sent: 500,
            command_overflow_events: 0,
            is_saturated: false,
            is_critical: false,
        };
        caches.set_channel_status(channel_status).await;

        let checker = BackpressureChecker::with_default_threshold(caches);

        let result = checker.try_get_channel_status();
        assert_eq!(result.command_saturation_percent, 75.0);
        assert_eq!(result.command_available_capacity, 250);
    }

    #[tokio::test]
    async fn test_try_is_stale_never_evaluated() {
        let caches = HealthStatusCaches::new();
        let checker = BackpressureChecker::with_default_threshold(caches);

        // Never evaluated = stale
        assert!(checker.try_is_stale());
    }

    #[tokio::test]
    async fn test_try_is_stale_recent_evaluation() {
        let caches = HealthStatusCaches::new();
        caches.mark_evaluated().await;

        let checker = BackpressureChecker::with_default_threshold(caches);

        // Just evaluated = not stale
        assert!(!checker.try_is_stale());
    }

    #[tokio::test]
    async fn test_get_backpressure_metrics() {
        let caches = HealthStatusCaches::new();

        // Set up some status
        let channel_status = ChannelHealthStatus {
            evaluated: true,
            command_saturation_percent: 50.0,
            command_available_capacity: 500,
            command_messages_sent: 1000,
            command_overflow_events: 2,
            is_saturated: false,
            is_critical: false,
        };
        caches.set_channel_status(channel_status).await;
        caches.mark_evaluated().await;

        let checker = BackpressureChecker::with_default_threshold(caches);

        let metrics = checker.get_backpressure_metrics().await;
        assert_eq!(metrics.command_channel_saturation_percent, 50.0);
        assert_eq!(metrics.command_channel_available_capacity, 500);
        assert_eq!(metrics.command_channel_messages_sent, 1000);
        assert!(!metrics.backpressure_active);
    }

    #[tokio::test]
    async fn test_is_healthy() {
        let caches = HealthStatusCaches::new();
        caches.mark_evaluated().await;

        let checker = BackpressureChecker::with_default_threshold(caches);

        // Default status + recently evaluated = healthy
        assert!(checker.is_healthy().await);
    }

    #[tokio::test]
    async fn test_is_healthy_with_backpressure() {
        let caches = HealthStatusCaches::new();

        let bp_status = BackpressureStatus {
            active: true,
            reason: Some("Test".to_string()),
            retry_after_secs: Some(30),
            source: None,
        };
        caches.set_backpressure(bp_status).await;
        caches.mark_evaluated().await;

        let checker = BackpressureChecker::with_default_threshold(caches);

        // Backpressure active = not healthy
        assert!(!checker.is_healthy().await);
    }

    #[tokio::test]
    async fn test_is_healthy_stale_data() {
        let caches = HealthStatusCaches::new();
        // Don't mark evaluated - data is stale

        let checker = BackpressureChecker::new(caches, Duration::from_millis(1));

        // Stale data = not healthy
        assert!(!checker.is_healthy().await);
    }

    #[test]
    fn test_custom_stale_threshold() {
        let caches = HealthStatusCaches::new();
        let checker = BackpressureChecker::new(caches, Duration::from_secs(60));

        // Should use the custom threshold
        assert_eq!(checker.stale_threshold, Duration::from_secs(60));
    }
}