mockforge-chaos 0.3.21

Chaos engineering features for MockForge - fault injection and resilience testing
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
use anyhow::Result;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::sync::Arc;
use tokio::sync::RwLock;

/// State representation for RL agent
#[derive(Debug, Clone, Serialize, Deserialize, Hash, Eq, PartialEq)]
pub struct SystemState {
    pub error_rate: u8,         // 0-100
    pub latency_level: u8,      // 0-100
    pub cpu_usage: u8,          // 0-100
    pub memory_usage: u8,       // 0-100
    pub active_failures: u8,    // Number of active failures
    pub service_health: String, // "healthy", "degraded", "critical"
}

/// Remediation action
#[derive(Debug, Clone, Serialize, Deserialize, Hash, Eq, PartialEq)]
pub enum RemediationAction {
    RestartService,
    ScaleUp(u32),
    ScaleDown(u32),
    ClearCache,
    RollbackDeployment,
    EnableCircuitBreaker,
    DisableRateLimiting,
    RestrictTraffic,
    NoAction,
}

/// Q-Learning parameters
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct QLearningConfig {
    pub learning_rate: f64,     // Alpha: 0.0 - 1.0
    pub discount_factor: f64,   // Gamma: 0.0 - 1.0
    pub exploration_rate: f64,  // Epsilon: 0.0 - 1.0
    pub exploration_decay: f64, // Epsilon decay rate
    pub min_exploration: f64,   // Minimum epsilon
}

impl Default for QLearningConfig {
    fn default() -> Self {
        Self {
            learning_rate: 0.1,
            discount_factor: 0.95,
            exploration_rate: 1.0,
            exploration_decay: 0.995,
            min_exploration: 0.01,
        }
    }
}

/// Q-Table entry
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct QValue {
    pub value: f64,
    pub visit_count: u64,
}

/// Reinforcement Learning Agent
pub struct RLAgent {
    q_table: Arc<RwLock<HashMap<(SystemState, RemediationAction), QValue>>>,
    config: QLearningConfig,
    current_epsilon: f64,
}

impl RLAgent {
    pub fn new(config: QLearningConfig) -> Self {
        Self {
            q_table: Arc::new(RwLock::new(HashMap::new())),
            current_epsilon: config.exploration_rate,
            config,
        }
    }

    /// Select action using epsilon-greedy policy
    pub async fn select_action(&self, state: &SystemState) -> RemediationAction {
        if rand::random::<f64>() < self.current_epsilon {
            // Explore: random action
            self.random_action()
        } else {
            // Exploit: best known action
            self.best_action(state).await
        }
    }

    /// Get best action for given state
    async fn best_action(&self, state: &SystemState) -> RemediationAction {
        let q_table = self.q_table.read().await;
        let actions = self.possible_actions();

        let mut best_action = RemediationAction::NoAction;
        let mut best_value = f64::NEG_INFINITY;

        for action in actions {
            let key = (state.clone(), action.clone());
            let value = q_table.get(&key).map(|q| q.value).unwrap_or(0.0);

            if value > best_value {
                best_value = value;
                best_action = action;
            }
        }

        best_action
    }

    /// Get random action (for exploration)
    fn random_action(&self) -> RemediationAction {
        let actions = self.possible_actions();
        use rand::Rng;
        let mut rng = rand::rng();
        let idx = rng.random_range(0..actions.len());
        actions[idx].clone()
    }

    /// Get all possible actions
    fn possible_actions(&self) -> Vec<RemediationAction> {
        vec![
            RemediationAction::RestartService,
            RemediationAction::ScaleUp(2),
            RemediationAction::ScaleUp(4),
            RemediationAction::ScaleDown(2),
            RemediationAction::ClearCache,
            RemediationAction::RollbackDeployment,
            RemediationAction::EnableCircuitBreaker,
            RemediationAction::DisableRateLimiting,
            RemediationAction::RestrictTraffic,
            RemediationAction::NoAction,
        ]
    }

