rainier-drivers 2.0.0

Shared connectors for Rainier: Redis, Redis Cluster, Memcached, Kafka and AWS transports.
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
//! Redis streams and sorted sets — the commands a queue needs.
//!
//! Split from [`redis`](crate::redis) because it is a different *use* of the
//! same connection: the cache needs `GET`/`SET`, a queue needs an
//! acknowledgement protocol, and mixing them makes one file nobody can hold in
//! their head.
//!
//! # Why streams and not lists
//!
//! `LPUSH`/`BRPOP` is the usual Redis queue, and it has no acknowledgement:
//! `BRPOP` removes the entry, so a worker that dies holding one has taken it
//! with it. Streams (Redis 5.0+) have consumer groups — a pending entry list,
//! `XACK`, and `XAUTOCLAIM` to redeliver what a dead consumer left — which is
//! the reserve-then-acknowledge protocol
//! [`Queue`](../rainier_queue/trait.Queue.html) requires. A list cannot honour
//! that contract; a stream can.

use std::time::Duration;

use redis::Value;

use rainier_support::Result;

use crate::redis::RedisClient;

/// The field every entry's body is stored under.
///
/// One field rather than a field per property: the body is already JSON, and
/// splitting it across stream fields would mean two serialisations that can
/// disagree.
pub const BODY: &str = "body";

/// One entry read out of a stream.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct StreamEntry {
    /// The stream's id for it — `1700000000000-0`. What `XACK` needs.
    pub id: String,
    /// The body, as stored.
    pub body: Vec<u8>,
}

/// Promote every due member of a sorted set into a stream, atomically.
///
/// Streams cannot hold a delayed entry, so a delayed job waits in a sorted set
/// scored by the millisecond it becomes available, and something has to move
/// it across. In Lua because the read and the two writes have to be one step:
/// two workers promoting at once would otherwise both see the same member and
/// add it twice.
const PROMOTE_DUE: &str = r"
    local due = redis.call('ZRANGEBYSCORE', KEYS[1], '-inf', ARGV[1])
    if #due == 0 then return 0 end
    for _, body in ipairs(due) do
        redis.call('ZREM', KEYS[1], body)
        redis.call('XADD', KEYS[2], '*', ARGV[2], body)
    end
    return #due
";

impl RedisClient {
    /// `XADD stream * body <entry>`. Returns the entry's id.
    pub async fn xadd(&self, stream: &str, body: &[u8]) -> Result<String> {
        self.connection().query(redis::cmd("XADD").arg(stream).arg("*").arg(BODY).arg(body)).await
    }

    /// `XGROUP CREATE stream group $ MKSTREAM`, tolerating one that exists.
    ///
    /// Creating the group is idempotent by being allowed to fail: `BUSYGROUP`
    /// means another worker got there first, which is the normal case on every
    /// start after the first.
    pub async fn xgroup_create(&self, stream: &str, group: &str) -> Result<()> {
        // `0` rather than `$`: `$` would deliver only entries added *after* the
        // group existed, so every job pushed before the first worker started
        // would sit in the stream forever.
        let outcome: Result<()> = self
            .connection()
            .run(redis::cmd("XGROUP").arg("CREATE").arg(stream).arg(group).arg("0").arg("MKSTREAM"))
            .await;

        match outcome {
            Ok(()) => Ok(()),
            // Matched on the text, because the error mapping keeps Redis's
            // message and drops its `BUSYGROUP` code. `XGROUP CREATE` has one
            // "already exists" to report, so this cannot swallow another.
            Err(e) if already_exists(&e) => Ok(()),
            Err(e) => Err(e),
        }
    }

    /// `XREADGROUP … COUNT 1 STREAMS stream >` — one entry never delivered
    /// before.
    pub async fn xreadgroup_one(
        &self,
        stream: &str,
        group: &str,
        consumer: &str,
    ) -> Result<Option<StreamEntry>> {
        let reply: Value = self
            .connection()
            .query(
                redis::cmd("XREADGROUP")
                    .arg("GROUP")
                    .arg(group)
                    .arg(consumer)
                    .arg("COUNT")
                    .arg(1)
                    .arg("STREAMS")
                    .arg(stream)
                    .arg(">"),
            )
            .await?;

        Ok(read_reply(&reply))
    }

    /// `XAUTOCLAIM` — one entry another consumer took and never acknowledged.
    ///
    /// This is redelivery: an entry idle longer than `min_idle` belongs to a
    /// worker that is not coming back, and claiming it is how the job survives
    /// that worker's death.
    pub async fn xautoclaim_one(
        &self,
        stream: &str,
        group: &str,
        consumer: &str,
        min_idle: Duration,
    ) -> Result<Option<StreamEntry>> {
        let reply: Value = self
            .connection()
            .query(
                redis::cmd("XAUTOCLAIM")
                    .arg(stream)
                    .arg(group)
                    .arg(consumer)
                    .arg(min_idle.as_millis() as u64)
                    .arg("0-0")
                    .arg("COUNT")
                    .arg(1),
            )
            .await?;

        Ok(claim_reply(&reply))
    }

