chie-core 0.2.0

Core protocol logic for CHIE Protocol
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
//! Graceful degradation under resource pressure.
//!
//! This module implements strategies for gracefully degrading service quality when
//! resources (CPU, memory, disk, bandwidth) are under pressure, rather than failing
//! catastrophically. It allows the system to continue operating at reduced capacity
//! while maintaining critical functionality.
//!
//! # Example
//!
//! ```rust
//! use chie_core::degradation::{DegradationManager, ResourcePressure, ServiceDegradationLevel};
//!
//! # async fn example() {
//! let mut manager = DegradationManager::new();
//!
//! // Report resource pressure
//! manager.update_pressure(ResourcePressure {
//!     cpu_usage: 0.95,
//!     memory_usage: 0.85,
//!     disk_usage: 0.90,
//!     bandwidth_usage: 0.80,
//! });
//!
//! // Get current degradation level
//! let level = manager.current_level();
//! match level {
//!     ServiceDegradationLevel::Normal => {
//!         // Operate normally
//!     }
//!     ServiceDegradationLevel::LightDegradation => {
//!         // Reduce non-critical features
//!     }
//!     ServiceDegradationLevel::ModerateDegradation => {
//!         // Focus on core functionality
//!     }
//!     ServiceDegradationLevel::SevereDegradation => {
//!         // Minimal operations only
//!     }
//! }
//!
//! // Check if specific features should be disabled
//! if manager.should_disable_prefetching() {
//!     // Disable chunk prefetching
//! }
//! if manager.should_reduce_cache_size() {
//!     // Reduce cache memory usage
//! }
//! # }
//! ```

use serde::{Deserialize, Serialize};
use std::time::{Duration, Instant};

/// Current resource pressure measurements.
#[derive(Debug, Clone, Copy)]
pub struct ResourcePressure {
    /// CPU usage (0.0 to 1.0).
    pub cpu_usage: f64,
    /// Memory usage (0.0 to 1.0).
    pub memory_usage: f64,
    /// Disk usage (0.0 to 1.0).
    pub disk_usage: f64,
    /// Bandwidth usage (0.0 to 1.0).
    pub bandwidth_usage: f64,
}

impl ResourcePressure {
    /// Calculate overall pressure score (0.0 to 1.0).
    #[must_use]
    #[inline]
    pub fn overall_score(&self) -> f64 {
        // Weighted average (disk and memory are more critical)
        (self.cpu_usage * 0.2
            + self.memory_usage * 0.3
            + self.disk_usage * 0.3
            + self.bandwidth_usage * 0.2)
            .clamp(0.0, 1.0)
    }

    /// Check if any resource is critically high.
    #[must_use]
    #[inline]
    pub fn has_critical_resource(&self) -> bool {
        self.cpu_usage > 0.95
            || self.memory_usage > 0.95
            || self.disk_usage > 0.95
            || self.bandwidth_usage > 0.95
    }
}

impl Default for ResourcePressure {
    fn default() -> Self {
        Self {
            cpu_usage: 0.0,
            memory_usage: 0.0,
            disk_usage: 0.0,
            bandwidth_usage: 0.0,
        }
    }
}

/// Service degradation levels.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
pub enum ServiceDegradationLevel {
    /// Normal operation - all features enabled.
    Normal = 0,
    /// Light degradation - reduce non-critical features.
    LightDegradation = 1,
    /// Moderate degradation - focus on core functionality.
    ModerateDegradation = 2,
    /// Severe degradation - minimal operations only.
    SevereDegradation = 3,
}

impl ServiceDegradationLevel {
    /// Get degradation level from pressure score.
    #[must_use]
    #[inline]
    pub fn from_pressure_score(score: f64) -> Self {
        if score < 0.70 {
            Self::Normal
        } else if score < 0.80 {
            Self::LightDegradation
        } else if score < 0.90 {
            Self::ModerateDegradation
        } else {
            Self::SevereDegradation
        }
    }

    /// Get description of this degradation level.
    #[must_use]
    #[inline]
    pub const fn description(&self) -> &'static str {
        match self {
            Self::Normal => "Operating normally",
            Self::LightDegradation => "Light resource pressure - reducing non-critical features",
            Self::ModerateDegradation => "Moderate resource pressure - core functionality only",
            Self::SevereDegradation => "Severe resource pressure - minimal operations",
        }
    }

    /// Check if this is a degraded state.
    #[must_use]
    #[inline]
    pub const fn is_degraded(&self) -> bool {
        !matches!(self, Self::Normal)
    }
}

