aletheiadb 0.1.0

A high-performance bi-temporal graph database for LLM integration
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
//! Metrics contract for AletheiaDB.
//!
//! Metric names are stable public API. The built-in [`METRICS`] value keeps a
//! small atomic mirror for tests and local diagnostics; production export is
//! handled by an installed [`MetricsRecorder`] such as
//! [`metrics_rs_adapter::MetricsRsRecorder`](crate::observability::metrics_rs_adapter::MetricsRsRecorder).

use std::sync::atomic::{AtomicU64, Ordering};

const METRIC_READ_ORDERING: Ordering = Ordering::Acquire;
const METRIC_WRITE_ORDERING: Ordering = Ordering::Release;

/// Counter: errors by bounded category.
pub const METRIC_ERRORS: &str = "aletheiadb.errors";

/// Counter: write conflicts observed under snapshot isolation.
pub const METRIC_WRITE_CONFLICTS: &str = "aletheiadb.write_conflicts";

/// Counter: events that should be zero in healthy production systems.
pub const METRIC_CRITICAL_EVENTS: &str = "aletheiadb.critical_events";

/// Counter: transaction commits by durability mode and status.
pub const METRIC_TRANSACTION_COMMITS: &str = "aletheiadb.transaction.commits";

/// Histogram: transaction commit wall-clock duration in seconds.
pub const METRIC_TRANSACTION_COMMIT_DURATION: &str = "aletheiadb.transaction.commit.duration";

/// Histogram: operations included in a committed transaction.
pub const METRIC_TRANSACTION_OPERATIONS: &str = "aletheiadb.transaction.operations";

/// Metric label: bounded error category.
pub const METRIC_LABEL_CATEGORY: &str = "category";

/// Metric label: bounded operation status.
pub const METRIC_LABEL_STATUS: &str = "status";

/// Metric label: durability mode.
pub const METRIC_LABEL_DURABILITY_MODE: &str = "durability_mode";

/// Metric label: bounded critical event name.
pub const METRIC_LABEL_EVENT: &str = "event";

/// Global atomic metric mirror.
pub static METRICS: Metrics = Metrics::new();

/// Bounded error categories safe for metric labels.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ErrorCategory {
    /// Storage-layer error.
    Storage,
    /// Temporal validity or transaction-time error.
    Temporal,
    /// Query parsing, planning, or execution error.
    Query,
    /// Transaction lifecycle error.
    Transaction,
    /// Vector validation, indexing, or search error.
    Vector,
    /// I/O error.
    Io,
    /// Catch-all category for miscellaneous errors.
    Other,
}

impl ErrorCategory {
    /// Stable metric label value.
    #[must_use]
    pub const fn as_str(self) -> &'static str {
        match self {
            Self::Storage => "storage",
            Self::Temporal => "temporal",
            Self::Query => "query",
            Self::Transaction => "transaction",
            Self::Vector => "vector",
            Self::Io => "io",
            Self::Other => "other",
        }
    }
}

/// Critical event categories safe for metric labels.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum CriticalEvent {
    /// A thread panicked while holding a lock.
    LockPoison,
    /// Transaction timestamp monotonicity was violated.
    TimestampViolation,
    /// WAL checksum validation failed.
    WalChecksumFailure,
}

impl CriticalEvent {
    /// Stable metric label value.
    #[must_use]
    pub const fn as_str(self) -> &'static str {
        match self {
            Self::LockPoison => "lock_poison",
            Self::TimestampViolation => "timestamp_violation",
            Self::WalChecksumFailure => "wal_checksum_failure",
        }
    }
}

/// Sink for AletheiaDB's standard metrics.
///
/// Implementations should preserve the metric names and label sets defined in
/// this module. All methods default to no-op bodies so applications can record
/// only the samples they need.
pub trait MetricsRecorder: Send + Sync {
    /// Record one error by bounded category.
    fn record_error(&self, category: ErrorCategory) {
        let _ = category;
    }

    /// Record one write conflict.
    fn record_write_conflict(&self) {}

    /// Record one critical event.
    fn record_critical_event(&self, event: CriticalEvent) {
        let _ = event;
    }

    /// Record a completed transaction commit.
    fn record_transaction_commit(
        &self,
        duration_secs: f64,
        operations_count: u64,
        durability_mode: &str,
        status: &str,
    ) {
        let _ = (duration_secs, operations_count, durability_mode, status);
    }
}

/// Default metrics recorder that discards every sample.
#[derive(Debug, Default, Clone, Copy)]
pub struct NoOpMetrics;

impl MetricsRecorder for NoOpMetrics {}

/// Atomic metric mirror used for local diagnostics and existing tests.
pub struct Metrics {
    /// Number of lock poisoning events detected.
    pub lock_poison_count: AtomicU64,

    /// Number of timestamp monotonicity violations.
    pub timestamp_violations: AtomicU64,

