rustis 0.25.0

Redis async driver for Rust
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
use crate::{
    ClientError, Error, ErrorKind, Result, TimeoutKind,
    cache::Cache,
    client::Client,
    commands::{
        ClientTrackingOptions, ClientTrackingStatus, ClusterCommands, ConnectionCommands,
        FlushingMode, HashCommands, ServerCommands, StringCommands,
    },
    network::{sleep, timeout},
    resp::cmd,
    tests::{get_cluster_test_client, get_default_config, get_test_client, log_try_init},
};
use serial_test::serial;
use std::time::Duration;

#[tokio::test]
#[serial]
async fn cache_get() -> Result<()> {
    log_try_init();
    let client1 = Client::connect("redis://127.0.0.1?connection_name=client1").await?;
    let client2 = Client::connect("redis://127.0.0.1?connection_name=client2").await?;

    client2.flushall(FlushingMode::Sync).await?;
    client1
        .client_tracking(ClientTrackingStatus::Off, ClientTrackingOptions::default())
        .await?;

    client2.set("key", "value").await?;

    let cache = Cache::new(client1.clone(), 60, ClientTrackingOptions::default()).await?;

    let value: String = cache.get("key").await?;
    assert_eq!("value", value);

    let value: String = cache.get("key").await?;
    assert_eq!("value", value);

    client2.set("key", "new_value").await?;

    sleep(Duration::from_millis(100)).await;

    let value: String = cache.get("key").await?;
    assert_eq!("new_value", value);

    let value: String = cache.get("key").await?;
    assert_eq!("new_value", value);

    Ok(())
}

#[tokio::test]
#[serial]
async fn cache_key_serializing_to_zero_args_errors_instead_of_panicking() -> Result<()> {
    log_try_init();
    let client = Client::connect("redis://127.0.0.1?connection_name=cache_bad_key").await?;
    let cache = Cache::new(client, 60, ClientTrackingOptions::default()).await?;

    // A key that serializes to zero arguments (e.g. `None`) must surface a clean
    // error rather than panicking the caller thread.
    let result: Result<String> = cache.get(None::<String>).await;
    let error = result.unwrap_err();
    assert!(
        matches!(
            error.kind(),
            ErrorKind::Client(ClientError::InvalidCacheKey)
        ),
        "expected InvalidCacheKey, got {error:?}"
    );

    Ok(())
}

#[tokio::test]
#[serial]
async fn cache_hash() -> Result<()> {
    log_try_init();
    let client1 = Client::connect("redis://127.0.0.1?connection_name=client1").await?;
    let client2 = Client::connect("redis://127.0.0.1?connection_name=client2").await?;

    client2.flushall(FlushingMode::Sync).await?;
    client1
        .client_tracking(ClientTrackingStatus::Off, ClientTrackingOptions::default())
        .await?;

    client2
        .hset("key", [("field1", "value1"), ("field2", "value2")])
        .await?;

    let cache = Cache::new(client1.clone(), 60, ClientTrackingOptions::default()).await?;

    let mut values: Vec<(String, String)> = cache.hgetall("key").await?;
    values.sort_by(|(f1, _), (f2, _)| f1.cmp(f2));
    assert_eq!(
        vec![
            ("field1".to_string(), "value1".to_string()),
            ("field2".to_string(), "value2".to_string())
        ],
        values
    );

    let mut values: Vec<(String, String)> = cache.hgetall("key").await?;
    values.sort_by(|(f1, _), (f2, _)| f1.cmp(f2));
    assert_eq!(
        vec![
            ("field1".to_string(), "value1".to_string()),
            ("field2".to_string(), "value2".to_string())
        ],
        values
    );

    let len = cache.hlen("key").await?;
    assert_eq!(2, len);

    let len = cache.hlen("key").await?;
    assert_eq!(2, len);

    client2
        .hset("key", [("field1", "value11"), ("field2", "value22")])
        .await?;

    // The invalidation reaches `client1` on its own connection and is applied by
    // a separate task, so `client2`'s write completing says nothing about when
    // the entry is evicted. Wait for it, as `cache_get` and
    // `cache_survives_reconnection` already do.
    sleep(Duration::from_millis(100)).await;

    let mut values: Vec<(String, String)> = cache.hgetall("key").await?;
    values.sort_by(|(f1, _), (f2, _)| f1.cmp(f2));
    assert_eq!(
        vec![
            ("field1".to_string(), "value11".to_string()),
            ("field2".to_string(), "value22".to_string())
        ],
        values
    );

    let mut values: Vec<(String, String)> = cache.hgetall("key").await?;
    values.sort_by(|(f1, _), (f2, _)| f1.cmp(f2));
    assert_eq!(
        vec![
            ("field1".to_string(), "value11".to_string()),
            ("field2".to_string(), "value22".to_string())
        ],
        values
    );

    let len = cache.hlen("key").await?;
    assert_eq!(2, len);

    let len = cache.hlen("key").await?;
    assert_eq!(2, len);

    Ok(())
}

