tinystore 0.6.0

A small storage runtime for applications on SQLite: SQL, key-value state, durable jobs, files, search, logs and metrics in one directory
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
use std::sync::mpsc;
use std::sync::{Arc, Mutex};
use std::thread;
use std::time::Duration;

use super::*;
use crate::kv::fixture::*;
use crate::testing::{PATIENCE, holds_within};
use crate::{Clock, unix_millis};

/// Long enough for a thread to reach where a test waits for it.
fn locks(store: &Store) -> Arc<Locks> {
    Locks::of(store).unwrap()
}

/// Holds `key` on a thread of its own until told, saying once it holds.
fn held_elsewhere(lock: &Lock, key: &'static str) -> (mpsc::Sender<()>, thread::JoinHandle<Result<Fence>>) {
    let (holds, held) = mpsc::channel();
    let (go, told) = mpsc::channel::<()>();
    let lock = lock.clone();
    let holder = thread::spawn(move || {
        lock.hold(key, |lease| {
            holds.send(lease.fence()).unwrap();
            told.recv_timeout(PATIENCE).unwrap();
            Ok(lease.fence())
        })
    });
    held.recv_timeout(PATIENCE).unwrap();
    (go, holder)
}

// contract: LOCK-01
#[test]
fn a_key_is_held_by_one_holder_at_a_time_and_given_on_when_let_go() {
    let f = fixture();
    let invoices = f.store.lock("invoices").open().unwrap();
    let (go, first) = held_elsewhere(&invoices, "42");

    assert!(invoices.try_hold("42", |_| Ok::<_, Error>(())).unwrap().is_none(), "held: nothing ran");
    assert_eq!(invoices.try_hold("43", |_| Ok::<_, Error>("another key")).unwrap(), Some("another key"));

    let second = {
        let invoices = invoices.clone();
        thread::spawn(move || invoices.hold("42", |lease| Ok::<_, Error>(lease.fence())))
    };
    let locks = locks(&f.store);
    holds_within(PATIENCE, "the second in line", || locks.waiting() == 1);
    go.send(()).unwrap();
    let first = first.join().unwrap().unwrap();
    let second = second.join().unwrap().unwrap();
    assert!(second > first, "a later lease's fence is larger: {first:?} then {second:?}");
    assert_eq!(locks.waiting(), 0);
}

// contract: LOCK-01
#[test]
fn waiters_are_served_in_the_order_they_came() {
    let f = fixture();
    let reports = f.store.lock("reports").open().unwrap();
    let (go, first) = held_elsewhere(&reports, "sales");
    let locks = locks(&f.store);
    let served = Arc::new(Mutex::new(Vec::new()));
    let mut waiters = Vec::new();
    for name in ["b", "c", "d"] {
        let (reports, served) = (reports.clone(), Arc::clone(&served));
        waiters.push(thread::spawn(move || {
            reports.hold("sales", |_| {
                served.lock().unwrap().push(name);
                Ok::<_, Error>(())
            })
        }));
        let behind = waiters.len();
        holds_within(PATIENCE, "the next in line", || locks.waiting() == behind);
    }
    go.send(()).unwrap();
    first.join().unwrap().unwrap();
    for waiter in waiters {
        waiter.join().unwrap().unwrap();
    }
    assert_eq!(*served.lock().unwrap(), ["b", "c", "d"]);
}

#[test]
fn a_key_still_held_after_the_wait_is_in_use() {
    let f = fixture();
    let invoices = f.store.lock("invoices").open().unwrap();
    let (go, first) = held_elsewhere(&invoices, "42");
    let waiter = {
        let invoices = invoices.clone();
        thread::spawn(move || invoices.key("42").wait(5 * SECOND).hold(|_| Ok::<_, Error>(())))
    };
    let locks = locks(&f.store);
    holds_within(PATIENCE, "the waiter in line", || locks.waiting() == 1);
    f.clock.advance(5 * SECOND - Duration::from_millis(1));
    f.store.clock_moved();
    thread::sleep(Duration::from_millis(20));
    assert_eq!(locks.waiting(), 1, "a millisecond short of its wait");
    f.clock.advance(Duration::from_millis(1));
    f.store.clock_moved();
    let error = waiter.join().unwrap().unwrap_err();
    assert_eq!(error.kind(), ErrorKind::InUse, "{error}");
    assert!(error.to_string().starts_with("lock invoices: key \"42\""), "{error}");
    go.send(()).unwrap();
    first.join().unwrap().unwrap();
}

