udb 0.4.21

Universal Data Broker — a Rust gRPC broker over multiple databases (Postgres, MySQL, SQLite, MongoDB, ClickHouse, Cassandra, MSSQL, Redis, Qdrant, S3, Neo4j, …) with per-tenant RLS, 2PC, sagas, and CDC.
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
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
//! Redis engine — all `redis::` access lives here; the module is gated on the
//! `redis` feature. Every prefix sweep uses Redis `SCAN` ([`SWEEP_COMMAND`]),
//! NEVER `KEYS`, so a large keyspace never blocks the server.

use tonic::Status;

use crate::proto::udb::core::cache::services::v1 as cache_pb;

use super::config::{
    RECONCILE_DISCOVERY_MAX_ROUNDS, RECONCILE_MAX_KEYS_PER_NAMESPACE,
    RECONCILE_NAMESPACES_PER_PASS, SWEEP_COMMAND, SWEEP_COUNT,
};
use super::keys::{
    bytes_counter_key, clamped_scan_count, data_key, data_match, effective_max_bytes, meta_key,
    meta_match_all, namespace_match_all, parse_meta_key, reconcile_cursor_key, reconciled_sum,
    strip_data_prefix, would_exceed_budget,
};

fn map_err(context: &str, err: redis::RedisError) -> Status {
    crate::runtime::executor_utils::backend_transport_status("redis", context, err)
}

async fn connect(client: &redis::Client) -> Result<redis::aio::MultiplexedConnection, Status> {
    client
        .get_multiplexed_async_connection()
        .await
        .map_err(|err| {
            crate::runtime::executor_utils::backend_transport_status("redis", "connection", err)
        })
}

/// The decoded namespace meta blob (budget + default TTL).
#[derive(Default)]
struct NamespaceMeta {
    max_bytes: i64,
    default_ttl_seconds: i64,
}

async fn load_meta(
    conn: &mut redis::aio::MultiplexedConnection,
    tenant: &str,
    namespace: &str,
) -> Result<NamespaceMeta, Status> {
    let raw: Option<String> = redis::cmd("GET")
        .arg(meta_key(tenant, namespace))
        .query_async(conn)
        .await
        .map_err(|err| map_err("GET meta", err))?;
    let Some(raw) = raw else {
        return Ok(NamespaceMeta::default());
    };
    let value: serde_json::Value = serde_json::from_str(&raw).unwrap_or_default();
    Ok(NamespaceMeta {
        max_bytes: value
            .get("max_bytes")
            .and_then(serde_json::Value::as_i64)
            .unwrap_or(0),
        default_ttl_seconds: value
            .get("default_ttl_seconds")
            .and_then(serde_json::Value::as_i64)
            .unwrap_or(0),
    })
}

async fn read_counter(
    conn: &mut redis::aio::MultiplexedConnection,
    tenant: &str,
    namespace: &str,
) -> Result<i64, Status> {
    let used: Option<i64> = redis::cmd("GET")
        .arg(bytes_counter_key(tenant, namespace))
        .query_async(conn)
        .await
        .map_err(|err| map_err("GET counter", err))?;
    Ok(used.unwrap_or(0).max(0))
}

pub(crate) async fn get(
    client: &redis::Client,
    tenant: &str,
    namespace: &str,
    key: &str,
) -> Result<(bool, Vec<u8>, i64), Status> {
    let mut conn = connect(client).await?;
    let full = data_key(tenant, namespace, key);
    let value: Option<Vec<u8>> = redis::cmd("GET")
        .arg(&full)
        .query_async(&mut conn)
        .await
        .map_err(|err| map_err("GET", err))?;
    match value {
        Some(value) => {
            let ttl: i64 = redis::cmd("TTL")
                .arg(&full)
                .query_async(&mut conn)
                .await
                .map_err(|err| map_err("TTL", err))?;
            Ok((true, value, ttl))
        }
        None => Ok((false, Vec::new(), -2)),
    }
}

pub(crate) struct SetOutcome {
    pub used_bytes: i64,
    pub max_bytes: i64,
}

