udb 0.4.15

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
//! Redis cache executor for generic dispatch.

use redis::AsyncCommands;
use serde_json::{Value as Json, json};

use crate::runtime::executor_utils::{
    backend_transport_status, build_probe, capability_status, invalid_argument_fields,
};
use crate::runtime::executors::{
    BackendExecutor, BackendHealth, BackendProbe, MutationExecutor, ObjectExecutor, QueryExecutor,
    ResourceAdminExecutor, SearchExecutor,
};

#[derive(Clone)]
pub(crate) struct RedisExecutor {
    client: redis::Client,
    // Lazily-established multiplexed connection, shared across requests and clones.
    // Cloning a `MultiplexedConnection` is cheap (one shared actor/socket), so each
    // call clones the cached handle instead of opening a fresh connection (#76).
    conn: std::sync::Arc<tokio::sync::OnceCell<redis::aio::MultiplexedConnection>>,
}

impl std::fmt::Debug for RedisExecutor {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("RedisExecutor").finish_non_exhaustive()
    }
}

impl crate::runtime::backend_context::BackendContextEnforcer for RedisExecutor {
    fn backend_label(&self) -> &str {
        "redis"
    }

    fn enforce(
        &self,
        ctx: &crate::runtime::backend_context::AppliedContext,
    ) -> crate::runtime::backend_context::ContextEffect {
        // The Redis IR compiler emits keys of the form
        // `udb:{project}:{tenant}:<message>:<pk>`. The active context
        // is therefore enforced at key-construction time — a request
        // can't read or delete another tenant's key without going
        // around the broker.
        crate::runtime::backend_context::enforce_with_mechanism(
            ctx,
            "key namespace prefix udb:{project}:{tenant}:",
        )
    }
}

impl RedisExecutor {
    pub(crate) fn new(client: redis::Client) -> Self {
        Self {
            client,
            conn: std::sync::Arc::new(tokio::sync::OnceCell::new()),
        }
    }

    async fn connection(&self) -> Result<redis::aio::MultiplexedConnection, tonic::Status> {
        let conn = self
            .conn
            .get_or_try_init(|| self.client.get_multiplexed_async_connection())
            .await
            .map_err(|err| backend_transport_status("redis", "connection", err))?;
        Ok(conn.clone())
    }
}

impl BackendHealth for RedisExecutor {
    async fn ping(&self) -> Result<(), String> {
        let mut conn = self.connection().await.map_err(|err| err.to_string())?;
        redis::cmd("PING")
            .query_async::<String>(&mut conn)
            .await
            .map(|_| ())
            .map_err(|err| err.to_string())
    }
}

fn redis_invalid_field(
    field: impl Into<String>,
    description: impl Into<String>,
    message: impl Into<String>,
) -> tonic::Status {
    invalid_argument_fields(message, [(field.into(), description.into())])
}

fn invalid_redis_request_json_status(err: serde_json::Error) -> tonic::Status {
    redis_invalid_field(
        "request_json",
        "must be valid JSON for Redis generic dispatch",
        format!("invalid request json: {err}"),
    )
}

fn redis_required_field_status(field: &'static str, message: &'static str) -> tonic::Status {
    redis_invalid_field(
        field,
        format!("{field} is required for this Redis operation"),
        message,
    )
}

fn unsupported_redis_operation_status(kind: &'static str, operation: &str) -> tonic::Status {
    redis_invalid_field(
        "operation",
        format!("unsupported Redis {kind} operation"),
        format!("unsupported Redis {kind} operation '{operation}'"),
    )
}