// contract: LOCK-03
#[test]
fn max_hold_revokes_a_lease_gives_the_key_on_and_fails_the_hold_lost() {
    let f = fixture();
    let reports = f.store.lock("reports").max_hold(10 * SECOND).open().unwrap();
    let (stuck, told) = mpsc::channel::<()>();
    let (ran, running) = mpsc::channel();
    let hung = {
        let reports = reports.clone();
        thread::spawn(move || {
            reports.hold("sales", |lease| {
                ran.send(()).unwrap();
                told.recv_timeout(PATIENCE).unwrap();
                Ok::<_, Error>(lease.lost())
            })
        })
    };
    running.recv_timeout(PATIENCE).unwrap();
    let next = {
        let reports = reports.clone();
        thread::spawn(move || reports.key("sales").wait(MINUTE).hold(|lease| Ok::<_, Error>(lease.fence())))
    };
    let locks = locks(&f.store);
    holds_within(PATIENCE, "the next in line", || locks.waiting() == 1);
    f.clock.advance(10 * SECOND);
    f.store.clock_moved();
    next.join().unwrap().expect("revoked at max_hold, the key goes to the next in line");
    stuck.send(()).unwrap();
    let error = hung.join().unwrap().unwrap_err();
    assert_eq!(error.kind(), ErrorKind::Lost, "{error}");
}

// contract: LOCK-04
#[test]
fn a_hold_inside_its_own_hold_or_inside_a_transaction_is_invalid() {
    let f = fixture();
    let invoices = f.store.lock("invoices").open().unwrap();
    let nested = invoices.hold("42", |_| invoices.hold("42", |_| Ok::<_, Error>(())));
    assert_eq!(nested.unwrap_err().kind(), ErrorKind::Invalid);
    let other = invoices.hold("42", |_| invoices.hold("43", |_| Ok::<_, Error>("another key")));
    assert_eq!(other.unwrap(), "another key");

    let stock = f.store.bucket::<i64>("stock").open().unwrap();
    let inside = f.store.tx(|tx| {
        tx.with(&stock).set("sku", &1)?;
        invoices.hold("42", |_| Ok::<_, Error>(()))
    });
    let error = inside.unwrap_err();
    assert_eq!(error.kind(), ErrorKind::Invalid, "{error}");
    assert!(invoices.try_hold("42", |_| Ok::<_, Error>(())).unwrap().is_some(), "nothing was left held");
}

// contract: LOCK-02
#[test]
fn fences_grow_with_every_lease_and_across_a_restart() {
    let f = fixture();
    let invoices = f.store.lock("invoices").open().unwrap();
    let first = invoices.hold("42", |lease| Ok::<_, Error>(lease.fence())).unwrap();
    let second = invoices.hold("43", |lease| Ok::<_, Error>(lease.fence())).unwrap();
    assert!(second > first, "one count for every key");
    let epoch = locks(&f.store).epoch();
    assert_eq!(first.get() >> 64, u128::from(epoch));

    let f = f.reopen();
    assert_eq!(locks(&f.store).epoch(), epoch + 1, "the clock did not move: one past the last epoch");
    let invoices = f.store.lock("invoices").open().unwrap();
    let after = invoices.hold("42", |lease| Ok::<_, Error>(lease.fence())).unwrap();
    assert!(after > second, "{second:?} before the restart, {after:?} after");

    f.clock.advance(HOUR);
    let f = f.reopen();
    let now = u64::try_from(unix_millis(f.clock.now())).unwrap();
    assert_eq!(locks(&f.store).epoch(), now, "the clock went past the last epoch: its time");
}

