rustis 0.22.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
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
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
//! What the client holds on behalf of a consumer that has stopped keeping up,
//! and the budgets that bound it.
//!
//! The pub/sub half of the question lives with the pub/sub suite, next to the
//! subscription API it exercises. This module holds the send-queue half, which
//! needs a sustained outage and therefore the fault proxy, and the two push
//! sinks — the `MONITOR` feed and the tracking invalidation stream.
//!
//! The push sinks share the send path and the bounded channel with pub/sub, but
//! not its exposure, which is why they get their own scenarios. A `MONITOR`
//! stream is fed by every *other* client's traffic, so it grows without its
//! holder doing anything unusual, and the server offers no way to slow the feed
//! down. An invalidation stream carries a correctness signal rather than data:
//! discarding one leaves a key stale, so what has to be proven is not only the
//! bound but that the loss is counted and acted upon.

use crate::{
    ClientError, Error, Result,
    client::{BackpressureConfig, Client, Config, IntoConfig, ReconnectionConfig},
    commands::{
        BlockingCommands, ClientTrackingOptions, ClientTrackingStatus, ConnectionCommands,
        GenericCommands, StringCommands,
    },
    network::{QueueMetricsTestHook, timeout},
    resp::cmd,
    spawn,
    tests::{
        fault_injection_proxy::{Action, FaultProxy},
        get_default_addr, get_default_config, get_test_client, log_try_init, resident_bytes,
    },
};
use serial_test::serial;
use std::time::Duration;

/// Bytes charged per queued message on top of its command buffers. Mirrors the
/// crate-internal allowance so the tests can predict the bound.
const MESSAGE_OVERHEAD: usize = 1024;

/// Builds a client whose reconnection never gives up and whose send queue is
/// capped at `max_queued_bytes`, pointed at `addr`.
///
/// The reconnection cap stays at `0`: a non-zero one ends the network task for
/// good, which is a different failure from the one under test.
fn storm_config(addr: std::net::SocketAddr, max_queued_bytes: usize) -> Result<Config> {
    let mut config = format!("redis://{addr}").into_config()?;
    config.retry_on_error = true;
    config.reconnection = ReconnectionConfig::new_constant(0, 50);
    config.connect_timeout = Duration::from_millis(200);
    config.command_timeout = Duration::ZERO;
    config.backpressure = BackpressureConfig {
        max_queued_bytes,
        ..Default::default()
    };
    Ok(config)
}

/// The budget must actually bound the send queue during an outage — this is the
/// growth that was measured as unbounded before it existed.
///
/// The bound is asserted arithmetically rather than by a round number: with a
/// known budget and a known per-message charge, the peak depth the queue may
/// reach is computable, and the queue is allowed exactly one message of overrun
/// because an empty queue always admits whatever is offered.
#[tokio::test]
#[serial]
async fn the_send_queue_stops_growing_at_its_memory_budget() -> Result<()> {
    log_try_init();

    const VALUE_BYTES: usize = 1024;
    const BUDGET: usize = 1024 * 1024;
    const OFFERED: usize = 50_000;

    let proxy = FaultProxy::start_multi(get_default_addr(), vec![vec![], vec![Action::Drop]])
        .await
        .unwrap();

    let metrics = QueueMetricsTestHook::new();
    let mut config = storm_config(proxy.addr, BUDGET)?;
    config.queue_metrics_test_hook = Some(metrics.clone());
    let client = Client::connect(config).await?;

    client.send_and_forget(cmd("PING").kill_connection_on_read(1), None)?;

    let baseline_rss = resident_bytes();
    let value = "v".repeat(VALUE_BYTES);

    timeout(Duration::from_secs(60), async {
        while proxy.connections_accepted() < 3 {
            tokio::task::yield_now().await;
        }
        for i in 0..OFFERED {
            client.send_and_forget(
                cmd("SET")
                    .arg(format!("budget_key_{i}"))
                    .arg(value.as_str()),
                Some(true),
            )?;
            if i % 1000 == 0 {
                tokio::task::yield_now().await;
            }
        }
        // Let the network task drain the channel into the send queue, which is
        // where the budget applies.
        for _ in 0..1000 {
            tokio::task::yield_now().await;
        }
        Ok::<(), Error>(())
    })
    .await??;

    let peak = metrics.messages_to_send_high_water();
    let rss_delta = match (baseline_rss, resident_bytes()) {
        (Some(before), Some(after)) => Some(after.saturating_sub(before)),
        _ => None,
    };
    // A `SET key <1 KiB value>` buffer is at least the value itself, so this
    // under-estimates the per-message charge and therefore over-estimates the
    // permitted depth — the assertion stays conservative.
    let min_charge = VALUE_BYTES + MESSAGE_OVERHEAD;
    let max_depth = BUDGET / min_charge + 1;
    let report =
        format!("peak={peak} max_depth={max_depth} offered={OFFERED} rss_delta={rss_delta:?}");
    println!("send queue budget: {report}");

    assert!(
        peak <= max_depth,
        "the send queue must stay within its budget plus one message: {report}"
    );
    // `rss_delta` is reported, not asserted. Resident memory is a process-wide
    // figure: the shed commands are allocated and freed, and the allocator keeps
    // the pages, so the delta reflects arena behaviour as much as retention. It
    // was the right corroboration for a 220 MiB growth against a few MiB of
    // noise; it cannot prove a bound of that same order. The queue depth above
    // is exact and is the proof.

    Ok(())
}

