saola-quaint 0.2.0-alpha.14

An abstraction layer for SQL databases (PostgreSQL, MySQL, SQLite, MSSQL)
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
use std::{
    hash::{BuildHasher, Hash, Hasher, RandomState},
    sync::Arc,
};

use async_trait::async_trait;
use lru_cache::LruCache;
use postgres_types::Type;
#[cfg(feature = "telemetry")]
use telemetry::formatting::strip_query_traceparent;
use tokio::sync::Mutex;
use tokio_postgres::{Client, Error, Statement};

use super::query::{PreparedQuery, QueryMetadata, TypedQuery};

/// Types that can be used as a cache for prepared queries and statements.
#[async_trait]
pub trait QueryCache: From<CacheSettings> + Send + Sync {
    /// The type that is returned when a prepared query is requested from the cache.
    type Query<'a>: PreparedQuery;

    /// Retrieve a prepared query.
    async fn get_query<'a>(&self, client: &Client, sql: &'a str, types: &[Type]) -> Result<Self::Query<'a>, Error>;

    /// Retrieve a prepared statement.
    ///
    /// This is useful in scenarios that require direct access to a prepared statement,
    /// e.g. describing a query.
    async fn get_statement(&self, client: &Client, sql: &str, types: &[Type]) -> Result<Statement, Error>;
}

/// A no-op cache that creates a new prepared statement for every requested query.
/// Useful when we don't need caching.
#[derive(Debug, Default)]
pub struct NoOpCache;

#[async_trait]
impl QueryCache for NoOpCache {
    type Query<'a> = Statement;

    #[inline]
    async fn get_query<'a>(&self, client: &Client, sql: &'a str, types: &[Type]) -> Result<Statement, Error> {
        self.get_statement(client, sql, types).await
    }

    #[inline]
    async fn get_statement(&self, client: &Client, sql: &str, types: &[Type]) -> Result<Statement, Error> {
        client.prepare_typed(sql, types).await
    }
}

impl From<CacheSettings> for NoOpCache {
    fn from(_: CacheSettings) -> Self {
        Self
    }
}

/// An LRU cache that creates a prepared statement for every query that is not in the cache.
#[derive(Debug)]
pub struct PreparedStatementLruCache {
    cache: InnerLruCache<Statement>,
}

impl PreparedStatementLruCache {
    pub fn with_capacity(capacity: usize) -> Self {
        Self {
            cache: InnerLruCache::with_capacity(capacity),
        }
    }
}

#[async_trait]
impl QueryCache for PreparedStatementLruCache {
    type Query<'a> = Statement;

    #[inline]
    async fn get_query<'a>(&self, client: &Client, sql: &'a str, types: &[Type]) -> Result<Statement, Error> {
        self.get_statement(client, sql, types).await
    }

    async fn get_statement(&self, client: &Client, sql: &str, types: &[Type]) -> Result<Statement, Error> {
        match self.cache.get(sql, types).await {
            Some(statement) => Ok(statement),
            None => {
                let stmt = client.prepare_typed(sql, types).await?;
                self.cache.insert(sql, types, stmt.clone()).await;
                Ok(stmt)
            }
        }
    }
}

impl From<CacheSettings> for PreparedStatementLruCache {
    fn from(settings: CacheSettings) -> Self {
        Self::with_capacity(settings.capacity)
    }
}

/// An LRU cache that creates and stores query type information rather than prepared statements.
/// Queries are identified by their content with tracing information removed (which makes it
/// possible to cache traced queries at all) and returned as instances of [`TypedQuery`]. The
/// caching behavior is implemented in [`get_query`](Self::get_query), while statements returned
/// from [`get_statement`](Self::get_statement) are always freshly prepared, because statements
/// cannot be re-used when tracing information is present.
#[derive(Debug)]
pub struct TracingLruCache {
    cache: InnerLruCache<Arc<QueryMetadata>>,
}

impl TracingLruCache {
    pub fn with_capacity(capacity: usize) -> Self {
        Self {
            cache: InnerLruCache::with_capacity(capacity),
        }
    }
}

#[cfg(not(feature = "telemetry"))]
fn strip_query_traceparent(sql: &str) -> &str {
    sql
}

#[async_trait]
impl QueryCache for TracingLruCache {
    type Query<'a> = TypedQuery<'a>;

    async fn get_query<'a>(&self, client: &Client, sql: &'a str, types: &[Type]) -> Result<TypedQuery<'a>, Error> {
        let sql_without_traceparent = strip_query_traceparent(sql);

        let metadata = match self.cache.get(sql_without_traceparent, types).await {
            Some(metadata) => metadata,
            None => {
                let stmt = client.prepare_typed(sql_without_traceparent, types).await?;
                let metadata = Arc::new(QueryMetadata::from(&stmt));
                self.cache
                    .insert(sql_without_traceparent, types, metadata.clone())
                    .await;
                metadata
            }
        };
        Ok(TypedQuery::from_sql_and_metadata(sql, metadata))
    }

    async fn get_statement(&self, client: &Client, sql: &str, types: &[Type]) -> Result<Statement, Error> {
        client.prepare_typed(sql, types).await
    }
}

