use super::*;
use crate::background::{CheckpointWorkContext, CheckpointWorkProbe};
use hawdb_qos::{LocalQosPolicy, LocalQosScheduler};
use std::sync::atomic::{AtomicU64, Ordering};
use std::sync::Arc;
use std::time::{SystemTime, UNIX_EPOCH};
const PAYLOAD_BYTES: usize = 3 * STREAM_BUFFER_BYTES + 17;
const BLOCKS_PER_PASS: usize = 5;
fn scheduler() -> LocalQosScheduler {
LocalQosScheduler::new(LocalQosPolicy {
max_background_operations: Some(1),
max_total_background_operations: Some(1),
..LocalQosPolicy::default()
})
}
fn test_root(label: &str) -> PathBuf {
let nanos = SystemTime::now()
.duration_since(UNIX_EPOCH)
.unwrap()
.as_nanos();
std::env::temp_dir().join(format!("hawdb-immutable-work-{label}-{nanos}"))
}
fn staging_entries(root: &Path) -> usize {
fs::read_dir(root.join(OBJECTS_DIRECTORY).join(STAGING_DIRECTORY))
.map(|entries| entries.count())
.unwrap_or(0)
}
fn source(root: &Path) -> (PathBuf, Vec<u8>, ObjectReference) {
fs::create_dir_all(root).unwrap();
let path = root.join("source.bin");
let payload = (0..PAYLOAD_BYTES)
.map(|index| (index % 251) as u8)
.collect::<Vec<_>>();
fs::write(&path, &payload).unwrap();
let reference = ObjectReference::for_bytes(ObjectKind::CheckpointArtifact, 1, &payload);
(path, payload, reference)
}
#[test]
fn publication_takes_one_unit_and_wave_per_block_and_reports_progress() {
let root = test_root("units");
let (path, payload, reference) = source(&root);
let local = scheduler();
let probe = Arc::new(CheckpointWorkProbe::default());
let progress = Arc::new(AtomicU64::new(0));
let work = probe
.context(local.clone())
.with_progress_counter(progress.clone());
let mut store = ImmutableObjectStore::open(&root).unwrap();
assert_eq!(
store
.publish_file_with_work(reference, &path, BlockWork(Some(&work)))
.unwrap(),
PublishOutcome::Published
);
assert_eq!(probe.completed.load(Ordering::SeqCst), BLOCKS_PER_PASS);
assert_eq!(probe.io_waves.load(Ordering::SeqCst), BLOCKS_PER_PASS);
assert_eq!(progress.load(Ordering::SeqCst), BLOCKS_PER_PASS as u64);
assert_eq!(probe.peak_units.load(Ordering::SeqCst), 1);
probe.assert_released(&local);
assert_eq!(fs::read(store.object_path(reference)).unwrap(), payload);
assert_eq!(
store
.publish_file_with_work(reference, &path, BlockWork(Some(&work)))
.unwrap(),
PublishOutcome::Reused
);
assert_eq!(progress.load(Ordering::SeqCst), 2 * BLOCKS_PER_PASS as u64);
probe.assert_released(&local);
assert_eq!(staging_entries(&root), 0);
fs::remove_dir_all(root).unwrap();
}
#[test]
fn publication_cancelled_at_every_unit_and_wave_leaves_no_object_and_retries() {
let root = test_root("cancel");
let (path, payload, reference) = source(&root);
let local = scheduler();
for io in [false, true] {
for limit in 1..=BLOCKS_PER_PASS {
let probe = Arc::new(CheckpointWorkProbe::default());
if io {
probe.cancel_on_io_wave.store(limit, Ordering::SeqCst);
} else {
probe.cancel_after.store(limit, Ordering::SeqCst);
}
let work = probe.context(local.clone());
let mut store = ImmutableObjectStore::open(&root).unwrap();
let result = store.publish_file_with_work(reference, &path, BlockWork(Some(&work)));
if !io && limit == BLOCKS_PER_PASS {
assert_eq!(result.unwrap(), PublishOutcome::Published);
fs::remove_file(store.object_path(reference)).unwrap();
} else {
let error = result.unwrap_err();
assert!(
error.to_string().contains("stopped"),
"io={io} limit={limit}: {error}"
);
assert!(!store.object_path(reference).exists());
}
probe.assert_released(&local);
assert_eq!(staging_entries(&root), 0, "io={io} limit={limit}");
assert_eq!(fs::read(&path).unwrap(), payload);
}
}
let retry = Arc::new(CheckpointWorkProbe::default());
let work = retry.context(local.clone());
let mut store = ImmutableObjectStore::open(&root).unwrap();
assert_eq!(
store
.publish_file_with_work(reference, &path, BlockWork(Some(&work)))
.unwrap(),
PublishOutcome::Published
);
assert_eq!(fs::read(store.object_path(reference)).unwrap(), payload);
fs::remove_dir_all(root).unwrap();
}
#[test]
fn closure_digest_takes_one_unit_per_block_and_cancels_at_each() {
let root = test_root("digest");
let (path, _, reference) = source(&root);
let ordinary = crate::checkpoint_closure::digest_closure_artifact(
&path,
ObjectKind::CheckpointArtifact,
"digest",
BlockWork::default(),
)
.unwrap();
assert_eq!(ordinary.reference, reference);
let local = scheduler();
let probe = Arc::new(CheckpointWorkProbe::default());
let work: CheckpointWorkContext = probe.context(local.clone());
let digest = crate::checkpoint_closure::digest_closure_artifact(
&path,
ObjectKind::CheckpointArtifact,
"digest",
BlockWork(Some(&work)),
)
.unwrap();
assert_eq!(digest, ordinary);
assert_eq!(probe.completed.load(Ordering::SeqCst), BLOCKS_PER_PASS);
assert_eq!(probe.io_waves.load(Ordering::SeqCst), BLOCKS_PER_PASS);
probe.assert_released(&local);
for limit in 1..BLOCKS_PER_PASS {
let probe = Arc::new(CheckpointWorkProbe::default());
probe.cancel_after.store(limit, Ordering::SeqCst);
let work = probe.context(local.clone());
let error = crate::checkpoint_closure::digest_closure_artifact(
&path,
ObjectKind::CheckpointArtifact,
"digest",
BlockWork(Some(&work)),
)
.unwrap_err();
assert!(error.to_string().contains("stopped"), "{limit}: {error}");
probe.assert_released(&local);
}
fs::remove_dir_all(root).unwrap();
}