reddb-io-server 1.2.0

RedDB server-side engine: storage, runtime, replication, MCP, AI, and the gRPC/HTTP/RedWire/PG-wire dispatchers. Re-exported by the umbrella `reddb` crate.
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
428
429
430
431
432
//! Dedicated slow-query sink — writes structured JSON lines to `red-slow.log`.
//!
//! Below-threshold calls pay only one relaxed atomic load and return
//! immediately with zero allocations. Above-threshold calls go through
//! a deterministic counter-based sampler before writing to the
//! `NonBlocking` writer, which pushes the bytes onto a channel and
//! returns without waiting for the disk write.

use std::io::Write;
use std::path::PathBuf;
use std::sync::atomic::{AtomicU64, AtomicU8, Ordering};
use std::sync::{Arc, Mutex};

use tracing_appender::non_blocking::{NonBlocking, NonBlockingBuilder, WorkerGuard};

use crate::runtime::EffectiveScope;

// ---------------------------------------------------------------------------
// Public types
// ---------------------------------------------------------------------------

pub struct SlowQueryOpts {
    pub log_dir: PathBuf,
    pub threshold_ms: u64,
    /// 0..=100; values > 100 are clamped to 100.
    pub sample_pct: u8,
}

/// Closed enum of query kinds emitted in the slow-query log.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum QueryKind {
    Select,
    Insert,
    Update,
    Delete,
    Bulk,
    Aggregate,
    DDL,
    Internal,
}

impl QueryKind {
    fn as_str(self) -> &'static str {
        match self {
            Self::Select => "select",
            Self::Insert => "insert",
            Self::Update => "update",
            Self::Delete => "delete",
            Self::Bulk => "bulk",
            Self::Aggregate => "aggregate",
            Self::DDL => "ddl",
            Self::Internal => "internal",
        }
    }
}

// ---------------------------------------------------------------------------
// SlowQueryLogger
// ---------------------------------------------------------------------------

pub struct SlowQueryLogger {
    /// Interior-mutable because `Write::write_all` takes `&mut self`.
    writer: Mutex<NonBlocking>,
    /// Keeps the background writer thread alive for the process lifetime.
    _guard: WorkerGuard,
    threshold_ms: AtomicU64,
    /// 0..100; 100 means "emit every above-threshold query".
    sample_pct: AtomicU8,
    /// Monotonic counter across above-threshold calls — drives round-robin
    /// sampling so exactly `sample_pct`% of above-threshold calls emit.
    above_count: AtomicU64,
}

impl SlowQueryLogger {
    pub fn new(opts: SlowQueryOpts) -> Arc<Self> {
        let _ = std::fs::create_dir_all(&opts.log_dir);
        let path = opts.log_dir.join("red-slow.log");
        let file = std::fs::OpenOptions::new()
            .create(true)
            .append(true)
            .open(&path)
            .unwrap_or_else(|e| panic!("SlowQueryLogger: cannot open {}: {e}", path.display()));

        let (writer, guard) = NonBlockingBuilder::default()
            .buffered_lines_limit(65_536)
            .lossy(true)
            .finish(file);

        Arc::new(Self {
            writer: Mutex::new(writer),
            _guard: guard,
            threshold_ms: AtomicU64::new(opts.threshold_ms),
            sample_pct: AtomicU8::new(opts.sample_pct.min(100)),
            above_count: AtomicU64::new(0),
        })
    }

    /// Record a completed query. Below-threshold: single relaxed load, no
    /// allocation. Above-threshold + sampled: emit a JSON line to the sink.
    pub fn record(
        &self,
        kind: QueryKind,
        duration_ms: u64,
        sql_redacted: String,
        scope: &EffectiveScope,
    ) {
        // Hot path: threshold gate — single relaxed atomic load.
        if duration_ms < self.threshold_ms.load(Ordering::Relaxed) {
            return;
        }

        // Sampling: deterministic round-robin counter.
        // `above_count % 100 < sample_pct` gives exactly sample_pct% over
        // long runs without any floating-point or RNG overhead.
        let pct = u64::from(self.sample_pct.load(Ordering::Relaxed));
        if pct < 100 {
            let n = self.above_count.fetch_add(1, Ordering::Relaxed);
            if (n % 100) >= pct {
                return;
            }
        }

        self.emit(kind, duration_ms, sql_redacted, scope);
    }

