-
Notifications
You must be signed in to change notification settings - Fork 345
Expand file tree
/
Copy patht2_self_optimizing.rs
More file actions
644 lines (567 loc) · 20.3 KB
/
t2_self_optimizing.rs
File metadata and controls
644 lines (567 loc) · 20.3 KB
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
//! # Tier 2: Self-Optimizing Software and Workflows
//!
//! Agents that monitor agents.
//!
//! ## What Changes
//! - Systems watch structure and timing, not just outputs
//! - Learning adjusts coordination patterns
//! - Reflex gates prevent cascading failures
//!
//! ## Why This Matters
//! - Software becomes self-stabilizing
//! - Less ops, fewer incidents
//! - Debugging shifts from logs to structural witnesses
//!
//! This is a natural extension of RuVector as connective tissue.
use std::collections::{HashMap, VecDeque};
use std::time::{Duration, Instant};
/// A software component being monitored
#[derive(Clone, Debug, Hash, PartialEq, Eq)]
pub struct ComponentId(pub String);
/// Structural observation about a component
#[derive(Clone, Debug)]
pub struct StructuralEvent {
pub timestamp_us: u64,
pub component: ComponentId,
pub event_type: StructuralEventType,
pub latency_us: Option<u64>,
pub error: Option<String>,
}
#[derive(Clone, Debug)]
pub enum StructuralEventType {
/// Request received
RequestStart { request_id: u64 },
/// Request completed
RequestEnd { request_id: u64, success: bool },
/// Component called another
Call {
target: ComponentId,
request_id: u64,
},
/// Component received call result
CallReturn {
source: ComponentId,
request_id: u64,
success: bool,
},
/// Resource usage spike
ResourceSpike { resource: String, value: f32 },
/// Queue depth changed
QueueDepth { depth: usize },
/// Circuit breaker state change
CircuitBreaker { state: CircuitState },
}
#[derive(Clone, Debug, PartialEq)]
pub enum CircuitState {
Closed,
Open,
HalfOpen,
}
/// Witness log for structural debugging
#[derive(Clone, Debug)]
pub struct StructuralWitness {
pub timestamp: u64,
pub trigger: String,
pub component_states: HashMap<ComponentId, ComponentState>,
pub causal_chain: Vec<(ComponentId, StructuralEventType)>,
pub decision: String,
pub action_taken: Option<String>,
}
#[derive(Clone, Debug)]
pub struct ComponentState {
pub latency_p99_us: u64,
pub error_rate: f32,
pub queue_depth: usize,
pub circuit_state: CircuitState,
}
/// Coordination pattern learned over time
#[derive(Clone, Debug)]
pub struct CoordinationPattern {
pub name: String,
pub participants: Vec<ComponentId>,
pub expected_sequence: Vec<(ComponentId, ComponentId)>,
pub expected_latency_us: u64,
pub tolerance: f32,
pub occurrences: u64,
}
/// Reflex gate to prevent cascading failures
pub struct CascadeReflex {
pub trigger_threshold: f32, // Error rate threshold
pub propagation_window_us: u64,
pub recent_errors: VecDeque<(u64, ComponentId)>,
pub circuit_breakers: HashMap<ComponentId, CircuitBreaker>,
}
pub struct CircuitBreaker {
pub state: CircuitState,
pub failure_count: u32,
pub failure_threshold: u32,
pub reset_timeout_us: u64,
pub last_failure: u64,
}
impl CircuitBreaker {
pub fn new(threshold: u32, timeout_us: u64) -> Self {
Self {
state: CircuitState::Closed,
failure_count: 0,
failure_threshold: threshold,
reset_timeout_us: timeout_us,
last_failure: 0,
}
}
pub fn record_failure(&mut self, timestamp: u64) {
self.failure_count += 1;
self.last_failure = timestamp;
if self.failure_count >= self.failure_threshold {
self.state = CircuitState::Open;
}
}
pub fn record_success(&mut self) {
if self.state == CircuitState::HalfOpen {
self.state = CircuitState::Closed;
self.failure_count = 0;
}
}
pub fn check(&mut self, timestamp: u64) -> bool {
match self.state {
CircuitState::Closed => true,
CircuitState::Open => {
if timestamp - self.last_failure > self.reset_timeout_us {
self.state = CircuitState::HalfOpen;
true
} else {
false
}
}
CircuitState::HalfOpen => true,
}
}
}
impl CascadeReflex {
pub fn new(threshold: f32, window_us: u64) -> Self {
Self {
trigger_threshold: threshold,
propagation_window_us: window_us,
recent_errors: VecDeque::new(),
circuit_breakers: HashMap::new(),
}
}
/// Check for cascading failure pattern
pub fn check(&mut self, event: &StructuralEvent) -> Option<StructuralWitness> {
// Track errors
if matches!(&event.event_type, StructuralEventType::RequestEnd { success, .. } if !success)
{
self.recent_errors
.push_back((event.timestamp_us, event.component.clone()));
// Record in circuit breaker
self.circuit_breakers
.entry(event.component.clone())
.or_insert_with(|| CircuitBreaker::new(5, 30_000_000))
.record_failure(event.timestamp_us);
}
// Clean old errors
let cutoff = event
.timestamp_us
.saturating_sub(self.propagation_window_us);
while self
.recent_errors
.front()
.map(|e| e.0 < cutoff)
.unwrap_or(false)
{
self.recent_errors.pop_front();
}
// Count affected components
let mut affected: HashMap<ComponentId, u32> = HashMap::new();
for (_, comp) in &self.recent_errors {
*affected.entry(comp.clone()).or_default() += 1;
}
// Detect cascade (multiple components failing together)
if affected.len() >= 3 {
let witness = StructuralWitness {
timestamp: event.timestamp_us,
trigger: "Cascade detected".to_string(),
component_states: affected
.keys()
.map(|c| {
(
c.clone(),
ComponentState {
latency_p99_us: 0,
error_rate: *affected.get(c).unwrap_or(&0) as f32 / 10.0,
queue_depth: 0,
circuit_state: self
.circuit_breakers
.get(c)
.map(|cb| cb.state.clone())
.unwrap_or(CircuitState::Closed),
},
)
})
.collect(),
causal_chain: self
.recent_errors
.iter()
.map(|(_, c)| {
(
c.clone(),
StructuralEventType::RequestEnd {
request_id: 0,
success: false,
},
)
})
.collect(),
decision: format!("Open circuit breakers for {} components", affected.len()),
action_taken: Some("SHED_LOAD".to_string()),
};
// Open all affected circuit breakers
for comp in affected.keys() {
if let Some(cb) = self.circuit_breakers.get_mut(comp) {
cb.state = CircuitState::Open;
}
}
return Some(witness);
}
None
}
}
/// Pattern learner that discovers coordination patterns
pub struct PatternLearner {
pub observed_sequences: HashMap<String, CoordinationPattern>,
pub current_traces: HashMap<u64, Vec<(u64, ComponentId, ComponentId)>>,
pub learning_rate: f32,
}
impl PatternLearner {
pub fn new() -> Self {
Self {
observed_sequences: HashMap::new(),
current_traces: HashMap::new(),
learning_rate: 0.1,
}
}
/// Observe a call between components
pub fn observe_call(
&mut self,
caller: ComponentId,
callee: ComponentId,
request_id: u64,
timestamp: u64,
) {
self.current_traces
.entry(request_id)
.or_default()
.push((timestamp, caller, callee));
}
/// Complete a trace and learn from it
pub fn complete_trace(&mut self, request_id: u64) -> Option<String> {
let trace = self.current_traces.remove(&request_id)?;
if trace.len() < 2 {
return None;
}
// Create pattern signature
let participants: Vec<ComponentId> = trace
.iter()
.flat_map(|(_, from, to)| vec![from.clone(), to.clone()])
.collect();
let sequence: Vec<(ComponentId, ComponentId)> = trace
.iter()
.map(|(_, from, to)| (from.clone(), to.clone()))
.collect();
let total_latency =
trace.last().map(|l| l.0).unwrap_or(0) - trace.first().map(|f| f.0).unwrap_or(0);
let signature = format!("{:?}", sequence);
// Update or create pattern
let next_pattern_id = self.observed_sequences.len();
let pattern = self
.observed_sequences
.entry(signature.clone())
.or_insert_with(|| CoordinationPattern {
name: format!("Pattern_{}", next_pattern_id),
participants: participants.clone(),
expected_sequence: sequence.clone(),
expected_latency_us: total_latency,
tolerance: 0.5,
occurrences: 0,
});
pattern.occurrences += 1;
pattern.expected_latency_us =
((1.0 - self.learning_rate) * pattern.expected_latency_us as f32
+ self.learning_rate * total_latency as f32) as u64;
Some(pattern.name.clone())
}
/// Check if a trace violates learned patterns
pub fn check_violation(&self, trace: &[(u64, ComponentId, ComponentId)]) -> Option<String> {
if trace.len() < 2 {
return None;
}
let sequence: Vec<(ComponentId, ComponentId)> = trace
.iter()
.map(|(_, from, to)| (from.clone(), to.clone()))
.collect();
let signature = format!("{:?}", sequence);
if let Some(pattern) = self.observed_sequences.get(&signature) {
let latency =
trace.last().map(|l| l.0).unwrap_or(0) - trace.first().map(|f| f.0).unwrap_or(0);
let deviation = (latency as f32 - pattern.expected_latency_us as f32).abs()
/ pattern.expected_latency_us as f32;
if deviation > pattern.tolerance {
return Some(format!(
"{} latency deviation: expected {}us, got {}us ({:.0}%)",
pattern.name,
pattern.expected_latency_us,
latency,
deviation * 100.0
));
}
}
None
}
}
/// Main self-optimizing system
pub struct SelfOptimizingSystem {
/// Reflex gate for cascade prevention
pub cascade_reflex: CascadeReflex,
/// Pattern learner for coordination
pub pattern_learner: PatternLearner,
/// Component latency trackers
pub latency_trackers: HashMap<ComponentId, VecDeque<u64>>,
/// Witness log for debugging
pub witnesses: Vec<StructuralWitness>,
/// Optimization actions taken
pub optimizations: Vec<String>,
}
impl SelfOptimizingSystem {
pub fn new() -> Self {
Self {
cascade_reflex: CascadeReflex::new(0.1, 1_000_000),
pattern_learner: PatternLearner::new(),
latency_trackers: HashMap::new(),
witnesses: Vec::new(),
optimizations: Vec::new(),
}
}
/// Process a structural event
pub fn observe(&mut self, event: StructuralEvent) -> Option<StructuralWitness> {
// 1. Check reflex (cascade prevention)
if let Some(witness) = self.cascade_reflex.check(&event) {
self.witnesses.push(witness.clone());
return Some(witness);
}
// 2. Track patterns
match &event.event_type {
StructuralEventType::Call { target, request_id } => {
self.pattern_learner.observe_call(
event.component.clone(),
target.clone(),
*request_id,
event.timestamp_us,
);
}
StructuralEventType::RequestEnd {
request_id,
success: true,
} => {
if let Some(pattern_name) = self.pattern_learner.complete_trace(*request_id) {
// Pattern learned/reinforced
if self
.pattern_learner
.observed_sequences
.get(&pattern_name)
.map(|p| p.occurrences == 10)
.unwrap_or(false)
{
self.optimizations
.push(format!("Learned pattern: {}", pattern_name));
}
}
}
_ => {}
}
// 3. Track latency
if let Some(latency) = event.latency_us {
self.latency_trackers
.entry(event.component.clone())
.or_insert_with(|| VecDeque::with_capacity(100))
.push_back(latency);
let tracker = self.latency_trackers.get_mut(&event.component).unwrap();
if tracker.len() > 100 {
tracker.pop_front();
}
// Check for latency regression
if tracker.len() >= 10 {
let recent: Vec<_> = tracker.iter().rev().take(10).collect();
let avg: u64 = recent.iter().copied().sum::<u64>() / 10;
let old_avg: u64 = tracker.iter().take(10).sum::<u64>() / 10;
if avg > old_avg * 2 {
let witness = StructuralWitness {
timestamp: event.timestamp_us,
trigger: format!("Latency regression: {:?}", event.component),
component_states: HashMap::new(),
causal_chain: vec![],
decision: "Investigate latency spike".to_string(),
action_taken: None,
};
self.witnesses.push(witness.clone());
return Some(witness);
}
}
}
None
}
/// Get system health summary
pub fn health_summary(&self) -> SystemHealth {
let open_circuits: Vec<_> = self
.cascade_reflex
.circuit_breakers
.iter()
.filter(|(_, cb)| cb.state == CircuitState::Open)
.map(|(id, _)| id.clone())
.collect();
SystemHealth {
components_monitored: self.latency_trackers.len(),
patterns_learned: self.pattern_learner.observed_sequences.len(),
open_circuit_breakers: open_circuits,
recent_witnesses: self.witnesses.len(),
optimizations_applied: self.optimizations.len(),
}
}
}
#[derive(Debug)]
pub struct SystemHealth {
pub components_monitored: usize,
pub patterns_learned: usize,
pub open_circuit_breakers: Vec<ComponentId>,
pub recent_witnesses: usize,
pub optimizations_applied: usize,
}
fn main() {
println!("=== Tier 2: Self-Optimizing Software and Workflows ===\n");
let mut system = SelfOptimizingSystem::new();
// Simulate normal operation - learning coordination patterns
println!("Learning phase - observing normal coordination...");
for req in 0..50 {
let base_time = req * 10_000;
// Simulate: API -> Auth -> DB pattern
system.observe(StructuralEvent {
timestamp_us: base_time,
component: ComponentId("api".into()),
event_type: StructuralEventType::RequestStart { request_id: req },
latency_us: None,
error: None,
});
system.observe(StructuralEvent {
timestamp_us: base_time + 100,
component: ComponentId("api".into()),
event_type: StructuralEventType::Call {
target: ComponentId("auth".into()),
request_id: req,
},
latency_us: None,
error: None,
});
system.observe(StructuralEvent {
timestamp_us: base_time + 500,
component: ComponentId("auth".into()),
event_type: StructuralEventType::Call {
target: ComponentId("db".into()),
request_id: req,
},
latency_us: None,
error: None,
});
system.observe(StructuralEvent {
timestamp_us: base_time + 2000,
component: ComponentId("api".into()),
event_type: StructuralEventType::RequestEnd {
request_id: req,
success: true,
},
latency_us: Some(2000),
error: None,
});
}
let health = system.health_summary();
println!(" Patterns learned: {}", health.patterns_learned);
println!(" Components monitored: {}", health.components_monitored);
println!(" Optimizations: {:?}", system.optimizations);
// Simulate cascade failure
println!("\nSimulating cascade failure...");
for req in 50..60 {
let base_time = 500_000 + req * 1_000;
// Multiple components fail together
for comp in ["api", "auth", "db", "cache"] {
system.observe(StructuralEvent {
timestamp_us: base_time + 100,
component: ComponentId(comp.into()),
event_type: StructuralEventType::RequestEnd {
request_id: req,
success: false,
},
latency_us: Some(50_000), // Slow failure
error: Some("Connection timeout".into()),
});
}
}
// Check for cascade detection
if let Some(last_witness) = system.witnesses.last() {
println!("\n CASCADE DETECTED!");
println!(" Trigger: {}", last_witness.trigger);
println!(" Decision: {}", last_witness.decision);
println!(" Action: {:?}", last_witness.action_taken);
}
let health = system.health_summary();
println!(
"\n Circuit breakers opened: {:?}",
health.open_circuit_breakers
);
println!(" Witnesses logged: {}", health.recent_witnesses);
println!("\n=== Key Benefits ===");
println!("- Systems watch structure and timing, not just outputs");
println!("- Reflex gates prevent cascading failures");
println!("- Structural witnesses replace log diving");
println!("- Patterns learned automatically for anomaly detection");
println!("\nRuVector as connective tissue for self-stabilizing software.");
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_circuit_breaker() {
let mut cb = CircuitBreaker::new(3, 1000);
assert!(cb.check(0));
cb.record_failure(0);
cb.record_failure(1);
assert!(cb.check(2));
cb.record_failure(2);
assert!(!cb.check(3)); // Now open
assert!(cb.check(1004)); // After timeout, half-open
}
#[test]
fn test_pattern_learning() {
let mut learner = PatternLearner::new();
learner.observe_call(ComponentId("a".into()), ComponentId("b".into()), 1, 0);
learner.observe_call(ComponentId("b".into()), ComponentId("c".into()), 1, 100);
let pattern = learner.complete_trace(1);
assert!(pattern.is_some());
}
#[test]
fn test_cascade_detection() {
let mut system = SelfOptimizingSystem::new();
// Create cascade of failures
for i in 0..5 {
for comp in ["a", "b", "c", "d"] {
system.observe(StructuralEvent {
timestamp_us: i * 100,
component: ComponentId(comp.into()),
event_type: StructuralEventType::RequestEnd {
request_id: i,
success: false,
},
latency_us: None,
error: None,
});
}
}
assert!(!system.witnesses.is_empty());
}
}