    /// Number of WAL checksum failures detected.
    pub wal_checksum_failures: AtomicU64,

    /// Number of write-write conflicts.
    pub write_conflicts: AtomicU64,

    /// Total storage errors.
    pub error_storage_total: AtomicU64,

    /// Total temporal errors.
    pub error_temporal_total: AtomicU64,

    /// Total query errors.
    pub error_query_total: AtomicU64,

    /// Total transaction errors.
    pub error_transaction_total: AtomicU64,

    /// Total vector errors.
    pub error_vector_total: AtomicU64,

    /// Total I/O errors.
    pub error_io_total: AtomicU64,

    /// Total other errors.
    pub error_other_total: AtomicU64,

    /// Total transaction commits.
    pub transaction_commits_total: AtomicU64,
}

impl Default for Metrics {
    fn default() -> Self {
        Self::new()
    }
}

impl Metrics {
    /// Create a zeroed metric mirror.
    #[must_use]
    pub const fn new() -> Self {
        Self {
            lock_poison_count: AtomicU64::new(0),
            timestamp_violations: AtomicU64::new(0),
            wal_checksum_failures: AtomicU64::new(0),
            write_conflicts: AtomicU64::new(0),
            error_storage_total: AtomicU64::new(0),
            error_temporal_total: AtomicU64::new(0),
            error_query_total: AtomicU64::new(0),
            error_transaction_total: AtomicU64::new(0),
            error_vector_total: AtomicU64::new(0),
            error_io_total: AtomicU64::new(0),
            error_other_total: AtomicU64::new(0),
            transaction_commits_total: AtomicU64::new(0),
        }
    }

    /// Capture a point-in-time snapshot.
    #[must_use]
    pub fn snapshot(&self) -> MetricsSnapshot {
        MetricsSnapshot {
            lock_poison_count: self.lock_poison_count.load(METRIC_READ_ORDERING),
            timestamp_violations: self.timestamp_violations.load(METRIC_READ_ORDERING),
            wal_checksum_failures: self.wal_checksum_failures.load(METRIC_READ_ORDERING),
            write_conflicts: self.write_conflicts.load(METRIC_READ_ORDERING),
            error_storage_total: self.error_storage_total.load(METRIC_READ_ORDERING),
            error_temporal_total: self.error_temporal_total.load(METRIC_READ_ORDERING),
            error_query_total: self.error_query_total.load(METRIC_READ_ORDERING),
            error_transaction_total: self.error_transaction_total.load(METRIC_READ_ORDERING),
            error_vector_total: self.error_vector_total.load(METRIC_READ_ORDERING),
            error_io_total: self.error_io_total.load(METRIC_READ_ORDERING),
            error_other_total: self.error_other_total.load(METRIC_READ_ORDERING),
            transaction_commits_total: self.transaction_commits_total.load(METRIC_READ_ORDERING),
        }
    }

    /// Reset all counters to zero. Intended for tests only.
    #[cfg(test)]
    pub fn reset(&self) {
        self.lock_poison_count.store(0, METRIC_WRITE_ORDERING);
        self.timestamp_violations.store(0, METRIC_WRITE_ORDERING);
        self.wal_checksum_failures.store(0, METRIC_WRITE_ORDERING);
        self.write_conflicts.store(0, METRIC_WRITE_ORDERING);
        self.error_storage_total.store(0, METRIC_WRITE_ORDERING);
        self.error_temporal_total.store(0, METRIC_WRITE_ORDERING);
        self.error_query_total.store(0, METRIC_WRITE_ORDERING);
        self.error_transaction_total.store(0, METRIC_WRITE_ORDERING);
        self.error_vector_total.store(0, METRIC_WRITE_ORDERING);
        self.error_io_total.store(0, METRIC_WRITE_ORDERING);
        self.error_other_total.store(0, METRIC_WRITE_ORDERING);
        self.transaction_commits_total
            .store(0, METRIC_WRITE_ORDERING);
    }
}

impl MetricsRecorder for Metrics {
    fn record_error(&self, category: ErrorCategory) {
        let counter = match category {
            ErrorCategory::Storage => &self.error_storage_total,
            ErrorCategory::Temporal => &self.error_temporal_total,
            ErrorCategory::Query => &self.error_query_total,
            ErrorCategory::Transaction => &self.error_transaction_total,
            ErrorCategory::Vector => &self.error_vector_total,
            ErrorCategory::Io => &self.error_io_total,
            ErrorCategory::Other => &self.error_other_total,
        };
        counter.fetch_add(1, METRIC_WRITE_ORDERING);
    }

    fn record_write_conflict(&self) {
        self.write_conflicts.fetch_add(1, METRIC_WRITE_ORDERING);
    }