    /// Update Q-value based on observed outcome
    pub async fn update(
        &mut self,
        state: &SystemState,
        action: &RemediationAction,
        reward: f64,
        next_state: &SystemState,
    ) {
        let mut q_table = self.q_table.write().await;

        // Get current Q-value
        let key = (state.clone(), action.clone());
        let current_q = q_table.get(&key).map(|q| q.value).unwrap_or(0.0);

        // Get max Q-value for next state
        let actions = self.possible_actions();
        let max_next_q = actions
            .iter()
            .map(|a| {
                let next_key = (next_state.clone(), a.clone());
                q_table.get(&next_key).map(|q| q.value).unwrap_or(0.0)
            })
            .fold(f64::NEG_INFINITY, f64::max);

        // Q-learning update: Q(s,a) = Q(s,a) + α[r + γ·max Q(s',a') - Q(s,a)]
        let new_q = current_q
            + self.config.learning_rate
                * (reward + self.config.discount_factor * max_next_q - current_q);

        // Update Q-table
        q_table
            .entry(key)
            .and_modify(|q| {
                q.value = new_q;
                q.visit_count += 1;
            })
            .or_insert(QValue {
                value: new_q,
                visit_count: 1,
            });

        // Decay exploration rate
        self.current_epsilon =
            (self.current_epsilon * self.config.exploration_decay).max(self.config.min_exploration);
    }

    /// Calculate reward based on outcome
    pub fn calculate_reward(&self, before: &SystemState, after: &SystemState) -> f64 {
        let mut reward = 0.0;

        // Reward for reducing error rate
        reward += (before.error_rate as f64 - after.error_rate as f64) * 2.0;

        // Reward for reducing latency
        reward += (before.latency_level as f64 - after.latency_level as f64) * 1.5;

        // Reward for reducing CPU usage
        reward += (before.cpu_usage as f64 - after.cpu_usage as f64) * 0.5;

        // Reward for reducing active failures
        reward += (before.active_failures as f64 - after.active_failures as f64) * 5.0;

        // Health state bonus
        reward += match (before.service_health.as_str(), after.service_health.as_str()) {
            ("critical", "healthy") => 50.0,
            ("critical", "degraded") => 25.0,
            ("degraded", "healthy") => 20.0,
            ("healthy", "degraded") => -30.0,
            ("healthy", "critical") => -50.0,
            ("degraded", "critical") => -40.0,
            _ => 0.0,
        };

        reward
    }

    /// Get policy statistics
    pub async fn get_stats(&self) -> HashMap<String, serde_json::Value> {
        let q_table = self.q_table.read().await;

        let mut stats = HashMap::new();
        stats.insert("q_table_size".to_string(), serde_json::json!(q_table.len()));
        stats.insert("epsilon".to_string(), serde_json::json!(self.current_epsilon));

        // Calculate average Q-value
        let avg_q: f64 = if q_table.is_empty() {
            0.0
        } else {
            q_table.values().map(|q| q.value).sum::<f64>() / q_table.len() as f64
        };
        stats.insert("avg_q_value".to_string(), serde_json::json!(avg_q));

        // Most visited state-action pairs
        let mut visited: Vec<_> = q_table.iter().collect();
        visited.sort_by_key(|(_, q)| std::cmp::Reverse(q.visit_count));

        let top_pairs: Vec<_> = visited
            .iter()
            .take(10)
            .map(|((state, action), q)| {
                serde_json::json!({
                    "state": state,
                    "action": action,
                    "q_value": q.value,
                    "visits": q.visit_count,
                })
            })
            .collect();

        stats.insert("top_state_actions".to_string(), serde_json::json!(top_pairs));

        stats
    }

    /// Save Q-table to disk
    pub async fn save_model(&self, path: &str) -> Result<()> {
        let q_table = self.q_table.read().await;
        let data = serde_json::to_string_pretty(&*q_table)?;
        tokio::fs::write(path, data).await?;
        Ok(())
    }

    /// Load Q-table from disk
    pub async fn load_model(&mut self, path: &str) -> Result<()> {
        let data = tokio::fs::read_to_string(path).await?;
        let loaded_table: HashMap<(SystemState, RemediationAction), QValue> =
            serde_json::from_str(&data)?;

        let mut q_table = self.q_table.write().await;
        *q_table = loaded_table;

        Ok(())
    }
}

