kividb 1.2.1

A fast Redis®-compatible in-memory store written in 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
542
543
544
545
546
// src/write_order.rs
//
// Writes to one key reach replicas and the AOF in the order the store took
// them.
//
// A write has two halves: it changes the store under a shard lock, and then,
// with that lock released, hands its payload to the replication hub and the
// AOF. Two connections writing the *same key* at the same moment could do
// those halves crossed over — A changes the store, B changes the store, B
// hands over, A hands over. The master then holds A-then-B and every replica,
// and the AOF, B-then-A. For `SET` the last value differs; for `RPUSH` the
// order of the list; for `APPEND` the string. Nothing ever corrects it.
//
// Measured before this existed: six clients appending to one list, one
// replica, no faults of any kind — about 500 of 18 000 elements out of place
// on the replica, every run.
//
// The write gate (`write_gate`) does not prevent this: it is shared between
// writers, and exists to give a cutter a point with no write in flight.
//
// So a write that somebody is listening to holds a lock on each key it names,
// from before it touches the store until its payload is queued. The locks are
// stripes — a fixed table indexed by a hash of the key — so two writes to the
// same key always meet on the same lock, and writes to different keys almost
// never do. A write that names no key but changes many (`FLUSHALL`), and a
// script, which may touch keys it did not declare, take the write gate
// exclusively instead ([`write_gate::enter_exclusive`]).
//
// It is taken when nobody is listening too — no replica, no backlog, no AOF,
// no backup. There is then no second order to disagree with, but a write
// must still run whole before or after every other write to its keys, and
// not every command does all of its reading and writing under one hold of
// its shard's lock: `PFADD` read the registers under one and wrote them
// back under another, and two of them at once lost elements (#271). Holding
// the order of a key says nothing about propagating the write: whether
// anything is listening decides that, separately (`write_is_observed`).
//
// A few commands need less: they do the whole of their reading and writing
// of their one key under one hold of its shard's lock, so two of them on one
// key already run one before the other (`SADD`, `HSET`, `LPUSH`, `INCR`,
// `PFADD`, ...; see `handler::atomic_under_its_shard`). While nothing is
// listening, such a write takes its key's stripe *shared*
// ([`lock_key_shared`]): beside the others like it, which the shard orders,
// and never beside a write that holds the stripe whole — every other write,
// an expiry, an eviction, the field-expiry sweep. Holding one lock whole for
// the whole of every command on a hot key halved what one key could take
// (`SADD` 645k a second to 339k, 12 threads): the commands queued for it,
// one at a time, where the shard had let them overlap but for the moment
// each changes the value.

use parking_lot::{RwLock, RwLockReadGuard, RwLockWriteGuard};
use std::cell::Cell;
use std::marker::PhantomData;

/// A power of two. Large enough that unrelated keys rarely share a stripe.
const STRIPES: usize = 4096;

static STRIPE: [RwLock<()>; STRIPES] = [const { parking_lot::const_rwlock(()) }; STRIPES];

/// [`HELD`]: no guard on this thread.
const FREE: u8 = 0;
/// [`HELD`]: a guard that holds no stripe — of a write that names no key
/// (`FT.ALTER`, `FT.CREATE`), or [`held_exclusively`].
const NO_STRIPE: u8 = 1;
/// [`HELD`]: a guard that holds stripes.
const STRIPES_HELD: u8 = 2;

thread_local! {
    /// Set while this thread holds an [`OrderGuard`], so that a write made
    /// from inside another — a script's `redis.call`, the commands of a
    /// transaction applied as one — does not try to take a stripe its own
    /// thread already holds. One of [`FREE`], [`NO_STRIPE`] and
    /// [`STRIPES_HELD`].
    static HELD: Cell<u8> = const { Cell::new(FREE) };
}

/// Held by a write from before it touches the store until its replication /
/// AOF payload has been queued.
pub struct OrderGuard {
    /// The stripe of a single-key write — nearly all of them — held without
    /// allocating.
    _one: Option<RwLockWriteGuard<'static, ()>>,
    /// Or held shared, by a write that only has to keep the others out
    /// ([`lock_key_shared`]).
    _shared: Option<RwLockReadGuard<'static, ()>>,
    /// The stripes of a write of several keys.
    _stripes: Stripes,
    /// Whether this guard is the one that set [`HELD`].
    outermost: bool,
    /// Tied to the thread that took it.
    _not_send: PhantomData<*const ()>,
}

