use std::time::{Duration, Instant};
use libch::Writer;
use libch::check;
use libch::worker::OpWriter;
use libch_testkit::ops::{assert_clean, fail_point, insert, merge, short_grace, stored, tenant_with_parts};
use libch_testkit::schema::{batch, events};
const LARGE: u64 = 60_000;
fn doomed_at(writer: &OpWriter, point: &str) -> OpWriter {
writer.clone().with_settings_xml(fail_point(point)).with_deadline(Duration::from_secs(2))
}
#[tokio::test]
async fn operations_that_finish_leave_no_orphan() {
let (fixture, writer) = tenant_with_parts(0).await;
for index in 0..3 {
assert!(insert(&writer, &batch(index, LARGE)).await.unwrap().is_committed());
}
let written = check::orphans(&fixture.tenant, &fixture.key).await.unwrap();
assert!(written.named > 0, "the parts keep blobs in S3: {written}");
assert_eq!((written.freed, written.orphans.len()), (0, 0), "{written}");
assert_eq!(written.stored, written.named, "{written}");
let sources = ["202611_1_1_0", "202611_2_2_0", "202611_3_3_0"];
assert!(merge(&writer, &events(), "202611", &sources).await.unwrap().is_committed());
let merged = check::orphans(&fixture.tenant, &fixture.key).await.unwrap();
assert!(merged.stored > written.stored, "the merged part's blobs joined the sources': {merged}");
assert_eq!(merged.orphans.len(), 0, "{merged}");
tokio::time::sleep(Duration::from_millis(1_100)).await;
let housekeeper = writer.clone().with_settings_xml(short_grace());
let housekept = housekeeper.housekeep(&events()).await.unwrap().committed().unwrap();
assert_eq!(housekept.removed.len(), 3, "{housekept:?}");
let mut blobs_removed = housekept.blobs_removed;
println!("the housekeeping that removed the parts removed {blobs_removed} blobs");
let started = Instant::now();
let after = loop {
let found = check::orphans(&fixture.tenant, &fixture.key).await.unwrap();
assert_eq!(found.orphans.len(), 0, "{found}");
if found.freed == 0 {
break found;
}
assert!(started.elapsed() < Duration::from_secs(60), "freed blobs stay in S3: {found}");
tokio::time::sleep(Duration::from_millis(500)).await;
blobs_removed += housekeeper.housekeep(&events()).await.unwrap().committed().unwrap().blobs_removed;
};
assert_eq!(after.stored, after.named, "{after}");
assert_eq!(blobs_removed as usize, merged.stored - after.stored, "housekeeping counts the blobs it deletes");
assert_clean(&fixture).await;
}
#[tokio::test]
async fn a_worker_killed_before_its_commit_leaves_its_uploads_as_orphans() {
let (fixture, writer) = tenant_with_parts(0).await;
assert!(insert(&writer, &batch(0, LARGE)).await.unwrap().is_committed());
let one = check::orphans(&fixture.tenant, &fixture.key).await.unwrap();
let killed = insert(&doomed_at(&writer, "inodedb_insert_pause_before_commit"), &batch(1, LARGE)).await;
assert!(killed.unwrap_err().to_string().contains("was killed"));
let left = check::orphans(&fixture.tenant, &fixture.key).await.unwrap();
assert_eq!(left.named, one.named, "the dead worker committed nothing: {left}");
assert!(!left.orphans.is_empty(), "its uploads stay in S3: {left}");
assert!(left.bytes() > 0, "{left}");
assert_eq!(left.stored, left.named + left.orphans.len(), "{left}");
assert!(insert(&writer, &batch(1, LARGE)).await.unwrap().is_committed());
writer.housekeep(&events()).await.unwrap();
let after = check::orphans(&fixture.tenant, &fixture.key).await.unwrap();
assert_eq!(after.orphans, left.orphans, "{after}");
assert!(after.named > one.named, "{after}");
assert_clean(&fixture).await;
}
#[tokio::test]
async fn a_merge_killed_before_its_commit_leaves_its_part_as_orphans() {
let (fixture, writer) = tenant_with_parts(0).await;
for index in 0..2 {
assert!(insert(&writer, &batch(index, LARGE)).await.unwrap().is_committed());
}
let before = check::orphans(&fixture.tenant, &fixture.key).await.unwrap();
let sources = ["202611_1_1_0", "202611_2_2_0"];
let killed = merge(&doomed_at(&writer, "inodedb_merge_pause_before_commit"), &events(), "202611", &sources).await;
assert!(killed.unwrap_err().to_string().contains("was killed"));
let table = stored(&fixture, &events()).await;
let temporary: Vec<_> = table.other.iter().filter(|name| name.starts_with("tmp_merge_202611_1_2_1_")).collect();
assert_eq!(temporary.len(), 1, "{:?}", table.other);
let catalog = fixture.catalog().await.unwrap();
let definition = catalog.table(&events()).await.unwrap().unwrap();
assert_eq!(catalog.part_files(&definition, temporary[0]).await.unwrap(), ["by_kind.proj"]);
let left = check::orphans(&fixture.tenant, &fixture.key).await.unwrap();
assert_eq!(left.named, before.named, "{left}");
assert!(!left.orphans.is_empty(), "the merged part's blobs: {left}");
tokio::time::sleep(Duration::from_millis(1_100)).await;
let housekeeper = writer.clone().with_settings_xml("<merge_tree><temporary_directories_lifetime>1</temporary_directories_lifetime></merge_tree>");
let housekept = housekeeper.housekeep(&events()).await.unwrap().committed().unwrap();
assert_eq!(housekept.temporary_directories, 1, "{housekept:?}");
let after = check::orphans(&fixture.tenant, &fixture.key).await.unwrap();
assert_eq!((after.named, after.orphans.clone()), (before.named, left.orphans), "{after}");
assert_clean(&fixture).await;
}