    /// `XACK` then `XDEL` — finished with this entry, and it can go.
    ///
    /// Both, because `XACK` alone only clears the pending list: the entry stays
    /// in the stream, and a queue that never deletes grows until it fills the
    /// memory Redis is holding everything else in.
    pub async fn xack_delete(&self, stream: &str, group: &str, id: &str) -> Result<()> {
        self.connection().run(redis::cmd("XACK").arg(stream).arg(group).arg(id)).await?;
        self.connection().run(redis::cmd("XDEL").arg(stream).arg(id)).await
    }

    /// `XLEN`.
    pub async fn xlen(&self, stream: &str) -> Result<u64> {
        self.connection().query(redis::cmd("XLEN").arg(stream)).await
    }

    /// `ZADD key score member`.
    pub async fn zadd(&self, key: &str, score: i64, member: &[u8]) -> Result<()> {
        self.connection().run(redis::cmd("ZADD").arg(key).arg(score).arg(member)).await
    }

    /// `ZCARD`.
    pub async fn zcard(&self, key: &str) -> Result<u64> {
        self.connection().query(redis::cmd("ZCARD").arg(key)).await
    }

    /// Move everything in `zset` scored at or before `now_ms` into `stream`.
    ///
    /// Returns how many moved. A Lua script, so the read and the two writes
    /// are one step: two workers promoting at once would otherwise both see the
    /// same member and add it twice.
    pub async fn promote_due(&self, zset: &str, stream: &str, now_ms: i64) -> Result<u64> {
        self.connection()
            .query(
                redis::cmd("EVAL")
                    .arg(PROMOTE_DUE)
                    .arg(2)
                    .arg(zset)
                    .arg(stream)
                    .arg(now_ms)
                    .arg(BODY),
            )
            .await
    }

    /// `LPUSH`. Returns the list's new length.
    pub async fn lpush(&self, key: &str, value: &[u8]) -> Result<u64> {
        self.connection().query(redis::cmd("LPUSH").arg(key).arg(value)).await
    }

    /// `LRANGE key 0 -1`.
    pub async fn lrange_all(&self, key: &str) -> Result<Vec<Vec<u8>>> {
        self.connection().query(redis::cmd("LRANGE").arg(key).arg(0).arg(-1)).await
    }

    /// `CONFIG GET name`, or `None` when the server will not say.
    ///
    /// A managed Redis often disables `CONFIG`, so "no answer" is a normal
    /// outcome and not an error — the caller wanted to check something, not to
    /// depend on it.
    pub async fn config_get(&self, name: &str) -> Result<Option<String>> {
        let reply: Value =
            match self.connection().query(redis::cmd("CONFIG").arg("GET").arg(name)).await {
                Ok(reply) => reply,
                Err(_) => return Ok(None),
            };

        Ok(config_value(&reply))
    }

    /// `DEL` over several keys. Returns how many existed.
    pub async fn delete_all(&self, keys: &[String]) -> Result<u64> {
        if keys.is_empty() {
            return Ok(0);
        }

        let mut command = redis::cmd("DEL");
        for key in keys {
            command.arg(key);
        }
        self.connection().query(&command).await
    }
}

/// Whether an error is Redis saying the consumer group is already there.
///
/// The normal case on every start after the first, so it has to be tolerated —
/// and it is checked by message because that is all the mapped error keeps.
fn already_exists(error: &rainier_support::Error) -> bool {
    let message = error.message();
    message.contains("BUSYGROUP") || message.contains("already exists")
}

/// The value out of a `CONFIG GET` reply — `[name, value]`, or a map on RESP3.
fn config_value(reply: &Value) -> Option<String> {
    match reply {
        Value::Array(pairs) => as_string(pairs.get(1)?),
        Value::Map(pairs) => pairs.first().and_then(|(_, value)| as_string(value)),
        _ => None,
    }
}

/// The first entry in an `XREADGROUP` reply.
///
/// `[[stream_name, [[id, [field, value, …]], …]], …]` — one level deeper than
/// `XAUTOCLAIM`, because the reply can cover several streams.
fn read_reply(reply: &Value) -> Option<StreamEntry> {
    let Value::Array(streams) = reply else { return None };

    streams.iter().find_map(|stream| {
        let Value::Array(parts) = stream else { return None };
        entries(parts.get(1)?)
    })
}

/// The first entry in an `XAUTOCLAIM` reply.
///
/// `[cursor, [[id, [field, value, …]], …], [deleted…]]`.
fn claim_reply(reply: &Value) -> Option<StreamEntry> {
    let Value::Array(parts) = reply else { return None };
    entries(parts.get(1)?)
}

/// The first `[id, [field, value, …]]` pair, as an entry.
fn entries(value: &Value) -> Option<StreamEntry> {
    let Value::Array(entries) = value else { return None };

    entries.iter().find_map(|entry| {
        let Value::Array(parts) = entry else { return None };
        let id = as_string(parts.first()?)?;
        let body = field(parts.get(1)?, BODY)?;

        Some(StreamEntry { id, body })
    })
}