#[test]
fn a_semaphore_admits_its_limit_a_key_and_a_lock_of_its_name_is_another() {
    let f = fixture();
    let uploads = f.store.semaphore("uploads").limit(2).open().unwrap();
    let inside =
        uploads.hold("ann", |_| uploads.hold("ann", |_| Ok::<_, Error>("nested on one thread is the same key")));
    assert_eq!(inside.unwrap_err().kind(), ErrorKind::Invalid);

    let (go, holders): (Vec<_>, Vec<_>) = (0..2)
        .map(|_| {
            let (held, holds) = mpsc::channel();
            let (go, told) = mpsc::channel::<()>();
            let uploads = uploads.clone();
            let holder = thread::spawn(move || {
                uploads.hold("ann", |_| {
                    held.send(()).unwrap();
                    told.recv_timeout(PATIENCE).unwrap();
                    Ok::<_, Error>(())
                })
            });
            holds.recv_timeout(PATIENCE).unwrap();
            (go, holder)
        })
        .unzip();
    assert!(uploads.try_hold("ann", |_| Ok::<_, Error>(())).unwrap().is_none(), "two of two hold it");
    assert!(uploads.try_hold("bob", |_| Ok::<_, Error>(())).unwrap().is_some(), "a limit a key");
    let lock = f.store.lock("uploads").open().unwrap();
    assert!(lock.try_hold("ann", |_| Ok::<_, Error>(())).unwrap().is_some(), "the lock of the name is another");
    for (go, holder) in go.into_iter().zip(holders) {
        go.send(()).unwrap();
        holder.join().unwrap().unwrap();
    }
}

// contract: LOCK-06
#[test]
fn a_panic_lets_the_key_go() {
    let f = fixture();
    let invoices = f.store.lock("invoices").open().unwrap();
    let panicked = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
        invoices.hold("42", |_| -> Result<()> { panic!("the holder's function panicked") })
    }));
    assert!(panicked.is_err());
    assert!(invoices.try_hold("42", |_| Ok::<_, Error>(())).unwrap().is_some());
}

// contract: LOCK-06
#[test]
fn closing_the_store_ends_every_lease_and_answers_every_waiter() {
    let f = fixture();
    let invoices = f.store.lock("invoices").open().unwrap();
    let (stuck, told) = mpsc::channel::<()>();
    let (ran, running) = mpsc::channel();
    let holder = {
        let invoices = invoices.clone();
        thread::spawn(move || {
            invoices.hold("42", |lease| {
                ran.send(()).unwrap();
                told.recv_timeout(PATIENCE).unwrap();
                Ok::<_, Error>(lease.lost())
            })
        })
    };
    running.recv_timeout(PATIENCE).unwrap();
    let waiter = {
        let invoices = invoices.clone();
        thread::spawn(move || invoices.hold("42", |_| Ok::<_, Error>(())))
    };
    let locks = locks(&f.store);
    holds_within(PATIENCE, "the waiter in line", || locks.waiting() == 1);
    f.store.close().unwrap();
    assert_eq!(waiter.join().unwrap().unwrap_err().kind(), ErrorKind::Closed);
    stuck.send(()).unwrap();
    assert_eq!(holder.join().unwrap().unwrap_err().kind(), ErrorKind::Closed);
    assert_eq!(invoices.try_hold("42", |_| Ok::<_, Error>(())).unwrap_err().kind(), ErrorKind::Closed);
}

#[test]
fn names_keys_limits_and_bounds_are_checked() {
    let f = fixture();
    assert_eq!(f.store.lock("Invoices").open().unwrap_err().kind(), ErrorKind::Invalid);
    assert_eq!(f.store.semaphore("uploads").open().unwrap_err().kind(), ErrorKind::Invalid, "no limit");
    assert_eq!(f.store.semaphore("uploads").limit(0).open().unwrap_err().kind(), ErrorKind::Invalid);
    assert_eq!(f.store.semaphore("uploads").limit(1001).open().unwrap_err().kind(), ErrorKind::Invalid);
    assert_eq!(f.store.lock("a").max_hold(Duration::ZERO).open().unwrap_err().kind(), ErrorKind::Invalid);
    let invoices = f.store.lock("invoices").open().unwrap();
    assert_eq!(invoices.hold("", |_| Ok::<_, Error>(())).unwrap_err().kind(), ErrorKind::Invalid);
    let long = "k".repeat(1025);
    assert_eq!(invoices.hold(long.as_str(), |_| Ok::<_, Error>(())).unwrap_err().kind(), ErrorKind::Invalid);
    assert_eq!(invoices.hold(42, |_| Ok::<_, Error>("an integer key")).unwrap(), "an integer key");
}