    fn emit(
        &self,
        kind: QueryKind,
        duration_ms: u64,
        sql_redacted: String,
        scope: &EffectiveScope,
    ) {
        let ts_ms = crate::utils::now_unix_millis();
        let tenant = scope.tenant.as_deref().unwrap_or("").to_string();
        let identity = scope
            .identity
            .as_ref()
            .map(|(u, _)| u.as_str())
            .unwrap_or("")
            .to_string();

        let mut map = std::collections::BTreeMap::new();
        map.insert(
            "ts_ms".to_string(),
            crate::json::Value::Number(ts_ms as f64),
        );
        map.insert(
            "kind".to_string(),
            crate::json::Value::String(kind.as_str().to_string()),
        );
        map.insert(
            "duration_ms".to_string(),
            crate::json::Value::Number(duration_ms as f64),
        );
        map.insert("sql".to_string(), crate::json::Value::String(sql_redacted));
        map.insert("tenant".to_string(), crate::json::Value::String(tenant));
        map.insert("identity".to_string(), crate::json::Value::String(identity));

        let obj = crate::json::Value::Object(map);
        if let Ok(mut line) = crate::json::to_string(&obj) {
            line.push('\n');
            if let Ok(mut w) = self.writer.lock() {
                let _ = w.write_all(line.as_bytes());
            }
        }
    }
}

// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------

#[cfg(test)]
mod tests {
    use std::collections::HashSet;
    use std::time::Instant;

    use super::*;
    use crate::runtime::EffectiveScope;
    use crate::storage::transaction::snapshot::Snapshot;

    fn tmp_dir() -> PathBuf {
        let mut d = std::env::temp_dir();
        d.push(format!(
            "reddb-slow-{}-{}",
            std::process::id(),
            crate::utils::now_unix_nanos()
        ));
        d
    }

    fn logger(dir: &PathBuf, threshold_ms: u64, sample_pct: u8) -> Arc<SlowQueryLogger> {
        SlowQueryLogger::new(SlowQueryOpts {
            log_dir: dir.clone(),
            threshold_ms,
            sample_pct,
        })
    }

    fn empty_scope() -> EffectiveScope {
        EffectiveScope {
            tenant: None,
            identity: None,
            snapshot: Snapshot {
                xid: 0,
                in_progress: HashSet::new(),
            },
            visible_collections: None,
        }
    }

    fn flush(_log: &Arc<SlowQueryLogger>) {
        // NonBlocking sends bytes to a background thread; sleep gives the
        // worker time to drain the channel and write to disk.
        std::thread::sleep(std::time::Duration::from_millis(50));
    }

    fn read_log_lines(dir: &PathBuf) -> Vec<crate::json::Value> {
        let path = dir.join("red-slow.log");
        let body = std::fs::read_to_string(&path).unwrap_or_default();
        body.lines()
            .filter(|l| !l.is_empty())
            .map(|l| crate::json::from_str::<crate::json::Value>(l).expect("valid JSON"))
            .collect()
    }

    // -----------------------------------------------------------------------
    // Below-threshold: zero writes, fast
    // -----------------------------------------------------------------------

    #[test]
    fn below_threshold_no_file_writes() {
        let dir = tmp_dir();
        let log = logger(&dir, 1000, 100);
        let scope = empty_scope();

        for _ in 0..10_000 {
            log.record(QueryKind::Select, 5, "SELECT 1".into(), &scope);
        }

        flush(&log);
        let lines = read_log_lines(&dir);
        assert!(
            lines.is_empty(),
            "expected zero writes, got {}",
            lines.len()
        );
    }

    #[test]
    fn below_threshold_wall_time_under_10ms() {
        let dir = tmp_dir();
        let log = logger(&dir, 1000, 100);
        let scope = empty_scope();

        let start = Instant::now();
        for _ in 0..10_000 {
            log.record(QueryKind::Select, 5, "SELECT 1".into(), &scope);
        }
        let elapsed = start.elapsed();
        assert!(
            elapsed.as_millis() < 10,
            "10k below-threshold calls took {}ms (>10ms budget)",
            elapsed.as_millis()
        );
    }

    // -----------------------------------------------------------------------
    // Above-threshold: structured JSON line, parseable fields
    // -----------------------------------------------------------------------

    #[test]
    fn above_threshold_emits_json_line() {
        let dir = tmp_dir();
        let log = logger(&dir, 10, 100);
        let scope = empty_scope();

        log.record(QueryKind::Select, 100, "SELECT * FROM t".into(), &scope);
        flush(&log);

        let lines = read_log_lines(&dir);
        assert_eq!(lines.len(), 1, "expected 1 line");
        let v = &lines[0];
        assert_eq!(v.get("kind").and_then(|x| x.as_str()), Some("select"));
        assert_eq!(v.get("duration_ms").and_then(|x| x.as_i64()), Some(100));
        let sql = v.get("sql").and_then(|x| x.as_str());
        assert_eq!(sql, Some("SELECT * FROM t"));
    }

