use std::sync::atomic::{AtomicU8, AtomicU32, Ordering};
use std::{cell::Cell, marker::PhantomData, rc::Rc};
pub const NONE: u8 = 0;
pub const LAYOUT_CREATED: u8 = 1;
pub const FIRST_MANIFEST_PUBLISHED: u8 = 2;
pub const GENERATION_ID_RESERVED: u8 = 3;
pub const GENERATION_CREATED: u8 = 4;
pub const FREEZE_PUBLISHED: u8 = 5;
pub const SEGMENT_BEING_WRITTEN: u8 = 6;
pub const SEGMENT_FILES_RENAMED: u8 = 7;
pub const SEAL_PUBLISHED: u8 = 8;
pub const DROP_PUBLISHED: u8 = 9;
pub const MANIFEST_TMP_WRITTEN: u8 = 10;
pub const MANIFEST_RENAMED: u8 = 11;
pub fn fail_meta_read<K, T, C, H>(map: &super::SegMap<K, T, C, H>, n: u64)
where
K: crate::Key + std::hash::Hash + Eq + Send + Sync + 'static,
C: crate::Codec<T>,
H: crate::TypedWriteHook<K, T>,
{
map.core().shared.io.fail_read_at(n);
}
pub fn meta_read_attempts<K, T, C, H>(map: &super::SegMap<K, T, C, H>) -> u64
where
K: crate::Key + std::hash::Hash + Eq + Send + Sync + 'static,
C: crate::Codec<T>,
H: crate::TypedWriteHook<K, T>,
{
map.core().shared.io.read_attempts()
}
#[cfg(feature = "test-faults")]
pub fn make_legacy_fixture(collection_dir: &std::path::Path) -> crate::DbResult<()> {
use super::manifest;
use super::seal::{SEG_DIR, sidx_name};
use super::sidecar::{
SECTION_PAGE_INDEX, SIDX_FOOTER_LEN, Sidecar, encode_sidecar, parse_footer, parse_table,
};
let mut manifest = manifest::load(collection_dir)?
.ok_or(crate::DbError::Internal("legacy fixture has no manifest"))?;
for rec in &mut manifest.segments {
let path = collection_dir
.join(SEG_DIR)
.join(sidx_name(rec.segment_id, rec.version));
let bytes = std::fs::read(&path)?;
let fault = |fault: super::FormatFault| fault.into_db(rec.segment_id, None, &path);
let original = Sidecar::parse(&bytes).map_err(fault)?;
let footer = bytes.len() - SIDX_FOOTER_LEN;
let (at, count, crc) = parse_footer(&bytes[footer..], bytes.len() as u64).map_err(fault)?;
let table = parse_table(&bytes[at as usize..footer], count, crc, at).map_err(fault)?;
let sections: Vec<_> = table
.into_iter()
.filter(|section| section.kind != SECTION_PAGE_INDEX)
.map(|section| {
(
section.kind,
bytes[section.offset as usize..(section.offset + section.len) as usize]
.to_vec(),
)
})
.collect();
let legacy = encode_sidecar(&original.header, §ions);
std::fs::write(&path, &legacy)?;
rec.sidx_len = legacy.len() as u64;
rec.sidx_checksum = xxhash_rust::xxh3::xxh3_64(&legacy);
}
manifest::publish(super::fs::real().as_ref(), collection_dir, &manifest)
.map_err(|error| error.into_inner())
}
pub static STAGE: AtomicU8 = AtomicU8::new(NONE);
pub static SKIP: AtomicU32 = AtomicU32::new(0);
pub fn hit(stage: u8) {
if STAGE.load(Ordering::SeqCst) != stage {
return;
}
let passed = SKIP
.fetch_update(Ordering::SeqCst, Ordering::SeqCst, |left| {
left.checked_sub(1)
})
.is_ok();
if passed {
return;
}
std::process::exit(9);
}
#[must_use]
pub struct WorkerPauseGuard(PhantomData<Rc<()>>);
thread_local! { static WORKER_PAUSED: Cell<bool> = const { Cell::new(false) }; }
impl WorkerPauseGuard {
pub fn new() -> Self {
assert!(
WORKER_PAUSED.with(|paused| !paused.replace(true)),
"segmented worker startup is already paused"
);
Self(PhantomData)
}
}
impl Default for WorkerPauseGuard {
fn default() -> Self {
Self::new()
}
}
impl Drop for WorkerPauseGuard {
fn drop(&mut self) {
WORKER_PAUSED.with(|paused| paused.set(false));
}
}
pub(crate) fn worker_paused() -> bool {
WORKER_PAUSED.with(Cell::get)
}
pub fn flush_active<K, T, C, H>(map: &super::SegMap<K, T, C, H>) -> crate::DbResult<()>
where
K: crate::Key + std::hash::Hash + Eq + Send + Sync + 'static,
C: crate::Codec<T>,
H: crate::TypedWriteHook<K, T>,
{
map.core().view().active.durable_flush()
}
pub fn generation_ranges<K, T, C, H>(map: &super::SegMap<K, T, C, H>) -> Vec<(u64, usize, u64, u64)>
where
K: crate::Key + std::hash::Hash + Eq + Send + Sync + 'static,
C: crate::Codec<T>,
H: crate::TypedWriteHook<K, T>,
{
let view = map.core().view();
std::iter::once(&view.active)
.chain(view.frozen.iter())
.map(|g| (g.id(), g.shard_count(), g.min_gsn(), g.max_gsn()))
.collect()
}
#[must_use]
pub struct FinishFailGuard(PhantomData<Rc<()>>);
thread_local! { static FINISH_FAIL: Cell<bool> = const { Cell::new(false) }; }
impl FinishFailGuard {
pub fn new() -> Self {
assert!(
FINISH_FAIL.with(|failed| !failed.replace(true)),
"segmented finish failure is already armed"
);
Self(PhantomData)
}
}
impl Default for FinishFailGuard {
fn default() -> Self {
Self::new()
}
}
impl Drop for FinishFailGuard {
fn drop(&mut self) {
FINISH_FAIL.with(|failed| failed.set(false));
}
}
pub(crate) fn finish_failed() -> bool {
FINISH_FAIL.with(Cell::get)
}
pub fn frozen_record_gsns<K, T, C, H>(map: &super::SegMap<K, T, C, H>) -> crate::DbResult<Vec<u64>>
where
K: crate::Key + std::hash::Hash + Eq + Send + Sync + 'static,
C: crate::Codec<T>,
H: crate::TypedWriteHook<K, T>,
{
use zerocopy::FromBytes;
let core = map.core();
let view = core.view();
let mut gsns = Vec::new();
for generation in &view.frozen {
for (hash, _, loc) in generation.snapshot_range(0, u64::MAX, core.seed) {
let entry = generation.read_entry(hash, &loc.loc)?;
let header = crate::entry::EntryHeader::read_from_bytes(&entry[..16])
.map_err(|_| crate::DbError::Internal("invalid fault-fixture entry header"))?;
gsns.push(header.sequence());
}
}
Ok(gsns)
}
pub fn tree_flush_active<K, T, C, H>(map: &super::SegTree<K, T, C, H>) -> crate::DbResult<()>
where
K: crate::Key + std::hash::Hash + Eq + Send + Sync + 'static,
C: crate::Codec<T>,
H: crate::TypedWriteHook<K, T>,
{
map.core().view().active.durable_flush()
}
pub fn tree_generation_ranges<K, T, C, H>(
map: &super::SegTree<K, T, C, H>,
) -> Vec<(u64, usize, u64, u64)>
where
K: crate::Key + std::hash::Hash + Eq + Send + Sync + 'static,
C: crate::Codec<T>,
H: crate::TypedWriteHook<K, T>,
{
let view = map.core().view();
std::iter::once(&view.active)
.chain(view.frozen.iter())
.map(|g| (g.id(), g.shard_count(), g.min_gsn(), g.max_gsn()))
.collect()
}
pub fn tree_frozen_record_gsns<K, T, C, H>(
map: &super::SegTree<K, T, C, H>,
) -> crate::DbResult<Vec<u64>>
where
K: crate::Key + std::hash::Hash + Eq + Send + Sync + 'static,
C: crate::Codec<T>,
H: crate::TypedWriteHook<K, T>,
{
use zerocopy::FromBytes;
let core = map.core();
let view = core.view();
let mut gsns = Vec::new();
for generation in &view.frozen {
for (hash, _, loc) in generation.snapshot_range(0, u64::MAX, core.seed) {
let entry = generation.read_entry(hash, &loc.loc)?;
let header = crate::entry::EntryHeader::read_from_bytes(&entry[..16])
.map_err(|_| crate::DbError::Internal("invalid fault-fixture entry header"))?;
gsns.push(header.sequence());
}
}
Ok(gsns)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn worker_scoped_isolation_allows_foreign_thread_startup() {
let _pause = WorkerPauseGuard::new();
assert!(worker_paused());
std::thread::spawn(|| {
let dir = tempfile::tempdir().unwrap();
let core = super::super::testkit::open_core_with_worker(
dir.path(),
super::super::SegConfig::test(),
);
assert!(
core.worker_running(),
"foreign caller must start its normal worker"
);
})
.join()
.unwrap();
}
#[test]
fn finish_scoped_isolation_does_not_fail_foreign_caller() {
let _fail = FinishFailGuard::new();
assert!(finish_failed());
std::thread::spawn(|| {
use crate::txn::participant::TxParticipant;
let dir = tempfile::tempdir().unwrap();
let map: super::super::SegMap<
super::super::testkit::TestKey,
String,
super::super::testkit::TextCodec,
> = super::super::SegMap::open_with(
dir.path(),
super::super::testkit::meta(),
super::super::SegConfig::test(),
super::super::testkit::TextCodec,
super::super::core::OpenOptions {
worker: false,
..super::super::core::OpenOptions::production()
},
)
.unwrap();
let held = map.lock_shards(&[]).unwrap();
held.finish(
&mut Vec::new(),
&mut crate::watch::WatchWakeBatch::default(),
)
.expect("foreign caller must finish normally");
assert!(
!finish_failed(),
"foreign caller must not inherit finish injection"
);
})
.join()
.unwrap();
}
#[test]
fn worker_and_finish_scoped_panic_reset() {
assert!(
std::panic::catch_unwind(|| {
let _pause = WorkerPauseGuard::new();
let _fail = FinishFailGuard::new();
assert!(worker_paused());
assert!(finish_failed());
panic!("reset scoped controls");
})
.is_err()
);
assert!(!worker_paused());
assert!(!finish_failed());
let _pause = WorkerPauseGuard::new();
let _fail = FinishFailGuard::new();
}
}