/// A command shed by a full send queue must say so, with an error distinct from
/// the two other ways a command dies during an outage.
#[tokio::test]
#[serial]
async fn a_command_refused_by_a_full_send_queue_reports_it() -> Result<()> {
    log_try_init();

    // One filler is larger than the whole budget, so the queue is over budget
    // from the first one and every later command is refused whatever its size.
    const BUDGET: usize = 4 * 1024;
    const FILLER_BYTES: usize = 8 * 1024;

    let proxy = FaultProxy::start_multi(get_default_addr(), vec![vec![], vec![Action::Drop]])
        .await
        .unwrap();

    let client = Client::connect(storm_config(proxy.addr, BUDGET)?).await?;
    client.send_and_forget(cmd("PING").kill_connection_on_read(1), None)?;

    let error = timeout(Duration::from_secs(30), async {
        while proxy.connections_accepted() < 3 {
            tokio::task::yield_now().await;
        }
        let value = "v".repeat(FILLER_BYTES);
        for _ in 0..10 {
            client.send_and_forget(
                cmd("SET").arg("refused_filler").arg(value.as_str()),
                Some(true),
            )?;
        }
        for _ in 0..100 {
            tokio::task::yield_now().await;
        }

        // A retryable command would normally be queued and wait for the link to
        // come back; over budget it must be refused instead, and say why.
        let result: Result<String> = client.send(cmd("PING"), Some(true)).await;
        match result {
            Err(e) => Ok::<Error, Error>(e),
            Ok(_) => panic!("a command offered to a full send queue must not succeed"),
        }
    })
    .await??;

    assert!(
        matches!(error, Error::Client(ClientError::SendQueueFull)),
        "a command shed by a full queue must report SendQueueFull, got {error:?}"
    );

    Ok(())
}

/// The invariant that makes the budget safe: it sheds only *incoming* commands.
///
/// A command that was accepted, then replayed by a reconnection, must never be
/// refused by the budget — otherwise the client would drop a command its caller
/// had been told was on its way. Here the budget is far smaller than the queue
/// the outage builds, so every replayed command is offered to a queue that is
/// already over budget.
#[tokio::test]
#[serial]
async fn a_command_already_queued_survives_the_reconnection_that_replays_it() -> Result<()> {
    log_try_init();

    const BUDGET: usize = 4 * 1024;
    const COMMANDS: usize = 20;

    // The outage lasts two failed reconnections, then the link comes back.
    let proxy = FaultProxy::start_multi(
        get_default_addr(),
        vec![vec![], vec![Action::Drop], vec![Action::Drop], vec![]],
    )
    .await
    .unwrap();

    let client = Client::connect(storm_config(proxy.addr, BUDGET)?).await?;

    let control = Client::connect(get_default_addr()).await?;
    for i in 0..COMMANDS {
        control.del(format!("replayed_key_{i}")).await?;
    }

    client.send_and_forget(cmd("PING").kill_connection_on_read(1), None)?;

    // Every command that is *accepted* must eventually reach the server. Ones
    // refused up front are counted separately: shedding a new command is the
    // budget working, losing an accepted one is the bug this guards against.
    let mut handles = Vec::new();
    timeout(Duration::from_secs(30), async {
        while proxy.connections_accepted() < 2 {
            tokio::task::yield_now().await;
        }
        for i in 0..COMMANDS {
            let client = client.clone();
            handles.push(spawn(async move {
                let result: Result<String> = client
                    .send(
                        cmd("SET").arg(format!("replayed_key_{i}")).arg(i),
                        Some(true),
                    )
                    .await;
                result.map(|_| ())
            }));
            tokio::task::yield_now().await;
        }
        Ok::<(), Error>(())
    })
    .await??;

    let mut accepted = 0usize;
    let mut shed = 0usize;
    for handle in handles {
        match timeout(Duration::from_secs(30), handle).await {
            Ok(Ok(Ok(()))) => accepted += 1,
            Ok(Ok(Err(Error::Client(ClientError::SendQueueFull)))) => shed += 1,
            other => panic!("unexpected outcome for a queued command: {other:?}"),
        }
    }

    let mut stored = 0usize;
    for i in 0..COMMANDS {
        let value: Option<usize> = control.get(format!("replayed_key_{i}")).await?;
        if value.is_some() {
            stored += 1;
        }
    }

    let report = format!("accepted={accepted} shed={shed} stored={stored} of {COMMANDS}");
    println!("replay invariant: {report}");

    assert!(
        accepted > 0,
        "the test proves nothing unless some commands were accepted: {report}"
    );
    assert_eq!(
        accepted, stored,
        "every accepted command must have reached the server: {report}"
    );

    Ok(())
}