/// What a holder through a connection asks: a lease that lasts `ttl` unless
/// renewed.
#[cfg(feature = "wire")]
fn remote(key: &str, ttl: Duration, wait: Duration, request: Option<lease::RequestId>) -> Ask {
    let place = Place::new(Kind::Lock, Arc::from("invoices"), key.to_owned());
    Ask { place, limit: 1, ttl: Some(ttl), max_hold: None, wait, request }
}

#[cfg(feature = "wire")]
fn asked(locks: &Locks, ask: Ask) -> (Option<u64>, mpsc::Receiver<Result<Answer>>) {
    let (answered, answer) = mpsc::channel();
    let waiting = locks.ask(ask, Box::new(move |answer| drop(answered.send(answer))));
    (waiting, answer)
}

#[cfg(feature = "wire")]
fn granted(answer: &mpsc::Receiver<Result<Answer>>) -> Granted {
    match answer.recv_timeout(PATIENCE).unwrap().unwrap() {
        Answer::Granted(granted) => granted,
        Answer::Busy => panic!("busy where a grant was due"),
    }
}

// contract: LOCK-05
#[cfg(feature = "wire")]
#[test]
fn a_lease_through_a_connection_lasts_its_ttl_from_its_last_renewal() {
    let f = fixture();
    let locks = locks(&f.store);
    let (_, first) = asked(&locks, remote("42", 10 * SECOND, Duration::ZERO, None));
    let first = granted(&first);
    let (waiting, second) = asked(&locks, remote("42", 10 * SECOND, MINUTE, None));
    assert!(waiting.is_some());

    f.clock.advance(9 * SECOND);
    let deadline = locks.renew(&first.lease, 10 * SECOND).unwrap();
    assert_eq!(deadline, unix_millis(f.clock.now()) + 10_000, "ten seconds from the renewal");
    f.clock.advance(9 * SECOND);
    f.store.clock_moved();
    thread::sleep(Duration::from_millis(20));
    assert!(second.try_recv().is_err(), "renewed, the first still holds it");

    f.clock.advance(SECOND);
    f.store.clock_moved();
    let second = granted(&second);
    assert!(second.fence > first.fence);
    assert_eq!(first.watch.ending(), Ending::Lost, "the first is told it lost the key");
    let renewed = locks.renew(&first.lease, 10 * SECOND).unwrap_err();
    assert_eq!(renewed.kind(), ErrorKind::Lost, "a lease that ran out is never renewed");
    assert!(!locks.release(&first.lease), "nor let go of the key another holds now");
    assert!(locks.release(&second.lease));
}

#[cfg(feature = "wire")]
#[test]
fn a_request_asked_again_answers_its_lease_or_takes_over_its_place_in_line() {
    let f = fixture();
    let locks = locks(&f.store);
    let request = Some([7; 16]);
    let (_, answer) = asked(&locks, remote("42", 10 * SECOND, Duration::ZERO, request));
    let lease = granted(&answer);
    let (_, again) = asked(&locks, remote("42", 10 * SECOND, Duration::ZERO, request));
    let again = granted(&again);
    assert_eq!((again.lease, again.fence), (lease.lease, lease.fence), "the same lease, not the key twice");

    let waiting_request = Some([8; 16]);
    let (waiting, lost) = asked(&locks, remote("42", 10 * SECOND, MINUTE, waiting_request));
    assert!(waiting.is_some());
    let (waiting_again, taken_over) = asked(&locks, remote("42", 10 * SECOND, MINUTE, waiting_request));
    assert_eq!(waiting_again, waiting, "the place taken over keeps its waiter's id, which a cancel names");
    assert_eq!(lost.recv_timeout(PATIENCE).unwrap().err().map(|error| error.kind()), Some(ErrorKind::Cancelled));
    assert_eq!(locks.waiting(), 1, "one place in line");
    assert!(locks.release(&lease.lease));
    granted(&taken_over);
}