    fn record_critical_event(&self, event: CriticalEvent) {
        let counter = match event {
            CriticalEvent::LockPoison => &self.lock_poison_count,
            CriticalEvent::TimestampViolation => &self.timestamp_violations,
            CriticalEvent::WalChecksumFailure => &self.wal_checksum_failures,
        };
        counter.fetch_add(1, METRIC_WRITE_ORDERING);
    }

    fn record_transaction_commit(
        &self,
        _duration_secs: f64,
        _operations_count: u64,
        _durability_mode: &str,
        _status: &str,
    ) {
        self.transaction_commits_total
            .fetch_add(1, METRIC_WRITE_ORDERING);
    }
}

/// Snapshot of the built-in atomic metric mirror.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct MetricsSnapshot {
    /// Number of lock poisoning events detected.
    pub lock_poison_count: u64,

    /// Number of timestamp monotonicity violations.
    pub timestamp_violations: u64,

    /// Number of WAL checksum failures detected.
    pub wal_checksum_failures: u64,

    /// Number of write-write conflicts.
    pub write_conflicts: u64,

    /// Total storage errors.
    pub error_storage_total: u64,

    /// Total temporal errors.
    pub error_temporal_total: u64,

    /// Total query errors.
    pub error_query_total: u64,

    /// Total transaction errors.
    pub error_transaction_total: u64,

    /// Total vector errors.
    pub error_vector_total: u64,

    /// Total I/O errors.
    pub error_io_total: u64,

    /// Total other errors.
    pub error_other_total: u64,

    /// Total transaction commits.
    pub transaction_commits_total: u64,
}

impl MetricsSnapshot {
    /// `true` when any critical corruption indicator is non-zero.
    #[must_use]
    pub const fn has_critical_errors(&self) -> bool {
        self.lock_poison_count > 0
            || self.timestamp_violations > 0
            || self.wal_checksum_failures > 0
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::sync::Arc;
    use std::thread;

    #[test]
    fn error_category_string_values_are_stable() {
        assert_eq!(ErrorCategory::Storage.as_str(), "storage");
        assert_eq!(ErrorCategory::Temporal.as_str(), "temporal");
        assert_eq!(ErrorCategory::Query.as_str(), "query");
        assert_eq!(ErrorCategory::Transaction.as_str(), "transaction");
        assert_eq!(ErrorCategory::Vector.as_str(), "vector");
        assert_eq!(ErrorCategory::Io.as_str(), "io");
        assert_eq!(ErrorCategory::Other.as_str(), "other");
    }

    #[test]
    fn metrics_initialization() {
        let metrics = Metrics::new();
        let snapshot = metrics.snapshot();

        assert_eq!(snapshot.lock_poison_count, 0);
        assert_eq!(snapshot.timestamp_violations, 0);
        assert_eq!(snapshot.wal_checksum_failures, 0);
        assert_eq!(snapshot.write_conflicts, 0);
        assert_eq!(snapshot.transaction_commits_total, 0);
    }

    #[test]
    fn metrics_increment_through_contract() {
        let metrics = Metrics::new();

        metrics.record_critical_event(CriticalEvent::LockPoison);
        metrics.record_write_conflict();
        metrics.record_error(ErrorCategory::Storage);
        metrics.record_transaction_commit(0.001, 3, "Synchronous", "committed");

        let snapshot = metrics.snapshot();
        assert_eq!(snapshot.lock_poison_count, 1);
        assert_eq!(snapshot.write_conflicts, 1);
        assert_eq!(snapshot.error_storage_total, 1);
        assert_eq!(snapshot.transaction_commits_total, 1);
    }

    #[test]
    fn concurrent_increments() {
        let metrics = Arc::new(Metrics::new());
        let mut handles = vec![];

        for _ in 0..10 {
            let metrics_clone = Arc::clone(&metrics);
            let handle = thread::spawn(move || {
                for _ in 0..100 {
                    metrics_clone.record_write_conflict();
                }
            });
            handles.push(handle);
        }

        for handle in handles {
            handle.join().unwrap();
        }

        assert_eq!(metrics.snapshot().write_conflicts, 1000);
    }

    #[test]
    fn has_critical_errors() {
        let metrics = Metrics::new();

        assert!(!metrics.snapshot().has_critical_errors());

        metrics.record_critical_event(CriticalEvent::LockPoison);
        assert!(metrics.snapshot().has_critical_errors());

        metrics.reset();
        metrics.record_critical_event(CriticalEvent::TimestampViolation);
        assert!(metrics.snapshot().has_critical_errors());

        metrics.reset();
        metrics.record_critical_event(CriticalEvent::WalChecksumFailure);
        assert!(metrics.snapshot().has_critical_errors());

        metrics.reset();
        metrics.record_write_conflict();
        assert!(!metrics.snapshot().has_critical_errors());
    }

    #[test]
    fn global_metrics_singleton() {
        METRICS.record_critical_event(CriticalEvent::LockPoison);
        assert!(METRICS.snapshot().lock_poison_count >= 1);
    }
}