/// Adaptive Risk Assessment Engine
pub struct AdaptiveRiskAssessor {
    risk_history: Arc<RwLock<Vec<RiskAssessment>>>,
    rl_agent: Arc<RwLock<RLAgent>>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RiskAssessment {
    pub timestamp: chrono::DateTime<chrono::Utc>,
    pub state: SystemState,
    pub risk_level: f64, // 0.0 - 1.0
    pub recommended_actions: Vec<RemediationAction>,
    pub confidence: f64, // 0.0 - 1.0
}

impl AdaptiveRiskAssessor {
    pub fn new(rl_agent: Arc<RwLock<RLAgent>>) -> Self {
        Self {
            risk_history: Arc::new(RwLock::new(Vec::new())),
            rl_agent,
        }
    }

    /// Assess risk for current system state
    pub async fn assess_risk(&self, state: &SystemState) -> RiskAssessment {
        let mut risk_level = 0.0;

        // Factor in various metrics
        risk_level += state.error_rate as f64 / 100.0 * 0.3;
        risk_level += state.latency_level as f64 / 100.0 * 0.2;
        risk_level += state.cpu_usage as f64 / 100.0 * 0.15;
        risk_level += state.memory_usage as f64 / 100.0 * 0.15;
        risk_level += state.active_failures as f64 / 10.0 * 0.2;

        // Health state impact
        risk_level += match state.service_health.as_str() {
            "critical" => 0.4,
            "degraded" => 0.2,
            _ => 0.0,
        };

        risk_level = risk_level.min(1.0);

        // Get recommended actions from RL agent
        let agent = self.rl_agent.read().await;
        let action = agent.best_action(state).await;

        // Calculate confidence based on Q-table visit counts
        let q_table = agent.q_table.read().await;
        let key = (state.clone(), action.clone());
        let confidence = q_table
            .get(&key)
            .map(|q| (q.visit_count as f64 / 100.0).min(1.0))
            .unwrap_or(0.1);

        let assessment = RiskAssessment {
            timestamp: chrono::Utc::now(),
            state: state.clone(),
            risk_level,
            recommended_actions: vec![action],
            confidence,
        };

        // Store in history
        let mut history = self.risk_history.write().await;
        history.push(assessment.clone());

        // Keep only last 1000 assessments
        if history.len() > 1000 {
            let excess = history.len() - 1000;
            history.drain(0..excess);
        }

        assessment
    }

    /// Get risk trend over time
    pub async fn get_risk_trend(&self) -> Vec<(chrono::DateTime<chrono::Utc>, f64)> {
        let history = self.risk_history.read().await;
        history.iter().map(|a| (a.timestamp, a.risk_level)).collect()
    }
}

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

    #[tokio::test]
    async fn test_rl_agent_learning() {
        let config = QLearningConfig::default();
        let mut agent = RLAgent::new(config);

        let state = SystemState {
            error_rate: 50,
            latency_level: 60,
            cpu_usage: 80,
            memory_usage: 70,
            active_failures: 3,
            service_health: "degraded".to_string(),
        };

        let action = RemediationAction::RestartService;

        let next_state = SystemState {
            error_rate: 10,
            latency_level: 20,
            cpu_usage: 40,
            memory_usage: 50,
            active_failures: 0,
            service_health: "healthy".to_string(),
        };

        let reward = agent.calculate_reward(&state, &next_state);
        agent.update(&state, &action, reward, &next_state).await;

        // Verify Q-value was updated
        let stats = agent.get_stats().await;
        assert!(stats.contains_key("q_table_size"));
    }

    #[tokio::test]
    async fn test_risk_assessment() {
        let config = QLearningConfig::default();
        let agent = Arc::new(RwLock::new(RLAgent::new(config)));
        let assessor = AdaptiveRiskAssessor::new(agent);

        let state = SystemState {
            error_rate: 75,
            latency_level: 80,
            cpu_usage: 90,
            memory_usage: 85,
            active_failures: 5,
            service_health: "critical".to_string(),
        };

        let assessment = assessor.assess_risk(&state).await;

        assert!(assessment.risk_level > 0.5);
        assert!(!assessment.recommended_actions.is_empty());
    }
}