tinystore 0.5.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
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
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
//! Workers: a loop that claims a queue's jobs as they fall due and settles
//! them, and handler threads that run each one.
//!
//! The loop holds up to two jobs a handler, the one it runs and its next, or
//! one where the queue bounds what runs, and claims what it lacks in the same grouped write that settles the jobs its
//! handlers finished, so that a queue under load commits once for many jobs and
//! a handler never waits for a commit to start its next.

use std::collections::{HashMap, VecDeque};
use std::fmt;
use std::panic::{AssertUnwindSafe, catch_unwind};
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::{Arc, Condvar, Mutex, MutexGuard, PoisonError, Weak};
use std::thread::{self, JoinHandle};
use std::time::{Duration, Instant};

use serde::de::DeserializeOwned;

use super::alarm::{LONGEST_SLEEP, Read, Wake};
use super::claim::{self, CLAIM_BATCH, Claimed, Claiming, How, Lease, Settled};
use super::engine::Jobs;
use super::run::{Outcome, Run};
use super::state::QueueState;
use super::values;
use crate::clock::millis;
use crate::sqlite::sql_error;
use crate::{Error, ErrorKind, Result};

/// How long a lease holds a job its worker runs; the loop extends it every
/// half lease, so that it bounds how long a vanished worker keeps a job, not
/// how long a handler may take.
pub(crate) const LEASE: Duration = Duration::from_secs(30);

/// How long a stopping worker waits for its handlers past their timeout
/// before it lets them go.
const GRACE: Duration = Duration::from_secs(10);
/// How long a failed write waits to try again.
const RETRY_WRITE: Duration = Duration::from_secs(1);

const SPILLED_VALUE: &str = "select value from _tinystore_jobs_spilled where id = ?1";

/// What handlers hand back to their loop, and what wakes it.
#[derive(Default)]
pub(crate) struct Inbox {
    state: Mutex<InboxState>,
    changed: Condvar,
}

#[derive(Default)]
struct InboxState {
    /// Each job a handler finished, and how to settle it; none for a job a
    /// cancel took, which settles nothing.
    finished: Vec<(Arc<Lease>, Option<How>)>,
    woken: bool,
    stopping: bool,
}

impl Inbox {
    fn finish(&self, lease: Arc<Lease>, how: Option<How>) {
        self.lock().finished.push((lease, how));
        self.changed.notify_all();
    }

    fn stop(&self) {
        self.lock().stopping = true;
        self.changed.notify_all();
    }

    fn stopping(&self) -> bool {
        self.lock().stopping
    }

    fn take(&self) -> Vec<(Arc<Lease>, Option<How>)> {
        std::mem::take(&mut self.lock().finished)
    }

    /// Sleeps until a handler finishes or `sleep` passes: a stopping loop's
    /// wait for its handlers, which the stop it already saw does not cut short.
    fn wait_finished(&self, sleep: Duration) {
        let state = self.lock();
        let _ = self
            .changed
            .wait_timeout_while(state, sleep, |state| state.finished.is_empty())
            .unwrap_or_else(PoisonError::into_inner);
    }

    /// Sleeps until a handler finishes, the alarm is lowered, the worker
    /// stops, or `sleep` passes.
    fn wait(&self, sleep: Duration) {
        let state = self.lock();
        let (mut state, _) = self
            .changed
            .wait_timeout_while(state, sleep, |state| state.finished.is_empty() && !state.woken && !state.stopping)
            .unwrap_or_else(PoisonError::into_inner);
        state.woken = false;
    }

    fn lock(&self) -> MutexGuard<'_, InboxState> {
        self.state.lock().unwrap_or_else(PoisonError::into_inner)
    }
}

impl Wake for Inbox {
    fn wake(&self) {
        self.lock().woken = true;
        self.changed.notify_all();
    }
}

/// Where a worker's loop hands the jobs it claims: its handler threads, or a
/// client across the wire. Either answers each job through the worker's
/// inbox.
pub(crate) trait Hand: Send + Sync {
    /// Takes a job, answered later through the inbox; or answers it at once,
    /// none for one a cancel took, which settles nothing.
    fn hand(&self, lease: Arc<Lease>) -> std::result::Result<(), Option<How>>;