#[tokio::test]
#[serial]
async fn cache_mget() -> Result<()> {
    log_try_init();
    let client1 = Client::connect("redis://127.0.0.1?connection_name=client1").await?;
    let client2 = Client::connect("redis://127.0.0.1?connection_name=client2").await?;

    client2.flushall(FlushingMode::Sync).await?;
    client1
        .client_tracking(ClientTrackingStatus::Off, ClientTrackingOptions::default())
        .await?;

    let cache = Cache::new(client1.clone(), 60, ClientTrackingOptions::default()).await?;

    client2
        .mset([("key1", "value1"), ("key2", "value2")])
        .await?;

    assert_eq!("value1", cache.get::<String>("key1").await?);

    let values: Vec<String> = cache.mget(["key1", "key2"]).await?;
    assert_eq!(vec!["value1".to_string(), "value2".to_string()], values);

    assert_eq!("value1", cache.get::<String>("key1").await?);
    assert_eq!("value2", cache.get::<String>("key2").await?);

    let values: Vec<String> = cache.mget(["key1", "key2"]).await?;
    assert_eq!(vec!["value1".to_string(), "value2".to_string()], values);

    Ok(())
}

/// Server-side tracking is per-connection state that dies with the socket. After a
/// reconnection, the cache must both drop everything it holds — invalidations missed
/// during the outage are unrecoverable — and re-arm tracking so later writes keep
/// invalidating it.
#[tokio::test]
#[serial]
async fn cache_survives_reconnection() -> Result<()> {
    log_try_init();
    let client1 = Client::connect("redis://127.0.0.1?connection_name=client1").await?;
    let client2 = Client::connect("redis://127.0.0.1?connection_name=client2").await?;

    client2.flushall(FlushingMode::Sync).await?;
    client1
        .client_tracking(ClientTrackingStatus::Off, ClientTrackingOptions::default())
        .await?;

    client2.set("key", "value").await?;

    let cache = Cache::new(client1.clone(), 60, ClientTrackingOptions::default()).await?;
    assert_eq!("value", cache.get::<String>("key").await?);

    // Close the connection under the cache, then write the key while it is down:
    // the invalidation for that write can never be delivered.
    client1.send_and_forget(cmd("PING").kill_connection_on_read(1), None)?;
    client2.set("key", "new_value").await?;

    sleep(Duration::from_millis(2500)).await;

    assert_eq!(
        "new_value",
        cache.get::<String>("key").await?,
        "a value written during the outage must not be served from the cache"
    );

    // Tracking must be armed again on the new connection, otherwise invalidations
    // silently stop for the rest of the client's life.
    client2.set("key", "newer_value").await?;
    sleep(Duration::from_millis(100)).await;

    assert_eq!(
        "newer_value",
        cache.get::<String>("key").await?,
        "invalidations must still be delivered after a reconnection"
    );

    Ok(())
}