/// The value of `name` in a flat `[field, value, field, value]` array.
fn field(value: &Value, name: &str) -> Option<Vec<u8>> {
    let Value::Array(pairs) = value else { return None };

    pairs.chunks(2).find_map(|pair| match pair {
        [key, value] if as_string(key).as_deref() == Some(name) => as_bytes(value),
        _ => None,
    })
}

fn as_string(value: &Value) -> Option<String> {
    match value {
        Value::BulkString(bytes) => String::from_utf8(bytes.clone()).ok(),
        Value::SimpleString(text) => Some(text.clone()),
        _ => None,
    }
}

fn as_bytes(value: &Value) -> Option<Vec<u8>> {
    match value {
        Value::BulkString(bytes) => Some(bytes.clone()),
        Value::SimpleString(text) => Some(text.clone().into_bytes()),
        _ => None,
    }
}

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

    fn bulk(text: &str) -> Value {
        Value::BulkString(text.as_bytes().to_vec())
    }

    /// What `XREADGROUP GROUP g c COUNT 1 STREAMS s >` actually replies with.
    fn xreadgroup_reply() -> Value {
        Value::Array(vec![Value::Array(vec![
            bulk("queue:default"),
            Value::Array(vec![Value::Array(vec![
                bulk("1700000000000-0"),
                Value::Array(vec![bulk("body"), bulk(r#"{"id":"j1"}"#)]),
            ])]),
        ])])
    }

    /// What `XAUTOCLAIM s g c 30000 0-0 COUNT 1` replies with.
    fn xautoclaim_reply() -> Value {
        Value::Array(vec![
            bulk("0-0"),
            Value::Array(vec![Value::Array(vec![
                bulk("1700000000001-0"),
                Value::Array(vec![bulk("body"), bulk(r#"{"id":"j2"}"#)]),
            ])]),
            Value::Array(vec![]),
        ])
    }

    #[test]
    fn a_read_reply_yields_its_entry() {
        let entry = read_reply(&xreadgroup_reply()).expect("one entry");

        assert_eq!(entry.id, "1700000000000-0");
        assert_eq!(entry.body, br#"{"id":"j1"}"#.to_vec());
    }

    #[test]
    fn a_claim_reply_is_one_level_shallower_and_still_parses() {
        // The two commands nest differently, which is the whole reason these
        // are separate functions and separately tested.
        let entry = claim_reply(&xautoclaim_reply()).expect("one entry");

        assert_eq!(entry.id, "1700000000001-0");
        assert_eq!(entry.body, br#"{"id":"j2"}"#.to_vec());
    }

    #[test]
    fn an_empty_queue_yields_nothing_rather_than_an_error() {
        // Both commands answer this way when there is nothing to take, and it
        // is the most common reply a busy worker sees.
        assert_eq!(read_reply(&Value::Nil), None);
        assert_eq!(read_reply(&Value::Array(vec![])), None);

        let empty_claim =
            Value::Array(vec![bulk("0-0"), Value::Array(vec![]), Value::Array(vec![])]);
        assert_eq!(claim_reply(&empty_claim), None);
    }

    #[test]
    fn an_entry_missing_its_body_field_is_skipped() {
        // Something else wrote to this stream. Better to see nothing than to
        // hand the queue a job it will fail to parse on every redelivery.
        let reply = Value::Array(vec![Value::Array(vec![
            bulk("queue:default"),
            Value::Array(vec![Value::Array(vec![
                bulk("1-0"),
                Value::Array(vec![bulk("something-else"), bulk("{}")]),
            ])]),
        ])]);

        assert_eq!(read_reply(&reply), None);
    }

    #[test]
    fn a_reply_shape_we_do_not_know_is_none_not_a_panic() {
        for reply in [Value::Okay, Value::Int(1), bulk("nonsense")] {
            assert_eq!(read_reply(&reply), None);
            assert_eq!(claim_reply(&reply), None);
        }
    }

    #[test]
    fn a_config_reply_yields_its_value_in_either_protocol() {
        let resp2 = Value::Array(vec![bulk("maxmemory-policy"), bulk("noeviction")]);
        let resp3 = Value::Map(vec![(bulk("maxmemory-policy"), bulk("allkeys-lru"))]);

        assert_eq!(config_value(&resp2).as_deref(), Some("noeviction"));
        assert_eq!(config_value(&resp3).as_deref(), Some("allkeys-lru"));
        assert_eq!(config_value(&Value::Nil), None);
    }

    #[test]
    fn the_promotion_script_moves_and_removes_in_one_step() {
        // Read as a string, because the alternative is discovering at 3am that
        // it promoted without removing and every delayed job ran forever.
        assert!(PROMOTE_DUE.contains("ZRANGEBYSCORE"));
        assert!(PROMOTE_DUE.contains("ZREM"));
        assert!(PROMOTE_DUE.contains("XADD"));
    }
}