    /// Takes nothing more, and answers the jobs it was handed and had not
    /// started, which go back uncounted.
    fn close(&self) -> Vec<Arc<Lease>>;
}

/// The claimed jobs on their way to the handlers, which read their values.
#[derive(Default)]
struct Feed {
    state: Mutex<(VecDeque<Arc<Lease>>, bool)>,
    ready: Condvar,
}

impl Feed {
    fn push(&self, lease: Arc<Lease>) {
        self.lock().0.push_back(lease);
        self.ready.notify_one();
    }

    /// The next job for a handler; none once the feed is closed and empty.
    fn take(&self) -> Option<Arc<Lease>> {
        let state = self.lock();
        let mut state = self
            .ready
            .wait_while(state, |(jobs, closed)| jobs.is_empty() && !*closed)
            .unwrap_or_else(PoisonError::into_inner);
        state.0.pop_front()
    }

    /// Closes the feed and answers the jobs no handler started.
    fn close(&self) -> Vec<Arc<Lease>> {
        let mut state = self.lock();
        state.1 = true;
        let unstarted = state.0.drain(..).collect();
        drop(state);
        self.ready.notify_all();
        unstarted
    }

    fn lock(&self) -> MutexGuard<'_, (VecDeque<Arc<Lease>>, bool)> {
        self.state.lock().unwrap_or_else(PoisonError::into_inner)
    }
}

impl Hand for Feed {
    fn hand(&self, lease: Arc<Lease>) -> std::result::Result<(), Option<How>> {
        self.push(lease);
        Ok(())
    }

    fn close(&self) -> Vec<Arc<Lease>> {
        Feed::close(self)
    }
}

/// One worker's loop: the jobs it holds, which its handlers run, and the
/// settlements it has yet to write.
struct Loop {
    jobs: Arc<Jobs>,
    queue: Arc<QueueState>,
    /// Jobs it may hold: running, or claimed for a handler still busy.
    hold: usize,
    inbox: Arc<Inbox>,
    hand: Arc<dyn Hand>,
    holding: HashMap<i64, Arc<Lease>>,
    pending: Vec<(Arc<Lease>, How)>,
    /// Jobs handed to the handlers and not yet finished.
    in_hand: usize,
    /// Whether it returns once nothing is due and nothing is held: `run_due`.
    until_idle: bool,
    /// Jobs its handlers finished, for `run_due`'s answer.
    finished: usize,
}

/// What one write of the loop claimed, and when the queue next needs a claim
/// when it took fewer than it wanted.
#[derive(Default)]
struct ClaimResult {
    leases: Vec<Lease>,
    abandoned: Vec<i64>,
    /// Of the abandoned, those failed for good.
    failed: usize,
    full: bool,
    next: Option<i64>,
}

impl Loop {
    fn new(jobs: Arc<Jobs>, queue: Arc<QueueState>, local: usize, inbox: Arc<Inbox>, hand: Arc<dyn Hand>) -> Loop {
        let hold = if queue.policy.bounded() { local } else { local * 2 };
        Loop {
            jobs,
            queue,
            hold,
            inbox,
            hand,
            holding: HashMap::new(),
            pending: Vec::new(),
            in_hand: 0,
            until_idle: false,
            finished: 0,
        }
    }

    /// Claims and settles until the worker stops, the store closes, or, until
    /// idle, nothing more may start and nothing is held; then gives back what
    /// no handler started, waits for the handlers under way and writes what
    /// they answered.
    fn run(&mut self) -> Result<usize> {
        let looped = self.cycle();
        for lease in self.hand.close() {
            self.in_hand -= 1;
            self.pending.push((lease, How::GiveBack));
        }
        let drained = self.drain();
        looped.and(drained).map(|()| self.finished)
    }