pub(crate) async fn set(
    client: &redis::Client,
    tenant: &str,
    namespace: &str,
    key: &str,
    value: &[u8],
    ttl_seconds: i64,
) -> Result<SetOutcome, Status> {
    let mut conn = connect(client).await?;
    let full = data_key(tenant, namespace, key);
    let meta = load_meta(&mut conn, tenant, namespace).await?;
    let max_bytes = effective_max_bytes(meta.max_bytes);

    // Replacing an existing key only charges the size DELTA against the budget.
    let old_len: i64 = redis::cmd("STRLEN")
        .arg(&full)
        .query_async(&mut conn)
        .await
        .map_err(|err| map_err("STRLEN", err))?;
    let new_len = value.len() as i64;
    let delta = new_len - old_len;
    let used = read_counter(&mut conn, tenant, namespace).await?;
    if would_exceed_budget(used, delta, max_bytes) {
        return Err(crate::runtime::executor_utils::quota_refusal_status(
            "cache",
            "namespace byte budget",
            format!(
                "cache namespace '{namespace}' byte budget exhausted \
                 (used {used} + {delta} > max {max_bytes})"
            ),
        ));
    }

    // Resolve TTL: explicit request value wins; else the namespace default; else
    // no expiry.
    let ttl = if ttl_seconds > 0 {
        ttl_seconds
    } else {
        meta.default_ttl_seconds.max(0)
    };
    let mut set_cmd = redis::cmd("SET");
    set_cmd.arg(&full).arg(value);
    if ttl > 0 {
        set_cmd.arg("EX").arg(ttl);
    }
    set_cmd
        .query_async::<()>(&mut conn)
        .await
        .map_err(|err| map_err("SET", err))?;

    let used_after: i64 = if delta != 0 {
        redis::cmd("INCRBY")
            .arg(bytes_counter_key(tenant, namespace))
            .arg(delta)
            .query_async(&mut conn)
            .await
            .map_err(|err| map_err("INCRBY", err))?
    } else {
        used
    };
    Ok(SetOutcome {
        used_bytes: used_after.max(0),
        max_bytes,
    })
}

pub(crate) async fn delete(
    client: &redis::Client,
    tenant: &str,
    namespace: &str,
    key: &str,
) -> Result<(bool, i64), Status> {
    let mut conn = connect(client).await?;
    let full = data_key(tenant, namespace, key);
    let old_len: i64 = redis::cmd("STRLEN")
        .arg(&full)
        .query_async(&mut conn)
        .await
        .map_err(|err| map_err("STRLEN", err))?;
    let removed: i64 = redis::cmd("DEL")
        .arg(&full)
        .query_async(&mut conn)
        .await
        .map_err(|err| map_err("DEL", err))?;
    if removed == 0 {
        let used = read_counter(&mut conn, tenant, namespace).await?;
        return Ok((false, used));
    }
    let used_after: i64 = redis::cmd("INCRBY")
        .arg(bytes_counter_key(tenant, namespace))
        .arg(-old_len)
        .query_async(&mut conn)
        .await
        .map_err(|err| map_err("INCRBY", err))?;
    Ok((true, used_after.max(0)))
}

pub(crate) async fn scan(
    client: &redis::Client,
    tenant: &str,
    namespace: &str,
    key_prefix: &str,
    limit: i32,
    page_token: &str,
) -> Result<(Vec<cache_pb::CacheItem>, String), Status> {
    let mut conn = connect(client).await?;
    let pattern = data_match(tenant, namespace, key_prefix);
    let cursor: u64 = page_token.parse().unwrap_or(0);
    // Named clamp: the caller limit is capped at MAX_SCAN_PAGE_LIMIT, never
    // passed through raw as the SCAN COUNT hint.
    let count = clamped_scan_count(limit);
    // SCAN, never KEYS: cursor-paged so a large keyspace never blocks Redis.
    let (next, keys): (u64, Vec<String>) = redis::cmd(SWEEP_COMMAND)
        .arg(cursor)
        .arg("MATCH")
        .arg(&pattern)
        .arg("COUNT")
        .arg(count)
        .query_async(&mut conn)
        .await
        .map_err(|err| map_err("SCAN", err))?;

    let mut items = Vec::with_capacity(keys.len());
    for full in &keys {
        let Some(local) = strip_data_prefix(full, tenant, namespace) else {
            continue;
        };
        let value: Option<Vec<u8>> = redis::cmd("GET")
            .arg(full)
            .query_async(&mut conn)
            .await
            .map_err(|err| map_err("GET", err))?;
        let Some(value) = value else { continue };
        let ttl: i64 = redis::cmd("TTL")
            .arg(full)
            .query_async(&mut conn)
            .await
            .map_err(|err| map_err("TTL", err))?;
        items.push(cache_pb::CacheItem {
            key: local.to_string(),
            value,
            ttl_remaining_seconds: ttl,
        });
    }
    // Redis SCAN signals end-of-iteration with cursor 0; surface "" then.
    let next_token = if next == 0 {
        String::new()
    } else {
        next.to_string()
    };
    Ok((items, next_token))
}

pub(crate) async fn create_namespace(
    client: &redis::Client,
    tenant: &str,
    namespace: &str,
    max_bytes: i64,
    default_ttl_seconds: i64,
) -> Result<(), Status> {
    let mut conn = connect(client).await?;
    let meta = serde_json::json!({
        "max_bytes": max_bytes,
        "default_ttl_seconds": default_ttl_seconds,
    });
    redis::cmd("SET")
        .arg(meta_key(tenant, namespace))
        .arg(meta.to_string())
        .query_async::<()>(&mut conn)
        .await
        .map_err(|err| map_err("SET meta", err))?;
    Ok(())
}

