use super::{LogRecord, TxID}; use crate::io::Completion; use crate::storage::wal::TursoRwLock; #[cfg(any(test, feature = "test_helper"))] use crate::sync::atomic::AtomicUsize; use crate::sync::atomic::{AtomicBool, Ordering}; use crate::sync::Arc; use crate::sync::Mutex; use crate::SyncMode; use rustc_hash::FxHashSet as HashSet; use std::collections::{BTreeMap, VecDeque}; #[derive(Debug)] pub(crate) struct QueuedCommit { pub ticket: u64, pub tx_id: TxID, pub log_record: LogRecord, pub sync_mode: SyncMode, } #[derive(Debug)] pub(crate) struct GroupBatch { pub rest: VecDeque, pub writing: QueuedCommit, pub advanced_through: Option, } impl GroupBatch { pub fn from_lead(writing: QueuedCommit, rest: VecDeque) -> Self { Self { writing, rest, advanced_through: None, } } } #[derive(Debug)] struct GroupState { next_ticket: u64, durable_through: u64, written_through: u64, full_sync_written_through: u64, pending: VecDeque, /// Tickets whose in-flight `log_tx` was discarded before the offset was /// advanced. The waiter rebuilds its log record instead of hanging. retry: HashSet, /// Tx currently inside `log_tx`, before the offset advanced. issued: Option, /// Waiters that dropped after `log_tx`. The leader finishes or rolls them /// back. They must not roll back themselves. abandoned: HashSet, /// Waiters asleep until the leader makes their ticket durable, asks /// them to retry, or releases the commit lock. parked: BTreeMap, } pub(crate) enum GroupWork { Lead { writing: QueuedCommit, rest: VecDeque, }, SyncPrefix, None, } #[derive(Debug)] pub(crate) struct CommitCoordinator { pub pager_commit_lock: Arc, group_commit_enabled: AtomicBool, group: Mutex, #[cfg(any(test, feature = "test_helper"))] last_group_size: AtomicUsize, #[cfg(any(test, feature = "test_helper"))] park_calls: AtomicUsize, } impl CommitCoordinator { pub(crate) fn new() -> Self { Self { pager_commit_lock: Arc::new(TursoRwLock::new()), group_commit_enabled: AtomicBool::new(true), group: Mutex::new(GroupState { next_ticket: 0, durable_through: 0, written_through: 0, full_sync_written_through: 0, pending: VecDeque::new(), retry: HashSet::default(), issued: None, abandoned: HashSet::default(), parked: BTreeMap::new(), }), #[cfg(any(test, feature = "test_helper"))] last_group_size: AtomicUsize::new(0), #[cfg(any(test, feature = "test_helper"))] park_calls: AtomicUsize::new(0), } } /// The completion a waiter sleeps on while another transaction holds the /// commit lock. Returns a plain yield when the waiter can already make /// progress, so the lock check and the registration happen under one /// lock and a release cannot slip in between them. pub(crate) fn park(&self, ticket: u64) -> Completion { #[cfg(any(test, feature = "test_helper"))] self.park_calls.fetch_add(1, Ordering::Relaxed); let mut group = self.group.lock(); let can_progress = group.durable_through >= ticket || group.retry.contains(&ticket) || !self.pager_commit_lock.is_write_locked(); if can_progress { return Completion::new_yield(); } group .parked .entry(ticket) .or_insert_with(Completion::new_wait) .clone() } pub(crate) fn unlock_pager_commit_lock(&self) { self.pager_commit_lock.unlock(); let woken = { let mut group = self.group.lock(); take_parked_through(&mut group, u64::MAX) }; wake(woken); } pub(crate) fn group_commit_enabled(&self) -> bool { self.group_commit_enabled.load(Ordering::Acquire) } pub(crate) fn set_group_commit_enabled(&self, enabled: bool) { self.group_commit_enabled.store(enabled, Ordering::Release); } pub(crate) fn enqueue(&self, tx_id: TxID, log_record: LogRecord, sync_mode: SyncMode) -> u64 { let mut group = self.group.lock(); group.next_ticket += 1; let ticket = group.next_ticket; group.pending.push_back(QueuedCommit { ticket, tx_id, log_record, sync_mode, }); ticket } #[cfg(test)] pub(crate) fn take_pending(&self) -> VecDeque { let mut group = self.group.lock(); let batch = std::mem::take(&mut group.pending); #[cfg(test)] if !batch.is_empty() { self.last_group_size.store(batch.len(), Ordering::Release); } batch } pub(crate) fn take_work(&self) -> GroupWork { let mut group = self.group.lock(); if !group.retry.is_empty() { return if group.written_through > group.durable_through { GroupWork::SyncPrefix } else { GroupWork::None }; } match group.pending.pop_front() { Some(writing) => { let rest = std::mem::take(&mut group.pending); #[cfg(any(test, feature = "test_helper"))] { self.last_group_size .store(rest.len() + 1, Ordering::Release); } GroupWork::Lead { writing, rest } } None if group.written_through > group.durable_through => GroupWork::SyncPrefix, None => GroupWork::None, } } pub(crate) fn requeue(&self, records: impl DoubleEndedIterator) { let mut group = self.group.lock(); for entry in records.rev() { group.pending.push_front(entry); } } pub(crate) fn mark_durable(&self, ticket: u64) { let woken = { let mut group = self.group.lock(); let ticket = dense_prefix_cap(ticket, group.written_through, &group.retry); group.durable_through = group.durable_through.max(ticket); let durable_through = group.durable_through; take_parked_through(&mut group, durable_through) }; wake(woken); } pub(crate) fn durable_through(&self) -> u64 { self.group.lock().durable_through } pub(crate) fn note_written(&self, ticket: u64) { let mut group = self.group.lock(); if group.retry.iter().any(|&hole| hole <= ticket) { return; } group.written_through = group.written_through.max(ticket); } pub(crate) fn note_full_sync_written(&self, ticket: u64) { let mut group = self.group.lock(); group.full_sync_written_through = group.full_sync_written_through.max(ticket); } pub(crate) fn needs_full_sync(&self) -> bool { let group = self.group.lock(); group.full_sync_written_through > group.durable_through } pub(crate) fn written_through(&self) -> u64 { self.group.lock().written_through } /// Removes `ticket` from the queue. Returns whether it was still waiting. pub(crate) fn drop_pending(&self, ticket: u64) -> bool { let mut group = self.group.lock(); group.retry.remove(&ticket); group.parked.remove(&ticket); if let Some(index) = group .pending .iter() .position(|queued| queued.ticket == ticket) { group.pending.remove(index); true } else { false } } pub(crate) fn request_retry(&self, ticket: u64) { let woken = { let mut group = self.group.lock(); group.retry.insert(ticket); if group.written_through >= ticket { group.written_through = ticket.saturating_sub(1); } if group.durable_through >= ticket { group.durable_through = ticket.saturating_sub(1); } group.parked.remove(&ticket) }; wake(woken); } pub(crate) fn take_retry(&self, ticket: u64) -> bool { self.group.lock().retry.remove(&ticket) } pub(crate) fn note_write_issued(&self, tx_id: TxID) { self.group.lock().issued = Some(tx_id); } pub(crate) fn clear_issued(&self) { self.group.lock().issued = None; } pub(crate) fn abandon_if_issued(&self, tx_id: TxID) -> bool { let mut group = self.group.lock(); if group.issued == Some(tx_id) { group.abandoned.insert(tx_id); true } else { false } } pub(crate) fn is_abandoned(&self, tx_id: TxID) -> bool { self.group.lock().abandoned.contains(&tx_id) } pub(crate) fn take_abandoned(&self, tx_id: TxID) -> bool { self.group.lock().abandoned.remove(&tx_id) } #[cfg(any(test, feature = "test_helper"))] pub(crate) fn last_group_size(&self) -> usize { self.last_group_size.load(Ordering::Acquire) } #[cfg(any(test, feature = "test_helper"))] pub(crate) fn parked_tickets(&self) -> Vec { self.group.lock().parked.keys().copied().collect() } #[cfg(any(test, feature = "test_helper"))] pub(crate) fn park_calls(&self) -> usize { self.park_calls.load(Ordering::Relaxed) } } fn take_parked_through(group: &mut GroupState, through: u64) -> Vec { let keep = group.parked.split_off(&through.saturating_add(1)); std::mem::replace(&mut group.parked, keep) .into_values() .collect() } fn wake(parked: impl IntoIterator) { for completion in parked { completion.complete(0); } } fn dense_prefix_cap(ticket: u64, written_through: u64, retry: &HashSet) -> u64 { let mut cap = ticket.min(written_through); if let Some(hole) = retry.iter().copied().filter(|&t| t <= cap).min() { cap = hole.saturating_sub(1); } cap }