    /// Waits for the handlers under way, each bounded by its timeout and a
    /// grace, writing what they answer as it comes and extending the leases
    /// they still run on, so that no other claim takes a job a handler runs.
    fn drain(&mut self) -> Result<()> {
        let deadline = Instant::now() + self.queue.policy.timeout + GRACE;
        loop {
            self.gather();
            let now = self.jobs.now();
            let left = deadline.saturating_duration_since(Instant::now());
            if self.in_hand == 0 || left.is_zero() {
                return match self.write(now, 0) {
                    Err(error) if self.stopped_by_close(&error) => Ok(()),
                    written => written.map(drop),
                };
            }
            self.extend_due(now);
            let sleep = match self.write(now, 0) {
                Ok(_) => self.next_extension(now).map_or(LONGEST_SLEEP, |at| span(at - now)),
                Err(error) if self.stopped_by_close(&error) => return Ok(()),
                Err(error) if error.kind() == ErrorKind::Closed => return Err(error),
                Err(error) => {
                    self.queue.writes.observe(&self.queue.name, &error.to_string());
                    RETRY_WRITE
                }
            };
            self.inbox.wait_finished(sleep.min(left).min(LONGEST_SLEEP));
        }
    }

    /// Whether `error` is the store's close reaching a worker's write, which
    /// ends the worker as a stop does. `run_due` is a call, and fails
    /// `closed`; so does a write a close did not cause.
    fn stopped_by_close(&self, error: &Error) -> bool {
        !self.until_idle && error.kind() == ErrorKind::Closed && self.jobs.closed()
    }

    fn cycle(&mut self) -> Result<()> {
        loop {
            self.gather();
            if self.inbox.stopping() || self.jobs.closed() {
                return Ok(());
            }
            let now = self.jobs.now();
            self.extend_due(now);
            let (want, read, rated) = self.wanted(now);
            let result = self.write(now, want);
            self.answer(read, result.as_ref().ok());
            let result = match result {
                Ok(result) => result,
                Err(error) if self.stopped_by_close(&error) => return Ok(()),
                Err(error) if self.until_idle || error.kind() == ErrorKind::Closed => return Err(error),
                Err(error) => {
                    self.queue.writes.observe(&self.queue.name, &error.to_string());
                    self.inbox.wait(RETRY_WRITE);
                    continue;
                }
            };
            let full = want > 0 && result.full;
            self.dispatch(result.leases);
            if self.until_idle && self.idle(now, rated) {
                return Ok(());
            }
            if !full {
                self.wait(now, rated);
            }
        }
    }

    /// How many jobs to claim at `now`: as many as handlers are free, when the
    /// alarm says a job may be due. `rated` is when the queue's rate next lets
    /// a job start, when it lets none start now.
    fn wanted(&self, now: i64) -> (usize, Option<Read>, Option<i64>) {
        let finishing = self.pending.iter().filter(|(_, how)| !matches!(how, How::Extend { .. })).count();
        let free = (self.hold + finishing).saturating_sub(self.holding.len());
        if free == 0 {
            return (0, None, None);
        }
        let room = match &self.queue.rate {
            Some(rate) => match rate.room(now) {
                (0, next) => return (0, None, next),
                (room, _) => room,
            },
            None => usize::MAX,
        };
        match self.queue.alarm.rung(now) {
            Some(read) => (free.min(CLAIM_BATCH).min(room), Some(read), None),
            None => (0, None, None),
        }
    }

    /// Ends the alarm read a claim made: a claim that found fewer jobs than it
    /// wanted knows when the queue is next due, a full or failed one does not.
    fn answer(&self, read: Option<Read>, result: Option<&ClaimResult>) {
        let Some(read) = read else {
            return;
        };
        match result {
            Some(result) if !result.full => self.queue.alarm.set(read, result.next),
            _ => self.queue.alarm.forget(read),
        }
    }

    /// Takes what the handlers finished, without waiting.
    fn gather(&mut self) {
        for (lease, how) in self.inbox.take() {
            self.in_hand -= 1;
            self.finished += 1;
            match how {
                Some(how) => self.pending.push((lease, how)),
                None => self.let_go(&lease),
            }
        }
    }