/// Tracking is per-connection and each node only invalidates the keys it holds,
/// so a cluster cache is only correct if tracking reached every node. Keys are
/// picked to land on more than one shard: `key1` hashes into the middle range,
/// `key2` and `key3` into the first.
#[tokio::test]
#[serial]
async fn cluster_cache_is_invalidated_for_keys_on_every_shard() -> Result<()> {
    log_try_init();
    let cached_client = get_cluster_test_client().await?;
    let writer = get_cluster_test_client().await?;

    writer.flushall(FlushingMode::Sync).await?;
    for key in ["key1", "key2", "key3"] {
        writer.set(key, "before").await?;
    }

    // The keys must not all live on the same shard, otherwise the test would pass
    // with tracking armed on a single node.
    let mut slots = Vec::new();
    for key in ["key1", "key2", "key3"] {
        slots.push(writer.cluster_keyslot(key).await?);
    }
    assert!(
        slots.iter().any(|s| *s > 5460),
        "the probe keys must span more than one shard, slots: {slots:?}"
    );

    let cache = Cache::new(cached_client.clone(), 60, ClientTrackingOptions::default()).await?;

    for key in ["key1", "key2", "key3"] {
        assert_eq!("before", cache.get::<String>(key).await?);
    }

    for key in ["key1", "key2", "key3"] {
        writer.set(key, "after").await?;
    }
    sleep(Duration::from_millis(200)).await;

    for key in ["key1", "key2", "key3"] {
        assert_eq!(
            "after",
            cache.get::<String>(key).await?,
            "the cache must be invalidated for `{key}`, whichever shard holds it"
        );
    }

    Ok(())
}

/// Losing an invalidation must cost a cache flush, not a stale read.
///
/// This is the one path in the backpressure work whose failure is a correctness
/// bug rather than a memory one: an invalidation discarded to stay within budget
/// names a key that will never be named again, so acting only on the survivors
/// would leave that key cached and served for good.
///
/// The scenario forces exactly that. The budget is one byte, so any burst that
/// leaves two messages queued at once evicts; the tracked key is written
/// *first*, so its invalidation is the oldest and therefore the prime candidate
/// for eviction; and thousands of writes follow to guarantee the burst. The
/// flush is waited for rather than slept on — the cache exposes its flush
/// generation for that.
#[tokio::test]
#[serial]
async fn a_lost_invalidation_flushes_the_cache_instead_of_serving_stale_data() -> Result<()> {
    log_try_init();

    const PREFIX: &str = "lost_invalidation_key_";
    const OTHER_KEYS: usize = 5_000;

    let mut config = get_default_config()?;
    // The smallest non-zero budget: `0` would mean unbounded.
    config.backpressure.max_push_bytes = 1;
    let cached_client = Client::connect(config).await?;
    let writer = get_test_client().await?;

    let target = format!("{PREFIX}target");
    writer.set(&target, "v1").await?;

    let cache = Cache::new(
        cached_client.clone(),
        60,
        ClientTrackingOptions::default()
            .prefix(PREFIX)
            .broadcasting(),
    )
    .await?;

    let value: String = cache.get(&target).await?;
    assert_eq!("v1", value, "the value under test must start out cached");

    // The tracked key changes first, so its invalidation is the oldest queued
    // and the one drop-oldest sheds. The rest of the burst is what overruns the
    // budget in the first place.
    //
    // Not awaited, and this is the whole point: awaiting it would let its
    // invalidation arrive alone and be handled key by key, so the test would pass
    // without the flush ever mattering. Pipelined into the burst, it is the oldest
    // queued message when the budget is breached.
    writer.send_and_forget(cmd("SET").arg(&target).arg("v2"), None)?;
    for i in 0..OTHER_KEYS {
        writer.send_and_forget(cmd("SET").arg(format!("{PREFIX}{i}")).arg("v"), None)?;
    }
    let _: String = writer.send(cmd("PING"), None).await?;

    timeout(Duration::from_secs(30), TimeoutKind::Command, async {
        while cache.flush_generation() == 0 {
            tokio::task::yield_now().await;
        }
        Ok::<(), Error>(())
    })
    .await??;

    let value: String = cache.get(&target).await?;
    assert_eq!(
        "v2", value,
        "a dropped invalidation must have flushed the cache, not left a stale value"
    );

    cached_client
        .client_tracking(ClientTrackingStatus::Off, ClientTrackingOptions::default())
        .await?;
    Ok(())
}

