tinystore 0.8.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
//! How an awaited call runs: the helpers every engine's async calls are
//! built on, so that each says where its work runs and what its future's
//! drop does in one word.
//!
//! | helper                | runs                                                 | its future dropped once polled  |
//! |-----------------------|------------------------------------------------------|---------------------------------|
//! | [`point`]             | in the first poll when a reader is idle, else pool   | answered in that poll, or stops |
//! | [`step`]              | in the first poll, else on the pool                  | applied all the same            |
//! | [`read`]              | on the store's pool, under a token of its own        | stops; its reader goes back     |
//! | [`write`]             | a submit in the first poll, its commit on the pool   | commits; its answer is let go   |
//! | [`reserve`]           | a waiter in the store's memory, on no thread         | leaves the line                 |
//! | [`blocking`]          | blocking code on the store's pool, in a line or not  | runs to its end                 |
//! | [`passed_on`]         | a call to a copy's home, on a thread of its own      | runs to its end                 |
//! | [`passed_on_in_line`] | its turn in a line of the pool, then as `passed_on`  | runs to its end                 |
//! | [`holding`]           | as `blocking`, its memory awaited on no thread       | runs to its end                 |
//!
//! Each does nothing before its first poll. A call clones its handle and
//! owns its arguments, so that its future is `Send + 'static` and a program
//! can spawn it. Each future captures its type parameters alone,
//! `use<T, R>`, and never a borrow of an argument: a builder taken by
//! value, as a list's `page(self)`, lets go of what it lends as the call
//! returns.
//!
//! The pool's sixteen threads at most are shared by every awaited call,
//! transaction and commit's lead, and by the pipe's connections, so a call
//! passed on to a copy's home waits for its answer on a thread of its own: a
//! round trip, or up to 25 s for a once run's claim, which the home answers
//! once the run under way there ends. Two waits for another node still hold
//! a thread of the pool: a read on a copy that passes an idle key's renewal
//! on, and a commit's lead at a home whose copies are synchronous.
//!
//! What runs in a poll, a point read or a write's submit, parks its thread
//! nowhere: each wait `polled::may_wait` guards refuses a point read, which
//! runs again on the pool, and fails a submit `Internal`. Some waits no check
//! guards yet, and an engine keeps them out of a poll itself: a search's
//! wait for its index to settle, a records tail's wait for what arrives, the
//! codec records' appends compress with, a blob's sync of its directory and
//! its retries of a rename, a channel's receive, a job's watch and a
//! worker's wait for jobs, and the waits of a home with synchronous copies,
//! inside a commit.

use std::future::Future;
use std::panic::{AssertUnwindSafe, catch_unwind};
use std::sync::Arc;
use std::time::Duration;

use super::oneshot::{Answer, pending};
use crate::cancel::{self, Captured};
use crate::memory::PATIENCE;
use crate::polled::{self, Lead};
use crate::pool::Pool;
use crate::{CancellationToken, Error, Memory, Reservation, Result};

/// A point read, as a key's value or a counter: tried in the future's first
/// poll, on the polling thread, where one of its file's readers is idle and
/// it answers in microseconds; run as [`read`] runs on the pool where it
/// would wait for a reader or open one, write, submitted or blocking, wait
/// for memory, or make any other wait `polled::may_wait` guards.
///
/// So `read` may run twice, in the poll up to a wait and then on the pool:
/// it must submit nothing and change nothing before its last possible wait,
/// nor after a wait it was refused, whose error it passes up unchanged. The
/// tokens around this call reach it in both places, as [`read`] says.
#[allow(dead_code, reason = "the engines' async point reads are built on it")]
pub(crate) fn point<T, R>(
    pool: &Arc<Pool>,
    what: String,
    read: R,
) -> impl Future<Output = Result<T>> + Send + 'static + use<T, R>
where
    R: Fn() -> Result<T> + Send + 'static,
    T: Send + 'static,
{
    let (tokens, pool) = (cancel::captured(), Arc::clone(pool));
    async move {
        if let Some(answer) = polled::without_waiting(|| tokens.run(None, &read)) {
            return answer;
        }
        on_the_pool(pool, tokens, what, read).await
    }
}