    /// Extends the leases held past half their length, so that a long handler
    /// keeps its job and a vanished process loses it within a lease.
    fn extend_due(&mut self, now: i64) {
        let half = millis(LEASE) / 2;
        for (id, lease) in &self.holding {
            let settling = self.pending.iter().any(|(pending, _)| pending.id == *id);
            if !settling && !lease.settled() && lease.until() - now <= half {
                self.pending.push((Arc::clone(lease), How::Extend { until: now + millis(LEASE) }));
            }
        }
    }

    /// Settles what is pending and claims up to `want` jobs in one grouped
    /// write. A settlement whose lease another claim has taken is dropped.
    fn write(&mut self, now: i64, want: usize) -> Result<ClaimResult> {
        if self.pending.is_empty() && want == 0 {
            return Ok(ClaimResult::default());
        }
        let pending = self.pending.clone();
        let seen = pending.iter().any(|(_, how)| !matches!(how, How::Extend { .. }));
        let queue = Arc::clone(&self.queue);
        let until = now + millis(LEASE);
        let (settled, result) = self.jobs.file().write(0, move |tx| {
            let mut settled = Vec::with_capacity(pending.len());
            for (lease, how) in &pending {
                settled.push(claim::settle(tx, &queue, lease, how, now)?);
            }
            if want == 0 {
                return Ok((settled, ClaimResult::default()));
            }
            let claiming = Claiming { queue: &queue, now, until, limit: want };
            let Claimed { leases, abandoned, failed, more } = claim::claim(tx, claiming)?;
            let full = leases.len() + abandoned.len() == want || more;
            let next = if full { None } else { claim::next_claim(tx, &claiming)? };
            Ok((settled, ClaimResult { leases, abandoned, failed, full, next }))
        })?;
        self.settled(settled, now);
        self.jobs.measure_claims(&self.queue.name, &result.leases, now);
        if seen || !result.leases.is_empty() || !result.abandoned.is_empty() {
            self.queue.watchers.changed();
        }
        for _ in 0..result.failed {
            self.queue.failures.observe(&self.queue.name, "its attempts ended without a settlement");
        }
        Ok(result)
    }

    /// Follows in memory what the written settlements changed.
    fn settled(&mut self, settled: Vec<Settled>, now: i64) {
        let mut room = false;
        for ((lease, how), settled) in std::mem::take(&mut self.pending).into_iter().zip(settled) {
            self.jobs.measure_settled(&self.queue.name, &lease, &how, &settled, now);
            let extension = matches!(how, How::Extend { .. });
            lease.apply(&settled, extension);
            if let Some(due) = settled.due {
                self.queue.alarm.lower(due);
            }
            if let Some(failed) = &settled.failed {
                self.queue.failures.observe(&self.queue.name, failed);
            }
            if settled.lost && !lease.cancelled() {
                self.queue
                    .lost
                    .observe(&self.queue.name, "a lease ended while its handler ran, and another claim took the job");
            }
            if !extension || settled.lost {
                self.let_go(&lease);
                room = true;
            }
        }
        if room {
            self.queue.room_made(now);
        }
    }

    fn let_go(&mut self, lease: &Lease) {
        self.holding.remove(&lease.id);
        self.queue.release(lease);
    }

    /// Hands claimed jobs to the handlers, which read their values.
    fn dispatch(&mut self, leases: Vec<Lease>) {
        for lease in leases {
            let lease = Arc::new(lease);
            self.holding.insert(lease.id, Arc::clone(&lease));
            self.queue.hold(&lease);
            match self.hand.hand(Arc::clone(&lease)) {
                Ok(()) => self.in_hand += 1,
                Err(Some(how)) => self.pending.push((lease, how)),
                Err(None) => self.let_go(&lease),
            }
        }
    }

    /// Whether the loop holds nothing, has nothing to write, and no job is due,
    /// or the queue's rate lets none start until `rated`.
    fn idle(&self, now: i64, rated: Option<i64>) -> bool {
        self.holding.is_empty() && self.pending.is_empty() && (rated.is_some() || !self.queue.alarm.due(now))
    }