/// The stripes an [`OrderGuard`] holds for a write of several keys: whole
/// ([`lock_keys`], [`lock_keys_in`]) or shared ([`lock_keys_shared`]). One
/// field for both: a guard of one key, nearly every one, has nothing more to
/// drop than it had.
enum Stripes {
    None,
    Whole(#[allow(dead_code)] Vec<RwLockWriteGuard<'static, ()>>),
    Shared(#[allow(dead_code)] Vec<RwLockReadGuard<'static, ()>>),
}

impl OrderGuard {
    /// A guard that holds no stripe.
    fn holding_none(outermost: bool) -> Self {
        OrderGuard {
            _one: None,
            _shared: None,
            _stripes: Stripes::None,
            outermost,
            _not_send: PhantomData,
        }
    }
}

impl Drop for OrderGuard {
    fn drop(&mut self) {
        if self.outermost {
            HELD.with(|h| h.set(FREE));
        }
    }
}

fn stripe_of(db: usize, key: &[u8]) -> usize {
    (xxhash_rust::xxh3::xxh3_64_with_seed(key, db as u64) as usize) & (STRIPES - 1)
}

/// Take the order lock of every key in `keys`, in database `db`.
///
/// Stripes are taken in index order, so two writes that share several keys
/// cannot deadlock. Nested on one thread, it takes nothing: the outer write's
/// guard already orders everything this thread does until it is dropped.
pub fn lock_keys<'a>(db: usize, keys: impl Iterator<Item = &'a [u8]>) -> OrderGuard {
    if HELD.with(|h| h.get()) != FREE {
        return nested();
    }
    let mut keys = keys;
    let first = keys.next().map(|key| stripe_of(db, key));
    let held = if first.is_some() {
        STRIPES_HELD
    } else {
        NO_STRIPE
    };
    HELD.with(|h| h.set(held));
    let mut guard = OrderGuard::holding_none(true);
    match (first, keys.next()) {
        (None, _) => {}
        (Some(only), None) => guard._one = Some(STRIPE[only].write()),
        (Some(first), Some(second)) => {
            let mut stripes: Vec<usize> = vec![first, stripe_of(db, second)];
            stripes.extend(keys.map(|key| stripe_of(db, key)));
            stripes.sort_unstable();
            stripes.dedup();
            guard._stripes =
                Stripes::Whole(stripes.into_iter().map(|i| STRIPE[i].write()).collect());
        }
    }
    guard
}

/// The order lock of `key`, in database `db`, held shared: for a write that
/// nothing listens to, by a command that reads and writes its one key under
/// one hold of the key's shard lock, so that two such writes of the key are
/// ordered by the shard already (see the module). It waits for, and keeps
/// out, every write that holds the key's order whole ([`lock_keys`]): one
/// that reads the key under one hold of the shard and writes it under
/// another (`SUNIONSTORE`, `BITFIELD`), one that may empty it (`SREM`), an
/// expiry, an eviction. Nested, it takes nothing, as [`lock_keys`] does.
pub fn lock_key_shared(db: usize, key: &[u8]) -> OrderGuard {
    if HELD.with(|h| h.get()) != FREE {
        return nested();
    }
    HELD.with(|h| h.set(STRIPES_HELD));
    let mut guard = OrderGuard::holding_none(true);
    guard._shared = Some(STRIPE[stripe_of(db, key)].read());
    guard
}

/// [`lock_key_shared`] for a write of several keys that reads and writes all
/// of them under every one of their shards' locks at once (`MSET`,
/// `MSETNX`): two such writes to a key are ordered by its shard, so they
/// run beside each other, and neither runs beside a write that holds a
/// stripe of theirs whole. Stripes are taken in index order, as
/// [`lock_keys`] takes them, so the two cannot deadlock.
///
/// Taking them whole queued every `MSET` behind every other that shared one
/// of its ten stripes, for the whole of the command: with 1,200 clients
/// that wait was longer than the work.
pub fn lock_keys_shared<'a>(db: usize, keys: impl Iterator<Item = &'a [u8]>) -> OrderGuard {
    if HELD.with(|h| h.get()) != FREE {
        return nested();
    }
    let mut stripes: Vec<usize> = keys.map(|key| stripe_of(db, key)).collect();
    stripes.sort_unstable();
    stripes.dedup();
    let held = if stripes.is_empty() {
        NO_STRIPE
    } else {
        STRIPES_HELD
    };
    HELD.with(|h| h.set(held));
    let mut guard = OrderGuard::holding_none(true);
    guard._stripes = Stripes::Shared(stripes.into_iter().map(|i| STRIPE[i].read()).collect());
    guard
}