impl QueryExecutor for RedisExecutor {
    /// `{"operation":"get|mget|exists","key":"k","keys":["a"]}`.
    async fn query(&self, request_json: &str) -> Result<String, tonic::Status> {
        let spec: Json =
            serde_json::from_str(request_json).map_err(invalid_redis_request_json_status)?;
        let operation = spec
            .get("operation")
            .and_then(Json::as_str)
            .unwrap_or("get");
        let mut conn = self.connection().await?;
        match operation {
            "get" | "cache_get" | "read_through" => {
                let key = spec
                    .get("key")
                    .and_then(Json::as_str)
                    .ok_or_else(|| redis_required_field_status("key", "key is required"))?;
                let value: Option<Vec<u8>> = conn
                    .get(key)
                    .await
                    .map_err(|err| backend_transport_status("redis", "GET", err))?;
                Ok(json!({
                    "key": key,
                    "hit": value.is_some(),
                    "value": value.and_then(|bytes| String::from_utf8(bytes).ok())
                })
                .to_string())
            }
            "mget" => {
                let keys = spec
                    .get("keys")
                    .and_then(Json::as_array)
                    .ok_or_else(|| {
                        redis_invalid_field(
                            "keys",
                            "must be an array for Redis mget",
                            "keys must be an array",
                        )
                    })?
                    .iter()
                    .filter_map(Json::as_str)
                    .collect::<Vec<_>>();
                let values: Vec<Option<Vec<u8>>> = conn
                    .get(&keys)
                    .await
                    .map_err(|err| backend_transport_status("redis", "MGET", err))?;
                Ok(json!({
                    "values": keys.into_iter().zip(values.into_iter()).map(|(key, value)| {
                        json!({
                            "key": key,
                            "hit": value.is_some(),
                            "value": value.and_then(|bytes| String::from_utf8(bytes).ok())
                        })
                    }).collect::<Vec<_>>()
                })
                .to_string())
            }
            "exists" => {
                let key = spec
                    .get("key")
                    .and_then(Json::as_str)
                    .ok_or_else(|| redis_required_field_status("key", "key is required"))?;
                let exists: bool = conn
                    .exists(key)
                    .await
                    .map_err(|err| backend_transport_status("redis", "EXISTS", err))?;
                Ok(json!({ "key": key, "exists": exists }).to_string())
            }
            "scan" | "cache_scan" => {
                let pattern = spec.get("pattern").and_then(Json::as_str).unwrap_or("*");
                let cursor = spec
                    .get("cursor")
                    .and_then(|value| {
                        value
                            .as_u64()
                            .or_else(|| value.as_str().and_then(|raw| raw.parse::<u64>().ok()))
                    })
                    .unwrap_or(0);
                let limit = spec
                    .get("limit")
                    .and_then(Json::as_u64)
                    .unwrap_or(10)
                    .max(1);
                let (next_cursor, keys): (u64, Vec<String>) = redis::cmd("SCAN")
                    .arg(cursor)
                    .arg("MATCH")
                    .arg(pattern)
                    .arg("COUNT")
                    .arg(limit)
                    .query_async(&mut conn)
                    .await
                    .map_err(|err| backend_transport_status("redis", "SCAN", err))?;
                let values: Vec<Option<Vec<u8>>> = if keys.is_empty() {
                    Vec::new()
                } else {
                    conn.get(&keys)
                        .await
                        .map_err(|err| backend_transport_status("redis", "MGET after SCAN", err))?
                };
                let entries = keys
                    .into_iter()
                    .zip(values.into_iter())
                    .map(|(key, value)| {
                        json!({
                            "key": key,
                            "value": value.and_then(|bytes| String::from_utf8(bytes).ok())
                        })
                    })
                    .collect::<Vec<_>>();
                let next_page_token = if next_cursor == 0 {
                    String::new()
                } else {
                    next_cursor.to_string()
                };
                Ok(json!({
                    "entries": entries,
                    "next_page_token": next_page_token
                })
                .to_string())
            }
            other => Err(unsupported_redis_operation_status("query", other)),
        }
    }
}

impl MutationExecutor for RedisExecutor {
    /// `{"operation":"set|delete|expire","key":"k","value":"v","ttl":60}`.
    async fn mutate(&self, request_json: &str) -> Result<String, tonic::Status> {
        let spec: Json =
            serde_json::from_str(request_json).map_err(invalid_redis_request_json_status)?;
        let operation = spec
            .get("operation")
            .and_then(Json::as_str)
            .unwrap_or("set");
        let key = spec
            .get("key")
            .and_then(Json::as_str)
            .ok_or_else(|| redis_required_field_status("key", "key is required"))?;
        let mut conn = self.connection().await?;
        match operation {
            "set" | "cache_set" | "write_through" => {
                let value = spec
                    .get("value")
                    .map(|value| {
                        value
                            .as_str()
                            .map(ToString::to_string)
                            .unwrap_or_else(|| value.to_string())
                    })
                    .ok_or_else(|| redis_required_field_status("value", "value is required"))?;
                if let Some(ttl) = spec.get("ttl").and_then(Json::as_u64) {
                    conn.set_ex::<_, _, ()>(key, value, ttl)
                        .await
                        .map_err(|err| backend_transport_status("redis", "SETEX", err))?;
                } else {
                    conn.set::<_, _, ()>(key, value)
                        .await
                        .map_err(|err| backend_transport_status("redis", "SET", err))?;
                }
                Ok(json!({ "affected_rows": 1 }).to_string())
            }
            "delete" | "del" | "cache_delete" => {
                let deleted: u64 = conn
                    .del(key)
                    .await
                    .map_err(|err| backend_transport_status("redis", "DEL", err))?;
                Ok(json!({ "affected_rows": deleted }).to_string())
            }
            "expire" => {
                let ttl = spec
                    .get("ttl")
                    .and_then(Json::as_i64)
                    .ok_or_else(|| redis_required_field_status("ttl", "ttl is required"))?;
                let changed: bool = conn
                    .expire(key, ttl)
                    .await
                    .map_err(|err| backend_transport_status("redis", "EXPIRE", err))?;
                Ok(json!({ "affected_rows": i64::from(changed) }).to_string())
            }
            other => Err(unsupported_redis_operation_status("mutation", other)),
        }
    }
}

