use std::path::{Path, PathBuf};
use std::time::Duration;
use libch::pool::{Pool, PoolOptions};
use libch::s3::S3Client;
use libch::worker::OpWriter;
use libch::writer::Budget;
use libch::{Outcome, Writer};
use libch_testkit::ops::{assert_clean, insert, tenant_with_parts, totals};
use libch_testkit::schema::{batch, events};
use libch_testkit::{Stack, stack};
const LARGE: u64 = 60_000;
struct Scratch(PathBuf);
impl std::ops::Deref for Scratch {
type Target = Path;
fn deref(&self) -> &Path {
&self.0
}
}
impl Drop for Scratch {
fn drop(&mut self) {
let _ = std::fs::remove_dir_all(&self.0);
}
}
fn forking_pool(stack: &Stack, waiting: usize, concurrency: usize) -> (Pool, Scratch) {
let scratch = Scratch(stack.work_dir.join(format!("forking-{:016x}", rand::random::<u64>())));
let pool = Pool::new(stack.worker_command().scratch(&*scratch).forking(), PoolOptions { waiting, concurrency }).unwrap();
(pool, scratch)
}
fn parents(scratch: &Path) -> Vec<u32> {
let me = std::process::id().to_string();
let under = format!("--path={}", scratch.display());
let mut found = Vec::new();
for entry in std::fs::read_dir("/proc").unwrap().flatten() {
let Some(pid) = entry.file_name().to_str().and_then(|name| name.parse::<u32>().ok()) else { continue };
let Ok(stat) = std::fs::read_to_string(format!("/proc/{pid}/stat")) else { continue };
let parent = stat.rsplit_once(") ").and_then(|(_, rest)| rest.split(' ').nth(1).map(str::to_string));
let command = std::fs::read(format!("/proc/{pid}/cmdline")).unwrap_or_default();
let arguments: Vec<&[u8]> = command.split(|&byte| byte == 0).collect();
let ours = arguments.iter().any(|argument| argument.starts_with(under.as_bytes()));
if parent.as_deref() == Some(me.as_str()) && ours && arguments.contains(&b"--fork".as_slice()) {
found.push(pid);
}
}
found
}
fn threads_of(pid: u32) -> usize {
std::fs::read_dir(format!("/proc/{pid}/task")).map(|tasks| tasks.count()).unwrap_or(0)
}
fn memory_holds(pid: u32, needle: &[u8]) -> bool {
use std::io::{Read, Seek, SeekFrom};
let maps = std::fs::read_to_string(format!("/proc/{pid}/maps")).unwrap();
let mut memory = std::fs::File::open(format!("/proc/{pid}/mem")).unwrap();
for line in maps.lines() {
let mut fields = line.split_whitespace();
let (Some(range), Some(permissions)) = (fields.next(), fields.next()) else { continue };
if !permissions.starts_with("rw") {
continue;
}
let Some((start, end)) = range.split_once('-') else { continue };
let (start, end) = (u64::from_str_radix(start, 16).unwrap(), u64::from_str_radix(end, 16).unwrap());
let mut region = vec![0u8; (end - start) as usize];
if memory.seek(SeekFrom::Start(start)).is_err() || memory.read_exact(&mut region).is_err() {
continue;
}
if region.windows(needle.len()).any(|window| window == needle) {
return true;
}
}
false
}
#[tokio::test]
async fn a_parent_serves_many_operations_and_keeps_nothing_of_them() {
let operations: u64 = std::env::var("LIBCH_FORK_OPERATIONS").ok().and_then(|count| count.parse().ok()).unwrap_or(200);
let stack = stack().await;
let (first, _) = tenant_with_parts(0).await;
let (second, _) = tenant_with_parts(0).await;
let (pool, scratch) = forking_pool(stack, 1, 1);
let writers = [pool.writer(&first.tenant, &first.key), pool.writer(&second.tenant, &second.key)];
let (mut sums, mut rows) = ([0u64; 2], [0u64; 2]);
for operation in 0..operations {
let tenant = (operation % 2) as usize;
if operation % 20 < 2 {
let batch = batch(operation, if operation < 40 { LARGE } else { 100 });
assert!(insert(&writers[tenant], &batch).await.unwrap().is_committed(), "operation {operation}");
sums[tenant] += batch.sum;
rows[tenant] += batch.rows;
} else {
assert_eq!(writers[tenant].describe(false).await.unwrap().tables.len(), 3, "operation {operation}");
}
}
let stats = pool.stats();
assert!(stats.started <= 2, "the pool's one parent served every operation: {stats:?}");
let parents = parents(&scratch);
assert_eq!(parents.len(), 1, "{parents:?}");
assert_eq!(threads_of(parents[0]), 1, "the parent forks from its only thread");
for fixture in [&first, &second] {
assert!(!memory_holds(parents[0], fixture.key.as_bytes()), "the parent's memory holds a tenant's key");
}
assert!(memory_holds(parents[0], b"--fork"));
let mut names = Vec::new();
for fixture in [&first, &second] {
let prefix = fixture.tenant.s3.key_prefix();
let objects = S3Client::new(&fixture.tenant.s3).unwrap().list(&prefix).await.unwrap();
assert!(objects.len() >= 2, "the large batches keep blobs in S3: {objects:?}");
names.extend(objects.into_iter().map(|object| object.key[prefix.len()..].to_string()));
}
let distinct: std::collections::HashSet<&String> = names.iter().collect();
assert_eq!(distinct.len(), names.len(), "{names:?}");
for (tenant, fixture) in [&first, &second].into_iter().enumerate() {
assert_eq!(totals(fixture).await, (rows[tenant], sums[tenant], sums[tenant]), "tenant {tenant}");
assert_clean(fixture).await;
}
println!("{operations} operations through one parent: {stats:?}");
}
#[tokio::test(flavor = "multi_thread")]
async fn operations_run_through_forking_workers() {
let stack = stack().await;
let (fixture, _) = tenant_with_parts(0).await;
let (pool, _scratch) = forking_pool(stack, 4, 4);
let writer = pool.writer(&fixture.tenant, &fixture.key);
let batches: Vec<_> = (0..12).map(|index| batch(index, if index == 11 { 60_000 } else { 100 })).collect();
let inserted = futures_util::future::join_all(batches.iter().map(|batch| insert(&writer, batch))).await;
assert!(inserted.into_iter().all(|outcome| outcome.unwrap().is_committed()));
let planned = writer.plan(&events(), &Budget::default()).await.unwrap();
assert!(!planned.is_empty());
for merge in &planned {
assert!(writer.merge(&events(), &merge.candidate).await.unwrap().is_committed());
}
assert!(writer.housekeep(&events()).await.unwrap().is_committed());
assert_eq!(insert(&writer, &batches[0]).await.unwrap(), Outcome::Duplicate, "a batch sent again finds its token");
let sum = batches.iter().map(|batch| batch.sum).sum();
assert_eq!(totals(&fixture).await, (60_000 + 11 * 100, sum, sum));
assert!(pool.stats().started <= 8, "{:?}", pool.stats());
assert_clean(&fixture).await;
}
#[tokio::test]
async fn a_deadline_kills_the_parent_and_its_child() {
let stack = stack().await;
let (fixture, _) = tenant_with_parts(1).await;
let (pool, scratch) = forking_pool(stack, 1, 1);
let writer = pool.writer(&fixture.tenant, &fixture.key);
writer.describe(false).await.unwrap();
let before = parents(&scratch);
assert_eq!(before.len(), 1);
let second = batch(1, 100);
let held = writer.clone().with_settings_xml(libch_testkit::ops::fail_point("inodedb_insert_pause_before_commit")).with_deadline(Duration::from_secs(2));
let killed = insert(&held, &second).await.unwrap_err();
assert!(killed.to_string().contains("was killed"), "{killed}");
tokio::time::sleep(Duration::from_millis(300)).await;
assert!(!std::path::Path::new(&format!("/proc/{}", before[0])).exists() || threads_of(before[0]) == 0);
assert!(insert(&writer, &second).await.unwrap().is_committed());
let after = parents(&scratch);
assert!(after.len() == 1 && after != before, "{before:?} then {after:?}");
assert_eq!(totals(&fixture).await.0, 200);
assert_clean(&fixture).await;
}
#[tokio::test]
async fn forking_workers_run_in_the_sandbox() {
let stack = stack().await;
let (fixture, _) = tenant_with_parts(0).await;
let pool = Pool::new(stack.sandboxed_worker_command().forking(), PoolOptions { waiting: 1, concurrency: 1 }).unwrap();
let writer: OpWriter = pool.writer(&fixture.tenant, &fixture.key);
let mut sum = 0;
for index in 0..6 {
let batch = batch(index, if index == 5 { 60_000 } else { 100 });
sum += batch.sum;
assert!(insert(&writer, &batch).await.unwrap().is_committed());
}
for merge in &writer.plan(&events(), &Budget::default()).await.unwrap() {
assert!(writer.merge(&events(), &merge.candidate).await.unwrap().is_committed());
}
assert!(writer.housekeep(&events()).await.unwrap().is_committed());
assert_eq!(totals(&fixture).await, (60_500, sum, sum));
assert!(pool.stats().started <= 2, "{:?}", pool.stats());
assert_clean(&fixture).await;
}
#[tokio::test]
async fn a_failed_insert_leaves_a_parent_ready_for_the_next_request() {
use libch::writer::{Insert, TableName};
let stack = stack().await;
let (fixture, _) = tenant_with_parts(0).await;
let (pool, scratch) = forking_pool(stack, 1, 1);
let writer = pool.writer(&fixture.tenant, &fixture.key);
assert!(insert(&writer, &batch(0, 100)).await.unwrap().is_committed());
let large = batch(1, LARGE);
let absent = TableName::new("d", "absent");
let refused = writer.insert(Insert { table: &absent, format: "JSONEachRow", token: "absent", data: large.data.clone() }).await.unwrap_err();
assert_eq!(refused.clickhouse_code(), Some(60), "{refused}");
assert!(insert(&writer, &batch(2, 100)).await.unwrap().is_committed());
let mut broken = large.data.to_vec();
broken.truncate(broken.len() / 3);
broken.extend_from_slice(b"{\"ts\":\"not a time\n");
broken.extend_from_slice(&large.data);
let refused = writer.insert(Insert { table: &events(), format: "JSONEachRow", token: "broken", data: broken.into() }).await.unwrap_err();
assert!(refused.clickhouse_code().is_some(), "{refused}");
assert!(insert(&writer, &batch(3, 100)).await.unwrap().is_committed());
writer.plan(&events(), &Budget::default()).await.unwrap();
assert!(insert(&writer, &large).await.unwrap().is_committed());
let sum = batch(0, 100).sum + batch(2, 100).sum + batch(3, 100).sum + large.sum;
assert_eq!(totals(&fixture).await, (300 + LARGE, sum, sum));
assert!(pool.stats().started <= 2, "the pool's one parent served every request: {:?}", pool.stats());
assert_eq!(parents(&scratch).len(), 1);
assert_clean(&fixture).await;
}
#[tokio::test]
async fn a_child_that_dies_takes_its_parent_out_of_the_pool() {
let stack = stack().await;
let (fixture, _) = tenant_with_parts(1).await;
let (pool, scratch) = forking_pool(stack, 1, 1);
let writer = pool.writer(&fixture.tenant, &fixture.key);
writer.describe(false).await.unwrap();
let before = parents(&scratch);
assert_eq!(before.len(), 1);
let second = batch(1, 100);
let held = writer.clone().with_settings_xml(libch_testkit::ops::fail_point("inodedb_insert_pause_before_commit"));
let insert_held = {
let second = second.clone();
tokio::spawn(async move { insert(&held, &second).await })
};
let children = || std::fs::read_to_string(format!("/proc/{0}/task/{0}/children", before[0])).unwrap_or_default();
let started = std::time::Instant::now();
let child: u32 = loop {
if let Some(child) = children().split_whitespace().next().and_then(|pid| pid.parse().ok()) {
break child;
}
assert!(started.elapsed() < Duration::from_secs(30), "the parent forked no child");
tokio::time::sleep(Duration::from_millis(5)).await;
};
while threads_of(child) < 8 {
assert!(started.elapsed() < Duration::from_secs(30), "the child never attached the tenant's disk");
tokio::time::sleep(Duration::from_millis(5)).await;
}
assert!(std::process::Command::new("kill").args(["-KILL", &child.to_string()]).status().unwrap().success());
let failed = insert_held.await.unwrap().unwrap_err();
assert!(failed.to_string().contains("child failed with signal: 9"), "{failed}");
assert!(insert(&writer, &second).await.unwrap().is_committed());
let after = parents(&scratch);
assert!(after.len() == 1 && after != before, "{before:?} then {after:?}");
assert_eq!(totals(&fixture).await.0, 200);
assert_clean(&fixture).await;
}