use crate::config::FullPolicy;
use crate::error::{DurabilityOutcome, ErrorKind};
use bytes::Bytes;
use std::fmt;
use std::ops::Deref;
use std::time::Duration;
#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
#[repr(transparent)]
pub struct StreamId(u64);
impl StreamId {
#[must_use]
pub const fn new(value: u64) -> Self {
Self(value)
}
#[must_use]
pub const fn get(self) -> u64 {
self.0
}
}
impl From<u64> for StreamId {
fn from(value: u64) -> Self {
Self::new(value)
}
}
impl fmt::Display for StreamId {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
self.0.fmt(formatter)
}
}
#[derive(Clone, Copy, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub(crate) struct RootId([u8; 16]);
impl RootId {
pub(crate) const LEN: usize = 16;
pub(crate) fn random() -> std::io::Result<Self> {
let mut bytes = [0_u8; Self::LEN];
getrandom::fill(&mut bytes).map_err(|error| std::io::Error::other(error.to_string()))?;
Ok(Self(bytes))
}
pub(crate) const fn from_bytes(bytes: [u8; Self::LEN]) -> Self {
Self(bytes)
}
pub(crate) const fn to_bytes(self) -> [u8; Self::LEN] {
self.0
}
}
impl fmt::Debug for RootId {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
formatter.write_str("RootId(")?;
write_hex(formatter, &self.0)?;
formatter.write_str(")")
}
}
#[derive(Clone, Copy, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub struct RecordId([u8; Self::BYTE_LEN]);
impl RecordId {
pub const BYTE_LEN: usize = 32;
#[must_use]
pub const fn from_bytes(bytes: [u8; Self::BYTE_LEN]) -> Self {
Self(bytes)
}
#[must_use]
pub const fn to_bytes(self) -> [u8; Self::BYTE_LEN] {
self.0
}
#[must_use]
pub const fn as_bytes(&self) -> &[u8; Self::BYTE_LEN] {
&self.0
}
pub(crate) fn from_parts(root_id: RootId, stream_id: StreamId, sequence: u64) -> Self {
let mut bytes = [0_u8; Self::BYTE_LEN];
bytes[..16].copy_from_slice(&root_id.to_bytes());
bytes[16..24].copy_from_slice(&stream_id.get().to_le_bytes());
bytes[24..32].copy_from_slice(&sequence.to_le_bytes());
Self(bytes)
}
pub(crate) fn root_id(self) -> RootId {
let mut bytes = [0_u8; RootId::LEN];
bytes.copy_from_slice(&self.0[..16]);
RootId::from_bytes(bytes)
}
pub(crate) fn stream_id(self) -> StreamId {
let mut bytes = [0_u8; 8];
bytes.copy_from_slice(&self.0[16..24]);
StreamId::new(u64::from_le_bytes(bytes))
}
pub(crate) fn sequence(self) -> u64 {
let mut bytes = [0_u8; 8];
bytes.copy_from_slice(&self.0[24..32]);
u64::from_le_bytes(bytes)
}
}
impl fmt::Debug for RecordId {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
formatter.write_str("RecordId(")?;
write_hex(formatter, &self.0)?;
formatter.write_str(")")
}
}
impl fmt::Display for RecordId {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
write_hex(formatter, &self.0)
}
}
fn write_hex(formatter: &mut fmt::Formatter<'_>, bytes: &[u8]) -> fmt::Result {
for byte in bytes {
write!(formatter, "{byte:02x}")?;
}
Ok(())
}
#[derive(Clone, Debug, Default, Eq, PartialEq)]
pub struct Record {
pub metadata: Bytes,
pub payload: Bytes,
}
impl Record {
#[must_use]
pub fn new(payload: impl Into<Bytes>) -> Self {
Self {
metadata: Bytes::new(),
payload: payload.into(),
}
}
#[must_use]
pub fn with_metadata(mut self, metadata: impl Into<Bytes>) -> Self {
self.metadata = metadata.into();
self
}
}
#[derive(Clone, Debug, Eq, PartialEq)]
#[non_exhaustive]
pub struct PendingRecord {
pub id: RecordId,
pub metadata: Bytes,
pub payload: Bytes,
}
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct ReadLimits {
pub max_records: usize,
pub max_bytes: u64,
}
impl ReadLimits {
#[must_use]
pub const fn new(max_records: usize, max_bytes: u64) -> Self {
Self {
max_records,
max_bytes,
}
}
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct PendingSnapshot(Vec<PendingRecord>);
impl PendingSnapshot {
pub(crate) fn new(records: Vec<PendingRecord>) -> Self {
debug_assert!(!records.is_empty());
Self(records)
}
#[must_use]
pub fn len(&self) -> usize {
self.0.len()
}
#[must_use]
pub fn is_empty(&self) -> bool {
self.0.is_empty()
}
#[must_use]
pub fn records(&self) -> &[PendingRecord] {
&self.0
}
}
impl Deref for PendingSnapshot {
type Target = [PendingRecord];
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl IntoIterator for PendingSnapshot {
type Item = PendingRecord;
type IntoIter = std::vec::IntoIter<PendingRecord>;
fn into_iter(self) -> Self::IntoIter {
self.0.into_iter()
}
}
impl<'a> IntoIterator for &'a PendingSnapshot {
type Item = &'a PendingRecord;
type IntoIter = std::slice::Iter<'a, PendingRecord>;
fn into_iter(self) -> Self::IntoIter {
self.0.iter()
}
}
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
#[non_exhaustive]
pub struct DurationStats {
pub observations: u64,
pub total: Duration,
pub max: Duration,
}
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
#[non_exhaustive]
pub struct StorageStats {
pub configured_capacity_bytes: Option<u64>,
pub full_policy: Option<FullPolicy>,
pub durable_streams: u64,
pub pending_records: u64,
pub pending_payload_bytes: u64,
pub actual_file_bytes: u64,
pub maintenance_headroom_bytes: u64,
pub data_admissible_bytes: Option<u64>,
pub live_segments: u64,
pub sealed_segments: u64,
pub reclaimable_segments: u64,
pub reclaimable_bytes: u64,
}
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
#[non_exhaustive]
pub struct WaitStats {
pub current: usize,
pub waits: u64,
pub elapsed: DurationStats,
}
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
#[non_exhaustive]
pub struct StorageJobStats {
pub append: DurationStats,
pub read: DurationStats,
pub release: DurationStats,
pub reclaim: DurationStats,
pub segment_rollover: DurationStats,
}
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
#[non_exhaustive]
pub struct PressureStats {
pub command_queue_capacity: usize,
pub queue_depth: usize,
pub active_storage_jobs: usize,
pub admitted_commands: u64,
pub reactor_dispatch_wait: DurationStats,
pub storage_job_elapsed: DurationStats,
pub storage_jobs: StorageJobStats,
pub queue_wait: WaitStats,
pub readiness_wait: WaitStats,
pub capacity_wait: WaitStats,
}
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
#[non_exhaustive]
pub struct OperationCounters {
pub started: u64,
pub succeeded: u64,
pub failed: u64,
pub cancelled: u64,
pub records: u64,
pub payload_bytes: u64,
pub elapsed: DurationStats,
}
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
#[non_exhaustive]
pub struct OperationStats {
pub append: OperationCounters,
pub read: OperationCounters,
pub release: OperationCounters,
pub reclaim: OperationCounters,
}
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
#[non_exhaustive]
pub struct CommitStats {
pub append_groups: u64,
pub append_units: u64,
pub append_records: u64,
pub append_encoded_bytes: u64,
pub max_append_units: u64,
pub max_append_encoded_bytes: u64,
pub release_groups: u64,
pub release_units: u64,
pub release_records: u64,
pub release_encoded_bytes: u64,
pub max_release_units: u64,
pub max_release_encoded_bytes: u64,
}
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
#[non_exhaustive]
pub struct MaintenanceStats {
pub automatic_reclaim_passes: u64,
pub explicit_reclaim_passes: u64,
pub size_rollovers: u64,
pub age_rollovers: u64,
pub reclaim_rollovers: u64,
pub reclaimed_segments: u64,
pub reclaimed_bytes: u64,
pub manifest_compactions: u64,
}
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
#[non_exhaustive]
pub struct RecoveryStats {
pub manifest_frames_scanned: u64,
pub segments_scanned: u64,
pub epochs_scanned: u64,
pub records_scanned: u64,
pub repaired_active_tails: u64,
pub repaired_manifest_tails: u64,
pub completed_segment_seals: u64,
pub completed_segment_deletions: u64,
pub removed_temporary_files: u64,
pub elapsed: Duration,
}
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
#[non_exhaustive]
pub struct RootStats {
pub detailed_timings: bool,
pub storage: StorageStats,
pub pressure: PressureStats,
pub operations: OperationStats,
pub commits: CommitStats,
pub maintenance: MaintenanceStats,
pub recovery: RecoveryStats,
}
#[derive(Clone, Copy, Debug, Default, Eq, Hash, PartialEq)]
#[non_exhaustive]
pub enum RootState {
#[default]
Running,
ShuttingDown,
Poisoned,
Closed,
}
impl RootState {
#[must_use]
pub const fn as_str(self) -> &'static str {
match self {
Self::Running => "running",
Self::ShuttingDown => "shutting_down",
Self::Poisoned => "poisoned",
Self::Closed => "closed",
}
}
}
impl fmt::Display for RootState {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
formatter.write_str(self.as_str())
}
}
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
#[non_exhaustive]
pub enum OperationKind {
Append,
Read,
Release,
Reclaim,
SegmentRollover,
StatePublication,
Reactor,
}
impl OperationKind {
#[must_use]
pub const fn as_str(self) -> &'static str {
match self {
Self::Append => "append",
Self::Read => "read",
Self::Release => "release",
Self::Reclaim => "reclaim",
Self::SegmentRollover => "segment_rollover",
Self::StatePublication => "state_publication",
Self::Reactor => "reactor",
}
}
}
impl fmt::Display for OperationKind {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
formatter.write_str(self.as_str())
}
}
#[derive(Clone, Debug, Eq, PartialEq)]
#[non_exhaustive]
pub struct FailureInfo {
pub operation: OperationKind,
pub error_kind: ErrorKind,
pub durability_outcome: DurabilityOutcome,
pub message: String,
}
#[derive(Clone, Debug, Default, Eq, PartialEq)]
#[non_exhaustive]
pub struct RootHealth {
pub generation: u64,
pub state: RootState,
pub failure: Option<FailureInfo>,
}
#[derive(Clone, Debug, Default, Eq, PartialEq)]
#[non_exhaustive]
pub struct StreamStats {
pub durable_known: bool,
pub pending_records: u64,
pub pending_payload_bytes: u64,
}
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
#[non_exhaustive]
pub struct ReclaimReport {
pub segments: u64,
pub bytes: u64,
}
impl ReclaimReport {
#[must_use]
pub const fn is_empty(self) -> bool {
self.segments == 0 && self.bytes == 0
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn record_id_has_fixed_root_stream_sequence_encoding() {
let root = RootId::from_bytes([0x11; 16]);
let stream = StreamId::new(0x0203_0405_0607_0809);
let id = RecordId::from_parts(root, stream, 0x0a0b_0c0d_0e0f_1011);
assert_eq!(&id.as_bytes()[..16], &[0x11; 16]);
assert_eq!(id.root_id(), root);
assert_eq!(id.stream_id(), stream);
assert_eq!(id.sequence(), 0x0a0b_0c0d_0e0f_1011);
assert_eq!(RecordId::from_bytes(id.to_bytes()), id);
}
}