impl From<CacheSettings> for TracingLruCache {
    fn from(settings: CacheSettings) -> Self {
        Self::with_capacity(settings.capacity)
    }
}

/// Settings related to query caching.
#[derive(Debug)]
pub struct CacheSettings {
    pub capacity: usize,
}

/// Key uniquely representing an SQL statement in the prepared statements cache.
#[derive(Debug, PartialEq, Eq, Hash)]
struct QueryKey(u64);

impl QueryKey {
    fn new<S: BuildHasher>(st: &S, sql: &str, params: &[Type]) -> Self {
        Self(st.hash_one((sql, params)))
    }
}

#[derive(Debug)]
struct InnerLruCache<V> {
    cache: Mutex<LruCache<QueryKey, V, NoOpHasherBuilder>>,
    state: RandomState,
}

impl<V> InnerLruCache<V> {
    fn with_capacity(capacity: usize) -> Self {
        Self {
            cache: Mutex::new(LruCache::with_hasher(capacity, NoOpHasherBuilder)),
            state: RandomState::new(),
        }
    }

    async fn get(&self, sql: &str, types: &[Type]) -> Option<V>
    where
        V: Clone,
    {
        let mut cache = self.cache.lock().await;
        let capacity = cache.capacity();
        let stored = cache.len();

        let key = QueryKey::new(&self.state, sql, types);
        // we call `get_mut` because LRU requires mutable access for lookups
        match cache.get_mut(&key) {
            Some(value) => {
                tracing::trace!(
                    message = "query cache hit",
                    query = sql,
                    capacity = capacity,
                    stored = stored,
                );
                Some(value.clone())
            }
            None => {
                tracing::trace!(
                    message = "query cache miss",
                    query = sql,
                    capacity = capacity,
                    stored = stored,
                );
                None
            }
        }
    }

    pub async fn insert(&self, sql: &str, types: &[Type], value: V) {
        let key = QueryKey::new(&self.state, sql, types);
        self.cache.lock().await.insert(key, value);
    }
}

struct NoOpHasherBuilder;

impl BuildHasher for NoOpHasherBuilder {
    type Hasher = NoOpHasher;

    fn build_hasher(&self) -> Self::Hasher {
        NoOpHasher(None)
    }
}

/// A hasher that expects to be called with a single u64 and returns it as the hash.
struct NoOpHasher(Option<u64>);

impl Hasher for NoOpHasher {
    fn finish(&self) -> u64 {
        self.0.expect("NoopHasher should have been called with a single u64")
    }

    fn write(&mut self, _bytes: &[u8]) {
        panic!("NoopHasher should only be called with u64")
    }

