use hawdb_core::time::Instant;
use hawdb_core::{
RuntimeCancellationReason, RuntimeIoWaveError, RuntimeIoWavePermit, RuntimeMemoryError,
RuntimeMemoryPermit, RuntimeTaskContext,
};
use hawdb_qos::{
LocalQosBatchPermit, LocalQosScheduler, QosAdmission, QosTelemetrySink, QosUnitEvent,
QosUnitPhase, WorkClass, WorkRequest,
};
use std::error::Error;
use std::fmt::{Display, Formatter};
use std::num::NonZeroUsize;
use std::sync::atomic::{AtomicU64, Ordering};
use std::sync::{Arc, Mutex};
mod buffer;
pub(crate) use buffer::CheckpointBytes;
pub use buffer::CheckpointSharedBytes;
mod values;
pub(crate) use values::{CheckpointSharedValues, CheckpointValues};
mod allocation;
pub(crate) use allocation::{
CheckpointAllocationLookup, CheckpointAllocationOwner, CheckpointAllocationToken,
};
pub const CHECKPOINT_UNITS_PER_ADMISSION: usize = 64;
#[doc(hidden)]
#[derive(Debug, Clone, Default)]
pub struct CheckpointWorkContext {
shared: Arc<WorkShared>,
recorded_failure: Option<Arc<Mutex<Option<CheckpointWorkError>>>>,
}
#[derive(Debug, Clone, Default)]
struct WorkShared {
task: RuntimeTaskContext,
scheduler: Option<LocalQosScheduler>,
units: Option<Arc<UnitAccounting>>,
}
#[derive(Debug, Default)]
struct UnitAccounting {
batch: Option<Arc<UnitBatch>>,
progress: Option<Arc<AtomicU64>>,
}
#[doc(hidden)]
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub struct CheckpointUnitBatchStats {
pub units: u64,
pub finished_units: u64,
pub abandoned_units: u64,
pub admissions: u64,
pub refusals: u64,
pub clock_reads: u64,
}
#[derive(Debug)]
struct UnitBatch {
scheduler: LocalQosScheduler,
state: Mutex<UnitBatchState>,
}
#[derive(Debug, Default)]
struct UnitBatchState {
permit: Option<LocalQosBatchPermit>,
observer: Option<Arc<dyn QosTelemetrySink>>,
started: usize,
active: usize,
failed: bool,
closing: bool,
stats: CheckpointUnitBatchStats,
}
struct StartedUnit {
observer: Option<Arc<dyn QosTelemetrySink>>,
unit_in_batch: usize,
admitted: bool,
}
impl UnitBatch {
fn new(scheduler: LocalQosScheduler) -> Self {
Self {
scheduler,
state: Mutex::new(UnitBatchState::default()),
}
}
fn lock(&self) -> std::sync::MutexGuard<'_, UnitBatchState> {
self.state
.lock()
.unwrap_or_else(|poisoned| poisoned.into_inner())
}
fn start(&self, task: &RuntimeTaskContext) -> Result<StartedUnit, CheckpointWorkError> {
let mut state = self.lock();
let resumed = state.active > 0
|| (state.started < CHECKPOINT_UNITS_PER_ADMISSION
&& !state.closing
&& state
.permit
.as_ref()
.is_some_and(LocalQosBatchPermit::resume));
if !resumed {
let now = Instant::now();
state.stats.clock_reads += 1;
if let Some(permit) = state.permit.take() {
permit.finish_with_outcome_at(!state.failed, now);
}
state.started = 0;
state.failed = false;
state.closing = false;
state.observer = None;
task.check_deadline_at(now)
.map_err(CheckpointWorkError::Stopped)?;
state.stats.admissions += 1;
match self
.scheduler
.try_start_batch_at(WorkRequest::background(WorkClass::Mutation, 1), now)
{
Ok(permit) => state.permit = Some(permit),
Err(admission) => {
state.stats.refusals += 1;
return Err(CheckpointWorkError::Admission(admission));
}
}
state.observer = self
.scheduler
.telemetry_sink()
.filter(|sink| sink.observes_units());
}
state.started += 1;
state.active += 1;
state.stats.units += 1;
Ok(StartedUnit {
observer: state.observer.clone(),
unit_in_batch: state.started,
admitted: !resumed,
})
}
fn end(&self, success: bool) {
let mut state = self.lock();
state.active -= 1;
if success {
state.stats.finished_units += 1;
} else {
state.stats.abandoned_units += 1;
state.failed = true;
}
if state.active == 0 {
if state.failed || state.closing {
Self::release_locked(&mut state);
} else if let Some(permit) = &state.permit {
permit.idle();
}
}
}
fn close(&self, failed: bool) {
let mut state = self.lock();
state.failed |= failed;
if state.active == 0 {
Self::release_locked(&mut state);
} else {
state.closing = true;
}
}
fn release_locked(state: &mut UnitBatchState) {
if let Some(permit) = state.permit.take() {
if !permit.is_reclaimed() {
state.stats.clock_reads += 1;
}
permit.finish_with_outcome(!state.failed);
}
state.started = 0;
state.failed = false;
state.closing = false;
state.observer = None;
}
}
impl Drop for UnitBatch {
fn drop(&mut self) {
let state = self
.state
.get_mut()
.unwrap_or_else(|poisoned| poisoned.into_inner());
if let Some(permit) = state.permit.take() {
permit.finish_with_outcome(!state.failed);
}
}
}
impl CheckpointWorkContext {
#[inline]
pub(crate) fn task_context(&self) -> &RuntimeTaskContext {
&self.shared.task
}
pub fn new(task: RuntimeTaskContext) -> Self {
Self {
shared: Arc::new(WorkShared {
task,
scheduler: None,
units: None,
}),
recorded_failure: None,
}
}
pub fn with_scheduler(mut self, scheduler: LocalQosScheduler) -> Self {
let batch = Some(Arc::new(UnitBatch::new(scheduler.clone())));
let shared = Arc::make_mut(&mut self.shared);
shared.scheduler = Some(scheduler);
shared.set_units(|units| units.batch = batch);
self
}
pub fn release_unit_batch(&self) {
if let Some(batch) = self.batch() {
batch.close(false);
}
}
pub fn unit_batch_stats(&self) -> CheckpointUnitBatchStats {
self.batch()
.map(|batch| batch.lock().stats)
.unwrap_or_default()
}
pub fn with_progress_counter(mut self, progress: Arc<AtomicU64>) -> Self {
Arc::make_mut(&mut self.shared).set_units(|units| units.progress = Some(progress));
self
}
#[inline]
pub(crate) fn is_same(&self, other: &Self) -> bool {
Arc::ptr_eq(&self.shared, &other.shared)
&& match (&self.recorded_failure, &other.recorded_failure) {
(None, None) => true,
(Some(left), Some(right)) => Arc::ptr_eq(left, right),
_ => false,
}
}
#[inline]
pub(crate) fn rebind(&mut self, work: &Self) {
if !self.is_same(work) {
#[cfg(test)]
tests::REBINDS.with(|rebinds| rebinds.set(rebinds.get() + 1));
*self = work.clone();
}
}
#[cfg(test)]
pub(crate) fn fork_handle(&self) -> Self {
Self {
shared: Arc::new((*self.shared).clone()),
recorded_failure: self.recorded_failure.clone(),
}
}
#[inline]
fn batch(&self) -> Option<&Arc<UnitBatch>> {
self.shared.units.as_ref()?.batch.as_ref()
}
pub(crate) fn scheduler(&self) -> Option<LocalQosScheduler> {
self.shared.scheduler.clone()
}
pub(crate) fn memory_ceiling(&self) -> u64 {
self.shared
.task
.memory_reservation()
.map_or(u64::MAX, |reservation| reservation.memory_bytes())
}
#[inline]
pub fn checkpoint(&self) -> Result<(), CheckpointWorkError> {
self.shared
.task
.checkpoint()
.map_err(CheckpointWorkError::Stopped)
.map_err(|error| self.record_failure(error))
}
#[doc(hidden)]
pub fn classify<T, E>(
&self,
operation: impl FnOnce(&Self) -> Result<T, E>,
) -> Result<T, CheckpointOperationError<E>> {
let unit = self.start_unit().map_err(CheckpointOperationError::Work)?;
let recorded = Arc::new(Mutex::new(None));
let mut scoped = self.clone();
scoped.recorded_failure = Some(recorded.clone());
unit.finish_without_progress();
let result = operation(&scoped);
self.release_unit_batch();
let failure = recorded
.lock()
.unwrap_or_else(|poisoned| poisoned.into_inner())
.take();
match failure {
Some(error) => Err(CheckpointOperationError::Work(self.record_failure(error))),
None => result.map_err(CheckpointOperationError::Operation),
}
}
#[cold]
pub(crate) fn record_failure(&self, error: CheckpointWorkError) -> CheckpointWorkError {
if let Some(recorded) = &self.recorded_failure {
let mut first = recorded
.lock()
.unwrap_or_else(|poisoned| poisoned.into_inner());
if first.is_none() {
*first = Some(error.clone());
}
}
error
}
#[inline]
pub(crate) fn start_unit(&self) -> Result<CheckpointWorkUnit, CheckpointWorkError> {
match self.batch() {
None => {
self.checkpoint()?;
Ok(CheckpointWorkUnit {
accounting: self.shared.units.clone(),
observer: None,
})
}
Some(batch) => self.start_batched_unit(batch),
}
}
#[inline(never)]
fn start_batched_unit(
&self,
batch: &Arc<UnitBatch>,
) -> Result<CheckpointWorkUnit, CheckpointWorkError> {
let task = &self.shared.task;
if let Err(reason) = task.check_cancellation() {
batch.close(false);
return Err(self.record_failure(CheckpointWorkError::Stopped(reason)));
}
let StartedUnit {
observer,
unit_in_batch,
admitted,
} = match batch.start(task) {
Ok(started) => started,
Err(error) => return Err(self.record_failure(error)),
};
let unit = CheckpointWorkUnit {
accounting: self.shared.units.clone(),
observer: observer.map(|observer| (observer, unit_in_batch)),
};
if let Some((observer, unit_in_batch)) = &unit.observer {
observer.record_qos_unit(QosUnitEvent {
phase: QosUnitPhase::Started,
class: WorkClass::Mutation,
unit_in_batch: *unit_in_batch,
});
}
if admitted || unit.observer.is_some() {
task.check_cancellation()
.map_err(CheckpointWorkError::Stopped)
.map_err(|error| self.record_failure(error))?;
}
Ok(unit)
}
pub(crate) fn next_input<I: Iterator>(
&self,
input: &mut I,
source_admits: bool,
) -> Result<(Option<I::Item>, CheckpointWorkUnit), CheckpointWorkError> {
self.checkpoint()?;
if source_admits {
let item = input.next();
let unit = self.start_unit()?;
Ok((item, unit))
} else {
let unit = self.start_unit()?;
let item = {
let _wave = self.io_wave()?;
input.next()
};
Ok((item, unit))
}
}
pub(crate) fn io_wave(
&self,
) -> Result<Option<Box<dyn RuntimeIoWavePermit>>, CheckpointWorkError> {
self.shared
.task
.acquire_io_wave(NonZeroUsize::MIN)
.map_err(CheckpointWorkError::Io)
.map_err(|error| self.record_failure(error))
}
pub(crate) fn reserve_memory(
&self,
bytes: usize,
) -> Result<Option<Box<dyn RuntimeMemoryPermit>>, CheckpointWorkError> {
let bytes = u64::try_from(bytes).map_err(|_| {
CheckpointWorkError::Memory(RuntimeMemoryError::ReservationExceeded {
requested_bytes: u64::MAX,
available_bytes: 0,
})
})?;
self.shared
.task
.reserve_working_memory(bytes)
.map_err(CheckpointWorkError::Memory)
.map_err(|error| self.record_failure(error))
}
pub(crate) fn integrity(
&self,
bytes: &[u8],
) -> Result<hawdb_integrity::IntegrityDigest, CheckpointWorkError> {
let mut hasher = hawdb_integrity::IntegrityHasher::new();
for block in bytes.chunks(64 * 1024) {
let unit = self.start_unit()?;
hasher.update(block);
unit.finish();
}
self.checkpoint()?;
Ok(hasher.finish())
}
pub(crate) fn checksum(&self, bytes: &[u8]) -> Result<u64, CheckpointWorkError> {
let mut hasher = hawdb_integrity::Crc32cHasher::new();
for block in bytes.chunks(64 * 1024) {
let unit = self.start_unit()?;
hasher.update(block);
unit.finish();
}
self.checkpoint()?;
Ok(hasher.finish())
}
pub(crate) fn arc_bytes(
&self,
bytes: &[u8],
) -> Result<CheckpointSharedBytes, CheckpointWorkError> {
CheckpointSharedBytes::copy(bytes, self)
}
}
impl WorkShared {
fn set_units(&mut self, edit: impl FnOnce(&mut UnitAccounting)) {
let mut units = UnitAccounting::default();
if let Some(current) = &self.units {
units.batch = current.batch.clone();
units.progress = current.progress.clone();
}
edit(&mut units);
self.units = (units.batch.is_some() || units.progress.is_some()).then(|| Arc::new(units));
}
}
pub(crate) struct CheckpointWorkUnit {
accounting: Option<Arc<UnitAccounting>>,
observer: Option<(Arc<dyn QosTelemetrySink>, usize)>,
}
impl CheckpointWorkUnit {
#[inline]
pub(crate) fn finish(mut self) {
if let Some(progress) = self
.accounting
.as_ref()
.and_then(|units| units.progress.as_ref())
{
progress.fetch_add(1, Ordering::Relaxed);
}
self.end(true);
}
fn finish_without_progress(mut self) {
self.end(true);
}
#[inline]
fn end(&mut self, success: bool) {
if let Some(accounting) = self.accounting.take() {
self.end_accounted(accounting, success);
}
}
#[inline(never)]
fn end_accounted(&mut self, accounting: Arc<UnitAccounting>, success: bool) {
let Some(batch) = &accounting.batch else {
return;
};
batch.end(success);
if let Some((observer, unit_in_batch)) = self.observer.take() {
observer.record_qos_unit(QosUnitEvent {
phase: if success {
QosUnitPhase::Finished
} else {
QosUnitPhase::Abandoned
},
class: WorkClass::Mutation,
unit_in_batch,
});
}
}
}
impl Drop for CheckpointWorkUnit {
#[inline]
fn drop(&mut self) {
self.end(false);
}
}
#[doc(hidden)]
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum CheckpointWorkError {
Stopped(RuntimeCancellationReason),
Admission(QosAdmission),
Io(RuntimeIoWaveError),
Memory(RuntimeMemoryError),
FileDescriptors(hawdb_core::error::FileDescriptorError),
Contended(&'static str),
Allocation { bytes: u64, reason: String },
}
#[doc(hidden)]
#[derive(Debug)]
pub enum CheckpointOperationError<E> {
Work(CheckpointWorkError),
Operation(E),
}
impl<E> From<E> for CheckpointOperationError<E> {
fn from(error: E) -> Self {
Self::Operation(error)
}
}
impl Display for CheckpointWorkError {
fn fmt(&self, formatter: &mut Formatter<'_>) -> std::fmt::Result {
match self {
Self::Stopped(reason) => write!(formatter, "checkpoint build stopped: {reason}"),
Self::Admission(reason) => {
write!(formatter, "checkpoint unit admission deferred: {reason:?}")
}
Self::Io(error) => write!(formatter, "checkpoint build I/O stopped: {error}"),
Self::Memory(error) => {
write!(formatter, "checkpoint memory admission deferred: {error}")
}
Self::FileDescriptors(error) => {
write!(
formatter,
"checkpoint descriptor admission deferred: {error}"
)
}
Self::Contended(resource) => {
write!(formatter, "checkpoint resource is busy: {resource}")
}
Self::Allocation { bytes, reason } => write!(
formatter,
"checkpoint allocation of {bytes} bytes failed: {reason}"
),
}
}
}
impl Error for CheckpointWorkError {
fn source(&self) -> Option<&(dyn Error + 'static)> {
match self {
Self::Stopped(reason) => Some(reason),
Self::Io(error) => Some(error),
Self::Admission(_) => None,
Self::Memory(error) => Some(error),
Self::FileDescriptors(error) => Some(error),
Self::Contended(_) => None,
Self::Allocation { .. } => None,
}
}
}
#[cfg(test)]
pub(crate) mod tests {
use super::*;
use hawdb_core::{RuntimeCancellationToken, RuntimeIoWaveController};
use hawdb_qos::{QosTelemetryEvent, QosTelemetryOutcome, QosTelemetryPhase, QosTelemetrySink};
use std::cell::Cell;
use std::sync::atomic::{AtomicUsize, Ordering};
use std::sync::Arc;
thread_local! {
pub(crate) static REBINDS: Cell<u64> = const { Cell::new(0) };
}
pub(crate) fn rebinds() -> u64 {
REBINDS.with(Cell::get)
}
#[test]
fn progress_counts_completed_work_but_not_classification_denial_or_abandonment() {
let progress = Arc::new(AtomicU64::new(0));
let scheduler = LocalQosScheduler::new(hawdb_qos::LocalQosPolicy {
max_background_operations: Some(1),
max_total_background_operations: Some(1),
..Default::default()
});
let work = CheckpointWorkContext::default()
.with_scheduler(scheduler.clone())
.with_progress_counter(progress.clone());
work.classify(|_| Ok::<_, ()>(())).unwrap();
assert_eq!(progress.load(Ordering::Relaxed), 0);
drop(work.start_unit().unwrap());
assert_eq!(progress.load(Ordering::Relaxed), 0);
let occupied = scheduler
.try_start(WorkRequest::background(WorkClass::Mutation, 1))
.unwrap();
assert!(matches!(
work.start_unit(),
Err(CheckpointWorkError::Admission(_))
));
assert_eq!(progress.load(Ordering::Relaxed), 0);
drop(occupied);
work.start_unit().unwrap().finish();
assert_eq!(progress.load(Ordering::Relaxed), 1);
assert_eq!(Arc::strong_count(&progress), 2);
drop(work);
assert_eq!(Arc::strong_count(&progress), 1);
}
#[derive(Debug, Default)]
pub(crate) struct CheckpointWorkProbe {
pub(crate) cancellation: RuntimeCancellationToken,
pub(crate) completed: AtomicUsize,
pub(crate) started: AtomicUsize,
pub(crate) admissions: AtomicUsize,
pub(crate) active_units: AtomicUsize,
pub(crate) peak_units: AtomicUsize,
pub(crate) io_waves: AtomicUsize,
pub(crate) active_io: Arc<AtomicUsize>,
pub(crate) cancel_after: AtomicUsize,
pub(crate) cancel_on_io_wave: AtomicUsize,
}
impl CheckpointWorkProbe {
pub(crate) fn context(
self: &Arc<Self>,
scheduler: LocalQosScheduler,
) -> CheckpointWorkContext {
scheduler.set_telemetry_sink(Some(self.clone()));
let task = RuntimeTaskContext::without_deadline(self.cancellation.clone())
.with_io_wave_controller(self.clone());
CheckpointWorkContext::new(task).with_scheduler(scheduler)
}
pub(crate) fn assert_released(&self, scheduler: &LocalQosScheduler) {
assert_eq!(
self.started.load(Ordering::SeqCst),
self.completed.load(Ordering::SeqCst),
"a checkpoint unit is still active"
);
assert_eq!(self.active_io.load(Ordering::SeqCst), 0);
assert_eq!(scheduler.state().running_background_operations, 0);
}
}
impl QosTelemetrySink for CheckpointWorkProbe {
fn record_qos(&self, event: QosTelemetryEvent) {
assert_eq!(event.estimated_operations, 1);
match (event.phase, event.outcome) {
(QosTelemetryPhase::Admission, QosTelemetryOutcome::Admitted) => {
self.admissions.fetch_add(1, Ordering::SeqCst);
let active = self.active_units.fetch_add(1, Ordering::SeqCst) + 1;
self.peak_units.fetch_max(active, Ordering::SeqCst);
}
(QosTelemetryPhase::Completion, _) => {
let _ = self.active_units.try_update(
Ordering::SeqCst,
Ordering::SeqCst,
|active| active.checked_sub(1),
);
}
_ => {}
}
}
fn observes_units(&self) -> bool {
true
}
fn record_qos_unit(&self, event: QosUnitEvent) {
assert!(event.unit_in_batch >= 1);
if event.phase == QosUnitPhase::Started {
self.started.fetch_add(1, Ordering::SeqCst);
return;
}
let completed = self.completed.fetch_add(1, Ordering::SeqCst) + 1;
let limit = self.cancel_after.load(Ordering::SeqCst);
if limit > 0 && completed >= limit {
self.cancellation.cancel();
}
}
}
#[derive(Debug)]
struct IoLease(Arc<AtomicUsize>);
impl Drop for IoLease {
fn drop(&mut self) {
assert_eq!(self.0.fetch_sub(1, Ordering::SeqCst), 1);
}
}
impl RuntimeIoWaveController for CheckpointWorkProbe {
fn acquire(
&self,
slots: NonZeroUsize,
task: &RuntimeTaskContext,
) -> Result<Box<dyn RuntimeIoWavePermit>, RuntimeIoWaveError> {
assert_eq!(slots, NonZeroUsize::MIN);
task.checkpoint().map_err(RuntimeIoWaveError::Stopped)?;
let wave = self.io_waves.fetch_add(1, Ordering::SeqCst) + 1;
if self.cancel_on_io_wave.load(Ordering::SeqCst) == wave {
self.cancellation.cancel();
task.checkpoint().map_err(RuntimeIoWaveError::Stopped)?;
}
assert_eq!(
self.active_io.fetch_add(1, Ordering::SeqCst),
0,
"a builder must release its wave before another builder acquires one"
);
Ok(Box::new(IoLease(self.active_io.clone())))
}
fn try_acquire(
&self,
slots: NonZeroUsize,
task: &RuntimeTaskContext,
) -> Result<Option<Box<dyn RuntimeIoWavePermit>>, RuntimeIoWaveError> {
task.checkpoint().map_err(RuntimeIoWaveError::Stopped)?;
if self.active_io.load(Ordering::SeqCst) > 0 {
Ok(None)
} else {
self.acquire(slots, task).map(Some)
}
}
}
}
#[cfg(test)]
mod batch_tests;
mod decode;
pub(crate) use decode::{
allocation as checkpoint_decode_allocation, CheckpointDecodeContext, CheckpointVectorPush,
};
mod text;
#[doc(hidden)]
pub use text::CheckpointText;
pub(crate) fn decode_vector(
encoded: Vec<u8>,
work: Option<&CheckpointWorkContext>,
) -> hawdb_core::Result<hawdb_core::Vector> {
hawdb_core::Vector::from_encoded_with_work(
encoded,
|| vector_unit(work),
|unit| finish_vector_unit(unit, work),
|error| hawdb_core::HawDBError::Storage(error.to_string()),
)
}
pub(crate) fn validate_vector<'a>(
encoded: &'a [u8],
work: Option<&CheckpointWorkContext>,
) -> hawdb_core::Result<hawdb_core::VectorRef<'a>> {
hawdb_core::VectorRef::from_encoded_with_work(
encoded,
|| vector_unit(work),
|unit| finish_vector_unit(unit, work),
|error| hawdb_core::HawDBError::Storage(error.to_string()),
)
}
fn vector_unit(
work: Option<&CheckpointWorkContext>,
) -> hawdb_core::Result<Option<CheckpointWorkUnit>> {
work.map(CheckpointWorkContext::start_unit)
.transpose()
.map_err(hawdb_core::HawDBError::from_storage_error)
}
fn finish_vector_unit(
unit: Option<CheckpointWorkUnit>,
work: Option<&CheckpointWorkContext>,
) -> hawdb_core::Result<()> {
if let Some(unit) = unit {
unit.finish();
}
if let Some(work) = work {
work.checkpoint()
.map_err(hawdb_core::HawDBError::from_storage_error)?;
}
Ok(())
}