#![cfg(target_os = "linux")]
#![allow(
clippy::unwrap_used,
clippy::expect_used,
clippy::panic,
clippy::print_stderr,
unused_results,
unused_must_use
)]
use std::ops::ControlFlow;
use std::path::{Path, PathBuf};
use std::sync::atomic::{AtomicU32, Ordering};
use std::sync::{Mutex, MutexGuard, PoisonError, mpsc};
use std::time::{Duration, Instant};
use store_io::{
CommitHold, CopyOptions, IoInterface, LinuxTier, ScanItem, Store, StoreOptions, Trust,
copy_store,
};
const MIB: u64 = 1 << 20;
const LOG: u64 = 24 * MIB;
const PAGES: u64 = 8 * MIB;
const UNPACED: u64 = 4;
const WATCHDOG: Duration = Duration::from_secs(120);
fn opts() -> StoreOptions {
StoreOptions {
trust: Trust {
override_refusal: true,
..Trust::default()
},
linux_tier: LinuxTier::Uring,
commit_quiet: Duration::from_millis(50),
background_unpaced: UNPACED as u32,
..StoreOptions::default()
}
}
fn serial() -> MutexGuard<'static, ()> {
static ONE: Mutex<()> = Mutex::new(());
ONE.lock().unwrap_or_else(PoisonError::into_inner)
}
struct Dir(PathBuf);
impl Dir {
fn new(tag: &str) -> Self {
static N: AtomicU32 = AtomicU32::new(0);
let base = std::env::var_os("STORE_IO_TEST_DIR")
.or_else(|| std::env::var_os("HOME"))
.map_or_else(std::env::temp_dir, PathBuf::from);
let n = N.fetch_add(1, Ordering::Relaxed);
let dir = base.join(format!(
".store-io-progress-{}-{tag}-{n}",
std::process::id()
));
let _gone = std::fs::remove_dir_all(&dir);
std::fs::create_dir_all(&dir).unwrap();
Self(dir)
}
fn join(&self, name: &str) -> PathBuf {
self.0.join(name)
}
}
impl Drop for Dir {
fn drop(&mut self) {
let _cleanup = std::fs::remove_dir_all(&self.0);
}
}
fn create(path: &Path) -> Option<Store> {
match Store::create(path, opts()) {
Ok(s) => {
assert_eq!(
s.report().io.interface,
IoInterface::IoUring,
"{:?}",
s.report().io
);
Some(s)
}
Err(e) => {
assert!(
std::env::var_os("STORE_IO_EXPECT_URING").is_none(),
"io_uring refused ({e}) where STORE_IO_EXPECT_URING says it works"
);
eprintln!("io_uring refused here ({e}): the progress scenarios are skipped");
None
}
}
}
fn byte(i: u64, seed: u8) -> u8 {
let word = (i / 8).wrapping_mul(0x9E37_79B9_7F4A_7C15);
(word >> ((i % 8) * 8)) as u8 ^ seed
}
fn chunk(at: u64, len: u64, seed: u8) -> Vec<u8> {
(at..at + len).map(|i| byte(i, seed)).collect()
}
fn fill(s: &Store) {
let log = s.provision_append_region("log", LOG).unwrap();
let mut last = None;
for at in (0..LOG).step_by(MIB as usize) {
last = Some(log.append(&chunk(at, MIB, 0x10)).unwrap());
}
log.sync_through(&last.unwrap()).unwrap();
let pages = s.provision_page_region("pages", PAGES).unwrap();
for at in (0..PAGES).step_by(MIB as usize) {
pages
.write_durable(pages.pos(at).unwrap(), &chunk(at, MIB, 0x20))
.unwrap();
}
}
fn check(s: &Store) {
let log = s.append_region("log").unwrap();
assert_eq!(log.len(), LOG);
let mut back = vec![0u8; MIB as usize];
for at in (0..LOG).step_by(MIB as usize) {
assert_eq!(log.read(at, &mut back).unwrap(), MIB as usize);
assert!(back == chunk(at, MIB, 0x10), "log at {at}");
}
let pages = s.page_region("pages").unwrap();
for at in (0..PAGES).step_by(MIB as usize) {
assert_eq!(
pages.read(pages.pos(at).unwrap(), &mut back).unwrap(),
MIB as usize
);
assert!(back == chunk(at, MIB, 0x20), "pages at {at}");
}
}
#[derive(Clone, Copy, Debug)]
enum Mode {
Paced,
Unpaced,
Switch,
}
fn wait_quiet(holder: &Store) {
let deadline = Instant::now() + Duration::from_secs(10);
while holder.qos().active {
assert!(Instant::now() < deadline, "commits never went quiet");
std::thread::sleep(Duration::from_millis(10));
}
}
fn within<T: Send + 'static>(
name: &str,
f: impl FnOnce() -> T + Send + 'static,
meanwhile: impl FnOnce(&dyn Fn() -> bool),
) -> T {
let (tx, rx) = mpsc::channel();
let t = std::thread::Builder::new()
.name(format!("progress-{name}"))
.spawn(move || {
let _sent = tx.send(f());
})
.unwrap();
meanwhile(&|| !t.is_finished());
match rx.recv_timeout(WATCHDOG) {
Ok(v) => {
t.join().unwrap();
v
}
Err(mpsc::RecvTimeoutError::Timeout) => {
eprintln!("{name}: not complete within {WATCHDOG:?}: a call waiting on itself");
std::process::abort()
}
Err(mpsc::RecvTimeoutError::Disconnected) => match t.join() {
Err(p) => std::panic::resume_unwind(p),
Ok(()) => panic!("{name}: ended with no result"),
},
}
}
fn copy_in(mode: Mode) {
let _one = serial();
let name = format!("copy-{mode:?}");
let dir = Dir::new(&name);
let Some(holder) = create(&dir.join("holder")) else {
return;
};
let src = create(&dir.join("src")).unwrap();
fill(&src);
drop(src);
let mut hold: Option<CommitHold> = None;
match mode {
Mode::Paced => {
hold = Some(holder.commit_hold());
assert!(holder.qos().active);
}
Mode::Unpaced | Mode::Switch => wait_quiet(&holder),
}
let (from, to) = (dir.join("src"), dir.join("dst"));
let (mut before, mut after) = (0u64, 0u64);
let report = within(
&name,
move || {
copy_store(
from,
to,
CopyOptions {
store: opts(),
..CopyOptions::default()
},
)
},
|running| {
while running() {
let n = holder.qos().in_flight;
if hold.is_some() && matches!(mode, Mode::Switch) {
after = after.max(n);
} else {
before = before.max(n);
}
if matches!(mode, Mode::Switch) && hold.is_none() && n > 0 {
assert!(!holder.qos().active, "{name}: paced before the switch");
hold = Some(holder.commit_hold());
assert!(running(), "{name}: the copy ended at the switch");
eprintln!("{name}: switched to paced with {n} piece(s) in flight");
}
std::thread::yield_now();
}
},
)
.unwrap();
eprintln!("{name}: at most {before} piece(s) in flight, {after} after the switch");
const CHECKING: u64 = 2;
match mode {
Mode::Paced => assert!(
(1..=1 + CHECKING).contains(&before),
"{name}: {before} in flight"
),
Mode::Unpaced => {
assert!(
(2..=UNPACED + CHECKING).contains(&before),
"{name}: {before} in flight"
);
assert!(!holder.qos().active, "{name}: commits went active");
}
Mode::Switch => {
assert!(hold.is_some(), "{name}: the switch was never reached");
assert!(after <= UNPACED + CHECKING, "{name}: {after} in flight");
}
}
drop(hold);
assert_eq!(report.regions, 2, "{report:?}");
assert_eq!(report.bytes, LOG + PAGES, "{report:?}");
let dst = Store::open_with(dir.join("dst"), opts()).unwrap();
check(&dst);
}
fn scan_in(mode: Mode) {
let _one = serial();
let name = format!("scan-{mode:?}");
let dir = Dir::new(&name);
let Some(s) = create(&dir.join("s")) else {
return;
};
fill(&s);
let mut hold = None;
match mode {
Mode::Paced => hold = Some(s.commit_hold()),
Mode::Unpaced | Mode::Switch => wait_quiet(&s),
}
let s2 = s.clone();
let n = name.clone();
let (sum, read, most, switched) = within(
&name,
move || {
let log = s2.append_region("log").unwrap();
let (mut next, mut most) = (0u64, 0u64);
let mut held = None;
let sum = log.scan(0, |item| {
if let ScanItem::Data { offset, bytes } = item {
assert_eq!(offset, next, "delivered in order");
assert!(
bytes == chunk(offset, bytes.len() as u64, 0x10).as_slice(),
"the scan's bytes at {offset}"
);
next += bytes.len() as u64;
let in_flight = s2.qos().in_flight;
most = most.max(in_flight);
if matches!(mode, Mode::Switch)
&& held.is_none()
&& next >= LOG / 3
&& in_flight > 0
{
assert!(!s2.qos().active, "{n}: paced before the switch");
held = Some(s2.commit_hold());
assert!(s2.qos().active);
}
}
ControlFlow::Continue(())
});
(sum, next, most, held.is_some())
},
|_| {},
);
eprintln!("{name}: at most {most} read(s) in flight at a delivery");
match mode {
Mode::Paced => assert!(most <= 1, "{name}: {most} in flight"),
Mode::Unpaced => {
assert!((2..=UNPACED).contains(&most), "{name}: {most} in flight");
assert!(!s.qos().active, "{name}: commits went active");
}
Mode::Switch => assert!(switched, "{name}: the switch was never reached"),
}
drop(hold);
let sum = sum.unwrap();
assert_eq!(sum.unreadable, 0);
assert_eq!(read, LOG, "every byte delivered");
}
#[test]
fn test_a_paced_copy_completes() {
copy_in(Mode::Paced);
}
#[test]
fn test_an_unpaced_copy_completes() {
copy_in(Mode::Unpaced);
}
#[test]
fn test_a_copy_switched_from_unpaced_to_paced_completes() {
copy_in(Mode::Switch);
}
#[test]
fn test_a_paced_scan_completes() {
scan_in(Mode::Paced);
}
#[test]
fn test_an_unpaced_scan_completes() {
scan_in(Mode::Unpaced);
}
#[test]
fn test_a_scan_switched_from_unpaced_to_paced_completes() {
scan_in(Mode::Switch);
}