impl SearchExecutor for RedisExecutor {
    async fn search(&self, _request_json: &str) -> Result<String, tonic::Status> {
        Err(capability_status(
            "redis",
            "search",
            "vector_search",
            "redis does not support vector search",
        ))
    }
}

impl ObjectExecutor for RedisExecutor {
    async fn get_object(&self, _request_json: &str) -> Result<Vec<u8>, tonic::Status> {
        Err(capability_status(
            "redis",
            "get_object",
            "object_store",
            "redis is not an object store",
        ))
    }

    async fn put_object(
        &self,
        _request_json: &str,
        _bytes: Vec<u8>,
    ) -> Result<String, tonic::Status> {
        Err(capability_status(
            "redis",
            "put_object",
            "object_store",
            "redis is not an object store",
        ))
    }
}

impl ResourceAdminExecutor for RedisExecutor {
    async fn ensure_resource(
        &self,
        _resource_name: &str,
        _spec_json: &str,
    ) -> Result<(), tonic::Status> {
        Err(capability_status(
            "redis",
            "ensure_resource",
            "resource_lifecycle",
            "redis does not expose resource lifecycle operations",
        ))
    }

    async fn drop_resource(&self, _resource_name: &str) -> Result<(), tonic::Status> {
        Err(capability_status(
            "redis",
            "drop_resource",
            "resource_lifecycle",
            "redis does not expose resource lifecycle operations",
        ))
    }

    async fn list_resources(&self) -> Result<Vec<String>, tonic::Status> {
        Err(capability_status(
            "redis",
            "list_resources",
            "resource_lifecycle",
            "redis does not expose resource lifecycle operations",
        ))
    }
}

impl BackendExecutor for RedisExecutor {
    async fn transaction(&self, _request_json: &str) -> Result<String, tonic::Status> {
        Err(capability_status(
            "redis",
            "transaction",
            "transactions",
            "redis generic dispatch does not expose MULTI/EXEC transactions",
        ))
    }

    async fn probe(&self) -> Result<BackendProbe, tonic::Status> {
        Ok(build_probe(
            "redis",
            <Self as BackendHealth>::ping(self).await,
        ))
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::proto::{ErrorDetail, ErrorKind};
    use crate::runtime::executor_utils::ERROR_DETAIL_METADATA_KEY;

    fn decode_detail(status: &tonic::Status) -> ErrorDetail {
        let raw = status
            .metadata()
            .get_bin(ERROR_DETAIL_METADATA_KEY)
            .expect("typed detail trailer is present");
        crate::runtime::executor_utils::decode_error_detail_from_raw(&raw)
    }

    fn assert_single_field(status: &tonic::Status, field: &str) {
        let detail = decode_detail(status);
        assert_eq!(detail.kind, ErrorKind::Validation as i32);
        assert_eq!(detail.field_violations.len(), 1);
        assert_eq!(detail.field_violations[0].field, field);
    }

    #[test]
    fn redis_request_json_validation_carries_field_violation() {
        let err = serde_json::from_str::<Json>("{")
            .map_err(invalid_redis_request_json_status)
            .unwrap_err();
        assert_eq!(err.code(), tonic::Code::InvalidArgument);
        assert!(err.message().starts_with("invalid request json:"));
        assert_single_field(&err, "request_json");
    }

    #[test]
    fn redis_required_field_validation_carries_field_violations() {
        let key = redis_required_field_status("key", "key is required");
        assert_eq!(key.message(), "key is required");
        assert_single_field(&key, "key");

        let value = redis_required_field_status("value", "value is required");
        assert_eq!(value.message(), "value is required");
        assert_single_field(&value, "value");

        let ttl = redis_required_field_status("ttl", "ttl is required");
        assert_eq!(ttl.message(), "ttl is required");
        assert_single_field(&ttl, "ttl");

        let keys = redis_invalid_field(
            "keys",
            "must be an array for Redis mget",
            "keys must be an array",
        );
        assert_eq!(keys.message(), "keys must be an array");
        assert_single_field(&keys, "keys");
    }

    #[test]
    fn redis_unsupported_operation_validation_carries_field_violation() {
        let query = unsupported_redis_operation_status("query", "bad");
        assert_eq!(query.message(), "unsupported Redis query operation 'bad'");
        assert_single_field(&query, "operation");

        let mutation = unsupported_redis_operation_status("mutation", "bad");
        assert_eq!(
            mutation.message(),
            "unsupported Redis mutation operation 'bad'"
        );
        assert_single_field(&mutation, "operation");
    }
}