// contract: LOCK-01
// contract: LOCK-05
#[cfg(feature = "wire")]
#[test]
fn a_request_asked_again_once_its_lease_ran_out_is_asked_afresh_and_the_key_is_held_once() {
    let f = fixture();
    let locks = locks(&f.store);
    let request = Some([7; 16]);
    let (_, answer) = asked(&locks, remote("42", 10 * SECOND, Duration::ZERO, request));
    let lease = granted(&answer);
    // the answer was lost, and the client asks again after the ttl, nobody asking between
    f.clock.advance(11 * SECOND);
    let (_, again) = asked(&locks, remote("42", 10 * SECOND, Duration::ZERO, request));
    let again = granted(&again);
    assert!(again.fence > lease.fence, "a lease of its own, not the one that ran out");
    assert_eq!(lease.watch.ending(), Ending::Lost);
    let (_, other) = asked(&locks, remote("42", 10 * SECOND, Duration::ZERO, None));
    let busy = other.recv_timeout(PATIENCE).unwrap().unwrap();
    assert!(matches!(busy, Answer::Busy), "one holder at a time");
    assert!(locks.release(&again.lease));
}

// contract: LOCK-01
// contract: LOCK-05
#[cfg(feature = "wire")]
#[test]
fn a_request_asked_again_late_in_its_lease_holds_it_for_its_ttl_from_the_answer() {
    let f = fixture();
    let locks = locks(&f.store);
    let request = Some([7; 16]);
    let (_, answer) = asked(&locks, remote("42", 10 * SECOND, Duration::ZERO, request));
    let lease = granted(&answer);
    // the answer was lost, and the client asks again late in the ttl: it counts its deadline from this answer
    f.clock.advance(9 * SECOND);
    let (_, again) = asked(&locks, remote("42", 10 * SECOND, Duration::ZERO, request));
    let again = granted(&again);
    assert_eq!((again.lease, again.fence), (lease.lease, lease.fence), "the same lease");
    assert_eq!(again.expires, Some(unix_millis(f.clock.now()) + 10_000), "ten seconds from the answer");
    f.clock.advance(2 * SECOND);
    f.store.clock_moved();
    let (_, other) = asked(&locks, remote("42", 10 * SECOND, Duration::ZERO, None));
    let busy = other.recv_timeout(PATIENCE).unwrap().unwrap();
    assert!(matches!(busy, Answer::Busy), "the client still holds it, as it believes");
    assert_eq!(lease.watch.ending(), Ending::Live);
    locks.renew(&lease.lease, 10 * SECOND).unwrap();
    assert!(locks.release(&lease.lease));
}

// contract: LOCK-05
#[cfg(feature = "wire")]
#[test]
fn a_lease_of_a_client_gone_is_let_go_at_its_deadline_though_nobody_asks_its_key() {
    let f = fixture();
    let locks = locks(&f.store);
    let (_, answer) = asked(&locks, remote("job-7f3a", 10 * SECOND, Duration::ZERO, Some([9; 16])));
    let lease = granted(&answer);
    f.clock.advance(10 * SECOND);
    f.store.clock_moved();
    holds_within(PATIENCE, "the lease ran out", || lease.watch.ending() == Ending::Lost);
    assert_eq!(locks.kept(), 0, "nothing of it is kept");
}

// contract: LOCK-06
#[cfg(feature = "wire")]
#[test]
fn a_cancelled_waiter_leaves_its_line() {
    let f = fixture();
    let locks = locks(&f.store);
    let (_, held) = asked(&locks, remote("42", 10 * SECOND, Duration::ZERO, None));
    let held = granted(&held);
    let (waiting, gone) = asked(&locks, remote("42", 10 * SECOND, MINUTE, None));
    let (_, next) = asked(&locks, remote("42", 10 * SECOND, MINUTE, None));
    locks.cancel(waiting.unwrap());
    assert_eq!(locks.waiting(), 1);
    assert!(locks.release(&held.lease));
    granted(&next);
    assert!(gone.try_recv().is_err(), "a cancelled waiter is answered nothing");
}

// contract: STORE-10
#[cfg(feature = "async")]
#[test]
fn a_hold_tried_in_a_point_read_is_refused_and_holds_nothing() {
    let f = fixture();
    let invoices = f.store.lock("invoices").open().unwrap();
    let tried = crate::polled::without_waiting(|| invoices.try_hold("42", |_| Ok::<_, Error>("ran")));
    assert!(tried.is_none(), "a lock's answer comes from its own thread");
    assert_eq!(locks(&f.store).waiting(), 0);
    assert_eq!(invoices.try_hold("42", |_| Ok::<_, Error>("held")).unwrap(), Some("held"), "nothing held the key");
}