    /// Sleeps until a handler finishes, the alarm rings or is lowered, a held
    /// lease needs extending, or the worker stops. With every handler busy the
    /// alarm does not set the time, and with the rate holding it back only the
    /// time the rate names does.
    fn wait(&self, now: i64, rated: Option<i64>) {
        let mut sleep = match rated {
            Some(rated) => span(rated - now).min(LONGEST_SLEEP),
            None if self.holding.len() >= self.hold => LONGEST_SLEEP,
            None => self.queue.alarm.sleep(now),
        };
        if let Some(first) = self.next_extension(now) {
            sleep = sleep.min(span(first - now));
        }
        if !sleep.is_zero() {
            self.inbox.wait(sleep);
        }
    }

    /// When the first lease held is due to be extended: half a lease before it
    /// ends.
    fn next_extension(&self, now: i64) -> Option<i64> {
        let half = millis(LEASE) / 2;
        let first = self.holding.values().filter(|lease| !lease.settled()).map(|lease| lease.until() - half).min();
        first.map(|first| first.max(now))
    }
}

fn span(ms: i64) -> Duration {
    Duration::from_millis(u64::try_from(ms).unwrap_or(0))
}

/// Runs a handler on each job the feed hands over, until it closes.
fn serve<V, F, R, E>(jobs: &Arc<Jobs>, queue: &Arc<QueueState>, feed: &Feed, inbox: &Inbox, handler: &F)
where
    V: DeserializeOwned,
    F: Fn(V, &Run) -> std::result::Result<R, E>,
    R: Into<Outcome>,
    E: fmt::Display,
{
    while let Some(lease) = feed.take() {
        let how = run_one(jobs, queue, &lease, handler);
        inbox.finish(lease, how);
    }
}

/// Reads a job's value, runs the handler on it and says how to settle it;
/// none when a cancel took the job, which then settles nothing.
fn run_one<V, F, R, E>(jobs: &Arc<Jobs>, queue: &Arc<QueueState>, lease: &Arc<Lease>, handler: &F) -> Option<How>
where
    V: DeserializeOwned,
    F: Fn(V, &Run) -> std::result::Result<R, E>,
    R: Into<Outcome>,
    E: fmt::Display,
{
    if lease.cancelled() {
        return None;
    }
    let value = match value_or_how::<V>(jobs, lease) {
        Ok(value) => value,
        Err(how) => return Some(how),
    };
    if !lease.begin(jobs.now()) {
        return None;
    }
    queue.watchers.changed();
    let run = Run::new(Arc::clone(lease), Arc::clone(queue), Arc::clone(jobs));
    let ran = catch_unwind(AssertUnwindSafe(|| handler(value, &run)));
    answered(jobs, queue, lease, ran.map(|ran| ran.map(Into::into).map_err(|error| error.to_string())))
}

/// How a handler's answer settles its job, a panic's among them: none when a
/// cancel took the job, whose handler's answer settles nothing.
pub(crate) fn answered(
    jobs: &Jobs,
    queue: &QueueState,
    lease: &Lease,
    ran: std::thread::Result<std::result::Result<Outcome, String>>,
) -> Option<How> {
    if lease.cancelled() {
        return None;
    }
    Some(match ran {
        Ok(Ok(outcome)) => outcome.how(jobs.now()),
        Ok(Err(cause)) => How::Retry { at: None, cause },
        Err(panic) => {
            let message = panic_message(&*panic);
            queue.panics.observe(&queue.name, &message);
            How::Retry { at: None, cause: format!("the handler panicked: {message}") }
        }
    })
}

/// A claimed job's value, or how to settle a job whose value cannot be read:
/// failed for good when it no longer reads as the queue's type, retried when
/// the read failed.
pub(crate) fn value_or_how<V: DeserializeOwned>(jobs: &Jobs, lease: &Lease) -> std::result::Result<V, How> {
    read_value::<V>(jobs, lease).map_err(|error| match error.kind() {
        ErrorKind::Invalid => How::Fail { cause: error.to_string() },
        _ => How::Retry { at: None, cause: error.to_string() },
    })
}