/// Actions to take during degradation.
#[derive(Debug, Clone, Copy)]
pub struct DegradationActions {
    /// Disable chunk prefetching.
    pub disable_prefetching: bool,
    /// Reduce cache size.
    pub reduce_cache_size: bool,
    /// Disable analytics collection.
    pub disable_analytics: bool,
    /// Throttle bandwidth.
    pub throttle_bandwidth: bool,
    /// Pause garbage collection.
    pub pause_gc: bool,
    /// Disable backup operations.
    pub disable_backups: bool,
    /// Reduce connection pool size.
    pub reduce_connection_pool: bool,
    /// Reject new content pinning.
    pub reject_new_pins: bool,
}

impl DegradationActions {
    /// Get actions for a given degradation level.
    #[must_use]
    pub const fn for_level(level: ServiceDegradationLevel) -> Self {
        match level {
            ServiceDegradationLevel::Normal => Self {
                disable_prefetching: false,
                reduce_cache_size: false,
                disable_analytics: false,
                throttle_bandwidth: false,
                pause_gc: false,
                disable_backups: false,
                reduce_connection_pool: false,
                reject_new_pins: false,
            },
            ServiceDegradationLevel::LightDegradation => Self {
                disable_prefetching: true,
                reduce_cache_size: true,
                disable_analytics: false,
                throttle_bandwidth: false,
                pause_gc: false,
                disable_backups: false,
                reduce_connection_pool: false,
                reject_new_pins: false,
            },
            ServiceDegradationLevel::ModerateDegradation => Self {
                disable_prefetching: true,
                reduce_cache_size: true,
                disable_analytics: true,
                throttle_bandwidth: true,
                pause_gc: true,
                disable_backups: true,
                reduce_connection_pool: true,
                reject_new_pins: false,
            },
            ServiceDegradationLevel::SevereDegradation => Self {
                disable_prefetching: true,
                reduce_cache_size: true,
                disable_analytics: true,
                throttle_bandwidth: true,
                pause_gc: true,
                disable_backups: true,
                reduce_connection_pool: true,
                reject_new_pins: true,
            },
        }
    }
}

/// Manages graceful degradation based on resource pressure.
pub struct DegradationManager {
    current_level: ServiceDegradationLevel,
    current_pressure: ResourcePressure,
    last_update: Instant,
    pressure_history: Vec<(Instant, f64)>,
    hysteresis_duration: Duration,
}

impl DegradationManager {
    /// Create a new degradation manager.
    #[must_use]
    pub fn new() -> Self {
        Self {
            current_level: ServiceDegradationLevel::Normal,
            current_pressure: ResourcePressure::default(),
            last_update: Instant::now(),
            pressure_history: Vec::new(),
            hysteresis_duration: Duration::from_secs(60), // 1 minute hysteresis
        }
    }

    /// Update resource pressure and recalculate degradation level.
    pub fn update_pressure(&mut self, pressure: ResourcePressure) {
        self.current_pressure = pressure;
        self.last_update = Instant::now();

        let score = pressure.overall_score();
        self.pressure_history.push((Instant::now(), score));

        // Keep only last 5 minutes of history
        let cutoff = Instant::now() - Duration::from_secs(300);
        self.pressure_history.retain(|(t, _)| *t > cutoff);

        // Calculate new level with hysteresis to prevent flapping
        let new_level = ServiceDegradationLevel::from_pressure_score(score);

        // Only change level if sustained for hysteresis duration
        if new_level != self.current_level {
            let sustained = self.is_level_sustained(new_level);
            if sustained {
                self.current_level = new_level;
            }
        }
    }

    /// Check if a degradation level has been sustained.
    fn is_level_sustained(&self, level: ServiceDegradationLevel) -> bool {
        let cutoff = Instant::now() - self.hysteresis_duration;
        let recent_scores: Vec<f64> = self
            .pressure_history
            .iter()
            .filter(|(t, _)| *t > cutoff)
            .map(|(_, s)| *s)
            .collect();

        if recent_scores.is_empty() {
            return false;
        }

        // Check if all recent scores match this level
        recent_scores
            .iter()
            .all(|&score| ServiceDegradationLevel::from_pressure_score(score) == level)
    }

    /// Get current degradation level.
    #[must_use]
    #[inline]
    pub const fn current_level(&self) -> ServiceDegradationLevel {
        self.current_level
    }

    /// Get current resource pressure.
    #[must_use]
    #[inline]
    pub const fn current_pressure(&self) -> &ResourcePressure {
        &self.current_pressure
    }

    /// Get recommended actions for current degradation level.
    #[must_use]
    pub const fn get_actions(&self) -> DegradationActions {
        DegradationActions::for_level(self.current_level)
    }

    /// Check if prefetching should be disabled.
    #[must_use]
    #[inline]
    pub fn should_disable_prefetching(&self) -> bool {
        self.get_actions().disable_prefetching
    }

    /// Check if cache size should be reduced.
    #[must_use]
    #[inline]
    pub fn should_reduce_cache_size(&self) -> bool {
        self.get_actions().reduce_cache_size
    }

