| 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 | use std::{ |
| 43 | collections::HashMap, |
| 44 | io::ErrorKind, |
| 45 | path::{Path, PathBuf}, |
| 46 | process::Stdio, |
| 47 | sync::Mutex, |
| 48 | time::{Duration, Instant}, |
| 49 | }; |
| 50 | |
| 51 | use tokio::{ |
| 52 | io::{AsyncBufReadExt, AsyncReadExt, AsyncWriteExt, BufReader}, |
| 53 | process::{Child, ChildStderr, ChildStdin, ChildStdout}, |
| 54 | sync::Mutex as AsyncMutex, |
| 55 | }; |
| 56 | |
| 57 | use crate::{ |
| 58 | application::port::GitQueryError, |
| 59 | domain::ObjectId, |
| 60 | infrastructure::{ |
| 61 | git::{count_spawn, git_command}, |
| 62 | metrics::metrics, |
| 63 | }, |
| 64 | }; |
| 65 | |
| 66 | |
| 67 | |
| 68 | |
| 69 | |
| 70 | |
| 71 | |
| 72 | |
| 73 | |
| 74 | const MAX_LIVE_REPOSITORIES: usize = 64; |
| 75 | |
| 76 | |
| 77 | |
| 78 | |
| 79 | |
| 80 | |
| 81 | const IDLE_TIMEOUT: Duration = Duration::from_secs(5 * 60); |
| 82 | |
| 83 | |
| 84 | |
| 85 | |
| 86 | |
| 87 | |
| 88 | |
| 89 | |
| 90 | const GIT_TIMEOUT: Duration = Duration::from_secs(20); |
| 91 | |
| 92 | |
| 93 | |
| 94 | |
| 95 | |
| 96 | |
| 97 | |
| 98 | const NOT_FOUND_MARKERS: [&str; 4] = ["missing", "ambiguous", "dangling", "notdir"]; |
| 99 | |
| 100 | |
| 101 | |
| 102 | |
| 103 | |
| 104 | |
| 105 | |
| 106 | const MAX_BATCH_SPECS: usize = 256; |
| 107 | const MAX_BATCH_BYTES: usize = 8 * 1024; |
| 108 | |
| 109 | |
| 110 | #[derive(Debug, Clone, PartialEq, Eq)] |
| 111 | pub(crate) struct ObjectInfo { |
| 112 | pub(crate) id: ObjectId, |
| 113 | pub(crate) kind: ObjectKind, |
| 114 | pub(crate) size: u64, |
| 115 | } |
| 116 | |
| 117 | |
| 118 | |
| 119 | |
| 120 | |
| 121 | |
| 122 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 123 | pub(crate) enum ObjectKind { |
| 124 | Blob, |
| 125 | Tree, |
| 126 | Commit, |
| 127 | Tag, |
| 128 | } |
| 129 | |
| 130 | impl ObjectKind { |
| 131 | fn from_str(value: &str) -> Option<Self> { |
| 132 | match value { |
| 133 | "blob" => Some(Self::Blob), |
| 134 | "tree" => Some(Self::Tree), |
| 135 | "commit" => Some(Self::Commit), |
| 136 | "tag" => Some(Self::Tag), |
| 137 | _ => None, |
| 138 | } |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | |
| 143 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 144 | enum Mode { |
| 145 | Check, |
| 146 | Contents, |
| 147 | } |
| 148 | |
| 149 | impl Mode { |
| 150 | fn flag(self) -> &'static str { |
| 151 | match self { |
| 152 | Self::Check => "--batch-check", |
| 153 | Self::Contents => "--batch", |
| 154 | } |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | |
| 159 | |
| 160 | |
| 161 | |
| 162 | |
| 163 | #[derive(Debug, Clone, Copy)] |
| 164 | enum Request<'a> { |
| 165 | |
| 166 | Check(&'a [String]), |
| 167 | |
| 168 | Contents(&'a ObjectInfo), |
| 169 | } |
| 170 | |
| 171 | impl Request<'_> { |
| 172 | fn mode(self) -> Mode { |
| 173 | match self { |
| 174 | Self::Check(_) => Mode::Check, |
| 175 | Self::Contents(_) => Mode::Contents, |
| 176 | } |
| 177 | } |
| 178 | } |
| 179 | |
| 180 | |
| 181 | #[derive(Debug)] |
| 182 | enum Reply { |
| 183 | Check(Vec<Option<ObjectInfo>>), |
| 184 | Contents(Vec<u8>), |
| 185 | } |
| 186 | |
| 187 | |
| 188 | |
| 189 | |
| 190 | |
| 191 | |
| 192 | |
| 193 | #[derive(Debug, Default)] |
| 194 | struct Slot { |
| 195 | check: AsyncMutex<Option<Batch>>, |
| 196 | contents: AsyncMutex<Option<Batch>>, |
| 197 | } |
| 198 | |
| 199 | impl Slot { |
| 200 | fn process(&self, mode: Mode) -> &AsyncMutex<Option<Batch>> { |
| 201 | match mode { |
| 202 | Mode::Check => &self.check, |
| 203 | Mode::Contents => &self.contents, |
| 204 | } |
| 205 | } |
| 206 | } |
| 207 | |
| 208 | |
| 209 | #[derive(Debug)] |
| 210 | struct Entry { |
| 211 | slot: std::sync::Arc<Slot>, |
| 212 | last_used: Instant, |
| 213 | } |
| 214 | |
| 215 | |
| 216 | |
| 217 | |
| 218 | |
| 219 | |
| 220 | |
| 221 | |
| 222 | #[derive(Debug, Default)] |
| 223 | pub struct CatFilePool { |
| 224 | repos: Mutex<HashMap<PathBuf, Entry>>, |
| 225 | } |
| 226 | |
| 227 | impl CatFilePool { |
| 228 | pub fn new() -> Self { |
| 229 | Self::default() |
| 230 | } |
| 231 | |
| 232 | |
| 233 | pub(crate) async fn info( |
| 234 | &self, |
| 235 | repo: &Path, |
| 236 | spec: &str, |
| 237 | ) -> Result<Option<ObjectInfo>, GitQueryError> { |
| 238 | Ok(self |
| 239 | .info_many(repo, std::slice::from_ref(&spec.to_owned())) |
| 240 | .await? |
| 241 | .pop() |
| 242 | .flatten()) |
| 243 | } |
| 244 | |
| 245 | |
| 246 | |
| 247 | |
| 248 | |
| 249 | |
| 250 | pub(crate) async fn info_many( |
| 251 | &self, |
| 252 | repo: &Path, |
| 253 | specs: &[String], |
| 254 | ) -> Result<Vec<Option<ObjectInfo>>, GitQueryError> { |
| 255 | let mut answers = Vec::with_capacity(specs.len()); |
| 256 | |
| 257 | for chunk in chunks(specs) { |
| 258 | let Reply::Check(replies) = self.exchange(repo, Request::Check(chunk)).await? else { |
| 259 | unreachable!("a check is answered with a check") |
| 260 | }; |
| 261 | |
| 262 | answers.extend(replies); |
| 263 | } |
| 264 | |
| 265 | Ok(answers) |
| 266 | } |
| 267 | |
| 268 | |
| 269 | |
| 270 | |
| 271 | |
| 272 | |
| 273 | |
| 274 | |
| 275 | pub(crate) async fn contents( |
| 276 | &self, |
| 277 | repo: &Path, |
| 278 | info: &ObjectInfo, |
| 279 | ) -> Result<Vec<u8>, GitQueryError> { |
| 280 | let Reply::Contents(bytes) = self.exchange(repo, Request::Contents(info)).await? else { |
| 281 | unreachable!("a contents request is answered with contents") |
| 282 | }; |
| 283 | |
| 284 | Ok(bytes) |
| 285 | } |
| 286 | |
| 287 | |
| 288 | |
| 289 | |
| 290 | |
| 291 | |
| 292 | |
| 293 | |
| 294 | |
| 295 | async fn exchange(&self, repo: &Path, request: Request<'_>) -> Result<Reply, GitQueryError> { |
| 296 | |
| 297 | |
| 298 | |
| 299 | metrics().record_cat_file_request(); |
| 300 | |
| 301 | let mode = request.mode(); |
| 302 | let slot = self.slot(repo); |
| 303 | let mut process = slot.process(mode).lock().await; |
| 304 | |
| 305 | for attempt in 0..2 { |
| 306 | if process.is_none() { |
| 307 | |
| 308 | |
| 309 | |
| 310 | if attempt == 1 { |
| 311 | metrics().record_cat_file_respawn(); |
| 312 | } |
| 313 | |
| 314 | *process = Some(Batch::spawn(repo, mode)?); |
| 315 | } |
| 316 | |
| 317 | |
| 318 | |
| 319 | |
| 320 | |
| 321 | let outcome = { |
| 322 | let batch = process.as_mut().expect("just spawned"); |
| 323 | |
| 324 | tokio::time::timeout(GIT_TIMEOUT, batch.run(request)).await |
| 325 | }; |
| 326 | |
| 327 | match outcome { |
| 328 | Ok(Ok(value)) => return Ok(value), |
| 329 | Ok(Err(Desync::Protocol(message))) => { |
| 330 | |
| 331 | |
| 332 | *process = None; |
| 333 | return Err(GitQueryError::new(format!( |
| 334 | "git cat-file {} spoke out of turn: {message}", |
| 335 | mode.flag() |
| 336 | ))); |
| 337 | } |
| 338 | Ok(Err(Desync::Gone(message))) => { |
| 339 | *process = None; |
| 340 | |
| 341 | if attempt == 1 { |
| 342 | return Err(GitQueryError::new(format!( |
| 343 | "git cat-file {} would not stay running: {message}", |
| 344 | mode.flag() |
| 345 | ))); |
| 346 | } |
| 347 | } |
| 348 | Err(_elapsed) => { |
| 349 | *process = None; |
| 350 | return Err(GitQueryError::timed_out(GIT_TIMEOUT)); |
| 351 | } |
| 352 | } |
| 353 | } |
| 354 | |
| 355 | unreachable!("the loop either returns or retries once") |
| 356 | } |
| 357 | |
| 358 | |
| 359 | |
| 360 | |
| 361 | |
| 362 | |
| 363 | |
| 364 | fn slot(&self, repo: &Path) -> std::sync::Arc<Slot> { |
| 365 | let mut repos = self.repos.lock().expect("lock poisoned"); |
| 366 | let now = Instant::now(); |
| 367 | |
| 368 | repos.retain(|_, entry| { |
| 369 | std::sync::Arc::strong_count(&entry.slot) > 1 |
| 370 | || now.duration_since(entry.last_used) < IDLE_TIMEOUT |
| 371 | }); |
| 372 | |
| 373 | if let Some(entry) = repos.get_mut(repo) { |
| 374 | entry.last_used = now; |
| 375 | return entry.slot.clone(); |
| 376 | } |
| 377 | |
| 378 | if repos.len() >= MAX_LIVE_REPOSITORIES { |
| 379 | let oldest = repos |
| 380 | .iter() |
| 381 | .filter(|(_, entry)| std::sync::Arc::strong_count(&entry.slot) == 1) |
| 382 | .min_by_key(|(_, entry)| entry.last_used) |
| 383 | .map(|(path, _)| path.clone()); |
| 384 | |
| 385 | if let Some(oldest) = oldest { |
| 386 | repos.remove(&oldest); |
| 387 | } |
| 388 | } |
| 389 | |
| 390 | let slot = std::sync::Arc::new(Slot::default()); |
| 391 | |
| 392 | repos.insert( |
| 393 | repo.to_path_buf(), |
| 394 | Entry { |
| 395 | slot: slot.clone(), |
| 396 | last_used: now, |
| 397 | }, |
| 398 | ); |
| 399 | |
| 400 | slot |
| 401 | } |
| 402 | |
| 403 | |
| 404 | |
| 405 | |
| 406 | |
| 407 | |
| 408 | |
| 409 | |
| 410 | #[must_use] |
| 411 | pub fn live_repositories(&self) -> usize { |
| 412 | self.repos.lock().map_or(0, |repos| repos.len()) |
| 413 | } |
| 414 | } |
| 415 | |
| 416 | |
| 417 | fn chunks(specs: &[String]) -> Vec<&[String]> { |
| 418 | let mut groups = Vec::new(); |
| 419 | let mut start = 0; |
| 420 | let mut bytes = 0; |
| 421 | |
| 422 | for (index, spec) in specs.iter().enumerate() { |
| 423 | let next = bytes + spec.len() + 1; |
| 424 | |
| 425 | if index > start && (index - start >= MAX_BATCH_SPECS || next > MAX_BATCH_BYTES) { |
| 426 | groups.push(&specs[start..index]); |
| 427 | start = index; |
| 428 | bytes = spec.len() + 1; |
| 429 | } else { |
| 430 | bytes = next; |
| 431 | } |
| 432 | } |
| 433 | |
| 434 | if start < specs.len() { |
| 435 | groups.push(&specs[start..]); |
| 436 | } |
| 437 | |
| 438 | groups |
| 439 | } |
| 440 | |
| 441 | |
| 442 | #[derive(Debug)] |
| 443 | enum Desync { |
| 444 | |
| 445 | Protocol(String), |
| 446 | |
| 447 | Gone(String), |
| 448 | } |
| 449 | |
| 450 | |
| 451 | #[derive(Debug)] |
| 452 | struct Batch { |
| 453 | child: Child, |
| 454 | stdin: ChildStdin, |
| 455 | stdout: BufReader<ChildStdout>, |
| 456 | stderr: Option<ChildStderr>, |
| 457 | } |
| 458 | |
| 459 | impl Batch { |
| 460 | |
| 461 | async fn run(&mut self, request: Request<'_>) -> Result<Reply, Desync> { |
| 462 | match request { |
| 463 | Request::Check(specs) => self.check(specs).await.map(Reply::Check), |
| 464 | Request::Contents(info) => self.contents(info).await.map(Reply::Contents), |
| 465 | } |
| 466 | } |
| 467 | |
| 468 | fn spawn(repo: &Path, mode: Mode) -> Result<Self, GitQueryError> { |
| 469 | let mut command = git_command(); |
| 470 | |
| 471 | command |
| 472 | .arg("-C") |
| 473 | .arg(repo) |
| 474 | .arg("cat-file") |
| 475 | .arg(mode.flag()) |
| 476 | |
| 477 | |
| 478 | .arg("-z") |
| 479 | .stdin(Stdio::piped()) |
| 480 | .stdout(Stdio::piped()) |
| 481 | .stderr(Stdio::piped()) |
| 482 | |
| 483 | |
| 484 | |
| 485 | .kill_on_drop(true); |
| 486 | count_spawn(&command); |
| 487 | |
| 488 | let mut child = command |
| 489 | .spawn() |
| 490 | .map_err(|error| GitQueryError::new(format!("could not run git cat-file: {error}")))?; |
| 491 | |
| 492 | let stdin = child.stdin.take().expect("stdin was piped"); |
| 493 | let stdout = BufReader::new(child.stdout.take().expect("stdout was piped")); |
| 494 | let stderr = child.stderr.take(); |
| 495 | |
| 496 | Ok(Self { |
| 497 | child, |
| 498 | stdin, |
| 499 | stdout, |
| 500 | stderr, |
| 501 | }) |
| 502 | } |
| 503 | |
| 504 | |
| 505 | async fn check(&mut self, specs: &[String]) -> Result<Vec<Option<ObjectInfo>>, Desync> { |
| 506 | let mut request = Vec::new(); |
| 507 | |
| 508 | for spec in specs { |
| 509 | request.extend_from_slice(spec.as_bytes()); |
| 510 | request.push(0); |
| 511 | } |
| 512 | |
| 513 | self.write(&request).await?; |
| 514 | |
| 515 | let mut answers = Vec::with_capacity(specs.len()); |
| 516 | |
| 517 | for _ in specs { |
| 518 | let line = self.line().await?; |
| 519 | |
| 520 | answers.push(parse_check_line(&line)?); |
| 521 | } |
| 522 | |
| 523 | Ok(answers) |
| 524 | } |
| 525 | |
| 526 | |
| 527 | async fn contents(&mut self, info: &ObjectInfo) -> Result<Vec<u8>, Desync> { |
| 528 | let mut request = info.id.as_str().as_bytes().to_vec(); |
| 529 | request.push(0); |
| 530 | |
| 531 | self.write(&request).await?; |
| 532 | |
| 533 | let line = self.line().await?; |
| 534 | |
| 535 | |
| 536 | |
| 537 | let Some(header) = parse_check_line(&line)? else { |
| 538 | return Err(Desync::Protocol(format!( |
| 539 | "{} is missing from a repository that had just described it", |
| 540 | info.id.as_str() |
| 541 | ))); |
| 542 | }; |
| 543 | |
| 544 | if &header != info { |
| 545 | return Err(Desync::Protocol(format!( |
| 546 | "asked for {:?} and was answered with {:?}", |
| 547 | info, header |
| 548 | ))); |
| 549 | } |
| 550 | |
| 551 | |
| 552 | |
| 553 | let mut body = vec![0u8; header.size as usize + 1]; |
| 554 | |
| 555 | self.stdout |
| 556 | .read_exact(&mut body) |
| 557 | .await |
| 558 | .map_err(|error| Desync::Gone(format!("reading {} bytes: {error}", header.size)))?; |
| 559 | |
| 560 | if body.pop() != Some(b'\n') { |
| 561 | return Err(Desync::Protocol(format!( |
| 562 | "{} was not followed by a newline", |
| 563 | header.id.as_str() |
| 564 | ))); |
| 565 | } |
| 566 | |
| 567 | Ok(body) |
| 568 | } |
| 569 | |
| 570 | async fn write(&mut self, request: &[u8]) -> Result<(), Desync> { |
| 571 | match self.stdin.write_all(request).await { |
| 572 | Ok(()) => self |
| 573 | .stdin |
| 574 | .flush() |
| 575 | .await |
| 576 | .map_err(|error| Desync::Gone(error.to_string())), |
| 577 | |
| 578 | |
| 579 | Err(error) => Err(Desync::Gone(self.why_gone(error).await)), |
| 580 | } |
| 581 | } |
| 582 | |
| 583 | async fn line(&mut self) -> Result<String, Desync> { |
| 584 | let mut line = String::new(); |
| 585 | |
| 586 | match self.stdout.read_line(&mut line).await { |
| 587 | |
| 588 | |
| 589 | Ok(0) => Err(Desync::Gone( |
| 590 | self.why_gone(ErrorKind::UnexpectedEof.into()).await, |
| 591 | )), |
| 592 | Ok(_) => Ok(line.trim_end_matches('\n').to_owned()), |
| 593 | Err(error) => Err(Desync::Gone(self.why_gone(error).await)), |
| 594 | } |
| 595 | } |
| 596 | |
| 597 | |
| 598 | |
| 599 | |
| 600 | |
| 601 | async fn why_gone(&mut self, error: std::io::Error) -> String { |
| 602 | let mut said = String::new(); |
| 603 | |
| 604 | if let Some(stderr) = self.stderr.as_mut() { |
| 605 | let mut buffer = vec![0u8; 4096]; |
| 606 | |
| 607 | if let Ok(read) = stderr.read(&mut buffer).await { |
| 608 | said = String::from_utf8_lossy(&buffer[..read]).trim().to_owned(); |
| 609 | } |
| 610 | } |
| 611 | |
| 612 | let status = self.child.try_wait().ok().flatten(); |
| 613 | |
| 614 | match (said.is_empty(), status) { |
| 615 | (false, _) => said, |
| 616 | (true, Some(status)) => format!("{error} ({status})"), |
| 617 | (true, None) => error.to_string(), |
| 618 | } |
| 619 | } |
| 620 | } |
| 621 | |
| 622 | |
| 623 | |
| 624 | |
| 625 | |
| 626 | fn parse_check_line(line: &str) -> Result<Option<ObjectInfo>, Desync> { |
| 627 | if line |
| 628 | .rsplit(' ') |
| 629 | .next() |
| 630 | .is_some_and(|last| NOT_FOUND_MARKERS.contains(&last)) |
| 631 | { |
| 632 | return Ok(None); |
| 633 | } |
| 634 | |
| 635 | let fields: Vec<&str> = line.split_whitespace().collect(); |
| 636 | |
| 637 | let [id, kind, size] = fields[..] else { |
| 638 | return Err(Desync::Protocol(format!( |
| 639 | "described an object in a shape we do not understand: {line:?}" |
| 640 | ))); |
| 641 | }; |
| 642 | |
| 643 | Ok(Some(ObjectInfo { |
| 644 | |
| 645 | |
| 646 | |
| 647 | id: ObjectId::new(id) |
| 648 | .map_err(|error| Desync::Protocol(format!("named a bad object id: {error}")))?, |
| 649 | kind: ObjectKind::from_str(kind) |
| 650 | .ok_or_else(|| Desync::Protocol(format!("reported an unknown object type {kind:?}")))?, |
| 651 | size: size |
| 652 | .parse() |
| 653 | .map_err(|_| Desync::Protocol(format!("reported an unreadable size {size:?}")))?, |
| 654 | })) |
| 655 | } |
| 656 | |
| 657 | #[cfg(test)] |
| 658 | mod tests { |
| 659 | use std::process::Command; |
| 660 | |
| 661 | use tempfile::TempDir; |
| 662 | |
| 663 | use super::*; |
| 664 | |
| 665 | |
| 666 | fn fixture() -> TempDir { |
| 667 | let dir = tempfile::tempdir().expect("temp dir"); |
| 668 | let path = dir.path(); |
| 669 | |
| 670 | run(path, &["init", "--quiet", "--initial-branch=main", "."]); |
| 671 | std::fs::create_dir(path.join("sub")).expect("mkdir"); |
| 672 | std::fs::write(path.join("a.txt"), b"hello\n").expect("write"); |
| 673 | std::fs::write(path.join("sub/b.txt"), b"nested\n").expect("write"); |
| 674 | run(path, &["add", "-A"]); |
| 675 | run( |
| 676 | path, |
| 677 | &[ |
| 678 | "-c", |
| 679 | "user.email=a@example.com", |
| 680 | "-c", |
| 681 | "user.name=A", |
| 682 | "commit", |
| 683 | "--quiet", |
| 684 | "-m", |
| 685 | "first", |
| 686 | ], |
| 687 | ); |
| 688 | |
| 689 | dir |
| 690 | } |
| 691 | |
| 692 | fn run(dir: &Path, args: &[&str]) { |
| 693 | let status = Command::new("git") |
| 694 | .arg("-C") |
| 695 | .arg(dir) |
| 696 | .args(args) |
| 697 | .status() |
| 698 | .expect("git runs"); |
| 699 | |
| 700 | assert!(status.success(), "git {args:?} failed"); |
| 701 | } |
| 702 | |
| 703 | #[tokio::test] |
| 704 | async fn an_object_is_described_by_id_type_and_size() { |
| 705 | let dir = fixture(); |
| 706 | let pool = CatFilePool::new(); |
| 707 | |
| 708 | let info = pool |
| 709 | .info(dir.path(), "HEAD:a.txt") |
| 710 | .await |
| 711 | .expect("asked") |
| 712 | .expect("found"); |
| 713 | |
| 714 | assert_eq!(info.kind, ObjectKind::Blob); |
| 715 | assert_eq!(info.size, 6); |
| 716 | } |
| 717 | |
| 718 | #[tokio::test] |
| 719 | async fn a_spec_that_names_nothing_is_not_a_failure() { |
| 720 | let dir = fixture(); |
| 721 | let pool = CatFilePool::new(); |
| 722 | |
| 723 | assert_eq!( |
| 724 | pool.info(dir.path(), "HEAD:nope").await.expect("asked"), |
| 725 | None |
| 726 | ); |
| 727 | assert_eq!( |
| 728 | pool.info(dir.path(), "nosuchref").await.expect("asked"), |
| 729 | None |
| 730 | ); |
| 731 | } |
| 732 | |
| 733 | #[tokio::test] |
| 734 | async fn contents_come_back_whole() { |
| 735 | let dir = fixture(); |
| 736 | let pool = CatFilePool::new(); |
| 737 | |
| 738 | let info = pool |
| 739 | .info(dir.path(), "HEAD:a.txt") |
| 740 | .await |
| 741 | .expect("asked") |
| 742 | .expect("found"); |
| 743 | |
| 744 | assert_eq!( |
| 745 | pool.contents(dir.path(), &info).await.expect("read"), |
| 746 | b"hello\n" |
| 747 | ); |
| 748 | } |
| 749 | |
| 750 | #[tokio::test] |
| 751 | async fn a_repository_is_asked_many_times_through_one_pair_of_processes() { |
| 752 | let dir = fixture(); |
| 753 | let pool = CatFilePool::new(); |
| 754 | |
| 755 | for _ in 0..5 { |
| 756 | let info = pool |
| 757 | .info(dir.path(), "HEAD:a.txt") |
| 758 | .await |
| 759 | .expect("asked") |
| 760 | .expect("found"); |
| 761 | |
| 762 | pool.contents(dir.path(), &info).await.expect("read"); |
| 763 | } |
| 764 | |
| 765 | assert_eq!(pool.live_repositories(), 1, "one repository, one slot"); |
| 766 | |
| 767 | |
| 768 | |
| 769 | let slot = pool.slot(dir.path()); |
| 770 | |
| 771 | assert!(slot.check.lock().await.is_some()); |
| 772 | assert!(slot.contents.lock().await.is_some()); |
| 773 | } |
| 774 | |
| 775 | #[tokio::test] |
| 776 | async fn an_exchange_and_a_respawn_each_move_their_own_counter() { |
| 777 | |
| 778 | |
| 779 | let metrics = crate::infrastructure::metrics::metrics(); |
| 780 | let requests = metrics.value_of("steid_cat_file_requests_total"); |
| 781 | let respawns = metrics.value_of("steid_cat_file_respawns_total"); |
| 782 | |
| 783 | let dir = fixture(); |
| 784 | let pool = CatFilePool::new(); |
| 785 | |
| 786 | pool.info(dir.path(), "HEAD:a.txt") |
| 787 | .await |
| 788 | .expect("asked") |
| 789 | .expect("found"); |
| 790 | |
| 791 | assert!( |
| 792 | metrics.value_of("steid_cat_file_requests_total") > requests, |
| 793 | "an exchange must be counted" |
| 794 | ); |
| 795 | |
| 796 | { |
| 797 | let slot = pool.slot(dir.path()); |
| 798 | let mut process = slot.check.lock().await; |
| 799 | let batch = process.as_mut().expect("spawned"); |
| 800 | |
| 801 | batch.child.start_kill().expect("kill"); |
| 802 | batch.child.wait().await.expect("reaped"); |
| 803 | } |
| 804 | |
| 805 | pool.info(dir.path(), "HEAD:a.txt") |
| 806 | .await |
| 807 | .expect("respawned and asked") |
| 808 | .expect("found"); |
| 809 | |
| 810 | assert!( |
| 811 | metrics.value_of("steid_cat_file_respawns_total") > respawns, |
| 812 | "a process found dead and started again must be counted" |
| 813 | ); |
| 814 | } |
| 815 | |
| 816 | #[tokio::test] |
| 817 | async fn a_killed_process_is_replaced_on_the_next_request() { |
| 818 | let dir = fixture(); |
| 819 | let pool = CatFilePool::new(); |
| 820 | |
| 821 | let info = pool |
| 822 | .info(dir.path(), "HEAD:a.txt") |
| 823 | .await |
| 824 | .expect("asked") |
| 825 | .expect("found"); |
| 826 | |
| 827 | |
| 828 | { |
| 829 | let slot = pool.slot(dir.path()); |
| 830 | let mut process = slot.check.lock().await; |
| 831 | let batch = process.as_mut().expect("spawned"); |
| 832 | |
| 833 | batch.child.start_kill().expect("kill"); |
| 834 | batch.child.wait().await.expect("reaped"); |
| 835 | } |
| 836 | |
| 837 | let again = pool |
| 838 | .info(dir.path(), "HEAD:a.txt") |
| 839 | .await |
| 840 | .expect("respawned and asked") |
| 841 | .expect("found"); |
| 842 | |
| 843 | assert_eq!(again, info); |
| 844 | } |
| 845 | |
| 846 | #[tokio::test] |
| 847 | async fn a_reply_that_does_not_match_the_question_kills_the_process() { |
| 848 | let dir = fixture(); |
| 849 | let pool = CatFilePool::new(); |
| 850 | |
| 851 | let mut info = pool |
| 852 | .info(dir.path(), "HEAD:a.txt") |
| 853 | .await |
| 854 | .expect("asked") |
| 855 | .expect("found"); |
| 856 | |
| 857 | |
| 858 | |
| 859 | info.size += 1; |
| 860 | |
| 861 | let error = pool |
| 862 | .contents(dir.path(), &info) |
| 863 | .await |
| 864 | .expect_err("a mismatched header is not an answer"); |
| 865 | |
| 866 | assert!( |
| 867 | error.to_string().contains("spoke out of turn"), |
| 868 | "expected a desync, got {error}" |
| 869 | ); |
| 870 | |
| 871 | |
| 872 | let slot = pool.slot(dir.path()); |
| 873 | assert!(slot.contents.lock().await.is_none()); |
| 874 | } |
| 875 | |
| 876 | #[tokio::test] |
| 877 | async fn the_stream_still_answers_correctly_after_a_desync() { |
| 878 | let dir = fixture(); |
| 879 | let pool = CatFilePool::new(); |
| 880 | |
| 881 | let real = pool |
| 882 | .info(dir.path(), "HEAD:a.txt") |
| 883 | .await |
| 884 | .expect("asked") |
| 885 | .expect("found"); |
| 886 | |
| 887 | let mut wrong = real.clone(); |
| 888 | wrong.size += 1; |
| 889 | |
| 890 | pool.contents(dir.path(), &wrong).await.expect_err("desync"); |
| 891 | |
| 892 | |
| 893 | |
| 894 | assert_eq!( |
| 895 | pool.contents(dir.path(), &real).await.expect("read"), |
| 896 | b"hello\n" |
| 897 | ); |
| 898 | } |
| 899 | |
| 900 | #[tokio::test] |
| 901 | async fn a_repository_that_is_not_there_is_an_error() { |
| 902 | let dir = fixture(); |
| 903 | let pool = CatFilePool::new(); |
| 904 | |
| 905 | let error = pool |
| 906 | .info(&dir.path().join("nowhere"), "HEAD") |
| 907 | .await |
| 908 | .expect_err("no repository, no answer"); |
| 909 | |
| 910 | assert!( |
| 911 | error.to_string().contains("would not stay running"), |
| 912 | "expected a spawn failure, got {error}" |
| 913 | ); |
| 914 | } |
| 915 | |
| 916 | #[tokio::test] |
| 917 | async fn the_pool_holds_at_most_its_cap() { |
| 918 | let pool = CatFilePool::new(); |
| 919 | let dir = fixture(); |
| 920 | |
| 921 | |
| 922 | |
| 923 | |
| 924 | for index in 0..MAX_LIVE_REPOSITORIES + 10 { |
| 925 | pool.slot(&dir.path().join(format!("repo-{index}"))); |
| 926 | } |
| 927 | |
| 928 | assert_eq!(pool.live_repositories(), MAX_LIVE_REPOSITORIES); |
| 929 | } |
| 930 | |
| 931 | #[tokio::test] |
| 932 | async fn the_repository_asked_about_most_recently_survives_eviction() { |
| 933 | let pool = CatFilePool::new(); |
| 934 | let dir = fixture(); |
| 935 | let kept = dir.path().join("repo-0"); |
| 936 | |
| 937 | pool.slot(&kept); |
| 938 | |
| 939 | for index in 1..MAX_LIVE_REPOSITORIES { |
| 940 | pool.slot(&dir.path().join(format!("repo-{index}"))); |
| 941 | } |
| 942 | |
| 943 | |
| 944 | pool.slot(&kept); |
| 945 | pool.slot(&dir.path().join("one-too-many")); |
| 946 | |
| 947 | let repos = pool.repos.lock().expect("lock poisoned"); |
| 948 | |
| 949 | assert_eq!(repos.len(), MAX_LIVE_REPOSITORIES); |
| 950 | assert!(repos.contains_key(&kept), "the most recent was evicted"); |
| 951 | } |
| 952 | |
| 953 | #[test] |
| 954 | fn specs_are_written_in_groups_a_pipe_can_hold() { |
| 955 | let many: Vec<String> = (0..MAX_BATCH_SPECS * 2 + 3) |
| 956 | .map(|index| format!("{index:040}")) |
| 957 | .collect(); |
| 958 | |
| 959 | let groups = chunks(&many); |
| 960 | |
| 961 | assert!(groups.iter().all(|group| group.len() <= MAX_BATCH_SPECS)); |
| 962 | assert!(groups.iter().all( |
| 963 | |group| group.iter().map(|spec| spec.len() + 1).sum::<usize>() <= MAX_BATCH_BYTES |
| 964 | )); |
| 965 | assert_eq!( |
| 966 | groups.iter().map(|group| group.len()).sum::<usize>(), |
| 967 | many.len() |
| 968 | ); |
| 969 | } |
| 970 | |
| 971 | #[test] |
| 972 | fn one_enormous_spec_is_still_a_group_of_its_own() { |
| 973 | let specs = vec!["a".repeat(MAX_BATCH_BYTES * 2), "b".to_owned()]; |
| 974 | let groups = chunks(&specs); |
| 975 | |
| 976 | assert_eq!(groups.len(), 2, "a spec too big to batch still gets asked"); |
| 977 | } |
| 978 | } |