-
Notifications
You must be signed in to change notification settings - Fork 36
Expand file tree
/
Copy pathstore.rs
More file actions
212 lines (183 loc) · 6.35 KB
/
store.rs
File metadata and controls
212 lines (183 loc) · 6.35 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
// Thread-safe metrics store with dirty tracking.
use crate::{AgentStatus, MetricEvent, PipelineTrace, SystemSnapshot};
use parking_lot::RwLock;
use std::collections::{HashMap, VecDeque};
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::Arc;
use tokio::sync::broadcast;
pub struct MetricsStore {
agents: RwLock<HashMap<String, AgentStatus>>,
traces: RwLock<VecDeque<PipelineTrace>>,
snapshots: RwLock<VecDeque<SystemSnapshot>>,
agents_dirty: AtomicBool,
traces_dirty: AtomicBool,
snapshots_dirty: AtomicBool,
broadcast_tx: RwLock<Option<broadcast::Sender<String>>>,
}
// Ring buffer caps
const MAX_TRACES: usize = 200;
const MAX_SNAPSHOTS: usize = 300;
impl MetricsStore {
pub fn new() -> Arc<Self> {
Arc::new(Self {
agents: RwLock::new(HashMap::new()),
traces: RwLock::new(VecDeque::new()),
snapshots: RwLock::new(VecDeque::new()),
agents_dirty: AtomicBool::new(false),
traces_dirty: AtomicBool::new(false),
snapshots_dirty: AtomicBool::new(false),
broadcast_tx: RwLock::new(None),
})
}
pub fn init_broadcast(&self, tx: broadcast::Sender<String>) {
*self.broadcast_tx.write() = Some(tx);
}
pub fn subscribe(&self) -> Option<broadcast::Receiver<String>> {
self.broadcast_tx.read().as_ref().map(|tx| tx.subscribe())
}
fn broadcast_event(&self, event: &MetricEvent) {
if let Some(ref tx) = *self.broadcast_tx.read() {
if let Ok(json) = serde_json::to_string(event) {
let _ = tx.send(json);
}
}
}
pub fn record_agent(&self, status: AgentStatus) {
self.broadcast_event(&MetricEvent::AgentStatusChanged(status.clone()));
self.agents.write().insert(status.id.clone(), status);
self.agents_dirty.store(true, Ordering::Release);
}
pub fn push_trace(&self, trace: PipelineTrace) {
self.broadcast_event(&MetricEvent::PipelineCompleted(trace.clone()));
let mut buf = self.traces.write();
if buf.len() >= MAX_TRACES {
buf.pop_front();
}
buf.push_back(trace);
self.traces_dirty.store(true, Ordering::Release);
}
pub fn push_snapshot(&self, snap: SystemSnapshot) {
self.broadcast_event(&MetricEvent::SystemSnapshot(snap.clone()));
let mut buf = self.snapshots.write();
if buf.len() >= MAX_SNAPSHOTS {
buf.pop_front();
}
buf.push_back(snap);
self.snapshots_dirty.store(true, Ordering::Release);
}
pub fn take_agents_if_dirty(&self) -> Option<Vec<AgentStatus>> {
if self.agents_dirty.swap(false, Ordering::AcqRel) {
Some(self.agents.read().values().cloned().collect())
} else {
None
}
}
pub fn take_traces_if_dirty(&self) -> Option<Vec<PipelineTrace>> {
if self.traces_dirty.swap(false, Ordering::AcqRel) {
Some(self.traces.read().iter().cloned().collect())
} else {
None
}
}
pub fn take_snapshots_if_dirty(&self) -> Option<Vec<SystemSnapshot>> {
if self.snapshots_dirty.swap(false, Ordering::AcqRel) {
Some(self.snapshots.read().iter().cloned().collect())
} else {
None
}
}
pub fn get_agents(&self) -> Vec<AgentStatus> {
self.agents.read().values().cloned().collect()
}
pub fn get_latest_snapshot(&self) -> Option<SystemSnapshot> {
self.snapshots.read().back().cloned()
}
pub fn get_traces(&self, n: usize) -> Vec<PipelineTrace> {
let buf = self.traces.read();
buf.iter().rev().take(n).rev().cloned().collect()
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::types::{unix_ms, AgentState};
fn sample_agent(id: &str, state: AgentState) -> AgentStatus {
AgentStatus {
id: id.into(),
name: id.into(),
status: state,
last_seen_ms: unix_ms(),
}
}
fn sample_snapshot() -> SystemSnapshot {
SystemSnapshot {
cpu_pct: 0.45,
memory_pct: 0.60,
gpu_pct: 0.0,
pipeline_latency_p50_ms: 120.0,
uptime_secs: 30,
timestamp_ms: unix_ms(),
}
}
#[test]
fn dirty_flag_cleared_on_read() {
let store = MetricsStore::new();
assert!(store.take_agents_if_dirty().is_none());
store.record_agent(sample_agent("n1", AgentState::Active));
let agents = store.take_agents_if_dirty().unwrap();
assert_eq!(agents.len(), 1);
// Not dirty anymore
assert!(store.take_agents_if_dirty().is_none());
}
#[test]
fn agent_upsert() {
let store = MetricsStore::new();
store.record_agent(sample_agent("n1", AgentState::Idle));
store.record_agent(sample_agent("n1", AgentState::Active));
let agents = store.get_agents();
assert_eq!(agents.len(), 1);
assert_eq!(agents[0].status, AgentState::Active);
}
#[test]
fn trace_ring_buffer_cap() {
let store = MetricsStore::new();
for i in 0..(MAX_TRACES + 50) {
store.push_trace(PipelineTrace {
asr_start_ms: i as u64,
asr_end_ms: i as u64 + 10,
llm_start_ms: 0,
llm_end_ms: 0,
tts_start_ms: 0,
tts_end_ms: 0,
tokens_per_sec: 0.0,
});
}
let traces = store.get_traces(999);
assert_eq!(traces.len(), MAX_TRACES);
// Oldest evicted
assert_eq!(traces[0].asr_start_ms, 50);
}
#[test]
fn snapshot_ring_buffer_cap() {
let store = MetricsStore::new();
for _ in 0..(MAX_SNAPSHOTS + 10) {
store.push_snapshot(sample_snapshot());
}
let snaps = store.take_snapshots_if_dirty().unwrap();
assert_eq!(snaps.len(), MAX_SNAPSHOTS);
}
#[test]
fn get_latest_snapshot_returns_newest() {
let store = MetricsStore::new();
store.push_snapshot(SystemSnapshot {
cpu_pct: 0.1,
..sample_snapshot()
});
store.push_snapshot(SystemSnapshot {
cpu_pct: 0.9,
..sample_snapshot()
});
let latest = store.get_latest_snapshot().unwrap();
assert!((latest.cpu_pct - 0.9).abs() < f64::EPSILON);
}
}