/// SCAN+DEL sweep of the ENTIRE namespace (data keys + counter + meta).
pub(crate) async fn flush_namespace(
    client: &redis::Client,
    tenant: &str,
    namespace: &str,
) -> Result<u64, Status> {
    let mut conn = connect(client).await?;
    let pattern = namespace_match_all(tenant, namespace);
    let mut cursor: u64 = 0;
    let mut deleted: u64 = 0;
    loop {
        // SCAN, never KEYS.
        let (next, keys): (u64, Vec<String>) = redis::cmd(SWEEP_COMMAND)
            .arg(cursor)
            .arg("MATCH")
            .arg(&pattern)
            .arg("COUNT")
            .arg(SWEEP_COUNT)
            .query_async(&mut conn)
            .await
            .map_err(|err| map_err("SCAN", err))?;
        // Count only data keys toward the reported invalidation total; the
        // counter/meta bookkeeping keys are swept but not "cache entries".
        let data_keys: Vec<&String> = keys
            .iter()
            .filter(|k| strip_data_prefix(k, tenant, namespace).is_some())
            .collect();
        if !keys.is_empty() {
            let removed: u64 = redis::cmd("DEL")
                .arg(&keys)
                .query_async(&mut conn)
                .await
                .map_err(|err| map_err("DEL", err))?;
            deleted = deleted.saturating_add(removed.min(data_keys.len() as u64));
        }
        if next == 0 {
            break;
        }
        cursor = next;
    }
    Ok(deleted)
}

pub(crate) struct NamespaceStats {
    pub used_bytes: i64,
    pub max_bytes: i64,
    pub item_count: u64,
}

pub(crate) async fn stats(
    client: &redis::Client,
    tenant: &str,
    namespace: &str,
) -> Result<NamespaceStats, Status> {
    let mut conn = connect(client).await?;
    let used_bytes = read_counter(&mut conn, tenant, namespace).await?;
    let meta = load_meta(&mut conn, tenant, namespace).await?;
    let pattern = data_match(tenant, namespace, "");
    let mut cursor: u64 = 0;
    let mut item_count: u64 = 0;
    loop {
        // SCAN, never KEYS.
        let (next, keys): (u64, Vec<String>) = redis::cmd(SWEEP_COMMAND)
            .arg(cursor)
            .arg("MATCH")
            .arg(&pattern)
            .arg("COUNT")
            .arg(SWEEP_COUNT)
            .query_async(&mut conn)
            .await
            .map_err(|err| map_err("SCAN", err))?;
        item_count = item_count.saturating_add(keys.len() as u64);
        if next == 0 {
            break;
        }
        cursor = next;
    }
    Ok(NamespaceStats {
        used_bytes,
        max_bytes: effective_max_bytes(meta.max_bytes),
        item_count,
    })
}