/// [`lock_keys`] for keys of several databases, each `(db, key)`: for a
/// write that puts a key in another database than the one it names it in
/// (`MOVE`, `COPY ... DB`). Its record is then ordered against the writes to
/// the key in either database, the expiry of the key where it went among
/// them: one that took the key there before the `MOVE` was queued sent its
/// `DEL` ahead of the `MOVE`, and the replica, and a replay of the AOF, kept
/// the key there for good.
pub fn lock_keys_in<'a>(keys: impl Iterator<Item = (usize, &'a [u8])>) -> OrderGuard {
    if HELD.with(|h| h.get()) != FREE {
        return nested();
    }
    let mut stripes: Vec<usize> = keys.map(|(db, key)| stripe_of(db, key)).collect();
    stripes.sort_unstable();
    stripes.dedup();
    let held = if stripes.is_empty() {
        NO_STRIPE
    } else {
        STRIPES_HELD
    };
    HELD.with(|h| h.set(held));
    let mut guard = OrderGuard::holding_none(true);
    guard._stripes = Stripes::Whole(stripes.into_iter().map(|i| STRIPE[i].write()).collect());
    guard
}

/// Mark this thread as ordered without taking any stripe — for a write that
/// holds the write gate exclusively, and so has no other write to be ordered
/// against. Writes nested inside it then take nothing either.
pub fn held_exclusively() -> OrderGuard {
    let outermost = HELD.with(|h| {
        let free = h.get() == FREE;
        if free {
            h.set(NO_STRIPE);
        }
        free
    });
    OrderGuard::holding_none(outermost)
}

/// The order of one key, for a walk over many keys (the backfill of a
/// search index) that a command makes one key at a time: each key's write
/// order is held while the walk reads its value and indexes it, so that a
/// write to the key falls before the read, or after the indexing, and then
/// indexes the key itself.
///
/// Unlike [`lock_keys`], it takes the stripe from inside a guard that holds
/// none: the command's own, which names no key (`FT.ALTER`, `FT.CREATE`), or
/// [`held_exclusively`]'s. One stripe taken while none is held cannot
/// deadlock. Inside a guard that holds stripes (a transaction that also
/// writes keys) it takes nothing, as [`lock_keys`] would: the key's stripe
/// may be one of them.
pub fn lock_key_for_walk(db: usize, key: &[u8]) -> OrderGuard {
    match HELD.with(|h| h.get()) {
        FREE => lock_keys(db, std::iter::once(key)),
        NO_STRIPE => {
            let mut guard = OrderGuard::holding_none(false);
            guard._one = Some(STRIPE[stripe_of(db, key)].write());
            guard
        }
        _ => nested(),
    }
}

/// Wait for every write that holds the order of a key now, shared or
/// whole, to let go of it: each stripe taken whole and let go of at once,
/// one after another. For a change after which a write takes its key's
/// order whole where it took it shared before: the first search index
/// (`vector::Indexes::write_to_add`). A write that took a stripe before
/// this got to it has finished when this returns; one that takes it after
/// this let go of it sees what was done before this was called.
///
/// One stripe at a time, never two held: it cannot deadlock with a write
/// of several keys. Not on a thread that holds stripes ([`holds_stripes`]),
/// which would wait for itself, and which the caller asks first: there it
/// takes nothing. Skipping only that thread's own stripes would not do
/// either: a write holding another stripe may be waiting for one of them.
pub fn barrier() {
    if holds_stripes() {
        return;
    }
    for stripe in &STRIPE {
        drop(stripe.write());
    }
}

/// Whether this thread holds stripes, so that [`barrier`] cannot run on
/// it: a write of keys, or a script that runs beside others under its
/// keys' order (`tag_locks`).
pub fn holds_stripes() -> bool {
    HELD.with(|h| h.get()) == STRIPES_HELD
}