/// A step that changes what memory holds, as an add of counters kept in
/// memory or a rate limit's allow: tried in the future's first poll as
/// [`point`] tries a read, and run where it would wait there as [`blocking`]
/// runs, on the pool and to its end, whether or not anyone still awaits it. A
/// step is a write that commits with a later flush, so it must not stop half
/// way, as a read whose future is dropped does (DUR-11).
///
/// `step` may run twice, as [`point`]'s read may: it must change nothing
/// before its last possible wait, nor after a wait it was refused. The tokens
/// around this call reach it in both places, and stop its reads as they stop
/// its blocking twin's.
#[allow(dead_code, reason = "kv's awaited steps in memory are built on it")]
pub(crate) fn step<T, S>(pool: &Arc<Pool>, what: String, step: S) -> impl Future<Output = Result<T>> + Send + 'static
where
    S: Fn() -> Result<T> + Send + 'static,
    T: Send + 'static,
{
    let (tokens, pool) = (cancel::captured(), Arc::clone(pool));
    async move {
        if let Some(answer) = polled::without_waiting(|| tokens.run(None, &step)) {
            return answer;
        }
        blocking(&pool, None, what, move || tokens.run(None, step)).await
    }
}

/// Runs `read` on the store's pool once the future is first polled, under a
/// token of its own that the future's drop cancels: a read whose future is
/// dropped stops where it is, its statement interrupted and its reader given
/// back at once, and one whose future went before a thread took it never
/// runs. `tokio::time::timeout` around the call stops it so.
///
/// The tokens of the thread that calls this, a program's
/// `token.run(|| db.all(q))`, reach the read on the pool too: an engine calls
/// it in its method's body, where the future is made, never inside an
/// `async` block, which runs where it is polled.
#[allow(dead_code, reason = "the engines' async reads are built on it")]
pub(crate) fn read<T, R>(
    pool: &Arc<Pool>,
    what: String,
    read: R,
) -> impl Future<Output = Result<T>> + Send + 'static + use<T, R>
where
    R: FnOnce() -> Result<T> + Send + 'static,
    T: Send + 'static,
{
    on_the_pool(Arc::clone(pool), cancel::captured(), what, read)
}

async fn on_the_pool<T, R>(pool: Arc<Pool>, tokens: Captured, what: String, read: R) -> Result<T>
where
    R: FnOnce() -> Result<T> + Send + 'static,
    T: Send + 'static,
{
    refuse_inside_a_transaction(&what)?;
    let token = CancellationToken::new();
    let _dropped = CancelOnDrop(token.clone());
    let (answer, answered) = pending(what);
    pool.run(Box::new(move || {
        if !answer.is_awaited() {
            return;
        }
        match catch_unwind(AssertUnwindSafe(|| tokens.run(Some(&token), read))) {
            Ok(outcome) => answer.send(outcome),
            Err(payload) => answer.panicked(payload),
        }
    }));
    answered.await
}

/// Cancels an awaited read's own token as its future is dropped: what the
/// read runs then stops, and what it has not started never starts.
struct CancelOnDrop(CancellationToken);

impl Drop for CancelOnDrop {
    fn drop(&mut self) {
        self.0.cancel();
    }
}

/// Takes `bytes` of the store's memory as [`Memory::reserve`] does, in the
/// same line, its waiters served in the order they came, but parks no
/// thread: the future is woken by whoever gives the bytes back, or fails
/// `Limit` on the store's timer once `patience` passed. Dropped while it
/// waits, it leaves the line, and the waiters behind it wait for less.
#[allow(dead_code, reason = "the engines' async writes that hold memory reserve through it")]
pub(crate) fn reserve(
    memory: &Arc<Memory>,
    bytes: u64,
    what: String,
    patience: Duration,
) -> impl Future<Output = Result<Reservation>> + Send + 'static + use<> {
    let memory = Arc::clone(memory);
    async move {
        let (answer, answered) = pending(what.clone());
        let _waiting = memory.reserve_then(bytes, &what, patience, move |reserved| answer.send(reserved));
        answered.await
    }
}

/// Runs `submit` in the future's first poll, on the polling thread, with
/// every commit a write it submits would lead there handed to the store's
/// pool, and answers with what `submit` gives its [`Answer`]: an engine's
/// `_then` form, its `done` sending the answer. A write that refuses before
/// it is queued, as one on a closed file, answers in that poll.
///
/// `submit` encodes its arguments and calls `File::submit`, nothing more: a
/// read, a blocking write or a patient reservation in it fails `Internal`,
/// and a wait `polled::may_wait` does not guard parks the executor. A form
/// that reads first, as sql's `rows_then` does, runs that read through
/// [`read`] or [`blocking`] before it submits, or is split in two.
///
/// Dropped before its first poll it writes nothing. Once polled the write
/// is queued, and it commits with its group whether or not anyone still
/// awaits it: only its answer is let go.
///
/// ```ignore
/// let (bucket, key, bytes) = (self.blocking.clone(), key.to_owned(), encoded);
/// run::write(&self.pool, format!("{bucket:?}: a set"), move |answer| {
///     bucket.put_raw_then(&key, bytes, move |written| answer.send(written))
/// })
/// ```
#[allow(dead_code, reason = "the engines' async writes are built on it")]
pub(crate) fn write<T, S>(
    pool: &Arc<Pool>,
    what: String,
    submit: S,
) -> impl Future<Output = Result<T>> + Send + 'static + use<T, S>
where
    S: FnOnce(Answer<T>) + Send + 'static,
    T: Send + 'static,
{
    let lead = lead_on(pool);
    async move {
        let (answer, answered) = pending(what);
        polled::leading_on(&lead, || submit(answer));
        answered.await
    }
}