    #[test]
    fn json_line_has_all_required_fields() {
        let dir = tmp_dir();
        let log = logger(&dir, 0, 100);
        let scope = empty_scope();

        log.record(
            QueryKind::Insert,
            42,
            "INSERT INTO t VALUES (1)".into(),
            &scope,
        );
        flush(&log);

        let lines = read_log_lines(&dir);
        assert_eq!(lines.len(), 1);
        let v = &lines[0];
        assert!(v.get("ts_ms").is_some(), "missing ts_ms");
        assert!(v.get("kind").is_some(), "missing kind");
        assert!(v.get("duration_ms").is_some(), "missing duration_ms");
        assert!(v.get("sql").is_some(), "missing sql");
        assert!(v.get("tenant").is_some(), "missing tenant");
        assert!(v.get("identity").is_some(), "missing identity");
    }

    #[test]
    fn all_query_kinds_serialise() {
        let kinds = [
            QueryKind::Select,
            QueryKind::Insert,
            QueryKind::Update,
            QueryKind::Delete,
            QueryKind::Bulk,
            QueryKind::Aggregate,
            QueryKind::DDL,
            QueryKind::Internal,
        ];
        for k in kinds {
            assert!(!k.as_str().is_empty());
        }
    }

    // -----------------------------------------------------------------------
    // Sampling property test: sample_pct=10 → ~10% (±2pp) over 10_000 calls
    // -----------------------------------------------------------------------

    #[test]
    fn sampling_property_10pct() {
        let dir = tmp_dir();
        // threshold=0 so every call is above-threshold
        let log = logger(&dir, 0, 10);
        let scope = empty_scope();

        let calls = 10_000u64;
        for i in 0..calls {
            log.record(QueryKind::Select, 1, format!("SELECT {i}"), &scope);
        }
        flush(&log);

        let lines = read_log_lines(&dir);
        let emitted = lines.len() as u64;
        // Expected: 1000 ± 200 (2pp of 10_000)
        assert!(
            emitted >= 800 && emitted <= 1200,
            "sample_pct=10 over {calls} calls emitted {emitted} (expected 800..=1200)"
        );
    }

    // -----------------------------------------------------------------------
    // Adversarial SQL: CRLF / NUL / quote → escape-safe JSON
    // -----------------------------------------------------------------------

    #[test]
    fn adversarial_sql_is_escape_safe() {
        let payloads: &[(&str, &str)] = &[
            ("crlf", "SELECT 1\r\nDROP TABLE t--"),
            ("nul", "SELECT '\x00'"),
            ("quote", r#"SELECT "secret" FROM t"#),
            ("json_inject", r#"SELECT 1},"pwned":true,{"x":"#),
            ("low_ctrl", "SELECT \x01\x02\x07\x1f"),
            ("backslash", "SELECT 'C:\\path\\file'"),
        ];

        for (label, sql) in payloads {
            let dir = tmp_dir();
            let log = logger(&dir, 0, 100);
            let scope = empty_scope();

            log.record(QueryKind::Select, 1, sql.to_string(), &scope);
            flush(&log);

            let path = dir.join("red-slow.log");
            let body = std::fs::read_to_string(&path).unwrap_or_default();
            let line = body
                .lines()
                .find(|l| !l.is_empty())
                .unwrap_or_else(|| panic!("{label}: no line emitted"));

            // Must be a single JSONL row — no embedded newline.
            assert!(
                !line.contains('\n'),
                "{label}: embedded newline in JSONL row"
            );

            // Must parse as valid JSON.
            let v: crate::json::Value = crate::json::from_str(line)
                .unwrap_or_else(|e| panic!("{label}: not valid JSON: {e}\n{line:?}"));

            // SQL must round-trip correctly.
            let recovered = v.get("sql").and_then(|x| x.as_str()).unwrap_or("");
            assert_eq!(recovered, *sql, "{label}: SQL round-trip mismatch");
        }
    }

    // -----------------------------------------------------------------------
    // Exact-threshold boundary: duration == threshold emits
    // -----------------------------------------------------------------------

    #[test]
    fn at_threshold_boundary_emits() {
        let dir = tmp_dir();
        let log = logger(&dir, 50, 100);
        let scope = empty_scope();

        // Exactly at threshold: duration_ms < threshold_ms is false → emits.
        log.record(QueryKind::Select, 50, "SELECT 1".into(), &scope);
        flush(&log);
        let lines = read_log_lines(&dir);
        assert_eq!(lines.len(), 1, "duration == threshold should emit");
    }

    #[test]
    fn just_below_threshold_does_not_emit() {
        let dir = tmp_dir();
        let log = logger(&dir, 50, 100);
        let scope = empty_scope();

        log.record(QueryKind::Select, 49, "SELECT 1".into(), &scope);
        flush(&log);
        let lines = read_log_lines(&dir);
        assert!(lines.is_empty(), "duration < threshold must not emit");
    }
}