/// The guard of a write made from inside another: it takes nothing.
fn nested() -> OrderGuard {
    OrderGuard::holding_none(false)
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::sync::Arc;
    use std::sync::atomic::{AtomicBool, Ordering};
    use std::time::Duration;

    #[test]
    fn two_writes_to_one_key_take_turns() {
        let guard = lock_keys(0, [b"k".as_slice()].into_iter());
        let entered = Arc::new(AtomicBool::new(false));
        let flag = Arc::clone(&entered);
        let other = std::thread::spawn(move || {
            let _guard = lock_keys(0, [b"other".as_slice(), b"k".as_slice()].into_iter());
            flag.store(true, Ordering::SeqCst);
        });
        std::thread::sleep(Duration::from_millis(100));
        assert!(!entered.load(Ordering::SeqCst), "the second write got in");
        drop(guard);
        other.join().unwrap();
        assert!(entered.load(Ordering::SeqCst));
    }

    #[test]
    fn shared_holds_of_one_key_overlap() {
        overlap(
            || lock_key_shared(0, b"s"),
            || drop(lock_key_shared(0, b"s")),
        );
    }

    /// `beside`, on a thread of its own, gets what it takes while `hold`'s
    /// guard is held. A `barrier` of another test queued on the stripe keeps
    /// new shared holds out until the guard is let go of, and would wait for
    /// it for ever if this joined the thread first: then the guard is let
    /// go of, and it is tried again.
    fn overlap(hold: impl Fn() -> OrderGuard, beside: fn()) {
        for _ in 0..100 {
            let held = hold();
            let (got, get) = std::sync::mpsc::channel();
            let other = std::thread::spawn(move || {
                beside();
                let _ = got.send(());
            });
            let overlapped = get.recv_timeout(Duration::from_millis(500)).is_ok();
            drop(held);
            other.join().unwrap();
            if overlapped {
                return;
            }
        }
        panic!("the second shared hold never got in beside the first");
    }

    #[test]
    fn a_shared_hold_and_a_whole_one_take_turns() {
        // The whole hold waits for the shared one.
        let shared = lock_key_shared(0, b"t");
        let entered = Arc::new(AtomicBool::new(false));
        let flag = Arc::clone(&entered);
        let whole = std::thread::spawn(move || {
            let _guard = lock_keys(0, [b"t".as_slice()].into_iter());
            flag.store(true, Ordering::SeqCst);
        });
        std::thread::sleep(Duration::from_millis(100));
        assert!(!entered.load(Ordering::SeqCst), "the whole hold got in");
        drop(shared);
        whole.join().unwrap();
        assert!(entered.load(Ordering::SeqCst));

        // And the shared one for the whole one.
        let whole = lock_keys(0, [b"u".as_slice(), b"t".as_slice()].into_iter());
        let entered = Arc::new(AtomicBool::new(false));
        let flag = Arc::clone(&entered);
        let shared = std::thread::spawn(move || {
            let _guard = lock_key_shared(0, b"t");
            flag.store(true, Ordering::SeqCst);
        });
        std::thread::sleep(Duration::from_millis(100));
        assert!(!entered.load(Ordering::SeqCst), "the shared hold got in");
        drop(whole);
        shared.join().unwrap();
        assert!(entered.load(Ordering::SeqCst));
    }

    /// Two writes of several keys that hold their keys' order shared run
    /// beside each other, and a write that holds one of those keys whole
    /// waits for both; nested, they take nothing.
    #[test]
    fn shared_holds_of_several_keys_overlap_and_keep_a_whole_one_out() {
        let keys = [b"ms:a".as_slice(), b"ms:b".as_slice(), b"ms:c".as_slice()];
        overlap(
            || lock_keys_shared(0, keys.into_iter()),
            || {
                drop(lock_keys_shared(
                    0,
                    [b"ms:c".as_slice(), b"ms:a".as_slice()].into_iter(),
                ))
            },
        );
        let first = lock_keys_shared(0, keys.into_iter());
        let entered = Arc::new(AtomicBool::new(false));
        let flag = Arc::clone(&entered);
        let whole = std::thread::spawn(move || {
            let _guard = lock_keys(0, [b"ms:b".as_slice()].into_iter());
            flag.store(true, Ordering::SeqCst);
        });
        std::thread::sleep(Duration::from_millis(100));
        assert!(!entered.load(Ordering::SeqCst), "the whole hold got in");
        let inner = lock_keys(0, [b"ms:a".as_slice()].into_iter());
        drop(inner);
        assert_eq!(HELD.with(|h| h.get()), STRIPES_HELD);
        drop(first);
        assert_eq!(HELD.with(|h| h.get()), FREE);
        whole.join().unwrap();
        assert!(entered.load(Ordering::SeqCst));
    }

    #[test]
    fn a_shared_hold_nested_in_a_whole_one_takes_nothing() {
        let outer = lock_keys(0, [b"v".as_slice()].into_iter());
        // On this thread: it would wait for itself if it took the stripe.
        let inner = lock_key_shared(0, b"v");
        drop(inner);
        assert_eq!(HELD.with(|h| h.get()), STRIPES_HELD);
        drop(outer);
        assert_eq!(HELD.with(|h| h.get()), FREE);
        let shared = lock_key_shared(0, b"v");
        assert_eq!(HELD.with(|h| h.get()), STRIPES_HELD);
        let inner = lock_keys(0, [b"v".as_slice()].into_iter());
        drop(inner);
        // Nor does a walk's hold: the key's stripe may be the one held.
        drop(lock_key_for_walk(0, b"v"));
        drop(shared);
        assert_eq!(HELD.with(|h| h.get()), FREE);
    }

    #[test]
    fn a_write_to_a_key_in_two_databases_orders_both() {
        let guard = lock_keys_in([(0, b"m".as_slice()), (3, b"m".as_slice())].into_iter());
        let entered = Arc::new(AtomicBool::new(false));
        let flag = Arc::clone(&entered);
        // An expiry of the key in database 3.
        let other = std::thread::spawn(move || {
            let _guard = lock_keys(3, [b"m".as_slice()].into_iter());
            flag.store(true, Ordering::SeqCst);
        });
        std::thread::sleep(Duration::from_millis(100));
        assert!(!entered.load(Ordering::SeqCst), "the expiry got in");
        drop(guard);
        other.join().unwrap();
        assert!(entered.load(Ordering::SeqCst));
    }

    #[test]
    fn writes_to_different_keys_do_not_wait() {
        // Different stripes, by construction of the names.
        let (a, b) = (b"order:a".as_slice(), b"order:b".as_slice());
        assert_ne!(stripe_of(0, a), stripe_of(0, b));
        let _guard = lock_keys(0, [a].into_iter());
        std::thread::spawn(move || {
            let _guard = lock_keys(0, [b].into_iter());
        })
        .join()
        .unwrap();
    }

    #[test]
    fn nested_and_repeated_keys_do_not_deadlock() {
        let outer = lock_keys(
            0,
            [b"n".as_slice(), b"n".as_slice(), b"m".as_slice()].into_iter(),
        );
        // The same key again, from inside: nothing is taken.
        let inner = lock_keys(0, [b"n".as_slice()].into_iter());
        drop(inner);
        // The outer guard is still the one in charge.
        assert_eq!(HELD.with(|h| h.get()), STRIPES_HELD);
        drop(outer);
        assert_eq!(HELD.with(|h| h.get()), FREE);

        let exclusive = held_exclusively();
        let inner = lock_keys(0, [b"n".as_slice()].into_iter());
        drop(inner);
        assert_eq!(HELD.with(|h| h.get()), NO_STRIPE);
        drop(exclusive);
        assert_eq!(HELD.with(|h| h.get()), FREE);
    }

    #[test]
    fn a_walk_inside_a_command_that_names_no_key_orders_each_key() {
        // The guard of `FT.ALTER`: no key, no stripe.
        let command = lock_keys(0, std::iter::empty());
        let walking = lock_key_for_walk(0, b"doc:1");
        let entered = Arc::new(AtomicBool::new(false));
        let flag = Arc::clone(&entered);
        // An `HSET` of the key the walk is at.
        let write = std::thread::spawn(move || {
            let _guard = lock_keys(0, [b"doc:1".as_slice()].into_iter());
            flag.store(true, Ordering::SeqCst);
        });
        std::thread::sleep(Duration::from_millis(100));
        assert!(!entered.load(Ordering::SeqCst), "the write got in");
        drop(walking);
        write.join().unwrap();
        assert!(entered.load(Ordering::SeqCst));
        // The command's guard is still the one in charge.
        assert_eq!(HELD.with(|h| h.get()), NO_STRIPE);
        drop(command);
        assert_eq!(HELD.with(|h| h.get()), FREE);

        // Inside a guard with stripes it takes nothing: no deadlock on a
        // stripe this thread holds.
        let outer = lock_keys(0, [b"doc:1".as_slice()].into_iter());
        drop(lock_key_for_walk(0, b"doc:1"));
        drop(outer);
        // With nothing held, it is an ordinary guard.
        drop(lock_key_for_walk(0, b"doc:1"));
        assert_eq!(HELD.with(|h| h.get()), FREE);
    }

    #[test]
    fn opposite_key_orders_do_not_deadlock() {
        let threads: Vec<_> = (0..8)
            .map(|t| {
                std::thread::spawn(move || {
                    for i in 0..2000 {
                        let keys: [&[u8]; 3] = if (t + i) % 2 == 0 {
                            [b"x", b"y", b"z"]
                        } else {
                            [b"z", b"y", b"x"]
                        };
                        let _guard = lock_keys(0, keys.into_iter());
                    }
                })
            })
            .collect();
        for t in threads {
            t.join().unwrap();
        }
    }
}