/// What hands a commit's lead to `pool`, or to a thread of its own when
/// every thread of the pool is busy or once the pool stopped as its store
/// closes: never back to the polling thread.
pub(crate) fn lead_on(pool: &Arc<Pool>) -> Lead {
    let pool = Arc::clone(pool);
    Arc::new(move |job| pool.run_always(job))
}

/// Runs `work` on the store's pool once the future is first polled, after
/// the jobs queued on `line` before it when one is named, and answers with
/// what it returns. Once polled it runs to its end whether or not anyone
/// still awaits it: a transaction's closure, which holds its file's writer,
/// or a write the core answers no other way. A panic in `work` is resumed
/// where the future is awaited.
///
/// `what` names the call in its errors: `store data: a transaction`.
#[allow(dead_code, reason = "the store's transactions run on it with kv or jobs, and the engines' blocking work")]
pub(crate) fn blocking<T, F>(
    pool: &Arc<Pool>,
    line: Option<String>,
    what: String,
    work: F,
) -> impl Future<Output = Result<T>> + Send + 'static + use<T, F>
where
    F: FnOnce() -> Result<T> + Send + 'static,
    T: Send + 'static,
{
    let pool = Arc::clone(pool);
    async move {
        refuse_inside_a_transaction(&what)?;
        let (answer, answered) = pending(what);
        let job = Box::new(move || answer_with(answer, work));
        match line {
            Some(line) => pool.run_in_line(&line, job),
            None => pool.run(job),
        }
        answered.await
    }
}

/// The name of the threads that wait for a copy's home.
const PASSED_ON: &str = "tinystore-passed-on";

/// Runs `call`, which passes on to the home of a copy and blocks until the
/// home answers, on a thread of its own once the future is first polled, and
/// answers with what it returns. Once polled it runs to its end whether or
/// not anyone still awaits it, as a write does. A panic in `call` is resumed
/// where the future is awaited.
#[allow(dead_code, reason = "the calls of kv and blobs passed on to a copy's home run on it")]
pub(crate) async fn passed_on<T, F>(what: String, call: F) -> Result<T>
where
    F: FnOnce() -> Result<T> + Send + 'static,
    T: Send + 'static,
{
    refuse_inside_a_transaction(&what)?;
    let (answer, answered) = pending(what.clone());
    let started = own_thread(move || match catch_unwind(AssertUnwindSafe(call)) {
        Ok(outcome) => answer.send(outcome),
        Err(payload) => answer.panicked(payload),
    });
    if let Err(error) = started {
        return Err(Error::io(format!("{what}: a thread to wait for the home's answer on"), error));
    }
    answered.await
}

/// Takes what a call to a copy's home needs with `take`, in its turn in
/// `line` of the store's pool, after the work the line held before it, then
/// runs `call` with it as [`passed_on`] does, on a thread of its own: an
/// upload's commit on a copy, whose pieces are written in its line and whose
/// home's answer would hold a thread of the pool. What `take` took is the
/// thread's, so that work the line holds after it finds it gone.
#[cfg(feature = "blobs")]
pub(crate) fn passed_on_in_line<A, T, Take, Call>(
    pool: &Arc<Pool>,
    line: String,
    what: String,
    (take, call): (Take, Call),
) -> impl Future<Output = Result<T>> + Send + 'static + use<A, T, Take, Call>
where
    Take: FnOnce() -> Result<A> + Send + 'static,
    Call: FnOnce(A) -> Result<T> + Send + 'static,
    A: Send + 'static,
    T: Send + 'static,
{
    let pool = Arc::clone(pool);
    async move {
        refuse_inside_a_transaction(&what)?;
        let (answer, answered) = pending(what.clone());
        let job = Box::new(move || match catch_unwind(AssertUnwindSafe(take)) {
            Ok(Ok(taken)) => pass_on_taken(what, answer, move || call(taken)),
            Ok(Err(error)) => answer.send(Err(error)),
            Err(payload) => answer.panicked(payload),
        });
        pool.run_in_line(&line, job);
        answered.await
    }
}

