|
1 | 1 | use crate::transport::Transport; |
2 | | -use corro_types::{actor::ActorId, agent::Agent}; |
| 2 | +use antithesis_sdk::assert_always; |
| 3 | +use corro_types::agent::Agent; |
3 | 4 | use metrics::gauge; |
| 5 | +use serde_json::json; |
4 | 6 | use std::time::Duration; |
5 | 7 | use tokio::task::block_in_place; |
6 | 8 | use tracing::error; |
7 | 9 | use tripwire::Tripwire; |
8 | 10 |
|
9 | 11 | pub async fn metrics_loop(agent: Agent, transport: Transport, mut tripwire: Tripwire) { |
10 | 12 | let mut metrics_interval = tokio::time::interval(Duration::from_secs(10)); |
| 13 | + let on_antithesis = std::env::var("ANTITHESIS_OUTPUT_DIR").is_ok(); |
11 | 14 |
|
12 | 15 | loop { |
13 | 16 | tokio::select! { |
14 | 17 | _ = metrics_interval.tick() => {}, |
15 | 18 | _ = &mut tripwire => break, |
16 | 19 | } |
17 | 20 |
|
18 | | - block_in_place(|| collect_metrics(&agent, &transport)); |
| 21 | + block_in_place(|| collect_metrics(&agent, &transport, on_antithesis)); |
19 | 22 | } |
20 | 23 | } |
21 | 24 |
|
22 | | -pub fn collect_metrics(agent: &Agent, transport: &Transport) { |
| 25 | +pub fn collect_metrics(agent: &Agent, transport: &Transport, on_antithesis: bool) { |
23 | 26 | agent.pool().emit_metrics(); |
24 | 27 | transport.emit_metrics(); |
25 | 28 |
|
@@ -50,38 +53,66 @@ pub fn collect_metrics(agent: &Agent, transport: &Transport) { |
50 | 53 | } |
51 | 54 | } |
52 | 55 |
|
53 | | - // TODO: collect from bookie? |
| 56 | + // Buffered changes stats across all actors |
54 | 57 | match conn |
55 | | - .prepare_cached("SELECT actor_id, (select count(site_id) FROM __corro_buffered_changes WHERE site_id = actor_id) FROM __corro_members") |
| 58 | + .prepare_cached( |
| 59 | + " |
| 60 | + SELECT |
| 61 | + COALESCE(json_extract(m.foca_state, '$.state'), 'orphan') AS peer_state, |
| 62 | + count(*) AS total, |
| 63 | + strftime('%s', 'now') - (min(bc.ts) >> 32) as oldest_age_seconds |
| 64 | + FROM __corro_buffered_changes bc |
| 65 | + LEFT JOIN __corro_members m ON m.actor_id = bc.site_id |
| 66 | + GROUP BY peer_state |
| 67 | + ", |
| 68 | + ) |
56 | 69 | .and_then(|mut prepped| { |
57 | 70 | prepped |
58 | | - .query_map((), |row| { |
59 | | - Ok((row.get::<_, ActorId>(0)?, row.get::<_, i64>(1)?)) |
| 71 | + .query_map([], |row| { |
| 72 | + Ok(( |
| 73 | + row.get::<_, String>(0)?, |
| 74 | + row.get::<_, i64>(1)?, |
| 75 | + row.get::<_, i64>(2)?, |
| 76 | + )) |
60 | 77 | }) |
61 | 78 | .and_then(|mapped| mapped.collect::<Result<Vec<_>, _>>()) |
62 | 79 | }) { |
63 | | - Ok(mapped) => { |
64 | | - for (actor_id, count) in mapped { |
65 | | - gauge!("corro.db.buffered.changes.rows.total", "actor_id" => actor_id.to_string()).set(count as f64) |
| 80 | + Ok(rows) => { |
| 81 | + for (peer_state, total_count, oldest_age_seconds) in rows { |
| 82 | + // v2 has O(N) time series while the v1 metric had O(N^2) time series |
| 83 | + // The name got changed on purpose so the metric has a fresh namespace |
| 84 | + gauge!("corro.db.buffered.changes.v2.total", "peer_state" => peer_state.clone()) |
| 85 | + .set(total_count as f64); |
| 86 | + gauge!("corro.db.buffered.changes.v2.oldest_age_seconds", "peer_state" => peer_state) |
| 87 | + .set(oldest_age_seconds as f64); |
66 | 88 | } |
67 | 89 | } |
68 | 90 | Err(e) => { |
69 | | - error!("could not query count for buffered changes: {e}"); |
| 91 | + error!("could not query buffered changes v2 stats: {e}"); |
70 | 92 | } |
71 | 93 | } |
72 | 94 |
|
73 | 95 | match conn |
74 | | - .prepare_cached("select actor_id, sum((end - start) + 1) from __corro_bookkeeping_gaps group by actor_id") |
| 96 | + .prepare_cached( |
| 97 | + " |
| 98 | + SELECT |
| 99 | + COALESCE(json_extract(m.foca_state, '$.state'), 'orphan') AS peer_state, |
| 100 | + COALESCE(SUM((g.end - g.start) + 1), 0) AS gaps_sum |
| 101 | + FROM __corro_bookkeeping_gaps g |
| 102 | + LEFT JOIN __corro_members m ON m.actor_id = g.actor_id |
| 103 | + GROUP BY peer_state |
| 104 | + ", |
| 105 | + ) |
75 | 106 | .and_then(|mut prepped| { |
76 | 107 | prepped |
77 | | - .query_map((), |row| { |
78 | | - Ok((row.get::<_, ActorId>(0)?, row.get::<_, u64>(1)?)) |
| 108 | + .query_map([], |row| { |
| 109 | + Ok((row.get::<_, String>(0)?, row.get::<_, u64>(1)?)) |
79 | 110 | }) |
80 | 111 | .and_then(|mapped| mapped.collect::<Result<Vec<_>, _>>()) |
81 | 112 | }) { |
82 | | - Ok(mapped) => { |
83 | | - for (actor_id, sum) in mapped { |
84 | | - gauge!("corro.db.gaps.sum", "actor_id" => actor_id.to_string()).set(sum as f64) |
| 113 | + Ok(rows) => { |
| 114 | + for (peer_state, sum) in rows { |
| 115 | + gauge!("corro.db.gaps.sum", "peer_state" => peer_state).set(sum as f64) |
85 | 116 | } |
86 | 117 | } |
87 | 118 | Err(e) => { |
@@ -109,4 +140,38 @@ pub fn collect_metrics(agent: &Agent, transport: &Transport) { |
109 | 140 | gauge!("corro.db.wal.size").set(meta.len() as f64); |
110 | 141 | } |
111 | 142 | } |
| 143 | + |
| 144 | + if on_antithesis { |
| 145 | + let gaps = conn |
| 146 | + .prepare_cached( |
| 147 | + "SELECT COALESCE(SUM(end - start + 1), 0) FROM __corro_bookkeeping_gaps", |
| 148 | + ) |
| 149 | + .and_then(|mut prepped| prepped.query_row([], |row| row.get::<_, i64>(0))); |
| 150 | + |
| 151 | + if let Ok(gaps) = gaps { |
| 152 | + const MAX_GAPS: i64 = 100; |
| 153 | + const MAX_QUEUE: u64 = 16_000; |
| 154 | + const MAX_P99_LAG: f64 = 3.0; |
| 155 | + |
| 156 | + let p99_lag = agent.metrics_tracker().quantile_lag(0.99).unwrap_or(0.0); |
| 157 | + let queue_size = agent.metrics_tracker().queue_size(); |
| 158 | + let is_healthy = gaps <= MAX_GAPS && queue_size <= MAX_QUEUE && p99_lag <= MAX_P99_LAG; |
| 159 | + let details = json!({ |
| 160 | + "gaps": gaps, |
| 161 | + "queue_size": queue_size, |
| 162 | + "p99_lag": p99_lag, |
| 163 | + "max_gaps": MAX_GAPS, |
| 164 | + "max_queue": MAX_QUEUE, |
| 165 | + "max_p99_lag": MAX_P99_LAG, |
| 166 | + }); |
| 167 | + |
| 168 | + assert_always!( |
| 169 | + is_healthy, |
| 170 | + "Corrosion node remains healthy in antithesis", |
| 171 | + &details |
| 172 | + ); |
| 173 | + } else if let Err(e) = gaps { |
| 174 | + error!("could not query gaps for antithesis health assertion: {e}"); |
| 175 | + } |
| 176 | + } |
112 | 177 | } |
0 commit comments