/// The byte accounting must come back down as the queue drains, otherwise the
/// budget would latch and refuse commands forever on a healthy connection.
#[tokio::test]
#[serial]
async fn the_send_queue_budget_is_released_when_the_queue_drains() -> Result<()> {
    log_try_init();

    const BUDGET: usize = 16 * 1024;

    let mut config = get_default_addr().into_config()?;
    config.backpressure = BackpressureConfig {
        max_queued_bytes: BUDGET,
        ..Default::default()
    };
    let client = Client::connect(config).await?;

    // Far more traffic than the budget, but sent in awaited waves so the queue
    // drains between them. Nothing may be refused.
    let value = "v".repeat(4096);
    for wave in 0..20 {
        for i in 0..2 {
            client
                .set(format!("drain_key_{wave}_{i}"), value.as_str())
                .await?;
        }
        let _: String = client.send(cmd("PING"), None).await?;
    }

    let stored: String = client.get("drain_key_19_1").await?;
    assert_eq!(
        value, stored,
        "a drained queue must keep accepting commands"
    );

    Ok(())
}

/// Waits for a push feed to go quiet, and answers how many messages it carried.
///
/// The count is read after the feed stops moving rather than against a predicted
/// total: Redis batches invalidations, so the number of messages is not the
/// number of keys written. Quiescence here is a fact and not a guess — the writer
/// has already had a command answered, so the server has nothing left to send.
async fn offered_once_quiet(metrics: &QueueMetricsTestHook) -> usize {
    let mut last = metrics.push_delivered();
    let mut still = 0;
    while still < 500 {
        tokio::task::yield_now().await;
        let now = metrics.push_delivered();
        if now == last {
            still += 1;
        } else {
            still = 0;
            last = now;
        }
    }
    last
}

/// A `MONITOR` stream that is held but not polled must stay within its budget.
///
/// This is the harshest of the three sinks: the feed is filled by other clients'
/// traffic, so it keeps growing even when its holder does nothing wrong beyond
/// reading it more slowly than the server produces it, and `MONITOR` offers no
/// way to push back. Shedding the oldest lines is the only bound available.
#[tokio::test]
#[serial]
async fn a_paused_monitor_is_bounded_by_its_memory_budget() -> Result<()> {
    log_try_init();

    // Large values make each monitored line large, so a small budget is reached
    // with few enough commands to keep the test quick.
    const VALUE_BYTES: usize = 4096;
    const BUDGET: usize = 256 * 1024;
    const OFFERED: usize = 5_000;

    let metrics = QueueMetricsTestHook::new();
    let mut config = get_default_config()?;
    config.queue_metrics_test_hook = Some(metrics.clone());
    config.backpressure.max_push_bytes = BUDGET;
    let monitored = Client::connect(config).await?;
    let writer = get_test_client().await?;

    // Held and never polled: the bound must come from the channel, not from a
    // reader keeping up.
    let held_stream = monitored.monitor().await?;

    let baseline_rss = resident_bytes();
    let value = "v".repeat(VALUE_BYTES);

    timeout(Duration::from_secs(60), async {
        for i in 0..OFFERED {
            writer.send_and_forget(
                cmd("SET")
                    .arg(format!("monitor_budget_key_{i}"))
                    .arg(value.as_str()),
                None,
            )?;
            // Bound the writer's own send queue, which is not what is under test.
            if i % 500 == 0 {
                let _: String = writer.send(cmd("PING"), None).await?;
            }
        }
        let _: String = writer.send(cmd("PING"), None).await?;
        while metrics.push_delivered() < OFFERED {
            tokio::task::yield_now().await;
        }
        Ok::<(), Error>(())
    })
    .await??;

    let delivered = offered_once_quiet(&metrics).await;
    let dropped = held_stream.dropped_messages();
    let offered_bytes = metrics.push_delivered_bytes();
    let rss_delta = match (baseline_rss, resident_bytes()) {
        (Some(before), Some(after)) => Some(after.saturating_sub(before)),
        _ => None,
    };
    // Every line delivered and not evicted is still held by the paused stream.
    let held = delivered.saturating_sub(dropped);
    // A monitored `SET` line quotes the value, so it is at least that long. This
    // under-estimates the per-line charge and therefore over-estimates the
    // permitted count, keeping the assertion conservative. One line of slack:
    // the channel always admits into an empty queue.
    let max_held = BUDGET / VALUE_BYTES + 1;
    let report = format!(
        "delivered={delivered} dropped={dropped} held={held} max_held={max_held} \
         offered={offered_bytes} B budget={BUDGET} B rss_delta={rss_delta:?}"
    );
    println!("paused monitor: {report}");

    assert!(
        delivered >= OFFERED,
        "the feed must carry at least the commands that were issued: {report}"
    );
    assert_eq!(
        0,
        metrics.push_delivery_failed(),
        "a live sink must never have a delivery refused: {report}"
    );
    assert!(
        dropped > 0,
        "far more was monitored than the budget allows, so lines must have been \
         dropped and counted: {report}"
    );
    assert!(
        held <= max_held,
        "the stream must hold no more than its budget allows: {report}"
    );
    // `rss_delta` is reported, not asserted, for the reason given above.

    drop(held_stream);
    Ok(())
}