/// A store of the caller's own serves the hits and receives the invalidations —
/// the two things a cache backed by anything other than `moka` has to prove.
#[tokio::test]
#[serial]
async fn a_cache_store_of_your_own_serves_the_hits_and_takes_the_invalidations() -> Result<()> {
    use crate::{
        cache::{CacheStore, CachedValue},
        resp::BulkString,
    };
    use bytes::Bytes;
    use std::{
        collections::HashMap,
        sync::{
            Mutex,
            atomic::{AtomicUsize, Ordering},
        },
    };

    #[derive(Default)]
    struct CountingStore {
        entries: Mutex<HashMap<(BulkString, Bytes), CachedValue>>,
        hits: AtomicUsize,
        invalidations: AtomicUsize,
    }

    impl CacheStore for std::sync::Arc<CountingStore> {
        async fn get(&self, key: &BulkString, subkey: &Bytes) -> Option<CachedValue> {
            let found = self
                .entries
                .lock()
                .ok()?
                .get(&(key.clone(), subkey.clone()))
                .cloned();
            if found.is_some() {
                self.hits.fetch_add(1, Ordering::SeqCst);
            }
            found
        }

        async fn insert(&self, key: BulkString, subkey: Bytes, response: CachedValue) {
            if let Ok(mut entries) = self.entries.lock() {
                entries.insert((key, subkey), response);
            }
        }

        async fn invalidate(&self, key: &BulkString) {
            self.invalidations.fetch_add(1, Ordering::SeqCst);
            if let Ok(mut entries) = self.entries.lock() {
                entries.retain(|(entry_key, _), _| entry_key != key);
            }
        }

        fn invalidate_all(&self) {
            if let Ok(mut entries) = self.entries.lock() {
                entries.clear();
            }
        }
    }

    log_try_init();
    let client1 = Client::connect("redis://127.0.0.1?connection_name=custom_store").await?;
    let client2 = Client::connect("redis://127.0.0.1?connection_name=custom_store_writer").await?;
    client2.flushall(FlushingMode::Sync).await?;
    client1
        .client_tracking(ClientTrackingStatus::Off, ClientTrackingOptions::default())
        .await?;
    client2.set("store_key", "value").await?;

    let store = std::sync::Arc::new(CountingStore::default());
    let cache = Cache::with_store(
        client1.clone(),
        std::sync::Arc::clone(&store),
        ClientTrackingOptions::default(),
    )
    .await?;

    // Miss, then a hit that can only have come from the store.
    let value: String = cache.get("store_key").await?;
    assert_eq!("value", value);
    assert_eq!(0, store.hits.load(Ordering::SeqCst));
    let value: String = cache.get("store_key").await?;
    assert_eq!("value", value);
    assert_eq!(1, store.hits.load(Ordering::SeqCst));

    // The server's invalidation must reach the store, not a moka cache behind it.
    client2.set("store_key", "new_value").await?;
    sleep(Duration::from_millis(100)).await;
    assert!(store.invalidations.load(Ordering::SeqCst) >= 1);
    let value: String = cache.get("store_key").await?;
    assert_eq!("new_value", value);

    // A read must stay spawnable. `CacheStore` declares `-> impl Future + Send`
    // rather than a bare `async fn` for exactly this: a bare one makes no
    // Send promise, and the whole `Cache::get` future would stop being Send —
    // a compile error at every `tokio::spawn`, in the user's code, not here.
    let spawned = tokio::spawn(async move {
        let value: String = cache.get("store_key").await?;
        Ok::<String, Error>(value)
    });
    assert_eq!("new_value", spawned.await.expect("task panicked")?);

    Ok(())
}