Skip to main content

chronix_engine/memtable/
error.rs

1//! Error types for the memtable crate.
2
3use thiserror::Error;
4
5/// Errors that can occur during memtable operations.
6#[derive(Debug, Error)]
7pub enum MemtableError {
8    /// The memtable is frozen and cannot accept new writes.
9    #[error("memtable is frozen and cannot accept writes")]
10    Frozen,
11
12    /// Memory capacity exceeded — the memtable has reached its size limit.
13    #[error("memtable capacity exceeded: current {current} bytes, limit {limit} bytes")]
14    CapacityExceeded {
15        /// Current memory usage in bytes.
16        current: usize,
17        /// Configured memory limit in bytes.
18        limit: usize,
19    },
20
21    /// A schema violation was detected during insert.
22    #[error("schema error: {0}")]
23    Schema(#[from] chronix_core::SchemaError),
24
25    /// The timestamp is outside the allowed out-of-order tolerance.
26    ///
27    /// The message names **timestamps**, because shard identifiers are an
28    /// internal quotient nobody can act on: a Telegraf agent that buffered for
29    /// an hour was told `write to shard 496833 rejected: outside tolerance
30    /// window [496836..=496840]`, which says neither how late the point was
31    /// nor what to do about it. It also names the way out, because there is
32    /// one — importing history is `backfill`, and every network write path
33    /// exposes it.
34    #[error(
35        "timestamp {timestamp} is outside the out-of-order window \
36         [{earliest}, {latest}] (+/-{tolerance} shard(s) of {shard_secs}s \
37         around the newest admitted write); import history with a backfill \
38         write instead"
39    )]
40    ShardOutOfRange {
41        /// The rejected timestamp, in epoch nanoseconds.
42        timestamp: i64,
43        /// Oldest timestamp the window admits, in epoch nanoseconds.
44        earliest: i64,
45        /// Newest timestamp the window admits, in epoch nanoseconds.
46        latest: i64,
47        /// Configured `ooo_shard_tolerance`.
48        tolerance: i64,
49        /// Configured `shard_duration`, in seconds.
50        shard_secs: u64,
51    },
52
53    /// A flush was asked for a shard the router does not hold.
54    ///
55    /// Its own variant: this was reported as `ShardOutOfRange` with a target
56    /// shard and a `[0..=0]` window, which is a different fact stated with
57    /// made-up numbers.
58    #[error("no such shard: {0}")]
59    NoSuchShard(i64),
60
61    /// An I/O error occurred during flush.
62    #[error("flush I/O error: {0}")]
63    Io(#[from] std::io::Error),
64
65    /// A segment write error occurred during flush.
66    #[error("segment write error: {0}")]
67    Segment(#[from] crate::segment::SegmentError),
68
69    /// Registering a flushed segment failed; the memtable stays frozen.
70    #[error("segment registration failed: {0}")]
71    Flush(String),
72
73    /// No frozen memtable is available for flushing.
74    #[error("no frozen memtable available for flush")]
75    NoFrozenMemtable,
76
77    /// A WAL error occurred during truncation.
78    #[error("WAL error: {0}")]
79    Wal(#[from] chronix_core::WalError),
80}
81
82/// Convenience type alias for memtable results.
83pub type Result<T> = std::result::Result<T, MemtableError>;