/// A tracking invalidation stream held by the caller must be bounded too.
///
/// The `Cache` (feature `client-cache`) polls its own stream continuously, but
/// `create_client_tracking_invalidation_stream` is public: a caller can hold one
/// and read it slowly, exactly like a subscriber. Loss there is a correctness
/// matter for whatever cache the caller built, so the counter that reports it
/// must be exact — it is that caller's only signal.
#[tokio::test]
#[serial]
async fn a_paused_invalidation_reader_is_bounded_by_its_memory_budget() -> Result<()> {
    log_try_init();

    // Invalidations are small, so the budget is small too. Long keys give each
    // message a known minimum size.
    const BUDGET: usize = 16 * 1024;
    const KEY_PADDING: usize = 128;
    const OFFERED_KEYS: usize = 5_000;

    let metrics = QueueMetricsTestHook::new();
    let mut config = get_default_config()?;
    config.queue_metrics_test_hook = Some(metrics.clone());
    config.backpressure.max_push_bytes = BUDGET;
    let tracked = Client::connect(config).await?;
    let writer = get_test_client().await?;

    // Held and never polled.
    let held_stream = tracked.create_client_tracking_invalidation_stream()?;

    // Broadcasting on a prefix: every write under it invalidates, with no need
    // for the tracked client to read the keys first.
    tracked
        .client_tracking(
            ClientTrackingStatus::On,
            ClientTrackingOptions::default()
                .prefix("invalidation_budget_key_")
                .broadcasting(),
        )
        .await?;

    let baseline_rss = resident_bytes();
    let padding = "p".repeat(KEY_PADDING);

    timeout(Duration::from_secs(60), async {
        for i in 0..OFFERED_KEYS {
            writer.send_and_forget(
                cmd("SET")
                    .arg(format!("invalidation_budget_key_{padding}_{i}"))
                    .arg("v"),
                None,
            )?;
            if i % 500 == 0 {
                let _: String = writer.send(cmd("PING"), None).await?;
            }
        }
        let _: String = writer.send(cmd("PING"), None).await?;
        while metrics.push_delivered() == 0 {
            tokio::task::yield_now().await;
        }
        Ok::<(), Error>(())
    })
    .await??;

    let delivered = offered_once_quiet(&metrics).await;
    let dropped = held_stream.dropped_messages();
    let offered_bytes = metrics.push_delivered_bytes();
    let rss_delta = match (baseline_rss, resident_bytes()) {
        (Some(before), Some(after)) => Some(after.saturating_sub(before)),
        _ => None,
    };
    let held = delivered.saturating_sub(dropped);
    // Redis batches invalidations, so a message carries an unpredictable number
    // of keys and its size cannot be derived from the keys written — it is
    // measured instead. Twice the budget's worth of average-sized messages is
    // allowed: the messages still held are the last ones, which may be smaller
    // than average, and a regression to no bound at all would sit orders of
    // magnitude above this.
    let average_bytes = offered_bytes.max(1) / delivered.max(1);
    let max_held = 2 * BUDGET / average_bytes.max(1) + 1;
    let report = format!(
        "delivered={delivered} dropped={dropped} held={held} max_held={max_held} \
         offered={offered_bytes} B average={average_bytes} B budget={BUDGET} B \
         rss_delta={rss_delta:?}"
    );
    println!("paused invalidation reader: {report}");

    assert_eq!(
        0,
        metrics.push_delivery_failed(),
        "a live sink must never have a delivery refused: {report}"
    );
    assert!(
        dropped > 0,
        "far more was invalidated than the budget allows, so invalidations must \
         have been dropped and counted: {report}"
    );
    assert!(
        held <= max_held,
        "the stream must hold no more than its budget allows: {report}"
    );

    drop(held_stream);
    tracked
        .client_tracking(ClientTrackingStatus::Off, ClientTrackingOptions::default())
        .await?;
    Ok(())
}