use std::fs::{self, File};
use std::os::unix::fs::{FileExt, PermissionsExt};
use std::time::SystemTime;
use super::beacon::{self, BEACON_LEN};
use super::*;
use crate::retention::{Action, Policy};
use crate::{NewRecord, ReaderConfig, Writer, WriterConfig};
const SEG: u64 = format::SEGMENT_SIZE_UNIT;
const HOUR: Duration = Duration::from_secs(3600);
fn config() -> WriterConfig {
WriterConfig::default().segment_size(SEG).prefault(false)
}
fn append(writer: &mut Writer, n: u64, payload_len: usize, local_ts_ns: u64) {
let payload = vec![0xA5; payload_len];
for _ in 0..n {
writer
.append(&NewRecord {
schema_id: 1,
venue_ts_ns: 0,
local_ts_ns,
payload: &payload,
})
.unwrap();
}
}
fn frame(payload_len: usize) -> u64 {
format::align8(u64::from(format::frame_len(payload_len).unwrap()))
}
fn records_per_segment(payload_len: usize) -> u64 {
(SEG - format::SEGMENT_HEADER_LEN as u64 - format::SEGMENT_TRAILER_LEN as u64)
/ frame(payload_len)
}
fn marker(dir: &Path) -> File {
File::options()
.write(true)
.open(dir.join("checkpoint"))
.unwrap()
}
fn backdate_checkpoint(dir: &Path, base: u64, by: Duration) {
let path = journal::segment_path(dir, base);
let file = File::options().read(true).write(true).open(&path).unwrap();
let mut header = journal::read_header(&file, &path, base).unwrap();
header.created_unix_ns -= u64::try_from(by.as_nanos()).unwrap();
file.write_all_at(&format::encode_segment_header(&header), 0)
.unwrap();
let marker = marker(dir);
let modified = marker.metadata().unwrap().modified().unwrap();
marker.set_modified(modified - by).unwrap();
}
fn caught_up(records: u64) -> Lag {
Lag {
records,
bytes: Some(0),
}
}
#[test]
fn missing_and_empty_directories_are_not_found() {
let dir = tempfile::tempdir().unwrap();
assert!(matches!(status(dir.path()), Err(Error::NotFound { .. })));
assert!(matches!(
status(dir.path().join("nope")),
Err(Error::Io { .. })
));
}
#[test]
fn empty_journal() {
let dir = tempfile::tempdir().unwrap();
let writer = Writer::open(dir.path(), config()).unwrap();
let s = status(dir.path()).unwrap();
assert_eq!(s.head.position, Position::new(0, 0, 64));
assert_eq!(s.head.end, HeadEnd::Open);
assert_eq!(s.head.segment_size, SEG);
assert_eq!(s.head.last_local_ts_ns, None);
assert!(s.head.verified);
assert_eq!(s.segments.count, 1);
assert_eq!(s.segments.first_seq, 0);
assert_eq!(s.segments.total_bytes, SEG);
assert!(s.segments.oldest_created_unix_ns.is_some());
assert_eq!(s.segments.before_checkpoint, 0);
assert_eq!(s.checkpoint, None);
assert_eq!(s.exposure.records, Some(0));
assert_eq!(s.exposure.bytes, Some(0));
assert_eq!(s.exposure.estimated_age, Some(Duration::ZERO));
assert_eq!(s.exposure.oldest_local_ts_ns, None);
assert!(s.exposure.verified);
assert_eq!(s.rates, None);
assert_eq!(s.storage.estimated_time_to_full, None);
assert!(s.readers.is_empty());
assert_eq!(s.readers_dir, dir.path().join("readers"));
assert_eq!(s.readers_dir_state, BeaconDirState::Missing);
assert_eq!(s.writer.pid, Some(std::process::id()));
assert_eq!(s.writer.alive, Some(true));
assert!(
s.breaches(&Thresholds::default().max_checkpoint_age(Duration::ZERO))
.is_empty()
);
drop(writer);
}
#[test]
fn no_checkpoint_exposes_everything() {
let dir = tempfile::tempdir().unwrap();
let mut writer = Writer::open(dir.path(), config()).unwrap();
append(&mut writer, 3, 10, 0);
append(&mut writer, 2, 10, 77);
let s = status(dir.path()).unwrap();
assert_eq!(s.head.position, Position::new(5, 0, 64 + 5 * frame(10)));
assert_eq!(s.head.last_local_ts_ns, Some(77));
assert!(s.head.verified, "one segment, scanned from its start");
assert_eq!(s.checkpoint, None);
assert_eq!(s.exposure.records, Some(5));
assert_eq!(s.exposure.bytes, Some(5 * frame(10)));
assert_eq!(s.exposure.estimated_age, None);
assert_eq!(s.exposure.oldest_local_ts_ns, None);
let t = Thresholds::default().max_checkpoint_age(HOUR);
assert_eq!(s.breaches(&t), vec![Breach::CheckpointAge]);
}
#[test]
fn checkpoint_age_and_exposure_after_more_appends() {
let dir = tempfile::tempdir().unwrap();
let mut writer = Writer::open(dir.path(), config()).unwrap();
append(&mut writer, 4, 10, 5);
writer.checkpoint().unwrap();
let after_checkpoint = crate::clock::realtime_ns();
let s = status(dir.path()).unwrap();
let c = s.checkpoint.unwrap();
assert_eq!(c.marker.next_seq, 4);
assert_eq!(c.marker.generation, 1);
assert_eq!(c.problem, None);
assert_eq!(c.age_unknown, None);
assert!(c.modified_unix_ns.unwrap() <= after_checkpoint);
assert!(c.modified_unix_ns.unwrap() <= s.sampled_unix_ns);
assert!(c.estimated_age.is_some());
assert_eq!(s.exposure.records, Some(0));
assert_eq!(s.exposure.estimated_age, Some(Duration::ZERO));
append(&mut writer, 3, 10, 9);
let s = status(dir.path()).unwrap();
assert!(s.head.verified, "the scan started at the checkpoint");
assert_eq!(s.exposure.records, Some(3));
assert_eq!(s.exposure.bytes, Some(3 * frame(10)));
assert_eq!(s.exposure.oldest_local_ts_ns, Some(9));
assert_eq!(
s.exposure.estimated_age,
s.checkpoint.unwrap().estimated_age
);
backdate_checkpoint(dir.path(), 0, HOUR);
let s = status(dir.path()).unwrap();
assert!(s.checkpoint.unwrap().estimated_age.unwrap() >= HOUR);
assert!(
s.breaches(&Thresholds::default().max_checkpoint_age(2 * HOUR))
.is_empty()
);
assert_eq!(
s.breaches(&Thresholds::default().max_checkpoint_age(HOUR / 2)),
vec![Breach::CheckpointAge]
);
}
#[test]
fn unreliable_checkpoint_times_are_unknown() {
let dir = tempfile::tempdir().unwrap();
let mut writer = Writer::open(dir.path(), config()).unwrap();
append(&mut writer, 2, 10, 0);
writer.checkpoint().unwrap();
writer.checkpoint().unwrap();
append(&mut writer, 1, 10, 0);
let t = Thresholds::default().max_checkpoint_age(HOUR);
marker(dir.path())
.set_modified(SystemTime::now() + HOUR)
.unwrap();
let s = status(dir.path()).unwrap();
let c = s.checkpoint.unwrap();
assert_eq!(c.age_unknown, Some(AgeUnknown::InFuture));
assert_eq!(c.estimated_age, None);
assert_eq!(s.exposure.estimated_age, None);
assert_eq!(s.breaches(&t), vec![Breach::CheckpointAge]);
assert_eq!(
s.breaches(&Thresholds::default().max_checkpoint_age(Duration::ZERO)),
vec![Breach::CheckpointAge]
);
marker(dir.path())
.set_modified(UNIX_EPOCH + Duration::from_secs(1_000_000))
.unwrap();
let s = status(dir.path()).unwrap();
assert_eq!(
s.checkpoint.unwrap().age_unknown,
Some(AgeUnknown::BeforeSegment)
);
assert_eq!(s.breaches(&t), vec![Breach::CheckpointAge]);
drop(writer);
marker(dir.path()).set_len(4096).unwrap();
let s = status(dir.path()).unwrap();
let c = s.checkpoint.unwrap();
assert_eq!(c.marker.generation, 2);
assert_eq!(c.age_unknown, Some(AgeUnknown::MarkerResized));
assert_eq!(s.breaches(&t), vec![Breach::CheckpointAge]);
let mut writer = Writer::open(dir.path(), config()).unwrap();
writer.checkpoint().unwrap();
marker(dir.path())
.set_modified(SystemTime::now() + HOUR)
.unwrap();
let s = status(dir.path()).unwrap();
assert_eq!(s.exposure.records, Some(0));
assert!(s.breaches(&t).is_empty());
}
#[test]
fn reopening_with_a_short_marker_keeps_the_age_unknown() {
let dir = tempfile::tempdir().unwrap();
let mut writer = Writer::open(dir.path(), config()).unwrap();
append(&mut writer, 2, 10, 0);
writer.checkpoint().unwrap();
writer.checkpoint().unwrap();
append(&mut writer, 1, 10, 0);
drop(writer);
backdate_checkpoint(dir.path(), 0, 3 * HOUR);
let full = format::CHECKPOINT_FILE_LEN as u64;
let t = Thresholds::default().max_checkpoint_age(HOUR);
let s = status(dir.path()).unwrap();
assert_eq!(s.checkpoint.unwrap().age_unknown, None);
assert_eq!(s.breaches(&t), vec![Breach::CheckpointAge]);
marker(dir.path()).set_len(4096).unwrap();
let unknown = |dir: &Path, reason: AgeUnknown, uncovered: u64| {
let s = status(dir).unwrap();
let c = s.checkpoint.unwrap();
assert_eq!(c.marker.generation, 2);
assert_eq!(c.estimated_age, None);
assert_eq!(c.age_unknown, Some(reason));
assert_eq!(s.exposure.records, Some(uncovered));
assert_eq!(s.breaches(&t), vec![Breach::CheckpointAge]);
};
unknown(dir.path(), AgeUnknown::MarkerResized, 1);
let mut writer = Writer::open(dir.path(), config()).unwrap();
assert_eq!(
fs::metadata(dir.path().join("checkpoint")).unwrap().len(),
4096
);
unknown(dir.path(), AgeUnknown::MarkerResized, 1);
append(&mut writer, 1, 10, 0);
writer.checkpoint().unwrap();
append(&mut writer, 1, 10, 0);
assert_eq!(
fs::metadata(dir.path().join("checkpoint")).unwrap().len(),
full
);
let s = status(dir.path()).unwrap();
assert_eq!(s.checkpoint.unwrap().marker.generation, 3);
assert!(s.checkpoint.unwrap().estimated_age.unwrap() < HOUR);
assert!(s.breaches(&t).is_empty());
drop(writer);
marker(dir.path()).set_len(4096).unwrap();
marker(dir.path()).set_len(full).unwrap();
unknown(dir.path(), AgeUnknown::OlderSlotLost, 3);
let mut writer = Writer::open(dir.path(), config()).unwrap();
writer.checkpoint().unwrap();
let s = status(dir.path()).unwrap();
assert_eq!(s.checkpoint.unwrap().age_unknown, None);
assert!(s.breaches(&t).is_empty());
}
#[test]
fn checkpoint_in_an_older_segment() {
let dir = tempfile::tempdir().unwrap();
let mut writer = Writer::open(dir.path(), config()).unwrap();
let per = records_per_segment(1000);
append(&mut writer, 2, 1000, 11);
writer.checkpoint().unwrap();
append(&mut writer, per, 1000, 12);
let s = status(dir.path()).unwrap();
assert_eq!(s.segments.count, 2);
assert_eq!(s.head.position.segment_base_seq, per);
assert_eq!(s.head.position.next_seq, per + 2);
assert_eq!(s.exposure.records, Some(per));
assert_eq!(s.exposure.oldest_local_ts_ns, Some(12));
assert!(!s.head.verified);
assert!(!s.exposure.verified);
let checkpoint_offset = 64 + 2 * frame(1000);
assert_eq!(
s.exposure.bytes,
Some(SEG - checkpoint_offset + s.head.position.offset)
);
assert_eq!(s.segments.before_checkpoint, 0);
writer.checkpoint().unwrap();
let s = status(dir.path()).unwrap();
assert_eq!(s.segments.before_checkpoint, 1);
assert_eq!(s.exposure.records, Some(0));
assert!(s.head.verified);
}
#[test]
fn dangling_checkpoint_is_corruption() {
let per = records_per_segment(1000);
let setup = || {
let dir = tempfile::tempdir().unwrap();
let mut writer = Writer::open(dir.path(), config()).unwrap();
append(&mut writer, 3 * per + 5, 1000, 0);
writer.checkpoint().unwrap();
append(&mut writer, 2, 1000, 0);
drop(writer);
dir
};
let check = |dir: &Path, problem: CheckpointProblem| {
assert!(Writer::open(dir, config()).is_err(), "the writer refuses");
let s = status(dir).unwrap();
assert_eq!(s.checkpoint.unwrap().problem, Some(problem));
assert_eq!(s.exposure.records, None);
assert_eq!(s.exposure.bytes, None);
assert_eq!(s.exposure.estimated_age, None);
assert!(!s.head.verified);
let breaches = s.breaches(&Thresholds::default());
assert_eq!(breaches, vec![Breach::CheckpointInvalid]);
assert!(breaches[0].is_corruption());
};
let dir = setup();
fs::remove_file(journal::segment_path(dir.path(), 3 * per)).unwrap();
check(dir.path(), CheckpointProblem::MissingSegment);
let dir = setup();
let slot = CheckpointSlot {
generation: 7,
next_seq: 3 * per + 5,
segment_base_seq: 3 * per,
offset: SEG,
};
marker(dir.path())
.write_all_at(&format::encode_checkpoint_slot(&slot), 65_536)
.unwrap();
check(dir.path(), CheckpointProblem::InvalidOffset);
let dir = setup();
let mut writer = Writer::open(dir.path(), config()).unwrap();
append(&mut writer, per, 1000, 0);
drop(writer);
let path = journal::segment_path(dir.path(), 3 * per);
File::options()
.write(true)
.open(path)
.unwrap()
.write_all_at(&[0], 0)
.unwrap();
check(dir.path(), CheckpointProblem::InvalidSegmentHeader);
}
#[test]
fn missing_middle_segment_is_unverified() {
let dir = tempfile::tempdir().unwrap();
let mut writer = Writer::open(dir.path(), config()).unwrap();
let per = records_per_segment(1000);
append(&mut writer, 2, 1000, 0);
writer.checkpoint().unwrap();
append(&mut writer, 3 * per, 1000, 0);
drop(writer);
fs::remove_file(journal::segment_path(dir.path(), per)).unwrap();
assert!(Writer::open(dir.path(), config()).is_err());
let s = status(dir.path()).unwrap();
assert!(!s.head.verified);
assert!(!s.exposure.verified);
assert_eq!(s.checkpoint.unwrap().problem, None);
assert!(!s.head.chain_broken);
assert!(s.breaches(&Thresholds::default()).is_empty());
}
#[test]
fn sealed_newest_segment_without_successor() {
let dir = tempfile::tempdir().unwrap();
let mut writer = Writer::open(dir.path(), config()).unwrap();
let per = records_per_segment(1000);
append(&mut writer, per + 1, 1000, 0);
drop(writer);
fs::remove_file(journal::segment_path(dir.path(), per)).unwrap();
let s = status(dir.path()).unwrap();
assert_eq!(s.head.end, HeadEnd::Sealed);
assert_eq!(s.head.position.next_seq, per);
assert!(s.breaches(&Thresholds::default()).is_empty());
}
#[test]
fn invalid_head_is_corruption() {
let dir = tempfile::tempdir().unwrap();
let mut writer = Writer::open(dir.path(), config()).unwrap();
append(&mut writer, 3, 10, 0);
drop(writer);
let path = journal::segment_path(dir.path(), 0);
let file = File::options().write(true).open(&path).unwrap();
let at = 64 + 2 * frame(10) + format::RECORD_HEADER_LEN as u64;
file.write_all_at(&[0x5A], at).unwrap();
let s = status(dir.path()).unwrap();
assert_eq!(s.head.end, HeadEnd::Invalid(CorruptReason::BadChecksum));
assert_eq!(s.head.position.next_seq, 2);
let breaches = s.breaches(&Thresholds::default());
assert_eq!(breaches, vec![Breach::HeadInvalid]);
assert!(breaches[0].is_corruption());
assert!(!Breach::CheckpointAge.is_corruption());
let unverified = |s: &Status| {
assert!(!s.head.verified);
assert!(!s.exposure.verified);
assert_eq!(s.exposure.records, None);
assert_eq!(s.exposure.bytes, None);
assert_eq!(s.exposure.estimated_age, None);
assert_eq!(s.exposure.oldest_local_ts_ns, None);
};
unverified(&s);
let mut sampler = Sampler::new(dir.path());
unverified(&sampler.sample().unwrap());
unverified(&sampler.sample().unwrap());
file.write_all_at(&[0xA5], at).unwrap();
let mut writer = Writer::open(dir.path(), config()).unwrap();
writer.checkpoint().unwrap();
append(&mut writer, 2, 10, 0);
drop(writer);
let at = 64 + 4 * frame(10) + format::RECORD_HEADER_LEN as u64;
file.write_all_at(&[0x5A], at).unwrap();
let s = status(dir.path()).unwrap();
assert!(matches!(s.head.end, HeadEnd::Invalid(_)), "{:?}", s.head);
assert!(s.checkpoint.unwrap().problem.is_none());
unverified(&s);
}
#[test]
fn corrupt_newest_header_is_an_error() {
let dir = tempfile::tempdir().unwrap();
let writer = Writer::open(dir.path(), config()).unwrap();
drop(writer);
let path = journal::segment_path(dir.path(), 0);
let file = File::options().write(true).open(&path).unwrap();
file.write_all_at(&[0], 0).unwrap();
assert!(matches!(status(dir.path()), Err(Error::Corrupt { .. })));
}
#[test]
fn spare_segment_and_headroom() {
let dir = tempfile::tempdir().unwrap();
let mut writer = Writer::open(dir.path(), config()).unwrap();
append(&mut writer, 2, 10, 0);
drop(writer);
fs::write(dir.path().join("00000000000000000002.tape.tmp"), [1u8; 100]).unwrap();
fs::create_dir(dir.path().join("quarantine")).unwrap();
let s = status(dir.path()).unwrap();
assert_eq!(s.segments.count, 1);
assert_eq!(s.head.position.next_seq, 2);
assert!(!s.storage.spare_segment);
assert_eq!(s.storage.next_segment_size, SEG);
assert!(s.storage.available_bytes <= s.storage.total_bytes);
assert_eq!(
s.storage.estimated_rolls_left,
s.storage.available_bytes / SEG
);
let spare_path = dir.path().join(journal::SPARE_FILE);
let spare = File::create(&spare_path).unwrap();
spare.set_len(2 * SEG).unwrap();
let s = status(dir.path()).unwrap();
assert!(!s.storage.spare_segment, "a sparse file counted as a spare");
assert_eq!(s.storage.next_segment_size, 2 * SEG);
assert_eq!(
s.storage.estimated_rolls_left,
s.storage.available_bytes / (2 * SEG)
);
drop(spare);
fs::remove_file(&spare_path).unwrap();
let spare = File::create(&spare_path).unwrap();
crate::mmap::preallocate(&spare, SEG).unwrap();
let s = status(dir.path()).unwrap();
assert!(s.storage.spare_segment);
assert_eq!(
s.storage.estimated_rolls_left,
s.storage.available_bytes / SEG
);
let t = Thresholds::default()
.min_headroom_bytes(u64::MAX)
.min_headroom_segments(u64::MAX);
assert_eq!(
s.breaches(&t),
vec![Breach::HeadroomBytes, Breach::HeadroomSegments]
);
assert!(
s.breaches(
&Thresholds::default()
.min_headroom_bytes(1)
.min_headroom_segments(0)
)
.is_empty()
);
}
#[test]
fn time_to_full_of_a_sealed_head() {
let rates = Rates {
interval: Duration::from_secs(1),
records_per_sec: 1.0,
bytes_per_sec: Some(SEG as f64),
};
let mut head = Head {
position: Position::new(5, 0, 64),
segment_size: SEG,
end: HeadEnd::Open,
last_local_ts_ns: None,
verified: true,
chain_broken: false,
};
let left = (SEG - 64) as f64 / SEG as f64;
let available = 10 * SEG + SEG / 2;
let open = estimate_storage(available, 100 * SEG, None, &head, Some(rates));
assert_eq!(open.estimated_rolls_left, 10);
assert_eq!(open.next_segment_size, SEG);
assert!((open.estimated_time_to_full.unwrap().as_secs_f64() - (10.0 + left)).abs() < 1e-6);
head.end = HeadEnd::Sealed;
let sealed = estimate_storage(available, 100 * SEG, None, &head, Some(rates));
assert_eq!(sealed.estimated_time_to_full.unwrap().as_secs_f64(), 10.0);
let spare = estimate_storage(
available,
100 * SEG,
Some((2 * SEG, true)),
&head,
Some(rates),
);
assert_eq!(spare.next_segment_size, 2 * SEG);
assert_eq!(spare.estimated_rolls_left, 5);
assert!(spare.spare_segment);
assert_eq!(spare.estimated_time_to_full.unwrap().as_secs_f64(), 10.0);
assert!(
!estimate_storage(available, 100 * SEG, Some((2 * SEG, false)), &head, None).spare_segment
);
assert_eq!(
estimate_storage(available, 100 * SEG, None, &head, None).estimated_time_to_full,
None
);
let dir = tempfile::tempdir().unwrap();
drop(Writer::open(dir.path(), config()).unwrap());
let sealed = storage(dir.path(), &head, Some(rates)).unwrap();
assert!(sealed.next_segment_size >= SEG);
assert_eq!(
sealed.estimated_rolls_left,
sealed.available_bytes / sealed.next_segment_size
);
let expected = (sealed.estimated_rolls_left * sealed.next_segment_size) as f64 / (SEG as f64);
let estimate = sealed.estimated_time_to_full.unwrap().as_secs_f64();
assert!(
(estimate - expected).abs() <= expected * 1e-9 + 1e-6,
"{estimate} vs {expected}"
);
}
#[test]
fn sampler_resumes_and_measures_rates() {
let dir = tempfile::tempdir().unwrap();
let mut writer = Writer::open(dir.path(), config()).unwrap();
append(&mut writer, 5, 100, 1);
let mut sampler = Sampler::new(dir.path());
let first = sampler.sample().unwrap();
assert_eq!(first.head.position.next_seq, 5);
assert_eq!(first.rates, None);
append(&mut writer, 7, 100, 2);
let second = sampler.sample().unwrap();
assert_eq!(second.head.position.next_seq, 12);
assert_eq!(second.head.last_local_ts_ns, Some(2));
assert!(
second.head.verified,
"resumed in the same file, from its start"
);
if let Some(rates) = second.rates {
assert!(rates.records_per_sec > 0.0);
assert!(rates.bytes_per_sec.unwrap() > 0.0);
assert!(second.storage.estimated_time_to_full.is_some());
}
let third = sampler.sample().unwrap();
assert_eq!(third.head, second.head);
if let Some(rates) = third.rates {
assert_eq!(rates.records_per_sec, 0.0);
}
assert_eq!(third.storage.estimated_time_to_full, None);
append(&mut writer, records_per_segment(100), 100, 3);
let fourth = sampler.sample().unwrap();
assert_eq!(fourth.head.position.next_seq, 12 + records_per_segment(100));
assert_eq!(fourth.head, status(dir.path()).unwrap().head);
}
#[test]
fn sampler_rescans_a_replaced_segment() {
let dir = tempfile::tempdir().unwrap();
let mut writer = Writer::open(dir.path(), config()).unwrap();
append(&mut writer, 6, 10, 0);
drop(writer);
let mut sampler = Sampler::new(dir.path());
assert_eq!(sampler.sample().unwrap().head.position.next_seq, 6);
let path = journal::segment_path(dir.path(), 0);
let mut image = fs::read(&path).unwrap();
let cut = (64 + 2 * frame(10)) as usize;
image[cut..].fill(0);
let tmp = dir.path().join("copy");
fs::write(&tmp, &image).unwrap();
fs::rename(&tmp, &path).unwrap();
let s = sampler.sample().unwrap();
assert_eq!(s.head.position, Position::new(2, 0, cut as u64));
assert_eq!(s.rates, None, "the history changed");
}
#[test]
fn sampler_detects_a_different_journal_in_the_same_inode() {
let dir = tempfile::tempdir().unwrap();
let mut writer = Writer::open(dir.path(), config()).unwrap();
append(&mut writer, 100, 10, 0);
drop(writer);
let mut sampler = Sampler::new(dir.path());
assert_eq!(sampler.sample().unwrap().head.position.next_seq, 100);
let other = tempfile::tempdir().unwrap();
let mut writer = Writer::open(other.path(), config()).unwrap();
append(&mut writer, 10, 10, 0);
drop(writer);
let image = fs::read(journal::segment_path(other.path(), 0)).unwrap();
let path = journal::segment_path(dir.path(), 0);
let ino = fs::metadata(&path).unwrap().ino();
File::options()
.write(true)
.open(&path)
.unwrap()
.write_all_at(&image, 0)
.unwrap();
assert_eq!(fs::metadata(&path).unwrap().ino(), ino);
let s = sampler.sample().unwrap();
assert_eq!(s.head.position.next_seq, 10);
assert_eq!(s.rates, None);
}
#[test]
fn sampler_after_a_repair() {
let dir = tempfile::tempdir().unwrap();
let mut writer = Writer::open(dir.path(), config()).unwrap();
let per = records_per_segment(1000);
append(&mut writer, 2, 1000, 1);
writer.checkpoint().unwrap();
append(&mut writer, per, 1000, 2);
drop(writer);
let mut sampler = Sampler::new(dir.path());
let first = sampler.sample().unwrap();
assert_eq!(first.exposure.oldest_local_ts_ns, Some(2));
assert_eq!(first.head.position.next_seq, per + 2);
let at = 64 + 2 * frame(1000) + format::RECORD_HEADER_LEN as u64;
File::options()
.write(true)
.open(journal::segment_path(dir.path(), 0))
.unwrap()
.write_all_at(&[0x00], at)
.unwrap();
let mut writer = Writer::open(dir.path(), config()).unwrap();
append(&mut writer, per, 1000, 99);
drop(writer);
let s = sampler.sample().unwrap();
assert_eq!(s.head.position.next_seq, per + 2);
assert_eq!(s.rates, None, "the history changed under the sampler");
assert_eq!(s.exposure.oldest_local_ts_ns, Some(99));
}
#[test]
fn sampler_does_not_cache_a_failed_lookup() {
let dir = tempfile::tempdir().unwrap();
let mut writer = Writer::open(dir.path(), config()).unwrap();
let per = records_per_segment(1000);
append(&mut writer, 2, 1000, 1);
writer.checkpoint().unwrap();
append(&mut writer, per, 1000, 2);
drop(writer);
let path = journal::segment_path(dir.path(), 0);
let at = 64 + 2 * frame(1000) + format::RECORD_HEADER_LEN as u64;
let file = File::options().write(true).open(&path).unwrap();
let mut original = [0u8; 1];
File::open(&path)
.unwrap()
.read_exact_at(&mut original, at)
.unwrap();
file.write_all_at(&[original[0] ^ 0xFF], at).unwrap();
let mut sampler = Sampler::new(dir.path());
assert_eq!(sampler.sample().unwrap().exposure.oldest_local_ts_ns, None);
file.write_all_at(&original, at).unwrap();
assert_eq!(
sampler.sample().unwrap().exposure.oldest_local_ts_ns,
Some(2)
);
}
#[test]
fn sampler_rescans_a_snapshot_restored_in_place() {
let dir = tempfile::tempdir().unwrap();
let mut writer = Writer::open(dir.path(), config()).unwrap();
append(&mut writer, 2, 10, 0);
let path = journal::segment_path(dir.path(), 0);
let snapshot = fs::read(&path).unwrap();
append(&mut writer, 4, 10, 0);
drop(writer);
let mut sampler = Sampler::new(dir.path());
let first = sampler.sample().unwrap();
assert_eq!(first.head.position.next_seq, 6);
assert!(first.head.verified);
let ino = fs::metadata(&path).unwrap().ino();
File::options()
.write(true)
.open(&path)
.unwrap()
.write_all_at(&snapshot, 0)
.unwrap();
assert_eq!(fs::metadata(&path).unwrap().ino(), ino);
let s = sampler.sample().unwrap();
let fresh = status(dir.path()).unwrap();
assert_eq!(fresh.head.position.next_seq, 2);
assert_eq!(s.head, fresh.head);
assert_eq!(s.rates, None, "the history changed under the sampler");
}
fn checkpoint_at_a_seal(dir: &Path, ts: u64) -> u64 {
let per = records_per_segment(1000);
let mut writer = Writer::open(dir, config()).unwrap();
append(&mut writer, per, 1000, 1);
writer.checkpoint().unwrap();
append(&mut writer, 2, 1000, ts);
per
}
#[test]
fn sampler_follows_a_replaced_successor() {
let dir = tempfile::tempdir().unwrap();
let per = checkpoint_at_a_seal(dir.path(), 2);
let other = tempfile::tempdir().unwrap();
checkpoint_at_a_seal(other.path(), 99);
let mut sampler = Sampler::new(dir.path());
let s = sampler.sample().unwrap();
assert_eq!(s.checkpoint.unwrap().marker.segment_base_seq, 0);
assert_eq!(s.head.position.segment_base_seq, per);
assert_eq!(s.exposure.oldest_local_ts_ns, Some(2));
fs::rename(
journal::segment_path(other.path(), per),
journal::segment_path(dir.path(), per),
)
.unwrap();
assert_eq!(
status(dir.path()).unwrap().exposure.oldest_local_ts_ns,
Some(99)
);
assert_eq!(
sampler.sample().unwrap().exposure.oldest_local_ts_ns,
Some(99)
);
}
#[test]
fn sampler_checks_a_marker_that_moved_behind_its_head() {
let dir = tempfile::tempdir().unwrap();
let mut writer = Writer::open(dir.path(), config()).unwrap();
append(&mut writer, 6, 10, 0);
drop(writer);
let mut sampler = Sampler::new(dir.path());
let first = sampler.sample().unwrap();
assert!(first.head.verified);
assert_eq!(first.checkpoint, None);
let slot = CheckpointSlot {
generation: 5,
next_seq: 3,
segment_base_seq: 0,
offset: 64 + frame(10) + 8,
};
marker(dir.path())
.write_all_at(&format::encode_checkpoint_slot(&slot), 65_536)
.unwrap();
let fresh = status(dir.path()).unwrap();
assert!(
matches!(fresh.head.end, HeadEnd::Invalid(_)),
"{:?}",
fresh.head
);
let s = sampler.sample().unwrap();
assert_eq!(s.head, fresh.head);
assert_eq!(
s.breaches(&Thresholds::default()),
vec![Breach::HeadInvalid]
);
let dir = tempfile::tempdir().unwrap();
let mut writer = Writer::open(dir.path(), config()).unwrap();
append(&mut writer, 6, 10, 0);
drop(writer);
let mut sampler = Sampler::new(dir.path());
sampler.sample().unwrap();
let slot = CheckpointSlot {
generation: 1,
next_seq: 2,
segment_base_seq: 0,
offset: 64 + 2 * frame(10),
};
marker(dir.path())
.write_all_at(&format::encode_checkpoint_slot(&slot), 65_536)
.unwrap();
let s = sampler.sample().unwrap();
assert_eq!(s.head.position.next_seq, 6);
assert!(s.head.verified);
assert_eq!(s.exposure.records, Some(4));
}
#[test]
fn checkpoint_segment_gone_after_the_listing_is_retried() {
let dir = tempfile::tempdir().unwrap();
let mut writer = Writer::open(dir.path(), config()).unwrap();
let per = records_per_segment(1000);
append(&mut writer, per + 2, 1000, 0);
writer.checkpoint().unwrap();
append(&mut writer, per, 1000, 0);
drop(writer);
let files = list_files(dir.path()).unwrap();
let marker = read_marker(dir.path()).unwrap().unwrap();
let c = marker.slot.unwrap();
assert_eq!(c.segment_base_seq, per);
fs::remove_file(journal::segment_path(dir.path(), per)).unwrap();
let mut open = OpenSegments::default();
let result = checkpoint_status(dir.path(), &files, &mut open, c, &marker, 0);
assert!(
matches!(&result, Err(Error::Io { source, .. }) if source.kind() == io::ErrorKind::NotFound),
"{result:?}"
);
}
#[test]
fn checkpoint_without_segments_is_dangling() {
let dir = tempfile::tempdir().unwrap();
let mut writer = Writer::open(dir.path(), config()).unwrap();
append(&mut writer, 3, 10, 0);
writer.checkpoint().unwrap();
drop(writer);
fs::remove_file(journal::segment_path(dir.path(), 0)).unwrap();
let err = Writer::open(dir.path(), config()).unwrap_err();
assert!(
matches!(
&err,
Error::Corrupt {
reason: CorruptReason::DanglingCheckpoint(0),
..
}
),
"{err}"
);
let err = status(dir.path()).unwrap_err();
assert!(
matches!(
&err,
Error::Corrupt {
reason: CorruptReason::DanglingCheckpoint(0),
..
}
),
"{err}"
);
}
#[test]
fn provably_missing_segment_is_corruption() {
let dir = tempfile::tempdir().unwrap();
let mut writer = Writer::open(dir.path(), config()).unwrap();
append(&mut writer, 3, 10, 0);
writer.checkpoint().unwrap();
append(&mut writer, 2, 10, 0);
drop(writer);
let base = 1_000_000;
let header = SegmentHeader {
segment_size: SEG,
base_seq: base,
created_unix_ns: crate::clock::realtime_ns(),
};
let path = journal::segment_path(dir.path(), base);
let file = File::create(&path).unwrap();
file.set_len(SEG).unwrap();
file.write_all_at(&format::encode_segment_header(&header), 0)
.unwrap();
let report = crate::inspect::verify(dir.path()).unwrap();
assert!(!report.problems.is_empty(), "verify agrees");
let s = status(dir.path()).unwrap();
assert!(s.head.chain_broken);
assert!(!s.head.verified);
assert_eq!(s.exposure.records, None);
assert_eq!(s.exposure.bytes, None);
let breaches = s.breaches(&Thresholds::default());
assert_eq!(breaches, vec![Breach::ChainBroken]);
assert!(breaches[0].is_corruption());
}
#[test]
fn reader_lag_in_process() {
let dir = tempfile::tempdir().unwrap();
let mut writer = Writer::open(dir.path(), config()).unwrap();
let per = records_per_segment(1000);
append(&mut writer, per + 3, 1000, 0);
let mut reader = Reader::open(dir.path(), ReaderConfig::default()).unwrap();
let cursor = reader.cursor();
assert_eq!(cursor.next_seq, 0);
assert_eq!(cursor.position, Position::new(0, 0, 64));
assert!(!cursor.seek_pending && !cursor.durable_only);
let s = status(dir.path()).unwrap();
let lag = s.lag(&reader.cursor()).unwrap();
assert_eq!(lag.records, per + 3);
assert_eq!(lag.bytes, Some(SEG + s.head.position.offset - 64));
for _ in 0..per + 1 {
reader.try_next().unwrap().unwrap();
}
assert_eq!(
reader.cursor().position,
Position::new(per + 1, per, 64 + frame(1000))
);
let lag = s.lag(&reader.cursor()).unwrap();
assert_eq!(lag.records, 2);
assert_eq!(lag.bytes, Some(2 * frame(1000)));
while reader.try_next().unwrap().is_some() {}
assert_eq!(s.lag(&reader.cursor()), Ok(caught_up(0)));
}
#[test]
fn lag_of_a_reader_that_is_ahead_or_seeking() {
let dir = tempfile::tempdir().unwrap();
let mut writer = Writer::open(dir.path(), config()).unwrap();
let per = records_per_segment(1000);
append(&mut writer, 2 * per, 1000, 0);
let s = status(dir.path()).unwrap();
let mut reader = Reader::open(dir.path(), ReaderConfig::default()).unwrap();
reader.seek(10 * per).unwrap();
let cursor = reader.cursor();
assert!(cursor.seek_pending);
assert_eq!(cursor.next_seq, 10 * per);
assert_eq!(s.lag(&cursor), Ok(caught_up(0)));
reader.seek(per + 5).unwrap();
let cursor = reader.cursor();
assert!(cursor.seek_pending);
let lag = s.lag(&cursor).unwrap();
assert_eq!(lag.records, per - 5);
assert_eq!(lag.bytes, None);
let mut reader = Reader::open(dir.path(), ReaderConfig::default()).unwrap();
for _ in 0..2 * per {
reader.try_next().unwrap().unwrap();
}
assert_eq!(s.lag(&reader.cursor()), Ok(caught_up(0)));
}
#[test]
fn lag_of_a_durable_only_reader() {
let dir = tempfile::tempdir().unwrap();
let mut writer = Writer::open(dir.path(), config()).unwrap();
append(&mut writer, 5, 10, 0);
let mut reader = Reader::open(dir.path(), ReaderConfig::default().durable_only(true)).unwrap();
assert!(reader.cursor().durable_only);
let s = status(dir.path()).unwrap();
assert_eq!(s.lag(&reader.cursor()), Ok(caught_up(0)));
writer.checkpoint().unwrap();
append(&mut writer, 100, 10, 0);
let s = status(dir.path()).unwrap();
assert_eq!(s.lag(&reader.cursor()).unwrap().records, 5);
while reader.try_next().unwrap().is_some() {}
assert_eq!(reader.position(), 5);
assert_eq!(s.lag(&reader.cursor()), Ok(caught_up(0)));
let normal = Reader::open(dir.path(), ReaderConfig::default()).unwrap();
assert_eq!(s.lag(&normal.cursor()).unwrap().records, 105);
}
#[test]
fn lag_needs_the_same_segment_file() {
let dir = tempfile::tempdir().unwrap();
let other = tempfile::tempdir().unwrap();
let mut writer = Writer::open(dir.path(), config()).unwrap();
append(&mut writer, 5, 10, 0);
let mut writer2 = Writer::open(other.path(), config()).unwrap();
append(&mut writer2, 50, 10, 0);
let foreign = Reader::open(other.path(), ReaderConfig::default()).unwrap();
let s = status(dir.path()).unwrap();
assert_eq!(s.lag(&foreign.cursor()), Err(LagUnknown::OtherHistory));
let config = config().retention(Policy::default().max_segments(1), Action::Delete);
drop(writer);
let reader = Reader::open(dir.path(), ReaderConfig::default()).unwrap();
let mut writer = Writer::open(dir.path(), config).unwrap();
append(&mut writer, 2 * records_per_segment(10), 10, 0);
writer.checkpoint().unwrap();
let s = status(dir.path()).unwrap();
assert_ne!(s.segments.first_seq, 0);
assert_eq!(s.lag(&reader.cursor()), Err(LagUnknown::SegmentGone));
}
#[test]
fn lag_of_a_cursor_ahead_of_the_sample() {
let dir = tempfile::tempdir().unwrap();
let mut writer = Writer::open(dir.path(), config()).unwrap();
let per = records_per_segment(1000);
append(&mut writer, per - 1, 1000, 0);
let s = status(dir.path()).unwrap();
append(&mut writer, 3, 1000, 0);
let mut reader = Reader::open(dir.path(), ReaderConfig::default()).unwrap();
for _ in 0..per + 1 {
reader.try_next().unwrap().unwrap();
}
assert_eq!(reader.cursor().position.segment_base_seq, per);
assert_eq!(s.lag(&reader.cursor()), Err(LagUnknown::AheadOfSample));
assert_eq!(LagUnknown::AheadOfSample.code(), "ahead_of_sample");
let s = status(dir.path()).unwrap();
assert_eq!(s.lag(&reader.cursor()).unwrap().records, 1);
}
#[test]
fn a_marker_changing_under_the_read_is_not_an_old_checkpoint() {
let dir = tempfile::tempdir().unwrap();
let mut writer = Writer::open(dir.path(), config()).unwrap();
append(&mut writer, 2, 10, 0);
writer.checkpoint().unwrap();
append(&mut writer, 1, 10, 0);
let t = Thresholds::default().max_checkpoint_age(HOUR);
let mut s = status(dir.path()).unwrap();
assert_eq!(s.exposure.records, Some(1));
let c = s.checkpoint.as_mut().unwrap();
c.estimated_age = None;
c.age_unknown = Some(AgeUnknown::Changing);
s.exposure.estimated_age = None;
assert!(s.breaches(&t).is_empty());
s.checkpoint.as_mut().unwrap().age_unknown = Some(AgeUnknown::InFuture);
assert_eq!(s.breaches(&t), vec![Breach::CheckpointAge]);
}
#[cfg(feature = "fault-injection")]
#[test]
fn takeover_rechecks_a_replaced_name() {
use crate::fault::{Pause, on_pause};
let dir = tempfile::tempdir().unwrap();
let mut writer = Writer::open(dir.path(), config()).unwrap();
append(&mut writer, 3, 10, 0);
let reader = Reader::open(dir.path(), ReaderConfig::default()).unwrap();
let path = stale_beacon(ReaderBeacon::create(dir.path(), "r", &reader).unwrap());
let precious = dir.path().join("precious");
fs::write(&precious, b"not a beacon").unwrap();
let (name, replacement) = (path.clone(), precious.clone());
let mut done = false;
on_pause(Some(Box::new(move |p| {
if p == Pause::CreatorOpenedExisting && !done {
done = true;
fs::rename(&replacement, &name).unwrap();
}
})));
let result = ReaderBeacon::create(dir.path(), "r", &reader);
on_pause(None);
let err = result.unwrap_err();
assert!(matches!(err, Error::InvalidConfig(_)), "{err}");
assert_eq!(fs::read(&path).unwrap(), b"not a beacon");
}
#[test]
fn footprint_cases() {
let file = |base, size| SegFile {
base,
size,
file: (0, base),
};
let files = [file(0, 100), file(10, 200), file(20, 300)];
let p = Position::new;
assert_eq!(footprint(&files, p(1, 0, 64), p(3, 0, 80)), Some(16));
assert_eq!(footprint(&files, p(3, 0, 80), p(1, 0, 64)), None);
assert_eq!(
footprint(&files, p(1, 0, 64), p(21, 20, 72)),
Some(36 + 200 + 72)
);
assert_eq!(footprint(&files, p(21, 20, 72), p(1, 0, 64)), None);
assert_eq!(footprint(&files, p(1, 5, 64), p(21, 20, 72)), None);
assert_eq!(footprint(&files, p(1, 0, 64), p(31, 30, 72)), None);
assert!(valid_offset(64, SEG));
assert!(valid_offset(SEG - 8, SEG));
assert!(!valid_offset(SEG, SEG));
assert!(!valid_offset(60, SEG));
assert!(!valid_offset(68, SEG));
}
#[test]
fn retention_removed_prefix() {
let dir = tempfile::tempdir().unwrap();
let config = config().retention(Policy::default().max_segments(2), Action::Delete);
let mut writer = Writer::open(dir.path(), config).unwrap();
let per = records_per_segment(1000);
let reader = Reader::open(dir.path(), ReaderConfig::default()).unwrap();
let mut beacon = ReaderBeacon::create(dir.path(), "old", &reader).unwrap();
beacon.flush(&reader).unwrap();
append(&mut writer, 4 * per + 1, 1000, 0);
writer.checkpoint().unwrap();
let s = status(dir.path()).unwrap();
assert_eq!(s.segments.count, 2);
assert_eq!(s.segments.first_seq, 3 * per);
let r = &s.readers[0];
assert_eq!(r.state, BeaconState::Live);
assert_eq!(r.report.unwrap().next_seq, 0);
assert_eq!(r.lag, None);
assert_eq!(r.lag_unknown, Some(LagUnknown::SegmentGone));
assert_eq!(r.oldest_unread_local_ts_ns, None);
assert_eq!(writer.metrics().retention_failures, 0);
}
fn stale_beacon(beacon: ReaderBeacon) -> PathBuf {
let bytes = fs::read(beacon.path()).unwrap();
let path = beacon.path().to_path_buf();
drop(beacon);
fs::write(&path, bytes).unwrap();
path
}
#[test]
fn beacons_live_stale_invalid() {
let dir = tempfile::tempdir().unwrap();
let mut writer = Writer::open(dir.path(), config()).unwrap();
let now = crate::clock::realtime_ns();
append(&mut writer, 10, 10, now);
let mut reader = Reader::open(dir.path(), ReaderConfig::default()).unwrap();
reader.try_next().unwrap().unwrap();
let dead = stale_beacon(ReaderBeacon::create(dir.path(), "dead", &reader).unwrap());
for _ in 0..3 {
reader.try_next().unwrap().unwrap();
}
let beacon = ReaderBeacon::create(dir.path(), "live-1", &reader).unwrap();
assert_eq!(beacon.path(), dir.path().join("readers").join("live-1"));
assert!(beacon.is_published());
let readers = dir.path().join("readers");
let valid = fs::read(beacon.path()).unwrap();
fs::write(readers.join("garbage"), [7u8; BEACON_LEN]).unwrap();
fs::write(readers.join("short"), [7u8; 10]).unwrap();
let mut long = valid.clone();
long.push(0);
fs::write(readers.join("long"), long).unwrap();
fs::write(readers.join(".hidden"), &valid).unwrap();
fs::write(readers.join("checkpoint"), &valid).unwrap();
fs::create_dir(readers.join("subdir")).unwrap();
std::os::unix::fs::symlink(&dead, readers.join("link")).unwrap();
let s = status(dir.path()).unwrap();
assert_eq!(s.readers_dir_state, BeaconDirState::Listed);
let names: Vec<_> = s.readers.iter().map(|r| r.name.as_str()).collect();
assert_eq!(names, ["dead", "garbage", "live-1", "long", "short"]);
let dead = &s.readers[0];
assert_eq!(dead.state, BeaconState::Stale);
assert_eq!(dead.report.unwrap().pid, std::process::id());
assert_eq!(
dead.lag,
Some(Lag {
records: 9,
bytes: Some(9 * frame(10))
})
);
for invalid in [1, 3, 4] {
assert_eq!(s.readers[invalid].state, BeaconState::Invalid);
assert_eq!(s.readers[invalid].report, None);
assert_eq!(s.readers[invalid].lag, None);
assert_eq!(s.readers[invalid].lag_unknown, None);
}
let live = &s.readers[2];
assert_eq!(live.state, BeaconState::Live);
let report = live.report.unwrap();
assert_eq!(report.pid, std::process::id());
assert_eq!(report.next_seq, 4);
assert_eq!(report.position, reader.cursor().position);
assert!(!report.durable_only && !report.seek_pending);
assert_eq!(live.lag.unwrap().records, 6);
assert_eq!(live.oldest_unread_local_ts_ns, Some(now));
assert!(live.unread_age.is_some());
assert_eq!(
s.breaches(&Thresholds::default().max_reader_lag(5)),
vec![Breach::ReaderLag {
name: "live-1".into()
}]
);
assert!(
s.breaches(&Thresholds::default().max_reader_lag(6))
.is_empty()
);
let err = ReaderBeacon::create(dir.path(), "live-1", &reader).unwrap_err();
assert!(matches!(&err, Error::BeaconInUse { .. }), "{err}");
let taken = ReaderBeacon::create(dir.path(), "dead", &reader).unwrap();
let s = status(dir.path()).unwrap();
assert_eq!(s.readers[0].state, BeaconState::Live);
assert_eq!(s.readers[0].lag.unwrap().records, 6);
drop(beacon);
drop(taken);
assert!(!readers.join("live-1").exists());
assert!(!readers.join("dead").exists());
}
#[test]
fn beacons_never_replace_other_files() {
let dir = tempfile::tempdir().unwrap();
let mut writer = Writer::open(dir.path(), config()).unwrap();
append(&mut writer, 3, 10, 0);
writer.checkpoint().unwrap();
drop(writer);
let reader = Reader::open(dir.path(), ReaderConfig::default()).unwrap();
let snapshot = |dir: &Path| {
let mut files: Vec<(String, Vec<u8>)> = fs::read_dir(dir)
.unwrap()
.map(|e| e.unwrap())
.filter(|e| e.file_type().unwrap().is_file())
.map(|e| {
(
e.file_name().into_string().unwrap(),
fs::read(e.path()).unwrap(),
)
})
.collect();
files.sort();
files
};
let before = snapshot(dir.path());
for name in [
"checkpoint",
"writer.lock",
"notify",
"next-segment.tmp",
"quarantine",
"readers",
"00000000000000000000.tape",
"00000000000000000000.tape.tmp",
"x.tmp",
"CHECKPOINT",
"Writer.Lock",
"NOTIFY",
"Next-Segment.TMP",
"Quarantine",
"READERS",
"00000000000000000000.TAPE",
"x.Tmp",
] {
assert!(!beacon::name_allowed(name), "{name}");
let err = ReaderBeacon::create_in(dir.path(), name, &reader).unwrap_err();
assert!(matches!(err, Error::InvalidConfig(_)), "{name}: {err}");
}
assert_eq!(snapshot(dir.path()), before, "a journal file was changed");
let beacons = tempfile::tempdir().unwrap();
let valid = {
let b = ReaderBeacon::create_in(beacons.path(), "probe", &reader).unwrap();
fs::read(b.path()).unwrap()
};
let mut long = valid.clone();
long.push(0);
let mut damaged = valid.clone();
damaged[3] ^= 1;
for (name, contents) in [
("notes", b"my notes".to_vec()),
("empty", Vec::new()),
("zeros", vec![0u8; BEACON_LEN]),
("long", long),
("damaged", damaged),
] {
let path = beacons.path().join(name);
fs::write(&path, &contents).unwrap();
let err = ReaderBeacon::create_in(beacons.path(), name, &reader).unwrap_err();
assert!(matches!(err, Error::InvalidConfig(_)), "{name}: {err}");
assert_eq!(fs::read(&path).unwrap(), contents, "{name} was changed");
}
let target = beacons.path().join("target");
fs::write(&target, &valid).unwrap();
std::os::unix::fs::symlink(&target, beacons.path().join("link")).unwrap();
assert!(ReaderBeacon::create_in(beacons.path(), "link", &reader).is_err());
assert_eq!(fs::read(&target).unwrap(), valid);
fs::create_dir(beacons.path().join("dir")).unwrap();
assert!(ReaderBeacon::create_in(beacons.path(), "dir", &reader).is_err());
assert!(beacons.path().join("dir").is_dir());
let leftovers: Vec<_> = fs::read_dir(beacons.path())
.unwrap()
.map(|e| e.unwrap().file_name().into_string().unwrap())
.filter(|n| beacon::temporary_name(n))
.collect();
assert!(leftovers.is_empty(), "{leftovers:?}");
}
#[test]
fn leftover_temporary_files_are_removed() {
let dir = tempfile::tempdir().unwrap();
drop(Writer::open(dir.path(), config()).unwrap());
let reader = Reader::open(dir.path(), ReaderConfig::default()).unwrap();
let beacons = tempfile::tempdir().unwrap();
let valid = {
let b = ReaderBeacon::create_in(beacons.path(), "probe", &reader).unwrap();
fs::read(b.path()).unwrap()
};
let dead = beacons.path().join(".r.4242.0.beacon-tmp");
fs::write(&dead, &valid).unwrap();
let notes = beacons.path().join(".notes.123.0.beacon-tmp");
fs::write(¬es, b"my notes").unwrap();
let empty = beacons.path().join(".r.4244.0.beacon-tmp");
fs::write(&empty, b"").unwrap();
let mut longer = valid.clone();
longer.push(0);
let long = beacons.path().join(".r.4245.0.beacon-tmp");
fs::write(&long, &longer).unwrap();
let held = beacons.path().join(".r.4243.0.beacon-tmp");
let held_file = File::create(&held).unwrap();
rustix::fs::flock(&held_file, rustix::fs::FlockOperation::LockExclusive).unwrap();
let unrelated = beacons.path().join(".something.beacon-tmp");
fs::write(&unrelated, b"").unwrap();
assert!(beacon::temporary_name(".r.4242.0.beacon-tmp"));
assert!(!beacon::temporary_name(".something.beacon-tmp"));
let _b = ReaderBeacon::create_in(beacons.path(), "r", &reader).unwrap();
assert!(!dead.exists());
assert!(held.exists());
assert!(unrelated.exists());
assert_eq!(fs::read(¬es).unwrap(), b"my notes");
assert!(empty.exists());
assert_eq!(fs::read(&long).unwrap(), longer);
}
#[test]
fn torn_stale_beacon_is_taken_over() {
let dir = tempfile::tempdir().unwrap();
let mut writer = Writer::open(dir.path(), config()).unwrap();
append(&mut writer, 5, 10, 0);
let mut reader = Reader::open(dir.path(), ReaderConfig::default()).unwrap();
let path = stale_beacon(ReaderBeacon::create(dir.path(), "torn", &reader).unwrap());
reader.try_next().unwrap().unwrap();
let newer = {
let beacons = tempfile::tempdir().unwrap();
let b = ReaderBeacon::create_in(beacons.path(), "newer", &reader).unwrap();
fs::read(b.path()).unwrap()
};
let old = fs::read(&path).unwrap();
let mut torn = newer[..40].to_vec();
torn.extend_from_slice(&old[40..]);
assert_ne!(torn, old);
fs::write(&path, &torn).unwrap();
assert!(beacon::decode(&torn).is_none());
let s = status(dir.path()).unwrap();
assert_eq!(s.readers[0].state, BeaconState::Invalid);
let taken = ReaderBeacon::create(dir.path(), "torn", &reader).unwrap();
assert!(taken.is_published());
let s = status(dir.path()).unwrap();
assert_eq!(s.readers[0].state, BeaconState::Live);
assert_eq!(s.readers[0].report.unwrap().next_seq, 1);
}
#[test]
fn only_what_a_beacon_writer_could_write_is_a_beacon() {
assert_eq!(format::SEGMENT_SIZE_UNIT, 1 << 16);
assert_eq!(format::MAX_SEGMENT_SIZE, 1 << 40);
assert_eq!(format::RECORD_ALIGN, 8);
let dir = tempfile::tempdir().unwrap();
let mut writer = Writer::open(dir.path(), config()).unwrap();
append(&mut writer, 3, 10, 0);
let first = {
let mut reader = Reader::open(dir.path(), ReaderConfig::default()).unwrap();
reader.try_next().unwrap().unwrap();
reader.try_next().unwrap().unwrap();
let beacons = tempfile::tempdir().unwrap();
let b = ReaderBeacon::create_in(beacons.path(), "a", &reader).unwrap();
fs::read(b.path()).unwrap()
};
drop(writer);
let mut writer = Writer::open(
dir.path(),
WriterConfig::default()
.segment_size(4 * SEG)
.prefault(false),
)
.unwrap();
append(&mut writer, records_per_segment(10) + 5, 10, 0);
writer.checkpoint().unwrap();
let second = {
let mut reader =
Reader::open(dir.path(), ReaderConfig::default().durable_only(true)).unwrap();
reader.seek(records_per_segment(10) + 2).unwrap();
let beacons = tempfile::tempdir().unwrap();
let b = ReaderBeacon::create_in(beacons.path(), "b", &reader).unwrap();
fs::read(b.path()).unwrap()
};
let (first, second): ([u8; BEACON_LEN], [u8; BEACON_LEN]) =
(first.try_into().unwrap(), second.try_into().unwrap());
for at in [12, 40, 48, 56, 64, 80] {
assert_ne!(first[at..at + 4], second[at..at + 4], "offset {at}");
}
for cut in 0..=BEACON_LEN {
for (new, old) in [(&first, &second), (&second, &first)] {
let mut torn = *old;
torn[..cut].copy_from_slice(&new[..cut]);
assert!(beacon::beacon_file(&torn), "cut at {cut}");
}
}
let mut ff = [0xffu8; BEACON_LEN];
ff[..12].copy_from_slice(&first[..12]);
assert!(!beacon::beacon_file(&ff));
for (at, value) in [
(12, 4),
(15, 1),
(16, 0),
(19, 0x80),
(20, 1),
(48, 1),
(53, 1),
(64, 1),
(69, 2),
(71, 1),
(100, 1),
] {
let mut bad = first;
if at == 16 {
bad[16..20].fill(0);
} else {
bad[at] = value;
}
assert!(!beacon::beacon_file(&bad), "byte {at} = {value}");
assert!(beacon::decode(&bad).is_none(), "byte {at} = {value}");
}
for report in [&first, &second] {
assert!(u64::from_le_bytes(report[48..56].try_into().unwrap()) >= 64);
assert_ne!(report[64..72], [0; 8]);
}
let mut zeros = [0u8; BEACON_LEN];
zeros[..20].copy_from_slice(&first[..20]);
zeros[12..16].fill(0);
assert!(!beacon::beacon_file(&zeros));
for (field, value) in [(48, 0u64), (48, 56), (64, 0)] {
let mut mix = first;
mix[field..field + 8].copy_from_slice(&value.to_le_bytes());
assert!(beacon::beacon_file(&mix), "field {field} = {value}");
let mut both = mix;
let other = if field == 48 { 64 } else { 48 };
both[other..other + 8].fill(0);
assert!(
!beacon::beacon_file(&both),
"field {field} = {value} and {other} zero"
);
}
let reader = Reader::open(dir.path(), ReaderConfig::default()).unwrap();
let beacons = tempfile::tempdir().unwrap();
let named = beacons.path().join("ff");
fs::write(&named, ff).unwrap();
let leftover = beacons.path().join(".r.4246.0.beacon-tmp");
fs::write(&leftover, ff).unwrap();
let err = ReaderBeacon::create_in(beacons.path(), "ff", &reader).unwrap_err();
assert!(matches!(err, Error::InvalidConfig(_)), "{err}");
assert_eq!(fs::read(&named).unwrap(), ff);
let _r = ReaderBeacon::create_in(beacons.path(), "r", &reader).unwrap();
assert_eq!(fs::read(&leftover).unwrap(), ff);
let mut torn = second;
torn[..44].copy_from_slice(&first[..44]);
assert!(beacon::decode(&torn).is_none());
let named = beacons.path().join("torn");
fs::write(&named, torn).unwrap();
let leftover = beacons.path().join(".x.4247.0.beacon-tmp");
fs::write(&leftover, torn).unwrap();
let taken = ReaderBeacon::create_in(beacons.path(), "torn", &reader).unwrap();
assert!(taken.is_published());
assert!(!leftover.exists());
drop(writer);
}
#[test]
fn a_change_of_reader_mode_is_published() {
let dir = tempfile::tempdir().unwrap();
let mut writer = Writer::open(dir.path(), config()).unwrap();
append(&mut writer, 2, 10, 0);
writer.checkpoint().unwrap();
append(&mut writer, 3, 10, 0);
let mut visible = Reader::open(dir.path(), ReaderConfig::default()).unwrap();
visible.try_next().unwrap().unwrap();
let mut durable = Reader::open(dir.path(), ReaderConfig::default().durable_only(true)).unwrap();
durable.try_next().unwrap().unwrap();
assert_eq!(visible.position(), durable.position());
let mut beacon = ReaderBeacon::create(dir.path(), "r", &visible).unwrap();
beacon.set_interval(Duration::ZERO);
let reported = |dir: &Path| {
let s = status(dir).unwrap();
let r = &s.readers[0];
(r.report.unwrap().durable_only, r.lag.unwrap().records)
};
assert_eq!(reported(dir.path()), (false, 4));
assert!(
beacon.update(&durable).unwrap(),
"the durable-only reader was not published"
);
assert_eq!(reported(dir.path()), (true, 1));
assert!(
beacon.update(&visible).unwrap(),
"the visible reader was not published"
);
assert_eq!(reported(dir.path()), (false, 4));
assert!(!beacon.update(&visible).unwrap());
drop(writer);
}
#[test]
fn unreadable_beacons_and_directories() {
let dir = tempfile::tempdir().unwrap();
let mut writer = Writer::open(dir.path(), config()).unwrap();
append(&mut writer, 3, 10, 0);
let reader = Reader::open(dir.path(), ReaderConfig::default()).unwrap();
let beacon = ReaderBeacon::create(dir.path(), "secret", &reader).unwrap();
fs::set_permissions(beacon.path(), fs::Permissions::from_mode(0o000)).unwrap();
let root = File::open(beacon.path()).is_ok();
let s = status(dir.path()).unwrap();
fs::set_permissions(beacon.path(), fs::Permissions::from_mode(0o644)).unwrap();
if !root {
assert_eq!(s.readers[0].state, BeaconState::Unreadable);
assert_eq!(s.readers[0].report, None);
}
assert_eq!(s.head.position.next_seq, 3);
drop(beacon);
let readers = dir.path().join("readers");
fs::set_permissions(&readers, fs::Permissions::from_mode(0o000)).unwrap();
let root = fs::read_dir(&readers).is_ok();
let s = status(dir.path()).unwrap();
fs::set_permissions(&readers, fs::Permissions::from_mode(0o755)).unwrap();
if !root {
assert_eq!(
s.readers_dir_state,
BeaconDirState::Unusable(io::ErrorKind::PermissionDenied)
);
assert!(s.readers.is_empty());
}
fs::remove_dir(&readers).unwrap();
fs::write(&readers, b"x").unwrap();
let s = status(dir.path()).unwrap();
assert!(matches!(s.readers_dir_state, BeaconDirState::Unusable(_)));
assert!(s.readers.is_empty());
assert_eq!(s.head.position.next_seq, 3);
}
#[test]
fn shared_beacon_directory() {
let a = tempfile::tempdir().unwrap();
let b = tempfile::tempdir().unwrap();
let beacons = tempfile::tempdir().unwrap();
let mut wa = Writer::open(a.path(), config()).unwrap();
append(&mut wa, 5, 10, 0);
let mut wb = Writer::open(b.path(), config()).unwrap();
append(&mut wb, 150, 10, 0);
let mut ra = Reader::open(a.path(), ReaderConfig::default()).unwrap();
while ra.try_next().unwrap().is_some() {}
let _beacon = ReaderBeacon::create_in(beacons.path(), "ra", &ra).unwrap();
let s = Sampler::new(b.path())
.readers_dir(beacons.path())
.sample()
.unwrap();
let r = &s.readers[0];
assert_eq!(r.state, BeaconState::Live);
assert_eq!(r.lag, None);
assert_eq!(r.lag_unknown, Some(LagUnknown::OtherHistory));
assert!(
s.breaches(&Thresholds::default().max_reader_lag(0))
.is_empty()
);
let s = Sampler::new(a.path())
.readers_dir(beacons.path())
.sample()
.unwrap();
assert_eq!(s.readers[0].lag, Some(caught_up(0)));
}
#[test]
fn durable_only_beacon() {
let dir = tempfile::tempdir().unwrap();
let mut writer = Writer::open(dir.path(), config()).unwrap();
append(&mut writer, 5, 10, 0);
writer.checkpoint().unwrap();
append(&mut writer, 20, 10, 0);
let mut reader = Reader::open(dir.path(), ReaderConfig::default().durable_only(true)).unwrap();
while reader.try_next().unwrap().is_some() {}
let _beacon = ReaderBeacon::create(dir.path(), "durable", &reader).unwrap();
let s = status(dir.path()).unwrap();
let r = &s.readers[0];
assert!(r.report.unwrap().durable_only);
assert_eq!(r.lag, Some(caught_up(0)));
assert_eq!(r.unread_age, Some(Duration::ZERO));
assert!(
s.breaches(&Thresholds::default().max_reader_lag(0))
.is_empty()
);
}
#[test]
fn beacon_names_are_validated() {
let dir = tempfile::tempdir().unwrap();
let _writer = Writer::open(dir.path(), config()).unwrap();
let reader = Reader::open(dir.path(), ReaderConfig::default()).unwrap();
let long = "x".repeat(65);
for bad in ["", ".x", "a/b", "a b", "é", long.as_str(), ".."] {
assert!(
matches!(
ReaderBeacon::create(dir.path(), bad, &reader),
Err(Error::InvalidConfig(_))
),
"{bad:?}"
);
}
for good in ["a", "A-z_0.9", &"x".repeat(64)] {
ReaderBeacon::create(dir.path(), good, &reader).unwrap();
}
}
#[test]
fn beacon_update_is_rate_limited() {
let dir = tempfile::tempdir().unwrap();
let mut writer = Writer::open(dir.path(), config()).unwrap();
append(&mut writer, 5, 10, 0);
let mut reader = Reader::open(dir.path(), ReaderConfig::default()).unwrap();
let mut beacon = ReaderBeacon::create(dir.path(), "r", &reader).unwrap();
beacon.set_interval(HOUR);
let read_back = |dir: &Path| status(dir).unwrap().readers[0].report.unwrap().next_seq;
assert!(!beacon.update(&reader).unwrap());
reader.try_next().unwrap().unwrap();
assert!(!beacon.update(&reader).unwrap());
assert_eq!(read_back(dir.path()), 0);
beacon.flush(&reader).unwrap();
assert_eq!(read_back(dir.path()), 1);
beacon.set_interval(Duration::ZERO);
assert!(!beacon.update(&reader).unwrap(), "position unchanged");
reader.try_next().unwrap().unwrap();
assert!(beacon.update(&reader).unwrap());
assert_eq!(read_back(dir.path()), 2);
reader.seek(4).unwrap();
assert!(beacon.update(&reader).unwrap());
let report = status(dir.path()).unwrap().readers[0].report.unwrap();
assert!(report.seek_pending);
assert_eq!(report.next_seq, 4);
reader.seek(10).unwrap();
assert!(beacon.update(&reader).unwrap());
assert_eq!(
status(dir.path()).unwrap().readers[0]
.report
.unwrap()
.position
.next_seq,
0
);
assert!(reader.try_next().unwrap().is_none());
assert_eq!(reader.position(), 10);
assert!(beacon.update(&reader).unwrap(), "the cursor moved");
let report = status(dir.path()).unwrap().readers[0].report.unwrap();
assert_eq!(report.next_seq, 10);
assert_eq!(report.position.next_seq, 5);
assert!(report.seek_pending);
assert!(!beacon.update(&reader).unwrap(), "nothing moved");
}
#[test]
fn unread_age_of_a_future_timestamp_is_unknown() {
let dir = tempfile::tempdir().unwrap();
let mut writer = Writer::open(dir.path(), config()).unwrap();
let future = crate::clock::realtime_ns() + u64::try_from(HOUR.as_nanos()).unwrap();
append(&mut writer, 3, 10, future);
let reader = Reader::open(dir.path(), ReaderConfig::default()).unwrap();
let _beacon = ReaderBeacon::create(dir.path(), "r", &reader).unwrap();
let s = status(dir.path()).unwrap();
let r = &s.readers[0];
assert_eq!(r.lag.unwrap().records, 3);
assert_eq!(r.oldest_unread_local_ts_ns, Some(future));
assert_eq!(r.unread_age, None);
}
#[cfg(feature = "fault-injection")]
#[test]
fn a_beacon_written_during_the_sample_has_an_update_age() {
use crate::fault::{Pause, on_pause};
let dir = tempfile::tempdir().unwrap();
let mut writer = Writer::open(dir.path(), config()).unwrap();
append(&mut writer, 3, 10, 0);
let mut reader = Reader::open(dir.path(), ReaderConfig::default()).unwrap();
let mut beacon = ReaderBeacon::create(dir.path(), "r", &reader).unwrap();
let mut written = false;
on_pause(Some(Box::new(move |p| {
if p == Pause::ObserverOpened && !written {
written = true;
std::thread::sleep(Duration::from_millis(2));
reader.try_next().unwrap().unwrap();
beacon.flush(&reader).unwrap();
}
})));
let s = status(dir.path());
on_pause(None);
let s = s.unwrap();
assert_eq!(
s.readers[0].report.unwrap().next_seq,
1,
"the observer read the new write"
);
assert!(s.readers[0].update_age.is_some());
assert!(s.readers[0].report.unwrap().updated_unix_ns <= s.sampled_unix_ns);
}
#[cfg(feature = "fault-injection")]
#[test]
fn a_beacon_replaced_during_every_observation_is_changing() {
use crate::fault::{Pause, on_pause};
let dir = tempfile::tempdir().unwrap();
let mut writer = Writer::open(dir.path(), config()).unwrap();
append(&mut writer, 3, 10, 0);
let reader = Reader::open(dir.path(), ReaderConfig::default()).unwrap();
let journal = dir.path().to_path_buf();
let mut beacon = Some(ReaderBeacon::create(dir.path(), "r", &reader).unwrap());
on_pause(Some(Box::new(move |p| {
if p == Pause::ObserverRead {
drop(beacon.take());
beacon = Some(ReaderBeacon::create(&journal, "r", &reader).unwrap());
}
})));
let s = status(dir.path());
on_pause(None);
let s = s.unwrap();
assert_eq!(s.readers.len(), 1);
let r = &s.readers[0];
assert_eq!(r.state, BeaconState::Changing);
assert_eq!(r.report, None);
assert_eq!(r.lag, None);
assert_eq!(r.lag_unknown, None);
assert!(
s.breaches(&Thresholds::default().max_reader_lag(0))
.is_empty()
);
}
#[test]
fn beacon_encoding_round_trips_and_rejects_damage() {
let payload = beacon::Payload {
report: BeaconReport {
pid: 7,
next_seq: u64::MAX - 1,
position: Position::new(12350, 12345, 4096),
seek_pending: true,
durable_only: true,
updated_unix_ns: 99,
},
segment_file: (3, 4),
segment_size: SEG,
segment_created_unix_ns: 5,
};
let b = beacon::encode(&payload);
assert_eq!(b.len(), BEACON_LEN);
assert_eq!(beacon::decode(&b), Some(payload));
for i in 0..BEACON_LEN {
let mut damaged = b;
damaged[i] ^= 1;
assert_eq!(beacon::decode(&damaged), None, "byte {i}");
}
assert_eq!(beacon::decode(&b[..BEACON_LEN - 1]), None);
let mut long = b.to_vec();
long.push(0);
assert_eq!(beacon::decode(&long), None);
}
#[test]
fn writer_metrics() {
let dir = tempfile::tempdir().unwrap();
let mut writer = Writer::open(dir.path(), config()).unwrap();
let m = writer.metrics();
assert_eq!(m.head, Position::new(0, 0, 64));
assert_eq!(m.segment_size, SEG);
assert_eq!(m.checkpoint, None);
assert_eq!(m.since_checkpoint, None);
assert_eq!(m.uncheckpointed_records, 0);
assert_eq!(m.uncheckpointed_bytes, 0);
assert!(!m.poisoned);
assert_eq!(m.retention_failures, 0);
let per = records_per_segment(1000);
append(&mut writer, per + 2, 1000, 0);
let m = writer.metrics();
assert_eq!(m.head, Position::new(per + 2, per, 64 + 2 * frame(1000)));
assert_eq!(m.uncheckpointed_records, per + 2);
assert_eq!(
m.uncheckpointed_bytes,
per * frame(1000) + 8 + 2 * frame(1000)
);
assert_eq!(status(dir.path()).unwrap().head.position, m.head);
writer.checkpoint().unwrap();
let m = writer.metrics();
assert_eq!(m.checkpoint.unwrap().next_seq, per + 2);
assert_eq!(m.checkpoint.unwrap().generation, 1);
assert!(m.since_checkpoint.is_some());
assert_eq!(m.uncheckpointed_records, 0);
assert_eq!(m.uncheckpointed_bytes, 0);
append(&mut writer, 1, 1000, 0);
assert_eq!(writer.metrics().uncheckpointed_bytes, frame(1000));
drop(writer);
let missing = dir.path().join("no-such-archive");
let config = config().retention(Policy::default().max_segments(1), Action::MoveTo(missing));
let mut writer = Writer::open(dir.path(), config).unwrap();
let m = writer.metrics();
assert_eq!(m.checkpoint.unwrap().next_seq, per + 2);
assert_eq!(m.since_checkpoint, None);
assert_eq!(m.uncheckpointed_records, 1);
assert_eq!(m.uncheckpointed_bytes, frame(1000));
assert_eq!(m.retention_failures, 1);
writer.checkpoint().unwrap();
assert_eq!(writer.metrics().retention_failures, 2);
assert!(writer.last_retention_error().is_some());
}
#[test]
fn writer_metrics_after_reopening_across_rolls() {
let dir = tempfile::tempdir().unwrap();
let mut writer = Writer::open(dir.path(), config()).unwrap();
let per = records_per_segment(1000);
append(&mut writer, 10, 1000, 0);
writer.checkpoint().unwrap();
append(&mut writer, 2 * per, 1000, 0);
let before = writer.metrics();
drop(writer);
let writer = Writer::open(dir.path(), config()).unwrap();
let after = writer.metrics();
assert_eq!(after.uncheckpointed_records, before.uncheckpointed_records);
assert_eq!(after.uncheckpointed_bytes, before.uncheckpointed_bytes);
assert_eq!(after.head, before.head);
}
#[test]
fn writer_metrics_without_a_marker_after_retention() {
let dir = tempfile::tempdir().unwrap();
let per = records_per_segment(1000);
let retain = config().retention(Policy::default().max_segments(1), Action::Delete);
let mut writer = Writer::open(dir.path(), retain).unwrap();
append(&mut writer, 2 * per + 1, 1000, 0);
writer.checkpoint().unwrap();
drop(writer);
fs::remove_file(dir.path().join("checkpoint")).unwrap();
let writer = Writer::open(dir.path(), config()).unwrap();
let s = status(dir.path()).unwrap();
assert_eq!(s.segments.first_seq, 2 * per);
assert_eq!(s.exposure.records, Some(1));
assert_eq!(writer.metrics().uncheckpointed_records, 1);
}