/// A claimed job's value, read from its own row outside the writer when it
/// spilled; one that no longer reads as the queue's type is `invalid`.
pub(crate) fn read_value<V: DeserializeOwned>(jobs: &Jobs, lease: &Lease) -> Result<V> {
    let encoded = match (&lease.inline, lease.spill) {
        (_, Some(spill)) => jobs.file().read(|c| {
            c.prepare_cached(SPILLED_VALUE)
                .and_then(|mut select| select.query_row([spill], |row| row.get::<_, Vec<u8>>(0)))
                .map_err(|error| sql_error("the value a claimed job spilled", error))
        })?,
        (Some(inline), None) => inline.clone(),
        (None, None) => Vec::new(),
    };
    values::decode(&encoded)
}

fn panic_message(panic: &(dyn std::any::Any + Send)) -> String {
    panic
        .downcast_ref::<&str>()
        .map(|message| (*message).to_owned())
        .or_else(|| panic.downcast_ref::<String>().cloned())
        .unwrap_or_else(|| "a panic without a message".to_owned())
}

/// A worker running a queue's handlers on threads the store started for it.
/// Dropped, it keeps running until the store closes; `stop` stops it.
#[must_use = "a worker runs until it is stopped or the store closes; keep it to stop it"]
pub struct Worker {
    running: Arc<Running>,
}

/// What a worker's threads share with the handle and the engine that stop it.
pub(crate) struct Running {
    inbox: Arc<Inbox>,
    hand: Arc<dyn Hand>,
    looping: Mutex<Option<JoinHandle<()>>>,
    handlers: Mutex<Vec<JoinHandle<()>>>,
    alive: Alive,
    stopped: AtomicBool,
}

/// Handler threads that have not yet ended, and the signal each gives as it
/// ends.
type Alive = Arc<(Mutex<usize>, Condvar)>;

impl Running {
    pub(crate) fn stopped(&self) -> bool {
        self.stopped.load(Ordering::SeqCst)
    }

    /// Takes no job more, waits for the handlers under way, each bounded by
    /// its timeout, and writes what they answered. A handler that never
    /// returns is let go after its timeout and a grace, its job given back
    /// when its lease ends.
    pub(crate) fn stop(&self) {
        self.stopped.store(true, Ordering::SeqCst);
        self.inbox.stop();
        let looping = self.looping.lock().unwrap_or_else(PoisonError::into_inner).take();
        if let Some(looping) = looping.filter(|handle| handle.thread().id() != thread::current().id()) {
            let _ = looping.join();
        }
        self.hand.close();
        let (alive, ended) = &*self.alive;
        let alive = alive.lock().unwrap_or_else(PoisonError::into_inner);
        let (alive, _) =
            ended.wait_timeout_while(alive, GRACE, |alive| *alive > 0).unwrap_or_else(PoisonError::into_inner);
        if *alive == 0 {
            drop(alive);
            for handle in self.handlers.lock().unwrap_or_else(PoisonError::into_inner).drain(..) {
                if handle.thread().id() != thread::current().id() {
                    let _ = handle.join();
                }
            }
        }
    }
}

impl Worker {
    /// Takes no job more and waits for the handlers under way, each bounded by
    /// its queue's timeout; the store's close stops every worker so.
    pub fn stop(&self) {
        self.running.stop();
    }
}

impl fmt::Debug for Worker {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str("Worker")
    }
}

