| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | use std::collections::{HashMap, VecDeque}; |
| 10 | use std::sync::{Mutex, MutexGuard}; |
| 11 | |
| 12 | use forge_ci::proto::RunnerLogBatch; |
| 13 | |
| 14 | |
| 15 | const MAX_LINES: usize = 2000; |
| 16 | |
| 17 | const MAX_BATCH_LINES: usize = 1000; |
| 18 | |
| 19 | const MAX_TEXT_BYTES: usize = 4096; |
| 20 | |
| 21 | const MAX_BOOT_ID_BYTES: usize = 64; |
| 22 | |
| 23 | const LEVELS: [&str; 5] = ["error", "warn", "info", "debug", "trace"]; |
| 24 | |
| 25 | |
| 26 | #[derive(Clone, Debug, PartialEq, Eq)] |
| 27 | pub struct Line { |
| 28 | |
| 29 | pub at_ms: i64, |
| 30 | pub level: String, |
| 31 | pub text: String, |
| 32 | |
| 33 | pub from_forge: bool, |
| 34 | } |
| 35 | |
| 36 | |
| 37 | #[derive(Debug, Default)] |
| 38 | struct PerRunner { |
| 39 | |
| 40 | boot_id: String, |
| 41 | |
| 42 | next_seq: u64, |
| 43 | lines: VecDeque<Line>, |
| 44 | |
| 45 | last_note: Option<String>, |
| 46 | } |
| 47 | |
| 48 | |
| 49 | #[derive(Debug, Default)] |
| 50 | pub struct RunnerLogs { |
| 51 | runners: Mutex<HashMap<String, PerRunner>>, |
| 52 | } |
| 53 | |
| 54 | impl RunnerLogs { |
| 55 | |
| 56 | |
| 57 | |
| 58 | pub fn ingest(&self, runner_id: &str, batch: &RunnerLogBatch) -> u64 { |
| 59 | if batch.boot_id.is_empty() || batch.boot_id.len() > MAX_BOOT_ID_BYTES { |
| 60 | return self.next_seq(runner_id); |
| 61 | } |
| 62 | let mut runners = self.lock(); |
| 63 | let entry = runners.entry(runner_id.to_owned()).or_default(); |
| 64 | if entry.boot_id != batch.boot_id { |
| 65 | entry.boot_id = batch.boot_id.clone(); |
| 66 | entry.next_seq = 0; |
| 67 | let short: String = batch.boot_id.chars().take(8).collect(); |
| 68 | push(entry, forge_line(format!("runner started (boot {short})"))); |
| 69 | } |
| 70 | if batch.first_seq > entry.next_seq { |
| 71 | let missing = batch.first_seq - entry.next_seq; |
| 72 | push(entry, forge_line(format!("{missing} lines missing"))); |
| 73 | } |
| 74 | for (i, line) in batch.lines.iter().take(MAX_BATCH_LINES).enumerate() { |
| 75 | let seq = batch.first_seq + i as u64; |
| 76 | if seq < entry.next_seq { |
| 77 | continue; |
| 78 | } |
| 79 | push( |
| 80 | entry, |
| 81 | Line { |
| 82 | at_ms: line.at_ms, |
| 83 | level: level_of(&line.level), |
| 84 | text: cut(&line.text, MAX_TEXT_BYTES), |
| 85 | from_forge: false, |
| 86 | }, |
| 87 | ); |
| 88 | } |
| 89 | let taken = batch.lines.len().min(MAX_BATCH_LINES) as u64; |
| 90 | entry.next_seq = entry.next_seq.max(batch.first_seq + taken); |
| 91 | entry.next_seq |
| 92 | } |
| 93 | |
| 94 | |
| 95 | |
| 96 | pub fn note(&self, runner_id: &str, text: &str) -> bool { |
| 97 | let mut runners = self.lock(); |
| 98 | let entry = runners.entry(runner_id.to_owned()).or_default(); |
| 99 | if entry.last_note.as_deref() == Some(text) { |
| 100 | return false; |
| 101 | } |
| 102 | entry.last_note = Some(text.to_owned()); |
| 103 | push(entry, forge_line(text.to_owned())); |
| 104 | true |
| 105 | } |
| 106 | |
| 107 | |
| 108 | |
| 109 | pub fn clear_note(&self, runner_id: &str) { |
| 110 | if let Some(entry) = self.lock().get_mut(runner_id) { |
| 111 | entry.last_note = None; |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | |
| 116 | pub fn lines(&self, runner_id: &str) -> Vec<Line> { |
| 117 | self.lock() |
| 118 | .get(runner_id) |
| 119 | .map(|entry| entry.lines.iter().cloned().collect()) |
| 120 | .unwrap_or_default() |
| 121 | } |
| 122 | |
| 123 | fn next_seq(&self, runner_id: &str) -> u64 { |
| 124 | self.lock().get(runner_id).map_or(0, |entry| entry.next_seq) |
| 125 | } |
| 126 | |
| 127 | fn lock(&self) -> MutexGuard<'_, HashMap<String, PerRunner>> { |
| 128 | |
| 129 | self.runners.lock().unwrap_or_else(|e| e.into_inner()) |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | |
| 134 | fn push(entry: &mut PerRunner, line: Line) { |
| 135 | entry.lines.push_back(line); |
| 136 | while entry.lines.len() > MAX_LINES { |
| 137 | entry.lines.pop_front(); |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | |
| 142 | fn forge_line(text: String) -> Line { |
| 143 | let at_ms = std::time::SystemTime::now() |
| 144 | .duration_since(std::time::UNIX_EPOCH) |
| 145 | .map_or(0, |d| i64::try_from(d.as_millis()).unwrap_or(i64::MAX)); |
| 146 | Line { |
| 147 | at_ms, |
| 148 | level: "info".to_owned(), |
| 149 | text, |
| 150 | from_forge: true, |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | |
| 155 | fn level_of(level: &str) -> String { |
| 156 | if LEVELS.contains(&level) { |
| 157 | level.to_owned() |
| 158 | } else { |
| 159 | "info".to_owned() |
| 160 | } |
| 161 | } |
| 162 | |
| 163 | |
| 164 | fn cut(text: &str, max: usize) -> String { |
| 165 | if text.len() <= max { |
| 166 | return text.to_owned(); |
| 167 | } |
| 168 | let mut end = max; |
| 169 | while !text.is_char_boundary(end) { |
| 170 | end -= 1; |
| 171 | } |
| 172 | text[..end].to_owned() |
| 173 | } |
| 174 | |
| 175 | #[cfg(test)] |
| 176 | mod tests { |
| 177 | use super::*; |
| 178 | use forge_ci::proto::RunnerLogLine; |
| 179 | |
| 180 | fn batch(boot: &str, first_seq: u64, texts: &[&str]) -> RunnerLogBatch { |
| 181 | RunnerLogBatch { |
| 182 | boot_id: boot.to_owned(), |
| 183 | first_seq, |
| 184 | lines: texts |
| 185 | .iter() |
| 186 | .map(|t| RunnerLogLine { |
| 187 | at_ms: 1, |
| 188 | level: "info".to_owned(), |
| 189 | text: (*t).to_owned(), |
| 190 | }) |
| 191 | .collect(), |
| 192 | } |
| 193 | } |
| 194 | |
| 195 | |
| 196 | #[test] |
| 197 | fn ingest_skips_what_is_known_and_marks_gaps() { |
| 198 | let logs = RunnerLogs::default(); |
| 199 | assert_eq!( |
| 200 | logs.ingest("r1", &batch("bootaaaaaaaaaa", 0, &["a", "b"])), |
| 201 | 2 |
| 202 | ); |
| 203 | |
| 204 | assert_eq!( |
| 205 | logs.ingest("r1", &batch("bootaaaaaaaaaa", 0, &["a", "b", "c"])), |
| 206 | 3 |
| 207 | ); |
| 208 | |
| 209 | assert_eq!(logs.ingest("r1", &batch("bootaaaaaaaaaa", 5, &["f"])), 6); |
| 210 | |
| 211 | let texts: Vec<String> = logs.lines("r1").into_iter().map(|l| l.text).collect(); |
| 212 | assert_eq!( |
| 213 | texts, |
| 214 | [ |
| 215 | "runner started (boot bootaaaa)", |
| 216 | "a", |
| 217 | "b", |
| 218 | "c", |
| 219 | "2 lines missing", |
| 220 | "f" |
| 221 | ] |
| 222 | ); |
| 223 | } |
| 224 | |
| 225 | |
| 226 | #[test] |
| 227 | fn note_is_written_once_until_cleared() { |
| 228 | let logs = RunnerLogs::default(); |
| 229 | assert!(logs.note("r1", "a restart is pending")); |
| 230 | assert!(!logs.note("r1", "a restart is pending")); |
| 231 | logs.clear_note("r1"); |
| 232 | assert!(logs.note("r1", "a restart is pending")); |
| 233 | let lines = logs.lines("r1"); |
| 234 | assert_eq!(lines.len(), 2); |
| 235 | assert!(lines.iter().all(|l| l.from_forge)); |
| 236 | } |
| 237 | } |