    fn write_u64(&mut self, i: u64) {
        assert!(self.0.is_none(), "NoopHasher should only be called once");
        self.0 = Some(i);
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    use std::future::Future;

    pub(crate) use crate::connector::postgres::url::PostgresNativeUrl;
    use crate::{
        connector::{MakeTlsConnectorManager, PostgresFlavour},
        tests::test_api::postgres::CONN_STR,
    };
    use url::Url;

    #[tokio::test]
    async fn noop_cache_returns_new_queries_every_time() {
        run_with_client(|client| async move {
            let cache = NoOpCache;
            let sql = "SELECT $1";
            let types = [Type::INT4];

            let stmt1 = cache.get_query(&client, sql, &types).await.unwrap();
            let stmt2 = cache.get_query(&client, sql, &types).await.unwrap();
            assert_ne!(stmt1.name(), stmt2.name());
        })
        .await;
    }

    #[tokio::test]
    async fn noop_cache_returns_new_statements_every_time() {
        run_with_client(|client| async move {
            let cache = NoOpCache;
            let sql = "SELECT $1";
            let types = [Type::INT4];

            let stmt1 = cache.get_statement(&client, sql, &types).await.unwrap();
            let stmt2 = cache.get_statement(&client, sql, &types).await.unwrap();
            assert_ne!(stmt1.name(), stmt2.name());
        })
        .await;
    }

    #[tokio::test]
    async fn prepared_statement_lru_cache_reuses_queries_within_capacity() {
        run_with_client(|client| async move {
            let cache = PreparedStatementLruCache::with_capacity(3);
            let sql = "SELECT $1";
            let types = [Type::INT4];

            let stmt1 = cache.get_query(&client, sql, &types).await.unwrap();
            let stmt2 = cache.get_query(&client, sql, &types).await.unwrap();
            assert_eq!(stmt1.name(), stmt2.name());

            // fill the cache with different types, causing the first query to be evicted
            for typ in [Type::INT8, Type::INT4_ARRAY, Type::INT8_ARRAY] {
                cache.get_query(&client, sql, &[typ]).await.unwrap();
            }

            // the old statement should be re-created
            let stmt3 = cache.get_query(&client, sql, &types).await.unwrap();
            assert_ne!(stmt1.name(), stmt3.name());
        })
        .await;
    }

    #[tokio::test]
    async fn prepared_statement_lru_cache_reuses_statements_within_capacity() {
        run_with_client(|client| async move {
            let cache = PreparedStatementLruCache::with_capacity(3);
            let sql = "SELECT $1";
            let types = [Type::INT4];

            let stmt1 = cache.get_statement(&client, sql, &types).await.unwrap();
            let stmt2 = cache.get_statement(&client, sql, &types).await.unwrap();
            assert_eq!(stmt1.name(), stmt2.name());

            // fill the cache with different types, causing the first query to be evicted
            for typ in [Type::INT8, Type::INT4_ARRAY, Type::INT8_ARRAY] {
                cache.get_query(&client, sql, &[typ]).await.unwrap();
            }

            // the old statement should be re-created
            let stmt3 = cache.get_statement(&client, sql, &types).await.unwrap();
            assert_ne!(stmt1.name(), stmt3.name());
        })
        .await;
    }

    #[tokio::test]
    async fn tracing_lru_cache_reuses_queries_within_capacity() {
        run_with_client(|client| async move {
            let cache = TracingLruCache::with_capacity(3);
            let sql = "SELECT $1";
            let types = [Type::INT4];

            let q1 = cache.get_query(&client, sql, &types).await.unwrap();
            let q2 = cache.get_query(&client, sql, &types).await.unwrap();
            assert!(
                Arc::ptr_eq(&q1.metadata, &q2.metadata),
                "q1 and q2 should re-use the same metadata"
            );

            // fill the cache with different types, causing the first query to be evicted
            for typ in [Type::INT8, Type::INT4_ARRAY, Type::INT8_ARRAY] {
                cache.get_query(&client, sql, &[typ]).await.unwrap();
            }

            // the old query should be re-created
            let q3 = cache.get_query(&client, sql, &types).await.unwrap();
            assert!(
                !Arc::ptr_eq(&q1.metadata, &q3.metadata),
                "q1 and q3 should not re-use the same metadata"
            );
        })
        .await;
    }

    #[tokio::test]
    async fn tracing_lru_cache_reuses_queries_with_different_traceparent() {
        run_with_client(|client| async move {
            let cache = TracingLruCache::with_capacity(1);
            let sql1 = "SELECT $1 /* traceparent=00-0af7651916cd43dd8448eb211c80319c-b7ad6b7169203331-01 */";
            let sql2 = "SELECT $1 /* traceparent=00-0af7651916cd43dd8448eb211c80319c-b7ad6b7169203331-02 */";
            let types = [Type::INT4];

            let q1 = cache.get_query(&client, sql1, &types).await.unwrap();
            assert_eq!(q1.sql, sql1);
            let q2 = cache.get_query(&client, sql2, &types).await.unwrap();
            // the requested query traceparent should be preserved
            assert_eq!(q2.sql, sql2);

            assert!(
                Arc::ptr_eq(&q1.metadata, &q2.metadata),
                "q1 and q2 should re-use the same metadata"
            );
        })
        .await;
    }

    #[tokio::test]
    async fn tracing_lru_cache_returns_new_statements_every_time() {
        run_with_client(|client| async move {
            let cache = TracingLruCache::with_capacity(1);
            let sql = "SELECT $1";
            let types = [Type::INT4];

            let q1 = cache.get_statement(&client, sql, &types).await.unwrap();
            let q2 = cache.get_statement(&client, sql, &types).await.unwrap();
            assert_ne!(q1.name(), q2.name());
        })
        .await;
    }

    #[tokio::test]
    async fn zero_sized_tracing_lru_cache_returns_new_metadata_every_time() {
        run_with_client(|client| async move {
            let cache = TracingLruCache::with_capacity(0);
            let sql = "SELECT $1";
            let types = [Type::INT4];

            let q1 = cache.get_query(&client, sql, &types).await.unwrap();
            let q2 = cache.get_query(&client, sql, &types).await.unwrap();
            assert!(
                !Arc::ptr_eq(&q1.metadata, &q2.metadata),
                "q1 and q2 should not re-use the same metadata"
            );
        })
        .await;
    }

    #[test]
    fn noop_hasher_returns_the_same_hash_the_input() {
        assert_eq!(NoOpHasherBuilder.hash_one(0xdeadc0deu64), 0xdeadc0de);
        assert_eq!(NoOpHasherBuilder.hash_one(0xcafeu64), 0xcafe);
    }

    #[test]
    #[should_panic(expected = "NoopHasher should only be called with u64")]
    fn noop_hasher_doesnt_accept_non_u64_input() {
        NoOpHasherBuilder.hash_one("hello");
    }

    async fn run_with_client<Func, Fut>(test: Func)
    where
        Func: FnOnce(Client) -> Fut,
        Fut: Future<Output = ()>,
    {
        let url = Url::parse(&CONN_STR).unwrap();
        let mut pg_url = PostgresNativeUrl::new(url).unwrap();
        pg_url.set_flavour(PostgresFlavour::Postgres);

        let tls_manager = MakeTlsConnectorManager::new(pg_url.clone());
        let tls = tls_manager.get_connector().await.unwrap();

        let (client, conn) = pg_url.to_config().connect(tls).await.unwrap();

        let set = tokio::task::LocalSet::new();
        set.spawn_local(conn);
        set.run_until(test(client)).await
    }
}