/// Starts a worker of `local` handlers on threads of the store's.
pub(crate) fn start<V, F, R, E>(jobs: &Arc<Jobs>, queue: &Arc<QueueState>, local: usize, handler: F) -> Result<Worker>
where
    V: DeserializeOwned + 'static,
    F: Fn(V, &Run) -> std::result::Result<R, E> + Send + Sync + 'static,
    R: Into<Outcome>,
    E: fmt::Display,
{
    if jobs.closed() {
        return Err(Error::closed(queue.describe()));
    }
    let (inbox, feed) = (Arc::new(Inbox::default()), Arc::new(Feed::default()));
    queue.alarm.subscribe(Arc::downgrade(&inbox) as Weak<dyn Wake>);
    let handler = Arc::new(handler);
    let alive = Arc::new((Mutex::new(0usize), Condvar::new()));
    let mut handlers = Vec::with_capacity(local);
    for at in 0..local {
        *alive.0.lock().unwrap_or_else(PoisonError::into_inner) += 1;
        let (jobs, queue_of, fed, inbox, handler, count) = (
            Arc::clone(jobs),
            Arc::clone(queue),
            Arc::clone(&feed),
            Arc::clone(&inbox),
            Arc::clone(&handler),
            Arc::clone(&alive),
        );
        let name = format!("tinystore-jobs-{}-{at}", queue.name);
        let spawned = thread::Builder::new().name(name).spawn(move || {
            serve(&jobs, &queue_of, &fed, &inbox, &*handler);
            let (alive, ended) = &*count;
            *alive.lock().unwrap_or_else(PoisonError::into_inner) -= 1;
            ended.notify_all();
        });
        match spawned {
            Ok(handle) => handlers.push(handle),
            Err(error) => {
                *alive.0.lock().unwrap_or_else(PoisonError::into_inner) -= 1;
                feed.close();
                return Err(Error::io(format!("{}: a handler's thread", queue.describe()), error));
            }
        }
    }
    let hand: Arc<dyn Hand> = feed;
    let looped = Loop::new(Arc::clone(jobs), Arc::clone(queue), local, inbox, hand);
    let running = run_loop(looped, (handlers, alive), None)?;
    Ok(Worker { running })
}

/// Where the answers for the jobs a client's worker holds go, or a worker's
/// whose handlers an awaited future runs: its loop's inbox. Each job handed
/// over is answered once, by its handler or by a cancel that took it.
#[derive(Clone, Default)]
#[cfg(any(feature = "wire", feature = "async"))]
pub(crate) struct Answers(Arc<Inbox>);

#[cfg(any(feature = "wire", feature = "async"))]
impl Answers {
    /// How the job's run ended; none for one a cancel took, which settles
    /// nothing.
    pub(crate) fn answer(&self, lease: Arc<Lease>, how: Option<How>) {
        self.0.finish(lease, how);
    }
}

/// A worker whose handlers are a client's, or an awaited future's: its loop
/// runs on a thread of the store's and hands its jobs to `hand`, and their
/// answers come back through its [`Answers`].
#[cfg(any(feature = "wire", feature = "async"))]
pub(crate) struct Remote {
    running: Arc<Running>,
}

#[cfg(any(feature = "wire", feature = "async"))]
impl Remote {
    /// Takes no job more and returns: the loop gives back the jobs it holds
    /// and has not handed over, and writes the answers that still come.
    pub(crate) fn halt(&self) {
        self.running.inbox.stop();
    }

    /// Halts the worker and waits for its loop to write what was answered; a
    /// job handed and not yet answered holds the stop up to the queue's
    /// timeout and a grace.
    #[cfg(feature = "wire")]
    pub(crate) fn stop(&self) {
        self.running.stop();
    }

    /// Lets the engine forget a worker whose loop ended: its close has
    /// nothing left to stop, and a program that runs `run_due` in a loop
    /// would otherwise grow the engine's list of workers until it closes.
    #[cfg(feature = "async")]
    pub(crate) fn ended(&self) {
        self.running.stopped.store(true, Ordering::SeqCst);
    }
}

#[cfg(any(feature = "wire", feature = "async"))]
impl fmt::Debug for Remote {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str("Remote")
    }
}

/// What a client's worker, or an awaited one, asks of its loop.
#[cfg(any(feature = "wire", feature = "async"))]
pub(crate) struct RemoteWork {
    /// The client's handlers.
    pub(crate) local: usize,
    /// Whether the loop ends once nothing is due and nothing is held: `runDue`.
    pub(crate) until_idle: bool,
    pub(crate) answers: Answers,
    pub(crate) hand: Arc<dyn Hand>,
    pub(crate) ended: Ended,
}

