1use crate::config::FullPolicy;
2use crate::error::{DurabilityOutcome, ErrorKind};
3use bytes::Bytes;
4use std::fmt;
5use std::ops::Deref;
6use std::time::Duration;
7
8#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
10#[repr(transparent)]
11pub struct StreamId(u64);
12
13impl StreamId {
14 #[must_use]
16 pub const fn new(value: u64) -> Self {
17 Self(value)
18 }
19
20 #[must_use]
22 pub const fn get(self) -> u64 {
23 self.0
24 }
25}
26
27impl From<u64> for StreamId {
28 fn from(value: u64) -> Self {
29 Self::new(value)
30 }
31}
32
33impl fmt::Display for StreamId {
34 fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
35 self.0.fmt(formatter)
36 }
37}
38
39#[derive(Clone, Copy, Eq, Hash, Ord, PartialEq, PartialOrd)]
41pub(crate) struct RootId([u8; 16]);
42
43impl RootId {
44 pub(crate) const LEN: usize = 16;
45
46 pub(crate) fn random() -> std::io::Result<Self> {
47 let mut bytes = [0_u8; Self::LEN];
48 getrandom::fill(&mut bytes).map_err(|error| std::io::Error::other(error.to_string()))?;
49 Ok(Self(bytes))
50 }
51
52 pub(crate) const fn from_bytes(bytes: [u8; Self::LEN]) -> Self {
53 Self(bytes)
54 }
55
56 pub(crate) const fn to_bytes(self) -> [u8; Self::LEN] {
57 self.0
58 }
59}
60
61impl fmt::Debug for RootId {
62 fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
63 formatter.write_str("RootId(")?;
64 write_hex(formatter, &self.0)?;
65 formatter.write_str(")")
66 }
67}
68
69#[derive(Clone, Copy, Eq, Hash, Ord, PartialEq, PartialOrd)]
71pub struct RecordId([u8; Self::BYTE_LEN]);
72
73impl RecordId {
74 pub const BYTE_LEN: usize = 32;
76
77 #[must_use]
79 pub const fn from_bytes(bytes: [u8; Self::BYTE_LEN]) -> Self {
80 Self(bytes)
81 }
82
83 #[must_use]
85 pub const fn to_bytes(self) -> [u8; Self::BYTE_LEN] {
86 self.0
87 }
88
89 #[must_use]
91 pub const fn as_bytes(&self) -> &[u8; Self::BYTE_LEN] {
92 &self.0
93 }
94
95 pub(crate) fn from_parts(root_id: RootId, stream_id: StreamId, sequence: u64) -> Self {
96 let mut bytes = [0_u8; Self::BYTE_LEN];
97 bytes[..16].copy_from_slice(&root_id.to_bytes());
98 bytes[16..24].copy_from_slice(&stream_id.get().to_le_bytes());
99 bytes[24..32].copy_from_slice(&sequence.to_le_bytes());
100 Self(bytes)
101 }
102
103 pub(crate) fn root_id(self) -> RootId {
104 let mut bytes = [0_u8; RootId::LEN];
105 bytes.copy_from_slice(&self.0[..16]);
106 RootId::from_bytes(bytes)
107 }
108
109 pub(crate) fn stream_id(self) -> StreamId {
110 let mut bytes = [0_u8; 8];
111 bytes.copy_from_slice(&self.0[16..24]);
112 StreamId::new(u64::from_le_bytes(bytes))
113 }
114
115 pub(crate) fn sequence(self) -> u64 {
116 let mut bytes = [0_u8; 8];
117 bytes.copy_from_slice(&self.0[24..32]);
118 u64::from_le_bytes(bytes)
119 }
120}
121
122impl fmt::Debug for RecordId {
123 fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
124 formatter.write_str("RecordId(")?;
125 write_hex(formatter, &self.0)?;
126 formatter.write_str(")")
127 }
128}
129
130impl fmt::Display for RecordId {
131 fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
132 write_hex(formatter, &self.0)
133 }
134}
135
136fn write_hex(formatter: &mut fmt::Formatter<'_>, bytes: &[u8]) -> fmt::Result {
137 for byte in bytes {
138 write!(formatter, "{byte:02x}")?;
139 }
140 Ok(())
141}
142
143#[derive(Clone, Debug, Default, Eq, PartialEq)]
145pub struct Record {
146 pub metadata: Bytes,
148 pub payload: Bytes,
150}
151
152impl Record {
153 #[must_use]
155 pub fn new(payload: impl Into<Bytes>) -> Self {
156 Self {
157 metadata: Bytes::new(),
158 payload: payload.into(),
159 }
160 }
161
162 #[must_use]
164 pub fn with_metadata(mut self, metadata: impl Into<Bytes>) -> Self {
165 self.metadata = metadata.into();
166 self
167 }
168}
169
170#[derive(Clone, Debug, Eq, PartialEq)]
172#[non_exhaustive]
173pub struct PendingRecord {
174 pub id: RecordId,
176 pub metadata: Bytes,
178 pub payload: Bytes,
180}
181
182#[derive(Clone, Copy, Debug, Eq, PartialEq)]
184pub struct ReadLimits {
185 pub max_records: usize,
187 pub max_bytes: u64,
189}
190
191impl ReadLimits {
192 #[must_use]
194 pub const fn new(max_records: usize, max_bytes: u64) -> Self {
195 Self {
196 max_records,
197 max_bytes,
198 }
199 }
200}
201
202#[derive(Clone, Debug, Eq, PartialEq)]
204pub struct PendingSnapshot(Vec<PendingRecord>);
205
206impl PendingSnapshot {
207 pub(crate) fn new(records: Vec<PendingRecord>) -> Self {
208 debug_assert!(!records.is_empty());
209 Self(records)
210 }
211
212 #[must_use]
214 pub fn len(&self) -> usize {
215 self.0.len()
216 }
217
218 #[must_use]
223 pub fn is_empty(&self) -> bool {
224 self.0.is_empty()
225 }
226
227 #[must_use]
229 pub fn records(&self) -> &[PendingRecord] {
230 &self.0
231 }
232}
233
234impl Deref for PendingSnapshot {
235 type Target = [PendingRecord];
236
237 fn deref(&self) -> &Self::Target {
238 &self.0
239 }
240}
241
242impl IntoIterator for PendingSnapshot {
243 type Item = PendingRecord;
244 type IntoIter = std::vec::IntoIter<PendingRecord>;
245
246 fn into_iter(self) -> Self::IntoIter {
247 self.0.into_iter()
248 }
249}
250
251impl<'a> IntoIterator for &'a PendingSnapshot {
252 type Item = &'a PendingRecord;
253 type IntoIter = std::slice::Iter<'a, PendingRecord>;
254
255 fn into_iter(self) -> Self::IntoIter {
256 self.0.iter()
257 }
258}
259
260#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
262#[non_exhaustive]
263pub struct DurationStats {
264 pub observations: u64,
266 pub total: Duration,
268 pub max: Duration,
270}
271
272#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
274#[non_exhaustive]
275pub struct StorageStats {
276 pub configured_capacity_bytes: Option<u64>,
278 pub full_policy: Option<FullPolicy>,
280 pub durable_streams: u64,
282 pub pending_records: u64,
284 pub pending_payload_bytes: u64,
286 pub actual_file_bytes: u64,
288 pub maintenance_headroom_bytes: u64,
290 pub data_admissible_bytes: Option<u64>,
292 pub live_segments: u64,
294 pub sealed_segments: u64,
296 pub reclaimable_segments: u64,
298 pub reclaimable_bytes: u64,
300}
301
302#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
304#[non_exhaustive]
305pub struct WaitStats {
306 pub current: usize,
308 pub waits: u64,
310 pub elapsed: DurationStats,
312}
313
314#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
316#[non_exhaustive]
317pub struct StorageJobStats {
318 pub append: DurationStats,
320 pub read: DurationStats,
322 pub release: DurationStats,
324 pub reclaim: DurationStats,
326 pub segment_rollover: DurationStats,
328}
329
330#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
332#[non_exhaustive]
333pub struct PressureStats {
334 pub command_queue_capacity: usize,
336 pub queue_depth: usize,
338 pub active_storage_jobs: usize,
340 pub admitted_commands: u64,
342 pub reactor_dispatch_wait: DurationStats,
344 pub storage_job_elapsed: DurationStats,
346 pub storage_jobs: StorageJobStats,
348 pub queue_wait: WaitStats,
350 pub readiness_wait: WaitStats,
352 pub capacity_wait: WaitStats,
354}
355
356#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
358#[non_exhaustive]
359pub struct OperationCounters {
360 pub started: u64,
362 pub succeeded: u64,
364 pub failed: u64,
366 pub cancelled: u64,
368 pub records: u64,
374 pub payload_bytes: u64,
378 pub elapsed: DurationStats,
380}
381
382#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
384#[non_exhaustive]
385pub struct OperationStats {
386 pub append: OperationCounters,
388 pub read: OperationCounters,
390 pub release: OperationCounters,
392 pub reclaim: OperationCounters,
394}
395
396#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
398#[non_exhaustive]
399pub struct CommitStats {
400 pub append_groups: u64,
402 pub append_units: u64,
404 pub append_records: u64,
406 pub append_encoded_bytes: u64,
408 pub max_append_units: u64,
410 pub max_append_encoded_bytes: u64,
412 pub release_groups: u64,
414 pub release_units: u64,
416 pub release_records: u64,
418 pub release_encoded_bytes: u64,
420 pub max_release_units: u64,
422 pub max_release_encoded_bytes: u64,
424}
425
426#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
428#[non_exhaustive]
429pub struct MaintenanceStats {
430 pub automatic_reclaim_passes: u64,
432 pub explicit_reclaim_passes: u64,
434 pub size_rollovers: u64,
436 pub age_rollovers: u64,
438 pub reclaim_rollovers: u64,
440 pub reclaimed_segments: u64,
442 pub reclaimed_bytes: u64,
444 pub manifest_compactions: u64,
446}
447
448#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
450#[non_exhaustive]
451pub struct RecoveryStats {
452 pub manifest_frames_scanned: u64,
454 pub segments_scanned: u64,
456 pub epochs_scanned: u64,
458 pub records_scanned: u64,
460 pub repaired_active_tails: u64,
462 pub repaired_manifest_tails: u64,
464 pub completed_segment_seals: u64,
466 pub completed_segment_deletions: u64,
468 pub removed_temporary_files: u64,
470 pub elapsed: Duration,
472}
473
474#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
480#[non_exhaustive]
481pub struct RootStats {
482 pub detailed_timings: bool,
484 pub storage: StorageStats,
486 pub pressure: PressureStats,
488 pub operations: OperationStats,
490 pub commits: CommitStats,
492 pub maintenance: MaintenanceStats,
494 pub recovery: RecoveryStats,
496}
497
498#[derive(Clone, Copy, Debug, Default, Eq, Hash, PartialEq)]
500#[non_exhaustive]
501pub enum RootState {
502 #[default]
504 Running,
505 ShuttingDown,
507 Poisoned,
509 Closed,
511}
512
513impl RootState {
514 #[must_use]
516 pub const fn as_str(self) -> &'static str {
517 match self {
518 Self::Running => "running",
519 Self::ShuttingDown => "shutting_down",
520 Self::Poisoned => "poisoned",
521 Self::Closed => "closed",
522 }
523 }
524}
525
526impl fmt::Display for RootState {
527 fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
528 formatter.write_str(self.as_str())
529 }
530}
531
532#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
534#[non_exhaustive]
535pub enum OperationKind {
536 Append,
538 Read,
540 Release,
542 Reclaim,
544 SegmentRollover,
546 StatePublication,
548 Reactor,
550}
551
552impl OperationKind {
553 #[must_use]
555 pub const fn as_str(self) -> &'static str {
556 match self {
557 Self::Append => "append",
558 Self::Read => "read",
559 Self::Release => "release",
560 Self::Reclaim => "reclaim",
561 Self::SegmentRollover => "segment_rollover",
562 Self::StatePublication => "state_publication",
563 Self::Reactor => "reactor",
564 }
565 }
566}
567
568impl fmt::Display for OperationKind {
569 fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
570 formatter.write_str(self.as_str())
571 }
572}
573
574#[derive(Clone, Debug, Eq, PartialEq)]
576#[non_exhaustive]
577pub struct FailureInfo {
578 pub operation: OperationKind,
580 pub error_kind: ErrorKind,
582 pub durability_outcome: DurabilityOutcome,
587 pub message: String,
589}
590
591#[derive(Clone, Debug, Default, Eq, PartialEq)]
593#[non_exhaustive]
594pub struct RootHealth {
595 pub generation: u64,
597 pub state: RootState,
599 pub failure: Option<FailureInfo>,
601}
602
603#[derive(Clone, Debug, Default, Eq, PartialEq)]
605#[non_exhaustive]
606pub struct StreamStats {
607 pub durable_known: bool,
609 pub pending_records: u64,
611 pub pending_payload_bytes: u64,
613}
614
615#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
617#[non_exhaustive]
618pub struct ReclaimReport {
619 pub segments: u64,
621 pub bytes: u64,
623}
624
625impl ReclaimReport {
626 #[must_use]
628 pub const fn is_empty(self) -> bool {
629 self.segments == 0 && self.bytes == 0
630 }
631}
632
633#[cfg(test)]
634mod tests {
635 use super::*;
636
637 #[test]
638 fn record_id_has_fixed_root_stream_sequence_encoding() {
639 let root = RootId::from_bytes([0x11; 16]);
640 let stream = StreamId::new(0x0203_0405_0607_0809);
641 let id = RecordId::from_parts(root, stream, 0x0a0b_0c0d_0e0f_1011);
642
643 assert_eq!(&id.as_bytes()[..16], &[0x11; 16]);
644 assert_eq!(id.root_id(), root);
645 assert_eq!(id.stream_id(), stream);
646 assert_eq!(id.sequence(), 0x0a0b_0c0d_0e0f_1011);
647 assert_eq!(RecordId::from_bytes(id.to_bytes()), id);
648 }
649}