use crate::codec::Codec;
use crate::error::{DbError, DbResult};
use crate::hook::TypedWriteHook;
use crate::key::Key;
use crate::seg::core::{SegCore, View};
use crate::seg::freeze::Admission;
use crate::seg::generation::{GenLookup, GenProbe, Generation, GenerationLocks};
use crate::seg::hash::{key_hash, overlapping_shards, shard_for_hash};
use crate::seg::{SegMap, SegTree};
use crate::txlog::record::{OpKind, VersionTag};
use crate::txn::TxTarget;
use crate::txn::participant::{DeferredReplay, PlannedOp, TxBackend, TxLocked, TxParticipant};
use std::collections::HashMap;
use std::hash::Hash;
use std::sync::Arc;
use std::sync::atomic::Ordering;
use std::time::Instant;
mod commit;
mod replay;
use commit::SegCommitLocked;
use replay::SegReplayLocked;
pub(crate) trait SegTxTarget {}
macro_rules! impl_segmented_participant {
($facade:ident) => {
impl<K, T, C, H> TxTarget for $facade<K, T, C, H>
where
K: Key + Hash + Eq + Send + Sync + 'static,
T: Send + Sync + 'static,
C: Codec<T> + 'static,
H: TypedWriteHook<K, T> + 'static,
{
type Key = K;
type Val = T;
fn tx_encode(&self, value: &T, out: &mut Vec<u8>) -> DbResult<()> {
self.codec.encode_to(value, out)
}
fn tx_decode(&self, bytes: &[u8]) -> DbResult<T> {
self.codec.decode_from(bytes)
}
}
impl<K, T, C, H> SegTxTarget for $facade<K, T, C, H>
where
K: Key + Hash + Eq + Send + Sync + 'static,
T: Send + Sync + 'static,
C: Codec<T> + 'static,
H: TypedWriteHook<K, T> + 'static,
{
}
impl<K, T, C, H> TxParticipant for $facade<K, T, C, H>
where
K: Key + Hash + Eq + Send + Sync + 'static,
T: Send + Sync + 'static,
C: Codec<T> + 'static,
H: TypedWriteHook<K, T> + 'static,
{
fn name(&self) -> &'static str {
self.core().meta.name
}
fn backend(&self) -> TxBackend {
TxBackend::Bitcask
}
fn schema_fingerprint(&self) -> u64 {
self.core().meta.typ_hash
}
fn encrypted(&self) -> bool {
false
}
fn max_entry_bytes(&self) -> Option<usize> {
Some(
self.config().write_buffer_size.min(
(u32::MAX as usize).saturating_sub(crate::seg::block::BLOCK_HEADER_LEN),
),
)
}
fn shard_count(&self) -> usize {
self.config().shard_count
}
fn shard_of(&self, key: &[u8]) -> DbResult<usize> {
checked_key::<K>(key)?;
Ok(shard_for_hash(
key_hash(key, self.core().seed),
self.shard_count(),
))
}
fn tx_check_user_op(&self) -> DbResult<()> {
if self.is_index_target_participant() {
return Err(DbError::Config(crate::seg::index_target::DIRECT_WRITE));
}
Ok(())
}
fn tx_needs_old_value(&self) -> bool {
self.hook_active()
}
fn tx_project(
&self,
key: &[u8],
old: Option<&[u8]>,
new: Option<&[u8]>,
) -> DbResult<Vec<crate::txn::participant::TxDerivedWrite>> {
self.project_bytes(&checked_key::<K>(key)?, old, new)
}
fn tx_counter_deltas(
&self,
key: &[u8],
old: Option<&[u8]>,
new: Option<&[u8]>,
) -> DbResult<Vec<crate::txn::participant::TxCounterDelta>> {
self.counter_bytes(&checked_key::<K>(key)?, old, new)
}
fn tx_preplan_projection_targets(&self) -> DbResult<Vec<Arc<dyn TxParticipant>>> {
let mut out = Vec::new();
if let Some(targets) = self.hook.projection_targets() {
for target in targets.erased_targets() {
out.push(targets.resolve_tx_participant(target.name).ok_or(
DbError::Internal(
"declared segmented index target missing participant",
),
)?);
}
}
if let Some(targets) = self.hook.counter_targets() {
out.extend(targets.all().map(|m| m.as_tx_participant()));
}
if H::EMITS
&& let Some(targets) = self.hook.stream_targets()
{
out.extend(targets.tx_participants());
}
Ok(out)
}
fn tx_preplan_stream_targets(&self) -> DbResult<Vec<Arc<dyn TxParticipant>>> {
Ok(if H::EMITS {
self.hook
.stream_targets()
.map(|t| t.tx_participants())
.unwrap_or_default()
} else {
Vec::new()
})
}
fn tx_emits(&self) -> DbResult<bool> {
Ok(H::EMITS)
}
fn tx_prepare_streams(&self) -> DbResult<Vec<crate::queue::retention::ProducerPermit>> {
if H::EMITS {
self.hook
.stream_targets()
.map_or(Ok(Vec::new()), crate::QueueTargets::prepare_streams)
} else {
Ok(Vec::new())
}
}
fn tx_stream_writes(
&self,
key: &[u8],
old: Option<&[u8]>,
new: Option<&[u8]>,
) -> DbResult<crate::txn::participant::TxStreamWrites> {
if !H::EMITS {
return Ok(crate::txn::participant::TxStreamWrites::empty());
}
let Some(targets) = self.hook.stream_targets() else {
return Ok(crate::txn::participant::TxStreamWrites::empty());
};
let key = checked_key::<K>(key)?;
let old = old.map(|v| self.codec.decode_from(v)).transpose()?;
let new = new.map(|v| self.codec.decode_from(v)).transpose()?;
let (rows, bytes) = self.effective_projection_limits();
let mut sink = crate::EmitSink::new(targets, rows, bytes);
self.hook
.stream_project(&key, old.as_ref(), new.as_ref(), &mut sink)?;
targets.lower(&sink)
}
fn tx_admit(&self) -> DbResult<()> {
self.core().admit_tx()
}
fn lock_shards<'a>(&'a self, shards: &[usize]) -> DbResult<Box<dyn TxLocked + 'a>> {
self.lock_shards_since(shards, None)
}
fn lock_shards_since<'a>(
&'a self,
shards: &[usize],
since_ms: Option<u64>,
) -> DbResult<Box<dyn TxLocked + 'a>> {
let callback = self.event_callback()?;
let core = self.core();
loop {
let view = core.view();
let real =
overlapping_shards(shards, self.shard_count(), view.active.shard_count())?;
let locks = view.active.lock_owned(&real)?;
core.check_writable()?;
if view.active.is_frozen() {
drop(locks);
continue;
}
if view.active.is_full() {
drop(locks);
core.make_room(&view.active, Admission::Deadline(Instant::now()))?;
continue;
}
return Ok(Box::new(SegCommitLocked {
locks,
core: core.clone(),
view,
reservations: Vec::new(),
logged: false,
cleaned: false,
events: Vec::new(),
callback,
observed: HashMap::new(),
since_ms,
}));
}
}
fn lock_for_replay<'a>(&'a self, _: &[usize]) -> DbResult<Box<dyn TxLocked + 'a>> {
Ok(Box::new(SegReplayLocked {
core: self.core().clone(),
view: self.core().view(),
events: Vec::new(),
callback: self.event_callback()?,
}))
}
}
impl<K, T, C, H> $facade<K, T, C, H>
where
K: Key + Hash + Eq + Send + Sync + 'static,
T: Send + Sync + 'static,
C: Codec<T> + 'static,
H: TypedWriteHook<K, T> + 'static,
{
fn event_callback(&self) -> DbResult<Option<commit::EventCallback<K>>> {
if !H::NEEDS_WRITE {
return Ok(None);
}
let owner = self
.tx_self
.get()
.and_then(std::sync::Weak::upgrade)
.ok_or(DbError::Internal("segmented hook owner was not bound"))?;
Ok(Some(Box::new(move |event| owner.fire_event(event))))
}
}
};
}
impl_segmented_participant!(SegMap);
impl_segmented_participant!(SegTree);
fn checked_key<K: Key>(bytes: &[u8]) -> DbResult<K> {
if bytes.len() != size_of::<K>() {
return Err(DbError::Internal(
"segmented transaction key length mismatch",
));
}
Ok(K::from_bytes(bytes))
}
fn bitcask(tag: VersionTag) -> DbResult<u64> {
match tag {
VersionTag::Bitcask { gsn } => Ok(gsn),
_ => Err(DbError::Internal(
"wrong version tag on segmented participant",
)),
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::NoHook;
use crate::seg::SegConfig;
use crate::seg::core::OpenOptions;
use crate::seg::testkit::{TestKey, TextCodec, key_of, meta};
fn open(path: &std::path::Path, config: SegConfig) -> SegMap<TestKey, String, TextCodec> {
SegMap::open_with(
path,
meta(),
config,
TextCodec,
OpenOptions {
worker: false,
..OpenOptions::production()
},
)
.unwrap()
}
fn open_tree(
path: &std::path::Path,
config: SegConfig,
) -> crate::seg::SegTree<TestKey, String, TextCodec> {
crate::seg::SegTree::open_with(
path,
meta(),
&[],
config,
TextCodec,
OpenOptions {
worker: false,
..OpenOptions::production()
},
)
.unwrap()
}
#[test]
fn tree_tx_locks_keep_historical_hash_routing() {
use crate::seg::testkit::ordered_key;
let dir = tempfile::tempdir().unwrap();
let mut config = SegConfig::test();
config.shard_count = 3;
let tree = open_tree(dir.path(), config.clone());
tree.put(&ordered_key(1), &"old".to_owned()).unwrap();
let old = tree.core().view().active.clone();
let missing = old.engine().gsn().fetch_add(1, Ordering::Relaxed);
tree.seal().unwrap();
tree.put(&ordered_key(1), &"young".to_owned()).unwrap();
tree.durable_flush().unwrap();
drop(old);
drop(tree);
config.shard_count = 7;
let tree = open_tree(dir.path(), config);
assert_eq!(tree.backend(), TxBackend::Bitcask);
let key = ordered_key(1);
let hash = key_hash(key.as_bytes(), tree.core().seed);
assert_eq!(
tree.shard_of(key.as_bytes()).unwrap(),
shard_for_hash(hash, 7)
);
let held = tree
.lock_shards(&[tree.shard_of(key.as_bytes()).unwrap()])
.unwrap();
assert_eq!(
held.apply_domain(key.as_bytes()).unwrap(),
shard_for_hash(hash, 3)
);
held.abort();
let older = tree.core().view().frozen[0].clone();
let next = tree.core().view().active.next_gsn();
let mut replay = tree.lock_for_replay(&[]).unwrap();
assert!(
replay
.needs_replay(key.as_bytes(), OpKind::Put, tag(missing))
.unwrap()
);
replay
.apply_put(key.as_bytes(), b"replayed", tag(missing))
.unwrap();
assert!(
matches!(older.lookup(hash, &key).unwrap(), GenLookup::Live(v) if v == b"replayed")
);
assert_eq!(tree.core().view().active.next_gsn(), next);
assert_eq!(tree.try_get(&key).unwrap(), Some("young".to_owned()));
}
#[test]
fn tree_tx_apply_and_replay_update_ordered_index() {
use crate::seg::testkit::ordered_key;
let dir = tempfile::tempdir().unwrap();
let tree = open_tree(dir.path(), SegConfig::test());
let keys = [ordered_key(9), ordered_key(1), ordered_key(5)];
let mut held = tree.lock_shards(&[0, 1]).unwrap();
let tags = held
.reserve(&keys.iter().map(plan).collect::<Vec<_>>())
.unwrap();
held.mark_logged();
for (key, version) in keys.iter().zip(tags) {
held.apply_put(key.as_bytes(), b"committed", version)
.unwrap();
}
held.finish(
&mut Vec::new(),
&mut crate::watch::WatchWakeBatch::default(),
)
.unwrap();
tree.durable_flush().unwrap();
drop(tree);
let tree = open_tree(dir.path(), SegConfig::test());
let fresh = tree.core().view().active.next_gsn();
let mut replay = tree.lock_for_replay(&[]).unwrap();
replay
.apply_put(ordered_key(3).as_bytes(), b"replayed", tag(fresh))
.unwrap();
replay
.apply_delete(ordered_key(5).as_bytes(), tag(fresh + 1))
.unwrap();
drop(replay);
let rows = tree.iter().map(|r| r.unwrap().0).collect::<Vec<_>>();
assert_eq!(rows, vec![ordered_key(1), ordered_key(3), ordered_key(9)]);
let mut held = tree.lock_shards(&[0, 1]).unwrap();
held.reserve(&[plan(&ordered_key(7))]).unwrap();
held.mark_logged();
drop(held);
assert!(matches!(
tree.put(&ordered_key(7), &"late".to_owned()),
Err(DbError::CollectionFenced { .. })
));
}
#[test]
fn tree_active_hook_requires_tx() {
let dir = tempfile::tempdir().unwrap();
let seen = Seen::default();
let tree = Arc::new(
crate::seg::SegTree::open_deferred(
dir.path(),
meta(),
&[],
false,
SegConfig::test(),
TextCodec,
Recorder(seen.clone()),
)
.unwrap(),
);
assert!(tree.tx_self.set(Arc::downgrade(&tree)).is_ok());
assert!(tree.tx_needs_old_value());
assert!(matches!(
tree.put(&key_of(1), &"value".to_owned()),
Err(DbError::SegHookRequiresTx { .. })
));
assert!(tree.event_callback().unwrap().is_some());
let key = key_of(1);
tree.core().put(&key, b"old").unwrap();
let mut held = tree
.lock_shards(&[tree.shard_of(key.as_bytes()).unwrap()])
.unwrap();
let version = held.reserve(&[plan(&key)]).unwrap()[0];
held.mark_logged();
assert_eq!(
held.apply_put(key.as_bytes(), b"value", version).unwrap(),
1
);
let mut deferred = held
.finish(
&mut Vec::new(),
&mut crate::watch::WatchWakeBatch::default(),
)
.unwrap();
assert_eq!(deferred.event_count(), 1);
assert!(
tree.tx_project(key_of(1).as_bytes(), None, Some(b"value"))
.unwrap()
.is_empty()
);
drop(tree);
assert!(seen.lock().unwrap().is_empty());
deferred.replay_one(0);
assert_eq!(
*seen.lock().unwrap(),
vec![(Some("old".to_owned()), Some("value".to_owned()))]
);
}
fn cleanup_fixture_with_deadline(
fixture: impl FnOnce() + Send + 'static,
timeout: std::time::Duration,
) -> Result<(), std::sync::mpsc::RecvTimeoutError> {
let (send, recv) = std::sync::mpsc::channel();
std::thread::spawn(move || {
let result = std::panic::catch_unwind(std::panic::AssertUnwindSafe(fixture));
let _ = send.send(result);
});
match recv.recv_timeout(timeout)? {
Ok(()) => Ok(()),
Err(panic) => std::panic::resume_unwind(panic),
}
}
fn bounded_cleanup(fixture: impl FnOnce() + Send + 'static) {
cleanup_fixture_with_deadline(fixture, std::time::Duration::from_secs(15))
.expect("whole cleanup fixture failed to finish before its deadline");
}
#[test]
fn cleanup_deadline_bounds_non_send_context_drop() {
use std::{sync::mpsc, time::Duration};
let (release, released) = mpsc::channel();
let (constructed, construction) = mpsc::channel();
let (finished, finish) = mpsc::channel::<()>();
let releaser = std::thread::spawn(move || {
let _ = finish.recv_timeout(Duration::from_millis(300));
release.send(()).unwrap();
});
let result = cleanup_fixture_with_deadline(
move || {
struct Held {
released: mpsc::Receiver<()>,
_not_send: std::rc::Rc<()>,
}
impl Drop for Held {
fn drop(&mut self) {
self.released.recv().unwrap();
}
}
let _held = Held {
released,
_not_send: std::rc::Rc::new(()),
};
constructed.send(()).unwrap();
},
Duration::from_millis(50),
);
construction.recv_timeout(Duration::from_secs(1)).unwrap();
finished.send(()).ok();
releaser.join().unwrap();
assert_eq!(
result,
Err(mpsc::RecvTimeoutError::Timeout),
"whole fixture deadline must include non-Send context Drop"
);
}
fn tag(gsn: u64) -> VersionTag {
VersionTag::Bitcask { gsn }
}
fn plan(key: &TestKey) -> PlannedOp<'_> {
PlannedOp {
key: key.as_bytes(),
delete: false,
}
}
#[test]
fn replay_routes_to_frozen_generation_with_historical_shards() {
let dir = tempfile::tempdir().unwrap();
let mut config = SegConfig::test();
config.shard_count = 3;
let map = open(dir.path(), config.clone());
map.put(&key_of(1), &"old".to_owned()).unwrap();
let old = map.core().view().active.clone();
let missing = old.engine().gsn().fetch_add(1, Ordering::Relaxed);
map.seal().unwrap();
map.put(&key_of(1), &"young".to_owned()).unwrap();
map.durable_flush().unwrap();
drop(old);
drop(map);
config.shard_count = 7;
let map = open(dir.path(), config);
let old = map.core().view().frozen[0].clone();
assert_eq!(old.shard_count(), 3);
let younger_next = map.core().view().active.next_gsn();
let mut replay = map.lock_for_replay(&[0]).unwrap();
assert!(
replay
.needs_replay(key_of(1).as_bytes(), OpKind::Put, tag(missing))
.unwrap()
);
replay
.apply_put(key_of(1).as_bytes(), b"replayed", tag(missing))
.unwrap();
assert_eq!(map.try_get(&key_of(1)).unwrap(), Some("young".to_owned()));
let h = key_hash(key_of(1).as_bytes(), map.core().seed);
assert!(matches!(old.lookup(h,&key_of(1)).unwrap(),GenLookup::Live(v) if v==b"replayed"));
assert_eq!(map.core().view().active.next_gsn(), younger_next);
assert!(
replay
.needs_replay(key_of(1).as_bytes(), OpKind::Put, tag(missing))
.unwrap()
);
assert_eq!(
old.engine().shards()[shard_for_hash(h, 3)]
.lock()
.armed_gsn(),
None
);
drop(replay);
map.durable_flush().unwrap();
drop(old);
drop(map);
let map = open(dir.path(), SegConfig::test());
assert!(
!map.lock_for_replay(&[])
.unwrap()
.needs_replay(key_of(1).as_bytes(), OpKind::Put, tag(missing))
.unwrap()
);
}
#[test]
fn replay_has_no_admission_with_full_active_and_saturated_queue() {
let dir = tempfile::tempdir().unwrap();
let config = SegConfig {
seal_max_keys: 1,
max_frozen_generations: 1,
..SegConfig::test()
};
let map = open(dir.path(), config.clone());
map.put(&key_of(1), &"one".to_owned()).unwrap();
map.seal().unwrap();
map.put(&key_of(2), &"two".to_owned()).unwrap();
map.durable_flush().unwrap();
drop(map);
let map = open(dir.path(), config);
let core = map.core();
let view = core.view();
assert!(view.active.is_full());
assert_eq!(view.frozen.len(), 1);
let mut replay = map.lock_for_replay(&[0, 1]).unwrap();
let known = view.active.next_gsn() - 1;
assert!(
!replay
.needs_replay(key_of(2).as_bytes(), OpKind::Put, tag(known))
.unwrap()
);
assert_eq!(core.view().active.id(), view.active.id());
assert_eq!(core.view().frozen.len(), 1);
}
#[test]
fn replay_rejects_wrong_tags_and_generation_gaps_and_skips_old_versions() {
let dir = tempfile::tempdir().unwrap();
let map = open(dir.path(), SegConfig::test());
map.put(&key_of(1), &"one".to_owned()).unwrap();
map.seal().unwrap();
let core = map.core();
let view = core.view();
let old = view.frozen[0].clone();
old.mark_frozen(old.frozen_at_ms(), old.min_gsn());
let mut replay = map.lock_for_replay(&[]).unwrap();
let wrong = VersionTag::Fixed {
slot_id: 0,
target_meta: 0,
};
assert!(matches!(
replay.needs_replay(key_of(1).as_bytes(), OpKind::Put, wrong),
Err(DbError::Internal(_))
));
assert!(
!replay
.needs_replay(key_of(1).as_bytes(), OpKind::Put, tag(0))
.unwrap()
);
view.active.seed_gsn(old.min_gsn() + 10);
assert!(matches!(
replay.needs_replay(key_of(1).as_bytes(), OpKind::Put, tag(old.min_gsn() + 1)),
Err(DbError::Internal(_))
));
}
#[test]
fn commit_cleanup_fences_logged_debt_and_disarms_before_unlock_on_drop() {
bounded_cleanup(move || {
let dir = tempfile::tempdir().unwrap();
let map = open(dir.path(), SegConfig::test());
let key = key_of(1);
let shard = map.shard_of(key.as_bytes()).unwrap();
let mut locked = map.lock_shards(&[shard]).unwrap();
let tags = locked.reserve(&[plan(&key)]).unwrap();
locked.mark_logged();
locked.tx_debug_arm(key.as_bytes(), tags[0]).unwrap();
let panic = std::panic::catch_unwind(std::panic::AssertUnwindSafe(move || {
let _held = locked;
panic!("unwind");
}));
assert!(panic.is_err());
assert!(matches!(
map.put(&key, &"late".to_owned()),
Err(DbError::CollectionFenced { .. })
));
let gen_ = map.core().view().active.clone();
assert_eq!(gen_.engine().shards()[shard].lock().armed_gsn(), None);
let _held = gen_.lock_owned(&[shard]).unwrap();
});
}
#[test]
fn unlogged_abort_is_unfenced_and_reservation_tags_follow_original_order() {
let dir = tempfile::tempdir().unwrap();
let map = open(dir.path(), SegConfig::test());
let keys = (0..20).map(key_of).collect::<Vec<_>>();
let mut held = map.lock_shards(&[0, 1]).unwrap();
let ops = keys.iter().map(plan).collect::<Vec<_>>();
let tags = held.reserve(&ops).unwrap();
let mut last = [None, None];
for (key, t) in keys.iter().zip(tags) {
let sid = held.apply_domain(key.as_bytes()).unwrap();
let g = bitcask(t).unwrap();
if let Some(prev) = last[sid] {
assert_eq!(g, prev + 1);
}
last[sid] = Some(g);
}
held.abort();
map.put(&keys[0], &"fine".to_owned()).unwrap();
assert!(map.core().fence_reason().is_none());
}
#[test]
fn published_participant_uses_same_arc_allocation() {
let dir = tempfile::tempdir().unwrap();
let map = Arc::new(open(dir.path(), SegConfig::test()));
let participant: Arc<dyn TxParticipant> = map.clone();
let collection: Arc<dyn crate::armour::Collection> = map.clone();
assert_eq!(
Arc::as_ptr(&participant) as *const (),
Arc::as_ptr(&collection) as *const ()
);
crate::access::ManagedOpen::bind_tx_arc(&map);
assert!(Arc::ptr_eq(
&map,
&map.tx_self.get().unwrap().upgrade().unwrap()
));
let _ = NoHook;
}
#[test]
fn saturated_commit_is_rejected_before_txlog_growth() {
let dir = tempfile::tempdir().unwrap();
let db = crate::armour::Db::open_test(dir.path().join("db")).unwrap();
let config = SegConfig {
seal_max_keys: 1,
max_frozen_generations: 1,
seal_wait_timeout: std::time::Duration::from_millis(1),
..SegConfig::test()
};
let map = Arc::new(open(&dir.path().join("rows"), config));
crate::sync::lock(&db.collections).push(map.clone());
map.put(&key_of(1), &"one".to_owned()).unwrap();
map.seal().unwrap();
map.put(&key_of(2), &"two".to_owned()).unwrap();
let before = db.txlog().total_bytes();
let failure = db
.tx()
.put(&map, &key_of(3), &"three".to_owned())
.unwrap()
.commit();
assert!(
matches!(failure, Err(DbError::SealBackpressure { .. })),
"{failure:?}"
);
assert_eq!(db.txlog().total_bytes(), before);
assert_eq!(map.try_get(&key_of(3)).unwrap(), None);
}
#[test]
fn logged_other_shard_drains_and_finish_reports_real_debt() {
let dir = tempfile::tempdir().unwrap();
let config = SegConfig {
shard_count: 3,
..SegConfig::test()
};
let map = open(dir.path(), config);
map.durable_flush().unwrap();
drop(map);
let map = open(
dir.path(),
SegConfig {
shard_count: 7,
..SegConfig::test()
},
);
let first = key_of(1);
let h = key_hash(first.as_bytes(), map.core().seed);
let domain = shard_for_hash(h, 3);
let other = (2..1000)
.map(key_of)
.find(|k| {
let first_real =
overlapping_shards(&[map.shard_of(first.as_bytes()).unwrap()], 7, 3).unwrap();
let other_real =
overlapping_shards(&[map.shard_of(k.as_bytes()).unwrap()], 7, 3).unwrap();
other_real.iter().all(|s| !first_real.contains(s))
})
.unwrap();
let mut debt = map
.lock_shards(&[map.shard_of(first.as_bytes()).unwrap()])
.unwrap();
let mut drain = map
.lock_shards(&[map.shard_of(other.as_bytes()).unwrap()])
.unwrap();
let debt_tag = debt.reserve(&[plan(&first)]).unwrap()[0];
let drain_tag = drain.reserve(&[plan(&other)]).unwrap()[0];
debt.mark_logged();
drain.mark_logged();
let mut fenced = Vec::new();
debt.finish(&mut fenced, &mut crate::watch::WatchWakeBatch::default())
.unwrap();
assert_eq!(fenced, vec![(domain, bitcask(debt_tag).unwrap())]);
drain
.apply_put(other.as_bytes(), b"drained", drain_tag)
.unwrap();
let mut healthy = Vec::new();
drain
.finish(&mut healthy, &mut crate::watch::WatchWakeBatch::default())
.unwrap();
assert!(healthy.is_empty());
assert_eq!(map.try_get(&other).unwrap(), Some("drained".to_owned()));
}
#[test]
fn replay_apply_failure_clears_armed_gsn_before_unlock() {
let dir = tempfile::tempdir().unwrap();
let map = open(dir.path(), SegConfig::test());
let view = map.core().view();
let key = key_of(1);
let gsn = view.active.next_gsn();
let mut replay = map.lock_for_replay(&[]).unwrap();
let too_big = vec![0; map.config().write_buffer_size + 1];
assert!(
replay
.apply_put(key.as_bytes(), &too_big, tag(gsn))
.is_err()
);
let s = shard_for_hash(
key_hash(key.as_bytes(), map.core().seed),
view.active.shard_count(),
);
assert_eq!(view.active.engine().shards()[s].lock().armed_gsn(), None);
let _held = view.active.lock_owned(&[s]).unwrap();
}
#[test]
fn journal_entry_cap_rejects_before_log_append() {
let dir = tempfile::tempdir().unwrap();
let db = crate::armour::Db::open_test(dir.path().join("db")).unwrap();
let map = Arc::new(open(&dir.path().join("rows"), SegConfig::test()));
crate::sync::lock(&db.collections).push(map.clone());
let before = db.txlog().total_bytes();
let value = "x".repeat(map.config().write_buffer_size);
assert!(
db.tx()
.put(&map, &key_of(1), &value)
.unwrap()
.commit()
.is_err()
);
assert_eq!(db.txlog().total_bytes(), before);
assert_eq!(map.try_get(&key_of(1)).unwrap(), None);
assert_eq!(map.max_entry_bytes(), Some(map.config().write_buffer_size));
}
#[test]
fn physical_domain_missing_write_recovers_with_more_and_fewer_planner_shards() {
for (physical, planner) in [(2, 4), (4, 2)] {
let dir = tempfile::tempdir().unwrap();
let path = dir.path().join("rows");
let initial = open(
&path,
SegConfig {
shard_count: physical,
..SegConfig::test()
},
);
initial.durable_flush().unwrap();
drop(initial);
let cfg = SegConfig {
shard_count: planner,
..SegConfig::test()
};
let map = Arc::new(open(&path, cfg.clone()));
let gen_ = map.core().view().active.clone();
assert_eq!(gen_.shard_count(), physical);
let keys = (0..2000).map(key_of).collect::<Vec<_>>();
let real = |k: &TestKey| shard_for_hash(key_hash(k, map.core().seed), physical);
let planned = |k: &TestKey| map.shard_of(k).unwrap();
let first = keys[0];
let same = *keys
.iter()
.find(|k| {
**k != first
&& real(k) == real(&first)
&& (physical >= planner || planned(k) != planned(&first))
})
.unwrap();
let other = *keys
.iter()
.find(|k| {
real(k) != real(&first)
&& (physical <= planner || planned(k) == planned(&first))
})
.unwrap();
let db = crate::armour::Db::open_test(dir.path().join("db")).unwrap();
crate::sync::lock(&db.collections).push(map.clone());
let failure = {
let _fail = crate::txn::failpoints::ApplyFailGuard::at(0);
db.tx()
.put(&map, &first, &"missing".to_owned())
.unwrap()
.put(&map, &same, &"same-domain".to_owned())
.unwrap()
.put(&map, &other, &"healthy".to_owned())
.unwrap()
.durable()
.commit()
};
match failure {
Err(DbError::TxFailedAfterLog { fenced, .. }) => {
assert_eq!(fenced.len(), 1);
assert_eq!(fenced[0].shard, real(&first));
}
other => panic!("expected physical-domain failure, got {other:?}"),
}
assert_eq!(map.try_get(&first).unwrap(), None);
assert_eq!(map.try_get(&other).unwrap(), Some("healthy".to_owned()));
map.durable_flush().unwrap(); for shard in gen_.engine().shards().iter() {
assert_eq!(shard.lock().armed_gsn(), None);
}
drop(gen_);
drop(map);
drop(db);
let db = crate::armour::Db::open_test(dir.path().join("db")).unwrap();
let map = open(&path, cfg);
crate::txn::replay::replay_pending(&db, &map, None, None).unwrap();
assert_eq!(map.try_get(&first).unwrap(), Some("missing".to_owned()));
assert_eq!(map.try_get(&same).unwrap(), Some("same-domain".to_owned()));
assert_eq!(map.try_get(&other).unwrap(), Some("healthy".to_owned()));
}
}
#[test]
fn cleanup_abort_finish_error_and_panic_after_arm_or_apply_disarm_and_fence() {
bounded_cleanup(move || {
for mode in 0..4 {
let dir = tempfile::tempdir().unwrap();
let map = open(dir.path(), SegConfig::test());
let keys = [key_of(1), key_of(2)];
let mut held = map.lock_shards(&[0, 1]).unwrap();
let tags = held
.reserve(&keys.iter().map(plan).collect::<Vec<_>>())
.unwrap();
held.mark_logged();
if mode == 3 {
held.apply_put(&keys[0], b"applied", tags[0]).unwrap();
}
held.tx_debug_arm(&keys[1], tags[1]).unwrap();
match mode {
0 => held.abort(),
1 => {
let _fail = crate::seg::failpoints::FinishFailGuard::new();
assert!(
held.finish(
&mut Vec::new(),
&mut crate::watch::WatchWakeBatch::default()
)
.is_err()
);
}
2 | 3 => {
assert!(
std::panic::catch_unwind(std::panic::AssertUnwindSafe(move || {
let _held = held;
panic!("logged cleanup unwind");
}))
.is_err()
);
}
_ => unreachable!(),
}
assert!(matches!(
map.put(&keys[1], &"late".to_owned()),
Err(DbError::CollectionFenced { .. })
));
assert!(matches!(map.seal(), Err(DbError::CollectionFenced { .. })));
assert_eq!(map.try_get(&keys[1]).unwrap(), None);
assert_eq!(
map.try_get(&keys[0]).unwrap(),
(mode == 3).then_some("applied".to_owned())
);
let gen_ = map.core().view().active.clone();
assert!(map.core().view().frozen.is_empty());
for shard in gen_.engine().shards().iter() {
assert_eq!(shard.lock().armed_gsn(), None);
}
let _locks = gen_.lock_owned(&[0, 1]).unwrap();
}
});
}
#[test]
fn cleanup_waiting_facade_and_freeze_refuse_after_logged_abort_before_unlock() {
bounded_cleanup(move || {
use std::time::Duration;
let dir = tempfile::tempdir().unwrap();
let map = open(dir.path(), SegConfig::test());
map.put(&key_of(0), &"existing".to_owned()).unwrap();
let gen_ = map.core().view().active.clone();
std::thread::scope(|scope| {
let key = key_of(1);
let mut held = map.lock_shards(&[0, 1]).unwrap();
let tag = held.reserve(&[plan(&key)]).unwrap()[0];
held.mark_logged();
held.tx_debug_arm(&key, tag).unwrap();
let writer_waiting = gen_.observe_index_lock_attempt();
let writer = scope.spawn(|| map.put(&key_of(1), &"refused".to_owned()));
writer_waiting.recv_timeout(Duration::from_secs(5)).unwrap();
let freeze_waiting = gen_.observe_index_lock_attempt();
let freeze = scope.spawn(|| map.seal());
freeze_waiting.recv_timeout(Duration::from_secs(5)).unwrap();
let manifest =
std::fs::read(dir.path().join(crate::seg::manifest::MANIFEST_NAME)).unwrap();
held.abort();
assert!(matches!(
writer.join().unwrap(),
Err(DbError::CollectionFenced { .. })
));
assert!(matches!(
freeze.join().unwrap(),
Err(DbError::CollectionFenced { .. })
));
assert_eq!(
std::fs::read(dir.path().join(crate::seg::manifest::MANIFEST_NAME)).unwrap(),
manifest
);
});
assert_eq!(
map.try_get(&key_of(0)).unwrap(),
Some("existing".to_owned())
);
assert_eq!(map.try_get(&key_of(1)).unwrap(), None);
assert!(!gen_.is_frozen());
assert!(map.core().view().frozen.is_empty());
for shard in gen_.engine().shards().iter() {
assert_eq!(shard.lock().armed_gsn(), None);
}
});
}
#[test]
fn cleanup_concurrent_logged_real_shard_drains_after_other_domain_fences() {
bounded_cleanup(move || {
use std::time::Duration;
let dir = tempfile::tempdir().unwrap();
let initial = open(
dir.path(),
SegConfig {
shard_count: 3,
..SegConfig::test()
},
);
initial.durable_flush().unwrap();
drop(initial);
let map = Arc::new(open(
dir.path(),
SegConfig {
shard_count: 7,
..SegConfig::test()
},
));
let first = key_of(1);
let first_real = overlapping_shards(&[map.shard_of(&first).unwrap()], 7, 3).unwrap();
let other = (2..1000)
.map(key_of)
.find(|k| {
overlapping_shards(&[map.shard_of(k).unwrap()], 7, 3)
.unwrap()
.iter()
.all(|s| !first_real.contains(s))
})
.unwrap();
let mut debt = map.lock_shards(&[map.shard_of(&first).unwrap()]).unwrap();
let tag = debt.reserve(&[plan(&first)]).unwrap()[0];
debt.mark_logged();
debt.tx_debug_arm(&first, tag).unwrap();
let (logged_tx, logged_rx) = std::sync::mpsc::channel();
let (drain_tx, drain_rx) = std::sync::mpsc::channel();
std::thread::scope(|scope| {
let debt = debt; let map = ↦
let drain = scope.spawn(move || {
let mut held = map.lock_shards(&[map.shard_of(&other).unwrap()]).unwrap();
let tag = held.reserve(&[plan(&other)]).unwrap()[0];
held.mark_logged();
logged_tx.send(()).unwrap();
drain_rx.recv_timeout(Duration::from_secs(5)).unwrap();
held.apply_put(&other, b"drained", tag).unwrap();
let mut fenced = Vec::new();
held.finish(&mut fenced, &mut crate::watch::WatchWakeBatch::default())
.unwrap();
assert!(fenced.is_empty());
});
logged_rx.recv_timeout(Duration::from_secs(5)).unwrap();
let mut fenced = Vec::new();
debt.finish(&mut fenced, &mut crate::watch::WatchWakeBatch::default())
.unwrap();
assert_eq!(
fenced,
vec![(
shard_for_hash(key_hash(&first, map.core().seed), 3),
bitcask(tag).unwrap()
)]
);
drain_tx.send(()).unwrap();
drain.join().unwrap();
});
assert_eq!(map.try_get(&other).unwrap(), Some("drained".to_owned()));
assert_eq!(map.try_get(&first).unwrap(), None);
for shard in map.core().view().active.engine().shards().iter() {
assert_eq!(shard.lock().armed_gsn(), None);
}
});
}
#[test]
fn cleanup_finish_error_reports_finish_stage_after_all_applies_without_phantom_debt() {
bounded_cleanup(move || {
let dir = tempfile::tempdir().unwrap();
let db = crate::armour::Db::open_test(dir.path().join("db")).unwrap();
let map = Arc::new(open(&dir.path().join("rows"), SegConfig::test()));
crate::sync::lock(&db.collections).push(map.clone());
let failure = {
let _fail = crate::seg::failpoints::FinishFailGuard::new();
db.tx()
.put(&map, &key_of(1), &"applied".to_owned())
.unwrap()
.durable()
.commit()
};
match failure {
Err(DbError::TxFailedAfterLog { stage, fenced, .. }) => {
assert_eq!(stage, crate::TxFailStage::Finish);
assert!(fenced.is_empty());
}
other => panic!("expected post-log finish error, got {other:?}"),
}
assert_eq!(map.try_get(&key_of(1)).unwrap(), Some("applied".to_owned()));
assert!(map.core().fence_reason().is_none());
map.put(&key_of(2), &"later".to_owned()).unwrap();
for shard in map.core().view().active.engine().shards().iter() {
assert_eq!(shard.lock().armed_gsn(), None);
}
});
}
#[test]
fn cleanup_concurrent_admission_bounds_threshold_overshoot_by_admitted_batches() {
bounded_cleanup(move || {
use std::time::Duration;
let dir = tempfile::tempdir().unwrap();
let db = Arc::new(crate::armour::Db::open_test(dir.path().join("db")).unwrap());
let map = Arc::new(open(
&dir.path().join("rows"),
SegConfig {
seal_max_keys: 2,
sync_batch_size: 100_000,
sync_interval: None,
..SegConfig::test()
},
));
crate::sync::lock(&db.collections).push(map.clone());
let gen_ = map.core().view().active.clone();
let mut keys = [Vec::new(), Vec::new()];
for key in (0..1000).map(key_of) {
let shard = shard_for_hash(key_hash(&key, map.core().seed), 2);
if keys[shard].len() < 3 {
keys[shard].push(key);
}
if keys.iter().all(|v| v.len() == 3) {
break;
}
}
assert!(keys.iter().all(|v| v.len() == 3));
let (admitted_tx, admitted_rx) = std::sync::mpsc::channel();
let (release_a, released_a) = std::sync::mpsc::channel();
let (release_b, released_b) = std::sync::mpsc::channel();
std::thread::scope(|scope| {
let mut handles = Vec::new();
for (keys, released) in keys.into_iter().zip([released_a, released_b]) {
let db = db.clone();
let map = map.clone();
let gen_ = gen_.clone();
let admitted = admitted_tx.clone();
handles.push(scope.spawn(move || {
let mut tx = db
.tx()
.guard(&map, &keys[0], move |_| {
assert!(
!gen_.is_full(),
"both batches must enter admission before apply"
);
admitted.send(()).unwrap();
released.recv_timeout(Duration::from_secs(5)).unwrap();
Ok(())
})
.unwrap();
for key in keys {
tx = tx.put(&map, &key, &"batch".to_owned()).unwrap();
}
tx.durable().commit().unwrap();
}));
}
admitted_rx.recv_timeout(Duration::from_secs(5)).unwrap();
admitted_rx.recv_timeout(Duration::from_secs(5)).unwrap();
assert_eq!(gen_.keys(), 0);
release_a.send(()).unwrap();
release_b.send(()).unwrap();
for handle in handles {
handle.join().unwrap();
}
});
assert!(Arc::ptr_eq(&gen_, &map.core().view().active));
assert_eq!(
gen_.keys(),
6,
"exactly two already-admitted three-write batches"
);
assert_eq!(
gen_.bytes(),
6 * crate::entry::entry_size(size_of::<TestKey>(), 5) as u64
);
assert!(gen_.is_full());
assert!(map.core().view().frozen.is_empty());
});
}
type Seen = Arc<std::sync::Mutex<Vec<(Option<String>, Option<String>)>>>;
struct Recorder(Seen);
impl TypedWriteHook<TestKey, String> for Recorder {
fn on_write(&self, _: &TestKey, old: Option<&String>, new: Option<&String>) {
self.0.lock().unwrap().push((old.cloned(), new.cloned()));
}
}
#[test]
fn replay_fires_on_write_for_the_visible_transition_only() {
let dir = tempfile::tempdir().unwrap();
let seen: Seen = Arc::default();
let map = Arc::new(
SegMap::open_deferred(
dir.path(),
meta(),
SegConfig::test(),
TextCodec,
Recorder(seen.clone()),
)
.unwrap(),
);
crate::access::ManagedOpen::bind_tx_arc(&map);
map.core().put(&key_of(1), b"old").unwrap();
let frozen = map.core().view().active.clone();
let shadowed = frozen.engine().gsn().fetch_add(1, Ordering::Relaxed);
map.seal().unwrap();
map.core().put(&key_of(1), b"young").unwrap();
let fresh = map
.core()
.view()
.active
.engine()
.gsn()
.fetch_add(1, Ordering::Relaxed);
let mut replay = map.lock_for_replay(&[]).unwrap();
assert_eq!(
replay
.apply_put(key_of(1).as_bytes(), b"shadowed", tag(shadowed))
.unwrap(),
0
);
assert_eq!(
replay
.apply_put(key_of(2).as_bytes(), b"new", tag(fresh))
.unwrap(),
1
);
let mut deferred = replay
.finish(
&mut Vec::new(),
&mut crate::watch::WatchWakeBatch::default(),
)
.unwrap();
assert_eq!(deferred.event_count(), 1);
deferred.replay_one(0);
assert_eq!(*seen.lock().unwrap(), vec![(None, Some("new".to_owned()))]);
}
#[test]
fn admission_waits_before_earlier_participants_are_locked() {
use crate::counter_tests::Group;
use std::time::Duration;
let dir = tempfile::tempdir().unwrap();
let db = Arc::new(crate::armour::Db::open_test(dir.path().join("db")).unwrap());
let native = db
.open_var_typed_map::<Group, crate::RapiraCodec, _>(crate::Config::test(), NoHook, &[])
.unwrap();
let wait = Duration::from_millis(1500);
let map = Arc::new(open(
&dir.path().join("rows"),
SegConfig {
seal_max_keys: 1,
max_frozen_generations: 1,
seal_wait_timeout: wait,
..SegConfig::test()
},
));
crate::sync::lock(&db.collections).push(map.clone());
map.put(&key_of(1), &"one".to_owned()).unwrap();
map.seal().unwrap();
map.put(&key_of(2), &"two".to_owned()).unwrap();
assert!(map.core().view().active.is_full());
let row = 7u64.to_be_bytes();
let before = db.txlog().total_bytes();
std::thread::scope(|scope| {
let committer = scope.spawn(|| {
db.tx()
.put(&native, &row, &Group { member_count: 1 })
.unwrap()
.put(&map, &key_of(3), &"three".to_owned())
.unwrap()
.commit()
});
std::thread::sleep(Duration::from_millis(200));
let started = std::time::Instant::now();
native.put(&row, &Group { member_count: 2 }).unwrap();
assert!(started.elapsed() < wait / 2, "{:?}", started.elapsed());
assert!(!committer.is_finished(), "admission must still be waiting");
assert!(matches!(
committer.join().unwrap(),
Err(DbError::SealBackpressure { .. })
));
});
assert_eq!(db.txlog().total_bytes(), before);
assert_eq!(
native.try_get(&row).unwrap(),
Some(Group { member_count: 2 })
);
assert_eq!(map.try_get(&key_of(3)).unwrap(), None);
}
}