/// Hears, on the loop's thread, how a loop ended: the jobs its handlers
/// finished, or why it stopped.
pub(crate) type Ended = Box<dyn FnOnce(Result<usize>) + Send>;

#[cfg(any(feature = "wire", feature = "async"))]
pub(crate) fn start_remote(jobs: &Arc<Jobs>, queue: &Arc<QueueState>, work: RemoteWork) -> Result<Remote> {
    if jobs.closed() {
        return Err(Error::closed(queue.describe()));
    }
    let RemoteWork { local, until_idle, answers, hand, ended } = work;
    let inbox = Arc::clone(&answers.0);
    queue.alarm.subscribe(Arc::downgrade(&inbox) as Weak<dyn Wake>);
    let mut looped = Loop::new(Arc::clone(jobs), Arc::clone(queue), local, inbox, hand);
    looped.until_idle = until_idle;
    let running = run_loop(looped, (Vec::new(), Alive::default()), Some(ended))?;
    Ok(Remote { running })
}

/// Starts a worker's loop on a thread of the store's, which the engine stops
/// at close; `ended` hears how the loop ended.
///
/// The engine keeps the worker before its loop starts: a close under way then
/// either stops it or refuses it, and never closes the file under a loop it
/// did not see (a schedule's worker spawned as its store closed wrote there).
fn run_loop(
    mut looped: Loop,
    (handlers, alive): (Vec<JoinHandle<()>>, Alive),
    ended: Option<Ended>,
) -> Result<Arc<Running>> {
    let (jobs, queue) = (Arc::clone(&looped.jobs), Arc::clone(&looped.queue));
    let (inbox, hand) = (Arc::clone(&looped.inbox), Arc::clone(&looped.hand));
    let running = Arc::new(Running {
        inbox,
        hand: Arc::clone(&hand),
        looping: Mutex::new(None),
        handlers: Mutex::new(handlers),
        alive,
        stopped: AtomicBool::new(false),
    });
    if !jobs.keep_worker(&running) {
        hand.close();
        return Err(Error::closed(queue.describe()));
    }
    let describe = queue.describe();
    let looping = thread::Builder::new()
        .name(format!("tinystore-jobs-{}", queue.name))
        .spawn(move || {
            let ran = crate::sqlite::own_work(|| looped.run());
            if let Err(error) = &ran {
                tracing::warn!(target: "tinystore", queue = %describe, %error, "a worker stopped on an error");
            }
            if let Some(ended) = ended {
                ended(ran);
            }
        })
        .map_err(|error| {
            hand.close();
            running.stopped.store(true, Ordering::SeqCst);
            Error::io(format!("{}: a worker's thread", queue.describe()), error)
        })?;
    // a close that stopped the worker meanwhile found no loop to wait for: it ends on its own
    *running.looping.lock().unwrap_or_else(PoisonError::into_inner) = Some(looping);
    Ok(running)
}

/// Runs what is due when it starts and what falls due meanwhile on `local`
/// handlers of this call's own, then returns how many jobs ran.
pub(crate) fn run_due<V, F, R, E>(jobs: &Arc<Jobs>, queue: &Arc<QueueState>, local: usize, handler: &F) -> Result<usize>
where
    V: DeserializeOwned,
    F: Fn(V, &Run) -> std::result::Result<R, E> + Sync,
    R: Into<Outcome>,
    E: fmt::Display,
{
    let (inbox, feed) = (Arc::new(Inbox::default()), Arc::new(Feed::default()));
    queue.alarm.subscribe(Arc::downgrade(&inbox) as Weak<dyn Wake>);
    thread::scope(|scope| {
        for _ in 0..local {
            scope.spawn(|| serve(jobs, queue, &feed, &inbox, handler));
        }
        let hand: Arc<dyn Hand> = Arc::clone(&feed) as Arc<dyn Hand>;
        let mut looped = Loop::new(Arc::clone(jobs), Arc::clone(queue), local, Arc::clone(&inbox), hand);
        looped.until_idle = true;
        let ran = looped.run();
        feed.close();
        ran
    })
}