/// Runs `call` on a thread of its own and answers with it, or answers why no
/// thread started.
#[cfg(feature = "blobs")]
fn pass_on_taken<T: Send + 'static>(
    what: String,
    answer: Answer<T>,
    call: impl FnOnce() -> Result<T> + Send + 'static,
) {
    let handed = Arc::new(std::sync::Mutex::new(Some(answer)));
    let answering = Arc::clone(&handed);
    let started = own_thread(move || {
        if let Some(answer) = crate::sync::lock(&answering).take() {
            answer_with(answer, call);
        }
    });
    // a thread that never started dropped its job, and left the answer here
    if let Err(error) = started
        && let Some(answer) = crate::sync::lock(&handed).take()
    {
        answer.send(Err(Error::io(format!("{what}: a thread to wait for the home's answer on"), error)));
    }
}

/// Starts `job` on a thread of its own, one of those that wait for a copy's
/// home.
#[allow(dead_code, reason = "the calls of kv and blobs passed on to a copy's home run on it")]
pub(crate) fn own_thread(job: impl FnOnce() + Send + 'static) -> std::io::Result<()> {
    std::thread::Builder::new().name(PASSED_ON.to_owned()).spawn(job).map(drop)
}

/// Runs `prepare` on the store's pool once the future is first polled, then
/// the work it gives with the bytes of the store's memory it asks for held:
/// an ingest's checks, then its write. The work runs at once where the bytes
/// fit, and otherwise waits for them as [`reserve`] does, in the memory's
/// line, on no thread, handed back to the pool once they fit: a write that
/// waits for memory never keeps a thread of the pool from the reads.
///
/// Once polled it runs to its end whether or not anyone still awaits it,
/// its wait for memory included, so that a write that was polled commits
/// (DUR-11). The wait fails `Limit` after the patience a blocking
/// reservation has, and `Closed` once the store closed. A panic in either
/// is resumed where the future is awaited.
#[allow(dead_code, reason = "metrics' awaited ingest holds its memory through it")]
pub(crate) fn holding<T, P, W>(
    pool: &Arc<Pool>,
    memory: &Arc<Memory>,
    what: String,
    prepare: P,
) -> impl Future<Output = Result<T>> + Send + 'static + use<T, P, W>
where
    P: FnOnce() -> Result<(u64, W)> + Send + 'static,
    W: FnOnce(Reservation) -> Result<T> + Send + 'static,
    T: Send + 'static,
{
    let (pool, memory) = (Arc::clone(pool), Arc::clone(memory));
    async move {
        refuse_inside_a_transaction(&what)?;
        let (answer, answered) = pending(what.clone());
        let handing = Arc::clone(&pool);
        pool.run(Box::new(move || match catch_unwind(AssertUnwindSafe(prepare)) {
            Ok(Ok((bytes, work))) => match memory.try_reserve(bytes, &what) {
                Ok(held) => answer_with(answer, || work(held)),
                Err(_) => {
                    let served = move |reserved: Result<Reservation>| match reserved {
                        Ok(held) => handing.run(Box::new(move || answer_with(answer, || work(held)))),
                        Err(refused) => answer.send(Err(refused)),
                    };
                    memory.reserve_then(bytes, &what, PATIENCE, served).stay();
                }
            },
            Ok(Err(refused)) => answer.send(Err(refused)),
            Err(payload) => answer.panicked(payload),
        }));
        answered.await
    }
}

/// Sends `answer` what `work` returns, or the panic it raised, which the
/// future resumes.
fn answer_with<T>(answer: Answer<T>, work: impl FnOnce() -> Result<T>) {
    match catch_unwind(AssertUnwindSafe(work)) {
        Ok(outcome) => answer.send(outcome),
        Err(payload) => answer.panicked(payload),
    }
}

/// Refuses a call that would hand its work to the pool from inside a
/// transaction this thread holds, as a program's `block_on` in a
/// transaction's closure would: the pool's thread would wait for the writer
/// this one holds, or read past what the transaction wrote.
pub(crate) fn refuse_inside_a_transaction(what: &str) -> Result<()> {
    if !crate::sqlite::in_a_transaction() {
        return Ok(());
    }
    Err(Error::invalid(format!(
        "{what}: an awaited call from inside a transaction would wait for the writer the transaction holds: \
         make it through the transaction, tx.with(&handle)"
    )))
}

#[cfg(test)]
#[path = "run_tests.rs"]
mod tests;