/// One bounded byte-counter reconciliation pass (leader-elected; called from
/// [`run_cache_invalidation_worker_once`]). Returns how many namespaces had
/// their counter recomputed.
///
/// Design + bounds:
///   - Namespaces are DISCOVERED by SCANning meta keys ([`meta_match_all`],
///     SCAN never KEYS) from a rotation cursor persisted under
///     [`reconcile_cursor_key`], so every namespace is eventually visited
///     round-robin across passes. Per pass: at most
///     [`RECONCILE_DISCOVERY_MAX_ROUNDS`] discovery SCAN round-trips and
///     [`RECONCILE_NAMESPACES_PER_PASS`] namespaces recomputed, each capped
///     at [`RECONCILE_MAX_KEYS_PER_NAMESPACE`] data keys.
///   - A cursor persisted across passes is still a valid SCAN cursor (Redis
///     cursors are stateless reverse-binary-iteration positions); a
///     concurrent keyspace resize can at worst skip or duplicate a namespace
///     within ONE rotation — the next rotation covers it, and the pass is
///     idempotent (it SETs an absolute recomputed value), so duplicates are
///     harmless.
///   - Eventual-consistency window: a namespace's counter is exact only at
///     the moment it is reconciled. Between visits, TTL expiries inflate it
///     (over-count = fails CLOSED toward `resource_exhausted`), and a
///     Set/Delete racing the recompute can leave the written sum stale by
///     that in-flight delta until the namespace's next turn. Both converge
///     on re-run.
pub(crate) async fn reconcile_bytes_counters_once(client: &redis::Client) -> Result<u64, Status> {
    let mut conn = connect(client).await?;
    // Resume the rotation where the previous pass stopped (0 = keyspace start).
    let stored: Option<String> = redis::cmd("GET")
        .arg(reconcile_cursor_key())
        .query_async(&mut conn)
        .await
        .map_err(|err| map_err("GET reconcile cursor", err))?;
    let mut cursor: u64 = stored.and_then(|v| v.parse().ok()).unwrap_or(0);
    let pattern = meta_match_all();
    let mut namespaces: Vec<(String, String)> = Vec::new();
    for _ in 0..RECONCILE_DISCOVERY_MAX_ROUNDS {
        // SCAN, never KEYS.
        let (next, keys): (u64, Vec<String>) = redis::cmd(SWEEP_COMMAND)
            .arg(cursor)
            .arg("MATCH")
            .arg(&pattern)
            .arg("COUNT")
            .arg(SWEEP_COUNT)
            .query_async(&mut conn)
            .await
            .map_err(|err| map_err("SCAN meta", err))?;
        namespaces.extend(keys.iter().filter_map(|key| parse_meta_key(key)));
        cursor = next;
        if cursor == 0 || namespaces.len() >= RECONCILE_NAMESPACES_PER_PASS {
            break;
        }
    }
    // SCAN may surface the same key more than once within an iteration; the
    // recompute is idempotent, but dedupe anyway so the per-pass namespace
    // budget is spent on DISTINCT namespaces.
    namespaces.sort();
    namespaces.dedup();
    namespaces.truncate(RECONCILE_NAMESPACES_PER_PASS);
    // Persist the rotation position so the NEXT pass continues from here.
    redis::cmd("SET")
        .arg(reconcile_cursor_key())
        .arg(cursor.to_string())
        .query_async::<()>(&mut conn)
        .await
        .map_err(|err| map_err("SET reconcile cursor", err))?;

    let mut reconciled: u64 = 0;
    for (tenant, namespace) in &namespaces {
        if reconcile_namespace_bytes(&mut conn, tenant, namespace)
            .await?
            .is_some()
        {
            reconciled = reconciled.saturating_add(1);
        }
    }
    Ok(reconciled)
}

/// Recompute ONE namespace's `__bytes__` counter from ground truth: SCAN the
/// namespace's data keys (only `:k:` keys — bookkeeping is excluded by the
/// pattern), STRLEN each, and SET the counter to the saturating sum
/// ([`reconciled_sum`]). Returns `Ok(None)` — counter left untouched — when
/// the namespace exceeds [`RECONCILE_MAX_KEYS_PER_NAMESPACE`]: a partial sum
/// would UNDER-count and fail open, whereas the stale counter only
/// over-counts. A key expiring between SCAN and STRLEN reads as length 0,
/// which is exactly its live usage.
async fn reconcile_namespace_bytes(
    conn: &mut redis::aio::MultiplexedConnection,
    tenant: &str,
    namespace: &str,
) -> Result<Option<i64>, Status> {
    let pattern = data_match(tenant, namespace, "");
    let mut cursor: u64 = 0;
    let mut lengths: Vec<i64> = Vec::new();
    loop {
        // SCAN, never KEYS.
        let (next, keys): (u64, Vec<String>) = redis::cmd(SWEEP_COMMAND)
            .arg(cursor)
            .arg("MATCH")
            .arg(&pattern)
            .arg("COUNT")
            .arg(SWEEP_COUNT)
            .query_async(&mut *conn)
            .await
            .map_err(|err| map_err("SCAN reconcile", err))?;
        for full in &keys {
            if strip_data_prefix(full, tenant, namespace).is_none() {
                continue;
            }
            if lengths.len() >= RECONCILE_MAX_KEYS_PER_NAMESPACE {
                tracing::debug!(
                    tenant = %tenant,
                    namespace = %namespace,
                    cap = RECONCILE_MAX_KEYS_PER_NAMESPACE,
                    "cache byte-counter reconciliation: namespace over the \
                     per-pass key cap; skipping (stale counter fails closed)"
                );
                return Ok(None);
            }
            let len: i64 = redis::cmd("STRLEN")
                .arg(full)
                .query_async(&mut *conn)
                .await
                .map_err(|err| map_err("STRLEN reconcile", err))?;
            lengths.push(len);
        }
        if next == 0 {
            break;
        }
        cursor = next;
    }
    let sum = reconciled_sum(&lengths);
    let current = read_counter(conn, tenant, namespace).await?;
    if lengths.is_empty() && current == 0 {
        // Nothing stored and no drift recorded: leave no ghost counter key
        // behind (e.g. right after a namespace flush swept the counter).
        return Ok(Some(0));
    }
    if current != sum {
        redis::cmd("SET")
            .arg(bytes_counter_key(tenant, namespace))
            .arg(sum)
            .query_async::<()>(&mut *conn)
            .await
            .map_err(|err| map_err("SET reconcile counter", err))?;
    }
    Ok(Some(sum))
}