    /// Check if analytics should be disabled.
    #[must_use]
    #[inline]
    pub fn should_disable_analytics(&self) -> bool {
        self.get_actions().disable_analytics
    }

    /// Check if bandwidth should be throttled.
    #[must_use]
    #[inline]
    pub fn should_throttle_bandwidth(&self) -> bool {
        self.get_actions().throttle_bandwidth
    }

    /// Check if garbage collection should be paused.
    #[must_use]
    #[inline]
    pub fn should_pause_gc(&self) -> bool {
        self.get_actions().pause_gc
    }

    /// Check if new content pins should be rejected.
    #[must_use]
    #[inline]
    pub fn should_reject_new_pins(&self) -> bool {
        self.get_actions().reject_new_pins
    }

    /// Get time since last pressure update.
    #[must_use]
    pub fn time_since_update(&self) -> Duration {
        Instant::now().duration_since(self.last_update)
    }

    /// Get average pressure score over the last duration.
    #[must_use]
    pub fn average_pressure_score(&self, duration: Duration) -> Option<f64> {
        let cutoff = Instant::now() - duration;
        let scores: Vec<f64> = self
            .pressure_history
            .iter()
            .filter(|(t, _)| *t > cutoff)
            .map(|(_, s)| *s)
            .collect();

        if scores.is_empty() {
            None
        } else {
            Some(scores.iter().sum::<f64>() / scores.len() as f64)
        }
    }
}

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

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

    #[test]
    fn test_resource_pressure_overall_score() {
        let pressure = ResourcePressure {
            cpu_usage: 0.5,
            memory_usage: 0.6,
            disk_usage: 0.7,
            bandwidth_usage: 0.4,
        };

        let score = pressure.overall_score();
        assert!(score > 0.5 && score < 0.7);
    }

    #[test]
    fn test_resource_pressure_critical() {
        let pressure = ResourcePressure {
            cpu_usage: 0.96,
            memory_usage: 0.5,
            disk_usage: 0.5,
            bandwidth_usage: 0.5,
        };

        assert!(pressure.has_critical_resource());

        let normal = ResourcePressure {
            cpu_usage: 0.5,
            memory_usage: 0.5,
            disk_usage: 0.5,
            bandwidth_usage: 0.5,
        };

        assert!(!normal.has_critical_resource());
    }

    #[test]
    fn test_degradation_level_from_score() {
        assert_eq!(
            ServiceDegradationLevel::from_pressure_score(0.5),
            ServiceDegradationLevel::Normal
        );
        assert_eq!(
            ServiceDegradationLevel::from_pressure_score(0.75),
            ServiceDegradationLevel::LightDegradation
        );
        assert_eq!(
            ServiceDegradationLevel::from_pressure_score(0.85),
            ServiceDegradationLevel::ModerateDegradation
        );
        assert_eq!(
            ServiceDegradationLevel::from_pressure_score(0.95),
            ServiceDegradationLevel::SevereDegradation
        );
    }

    #[test]
    fn test_degradation_actions() {
        let normal_actions = DegradationActions::for_level(ServiceDegradationLevel::Normal);
        assert!(!normal_actions.disable_prefetching);
        assert!(!normal_actions.reject_new_pins);

        let severe_actions =
            DegradationActions::for_level(ServiceDegradationLevel::SevereDegradation);
        assert!(severe_actions.disable_prefetching);
        assert!(severe_actions.reject_new_pins);
    }

    #[test]
    fn test_degradation_manager_update() {
        let mut manager = DegradationManager::new();

        assert_eq!(manager.current_level(), ServiceDegradationLevel::Normal);

        // Update with high pressure
        manager.update_pressure(ResourcePressure {
            cpu_usage: 0.95,
            memory_usage: 0.90,
            disk_usage: 0.92,
            bandwidth_usage: 0.88,
        });

        // Level won't change immediately due to hysteresis
        // but pressure is recorded
        assert!(manager.current_pressure().overall_score() > 0.9);
    }

    #[test]
    fn test_degradation_manager_helpers() {
        let mut manager = DegradationManager::new();

        // Normal level
        assert!(!manager.should_disable_prefetching());
        assert!(!manager.should_reject_new_pins());

        // Force severe degradation
        manager.current_level = ServiceDegradationLevel::SevereDegradation;
        assert!(manager.should_disable_prefetching());
        assert!(manager.should_reject_new_pins());
        assert!(manager.should_pause_gc());
    }

    #[test]
    fn test_average_pressure_score() {
        let mut manager = DegradationManager::new();

        manager.update_pressure(ResourcePressure {
            cpu_usage: 0.5,
            memory_usage: 0.5,
            disk_usage: 0.5,
            bandwidth_usage: 0.5,
        });

        let avg = manager.average_pressure_score(Duration::from_secs(60));
        assert!(avg.is_some());
        assert!((avg.unwrap() - 0.5).abs() < 0.1);
    }
}