| 1 | use std::collections::{HashMap, VecDeque}; |
| 2 | |
| 3 | use agents::Invocation; |
| 4 | |
| 5 | #[derive(Debug, Default, PartialEq, Eq)] |
| 6 | pub struct Lane { |
| 7 | pub active: Vec<u64>, |
| 8 | pub kills_pending: u32, |
| 9 | pub waiting: VecDeque<u64>, |
| 10 | } |
| 11 | |
| 12 | impl Lane { |
| 13 | fn is_idle(&self) -> bool { |
| 14 | self.active.is_empty() && self.kills_pending == 0 |
| 15 | } |
| 16 | } |
| 17 | |
| 18 | #[derive(Debug, PartialEq, Eq)] |
| 19 | pub enum Admit { |
| 20 | Launch, |
| 21 | Queued, |
| 22 | } |
| 23 | |
| 24 | pub struct RestoreInfo { |
| 25 | pub id: u64, |
| 26 | pub session_id: String, |
| 27 | pub queued: bool, |
| 28 | pub kill: Option<Invocation>, |
| 29 | } |
| 30 | |
| 31 | pub struct Restored { |
| 32 | pub lanes: Lanes, |
| 33 | pub resume_queue: Vec<u64>, |
| 34 | pub interrupted: Vec<u64>, |
| 35 | pub kills: Vec<(String, Invocation)>, |
| 36 | } |
| 37 | |
| 38 | #[derive(Debug, Default)] |
| 39 | pub struct Lanes { |
| 40 | lanes: HashMap<String, Lane>, |
| 41 | renamed: HashMap<String, String>, |
| 42 | } |
| 43 | |
| 44 | impl Lanes { |
| 45 | pub fn get(&self, session_id: &str) -> Option<&Lane> { |
| 46 | self.lanes.get(session_id) |
| 47 | } |
| 48 | |
| 49 | pub fn is_empty(&self) -> bool { |
| 50 | self.lanes.is_empty() |
| 51 | } |
| 52 | |
| 53 | pub fn idle_sessions(&self) -> Vec<String> { |
| 54 | self.lanes |
| 55 | .iter() |
| 56 | .filter(|(_, lane)| lane.is_idle()) |
| 57 | .map(|(session_id, _)| session_id.clone()) |
| 58 | .collect() |
| 59 | } |
| 60 | |
| 61 | pub fn has_kills_pending(&self, session_id: &str) -> bool { |
| 62 | self.lanes |
| 63 | .get(session_id) |
| 64 | .is_some_and(|lane| lane.kills_pending > 0) |
| 65 | } |
| 66 | |
| 67 | pub fn admit(&mut self, session_id: &str, run_id: u64) -> Admit { |
| 68 | let lane = self.lanes.entry(session_id.to_owned()).or_default(); |
| 69 | if lane.is_idle() && lane.waiting.is_empty() { |
| 70 | lane.active.push(run_id); |
| 71 | Admit::Launch |
| 72 | } else { |
| 73 | lane.waiting.push_back(run_id); |
| 74 | Admit::Queued |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | pub fn finished(&mut self, session_id: &str, run_id: u64) -> bool { |
| 79 | let Some(lane) = self.lanes.get_mut(session_id) else { |
| 80 | return false; |
| 81 | }; |
| 82 | let before = lane.active.len(); |
| 83 | lane.active.retain(|active| *active != run_id); |
| 84 | lane.active.len() != before |
| 85 | } |
| 86 | |
| 87 | pub fn next(&mut self, session_id: &str, is_queued: impl Fn(u64) -> bool) -> Option<u64> { |
| 88 | let lane = self.lanes.get_mut(session_id)?; |
| 89 | if !lane.is_idle() { |
| 90 | return None; |
| 91 | } |
| 92 | while let Some(candidate) = lane.waiting.pop_front() { |
| 93 | if is_queued(candidate) { |
| 94 | lane.active.push(candidate); |
| 95 | return Some(candidate); |
| 96 | } |
| 97 | } |
| 98 | self.lanes.remove(session_id); |
| 99 | None |
| 100 | } |
| 101 | |
| 102 | pub fn kill_started(&mut self, session_id: &str) { |
| 103 | self.lanes |
| 104 | .entry(session_id.to_owned()) |
| 105 | .or_default() |
| 106 | .kills_pending += 1; |
| 107 | } |
| 108 | |
| 109 | pub fn resolve(&self, session_id: &str) -> String { |
| 110 | let mut current = session_id; |
| 111 | for _ in 0..=self.renamed.len() { |
| 112 | match self.renamed.get(current) { |
| 113 | Some(next) => current = next, |
| 114 | None => break, |
| 115 | } |
| 116 | } |
| 117 | current.to_owned() |
| 118 | } |
| 119 | |
| 120 | pub fn kill_done(&mut self, session_id: &str) { |
| 121 | let session_id = self.resolve(session_id); |
| 122 | if let Some(lane) = self.lanes.get_mut(&session_id) { |
| 123 | lane.kills_pending = lane.kills_pending.saturating_sub(1); |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | pub fn remove_waiting(&mut self, session_id: &str, run_id: u64) { |
| 128 | if let Some(lane) = self.lanes.get_mut(session_id) { |
| 129 | lane.waiting.retain(|waiting| *waiting != run_id); |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | pub fn drain_waiting(&mut self, session_id: &str) -> Vec<u64> { |
| 134 | self.lanes |
| 135 | .get_mut(session_id) |
| 136 | .map(|lane| lane.waiting.drain(..).collect()) |
| 137 | .unwrap_or_default() |
| 138 | } |
| 139 | |
| 140 | pub fn rename(&mut self, from: &str, to: &str) -> Vec<u64> { |
| 141 | let Some(lane) = self.lanes.remove(from) else { |
| 142 | return Vec::new(); |
| 143 | }; |
| 144 | self.renamed.insert(from.to_owned(), to.to_owned()); |
| 145 | let moved: Vec<u64> = lane.waiting.iter().copied().collect(); |
| 146 | let target = self.lanes.entry(to.to_owned()).or_default(); |
| 147 | target.active.extend(lane.active); |
| 148 | target.kills_pending += lane.kills_pending; |
| 149 | target.waiting.extend(lane.waiting); |
| 150 | moved |
| 151 | } |
| 152 | |
| 153 | pub fn restore(infos: &[RestoreInfo], can_resume: impl Fn(u64) -> bool) -> Restored { |
| 154 | let mut lanes = Lanes::default(); |
| 155 | let mut resume_queue = Vec::new(); |
| 156 | let mut interrupted = Vec::new(); |
| 157 | let mut kills = Vec::new(); |
| 158 | for info in infos.iter().filter(|info| !info.queued) { |
| 159 | let has_active = lanes |
| 160 | .get(&info.session_id) |
| 161 | .is_some_and(|lane| !lane.active.is_empty()); |
| 162 | if !has_active && can_resume(info.id) { |
| 163 | lanes.admit(&info.session_id, info.id); |
| 164 | lanes.kill_started(&info.session_id); |
| 165 | resume_queue.push(info.id); |
| 166 | } else { |
| 167 | interrupted.push(info.id); |
| 168 | if let Some(kill) = &info.kill { |
| 169 | lanes.kill_started(&info.session_id); |
| 170 | kills.push((info.session_id.clone(), kill.clone())); |
| 171 | } |
| 172 | } |
| 173 | } |
| 174 | for info in infos.iter().filter(|info| info.queued) { |
| 175 | lanes |
| 176 | .lanes |
| 177 | .entry(info.session_id.clone()) |
| 178 | .or_default() |
| 179 | .waiting |
| 180 | .push_back(info.id); |
| 181 | } |
| 182 | Restored { |
| 183 | lanes, |
| 184 | resume_queue, |
| 185 | interrupted, |
| 186 | kills, |
| 187 | } |
| 188 | } |
| 189 | } |
| 190 | |
| 191 | #[cfg(test)] |
| 192 | mod tests { |
| 193 | use super::*; |
| 194 | |
| 195 | fn info(id: u64, session_id: &str, queued: bool, kill: bool) -> RestoreInfo { |
| 196 | RestoreInfo { |
| 197 | id, |
| 198 | session_id: session_id.to_owned(), |
| 199 | queued, |
| 200 | kill: kill.then(|| Invocation { |
| 201 | program: "kill".to_owned(), |
| 202 | args: Vec::new(), |
| 203 | cwd: None, |
| 204 | stdin: None, |
| 205 | }), |
| 206 | } |
| 207 | } |
| 208 | |
| 209 | fn waiting(lanes: &Lanes, session_id: &str) -> Vec<u64> { |
| 210 | lanes |
| 211 | .get(session_id) |
| 212 | .unwrap() |
| 213 | .waiting |
| 214 | .iter() |
| 215 | .copied() |
| 216 | .collect() |
| 217 | } |
| 218 | |
| 219 | #[test] |
| 220 | fn admit_launches_on_an_idle_lane_and_queues_while_busy() { |
| 221 | let mut lanes = Lanes::default(); |
| 222 | assert_eq!(lanes.admit("s", 1), Admit::Launch); |
| 223 | assert_eq!(lanes.admit("s", 2), Admit::Queued); |
| 224 | assert_eq!(lanes.admit("s", 3), Admit::Queued); |
| 225 | assert_eq!(lanes.admit("other", 4), Admit::Launch); |
| 226 | assert_eq!(lanes.get("s").unwrap().active, [1]); |
| 227 | assert_eq!(waiting(&lanes, "s"), [2, 3]); |
| 228 | } |
| 229 | |
| 230 | #[test] |
| 231 | fn a_pending_kill_blocks_admission_and_release() { |
| 232 | let mut lanes = Lanes::default(); |
| 233 | lanes.kill_started("s"); |
| 234 | assert_eq!(lanes.admit("s", 1), Admit::Queued); |
| 235 | assert_eq!(lanes.next("s", |_| true), None); |
| 236 | lanes.kill_done("s"); |
| 237 | assert_eq!(lanes.next("s", |_| true), Some(1)); |
| 238 | } |
| 239 | |
| 240 | #[test] |
| 241 | fn waiting_runs_are_released_in_order_one_at_a_time() { |
| 242 | let mut lanes = Lanes::default(); |
| 243 | lanes.admit("s", 1); |
| 244 | lanes.admit("s", 2); |
| 245 | lanes.admit("s", 3); |
| 246 | assert_eq!(lanes.next("s", |_| true), None); |
| 247 | assert!(lanes.finished("s", 1)); |
| 248 | assert!(!lanes.finished("s", 1)); |
| 249 | assert_eq!(lanes.next("s", |_| true), Some(2)); |
| 250 | assert_eq!(lanes.next("s", |_| true), None); |
| 251 | assert!(lanes.finished("s", 2)); |
| 252 | assert_eq!(lanes.next("s", |_| true), Some(3)); |
| 253 | assert!(lanes.finished("s", 3)); |
| 254 | assert_eq!(lanes.next("s", |_| true), None); |
| 255 | assert!(lanes.is_empty()); |
| 256 | } |
| 257 | |
| 258 | #[test] |
| 259 | fn release_skips_runs_that_are_no_longer_queued() { |
| 260 | let mut lanes = Lanes::default(); |
| 261 | lanes.admit("s", 1); |
| 262 | lanes.admit("s", 2); |
| 263 | lanes.admit("s", 3); |
| 264 | lanes.finished("s", 1); |
| 265 | assert_eq!(lanes.next("s", |id| id == 3), Some(3)); |
| 266 | } |
| 267 | |
| 268 | #[test] |
| 269 | fn kills_are_counted_not_flagged() { |
| 270 | let mut lanes = Lanes::default(); |
| 271 | lanes.kill_started("s"); |
| 272 | lanes.kill_started("s"); |
| 273 | lanes.admit("s", 1); |
| 274 | lanes.kill_done("s"); |
| 275 | assert!(lanes.has_kills_pending("s")); |
| 276 | assert_eq!(lanes.next("s", |_| true), None); |
| 277 | lanes.kill_done("s"); |
| 278 | lanes.kill_done("s"); |
| 279 | assert!(!lanes.has_kills_pending("s")); |
| 280 | assert_eq!(lanes.next("s", |_| true), Some(1)); |
| 281 | } |
| 282 | |
| 283 | #[test] |
| 284 | fn removing_and_draining_waiting_runs() { |
| 285 | let mut lanes = Lanes::default(); |
| 286 | for id in 1..=4 { |
| 287 | lanes.admit("s", id); |
| 288 | } |
| 289 | lanes.remove_waiting("s", 3); |
| 290 | assert_eq!(waiting(&lanes, "s"), [2, 4]); |
| 291 | assert_eq!(lanes.drain_waiting("s"), [2, 4]); |
| 292 | assert!(waiting(&lanes, "s").is_empty()); |
| 293 | assert!(lanes.drain_waiting("none").is_empty()); |
| 294 | } |
| 295 | |
| 296 | #[test] |
| 297 | fn renaming_moves_active_and_waiting_runs() { |
| 298 | let mut lanes = Lanes::default(); |
| 299 | lanes.admit("x", 1); |
| 300 | lanes.admit("x", 2); |
| 301 | assert_eq!(lanes.rename("x", "y"), [2]); |
| 302 | assert!(lanes.get("x").is_none()); |
| 303 | assert_eq!(lanes.get("y").unwrap().active, [1]); |
| 304 | assert_eq!(waiting(&lanes, "y"), [2]); |
| 305 | assert!(lanes.rename("missing", "z").is_empty()); |
| 306 | } |
| 307 | |
| 308 | #[test] |
| 309 | fn a_rename_collision_keeps_both_active_runs_blocking() { |
| 310 | let mut lanes = Lanes::default(); |
| 311 | lanes.admit("x", 1); |
| 312 | lanes.admit("x", 2); |
| 313 | lanes.admit("y", 3); |
| 314 | lanes.admit("y", 4); |
| 315 | lanes.kill_started("x"); |
| 316 | assert_eq!(lanes.rename("x", "y"), [2]); |
| 317 | let lane = lanes.get("y").unwrap(); |
| 318 | assert_eq!(lane.active, [3, 1]); |
| 319 | assert_eq!(lane.kills_pending, 1); |
| 320 | assert_eq!(waiting(&lanes, "y"), [4, 2]); |
| 321 | lanes.kill_done("y"); |
| 322 | assert!(lanes.finished("y", 3)); |
| 323 | assert_eq!(lanes.next("y", |_| true), None); |
| 324 | assert!(lanes.finished("y", 1)); |
| 325 | assert_eq!(lanes.next("y", |_| true), Some(4)); |
| 326 | } |
| 327 | |
| 328 | #[test] |
| 329 | fn a_kill_pending_across_a_rename_is_released_under_the_old_id() { |
| 330 | let mut lanes = Lanes::default(); |
| 331 | lanes.admit("x", 1); |
| 332 | lanes.kill_started("x"); |
| 333 | lanes.rename("x", "y"); |
| 334 | assert_eq!(lanes.resolve("x"), "y"); |
| 335 | assert!(lanes.has_kills_pending("y")); |
| 336 | lanes.kill_done("x"); |
| 337 | assert!(!lanes.has_kills_pending("y")); |
| 338 | } |
| 339 | |
| 340 | #[test] |
| 341 | fn restore_builds_active_candidates_waiting_order_and_kills() { |
| 342 | let infos = [ |
| 343 | info(5, "shared", false, false), |
| 344 | info(6, "shared", false, true), |
| 345 | info(7, "shared", true, false), |
| 346 | info(8, "shared", true, false), |
| 347 | info(9, "dead", false, true), |
| 348 | info(10, "other", false, false), |
| 349 | ]; |
| 350 | let restored = Lanes::restore(&infos, |id| id != 9); |
| 351 | assert_eq!(restored.resume_queue, [5, 10]); |
| 352 | assert_eq!(restored.interrupted, [6, 9]); |
| 353 | let sessions: Vec<&str> = restored |
| 354 | .kills |
| 355 | .iter() |
| 356 | .map(|(session, _)| session.as_str()) |
| 357 | .collect(); |
| 358 | assert_eq!(sessions, ["shared", "dead"]); |
| 359 | let lanes = restored.lanes; |
| 360 | let shared = lanes.get("shared").unwrap(); |
| 361 | assert_eq!(shared.active, [5]); |
| 362 | assert_eq!(shared.kills_pending, 2); |
| 363 | assert_eq!(waiting(&lanes, "shared"), [7, 8]); |
| 364 | assert_eq!(lanes.get("dead").unwrap().kills_pending, 1); |
| 365 | assert_eq!(lanes.get("other").unwrap().kills_pending, 1); |
| 366 | assert!(lanes.idle_sessions().is_empty()); |
| 367 | } |
| 368 | |
| 369 | #[test] |
| 370 | fn a_restored_lane_with_only_waiting_runs_is_idle() { |
| 371 | let restored = Lanes::restore(&[info(7, "s", true, false)], |_| true); |
| 372 | assert_eq!(restored.lanes.idle_sessions(), ["s"]); |
| 373 | } |
| 374 | } |